clo_pos_closing.sql 3.6 KB

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