stock_io_act.sql 3.3 KB

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