pl1_sales_ledger_detail.sql 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. truncate table dbt_list_type1;
  2. truncate table dbt_list_sum;
  3. drop procedure if exists pl1_sales_ledger_detail;
  4. DELIMITER //
  5. create procedure pl1_sales_ledger_detail( _listToken varchar(21), _sdate varchar(8), _edate varchar(8),
  6. _s1 varchar(32), _e1 varchar(32), _s2 varchar(32), _e2 varchar(32),
  7. _s3 varchar(32), _e3 varchar(32), _s4 varchar(32), _e4 varchar(32),
  8. _where varchar(386), _having varchar(386), _orderby varchar(64),
  9. _branch int, _storage int, _target int)
  10. BEGIN
  11. Declare _sbal_date varchar(8);
  12. Declare _ebal_date varchar(8);
  13. Declare _first_bal decimal(20,4);
  14. Declare _first_bal_caption varchar(20);
  15. Declare _first_id int;
  16. Declare _last_id int;
  17. Declare _line_id int;
  18. Declare _line_in decimal(20,4) default "0.0000";
  19. Declare _line_out decimal(20,4) default "0.0000";
  20. Declare _line_bal decimal(20,4) default "0.0000";
  21. Declare _in_sum decimal(20,4) default "0.0000";
  22. Declare _out_sum decimal(20,4) default "0.0000";
  23. Declare _company_name varchar(64);
  24. Declare _full_name varchar(96);
  25. Declare _not_found boolean;
  26. Declare _line_cur cursor for
  27. select t_id, d3, d4 from dbt_list_type1 where t_id > _first_id and t_id <= _last_id
  28. order by c1, c2, c3 asc;
  29. Declare continue handler for
  30. not found set _not_found = true;
  31. if _is_first_date(_sdate) then
  32. set _sbal_date = '19000101';
  33. set _ebal_date = '19000101';
  34. else
  35. set _sbal_date = _sdate;
  36. set _ebal_date = _edate;
  37. end if;
  38. -- 전월잔액
  39. select ifnull(last_mbal.bal_amt,0) , '이월잔액' into _first_bal, _first_bal_caption
  40. from
  41. dbr_company as mv
  42. left join
  43. ( select buyer_id, bal_amt
  44. from dbr_bal_buyer
  45. where yyyy_mm = _prev_month(_sdate) and branch_id = _branch
  46. ) as last_mbal on mv.id = last_mbal.buyer_id
  47. where mv.id = _target;
  48. insert into dbt_list_type1 ( created_on, list_token, c4, d5 )
  49. values ( unix_timestamp(), _listToken, _first_bal_caption, _first_bal);
  50. -- insert for sales
  51. insert into dbt_list_type1
  52. ( created_on, list_token, c1, c2, c3, c4, c5, d1, d2, d3)
  53. select unix_timestamp(), _listToken, concat( sales_date,'-', deal_code, '*', sbbd.seq_no ),
  54. concat( sales_no, '*', sbbd.seq_no ), deal_code,
  55. concat( item_code, ' / ', item_name, ' / ', sub_name ), '', -- bill_type 은 비운다.
  56. sales_qty, sorder_prc, sales_sum*sales_status
  57. from ( select id, sales_no, sales_date
  58. from dbr_sales
  59. where sales_date between _sdate and _edate
  60. ) as sbhd
  61. inner join dbr_sales_bd as sbbd on sbhd.id = sbbd.sales_id
  62. inner join dbr_sorder_bd as mnbd on mnbd.id = sbbd.sorder_bd_id
  63. inner join dbr_sorder as mnhd on mnhd.id = mnbd.sorder_id
  64. inner join dbr_item as itm on mnbd.item_id = itm.id
  65. inner join dbr_deal_type as deal on deal.id = mnhd.deal_type_id
  66. where branch_id = _branch and mnhd.buyer_id = _target;
  67. -- 출금내역
  68. insert into dbt_list_type1
  69. ( created_on, list_token, c1, c2, c3, c4, c5, d4)
  70. select unix_timestamp(), _listToken, concat( acc_date,'-', deal_code ),
  71. acc_slip_no, deal_code,
  72. bill_column1, bill_type, -1*slip_amt*sales_status
  73. from
  74. ( select id, acc_date, acc_slip_no, deal_type_id,
  75. bill_column1, bill_type, slip_amt
  76. from dbr_acc_slip
  77. where ( deal_type_id = 21 or deal_type_id = 23 ) -- 입금과 입금할인만
  78. and acc_date between _sdate and _edate
  79. and branch_id = _branch and company_id = _target
  80. ) as mnhd
  81. inner join dbr_deal_type as deal on deal.id = deal_type_id;
  82. -- -- get id range for current ledger
  83. select min(t_id), max(t_id) into _first_id, _last_id from dbt_list_type1 where list_token = _listToken;
  84. -- compute line_bal
  85. set _line_bal = _first_bal;
  86. open _line_cur;
  87. loop1: loop
  88. fetch _line_cur into _line_id, _line_in, _line_out;
  89. if _not_found then leave loop1; end if;
  90. set _in_sum = _in_sum + ifnull(_line_in,0);
  91. set _out_sum = _out_sum + ifnull(_line_out,0);
  92. set _line_bal = ifnull(_line_bal,0) + ifnull(_line_in,0) - ifnull(_line_out,0);
  93. update dbt_list_type1 set d5 = _line_bal where t_id = _line_id;
  94. end loop loop1;
  95. close _line_cur;
  96. -- write list_sum
  97. select company_name, concat(full_name, ' ', company_no)
  98. into _company_name, _full_name from dbr_company where id = _target;
  99. insert into dbt_list_sum
  100. ( created_on, list_token, c1, c2, c3, c4, d1, d2, d3, d4)
  101. values (unix_timestamp(), _listToken, _company_name, _full_name, _sdate, _edate,
  102. _first_bal, _in_sum, _out_sum, _line_bal);
  103. END;
  104. //
  105. DELIMITER ;