src-db/database/model/functions/C_REMITTANCE_POST.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Wed, 03 Sep 2008 17:55:37 +0000
changeset 1605 8a0fe0193bef
parent 1497 425af4296589
child 1929 ad0353d93b0f
permissions -rw-r--r--
Merge r2.5x intro trunk
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION C_REMITTANCE_POST">
adrian@94
     3
    <function name="C_REMITTANCE_POST" type="NULL">
juanpablo@1605
     4
      <parameter name="p_pinstance_id" type="VARCHAR" mode="in">
antonio@735
     5
        <default/>
antonio@735
     6
      </parameter>
juanpablo@1605
     7
      <parameter name="p_remittance_id" type="VARCHAR" mode="in">
antonio@735
     8
        <default><![CDATA[NULL]]></default>
antonio@735
     9
      </parameter>
gorkaion@239
    10
      <body><![CDATA[/*************************************************************************
carlos@0
    11
* The contents of this file are subject to the Openbravo  Public  License
carlos@0
    12
* Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
carlos@0
    13
* Version 1.1  with a permitted attribution clause; you may not  use this
carlos@0
    14
* file except in compliance with the License. You  may  obtain  a copy of
carlos@0
    15
* the License at http://www.openbravo.com/legal/license.html
carlos@0
    16
* Software distributed under the License  is  distributed  on  an "AS IS"
carlos@0
    17
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
carlos@0
    18
* License for the specific  language  governing  rights  and  limitations
carlos@0
    19
* under the License.
carlos@0
    20
* The Original Code is Openbravo ERP.
carlos@0
    21
* The Initial Developer of the Original Code is Openbravo SL
carlos@0
    22
* All portions are Copyright (C) 2001-2006 Openbravo SL
carlos@0
    23
* All Rights Reserved.
carlos@0
    24
* Contributor(s):  ______________________________________.
carlos@0
    25
************************************************************************/
carlos@0
    26
  --  Logistice
carlos@0
    27
  v_ResultStr VARCHAR2(2000):='';
carlos@0
    28
  v_Message VARCHAR2(2000):='';
carlos@0
    29
  v_Result NUMBER:=1; -- 0=failure
juanpablo@1605
    30
  v_Record_ID VARCHAR2(32) ;
juanpablo@1605
    31
  v_AD_User_ID VARCHAR2(32) ;
juanpablo@1605
    32
  v_AD_Client_ID VARCHAR2(32) ;
juanpablo@1605
    33
  v_AD_Org_ID VARCHAR2(32) ;
carlos@0
    34
  v_Processed CHAR(1) ;
carlos@0
    35
  v_Posted CHAR(1) ;
carlos@0
    36
  v_Processing CHAR(1);
juanpablo@1605
    37
  v_Currency VARCHAR2(32);
carlos@0
    38
  v_Date DATE;
carlos@0
    39
  v_CashBook_ISO_Code VARCHAR(10) ;
carlos@0
    40
  v_Record_Description VARCHAR2(2000):='';
juanpablo@1605
    41
  v_SettlementDocType_ID VARCHAR2(32) ;
juanpablo@1605
    42
  v_DPManagementDocType_ID VARCHAR2(32) ;
juanpablo@1605
    43
  v_Debt_Payment_ID VARCHAR2(32) ;
carlos@0
    44
  --Addesd by PSarobe 12062007
juanpablo@1605
    45
  v_Remittanceline_ID VARCHAR2(32);
carlos@0
    46
  v_documentno_Settlement VARCHAR2(40);
carlos@0
    47
  v_dateSettlement DATE;
carlos@0
    48
  v_Cancel_Processed CHAR(1);
carlos@0
    49
  v_nameBankstatement VARCHAR2 (60);
carlos@0
    50
  v_dateBankstatement DATE;
carlos@0
    51
  v_nameCash VARCHAR2 (60);
carlos@0
    52
  v_dateCash DATE;
juanpablo@1605
    53
  v_Bankstatementline_ID VARCHAR2(32);
juanpablo@1605
    54
  v_CashLine_ID VARCHAR2(32);
carlos@0
    55
  v_ispaid CHAR(1);
juanpablo@1605
    56
  v_Settlement_Cancel_ID VARCHAR2(32);
carlos@0
    57
  TYPE RECORD IS REF CURSOR;
carlos@0
    58
    Cur_Parameter RECORD;
carlos@0
    59
    Cur_RemittanceLines RECORD;
carlos@0
    60
    --  Parameter
carlos@0
    61
    --  Parameter Variables
carlos@0
    62
    v_Aux NUMBER;
carlos@0
    63
    v_CanceledNotChargeAmt NUMBER:=0;
carlos@0
    64
    v_DateTrx DATE;
carlos@0
    65
    v_DueDate DATE;
carlos@0
    66
    v_Status_to CHAR(2) ;
carlos@0
    67
    v_Consolidate CHAR(1) ;
juanpablo@1605
    68
    v_SettlementID varchar2(32) ;
carlos@0
    69
    v_DocumentNo NUMBER;
juanpablo@1605
    70
    v_DPMId varchar2(32);
juanpablo@1605
    71
    v_DPId varchar2(32);
juanpablo@1605
    72
    v_DPMLineId varchar2(32);
carlos@0
    73
    v_GeneratedAmt NUMBER;
carlos@0
    74
    v_PaymetRule VARCHAR2(2) ;
carlos@0
    75
    v_IsReceipt CHAR(1) ;
juanpablo@1605
    76
    v_BankPartner varchar2(32);
juanpablo@1605
    77
    v_vSettlementId varchar2(32);
juanpablo@1605
    78
    v_vDPManagementID varchar2(32);
juanpablo@1605
    79
    v_BankAccountID varchar2(32);
carlos@0
    80
    v_line NUMBER;
stefan@1497
    81
    v_remittanceLineCount NUMBER;
carlos@0
    82
    FINISH_PROCESS BOOLEAN:=FALSE;
carlos@0
    83
  BEGIN
carlos@0
    84
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
    85
      --  Update AD_PInstance
carlos@0
    86
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
    87
      v_ResultStr:='PInstanceNotFound';
antonio@735
    88
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
    89
      --  Get Parameters
carlos@0
    90
      v_ResultStr:='ReadingParameters';
carlos@0
    91
      FOR Cur_Parameter IN
carlos@0
    92
        (SELECT i.Record_ID,
carlos@0
    93
          i.AD_User_ID,
carlos@0
    94
          p.ParameterName,
carlos@0
    95
          p.P_String,
carlos@0
    96
          p.P_Number,
carlos@0
    97
          p.P_Date
carlos@0
    98
        FROM AD_PINSTANCE i
carlos@0
    99
        LEFT JOIN AD_PINSTANCE_PARA p
carlos@0
   100
          ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
   101
        WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
   102
        ORDER BY p.SeqNo
carlos@0
   103
        )
carlos@0
   104
      LOOP
carlos@0
   105
        v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
   106
      END LOOP; --  Get Parameter
carlos@0
   107
      DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
carlos@0
   108
    ELSE
gorkaion@239
   109
      DBMS_OUTPUT.PUT_LINE('--<<C_Settlement_Post>>') ;
carlos@0
   110
      v_Record_ID:=p_Remittance_ID;
carlos@0
   111
    END IF;
carlos@0
   112
  BEGIN --BODY
carlos@0
   113
    SELECT Processing, r.UpdatedBy,
carlos@0
   114
      r.AD_Client_ID, r.AD_Org_Id, Processed, DateTrx,
carlos@0
   115
      DueDate, rt.Status_To, rt.CONSOLIDATE, rt.PAYMENTRULECONSOLIDATED,
carlos@0
   116
      r.C_SETTLEMENT_ID, r.C_DP_MANAGEMENT_ID, r.C_BANKACCOUNT_ID
carlos@0
   117
    INTO v_Processing, v_AD_User_ID, v_AD_Client_ID, v_AD_Org_ID, v_processed,
carlos@0
   118
      v_DateTrx, v_DueDate, v_Status_to, v_Consolidate,
carlos@0
   119
      v_PaymetRule, v_vSettlementId, v_vDPManagementID, v_BankAccountID
carlos@0
   120
    FROM C_REMITTANCE r,
carlos@0
   121
      C_REMITTANCE_TYPE rt
carlos@0
   122
    WHERE c_Remittance_ID=v_Record_ID
carlos@0
   123
      AND r.c_Remittance_Type_id=rt.C_Remittance_Type_id;
carlos@0
   124
    SELECT C_Currency_ID
carlos@0
   125
    INTO v_Currency
carlos@0
   126
    FROM C_REMITTANCE r,
carlos@0
   127
      C_BANKACCOUNT a
carlos@0
   128
    WHERE c_Remittance_ID=v_Record_Id
carlos@0
   129
      AND r.C_BANKACCOUNT_ID=a.c_bankAccount_ID;
carlos@0
   130
stefan@1497
   131
    -- check if there are any active remittance lines at all
stefan@1497
   132
    SELECT COUNT(*)
stefan@1497
   133
    INTO v_remittanceLineCount
stefan@1497
   134
    FROM C_REMITTANCELINE rl
stefan@1497
   135
    WHERE rl.c_remittance_id=v_Record_Id
stefan@1497
   136
      AND rl.isactive='Y';
stefan@1497
   137
    
stefan@1497
   138
    IF(v_remittanceLineCount = 0) THEN
stefan@1497
   139
      RAISE_APPLICATION_ERROR(-20000, '@C_REMITTANCE_POST_NO_LINES@');
stefan@1497
   140
    END IF;
stefan@1497
   141
carlos@0
   142
    IF(v_Processing='Y') THEN
carlos@0
   143
      RAISE_APPLICATION_ERROR(-20000, '@OtherProcessActive@') ;
carlos@0
   144
    ELSE
carlos@0
   145
      UPDATE C_REMITTANCE SET Processing = 'Y' WHERE C_Remittance_ID = v_Record_ID;
carlos@0
   146
    END IF;
carlos@0
   147
carlos@0
   148
carlos@0
   149
    IF ((NOT FINISH_PROCESS) AND v_Processed='Y') THEN
carlos@0
   150
      /*
carlos@0
   151
      *  Checking Restrictions for unprocessing
carlos@0
   152
      */
carlos@0
   153
	  --Modified by PSarobe 12062007
carlos@0
   154
      SELECT COUNT(*), MAX(rl.line)
carlos@0
   155
      INTO v_Aux, v_line
carlos@0
   156
      FROM C_REMITTANCELINE rl,
carlos@0
   157
        C_DEBT_PAYMENT dp,
carlos@0
   158
        C_REMITTANCE r
carlos@0
   159
      WHERE r.C_Remittance_ID=v_Record_ID
carlos@0
   160
        AND rl.C_Remittance_ID=r.C_Remittance_ID
carlos@0
   161
        AND rl.c_debt_payment_id=dp.c_debt_payment_id
carlos@0
   162
		AND EXISTS (SELECT 1 FROM C_DP_MANAGEMENTLINE
carlos@0
   163
				    WHERE C_DP_Management_ID = r.C_DP_Management_ID
carlos@0
   164
					AND C_Debt_Payment_ID = dp.C_Debt_Payment_ID
carlos@0
   165
					AND Status_To != dp.Status);
carlos@0
   166
      IF v_Aux!=0 THEN
carlos@0
   167
        RAISE_APPLICATION_ERROR(-20000, '@Inline@'||v_line||' '||'WrongCurrentFinalStatusDP') ;
carlos@0
   168
      END IF;
carlos@0
   169
      IF NOT FINISH_PROCESS THEN
carlos@0
   170
        -- Unprocessing...
carlos@0
   171
        UPDATE C_REMITTANCE  SET Processed='N'  WHERE C_Remittance_ID=v_Record_ID;
antonio@735
   172
        C_DP_MANAGEMENT_POST(NULL, v_vDPManagementID) ;
carlos@0
   173
        SELECT COUNT(*)
carlos@0
   174
        INTO v_Aux
carlos@0
   175
        FROM C_REMITTANCE r
carlos@0
   176
        WHERE r.C_Remittance_ID=v_Record_ID
carlos@0
   177
          AND C_SETTLEMENT_ID IS NOT NULL;
carlos@0
   178
        IF v_Aux=1 THEN
antonio@735
   179
          C_SETTLEMENT_POST(NULL, v_vSettlementId) ;
carlos@0
   180
          UPDATE C_REMITTANCELINE
carlos@0
   181
            SET C_DEBT_PAYMENT_CANCELLED=NULL
carlos@0
   182
          WHERE C_Remittance_ID=v_Record_ID;
carlos@0
   183
          DELETE
carlos@0
   184
          FROM C_DP_MANAGEMENTLINE
carlos@0
   185
          WHERE C_DP_Management_ID=v_vDPManagementID
carlos@0
   186
            AND C_Debt_Payment_ID IN
carlos@0
   187
            (SELECT C_Debt_Payment_ID
carlos@0
   188
            FROM C_DEBT_PAYMENT
carlos@0
   189
            WHERE C_SETTLEMENT_GENERATE_ID=v_vSettlementId
carlos@0
   190
            )
carlos@0
   191
            ;
carlos@0
   192
          UPDATE C_REMITTANCE
carlos@0
   193
            SET C_Settlement_ID=NULL
carlos@0
   194
          WHERE C_Remittance_ID=v_Record_ID;
carlos@0
   195
          DELETE FROM C_DEBT_PAYMENT  WHERE C_SETTLEMENT_GENERATE_ID=v_vSettlementId;
carlos@0
   196
          DELETE FROM C_SETTLEMENT WHERE C_Settlement_ID=v_vSettlementId;
carlos@0
   197
        END IF;
carlos@0
   198
        UPDATE C_REMITTANCE
carlos@0
   199
          SET C_DP_Management_ID=NULL
carlos@0
   200
        WHERE C_Remittance_ID=v_Record_ID;
carlos@0
   201
        DELETE FROM C_DP_MANAGEMENTLINE  WHERE C_DP_Management_ID=v_vDPManagementID;
carlos@0
   202
        DELETE FROM C_DP_MANAGEMENT  WHERE C_DP_Management_ID=v_vDPManagementID;
carlos@0
   203
      END IF;
carlos@0
   204
	   v_Message:='@UnProcessedRemittance@';
carlos@0
   205
      FINISH_PROCESS:=TRUE;
carlos@0
   206
    END IF;
carlos@0
   207
    /*
carlos@0
   208
    *  Checking Restrictions
carlos@0
   209
    */
carlos@0
   210
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   211
      IF v_Consolidate='Y' AND (v_PaymetRule IS NULL) THEN
carlos@0
   212
          RAISE_APPLICATION_ERROR(-20000, '@PaymentRuleNotDefined@');
carlos@0
   213
        END IF;
carlos@0
   214
carlos@0
   215
      SELECT COUNT(*), MAX(rl.C_Remittanceline_Id)
carlos@0
   216
      INTO v_Aux, v_Remittanceline_ID
carlos@0
   217
      FROM C_REMITTANCELINE rl,
carlos@0
   218
        C_DEBT_PAYMENT dp
carlos@0
   219
      WHERE c_Remittance_ID=v_Record_ID
carlos@0
   220
        AND rl.c_debt_payment_id=dp.c_debt_payment_id
carlos@0
   221
        AND C_Debt_Payment_Status(dp.C_SETTLEMENT_CANCEL_ID, dp.CANCEL_PROCESSED, dp.GENERATE_PROCESSED, dp.IsPaid, dp.Isvalid, dp.C_CASHLINE_ID, dp.C_BANKSTATEMENTLINE_ID)!='P';
carlos@0
   222
      IF v_Aux!=0 THEN
carlos@0
   223
	  	SELECT rl.line, dp.c_Bankstatementline_Id, dp.c_cashline_id, dp.c_settlement_cancel_id, dp.ispaid, dp.cancel_processed
carlos@0
   224
		INTO v_line, v_Bankstatementline_ID, v_CashLine_ID, v_Settlement_Cancel_ID, v_ispaid, v_Cancel_Processed
carlos@0
   225
		FROM C_REMITTANCELINE rl, C_DEBT_PAYMENT dp
carlos@0
   226
		WHERE rl.c_debt_payment_id=dp.c_debt_payment_id
carlos@0
   227
		AND rl.C_REMITTANCELINE_ID = v_Remittanceline_ID;
carlos@0
   228
	       IF v_Bankstatementline_ID IS NOT NULL THEN
carlos@0
   229
			   	 SELECT C_BANKSTATEMENT.NAME, C_BANKSTATEMENT.STATEMENTDATE
carlos@0
   230
				 INTO v_nameBankstatement, v_dateBankstatement
carlos@0
   231
				 FROM C_BANKSTATEMENT, C_BANKSTATEMENTLINE
carlos@0
   232
				 WHERE C_BANKSTATEMENT.C_BANKSTATEMENT_ID = C_BANKSTATEMENTLINE.C_BANKSTATEMENT_ID
carlos@0
   233
				 AND C_BANKSTATEMENTLINE.C_BANKSTATEMENTLINE_ID = v_Bankstatementline_ID;
carlos@0
   234
          		 RAISE_APPLICATION_ERROR(-20000, '@Inline@'||v_line||' '||'@debtpayment@'||' '||'@ConciliatedDebtPaymentBank@'||v_nameBankstatement||' '||'@Bydate@'||v_dateBankstatement) ;
carlos@0
   235
			   END IF;
carlos@0
   236
			   IF v_CashLine_ID IS NOT NULL THEN
carlos@0
   237
			   	 SELECT C_CASH.NAME, C_CASH.STATEMENTDATE
carlos@0
   238
				 INTO v_nameCash, v_dateCash
carlos@0
   239
				 FROM C_CASH, C_CASHLINE
carlos@0
   240
				 WHERE C_CASH.C_CASH_ID = C_CASHLINE.C_CASH_ID
carlos@0
   241
				 AND C_CASHLINE.C_CASHLINE_ID = v_CashLine_ID;
carlos@0
   242
          		 RAISE_APPLICATION_ERROR(-20000, '@Inline@'||v_line||' '||'@debtpayment@'||' '||'@ConciliatedDebtPaymentCash@'||v_nameCash||' '||'@Bydate@'||v_dateCash) ;
carlos@0
   243
			   END IF;
carlos@0
   244
			   IF v_Cancel_Processed='Y' AND v_ispaid='N' THEN
carlos@0
   245
			   	 SELECT documentno, datetrx
carlos@0
   246
				 INTO v_documentno_Settlement, v_dateSettlement
carlos@0
   247
				 FROM C_SETTLEMENT
carlos@0
   248
				 WHERE C_SETTLEMENT_ID = v_Settlement_Cancel_ID;
carlos@0
   249
				 RAISE_APPLICATION_ERROR(-20000, '@Inline@'||v_line||' '||'@debtpayment@'||' '||'@ConciliatedDebtPaymentCancel@'||v_documentno_Settlement||' '||'@Bydate@'||v_dateSettlement) ;
carlos@0
   250
			   END IF;
carlos@0
   251
carlos@0
   252
carlos@0
   253
      END IF;
carlos@0
   254
    END IF;--FINISH_PROCESS
carlos@0
   255
    --Processing...
carlos@0
   256
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   257
      IF v_Consolidate='Y' THEN
carlos@0
   258
        --Create Settlement to insert debt payments
carlos@0
   259
        v_SettlementDocType_ID:=Ad_Get_Doctype(v_AD_Client_ID, v_AD_Org_ID, TO_CHAR('STT')) ;
carlos@0
   260
        Ad_Sequence_Next('C_Settlement', v_AD_Org_Id, v_SettlementID) ;
carlos@0
   261
        Ad_Sequence_Doctype(v_SettlementDocType_ID, v_Record_ID, 'Y', v_DocumentNo) ;
carlos@0
   262
        IF(v_DocumentNo IS NULL) THEN
carlos@0
   263
          Ad_Sequence_Doc('DocumentNo_C_Settlement', v_AD_Client_ID, 'Y', v_DocumentNo) ;
carlos@0
   264
        END IF;
carlos@0
   265
        INSERT
carlos@0
   266
        INTO C_SETTLEMENT
carlos@0
   267
          (
carlos@0
   268
            C_SETTLEMENT_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   269
            CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   270
            DOCUMENTNO, DATETRX, DATEACCT, SETTLEMENTTYPE,
carlos@0
   271
            C_DOCTYPE_ID, C_Currency_ID, ISGENERATED
carlos@0
   272
          )
carlos@0
   273
          VALUES
carlos@0
   274
          (
carlos@0
   275
            v_SettlementID, v_AD_Client_Id, v_AD_Org_Id, 'Y',
antonio@735
   276
            now(), v_AD_User_ID, now(), v_AD_User_ID,
carlos@0
   277
            '*RT*'||v_DocumentNo, v_dateTrx, v_dateTrx, 'C',
carlos@0
   278
            v_SettlementDocType_ID, v_Currency, 'Y'
carlos@0
   279
          )
carlos@0
   280
          ;
carlos@0
   281
        --Create DPManagement
carlos@0
   282
        v_DPManagementDocType_ID:=Ad_Get_Doctype(v_AD_Client_ID, v_AD_Org_ID, TO_CHAR('DPM')) ;
carlos@0
   283
        Ad_Sequence_Next('C_DP_Management', v_AD_Org_ID, v_DPMId) ;
carlos@0
   284
        Ad_Sequence_Doctype(v_DPManagementDocType_ID, v_Record_ID, 'Y', v_DocumentNo) ;
carlos@0
   285
        IF(v_DocumentNo IS NULL) THEN
carlos@0
   286
          Ad_Sequence_Doc('DocumentNo_C_DP_Management', v_AD_Client_ID, 'Y', v_DocumentNo) ;
carlos@0
   287
        END IF;
carlos@0
   288
        INSERT
carlos@0
   289
        INTO C_DP_MANAGEMENT
carlos@0
   290
          (
carlos@0
   291
            C_DP_MANAGEMENT_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   292
            CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   293
            DOCUMENTNO, DATETRX, C_DOCTYPE_ID, DATEACCT,
carlos@0
   294
            C_Currency_ID
carlos@0
   295
          )
carlos@0
   296
          VALUES
carlos@0
   297
          (
carlos@0
   298
            v_DPMId, v_ad_Client_Id, v_AD_Org_Id, 'Y',
antonio@735
   299
            now(), v_AD_User_ID, now(), v_AD_User_ID,
carlos@0
   300
            v_DocumentNo, v_DateTrx, v_DPManagementDocType_ID, v_DateTrx,
carlos@0
   301
            v_Currency
carlos@0
   302
          )
carlos@0
   303
          ;
carlos@0
   304
        v_GeneratedAmt:=0;
carlos@0
   305
        v_line:=0;
carlos@0
   306
		SELECT b.C_BPartner_id
carlos@0
   307
		INTO v_BankPartner
carlos@0
   308
		FROM C_REMITTANCE r,
carlos@0
   309
		C_BANKACCOUNT ba,
carlos@0
   310
		C_BANK b
carlos@0
   311
		WHERE r.C_Remittance_ID=v_Record_Id
carlos@0
   312
		AND r.C_BankAccount_ID=ba.c_BankAccount_ID
carlos@0
   313
		AND ba.C_Bank_ID=b.C_Bank_ID;
carlos@0
   314
carlos@0
   315
        FOR Cur_RemittanceLines IN
carlos@0
   316
          (SELECT dp.C_DEBT_PAYMENT_ID,
carlos@0
   317
            dp.AD_CLIENT_ID,
carlos@0
   318
            dp.AD_ORG_ID,
carlos@0
   319
            dp.ISACTIVE,
carlos@0
   320
            dp.CREATED,
carlos@0
   321
            dp.CREATEDBY,
carlos@0
   322
            dp.UPDATED,
carlos@0
   323
            dp.UPDATEDBY,
carlos@0
   324
            dp.ISRECEIPT,
carlos@0
   325
            dp.C_CURRENCY_ID,
carlos@0
   326
            dp.PAYMENTRULE,
carlos@0
   327
            dp.ISPAID,
carlos@0
   328
            dp.AMOUNT,
carlos@0
   329
            dp.DATEPLANNED,
carlos@0
   330
            dp.ISMANUAL,
carlos@0
   331
            dp.STATUS,
carlos@0
   332
            dp.C_BPartner_id,
carlos@0
   333
            dp.description,
carlos@0
   334
            rl.C_REMITTANCELINE_ID
carlos@0
   335
          FROM C_REMITTANCELINE rl,
carlos@0
   336
            C_DEBT_PAYMENT dp
carlos@0
   337
          WHERE C_Remittance_ID=v_Record_ID
carlos@0
   338
            AND dp.C_Debt_Payment_id=rl.c_Debt_Payment_ID
carlos@0
   339
          )
carlos@0
   340
        LOOP
carlos@0
   341
          Ad_Sequence_Next('C_Debt_Payment', Cur_RemittanceLines.AD_Org_ID, v_DPId) ;
carlos@0
   342
          INSERT
carlos@0
   343
          INTO C_DEBT_PAYMENT
carlos@0
   344
            (
carlos@0
   345
              C_DEBT_PAYMENT_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   346
              CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   347
              ISRECEIPT, C_CURRENCY_ID, PAYMENTRULE, ISPAID,
carlos@0
   348
              AMOUNT, DATEPLANNED, ISMANUAL, STATUS,
carlos@0
   349
              C_SETTLEMENT_GENERATE_ID, isValid, c_BPartner_ID, Generate_Processed,
carlos@0
   350
              Description, C_BANKACCOUNT_ID, status_initial
carlos@0
   351
            )
carlos@0
   352
            VALUES
carlos@0
   353
            (
carlos@0
   354
              v_DPID, Cur_RemittanceLines.ad_Client_Id, Cur_RemittanceLines.AD_Org_Id, 'Y',
antonio@735
   355
              now(), v_AD_User_ID, now(), v_AD_User_ID,
carlos@0
   356
              Cur_RemittanceLines.ISRECEIPT, Cur_RemittanceLines.C_CURRENCY_ID, Cur_RemittanceLines.PAYMENTRULE, Cur_RemittanceLines.ISPAID,
carlos@0
   357
              -Cur_RemittanceLines.AMOUNT, Cur_RemittanceLines.DATEPLANNED, 'N', Cur_RemittanceLines.STATUS,
carlos@0
   358
              v_SettlementID, 'Y', v_BankPartner, 'Y',
carlos@0
   359
               '*RT* '||Cur_RemittanceLines.description, v_BankAccountID, Cur_RemittanceLines.STATUS
carlos@0
   360
            )
carlos@0
   361
            ;
carlos@0
   362
          UPDATE C_REMITTANCELINE
carlos@0
   363
            SET C_DEBT_PAYMENT_CANCELLED=v_DPID
carlos@0
   364
          WHERE C_RemittanceLine_ID=Cur_RemittanceLines.c_RemittanceLine_id;
eduardo@450
   365
          /*v_line:=v_line + 10;
carlos@0
   366
          Ad_Sequence_Next('C_DP_ManagementLine', v_AD_Org_ID, v_DPMLineId) ;
carlos@0
   367
          INSERT
carlos@0
   368
          INTO C_DP_MANAGEMENTLINE
carlos@0
   369
            (
carlos@0
   370
              C_DP_MANAGEMENTLINE_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   371
              CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   372
              C_DP_MANAGEMENT_ID, STATUS_FROM, STATUS_TO, C_DEBT_PAYMENT_ID,
carlos@0
   373
              LINE
carlos@0
   374
            )
carlos@0
   375
            VALUES
carlos@0
   376
            (
carlos@0
   377
              v_DPMLineId, v_ad_Client_Id, v_AD_Org_Id, 'Y',
antonio@735
   378
              now(), v_AD_User_ID, now(), v_AD_User_ID,
carlos@0
   379
              v_DPMId, Cur_RemittanceLines.Status, v_Status_To, v_DPId,
carlos@0
   380
              v_line
carlos@0
   381
            )
eduardo@450
   382
            ;*/
carlos@0
   383
          v_line:=v_line + 10;
carlos@0
   384
          Ad_Sequence_Next('C_DP_ManagementLine', v_AD_Org_ID, v_DPMLineId) ;
carlos@0
   385
          INSERT
carlos@0
   386
          INTO C_DP_MANAGEMENTLINE
carlos@0
   387
            (
carlos@0
   388
              C_DP_MANAGEMENTLINE_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   389
              CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   390
              C_DP_MANAGEMENT_ID, STATUS_FROM, STATUS_TO, C_DEBT_PAYMENT_ID,
carlos@0
   391
              LINE
carlos@0
   392
            )
carlos@0
   393
            VALUES
carlos@0
   394
            (
carlos@0
   395
              v_DPMLineId, v_aD_Client_Id, v_AD_Org_Id, 'Y',
antonio@735
   396
              now(), v_AD_User_ID, now(), v_AD_User_ID,
carlos@0
   397
              v_DPMId, Cur_RemittanceLines.Status, v_Status_To, Cur_RemittanceLines.c_Debt_Payment_ID,
carlos@0
   398
              v_line
carlos@0
   399
            )
carlos@0
   400
            ;
carlos@0
   401
          --Calculate the total amount
carlos@0
   402
          --v_GeneratedAmt := v_GeneratedAmt + i.Amount;
carlos@0
   403
        END LOOP;
carlos@0
   404
        SELECT COALESCE(SUM(C_Currency_Round(  C_Currency_Convert(Amount, C_Currency_ID, v_Currency, v_dateTrx, NULL, v_aD_Client_Id, v_AD_Org_Id), v_Currency, NULL)), 0)
carlos@0
   405
        INTO v_GeneratedAmt
carlos@0
   406
        FROM C_Debt_Payment_V
carlos@0
   407
        WHERE C_Settlement_Generate_ID=v_SettlementID
carlos@0
   408
          AND isActive='Y'
carlos@0
   409
          AND isManual='N';
carlos@0
   410
        --Post DPManagement
antonio@735
   411
        C_DP_MANAGEMENT_POST(NULL, v_DPMId) ;
gorkaion@239
   412
        IF v_GeneratedAmt>0 THEN
carlos@0
   413
          v_IsReceipt:='N';
carlos@0
   414
        ELSE
carlos@0
   415
          v_IsReceipt:='Y';
carlos@0
   416
        END IF;
carlos@0
   417
        --Generate total debpayemet for the bank
carlos@0
   418
        Ad_Sequence_Next('C_Debt_Payment', v_AD_Org_ID, v_DPId) ;
carlos@0
   419
        INSERT
carlos@0
   420
        INTO C_DEBT_PAYMENT
carlos@0
   421
          (
carlos@0
   422
            C_DEBT_PAYMENT_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   423
            CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   424
            ISRECEIPT, C_CURRENCY_ID, PAYMENTRULE, ISPAID,
carlos@0
   425
            AMOUNT, DATEPLANNED, ISMANUAL, STATUS,
carlos@0
   426
            C_SETTLEMENT_GENERATE_ID, IsValid, C_BPartner_id, Generate_Processed,
carlos@0
   427
            C_BANKACCOUNT_ID, status_initial
carlos@0
   428
          )
carlos@0
   429
          VALUES
carlos@0
   430
          (
carlos@0
   431
            v_DPID, v_AD_Client_Id, v_AD_Org_Id, 'Y',
antonio@735
   432
            now(), v_AD_User_ID, now(), v_AD_User_ID,
carlos@0
   433
            v_ISRECEIPT, v_Currency, v_PaymetRule, 'N',
carlos@0
   434
            ABS(v_GeneratedAmt), v_DueDate, 'N', 'DE',
carlos@0
   435
            v_SettlementID, 'Y', v_BankPartner, 'Y',
carlos@0
   436
            v_BankAccountID, 'DE'
carlos@0
   437
          )
carlos@0
   438
          ;
carlos@0
   439
        --Post Settlement
antonio@735
   440
        C_SETTLEMENT_POST(NULL, v_SettlementID) ;
carlos@0
   441
        UPDATE C_REMITTANCE
carlos@0
   442
          SET C_Settlement_ID=v_SettlementID,
carlos@0
   443
          C_DP_Management_ID=v_DPMId,
carlos@0
   444
          Processed='Y'
carlos@0
   445
        WHERE C_Remittance_ID=v_Record_ID;
carlos@0
   446
        UPDATE C_DEBT_PAYMENT
carlos@0
   447
          SET C_BankAccount_ID=v_BankAccountID
carlos@0
   448
        WHERE C_Debt_Payment_ID IN
carlos@0
   449
          (SELECT C_Debt_Payment_Id
carlos@0
   450
          FROM C_REMITTANCELINE
carlos@0
   451
          WHERE C_Remittance_ID=v_Record_ID
carlos@0
   452
          )
carlos@0
   453
          ;
carlos@0
   454
      ELSE --Consolidate='N'
carlos@0
   455
        --Create DPManagement
carlos@0
   456
        v_DPManagementDocType_ID:=Ad_Get_Doctype(v_AD_Client_ID, v_AD_Org_ID, TO_CHAR('DPM')) ;
carlos@0
   457
        Ad_Sequence_Next('C_DP_Management', v_AD_Org_ID, v_DPMId) ;
carlos@0
   458
        Ad_Sequence_Doctype(v_DPManagementDocType_ID, v_Record_ID, 'Y', v_DocumentNo) ;
carlos@0
   459
        IF(v_DocumentNo IS NULL) THEN
carlos@0
   460
          Ad_Sequence_Doc('DocumentNo_C_DP_Management', v_AD_Client_ID, 'Y', v_DocumentNo) ;
carlos@0
   461
        END IF;
carlos@0
   462
        INSERT
carlos@0
   463
        INTO C_DP_MANAGEMENT
carlos@0
   464
          (
carlos@0
   465
            C_DP_MANAGEMENT_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   466
            CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   467
            DOCUMENTNO, DATETRX, C_DOCTYPE_ID, DATEACCT,
carlos@0
   468
            C_Currency_ID
carlos@0
   469
          )
carlos@0
   470
          VALUES
carlos@0
   471
          (
carlos@0
   472
            v_DPMId, v_ad_Client_Id, v_AD_Org_Id, 'Y',
antonio@735
   473
            now(), v_AD_User_ID, now(), v_AD_User_ID,
carlos@0
   474
            v_DocumentNo, v_DateTrx, v_DPManagementDocType_ID, v_DueDate,
carlos@0
   475
            v_Currency
carlos@0
   476
          )
carlos@0
   477
          ;
carlos@0
   478
        v_line:=0;
carlos@0
   479
        FOR Cur_RemittanceLines IN
carlos@0
   480
          (SELECT dp.C_DEBT_PAYMENT_ID,
carlos@0
   481
            dp.AD_CLIENT_ID,
carlos@0
   482
            dp.AD_ORG_ID,
carlos@0
   483
            dp.ISACTIVE,
carlos@0
   484
            dp.CREATED,
carlos@0
   485
            dp.CREATEDBY,
carlos@0
   486
            dp.UPDATED,
carlos@0
   487
            dp.UPDATEDBY,
carlos@0
   488
            dp.ISRECEIPT,
carlos@0
   489
            dp.C_CURRENCY_ID,
carlos@0
   490
            dp.PAYMENTRULE,
carlos@0
   491
            dp.ISPAID,
carlos@0
   492
            dp.AMOUNT,
carlos@0
   493
            dp.DATEPLANNED,
carlos@0
   494
            dp.ISMANUAL,
carlos@0
   495
            dp.STATUS,
carlos@0
   496
            dp.C_BPartner_id,
carlos@0
   497
            dp.description,
carlos@0
   498
            rl.C_REMITTANCELINE_ID
carlos@0
   499
          FROM C_REMITTANCELINE rl,
carlos@0
   500
            C_DEBT_PAYMENT dp
carlos@0
   501
          WHERE C_Remittance_ID=v_Record_ID
carlos@0
   502
            AND dp.C_Debt_Payment_id=rl.c_Debt_Payment_ID
carlos@0
   503
          )
carlos@0
   504
        LOOP
carlos@0
   505
          Ad_Sequence_Next('C_DP_ManagementLine', v_AD_Org_ID, v_DPMLineId) ;
carlos@0
   506
          v_line:=v_line + 10;
carlos@0
   507
          INSERT
carlos@0
   508
          INTO C_DP_MANAGEMENTLINE
carlos@0
   509
            (
carlos@0
   510
              C_DP_MANAGEMENTLINE_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   511
              CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   512
              C_DP_MANAGEMENT_ID, STATUS_FROM, STATUS_TO, C_DEBT_PAYMENT_ID,
carlos@0
   513
              LINE
carlos@0
   514
            )
carlos@0
   515
            VALUES
carlos@0
   516
            (
carlos@0
   517
              v_DPMLineId, v_aD_Client_Id, v_AD_Org_Id, 'Y',
antonio@735
   518
              now(), v_AD_User_ID, now(), v_AD_User_ID,
carlos@0
   519
              v_DPMId, Cur_RemittanceLines.Status, v_Status_To, Cur_RemittanceLines.c_Debt_Payment_ID,
carlos@0
   520
              v_line
carlos@0
   521
            )
carlos@0
   522
            ;
carlos@0
   523
        END LOOP;
carlos@0
   524
        --Post DPManagement
antonio@735
   525
        C_DP_MANAGEMENT_POST(NULL, v_DPMId) ;
carlos@0
   526
        UPDATE C_REMITTANCE
carlos@0
   527
          SET C_DP_Management_ID=v_DPMId,
carlos@0
   528
          Processed='Y'
carlos@0
   529
        WHERE C_Remittance_ID=v_Record_ID;
carlos@0
   530
        UPDATE C_DEBT_PAYMENT
carlos@0
   531
          SET C_BankAccount_ID=v_BankAccountID
carlos@0
   532
        WHERE C_Debt_Payment_ID IN
carlos@0
   533
          (SELECT C_Debt_Payment_Id
carlos@0
   534
          FROM C_REMITTANCELINE
carlos@0
   535
          WHERE C_Remittance_ID=v_Record_ID
carlos@0
   536
          )
carlos@0
   537
          ;
carlos@0
   538
      END IF;
carlos@0
   539
    END IF;--FINISH_PROCESS
gorkaion@239
   540
    --<<FINISH_PROCESS>>
carlos@0
   541
    --  Update AD_PInstance
carlos@0
   542
    UPDATE C_REMITTANCE SET Processing = 'N' WHERE C_Remittance_ID = v_Record_ID;
carlos@0
   543
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   544
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
antonio@735
   545
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
carlos@0
   546
    ELSE
gorkaion@239
   547
      DBMS_OUTPUT.PUT_LINE('--<<C_DP_Management_Post finished>> ' || v_Message) ;
carlos@0
   548
      IF(v_Result=0) THEN
carlos@0
   549
        RAISE_APPLICATION_ERROR(-20100, v_Message) ;
carlos@0
   550
      END IF;
carlos@0
   551
    END IF;
carlos@0
   552
    RETURN;
carlos@0
   553
  END; --BODY
carlos@0
   554
EXCEPTION
carlos@0
   555
WHEN OTHERS THEN
carlos@0
   556
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   557
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   558
  UPDATE C_REMITTANCE SET Processing = 'N' WHERE C_Remittance_ID = v_Record_ID;
carlos@0
   559
  IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   560
    ROLLBACK;
antonio@735
   561
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   562
  ELSE
carlos@0
   563
    RAISE;
carlos@0
   564
  END IF;
carlos@0
   565
  RETURN;
antonio@735
   566
END C_REMITTANCE_POST
gorkaion@239
   567
]]></body>
carlos@0
   568
    </function>
adrian@94
   569
  </database>