pl1_accounting_cash_ledger_detail.sql 4.3 KB

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