credit_bd_act.sql 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. drop procedure if exists credit_bd_act;
  2. create procedure credit_bd_act( _slip_code varchar(6), _target int, _act int)
  3. BEGIN
  4. Declare _created_on bigint ;
  5. Declare _updated_on bigint ;
  6. Declare _created_id int ;
  7. Declare _sorder_bd_id int(11) defauLt 0;
  8. Declare _credit_id int(11) default 0;
  9. Declare _sorder_date varchar(8);
  10. Declare _avail_date varchar(8);
  11. Declare _branch_id int ;
  12. Declare _user_id int ;
  13. Declare _deal_type_id int;
  14. Declare _sorder_sum decimal(20,4);
  15. Declare _credit_amt decimal(20,4);
  16. Declare _add_msg varchar(49);
  17. Declare _from_buyer_id int;
  18. Declare _to_buyer_id int;
  19. Declare _is_transfer varchar(49);
  20. Declare _tgt_sorder_bd_id int;
  21. Declare _tgt_credit_id int;
  22. if _slip_code = 'sordbd' then
  23. set _tgt_sorder_bd_id = _target;
  24. set _tgt_credit_id = 0;
  25. select sobd.id, sorder_date, date_format(date_add(_sorder_date, interval 1 day), '%Y%m%d'),
  26. branch_id, sohd.user_id, deal_type_id,
  27. buyer_id, sorder_sum, rdecimal((sorder_qty*offer_credit),0),
  28. concat( sorder_no, ' / ', item_code )
  29. into _sorder_bd_id, _sorder_date, _avail_date, _branch_id, _user_id, _deal_type_id,
  30. _to_buyer_id, _sorder_sum, _credit_amt, _add_msg
  31. from dbr_sorder as sohd
  32. inner join dbr_sorder_bd as sobd on sohd.id = sobd.sorder_id
  33. inner join dbr_item as itm on itm.id = sobd.item_id
  34. where sobd.id = _target;
  35. elseif _slip_code = 'credit' then -- sorder_date = credit_date = avail_date 로 한다.
  36. set _tgt_sorder_bd_id = 0;
  37. set _tgt_credit_id = _target;
  38. select id, credit_date, credit_date, branch_id, user_id, deal_type_id,
  39. is_transfer, from_buyer_id, to_buyer_id, manual_amt, credit_no
  40. into _credit_id, _sorder_date, _avail_date, _branch_id, _user_id, _deal_type_id,
  41. _is_transfer, _from_buyer_id, _to_buyer_id, _credit_amt, _add_msg
  42. from dbr_credit as crdt
  43. where crdt.id = _target;
  44. end if;
  45. -- act for stock_io
  46. if _act = 0 then
  47. insert into dbr_credit_bd
  48. ( created_on, bd_id, credit_id, occur_date, avail_date, branch_id,
  49. user_id, deal_type_id, buyer_id, credit_amt,
  50. add_msg, is_from_buyer )
  51. values ( unix_timestamp(), _sorder_bd_id, _credit_id, _sorder_date, _avail_date, _branch_id,
  52. _user_id, _deal_type_id, _to_buyer_id, _credit_amt,
  53. _add_msg, '0' );
  54. if _is_transfer = '1' then -- credit 에서만
  55. insert into dbr_credit_bd
  56. ( created_on, bd_id, credit_id, occur_date, avail_date, branch_id,
  57. user_id, deal_type_id, buyer_id, credit_amt,
  58. add_msg, is_from_buyer )
  59. values ( unix_timestamp(), _sorder_bd_id, _credit_id, _sorder_date, _avail_date, _branch_id,
  60. _user_id, _deal_type_id, _from_buyer_id, -1*_credit_amt,
  61. _add_msg, '1' );
  62. end if;
  63. elseif _act = 1 then
  64. update dbr_credit_bd
  65. set updated_on=unix_timestamp(),
  66. bd_id=_sorder_bd_id, occur_date=_sorder_date, avail_date=_avail_date,
  67. branch_id=_branch_id, user_id = _user_id, deal_type_id = _deal_type_id,
  68. buyer_id = _to_buyer_id, credit_amt = _credit_amt,
  69. add_msg = _add_msg
  70. where credit_id=_tgt_credit_id and bd_id= _tgt_sorder_bd_id and is_from_buyer = '0';
  71. if _is_transfer = '1' then -- credit 에서만
  72. update dbr_credit_bd
  73. set updated_on=unix_timestamp(),
  74. bd_id=_sorder_bd_id, occur_date=_sorder_date, avail_date=_avail_date,
  75. branch_id=_branch_id, user_id = _user_id, deal_type_id = _deal_type_id,
  76. buyer_id = _from_buyer_id, credit_amt = -1*_credit_amt,
  77. add_msg = _add_msg
  78. where credit_id=_tgt_credit_id and bd_id= _tgt_sorder_bd_id and is_from_buyer = '1';
  79. end if;
  80. elseif _act = -1 then
  81. delete from dbr_credit_bd
  82. where credit_id=_tgt_credit_id and bd_id= _tgt_sorder_bd_id and is_from_buyer = '0';
  83. if _is_transfer = '1' then -- credit 에서만
  84. delete from dbr_credit_bd
  85. where credit_id=_tgt_credit_id and bd_id= _tgt_sorder_bd_id and is_from_buyer = '1';
  86. end if;
  87. end if;
  88. END;