src-db/database/model/functions/C_DEBT_PAYMENT_CREATE.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Wed, 03 Sep 2008 17:55:37 +0000
changeset 1605 8a0fe0193bef
parent 756 ae11e4610537
child 1929 ad0353d93b0f
permissions -rw-r--r--
Merge r2.5x intro trunk
<?xml version="1.0"?>
  <database name="FUNCTION C_DEBT_PAYMENT_CREATE">
    <function name="C_DEBT_PAYMENT_CREATE" type="NULL">
      <parameter name="p_pinstance_id" type="VARCHAR" mode="in">
        <default/>
      </parameter>
      <body><![CDATA[/*************************************************************************
* The contents of this file are subject to the Openbravo  Public  License
* Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
* Version 1.1  with a permitted attribution clause; you may not  use this
* file except in compliance with the License. You  may  obtain  a copy of
* the License at http://www.openbravo.com/legal/license.html
* Software distributed under the License  is  distributed  on  an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific  language  governing  rights  and  limitations
* under the License.
* The Original Code is Openbravo ERP.
* The Initial Developer of the Original Code is Openbravo SL
* All portions are Copyright (C) 2001-2006 Openbravo SL
* All Rights Reserved.
* Contributor(s):  ______________________________________.
************************************************************************/
  --  Logistice
  v_ResultStr VARCHAR2(2000):='';
  v_Message VARCHAR2(2000):='';
  v_Result NUMBER:=1; -- 0=failure
  v_Record_ID VARCHAR2(32) ;
  v_AD_User_ID VARCHAR2(32) ;
  --  Parameter
  TYPE RECORD IS REF CURSOR;
    Cur_Parameter RECORD;
    --Parameter variables
    v_C_BPartner_ID VARCHAR2(32);
    v_Amount NUMBER;
    v_C_Currency_ID VARCHAR2(32);
    v_PaymentRule VARCHAR2(1) ;
    v_DatePlanned DATE;
    v_IsReceipt VARCHAR2(1) ;
    v_Description VARCHAR2(60) ;
    --Local variables
    v_Client_ID VARCHAR2(32);
    v_AD_Org_ID VARCHAR2(32);
    v_settlementID varchar2(32):=NULL;
    v_SettlementDocType_ID VARCHAR2(32) ;
    v_SDocumentNo C_SETTLEMENT.DocumentNo%TYPE;
    v_debtPaymentID varchar2(32) ;
    v_CBankAccount_ID C_BankStatement.C_BankAccount_ID%TYPE;
  v_C_BankCurrency VARCHAR2(32);
  v_BS_Date DATE;
  BEGIN
    --  Update AD_PInstance
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
    v_ResultStr:='PInstanceNotFound';
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
  BEGIN --BODY
    --  Get Parameters
    v_ResultStr:='ReadingParameters';
    FOR Cur_Parameter IN
      (SELECT i.Record_ID,
        p.ParameterName,
        p.P_String,
        p.P_Number,
        p.P_Date,
        i.AD_USER_ID
      FROM AD_PInstance i
      LEFT JOIN AD_PInstance_Para p
        ON i.AD_PInstance_ID=p.AD_PInstance_ID
      WHERE i.AD_PInstance_ID=p_PInstance_ID
      )
    LOOP
      v_Record_ID:=Cur_Parameter.Record_ID;
      v_AD_User_ID:=Cur_Parameter.AD_User_ID;
      IF(Cur_Parameter.ParameterName='C_BPartner_ID') THEN
        v_C_BPartner_ID:=Cur_Parameter.P_String;
        DBMS_OUTPUT.PUT_LINE('  C_BPartner_ID=' || v_C_BPartner_ID) ;
      ELSIF(Cur_Parameter.ParameterName='Amount') THEN
        v_Amount:=Cur_Parameter.P_Number;
        DBMS_OUTPUT.PUT_LINE('  Amount=' || v_Amount) ;
      ELSIF(Cur_Parameter.ParameterName='C_Currency_ID') THEN
        v_C_Currency_ID:=Cur_Parameter.P_String;
        DBMS_OUTPUT.PUT_LINE('  C_Currency_ID=' || v_C_Currency_ID) ;
      ELSIF(Cur_Parameter.ParameterName='PaymentRule') THEN
        v_PaymentRule:=Cur_Parameter.P_String;
        DBMS_OUTPUT.PUT_LINE('  v_PaymentRule=' || v_PaymentRule) ;
      ELSIF(Cur_Parameter.ParameterName='Dateplanned') THEN
        v_DatePlanned:=Cur_Parameter.P_Date;
        DBMS_OUTPUT.PUT_LINE('  DatePlanned=' || v_DatePlanned) ;
      ELSIF(Cur_Parameter.ParameterName='IsReceipt') THEN
        v_IsReceipt:=Cur_Parameter.p_String;
        DBMS_OUTPUT.PUT_LINE('  IsReceipt='||v_IsReceipt) ;
      ELSIF(Cur_Parameter.ParameterName='Description') THEN
        v_Description:=Cur_Parameter.p_String;
        DBMS_OUTPUT.PUT_LINE('  Description='||v_Description) ;
      ELSE
        DBMS_OUTPUT.PUT_LINE('*** Unknown Parameter=' || Cur_Parameter.ParameterName) ;
      END IF;
    END LOOP; --  Get Parameter
    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
    --Read bank statement
    v_ResultStr:='ReadingBankStatementLine '||v_record_Id;
    SELECT L.AD_Client_ID,
      L.AD_Org_ID,
      B.C_BankAccount_ID
    INTO v_Client_ID,
      v_AD_Org_ID,
      v_CBankAccount_ID
    FROM C_BankStatementLine L,
      C_BankStatement B
    WHERE L.C_BankStatementLine_ID=v_Record_ID
      AND L.C_BankStatement_ID=B.C_BankStatement_ID;
    --Insert Settlement
    v_ResultStr:='InsertingSettlement';
    v_SettlementDocType_ID:=Ad_Get_DocType(v_Client_ID, v_AD_Org_ID, TO_CHAR('STT')) ;
    Ad_Sequence_Next('C_Settlement', v_Record_ID, v_settlementID) ;
    Ad_Sequence_Doctype(v_SettlementDocType_ID, v_Record_ID, 'Y', v_SDocumentNo) ;
    IF(v_SDocumentNo IS NULL) THEN
      Ad_Sequence_Doc('DocumentNo_C_Settlement', v_Client_ID, 'Y', v_SDocumentNo) ;
    END IF;
    INSERT
    INTO C_SETTLEMENT
      (
        C_SETTLEMENT_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
        CREATED, CREATEDBY, UPDATED, UPDATEDBY,
        DOCUMENTNO, DATETRX, DATEACCT, SETTLEMENTTYPE,
        C_DOCTYPE_ID, PROCESSING, PROCESSED, POSTED,
        C_CURRENCY_ID, Description, ISGENERATED
      )
      /*, C_PROJECT_ID, C_CAMPAIGN_ID,
      C_ACTIVITY_ID, USER1_ID, USER2_ID, CREATEFROM)*/
      VALUES
      (
        v_SettlementID, v_Client_ID, v_AD_Org_ID, 'Y',
        now(), v_AD_User_ID, now(), v_AD_User_ID,
        '*DPC*'||v_SDocumentNo, trunc(now()), trunc(now()), 'C',
        v_SettlementDocType_ID, 'N', 'N', 'N',
        v_C_Currency_ID, v_Description, 'Y'
      )
      ;
    --Insert generated debt payment
    v_ResultStr:='InsertingGeneratedDebtPayement';
    Ad_Sequence_Next('C_Debt_Payment', v_Record_ID, v_debtPaymentID) ;
    INSERT
    INTO C_DEBT_PAYMENT
      (
        C_DEBT_PAYMENT_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
        CREATED, CREATEDBY, UPDATED, UPDATEDBY,
        ISRECEIPT, C_SETTLEMENT_GENERATE_ID, DESCRIPTION, C_INVOICE_ID,
        C_BPARTNER_ID, C_CURRENCY_ID,
        /* C_CASHLINE_ID, C_BANKACCOUNT_ID, C_CASHBOOK_ID,*/
        PAYMENTRULE, ISPAID, AMOUNT, WRITEOFFAMT, DATEPLANNED,
        ISMANUAL, ISVALID,
        /*C_BANKSTATEMENTLINE_ID,*/
        CHANGESETTLEMENTCANCEL, CANCEL_PROCESSED, GENERATE_PROCESSED, STATUS_INITIAL
      )
      VALUES
      (
        v_debtPaymentID, v_Client_ID, v_AD_Org_ID, 'Y',
        now(), v_AD_User_ID, now(), v_AD_User_ID,
        v_IsReceipt, v_settlementID, v_Description, null,
        v_C_BPartner_ID, v_C_Currency_ID,
        v_PaymentRule, 'N', v_Amount*(-1), 0, v_DatePlanned,
        'N', 'Y',
        'N', 'N', 'Y', 'DE'
      )
      ;
    Ad_Sequence_Next('C_Debt_Payment', v_Record_ID, v_debtPaymentID) ;
    --We insert it in the positive side of the bank account
    INSERT
    INTO C_DEBT_PAYMENT
      (
        C_DEBT_PAYMENT_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
        CREATED, CREATEDBY, UPDATED, UPDATEDBY,
        ISRECEIPT, C_SETTLEMENT_GENERATE_ID, DESCRIPTION, C_INVOICE_ID,
        C_BPARTNER_ID, C_CURRENCY_ID,
        /* C_CASHLINE_ID,*/
        C_BANKACCOUNT_ID,
        /*C_CASHBOOK_ID,*/
        PAYMENTRULE, ISPAID, AMOUNT, WRITEOFFAMT,
        DATEPLANNED, ISMANUAL, ISVALID,
        /*C_BANKSTATEMENTLINE_ID,*/
        CHANGESETTLEMENTCANCEL, CANCEL_PROCESSED, GENERATE_PROCESSED, STATUS_INITIAL
      )
      VALUES
      (
        v_debtPaymentID, v_Client_ID, v_AD_Org_ID, 'Y',
        now(), v_AD_User_ID, now(), v_AD_User_ID,
        v_IsReceipt, v_settlementID, v_Description, null,
        v_C_BPartner_ID, v_C_Currency_ID,
        v_CBankAccount_ID,
        v_PaymentRule, 'N', v_Amount, 0,
        v_DatePlanned, 'N', 'Y',
        'N', 'N', 'Y', 'DE'
      )
      ;
    C_SETTLEMENT_POST(null, v_settlementID);

  SELECT ba.C_Currency_ID, bs.STATEMENTDATE
    INTO v_C_BankCurrency, v_BS_Date
    FROM C_BankStatementLine bsl,
        C_BankStatement     bs,
      C_BankAccount    ba
   WHERE bsl.C_BankStatement_ID = bs.C_BankStatement_ID
     AND ba.C_BankAccount_ID = bs.C_BankAccount_ID
    AND bsl.C_BankStatementLine_ID = v_Record_ID;

  v_Amount := C_Currency_Convert(v_Amount, v_C_Currency_ID, v_C_BankCurrency, v_BS_Date, NULL, v_Client_ID, v_AD_Org_ID);

    UPDATE C_BankStatementLine
      SET C_Debt_Payment_ID=v_debtPaymentID,
      TrxAmt=((CASE v_IsReceipt WHEN 'Y' THEN 1 ELSE -1 END )) *v_Amount,
      StmtAmt=((CASE v_IsReceipt WHEN 'Y' THEN 1 ELSE -1 END)) *v_Amount,
      C_Currency_ID=v_C_BankCurrency,
      Description=v_Description,
      Updated=now(),
      UpdatedBy=v_AD_User_ID,
      C_Debt_Payment_Create='Y'
    WHERE C_BankStatementLine_ID=v_Record_ID;
    --  v_Message := 'Sett: '||v_SettlementID||' debt: '||v_debtPaymentID;
    --<<FINISH_PROCESS>>
    --  Update AD_PInstance
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
    RETURN;
  END; --BODY
EXCEPTION
WHEN OTHERS THEN
  v_ResultStr:= '@ERROR=' || SQLERRM;
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
  ROLLBACK;
  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
  RETURN;
END C_DEBT_PAYMENT_CREATE
]]></body>
    </function>
  </database>