clo_pos_closing.sql 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. -- truncate table dbr_pos_closing;
  2. drop procedure if exists clo_pos_closing;
  3. DELIMITER //
  4. create procedure clo_pos_closing( _curr_date varchar(8), _branch int, _user int)
  5. SQL SECURITY INVOKER
  6. BEGIN
  7. Declare _sales_day decimal(20,4) default "0.0000";
  8. Declare _ccard_day decimal(20,4) default "0.0000";
  9. Declare _cash_day decimal(20,4) default "0.0000";
  10. Declare _coupon_day decimal(20,4) default "0.0000";
  11. Declare _reward_day decimal(20,4) default "0.0000";
  12. Declare _discount_day decimal(20,4) default "0.0000";
  13. Declare _sorder_cnt int;
  14. Declare _sorder_new int;
  15. Declare _revisit_cnt int;
  16. Declare _sales_month decimal(20,4) default "0.0000";
  17. Declare _ccard_month decimal(20,4) default "0.0000";
  18. Declare _cash_month decimal(20,4) default "0.0000";
  19. Declare _coupon_month decimal(20,4) default "0.0000";
  20. Declare _reward_month decimal(20,4) default "0.0000";
  21. Declare _discount_month decimal(20,4) default "0.0000";
  22. Declare _not_found boolean;
  23. Declare _sales_day_cur cursor for
  24. select
  25. sum(sorder_sum), sum(ccard_amt), sum(cash_amt), sum(coupon_amt), sum(reward_amt),
  26. sum(discount_amt)
  27. from
  28. dbr_sorder as mx
  29. inner join
  30. ( select
  31. dbr_sorder.id, dbr_company.company_name, birth_date, count(sorder_no) as visit_cnt
  32. from
  33. dbr_company
  34. inner join dbr_sorder on dbr_company.id = dbr_sorder.buyer_id
  35. group by company_name) as com
  36. on com.id = mx.id
  37. inner join dbr_sorder_bd on mx.id = dbr_sorder_bd.sorder_id
  38. where sorder_date between _curr_date and _curr_date
  39. and mx.branch_id = _branch;
  40. Declare _sales_month_cur cursor for
  41. select
  42. sum(sorder_sum), sum(ccard_amt), sum(cash_amt), sum(coupon_amt), sum(reward_amt),
  43. sum(discount_amt)
  44. from
  45. dbr_sorder as mx
  46. inner join
  47. ( select
  48. dbr_sorder.id, dbr_company.company_name, birth_date, count(sorder_no) as visit_cnt
  49. from
  50. dbr_company
  51. inner join dbr_sorder on dbr_company.id = dbr_sorder.buyer_id
  52. group by company_name) as com
  53. on com.id = mx.id
  54. inner join dbr_sorder_bd on mx.id = dbr_sorder_bd.sorder_id
  55. where sorder_date between _first_date(_curr_date) and _curr_date
  56. and mx.branch_id = _branch;
  57. Declare _visit_day_cur cursor for
  58. select
  59. sum(if(is_first_order = '1', 1, 0)), sum(if(is_first_order <> '1', 1, 0))
  60. from dbr_sorder as mx
  61. where sorder_date between _curr_date and _curr_date
  62. and mx.branch_id = _branch;
  63. Declare continue handler for
  64. not found set _not_found = true;
  65. open _sales_day_cur;
  66. loop_sales_day: loop
  67. fetch _sales_day_cur into _sales_day, _ccard_day, _cash_day, _coupon_day, _reward_day,
  68. _discount_day;
  69. if _not_found then leave loop_sales_day; end if;
  70. end loop loop_sales_day;
  71. close _sales_day_cur;
  72. open _sales_month_cur;
  73. loop_month_day: loop
  74. fetch _sales_month_cur into _sales_month, _ccard_month, _cash_month, _coupon_month, _reward_month,
  75. _discount_month;
  76. if _not_found then leave loop_month_day; end if;
  77. end loop loop_month_day;
  78. close _sales_month_cur;
  79. open _visit_day_cur;
  80. loop_visit_day: loop
  81. fetch _visit_day_cur into _sorder_new, _revisit_cnt;
  82. if _not_found then leave loop_visit_day; end if;
  83. end loop loop_visit_day;
  84. close _visit_day_cur;
  85. delete from dbr_pos_closing
  86. where branch_id = branch_id and closing_date = _curr_date and user_id = _user;
  87. insert into dbr_pos_closing
  88. ( branch_id, closing_date, user_id, sales_day, ccard_day,
  89. cash_day, reward_day, discount_day, sorder_new, revisit_cnt,
  90. sales_month, ccard_month, cash_month, reward_month, discount_month
  91. )
  92. values ( _branch, _curr_date, _user, _sales_day, _ccard_day,
  93. _cash_day, _reward_day, _discount_day, _sorder_new, _revisit_cnt,
  94. _sales_month, _ccard_month, _cash_month, _reward_month, _discount_month
  95. );
  96. END; //
  97. DELIMITER ;