Advertisement
aadddrr

CANCEL SUBMIT CG RECEIPT

Mar 21st, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Adrian, Mar 21, 2017
  2.  
  3. CREATE OR REPLACE FUNCTION cb_cancel_submit_cg_receipt(bigint, character varying, bigint, character varying, bigint, character varying, character varying)
  4.   RETURNS void AS
  5. $BODY$
  6. DECLARE
  7.     pTenantId                           ALIAS FOR $1;
  8.     pSessionId                          ALIAS FOR $2;
  9.     pUserId                             ALIAS FOR $3;
  10.     pDatetime                           ALIAS FOR $4;
  11.     pCGReceiptId                        ALIAS FOR $5;
  12.     pRemarkApproval                     ALIAS FOR $6;
  13.     pPrevProcessNo                      ALIAS FOR $7;
  14.    
  15.     vEmptyId                            bigint;
  16.     vCGReceiptDocTypeId                 bigint;
  17.     vFlagNo                             character varying(1);
  18.     vRoundingModeNonTax                 character varying(5);
  19.     vOuStructure                        OU_BU_STRUCTURE%ROWTYPE;
  20.     result                              RECORD;
  21.    
  22.     vFunctionSubmit                     character varying;
  23.     vStatusDocReleased                  character varying(1);
  24.     vStatusDocDraft                     character varying;
  25.     vStatusDocInProgress                character varying;
  26.     vStatusDocVoid                      character varying(1);
  27.     vWorkflowStatusDraft                character varying;
  28.     vPoId                               bigint;
  29.     vZero                               bigint;
  30.    
  31.     vOuId                               bigint;
  32.     vScheme                             character varying;
  33.     vDocNo                              character varying;
  34.    
  35.     vEmpty                              character varying;
  36.     vYes                                character varying;
  37.     vNo                                 character varying;
  38.    
  39. BEGIN
  40.  
  41.     vEmptyId := -99;
  42.     vFlagNo := 'N';
  43.     vCGReceiptDocTypeId := 624;
  44.    
  45.     vFunctionSubmit := 'cb_submit_cg_receipt'
  46.     vStatusDocReleased := 'R';
  47.     vStatusDocDraft := 'D';
  48.     vStatusDocInProgress = 'I';
  49.     vStatusDocVoid := 'V';
  50.     vWorkflowStatusDraft := 'DRAFT';
  51.     vZero := 0;
  52.    
  53.     vEmpty := '';
  54.     vYes := 'Y';
  55.     vNo := 'N';
  56.    
  57.     --RAISE EXCEPTION 'Function Submit For Document Cheque Giro Receipt is not yet Created';
  58.    
  59.     -- get data
  60.     select f_get_ou_bu_structure(A.ou_id) AS ou, A.ou_id, A.doc_no, A.doc_date,
  61.         f_get_document_journal(A.doc_type_id) as doc, C.scheme
  62.     FROM cb_in_out_cashbank A, m_document C
  63.     WHERE A.in_out_cashbank_id = pCGReceiptId AND
  64.         A.doc_type_id = C.doc_type_id AND
  65.         A.doc_type_id = vCGReceiptDocTypeId AND
  66.         A.status_doc = vStatusDocReleased
  67.     INTO result;
  68.    
  69.     IF FOUND THEN
  70.         vOuStructure := result.ou;
  71.         vOuId := result.ou_id;
  72.         vDocNo := result.doc_no;
  73.         vScheme := result.scheme;
  74.        
  75.         /*
  76.          * validasi flg_deposit dan flg_realization harus N
  77.          */
  78.         IF NOT EXISTS (
  79.             SELECT (1)
  80.             FROM cb_cheque_giro_balance A, cb_in_out_cashbank B, cb_in_out_cheque_giro_payment C
  81.             WHERE B.in_out_cashbank_id = pCGReceiptId
  82.                 AND B.in_out_cashbank_id = C.in_out_cashbank_id
  83.                 AND A.tenant_id = B.tenant_id
  84.                 AND A.in_out_cashbank_id = B.in_out_cashbank_id
  85.                 AND A.in_out_cheque_giro_payment_id = C.in_out_cheque_giro_payment_id
  86.                 AND (
  87.                     A.flg_deposit <> vNo
  88.                     OR A.flg_realization <> vNo
  89.                 )
  90.         ) THEN
  91.        
  92.             /*
  93.              * validasi flg_alloc harus N
  94.              */
  95.             IF NOT EXISTS (
  96.                 SELECT (1)
  97.                 FROM fi_receipt_ar_balance A, cb_in_out_cheque_giro_payment B
  98.                 WHERE B.in_out_cashbank_id = pCGReceiptId
  99.                     AND A.receipt_ar_balance_id = B.in_out_cheque_giro_payment_id
  100.                     AND A.flg_alloc <> vNo
  101.             ) THEN
  102.            
  103.                 -- delete data cheque/giro balance
  104.                 DELETE
  105.                 FROM cb_cheque_giro_balance Z  
  106.                 WHERE EXISTS (
  107.                     SELECT (1)
  108.                     FROM cb_in_out_cashbank A
  109.                     INNER JOIN cb_in_out_cheque_giro_payment B ON A.in_out_cashbank_id = B.in_out_cashbank_id
  110.                     WHERE A.in_out_cashbank_id = pCGReceiptId
  111.                         AND Z.tenant_id = A.tenant_id
  112.                         AND Z.in_out_cashbank_id = A.in_out_cashbank_id
  113.                         AND Z.in_out_cheque_giro_payment_id = B.in_out_cheque_giro_payment_id
  114.                 );
  115.                
  116.                 -- update cb_in_out_cashbank
  117.                 UPDATE cb_in_out_cashbank
  118.                 SET status_doc = vStatusDocDraft,
  119.                     workflow_status = vWorkflowStatusDraft,
  120.                     version = version + 1,
  121.                     update_datetime = pDatetime,
  122.                     update_user_id = pUserId
  123.                 WHERE in_out_cashbank_id = pCGReceiptId;
  124.                
  125.                 -- delete data saldo for allocation
  126.                 DELETE
  127.                 FROM fi_receipt_ar_balance Z   
  128.                 WHERE EXISTS (
  129.                     SELECT (1)
  130.                     FROM cb_in_out_cheque_giro_payment A
  131.                     WHERE A.in_out_cashbank_id = pCGReceiptId
  132.                         AND Z.receipt_ar_balance_id = A.in_out_cheque_giro_payment_id
  133.                 );
  134.                
  135.                 -- PERFORM f_reset_approval_to_draft
  136.                 PERFORM f_reset_approval_to_draft(pTenantId, pSessionId, vScheme, pCGReceiptId, vDocNo, pDatetime, pRemarkApproval);
  137.                    
  138.                 -- UPDATE t_process_message
  139.                 UPDATE t_process_message
  140.                 SET process_no = pPrevProcessNo,
  141.                     update_datetime = pDatetime,
  142.                     update_user_id = pUserId,
  143.                     version = version + 1
  144.                 WHERE tenant_id = pTenantId
  145.                     AND process_name = vFunctionSubmit
  146.                     AND process_no = pCGReceiptId || '_' || vDocNo;
  147.        
  148.             ELSE
  149.                 RAISE EXCEPTION 'Document with id % is already allocated', pCGReceiptId;
  150.             END IF;
  151.        
  152.         ELSE
  153.             RAISE EXCEPTION 'Document with id % is already used for realization or is void', pCGReceiptId;
  154.         END IF;
  155.                    
  156.     ELSE
  157.         RAISE EXCEPTION 'Document with id % is not found or is on approval progress', pCGReceiptId;
  158.     END IF;
  159.    
  160. END;
  161. $BODY$
  162.   LANGUAGE plpgsql VOLATILE
  163.   COST 100;
  164.   /
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement