credit_bd_act.sql 3.7 KB

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