stock_io_act.sql 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. -- truncate table dbr_stock_io;
  2. drop procedure if exists stock_io_act;
  3. DELIMITER //
  4. create procedure stock_io_act( _slip_code varchar(6), _target int, _act int)
  5. BEGIN
  6. Declare _created_on bigint ;
  7. Declare _updated_on bigint ;
  8. Declare _slip_bd_id int ;
  9. Declare _slip_no varchar(21) ;
  10. Declare _seq_no tinyint;
  11. Declare _io_date char(8);
  12. Declare _deal_type_id int;
  13. Declare _storage_id int;
  14. Declare _company_id int;
  15. Declare _vat_rate_id int;
  16. Declare _item_id int;
  17. Declare _io_qty decimal(20,4);
  18. Declare _io_prc decimal(20,4);
  19. Declare _io_supply decimal(20,4);
  20. Declare _io_vat decimal(20,4);
  21. Declare _io_sum decimal(20,4);
  22. if _slip_code = 'purch' then
  23. select pubd.id, purch_no, pubd.seq_no, purch_date, deal_type_id,
  24. storage_id, supplier_id, vat_rate_id, item_id, purch_qty,
  25. porder_prc, purch_supply, purch_vat, purch_sum
  26. into _slip_bd_id, _slip_no, _seq_no, _io_date, _deal_type_id,
  27. _storage_id, _company_id, _vat_rate_id, _item_id, _io_qty,
  28. _io_prc, _io_supply, _io_vat, _io_sum
  29. from dbr_porder as pohd
  30. inner join dbr_porder_bd as pobd on pohd.id = pobd.porder_id
  31. inner join dbr_purch_bd as pubd on pobd.id = pubd.porder_bd_id
  32. inner join dbr_purch as puhd on puhd.id = pubd.purch_id
  33. where pubd.id = _target;
  34. elseif _slip_code = 'sales' then
  35. select sabd.id, sales_no, sabd.seq_no, sales_date, deal_type_id,
  36. storage_id, buyer_id, vat_rate_id, item_id, sales_qty,
  37. sorder_prc, sales_supply, sales_vat, sales_sum
  38. into _slip_bd_id, _slip_no, _seq_no, _io_date, _deal_type_id,
  39. _storage_id, _company_id, _vat_rate_id, _item_id, _io_qty,
  40. _io_prc, _io_supply, _io_vat, _io_sum
  41. from dbr_sorder as sohd
  42. inner join dbr_sorder_bd as sobd on sohd.id = sobd.sorder_id
  43. inner join dbr_sales_bd as sabd on sobd.id = sabd.sorder_bd_id
  44. inner join dbr_sales as sahd on sahd.id = sabd.sales_id
  45. where sabd.id = _target;
  46. elseif _slip_code = 'genio' then
  47. select gnhd.id, genio_no, gnbd.seq_no, genio_date, deal_type_id,
  48. storage_id, company_id, vat_rate_id, item_id, genio_qty,
  49. genio_prc, genio_supply, genio_vat, genio_sum
  50. into _slip_bd_id, _slip_no, _seq_no, _io_date, _deal_type_id,
  51. _storage_id, _company_id, _vat_rate_id, _item_id, _io_qty,
  52. _io_prc, _io_supply, _io_vat, _io_sum
  53. from dbr_genio as gnhd
  54. inner join dbr_genio_bd as gnbd on gnhd.id = gnbd.genio_id
  55. where gnbd.id = _target;
  56. end if;
  57. -- act for stock_io
  58. if _act = 0 then
  59. insert into dbr_stock_io
  60. ( created_on, slip_code, slip_bd_id, slip_no, seq_no, io_date,
  61. deal_type_id, storage_id, company_id, vat_rate_id, item_id,
  62. io_qty, io_prc, io_supply, io_vat, io_sum )
  63. values ( unix_timestamp(), _slip_code, _slip_bd_id, _slip_no, _seq_no, _io_date,
  64. _deal_type_id, _storage_id, _company_id, _vat_rate_id, _item_id,
  65. _io_qty, _io_prc, _io_supply, _io_vat, _io_sum );
  66. elseif _act = 1 then
  67. update dbr_stock_io
  68. set updated_on=unix_timestamp(),
  69. slip_no=_slip_no, seq_no=_seq_no, io_date=_io_date,
  70. deal_type_id=_deal_type_id, storage_id=_storage_id, company_id=_company_id,
  71. vat_rate_id=_vat_rate_id, item_id=_item_id,
  72. io_qty=_io_qty, io_prc=_io_prc, io_supply=_io_supply,
  73. io_vat=_io_vat, io_sum= _io_sum
  74. where slip_code=_slip_code and slip_bd_id= _target;
  75. elseif _act = -1 then
  76. delete from dbr_stock_io
  77. where slip_code=_slip_code and slip_bd_id= _target;
  78. end if;
  79. END;
  80. //
  81. DELIMITER ;
  82. -- call stock_io_act('purch', 1, 1);