Fanadia_Friska

cancel submit cost allocation

May 27th, 2020
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. -- Function: in_cancel_submit_cost_alloc_to_product(bigint, character varying, bigint, character varying, bigint, character varying, character varying)
  2.  
  3. -- DROP FUNCTION in_cancel_submit_cost_alloc_to_product(bigint, character varying, bigint, character varying, bigint, character varying, character varying);
  4.  
  5. CREATE OR REPLACE FUNCTION in_cancel_submit_cost_alloc_to_product(
  6. bigint,
  7. character varying,
  8. bigint,
  9. character varying,
  10. bigint,
  11. character varying,
  12. character varying)
  13. RETURNS void AS
  14. $BODY$
  15. DECLARE
  16. pTenantId ALIAS FOR $1;
  17. pSessionId ALIAS FOR $2;
  18. pUserId ALIAS FOR $3;
  19. pDatetime ALIAS FOR $4;
  20. pId ALIAS FOR $5;
  21. pRemarkApproval ALIAS FOR $6;
  22. pPrevProcessNo ALIAS FOR $7;
  23.  
  24. vFunctionSubmit character varying := 'in_submit_cost_alloc_to_product';
  25. vStatusDraft character varying := 'D';
  26. vStatusRelease character varying := 'R';
  27. vStatusInProgress character varying := 'I';
  28. vWorkflowStatus character varying := 'DRAFT';
  29. vInventoryLedgerCode character varying := 'INV';
  30. vStatusLedgerNotDone character varying := '0';
  31.  
  32. vDocNo character varying;
  33. vScheme character varying;
  34. vDocDate character varying;
  35. vDocJournal DOC_JOURNAL%ROWTYPE;
  36. vOuStructure OU_BU_STRUCTURE%ROWTYPE;
  37. result RECORD;
  38.  
  39. vJournalTrxId bigint;
  40. vOuId bigint;
  41. vDocTypeIdCostAllocToProduct bigint := 528;
  42.  
  43. BEGIN
  44.  
  45. /*
  46. * 1. get data
  47. * 2. Cek status ledger not yet closed
  48. * 2.1 DELETE from in_product_price_balance
  49. * 2.2 Update status doc in_inventory menjadi 'D' dan workflow_status nya menjadi DRAFT
  50. * 2.3 PERFORM cancel gl_cancel_admin_journal_trx
  51. * 2.4 Ambil ID dari gl_journal_trx
  52. * 2.5 DELETE FROM gl_journal_trx
  53. * 2.6 DELETE FROM gl_journal_trx_item
  54. * 2.7 DELETE FROM gl_journal_trx_mapping
  55. * 2.8. PERFORM f_reset_approval_to_draft
  56. * 2.9. UPDATE t_process_message
  57. */
  58.  
  59. -- 1. get data
  60. select f_get_ou_bu_structure(A.ou_from_id) AS ou, A.ou_from_id, A.doc_no, A.doc_date,
  61. f_get_document_journal(A.doc_type_id) as doc, C.scheme
  62. FROM in_inventory A, in_inventory_item B, m_document C
  63. WHERE A.inventory_id = B.inventory_id AND
  64. A.doc_type_id = C.doc_type_id AND
  65. A.inventory_id = pId AND
  66. A.doc_type_id = vDocTypeIdCostAllocToProduct
  67. AND A.status_doc = vStatusRelease INTO result;
  68.  
  69. IF FOUND THEN
  70. vOuStructure := result.ou;
  71. vOuId := result.ou_from_id;
  72. vDocNo := result.doc_no;
  73. vDocDate := result.doc_date;
  74. vDocJournal := result.doc;
  75. vScheme := result.scheme;
  76.  
  77. -- 2. Cek status ledger not yet closed
  78. IF EXISTS ( SELECT 1
  79. FROM m_admin_process_ledger e, m_ou_structure f, t_ou g
  80. WHERE e.tenant_id = pTenantId
  81. AND e.ou_id = f.ou_bu_id
  82. AND f.ou_id = vOuId
  83. AND e.date_year_month = SUBSTR(vDocDate, 1, 6)
  84. AND e.ledger_code = vInventoryLedgerCode AND
  85. f.ou_id = g.ou_id AND e.status_ledger = vStatusLedgerNotDone) THEN
  86.  
  87. -- 2.1 DELETE from in_product_price_balance
  88. DELETE from in_product_price_balance
  89. WHERE EXISTS(
  90. SELECT 1
  91. FROM in_inventory A, in_inventory_item B
  92. WHERE A.inventory_id = B.inventory_id AND
  93. A.inventory_id = pId AND
  94. in_product_price_balance.product_id = B.product_id AND
  95. in_product_price_balance.product_balance_id = B.product_balance_id AND
  96. in_product_price_balance.doc_type_id = A.doc_type_id AND
  97. in_product_price_balance.ref_id = B.inventory_item_id );
  98.  
  99. -- 2.2 Update status doc in_inventory menjadi 'D' dan workflow_status nya menjadi DRAFT
  100. UPDATE in_inventory SET status_doc = vStatusDraft, workflow_status = vWorkflowStatus,
  101. version = version - 1, update_datetime = pDatetime, update_user_id = pUserId
  102. WHERE inventory_id = pId;
  103.  
  104. -- 2.3 PERFORM cancel gl_cancel_admin_journal_trx
  105. PERFORM gl_cancel_admin_journal_trx(pTenantId, (vOuStructure).ou_bu_id, vOuId, (vDocJournal).journal_type, f_get_year_month_date(vDocDate), 'MONTHLY', pDatetime, pUserId);
  106.  
  107. -- 2.4 Ambil ID dari gl_journal_trx
  108. SELECT journal_trx_id INTO vJournalTrxId
  109. FROM gl_journal_trx
  110. WHERE doc_type_id = vDocTypeIdCostAllocToProduct AND doc_id = pId;
  111.  
  112. -- 2.5 DELETE FROM gl_journal_trx
  113. DELETE FROM gl_journal_trx WHERE journal_trx_id = vJournalTrxId;
  114.  
  115. -- 2.6 DELETE FROM gl_journal_trx_item
  116. DELETE FROM gl_journal_trx_item WHERE journal_trx_id = vJournalTrxId;
  117.  
  118. -- 2.7 DELETE FROM gl_journal_trx_mapping
  119. DELETE FROM gl_journal_trx_mapping WHERE journal_trx_id = vJournalTrxId;
  120.  
  121. -- 2.8. PERFORM f_reset_approval_to_draft
  122. PERFORM f_reset_approval_to_draft(pTenantId, pSessionId, vScheme, pId, vDocNo, pDatetime, pRemarkApproval);
  123.  
  124. -- 2.9. UPDATE t_process_message
  125. UPDATE t_process_message
  126. SET process_no = pPrevProcessNo,
  127. update_datetime = pDatetime,
  128. update_user_id = pUserId,
  129. version = version + 1
  130. WHERE tenant_id = pTenantId
  131. AND process_name = vFunctionSubmit
  132. AND process_no = pId || '_' || vDocNo;
  133.  
  134. ELSE
  135. RAISE EXCEPTION 'Admin Process Ledger for Inventory in year month % is already closed', SUBSTR(vDocDate, 1, 6);
  136. END IF;
  137.  
  138. ELSE
  139. RAISE EXCEPTION 'Document with id % is not found or document is on approval progress', pClaimNoteId;
  140. END IF;
  141.  
  142.  
  143. END;
  144. $BODY$
  145. LANGUAGE plpgsql VOLATILE
  146. COST 100;
  147. ALTER FUNCTION in_cancel_submit_cost_alloc_to_product(bigint, character varying, bigint, character varying, bigint, character varying, character varying)
  148. OWNER TO sts;
Advertisement
Add Comment
Please, Sign In to add comment