pl1_purch_ledger_detail.sql 4.3 KB

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