pl1_reward_bd_ledger_detail.sql 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. -- truncate table zzz_log;
  2. truncate table dbt_list_type1;
  3. truncate table dbt_list_sum;
  4. drop procedure if exists pl1_reward_bd_ledger_detail;
  5. DELIMITER //
  6. create procedure pl1_reward_bd_ledger_detail( _listToken varchar(21), _sdate varchar(8), _edate varchar(8),
  7. _s1 varchar(32), _e1 varchar(32), _s2 varchar(32), _e2 varchar(32),
  8. _s3 varchar(32), _e3 varchar(32), _s4 varchar(32), _e4 varchar(32),
  9. _where varchar(386), _having varchar(386), _orderby varchar(64),
  10. _branch int, _storage int, _target int)
  11. SQL SECURITY INVOKER
  12. BEGIN
  13. Declare _sbal_date varchar(8);
  14. Declare _ebal_date varchar(8);
  15. Declare _first_bal decimal(20,4);
  16. Declare _first_bad_bal decimal(20,4);
  17. Declare _first_bal_caption varchar(20);
  18. Declare _first_id int;
  19. Declare _last_id int;
  20. Declare _line_id int;
  21. Declare _line_badrwd_status varchar(64);
  22. Declare _line_in decimal(20,4) default "0.0000";
  23. Declare _line_out decimal(20,4) default "0.0000";
  24. Declare _line_bal decimal(20,4) default "0.0000";
  25. Declare _line_bad_bal decimal(20,4) default "0.0000";
  26. Declare _in_sum decimal(20,4) default "0.0000";
  27. Declare _out_sum decimal(20,4) default "0.0000";
  28. Declare _company_name varchar(64);
  29. Declare _full_name varchar(96);
  30. Declare _item_code varchar(21);
  31. Declare _item_sub_name varchar(128);
  32. Declare _not_found boolean;
  33. Declare _line_cur cursor for -- c5:badrwd_status
  34. select t_id, d3, d4 from dbt_list_type1 where list_token = _listToken
  35. order by order_by asc;
  36. Declare continue handler for
  37. not found set _not_found = true;
  38. if _is_first_date(_sdate) then
  39. set _sbal_date = '19000101';
  40. set _ebal_date = '19000101';
  41. else
  42. set _sbal_date = _sdate;
  43. set _ebal_date = _edate;
  44. end if;
  45. -- insert for first_bal
  46. select ifnull(last_mbal.bal_amt,0) + ifnull(rwd_bal.reward_amt,0) ,'이월잔액'
  47. into _first_bal, _first_bal_caption
  48. from
  49. dbr_company as mxv
  50. left join
  51. ( select buyer_id, bal_amt
  52. from dbr_bal_reward
  53. where yyyy_mm = _prev_month(_sdate) and branch_id = _branch
  54. ) as last_mbal
  55. on mxv.id = last_mbal.buyer_id
  56. left join
  57. ( select rwd.buyer_id, sum(reward_amt*reward_status) AS reward_amt
  58. from dbr_reward_bd as rwd
  59. inner join dbr_deal_type as deal on deal.id = rwd.deal_type_id
  60. where avail_date between _first_date(_sbal_date) and _prev_date(_ebal_date)
  61. and branch_id = _branch
  62. group by rwd.buyer_id
  63. ) as rwd_bal
  64. on mxv.id = rwd_bal.buyer_id
  65. where mxv.id = _target;
  66. insert into dbt_list_type1 ( created_on, list_token, c3, d5, order_by)
  67. values ( unix_timestamp(), _listToken, _first_bal_caption, _first_bal, '19000101');
  68. -- insert for reward_bd
  69. insert into dbt_list_type1
  70. ( created_on, list_token, c1, c2, c3, d1, d2, d3, d4, order_by )
  71. select unix_timestamp(), _listToken, avail_date, deal_code,
  72. concat( add_msg,' / ', item_name ),
  73. sobd.sorder_qty, sobd.sorder_sum, rwd.reward_amt*cast((reward_status+1)/2 as int),
  74. rwd.reward_amt*cast(-1*(reward_status-1)/2 as int),
  75. concat( avail_date, '-', -1*reward_status,'-', bd_id)
  76. from dbr_reward_bd as rwd
  77. inner join dbr_deal_type as deal on deal.id = rwd.deal_type_id
  78. left join dbr_sorder_bd as sobd on sobd.id = rwd.bd_id
  79. left join dbr_item as itm on sobd.item_id = itm.id
  80. where avail_date between _sdate and _edate and branch_id = _branch and rwd.buyer_id = _target;
  81. -- compute line_bal
  82. set _line_bal = _first_bal;
  83. set _line_bad_bal = _first_bad_bal;
  84. open _line_cur;
  85. loop1: loop
  86. fetch _line_cur into _line_id, _line_in, _line_out;
  87. if _not_found then leave loop1; end if;
  88. set _in_sum = _in_sum + ifnull(_line_in,0);
  89. set _out_sum = _out_sum + ifnull(_line_out,0);
  90. set _line_bal = ifnull(_line_bal,0) + ifnull(_line_in,0) - ifnull(_line_out,0);
  91. update dbt_list_type1 set d5 = _line_bal where t_id = _line_id;
  92. end loop loop1;
  93. close _line_cur;
  94. -- write list_sum
  95. select company_name, concat( main_contact, ' / ', email ) into _company_name, _full_name from dbr_company where id = _target;
  96. insert into dbt_list_sum
  97. ( created_on, list_token, c1, c2, c3, c4, d1, d2, d3, d4)
  98. values (unix_timestamp(), _listToken, _company_name, _full_name, _sdate, _edate,
  99. _first_bal, _in_sum, _out_sum, _line_bal);
  100. END;
  101. //
  102. DELIMITER ;