database/model/functions/C_INVOICE_POST.xml
author Adrián Romero <adrian.romero@openbravo.com>
Wed, 05 Dec 2007 20:43:45 +0000
changeset 170 4b5493776a7f
parent 132 33c5ca45fcff
child 239 a30acae847e1
permissions -rw-r--r--
Bug fixed: [ 1845110 ] Database model not in sync with database created
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION C_INVOICE_POST">
adrian@94
     3
    <function name="C_INVOICE_POST" type="NULL">
adrian@94
     4
      <parameter name="p_PInstance_ID" type="NUMERIC" mode="in"/>
adrian@94
     5
      <parameter name="p_Invoice_ID" type="NUMERIC" mode="in"/>
adrian@170
     6
      <body>/*************************************************************************
carlos@0
     7
  * The contents of this file are subject to the Compiere License
carlos@0
     8
  * Version 2.5.0 ("License"); You may not use this file except in
carlos@0
     9
  * compliance with the License. You may obtain a copy of the License at
carlos@0
    10
  * http://www.compiere.org/license.html
carlos@0
    11
  * Software distributed under the License is distributed on an
carlos@0
    12
  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
carlos@0
    13
  * implied. See the License for the specific language governing rights
carlos@0
    14
  * and limitations under the License.
adrian@170
    15
  * The Original Code is  Compiere  ERP &amp;  Business Solution
carlos@0
    16
  * The Initial Developer of the Original Code is Jorg Janke  and ComPiere,
carlos@0
    17
  Inc.
carlos@0
    18
  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
carlos@0
    19
  * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
carlos@0
    20
  * All Rights Reserved.
carlos@0
    21
  * Contributor(s): Openbravo SL
carlos@0
    22
  * Contributions are Copyright y 1999-2007 Openbravo, S.L
carlos@0
    23
  *************************************************************************
carlos@0
    24
  * $Id: C_Invoice_Post.sql,v 1.32 2003/07/22 05:41:27 jjanke Exp $
carlos@0
    25
  ***
carlos@0
    26
  * Title:  Post single Invoice
carlos@0
    27
  * Description:
carlos@0
    28
  *  Actions: COmplete, APprove, Reverse Correction, Void
carlos@0
    29
  *
carlos@0
    30
  * OpenItem Amount:
carlos@0
    31
  *  - C_BPartner.SO_CreditUsed is increased
carlos@0
    32
  * - if C_CashLine entry is created
carlos@0
    33
  *  - C_Cash_Post creates C_Allocation
carlos@0
    34
  *   - C_Allocation_Trg decreases C_BPartner.SO_CreditUsed
carlos@0
    35
  *
carlos@0
    36
  ************************************************************************/
carlos@0
    37
  -- Logistice
carlos@0
    38
  v_ResultStr VARCHAR2(2000):='';
carlos@0
    39
  v_Message VARCHAR2(2000):='';
carlos@0
    40
  v_Record_ID NUMBER;
carlos@0
    41
  v_Result NUMBER:=1; -- Success
carlos@0
    42
  v_TOTAL NUMBER;
carlos@0
    43
  v_C_Settlement_Cancel_ID NUMBER;
carlos@0
    44
  -- Parameter
carlos@0
    45
  TYPE RECORD IS REF CURSOR;
carlos@0
    46
    Cur_Parameter RECORD;
carlos@0
    47
carlos@0
    48
    Cur_line RECORD;
carlos@0
    49
    -- Record Info
carlos@0
    50
    v_Client_ID NUMBER;
carlos@0
    51
    v_Org_ID NUMBER;
carlos@0
    52
    v_UpdatedBy C_INVOICE.UpdatedBy%TYPE;
carlos@0
    53
    v_Processing C_INVOICE.Processing%TYPE;
carlos@0
    54
    v_Processed C_INVOICE.Processed%TYPE;
carlos@0
    55
    v_DocAction C_INVOICE.DocAction%TYPE;
carlos@0
    56
    v_DocStatus C_INVOICE.DocStatus%TYPE;
carlos@0
    57
    v_DoctypeReversed_ID NUMBER;
carlos@0
    58
    v_DocType_ID NUMBER;
carlos@0
    59
    v_DocTypeTarget_ID NUMBER;
carlos@0
    60
    v_IsApproved C_INVOICE.IsApproved%TYPE;
carlos@0
    61
    v_IsTransferred C_INVOICE.IsTransferred%TYPE;
carlos@0
    62
    v_PaymentRule C_INVOICE.PaymentRule%TYPE;
carlos@0
    63
    v_PaymentTerm C_INVOICE.C_PaymentTerm_ID%TYPE;
carlos@0
    64
    v_Order_ID NUMBER;
carlos@0
    65
    v_DateAcct DATE;
carlos@0
    66
    v_DateInvoiced DATE;
carlos@0
    67
    v_DocumentNo C_INVOICE.DocumentNo%TYPE;
carlos@0
    68
    v_BPartner_ID NUMBER;
carlos@0
    69
    v_BPartner_User_ID NUMBER;
carlos@0
    70
    v_IsSOTrx C_INVOICE.IsSOTrx%TYPE;
carlos@0
    71
    v_Posted C_INVOICE.Posted%TYPE;
carlos@0
    72
  --Added by P.SAROBE
carlos@0
    73
  v_documentno_Settlement VARCHAR2(40);
carlos@0
    74
  v_dateSettlement DATE;
carlos@0
    75
  v_Cancel_Processed CHAR(1);
carlos@0
    76
  v_nameBankstatement VARCHAR2 (60);
carlos@0
    77
  v_dateBankstatement DATE;
carlos@0
    78
  v_nameCash VARCHAR2 (60);
carlos@0
    79
  v_dateCash DATE;
carlos@0
    80
  v_Bankstatementline_ID NUMBER;
carlos@0
    81
  v_Debtpayment_ID NUMBER;
carlos@0
    82
  v_CashLine_ID NUMBER;
carlos@0
    83
  v_ispaid CHAR(1);
carlos@0
    84
  v_Settlement_Cancel_ID NUMBER;
carlos@0
    85
  --Finish added by P.Sarobe
carlos@0
    86
    --
carlos@0
    87
    v_GrandTotal NUMBER:=0;
carlos@0
    88
    v_TotalLines NUMBER:=0;
carlos@0
    89
    v_Currency_ID NUMBER;
carlos@0
    90
    v_Multiplier NUMBER:=1;
carlos@0
    91
    --
carlos@0
    92
    v_RInvoice_ID NUMBER;
carlos@0
    93
    v_RDocumentNo C_INVOICE.DocumentNo%TYPE;
carlos@0
    94
    v_NextNo NUMBER;
carlos@0
    95
    v_count NUMBER;
carlos@0
    96
    v_AD_Org_ID NUMBER;
carlos@0
    97
    v_POReference NVARCHAR2(40) ;
carlos@0
    98
    --
carlos@0
    99
    v_SettlementDocType_ID NUMBER(10) ;
carlos@0
   100
    v_SDocumentNo C_SETTLEMENT.DocumentNo%TYPE;
carlos@0
   101
    v_settlementID NUMBER(10):=NULL;
carlos@0
   102
    --
carlos@0
   103
    v_FirstSales C_BPARTNER.FirstSale%TYPE;
carlos@0
   104
    v_REInOutStatus M_INOUT.DocStatus%TYPE;
carlos@0
   105
    v_RECount NUMBER:=0;
carlos@0
   106
    v_REDateInvoiced DATE;
carlos@0
   107
    v_REtotalQtyInvoiced NUMBER:=0;
carlos@0
   108
    v_REdeliveredQty NUMBER:=0;
carlos@0
   109
    --
carlos@0
   110
    v_CumDiscount NUMBER;
carlos@0
   111
    v_OldCumDiscount NUMBER;
carlos@0
   112
    v_InvoiceLineSeqNo NUMBER;
carlos@0
   113
    v_InvoiceLine NUMBER;
carlos@0
   114
    v_Discount NUMBER;
carlos@0
   115
    v_Line NUMBER;
carlos@0
   116
    v_InvoiceDiscount NUMBER;
carlos@0
   117
    v_C_Project_Id NUMBER;
carlos@0
   118
 v_acctAmount NUMBER;
carlos@0
   119
 v_realAmount NUMBER;
carlos@0
   120
    Cur_InvoiceLine RECORD;
carlos@0
   121
    Cur_Discount RECORD;
carlos@0
   122
    Cur_CInvoiceDiscount RECORD;
carlos@0
   123
    Cur_TaxDiscount RECORD;
carlos@0
   124
    Cur_ReactivateInvoiceLine RECORD;
carlos@0
   125
    Cur_LastContact RECORD;
carlos@0
   126
    FINISH_PROCESS BOOLEAN:=FALSE;
carlos@0
   127
    END_PROCESSING BOOLEAN:=FALSE;
carlos@0
   128
    V_Aux NUMBER;
carlos@0
   129
    v_TargetDocBaseType C_DOCTYPE.DocBaseType%TYPE;
carlos@0
   130
    v_MultiplierARC NUMBER:=1;
carlos@0
   131
  BEGIN
carlos@0
   132
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   133
      --  Update AD_PInstance
carlos@0
   134
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
   135
      v_ResultStr:='PInstanceNotFound';
carlos@0
   136
      Ad_Update_Pinstance(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
   137
      -- Get Parameters
carlos@0
   138
      v_ResultStr:='ReadingParameters';
carlos@0
   139
      FOR Cur_Parameter IN
carlos@0
   140
        (SELECT i.Record_ID,
carlos@0
   141
          i.AD_User_ID,
carlos@0
   142
          p.ParameterName,
carlos@0
   143
          p.P_String,
carlos@0
   144
          p.P_Number,
carlos@0
   145
          p.P_Date
carlos@0
   146
        FROM AD_PINSTANCE i
carlos@0
   147
        LEFT JOIN AD_PINSTANCE_PARA p
carlos@0
   148
          ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
   149
        WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
   150
        ORDER BY p.SeqNo
carlos@0
   151
        )
carlos@0
   152
      LOOP
carlos@0
   153
        v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
   154
      END LOOP; -- Get Parameter
carlos@0
   155
      DBMS_OUTPUT.PUT_LINE('  v_Record_ID=' || v_Record_ID) ;
carlos@0
   156
    ELSE
adrian@170
   157
      DBMS_OUTPUT.PUT_LINE('--&lt;&lt;C_Invoive_Post&gt;&gt;') ;
carlos@0
   158
      v_Record_ID:=p_Invoice_ID;
carlos@0
   159
    END IF;
carlos@0
   160
  BEGIN --BODY
carlos@0
   161
carlos@0
   162
   /*Invoiceline acct dimension*/
carlos@0
   163
 SELECT C_INVOICE.ISSOTRX INTO v_IsSOTrx
carlos@0
   164
 FROM C_INVOICE
carlos@0
   165
 WHERE C_INVOICE_ID = v_Record_ID;
carlos@0
   166
 IF (v_IsSOTrx = 'N') THEN
carlos@0
   167
  FOR Cur_line IN
carlos@0
   168
     (SELECT C_INVOICELINE.C_InvoiceLine_ID,
carlos@0
   169
   C_INVOICELINE.LinenetAmt
carlos@0
   170
   FROM C_INVOICELINE
carlos@0
   171
   WHERE C_Invoice_ID = v_Record_ID
carlos@0
   172
  )
carlos@0
   173
      LOOP
carlos@0
   174
     SELECT SUM(Amt) INTO v_acctAmount
carlos@0
   175
  FROM C_INVOICELINE_ACCTDIMENSION
carlos@0
   176
  WHERE C_InvoiceLine_ID = Cur_line.C_InvoiceLine_ID;
adrian@170
   177
  IF (v_acctAmount &lt;&gt; Cur_line.LinenetAmt) THEN
carlos@0
   178
     v_Message:='@QuantitiesNotMatch@';
carlos@0
   179
          RAISE_APPLICATION_ERROR(-20000, '@QuantitiesNotMatch@') ;
carlos@0
   180
  END IF;
carlos@0
   181
      END LOOP;
carlos@0
   182
 END IF;
carlos@0
   183
    /**
carlos@0
   184
    * Read Invoice
carlos@0
   185
    */
carlos@0
   186
    v_ResultStr:='ReadingInvoice';
carlos@0
   187
    SELECT Processing, Processed, DocAction, DocStatus,
carlos@0
   188
      C_DocType_ID, C_DocTypeTarget_ID, IsApproved, IsTransferred,
carlos@0
   189
      PaymentRule, C_PaymentTerm_ID, DateAcct, DateInvoiced,
carlos@0
   190
      AD_Client_ID, AD_Org_ID, UpdatedBy, DocumentNo,
carlos@0
   191
      C_Order_ID, IsSOTrx, C_BPartner_ID, AD_User_ID,
carlos@0
   192
      C_Currency_ID, AD_Org_ID, POReference, Posted,
carlos@0
   193
      c_Project_Id
carlos@0
   194
    INTO v_Processing, v_Processed, v_DocAction, v_DocStatus,
carlos@0
   195
      v_DocType_ID, v_DocTypeTarget_ID, v_IsApproved, v_IsTransferred,
carlos@0
   196
      v_PaymentRule, v_PaymentTerm, v_DateAcct, v_DateInvoiced,
carlos@0
   197
      v_Client_ID, v_Org_ID, v_UpdatedBy, v_DocumentNo,
carlos@0
   198
      v_Order_ID, v_IsSOTrx, v_BPartner_ID, v_BPartner_User_ID,
carlos@0
   199
      v_Currency_ID, v_AD_Org_ID, v_POReference, v_Posted,
carlos@0
   200
      v_C_Project_Id
carlos@0
   201
    FROM C_INVOICE
carlos@0
   202
    WHERE C_Invoice_ID=v_Record_ID  FOR UPDATE;
carlos@0
   203
    DBMS_OUTPUT.PUT_LINE('Invoice_ID=' || v_Record_ID ||', DocAction=' || v_DocAction || ', DocStatus=' || v_DocStatus || ', DocType_ID=' || v_DocType_ID || ', DocTypeTarget_ID=' || v_DocTypeTarget_ID || ', IsApproved=' || v_IsApproved) ;
carlos@0
   204
    /**
carlos@0
   205
    * Invoice Voided, Closed, or Reversed - No Action
carlos@0
   206
    */
carlos@0
   207
    IF(v_DocStatus IN('VO', 'CL', 'RE')) THEN
carlos@0
   208
      RAISE_APPLICATION_ERROR(-20000, '@AlreadyPosted@');
carlos@0
   209
    END IF;
carlos@0
   210
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   211
      /**
carlos@0
   212
      * Unlock
carlos@0
   213
      */
carlos@0
   214
      IF(v_DocAction='XL') THEN
carlos@0
   215
        UPDATE C_INVOICE
carlos@0
   216
          SET Processing='N',
carlos@0
   217
          DocAction='--',
carlos@0
   218
          Updated=Now()
carlos@0
   219
        WHERE C_Invoice_ID=v_Record_ID;
carlos@0
   220
        FINISH_PROCESS:=TRUE;
carlos@0
   221
      END IF;
carlos@0
   222
    END IF;--FINISH_PROCESS
carlos@0
   223
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   224
      IF(v_Processing='Y') THEN
carlos@0
   225
        RAISE_APPLICATION_ERROR(-20000, '@OtherProcessActive@');
carlos@0
   226
      END IF;
carlos@0
   227
    END IF;--FINISH_PROCESS
carlos@0
   228
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   229
      /**
carlos@0
   230
      * Everything done
carlos@0
   231
      */
carlos@0
   232
      IF(v_Processed='Y' AND v_DocAction NOT IN('RC', 'RE')) THEN
carlos@0
   233
    RAISE_APPLICATION_ERROR(-20000, '@AlreadyPosted@');
carlos@0
   234
      END IF;
carlos@0
   235
    END IF;--FINISH_PROCESS
carlos@0
   236
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   237
      /**
carlos@0
   238
      * Void if Document not processed
carlos@0
   239
      */
carlos@0
   240
      IF(v_DocAction='VO' AND v_DocStatus NOT IN('CO', 'RE')) THEN
carlos@0
   241
        SELECT COUNT(*)
carlos@0
   242
          INTO v_Aux
carlos@0
   243
          FROM C_DEBT_PAYMENT
carlos@0
   244
        WHERE C_Invoice_ID = v_Record_ID;
adrian@170
   245
        IF V_Aux&gt;0 THEN
carlos@0
   246
      RAISE_APPLICATION_ERROR(-20000, '@InvoiceWithManualDP@');
carlos@0
   247
        ELSE
carlos@0
   248
          -- Reset Lines to 0
carlos@0
   249
          UPDATE C_INVOICELINE
carlos@0
   250
            SET QtyInvoiced=0,
carlos@0
   251
            LineNetAmt=0
carlos@0
   252
          WHERE C_Invoice_ID=v_Record_ID;
carlos@0
   253
          --
carlos@0
   254
          UPDATE C_INVOICE
carlos@0
   255
            SET DocStatus='VO',
carlos@0
   256
            DocAction='--',
carlos@0
   257
            Processed='Y',
carlos@0
   258
            Updated=Now()
carlos@0
   259
          WHERE C_Invoice_ID=v_Record_ID;
carlos@0
   260
          --
carlos@0
   261
        END IF;
carlos@0
   262
        FINISH_PROCESS:=TRUE;
carlos@0
   263
      END IF;
carlos@0
   264
    END IF;--FINISH_PROCESS
carlos@0
   265
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   266
      /**************************************************************************
carlos@0
   267
      * Start Processing ------------------------------------------------------
carlos@0
   268
      *************************************************************************/
carlos@0
   269
      v_ResultStr:='LockingInvoice';
carlos@0
   270
      BEGIN -- FOR COMMIT
carlos@0
   271
        UPDATE C_INVOICE  SET Processing='Y'  WHERE C_Invoice_ID=v_Record_ID;
carlos@0
   272
        -- Now, needs to go to END_PROCESSING to unlock
carlos@0
   273
        -- This Commit must remanin due differences between PL execution in Oracle and Postgres
carlos@0
   274
        IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   275
           COMMIT;
carlos@0
   276
        END IF;
carlos@0
   277
      EXCEPTION WHEN OTHERS THEN RAISE NO_DATA_FOUND;
carlos@0
   278
      END;--FOR  COMMIT
carlos@0
   279
      BEGIN -- FOR COMMIT
carlos@0
   280
carlos@0
   281
   -- Set org lines like the headear
carlos@0
   282
     UPDATE C_INVOICELINE
carlos@0
   283
      SET AD_ORG_ID = (SELECT AD_ORG_ID FROM C_INVOICE WHERE C_INVOICE_ID = v_Record_ID)
carlos@0
   284
    WHERE C_INVOICE_ID = v_Record_ID;
carlos@0
   285
carlos@0
   286
    SELECT COUNT(*)
carlos@0
   287
     INTO v_Count
carlos@0
   288
    FROM C_INVOICE C,
carlos@0
   289
         C_DOCTYPE
carlos@0
   290
   WHERE C_DOCTYPE.DocBaseType IN ('ARI', 'API','ARC','APC')
carlos@0
   291
    AND C_DOCTYPE.IsSOTrx=C.ISSOTRX
adrian@170
   292
    AND Ad_Isorgincluded(C.AD_Org_ID,C_DOCTYPE.AD_Org_ID, C.AD_Client_ID) &lt;&gt; -1
carlos@0
   293
    AND C.C_DOCTYPETARGET_ID = C_DOCTYPE.C_DOCTYPE_ID
carlos@0
   294
    AND C.C_INVOICE_ID = V_RECORD_ID;
carlos@0
   295
   IF v_Count=0 THEN
carlos@0
   296
        RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgDoctypeInvoice@');
carlos@0
   297
      END IF;
carlos@0
   298
carlos@0
   299
carlos@0
   300
carlos@0
   301
        /**
carlos@0
   302
        * Reverse Correction requires completes invoice ========================
carlos@0
   303
        */
carlos@0
   304
        IF(v_DocAction='RC' AND v_DocStatus='CO') THEN
carlos@0
   305
          v_ResultStr:='ReverseCorrection';
carlos@0
   306
          -- Copy Invoice with reverese Quantities (or Amounts)
carlos@0
   307
          Ad_Sequence_Next('C_Invoice', v_Record_ID, v_RInvoice_ID) ;
carlos@0
   308
          SELECT COALESCE(C_DOCTYPE_REVERSED_ID, C_DOCTYPE_ID)
carlos@0
   309
          INTO v_DoctypeReversed_ID
carlos@0
   310
          FROM C_DOCTYPE
carlos@0
   311
          WHERE C_DOCTYPE_ID=v_DocType_ID;
carlos@0
   312
          Ad_Sequence_Doctype(v_DoctypeReversed_ID, v_Record_ID, 'Y', v_RDocumentNo) ;
carlos@0
   313
          IF(v_RDocumentNo IS NULL) THEN
carlos@0
   314
            Ad_Sequence_Doc('DocumentNo_C_Invoice', v_Client_ID, 'Y', v_RDocumentNo) ;
carlos@0
   315
          END IF;
carlos@0
   316
          v_Message:='@ReversedBy@: ' || v_RDocumentNo;
carlos@0
   317
          --
carlos@0
   318
          DBMS_OUTPUT.PUT_LINE('Reversal Invoice_ID=' || v_RInvoice_ID || ' DocumentNo=' || v_RDocumentNo) ;
carlos@0
   319
          v_ResultStr:='InsertInvoice ID=' || v_RInvoice_ID;
carlos@0
   320
          -- Don't copy C_Payment_ID or C_CashLine_ID
carlos@0
   321
          INSERT
carlos@0
   322
          INTO C_INVOICE
carlos@0
   323
            (
carlos@0
   324
              C_Invoice_ID, C_Order_ID, AD_Client_ID, AD_Org_ID,
carlos@0
   325
              IsActive, Created, CreatedBy, Updated,
carlos@0
   326
              UpdatedBy, IsSOTrx, DocumentNo, DocStatus,
carlos@0
   327
              DocAction, Processing, Processed, C_DocType_ID,
carlos@0
   328
              C_DocTypeTarget_ID, Description, IsApproved, IsTransferred,
carlos@0
   329
              SalesRep_ID, DateInvoiced, DatePrinted, IsPrinted,
carlos@0
   330
              DateAcct, C_PaymentTerm_ID, C_BPartner_ID, C_BPartner_Location_ID,
carlos@0
   331
              AD_User_ID, POReference, DateOrdered, IsDiscountPrinted,
carlos@0
   332
              C_Currency_ID, PaymentRule, C_Charge_ID, ChargeAmt,
carlos@0
   333
              TotalLines, GrandTotal, M_PriceList_ID, C_Campaign_ID,
carlos@0
   334
              C_Project_ID, C_Activity_ID, AD_OrgTrx_ID, User1_ID,
carlos@0
   335
              User2_ID
carlos@0
   336
            )
carlos@0
   337
          SELECT v_RInvoice_ID, C_Order_ID, AD_Client_ID, AD_Org_ID,
carlos@0
   338
            IsActive, Now(), UpdatedBy, Now(),
carlos@0
   339
            UpdatedBy, IsSOTrx, v_RDocumentNo, 'DR',
carlos@0
   340
             'CO', 'N', 'N', v_DoctypeReversed_ID,
carlos@0
   341
            v_DoctypeReversed_ID, '(*R*: ' || DocumentNo || ') ' || Description, IsApproved, 'N',
carlos@0
   342
            SalesRep_ID, DateInvoiced, NULL, 'N',
carlos@0
   343
            DateAcct, C_PaymentTerm_ID, C_BPartner_ID, C_BPartner_Location_ID,
carlos@0
   344
            AD_User_ID, POReference, DateOrdered, IsDiscountPrinted,
carlos@0
   345
            C_Currency_ID, PaymentRule, C_Charge_ID, ChargeAmt * -1,
carlos@0
   346
            TotalLines * -1, GrandTotal * -1, M_PriceList_ID, C_Campaign_ID,
carlos@0
   347
            C_Project_ID, C_Activity_ID, AD_OrgTrx_ID, User1_ID,
carlos@0
   348
            User2_ID
carlos@0
   349
          FROM C_INVOICE
carlos@0
   350
          WHERE C_Invoice_ID=v_Record_ID;
carlos@0
   351
          --  Delete C_Invoice_Discounts inserted by the trigger
carlos@0
   352
          DELETE FROM C_INVOICE_DISCOUNT WHERE C_Invoice_ID=v_RInvoice_ID;
carlos@0
   353
          --  Insert discounts as in the reversed invoice
carlos@0
   354
          FOR Cur_Discount IN
carlos@0
   355
            (SELECT *
carlos@0
   356
            FROM C_INVOICE_DISCOUNT
carlos@0
   357
            WHERE C_Invoice_ID=v_Record_ID
carlos@0
   358
            ORDER BY LINE
carlos@0
   359
            )
carlos@0
   360
          LOOP
carlos@0
   361
            Ad_Sequence_Next('C_Invoice_Discount', v_Record_ID, v_NextNo) ;
carlos@0
   362
            INSERT
carlos@0
   363
            INTO C_INVOICE_DISCOUNT
carlos@0
   364
              (
carlos@0
   365
                C_INVOICE_DISCOUNT_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   366
                CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   367
                C_INVOICE_ID, C_DISCOUNT_ID, LINE, CASCADE
carlos@0
   368
              )
carlos@0
   369
              VALUES
carlos@0
   370
              (
carlos@0
   371
                v_NextNo, Cur_Discount.AD_Client_ID, Cur_Discount.AD_Org_ID, 'Y',
carlos@0
   372
                Now(), v_UpdatedBy, Now(), v_UpdatedBy,
carlos@0
   373
                v_RInvoice_ID, Cur_Discount.C_Discount_ID, Cur_Discount.Line, Cur_Discount.CASCADE
carlos@0
   374
              )
carlos@0
   375
              ;
carlos@0
   376
          END LOOP;
carlos@0
   377
          -- Create Reversal Invoice Lines
carlos@0
   378
          FOR Cur_InvoiceLine IN
carlos@0
   379
            (SELECT *
carlos@0
   380
            FROM C_INVOICELINE
carlos@0
   381
            WHERE C_Invoice_ID=v_Record_ID
carlos@0
   382
              AND C_INVOICE_DISCOUNT_ID IS NULL
carlos@0
   383
            ORDER BY Line
carlos@0
   384
            )
carlos@0
   385
          LOOP
carlos@0
   386
            Ad_Sequence_Next('C_InvoiceLine', v_Record_ID, v_NextNo) ;
carlos@0
   387
            INSERT
carlos@0
   388
            INTO C_INVOICELINE
carlos@0
   389
              (
carlos@0
   390
                C_InvoiceLine_ID, AD_Client_ID, AD_Org_ID, IsActive,
carlos@0
   391
                Created, CreatedBy, Updated, UpdatedBy,
carlos@0
   392
                C_Invoice_ID, C_OrderLine_ID, M_InOutLine_ID, Line,
carlos@0
   393
                Description, M_Product_ID, QtyInvoiced, PriceList,
carlos@0
   394
                PriceActual, LineNetAmt, C_Charge_ID, ChargeAmt,
carlos@0
   395
                C_UOM_ID, C_Tax_ID, PriceStd)
carlos@0
   396
              VALUES
carlos@0
   397
              (
carlos@0
   398
                v_NextNo, Cur_InvoiceLine.AD_Client_ID, Cur_InvoiceLine.AD_Org_ID, 'Y',
carlos@0
   399
                Now(), v_UpdatedBy, Now(), v_UpdatedBy,
carlos@0
   400
                v_RInvoice_ID, Cur_InvoiceLine.C_OrderLine_ID, Cur_InvoiceLine.M_InoutLine_ID, Cur_InvoiceLine.Line,
carlos@0
   401
                 '*R*: ' || Cur_InvoiceLine.Description, Cur_InvoiceLine.M_Product_ID, Cur_InvoiceLine.QtyInvoiced * -1, Cur_InvoiceLine.PriceList,
carlos@0
   402
                Cur_InvoiceLine.PriceActual, Cur_InvoiceLine.LineNetAmt * -1, Cur_InvoiceLine.C_Charge_ID, Cur_InvoiceLine.ChargeAmt * -1,
carlos@0
   403
                Cur_InvoiceLine.C_UOM_ID, Cur_InvoiceLine.C_Tax_ID, Cur_InvoiceLine.PriceStd)
carlos@0
   404
              ;
carlos@0
   405
            /* OrderLine.qtyInvoiced is updated in c_invoicePost
carlos@0
   406
            UPDATE C_OrderLine
carlos@0
   407
            SET QtyInvoiced = QtyInvoiced - Cur_InvoiceLine.QtyInvoiced,
carlos@0
   408
            Updated = now()
carlos@0
   409
            WHERE C_OrderLine_ID=Cur_InvoiceLine.C_OrderLine_ID; */
carlos@0
   410
          END LOOP; -- Create Reversal Invoice Lines
carlos@0
   411
          -- Close Invoice
carlos@0
   412
          UPDATE C_INVOICE
carlos@0
   413
            SET DocStatus='RE', -- it IS reversed
adrian@170
   414
            Description=COALESCE(TO_CHAR(Description), '') || ' (*R* -&gt; ' || v_RDocumentNo || ')',
carlos@0
   415
            DocAction='--',
carlos@0
   416
            Processed='Y',
carlos@0
   417
            Updated=Now()
carlos@0
   418
          WHERE C_Invoice_ID=v_Record_ID;
carlos@0
   419
          -- Post Reversal
carlos@0
   420
          C_Invoice_Post(NULL, v_RInvoice_ID) ;
carlos@0
   421
          -- Reversal Transaction is closed
carlos@0
   422
          UPDATE C_INVOICE
carlos@0
   423
            SET DocStatus='CL', -- the reversal transaction
carlos@0
   424
            DocAction='--',
carlos@0
   425
            Processed='Y'
carlos@0
   426
          WHERE C_Invoice_ID=v_RInvoice_ID;
carlos@0
   427
          -- If Payments from both invoices are pending, create a new settlement and cancel them
carlos@0
   428
          SELECT COUNT(*)
carlos@0
   429
          INTO v_count
carlos@0
   430
          FROM C_DEBT_PAYMENT dp
adrian@170
   431
          WHERE 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)&lt;&gt;'P'
carlos@0
   432
            AND(dp.C_Invoice_ID=v_Record_ID
carlos@0
   433
            OR dp.C_Invoice_ID=v_RInvoice_ID) ;
carlos@0
   434
          -- To cancel, the sum of amounts should be 0
carlos@0
   435
          IF(v_count=0) THEN
carlos@0
   436
            SELECT SUM(AMOUNT)
carlos@0
   437
            INTO v_count
carlos@0
   438
            FROM C_DEBT_PAYMENT dp
carlos@0
   439
            WHERE dp.C_Invoice_ID=v_Record_ID
carlos@0
   440
              OR dp.C_Invoice_ID=v_RInvoice_ID;
carlos@0
   441
            IF(v_count=0) THEN
carlos@0
   442
              v_SettlementDocType_ID:=Ad_Get_Doctype(v_Client_ID, v_AD_Org_ID, TO_CHAR('STT')) ;
carlos@0
   443
              Ad_Sequence_Next('C_Settlement', v_Record_ID, v_settlementID) ;
carlos@0
   444
              Ad_Sequence_Doctype(v_SettlementDocType_ID, v_Record_ID, 'Y', v_SDocumentNo) ;
carlos@0
   445
              IF(v_SDocumentNo IS NULL) THEN
carlos@0
   446
                Ad_Sequence_Doc('DocumentNo_C_Settlement', v_Client_ID, 'Y', v_SDocumentNo) ;
carlos@0
   447
              END IF;
carlos@0
   448
              INSERT
carlos@0
   449
              INTO C_SETTLEMENT
carlos@0
   450
                (
carlos@0
   451
                  C_SETTLEMENT_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   452
                  CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   453
                  DOCUMENTNO, DATETRX, DATEACCT, SETTLEMENTTYPE,
carlos@0
   454
                  C_DOCTYPE_ID, PROCESSING, PROCESSED, POSTED,
carlos@0
   455
                  C_CURRENCY_ID, C_PROJECT_ID, C_CAMPAIGN_ID, C_ACTIVITY_ID,
carlos@0
   456
                  USER1_ID, USER2_ID, CREATEFROM, ISGENERATED
carlos@0
   457
                )
carlos@0
   458
              SELECT v_settlementID, AD_Client_ID, AD_Org_ID, 'Y',
carlos@0
   459
                Now(), UpdatedBy, Now(), UpdatedBy,
carlos@0
   460
                '*RE*'||v_SDocumentNo, TRUNC(Now()), TRUNC(Now()), 'C',
carlos@0
   461
                v_SettlementDocType_ID, 'N', 'N', 'N',
carlos@0
   462
                C_Currency_ID, C_PROJECT_ID, C_CAMPAIGN_ID, C_ACTIVITY_ID,
carlos@0
   463
                USER1_ID, USER2_ID, 'N', 'Y'
carlos@0
   464
              FROM C_INVOICE
carlos@0
   465
              WHERE C_Invoice_ID=v_Record_ID;
carlos@0
   466
              UPDATE C_DEBT_PAYMENT
carlos@0
   467
                SET C_Settlement_Cancel_id=v_settlementID,
carlos@0
   468
                UPDATED=Now(),
carlos@0
   469
                UPDATEDBY=v_UpdatedBy
carlos@0
   470
              WHERE C_DEBT_PAYMENT.C_Invoice_ID=v_Record_ID
carlos@0
   471
                OR C_DEBT_PAYMENT.C_Invoice_ID=v_RInvoice_ID;
carlos@0
   472
              C_Settlement_Post(NULL, v_settlementID) ;
carlos@0
   473
            END IF;
carlos@0
   474
          END IF;
carlos@0
   475
          END_PROCESSING:=TRUE;
carlos@0
   476
        END IF;
carlos@0
   477
      EXCEPTION WHEN OTHERS THEN RAISE NO_DATA_FOUND;
carlos@0
   478
      END; -- FOR COMMIT
carlos@0
   479
    END IF;--FINISH_PROCESS
carlos@0
   480
      IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
carlos@0
   481
        /**************************************************************************
carlos@0
   482
        * Credit Multiplier
carlos@0
   483
        *************************************************************************/
carlos@0
   484
        DECLARE
carlos@0
   485
          v_DocBaseType C_DOCTYPE.DocBaseType%TYPE;
carlos@0
   486
        BEGIN
carlos@0
   487
          -- Is it a Credit Memo?
carlos@0
   488
          SELECT DocBaseType
carlos@0
   489
          INTO v_DocBaseType
carlos@0
   490
          FROM C_DOCTYPE
carlos@0
   491
          WHERE C_DocType_ID=v_DocType_ID;
carlos@0
   492
          IF(v_DocBaseType IN('ARC', 'API')) THEN
carlos@0
   493
            v_Multiplier:=-1;
carlos@0
   494
          END IF;
carlos@0
   495
        END;
carlos@0
   496
      END IF;--FINISH_PROCESS
carlos@0
   497
      IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
carlos@0
   498
        /************************************************************************
carlos@0
   499
        * Actions allowed: Reactivate
carlos@0
   500
        * Modified by Jon Alegria
carlos@0
   501
        */
carlos@0
   502
        IF(v_DocAction='RE') THEN
adrian@170
   503
          IF(v_DocStatus&lt;&gt;'CO') THEN
carlos@0
   504
            RAISE_APPLICATION_ERROR(-20000, '@NotCompletedInvoice@');
carlos@0
   505
          END IF;
carlos@0
   506
          IF(NOT END_PROCESSING) THEN
carlos@0
   507
            IF(v_Posted='Y') THEN
carlos@0
   508
              RAISE_APPLICATION_ERROR(-20000, '@InvoiceDocumentPosted@');
carlos@0
   509
            END IF;
carlos@0
   510
          END IF;--END_PROCESSING
carlos@0
   511
          IF(NOT END_PROCESSING) THEN
carlos@0
   512
            SELECT COUNT(*), MAX(C_DEBT_PAYMENT_ID)
carlos@0
   513
            INTO v_RECount, v_Debtpayment_ID
carlos@0
   514
            FROM C_DEBT_PAYMENT
carlos@0
   515
            WHERE C_DEBT_PAYMENT.C_Invoice_ID=v_Record_ID
adrian@170
   516
            AND C_Debt_Payment_Status(C_Settlement_Cancel_ID, Cancel_Processed, C_DEBT_PAYMENT.Generate_Processed, IsPaid, IsValid, C_Cashline_ID, C_BankstatementLine_ID)&lt;&gt;'P'
carlos@0
   517
            AND C_ORDER_ID IS NULL;
adrian@170
   518
            IF(v_RECount&lt;&gt;0) THEN
carlos@0
   519
              --Added by P.Sarobe. New messages
carlos@0
   520
          SELECT c_Bankstatementline_Id, c_cashline_id, c_settlement_cancel_id, ispaid, cancel_processed
carlos@0
   521
          INTO v_Bankstatementline_ID, v_CashLine_ID, v_Settlement_Cancel_ID, v_ispaid, v_Cancel_Processed
carlos@0
   522
          FROM C_DEBT_PAYMENT WHERE C_Debt_Payment_ID = v_Debtpayment_ID;
carlos@0
   523
               IF v_Bankstatementline_ID IS NOT NULL THEN
carlos@0
   524
                SELECT C_BANKSTATEMENT.NAME, C_BANKSTATEMENT.STATEMENTDATE
carlos@0
   525
            INTO v_nameBankstatement, v_dateBankstatement
carlos@0
   526
            FROM C_BANKSTATEMENT, C_BANKSTATEMENTLINE
carlos@0
   527
            WHERE C_BANKSTATEMENT.C_BANKSTATEMENT_ID = C_BANKSTATEMENTLINE.C_BANKSTATEMENT_ID
carlos@0
   528
            AND C_BANKSTATEMENTLINE.C_BANKSTATEMENTLINE_ID = v_Bankstatementline_ID;
carlos@0
   529
                  RAISE_APPLICATION_ERROR(-20000, '@ManagedDebtPaymentInvoiceBank@'||v_nameBankstatement||' '||'@Bydate@'||v_dateBankstatement) ;
carlos@0
   530
             END IF;
carlos@0
   531
             IF v_CashLine_ID IS NOT NULL THEN
carlos@0
   532
                SELECT C_CASH.NAME, C_CASH.STATEMENTDATE
carlos@0
   533
            INTO v_nameCash, v_dateCash
carlos@0
   534
            FROM C_CASH, C_CASHLINE
carlos@0
   535
            WHERE C_CASH.C_CASH_ID = C_CASHLINE.C_CASH_ID
carlos@0
   536
            AND C_CASHLINE.C_CASHLINE_ID = v_CashLine_ID;
carlos@0
   537
                  RAISE_APPLICATION_ERROR(-20000, '@ManagedDebtPaymentInvoiceCash@'||v_nameCash||' '||'@Bydate@'||v_dateCash) ;
carlos@0
   538
             END IF;
carlos@0
   539
             IF v_Cancel_Processed='Y' AND v_ispaid='N' THEN
carlos@0
   540
                SELECT documentno, datetrx
carlos@0
   541
            INTO v_documentno_Settlement, v_dateSettlement
carlos@0
   542
            FROM C_SETTLEMENT
carlos@0
   543
            WHERE C_SETTLEMENT_ID = v_Settlement_Cancel_ID;
carlos@0
   544
              RAISE_APPLICATION_ERROR(-20000, '@ManagedDebtPaymentOrderCancel@'||v_documentno_Settlement||' '||'@Bydate@'||v_dateSettlement) ;
carlos@0
   545
             END IF;
carlos@0
   546
            END IF;
carlos@0
   547
          END IF;--END_PROCESSING--To be fixed or deprecated
carlos@0
   548
          IF(NOT END_PROCESSING) THEN
carlos@0
   549
            SELECT COUNT(*)
carlos@0
   550
            INTO v_RECount
carlos@0
   551
            FROM C_DP_MANAGEMENTLINE ml,
carlos@0
   552
              C_DP_MANAGEMENT m,
carlos@0
   553
              C_DEBT_PAYMENT dp
carlos@0
   554
            WHERE ml.C_DP_Management_ID=m.C_DP_Management_ID
carlos@0
   555
              AND ml.C_Debt_Payment_ID=dp.C_Debt_Payment_ID
carlos@0
   556
              AND dp.C_Invoice_ID=v_Record_ID
carlos@0
   557
              AND m.processed='Y';
carlos@0
   558
            IF v_RECount!=0 THEN
carlos@0
   559
              RAISE_APPLICATION_ERROR(-20000, '@DPInvoiceManaged@');
carlos@0
   560
            END IF;
carlos@0
   561
          END IF;--END_PROCESSING
carlos@0
   562
          IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
carlos@0
   563
            -- Pending undo not Stocked BOM's
carlos@0
   564
            -- Undo BP Statictis
carlos@0
   565
            --Undo first sale
carlos@0
   566
            SELECT MIN(DateAcct)
carlos@0
   567
            INTO v_FirstSales
carlos@0
   568
            FROM C_INVOICE
adrian@170
   569
            WHERE C_Invoice_ID&lt;&gt;v_Record_ID
carlos@0
   570
              AND C_BPartner_ID=v_BPartner_ID;
carlos@0
   571
            UPDATE C_BPARTNER  SET FirstSale=v_FirstSales  WHERE C_BPartner_ID=v_BPartner_ID;
carlos@0
   572
            -- Undo Last contact
carlos@0
   573
            FOR Cur_LastContact IN
carlos@0
   574
              (SELECT Updated,
carlos@0
   575
                DocumentNo,
carlos@0
   576
                Ad_User_ID
carlos@0
   577
              FROM C_INVOICE
adrian@170
   578
              WHERE C_Invoice_ID&lt;&gt;v_Record_ID
carlos@0
   579
                AND Ad_User_ID=v_BPartner_User_ID
carlos@0
   580
              ORDER BY Updated DESC
carlos@0
   581
              )
carlos@0
   582
            LOOP
carlos@0
   583
              UPDATE AD_USER
carlos@0
   584
                SET LastContact=Cur_LastContact.Updated,
carlos@0
   585
                LastResult=Cur_LastContact.DocumentNo
carlos@0
   586
              WHERE AD_User_ID=Cur_LastContact.Ad_User_ID;
carlos@0
   587
              EXIT;
carlos@0
   588
            END LOOP;
carlos@0
   589
            IF(v_IsSOTrx='Y') THEN
carlos@0
   590
              -- Undo revenue and credit limit
carlos@0
   591
              UPDATE C_BPARTNER
carlos@0
   592
                SET ActualLifeTimeValue=ActualLifeTimeValue -(v_Multiplier *  C_Base_Convert(v_GrandTotal, v_Currency_ID, v_Client_ID, v_DateAcct, v_Org_ID))
carlos@0
   593
              WHERE C_BPartner_ID=v_BPartner_ID;
carlos@0
   594
              FOR Cur_ReactivateInvoiceLine IN
carlos@0
   595
                (SELECT C_InvoiceLine_ID,
carlos@0
   596
                  C_Orderline_ID,
carlos@0
   597
                  M_InoutLine_ID,
carlos@0
   598
                  QtyInvoiced
carlos@0
   599
                FROM C_INVOICELINE
carlos@0
   600
                WHERE C_Invoice_ID=v_Record_ID
carlos@0
   601
                )
carlos@0
   602
              LOOP
carlos@0
   603
                IF(Cur_ReactivateInvoiceLine.C_OrderLine_ID IS NOT NULL) THEN
carlos@0
   604
                  SELECT MAX(C_INVOICE.DateInvoiced)
carlos@0
   605
                  INTO v_REDateInvoiced
carlos@0
   606
                  FROM C_INVOICE,
carlos@0
   607
                    C_INVOICELINE
carlos@0
   608
                  WHERE C_INVOICE.C_Invoice_ID=C_INVOICELINE.C_INVOICE_ID
carlos@0
   609
                    AND C_INVOICELINE.C_ORDERLINE_ID=Cur_ReactivateInvoiceLine.C_ORDERLINE_ID
adrian@170
   610
                    AND C_INVOICELINE.C_InvoiceLine_ID&lt;&gt;Cur_ReactivateInvoiceLine.C_InvoiceLine_ID;
carlos@0
   611
                  UPDATE C_ORDERLINE
carlos@0
   612
                    SET QtyInvoiced=QtyInvoiced - Cur_ReactivateInvoiceLine.QtyInvoiced,
carlos@0
   613
                    DateInvoiced=v_REDateInvoiced
carlos@0
   614
                  WHERE C_ORDERLINE.C_OrderLine_ID=Cur_ReactivateInvoiceLine.C_OrderLine_ID;
carlos@0
   615
                END IF;
carlos@0
   616
                IF(Cur_ReactivateInvoiceLine.M_InOutLine_ID IS NOT NULL) THEN
carlos@0
   617
                  SELECT m.DOCSTATUS
carlos@0
   618
                  INTO v_REInOutStatus
carlos@0
   619
                  FROM M_INOUT m,
carlos@0
   620
                    M_INOUTLINE ml
carlos@0
   621
                  WHERE M.M_InOut_ID=ml.M_InOut_ID
carlos@0
   622
                    AND ml.M_InOutLine_ID=Cur_ReactivateInvoiceLine.M_InOutLine_ID;
adrian@170
   623
                  IF(v_REInOutStatus&lt;&gt;'RE') THEN
carlos@0
   624
                    SELECT COALESCE(SUM(C_INVOICELINE.QTYINVOICED), 0)
carlos@0
   625
                    INTO v_REtotalQtyInvoiced
carlos@0
   626
                    FROM C_INVOICELINE,
carlos@0
   627
                      C_INVOICE
carlos@0
   628
                    WHERE C_INVOICE.C_Invoice_ID=C_INVOICELINE.C_Invoice_ID
carlos@0
   629
                      AND C_INVOICE.Processed='Y'
carlos@0
   630
                      AND C_INVOICELINE.M_InOutLine_ID=Cur_ReactivateInvoiceLine.M_InOutLine_ID;
carlos@0
   631
                    v_REtotalQtyInvoiced:=v_REtotalQtyInvoiced - Cur_ReactivateInvoiceLine.QtyInvoiced;
carlos@0
   632
                    SELECT MovementQty
carlos@0
   633
                    INTO v_REdeliveredQty
carlos@0
   634
                    FROM M_INOUTLINE
carlos@0
   635
                    WHERE M_InOutLine_ID=Cur_ReactivateInvoiceLine.M_InOutLine_ID;
carlos@0
   636
                    UPDATE M_INOUTLINE
carlos@0
   637
                      SET IsInvoiced=(
carlos@0
   638
                      CASE v_REtotalQtyInvoiced
carlos@0
   639
                        WHEN 0
carlos@0
   640
                        THEN 'N'
carlos@0
   641
                        ELSE 'Y'
carlos@0
   642
                      END
carlos@0
   643
                      )
carlos@0
   644
                    WHERE M_InOutLine_ID=Cur_ReactivateInvoiceLine.M_InOutLine_ID;
carlos@0
   645
                  END IF;
carlos@0
   646
                END IF;
carlos@0
   647
              END LOOP;
carlos@0
   648
            ELSE
carlos@0
   649
              DELETE
carlos@0
   650
              FROM M_MATCHPO
carlos@0
   651
              WHERE C_InvoiceLine_ID IN
carlos@0
   652
                (SELECT C_InvoiceLine_ID FROM C_INVOICELINE WHERE C_Invoice_ID=v_Record_ID)
carlos@0
   653
                ;
carlos@0
   654
              DELETE
carlos@0
   655
              FROM M_MATCHINV
carlos@0
   656
              WHERE C_InvoiceLine_ID IN
carlos@0
   657
                (SELECT C_InvoiceLine_ID FROM C_INVOICELINE WHERE C_Invoice_ID=v_Record_ID)
carlos@0
   658
                ;
carlos@0
   659
            END IF;
carlos@0
   660
            UPDATE C_INVOICE
carlos@0
   661
              SET Processed='N',
carlos@0
   662
              DocStatus='DR',
carlos@0
   663
              DocAction='CO'
carlos@0
   664
            WHERE C_Invoice_Id=v_Record_ID;
carlos@0
   665
            --Delete automatically created records ...
carlos@0
   666
            DELETE
carlos@0
   667
            FROM C_CASHLINE
carlos@0
   668
            WHERE ISGENERATED='Y'
carlos@0
   669
              AND C_DEBT_PAYMENT_ID IN
carlos@0
   670
              (SELECT C_DEBT_PAYMENT_ID FROM C_DEBT_PAYMENT WHERE C_INVOICE_ID=v_Record_ID)
carlos@0
   671
              AND C_CASH_ID IN
carlos@0
   672
              (SELECT C_CASH_ID FROM C_CASH WHERE PROCESSED='N')
carlos@0
   673
              ;
carlos@0
   674
            -- Updates the debt-payments of the cash, to make them not to point to the invoice
carlos@0
   675
            UPDATE C_DEBT_PAYMENT
carlos@0
   676
              SET C_INVOICE_ID=NULL
carlos@0
   677
            WHERE C_Invoice_ID=v_Record_ID
carlos@0
   678
              AND C_Order_ID IS NOT NULL;
carlos@0
   679
            DELETE
carlos@0
   680
            FROM C_DEBT_PAYMENT
carlos@0
   681
            WHERE C_Invoice_ID=v_Record_ID
carlos@0
   682
              AND COALESCE(IsAutomaticGenerated, 'Y')='Y'
carlos@0
   683
              AND C_ORDER_ID IS NULL;
carlos@0
   684
            UPDATE C_DEBT_PAYMENT
carlos@0
   685
              SET IsValid='N'
carlos@0
   686
            WHERE C_Invoice_ID=v_Record_ID
carlos@0
   687
              AND COALESCE(IsAutomaticGenerated, 'Y')='N'
carlos@0
   688
              AND C_ORDER_ID IS NULL;
carlos@0
   689
            C_Bp_Socreditused_Refresh(v_BPartner_ID) ;
carlos@0
   690
            DELETE
carlos@0
   691
            FROM C_INVOICELINE
carlos@0
   692
            WHERE C_INVOICE_DISCOUNT_ID IS NOT NULL
carlos@0
   693
              AND C_INVOICE_ID=v_Record_ID;
carlos@0
   694
            END_PROCESSING:=TRUE;
carlos@0
   695
          END IF;--END_PROCESSING
carlos@0
   696
        END IF;
carlos@0
   697
      END IF;--FINISH_PROCESS
carlos@0
   698
      IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
carlos@0
   699
        /**************************************************************************
carlos@0
   700
        * Actions allowed: COmplete, APprove
carlos@0
   701
        */
carlos@0
   702
        IF(v_DocAction='AP' OR v_DocAction='CO') THEN
carlos@0
   703
          SELECT COUNT(*)
carlos@0
   704
          INTO v_count
carlos@0
   705
          FROM C_INVOICE c,
carlos@0
   706
            C_BPARTNER bp
carlos@0
   707
          WHERE c.C_BPARTNER_ID=bp.C_BPARTNER_ID
carlos@0
   708
            AND Ad_Isorgincluded(c.AD_ORG_ID, bp.AD_ORG_ID, bp.AD_CLIENT_ID)=-1
carlos@0
   709
            AND c.C_Invoice_ID=v_Record_ID;
adrian@170
   710
          IF v_count&gt;0 THEN
carlos@0
   711
            RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgBpartnerInvoice@') ;
carlos@0
   712
          END IF;
carlos@0
   713
          IF(NOT FINISH_PROCESS) THEN
adrian@170
   714
            WHILE(v_DocType_ID&lt;&gt;v_DocTypeTarget_ID)
carlos@0
   715
            LOOP
carlos@0
   716
            BEGIN
carlos@0
   717
              v_ResultStr:='UpdateDocType';
carlos@0
   718
              UPDATE C_INVOICE
carlos@0
   719
                SET C_DocType_ID=C_DocTypeTarget_ID
carlos@0
   720
              WHERE C_Invoice_ID=v_Record_ID;
carlos@0
   721
              v_DocType_ID:=v_DocTypeTarget_ID;
carlos@0
   722
            EXCEPTION
carlos@0
   723
            WHEN OTHERS THEN
carlos@0
   724
              v_ResultStr:='UpdateDocumentNo';
carlos@0
   725
              UPDATE C_INVOICE
carlos@0
   726
                SET DocumentNo=DocumentNo || '.'
carlos@0
   727
              WHERE C_Invoice_ID=v_Record_ID;
carlos@0
   728
            END;
carlos@0
   729
            END LOOP;
carlos@0
   730
          END IF;--FINISH_PROCESS
carlos@0
   731
        ELSE
carlos@0
   732
          v_Message:='@ActionNotAllowedHere@ (I-' || v_DocAction || ')';
carlos@0
   733
      RAISE_APPLICATION_ERROR(-20000, v_Message) ;
carlos@0
   734
          END_PROCESSING:=TRUE;
carlos@0
   735
        END IF;
carlos@0
   736
      END IF;--FINISH_PROCESS
carlos@0
   737
      IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
carlos@0
   738
        /**************************************************************************
carlos@0
   739
        * Resolve not-stocked BOMs
carlos@0
   740
        *************************************************************************/
carlos@0
   741
          DECLARE
carlos@0
   742
          -- Invoice Lines with non-stocked BOMs
carlos@0
   743
          CUR_BOM_Line RECORD;
carlos@0
   744
          CUR_BOM RECORD;
carlos@0
   745
          --
carlos@0
   746
          CountNo NUMBER;
carlos@0
   747
          v_PriceList_Version_ID NUMBER;
carlos@0
   748
          v_NextNo NUMBER;
carlos@0
   749
          v_Line NUMBER;
carlos@0
   750
          v_ChargeAmt NUMBER;
carlos@0
   751
          -- v_Offer_ID       NUMBER;
carlos@0
   752
          v_DateInvoiced DATE;
carlos@0
   753
          v_BPartner_ID NUMBER;
carlos@0
   754
          v_PriceList_ID NUMBER;
carlos@0
   755
          --
carlos@0
   756
          CUR_PriceList_Version RECORD;
carlos@0
   757
        BEGIN
carlos@0
   758
          v_ResultStr:='ResolveBOM';
carlos@0
   759
          LOOP
adrian@170
   760
            -- How many BOMs do we have:1
carlos@0
   761
            SELECT COUNT(*)
carlos@0
   762
            INTO CountNo
carlos@0
   763
            FROM C_INVOICELINE l
carlos@0
   764
            WHERE l.C_Invoice_ID=v_Record_ID
carlos@0
   765
              AND EXISTS
carlos@0
   766
              (SELECT *
carlos@0
   767
              FROM M_PRODUCT p
carlos@0
   768
              WHERE l.M_Product_ID=p.M_Product_ID
carlos@0
   769
                AND p.IsBOM='Y'
carlos@0
   770
                AND p.IsStocked='N'
carlos@0
   771
                AND p.productType='I'
carlos@0
   772
              )
carlos@0
   773
              ;
adrian@170
   774
            -- Nothing to do:2
carlos@3
   775
            EXIT WHEN CountNo=0;
carlos@0
   776
            DBMS_OUTPUT.PUT_LINE('  BOMs to resolve=' || CountNo) ;
carlos@0
   777
            -- Get Price List Version
carlos@0
   778
            FOR CUR_PriceList_Version IN
carlos@0
   779
              (SELECT COALESCE(SUM(v.M_PriceList_Version_ID), 0) AS PriceList_Version_ID, o.M_PriceList_ID AS M_PriceLIst_ID
carlos@0
   780
              FROM M_PRICELIST_VERSION v,
carlos@0
   781
                C_INVOICE o
carlos@0
   782
              WHERE v.M_PriceList_ID=o.M_PriceList_ID
adrian@170
   783
                AND v.ValidFrom&lt;=o.DateOrdered
carlos@0
   784
                AND v.IsActive='Y'
carlos@0
   785
                AND o.C_Invoice_ID=v_Record_ID
carlos@0
   786
              ORDER BY v.ValidFrom DESC
carlos@0
   787
              )
carlos@0
   788
            LOOP
carlos@0
   789
              v_PriceList_Version_ID:=CUR_PriceList_Version.PriceList_Version_ID;
carlos@0
   790
              v_PriceList_ID := CUR_PriceList_Version.M_PriceList_ID;
carlos@0
   791
              EXIT;
carlos@0
   792
            END LOOP;
carlos@0
   793
            --Reference Date for price DateOrdered
carlos@0
   794
            SELECT C_BPARTNER_ID,
carlos@0
   795
              DATEORDERED
carlos@0
   796
            INTO v_BPartner_ID,
carlos@0
   797
              v_DateInvoiced
carlos@0
   798
            FROM C_INVOICE
carlos@0
   799
            WHERE C_INVOICE_ID=v_Record_ID;
carlos@0
   800
            -- Replace Lines
carlos@0
   801
            FOR CUR_BOM_Line IN
carlos@0
   802
              (SELECT *
carlos@0
   803
              FROM C_INVOICELINE l
carlos@0
   804
              WHERE l.C_Invoice_ID=v_Record_ID
carlos@0
   805
                AND IsActive='Y'
carlos@0
   806
                AND EXISTS
carlos@0
   807
                (SELECT *
carlos@0
   808
                FROM M_PRODUCT p
carlos@0
   809
                WHERE l.M_Product_ID=p.M_Product_ID
carlos@0
   810
                  AND p.IsBOM='Y'
carlos@0
   811
                  AND p.IsStocked='N'
carlos@0
   812
                  AND p.productType='I'
carlos@0
   813
                )
carlos@0
   814
              ORDER BY l.Line  FOR UPDATE
carlos@0
   815
              )
carlos@0
   816
            LOOP
carlos@0
   817
              v_Line:=CUR_BOM_Line.Line;
carlos@0
   818
              -- One Time variables
carlos@0
   819
              v_ChargeAmt:=CUR_BOM_Line.ChargeAmt;
carlos@0
   820
              -- Create New Lines
carlos@0
   821
              FOR CUR_BOM IN
carlos@0
   822
                (SELECT b.M_ProductBOM_ID, p.C_UOM_ID, b.BOMQty, b.Description
carlos@0
   823
                FROM M_PRODUCT_BOM b,
carlos@0
   824
                     M_PRODUCT p
carlos@0
   825
                WHERE b.M_Product_ID=CUR_BOM_Line.M_Product_ID
carlos@0
   826
                  AND b.M_ProductBOM_ID=p.M_Product_ID
carlos@0
   827
                ORDER BY Line
carlos@0
   828
                )
carlos@0
   829
              LOOP
carlos@0
   830
                Ad_Sequence_Next('C_InvoiceLine', CUR_BOM_Line.AD_Client_ID, v_NextNo) ;
carlos@0
   831
                v_Line:=v_Line + 10;
carlos@0
   832
                --     v_Offer_ID := M_GET_OFFER(v_DateInvoiced, v_BPartner_ID, CUR_BOM.M_ProductBOM_ID);
carlos@0
   833
                /* INSERT INTO C_INVOICELINE
carlos@0
   834
                (C_InvoiceLine_ID,
carlos@0
   835
                AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,
carlos@0
   836
                C_Invoice_ID,Line,
carlos@0
   837
                Description,
carlos@0
   838
                M_Product_ID,C_UOM_ID,
carlos@0
   839
                QtyInvoiced,
carlos@0
   840
                PriceList,PriceActual,PriceLimit,LineNetAmt,
carlos@0
   841
                C_Charge_ID,ChargeAmt,
carlos@0
   842
                C_Tax_ID,
carlos@0
   843
                PriceStd)
carlos@0
   844
                VALUES
carlos@0
   845
                (v_NextNo,
carlos@0
   846
                CUR_BOM_Line.AD_Client_ID,CUR_BOM_Line.AD_Org_ID,CUR_BOM_Line.IsActive,now(),v_UpdatedBy,now(),v_UpdatedBy,
carlos@0
   847
                CUR_BOM_Line.C_Invoice_ID,v_Line,
carlos@0
   848
                CUR_BOM.Description,
carlos@0
   849
                CUR_BOM.M_ProductBOM_ID, CUR_BOM.C_UOM_ID,
carlos@0
   850
                CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty,
carlos@0
   851
                Bom_Pricelist(CUR_BOM.M_ProductBOM_ID, PriceList_Version_ID),M_GET_OFFER_PRICE(v_Offer_ID, Bom_Pricestd(CUR_BOM.M_ProductBOM_ID, PriceList_Version_ID)td),
carlos@0
   852
                Bom_Pricelimit(CUR_BOM.M_ProductBOM_ID, PriceList_Version_ID),
carlos@0
   853
                M_GET_OFFER_PRICE(v_Offer_ID, Bom_Pricestd(CUR_BOM.M_ProductBOM_ID, PriceList_Version_ID)) * CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty,
carlos@0
   854
                CUR_BOM_Line.C_Charge_ID,v_ChargeAmt,
carlos@0
   855
                CUR_BOM_Line.C_Tax_ID);
carlos@0
   856
                */
carlos@0
   857
                -- Bom_Pricestd(CUR_BOM.M_ProductBOM_ID, PriceList_Version_ID), v_Offer_ID);
carlos@0
   858
                -- One Time variables
carlos@0
   859
                INSERT
carlos@0
   860
                INTO C_INVOICELINE
carlos@0
   861
                  (
carlos@0
   862
                    C_InvoiceLine_ID, AD_Client_ID, AD_Org_ID, IsActive,
carlos@0
   863
                    Created, CreatedBy, Updated, UpdatedBy,
carlos@0
   864
                    C_Invoice_ID, Line, Description, M_Product_ID,
carlos@0
   865
                    C_UOM_ID, QtyInvoiced, PriceList,
carlos@0
   866
                    PriceActual,
carlos@0
   867
                    PriceLimit, LineNetAmt, C_Charge_ID, ChargeAmt,
carlos@0
   868
                    C_Tax_ID, PriceStd
carlos@0
   869
                  )
carlos@0
   870
                  VALUES
carlos@0
   871
                  (
carlos@0
   872
                    v_NextNo, CUR_BOM_Line.AD_Client_ID, CUR_BOM_Line.AD_Org_ID, CUR_BOM_Line.IsActive,
carlos@0
   873
                    Now(), v_UpdatedBy, Now(), v_UpdatedBy,
carlos@0
   874
                    CUR_BOM_Line.C_Invoice_ID, v_Line, CUR_BOM.Description, CUR_BOM.M_ProductBOM_ID,
carlos@0
   875
                    CUR_BOM.C_UOM_ID, CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, Bom_Pricelist(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID),
carlos@0
   876
                    M_Get_Offers_Price(v_DateInvoiced, v_BPartner_ID, CUR_BOM.M_ProductBOM_ID, Bom_Pricestd(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID),CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, v_PriceList_ID),
carlos@0
   877
                    Bom_Pricelimit(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID),
carlos@0
   878
                    M_Get_Offers_Price(v_DateInvoiced, v_BPartner_ID, CUR_BOM.M_ProductBOM_ID, Bom_Pricestd(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID),CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, v_PriceList_ID) * CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, CUR_BOM_Line.C_Charge_ID, v_ChargeAmt,
carlos@0
   879
                    CUR_BOM_Line.C_Tax_ID, Bom_Pricestd(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID)
carlos@0
   880
                  )
carlos@0
   881
                  ;
carlos@0
   882
                v_ChargeAmt:=0;
carlos@0
   883
              END LOOP; -- Create New Lines
carlos@0
   884
              -- Convert into Comment Line
carlos@0
   885
              UPDATE C_INVOICELINE
carlos@0
   886
                SET M_Product_ID=NULL,
carlos@0
   887
                PriceList=0,
carlos@0
   888
                PriceActual=0,
carlos@0
   889
                PriceLimit=0,
carlos@0
   890
                LineNetAmt=0,
carlos@0
   891
                ChargeAmt=0,
carlos@0
   892
                Description=
carlos@0
   893
                (SELECT p.NAME || ' ' || C_INVOICELINE.Description
carlos@0
   894
                FROM M_PRODUCT p
carlos@0
   895
                WHERE p.M_Product_ID=CUR_BOM_Line.M_Product_ID
carlos@0
   896
                )
carlos@0
   897
                ,
carlos@0
   898
                PriceStd=0,
carlos@0
   899
                M_Offer_ID=NULL
carlos@0
   900
              WHERE C_InvoiceLine_ID=CUR_BOM_Line.C_InvoiceLine_ID;
carlos@0
   901
            END LOOP; -- Replace Lines
carlos@0
   902
          END LOOP; -- BOM Loop
carlos@0
   903
        END;
carlos@0
   904
      END IF;--FINISH_PROCESS
carlos@0
   905
      IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
carlos@0
   906
        /**************************************************************************
carlos@0
   907
        * Calculate Discounts
carlos@0
   908
        *************************************************************************/
carlos@0
   909
        v_CumDiscount:=0;
carlos@0
   910
        v_OldCumDiscount:=0;
carlos@0
   911
        v_Line:=10;
carlos@0
   912
        SELECT MAX(LINE) +10
carlos@0
   913
        INTO v_InvoiceLineSeqNo
carlos@0
   914
        FROM C_INVOICELINE
carlos@0
   915
        WHERE C_INVOICE_ID=v_Record_ID;
carlos@0
   916
        FOR Cur_CInvoiceDiscount IN
carlos@0
   917
          (SELECT C_INVOICE_DISCOUNT.C_INVOICE_DISCOUNT_ID,
carlos@0
   918
            C_DISCOUNT.DISCOUNT,
carlos@0
   919
            C_DISCOUNT.M_PRODUCT_ID,
carlos@0
   920
            C_DISCOUNT.NAME,
carlos@0
   921
            C_INVOICE_DISCOUNT.CASCADE,
carlos@0
   922
            C_DISCOUNT.C_DISCOUNT_ID,
carlos@0
   923
            M_PRODUCT.C_UOM_ID
carlos@0
   924
          FROM C_INVOICE_DISCOUNT,
carlos@0
   925
            C_DISCOUNT,
carlos@0
   926
            M_PRODUCT
carlos@0
   927
          WHERE C_INVOICE_DISCOUNT.C_DISCOUNT_ID=C_DISCOUNT.C_DISCOUNT_ID
carlos@0
   928
            AND C_DISCOUNT.M_PRODUCT_ID=M_PRODUCT.M_PRODUCT_ID
carlos@0
   929
            AND C_INVOICE_DISCOUNT.C_INVOICE_ID=v_Record_ID
carlos@0
   930
            AND C_INVOICE_DISCOUNT.ISACTIVE='Y'
carlos@0
   931
          ORDER BY C_INVOICE_DISCOUNT.LINE
carlos@0
   932
          )
carlos@0
   933
        LOOP
carlos@0
   934
          v_CumDiscount:=(1-v_OldCumDiscount) * Cur_CInvoiceDiscount.Discount/100;
carlos@0
   935
          v_OldCumDiscount:=v_OldCumDiscount + v_CumDiscount;
carlos@0
   936
          FOR Cur_TaxDiscount IN
carlos@0
   937
            (SELECT C_INVOICELINE.C_TAX_ID,
carlos@0
   938
              SUM(C_INVOICELINE.LINENETAMT) AS LINENETAMT
carlos@0
   939
            FROM C_INVOICELINE
carlos@0
   940
            WHERE C_INVOICE_ID=v_Record_ID
adrian@170
   941
              AND C_INVOICELINE.LINENETAMT&lt;&gt;0
carlos@0
   942
              AND C_INVOICE_DISCOUNT_ID IS NULL
carlos@0
   943
            GROUP BY C_TAX_ID
carlos@0
   944
            )
carlos@0
   945
          LOOP
carlos@0
   946
            IF(Cur_CInvoiceDiscount.CASCADE='Y') THEN
carlos@0
   947
              v_Discount:=(-1) * Cur_TaxDiscount.LINENETAMT * v_CumDiscount;
carlos@0
   948
            ELSE
carlos@0
   949
              v_Discount:=(-1) * Cur_TaxDiscount.LINENETAMT * Cur_CInvoiceDiscount.Discount/100;
carlos@0
   950
            END IF;
carlos@0
   951
            v_InvoiceLineSeqNo:=10 + v_InvoiceLineSeqNo;
carlos@0
   952
            v_InvoiceLine:=Ad_Sequence_Nextno('C_InvoiceLine') ;
carlos@0
   953
            INSERT
carlos@0
   954
            INTO C_INVOICELINE
carlos@0
   955
              (
carlos@0
   956
                C_INVOICELINE_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   957
                CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   958
                C_INVOICE_ID, C_ORDERLINE_ID, M_INOUTLINE_ID, LINE,
carlos@0
   959
                DESCRIPTION, M_PRODUCT_ID, QTYINVOICED, PRICELIST,
carlos@0
   960
                PRICEACTUAL, PRICELIMIT, LINENETAMT, C_CHARGE_ID,
carlos@0
   961
                CHARGEAMT, C_UOM_ID, C_TAX_ID, S_RESOURCEASSIGNMENT_ID,
carlos@0
   962
                A_ASSET_ID, TAXAMT, M_ATTRIBUTESETINSTANCE_ID, ISDESCRIPTION,
carlos@0
   963
                QUANTITYORDER, M_PRODUCT_UOM_ID, PRICESTD
carlos@0
   964
              )
carlos@0
   965
              VALUES
carlos@0
   966
              (
carlos@0
   967
                v_InvoiceLine, v_Client_ID, v_Org_ID, 'Y',
carlos@0
   968
                Now(), v_UpdatedBy, Now(), v_UpdatedBy,
carlos@0
   969
                v_Record_ID, NULL, NULL, v_InvoiceLineSeqNo,
carlos@0
   970
                 Cur_CInvoiceDiscount.NAME, Cur_CInvoiceDiscount.M_PRODUCT_ID, 1, v_Discount,
carlos@0
   971
                v_Discount, v_Discount, v_Discount, NULL,
carlos@0
   972
                0, Cur_CInvoiceDiscount.C_UOM_ID, Cur_TaxDiscount.C_TAX_ID, NULL,
carlos@0
   973
                NULL, NULL, NULL, 'N' ,
carlos@0
   974
                NULL, NULL, v_Discount
carlos@0
   975
              )
carlos@0
   976
              ;
carlos@0
   977
            --v_InvoiceDiscount:=Ad_Sequence_Nextno('C_InvoiceLine');
carlos@0
   978
            UPDATE C_INVOICELINE
carlos@0
   979
              SET C_INVOICE_DISCOUNT_ID=Cur_CInvoiceDiscount.C_INVOICE_DISCOUNT_ID
carlos@0
   980
            WHERE C_INVOICELINE_ID=v_InvoiceLine;
carlos@0
   981
          END LOOP;
carlos@0
   982
          v_Line:=v_Line + 10;
carlos@0
   983
        END LOOP;
carlos@0
   984
      END IF;--FINISH_PROCESS
carlos@0
   985
      IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
carlos@0
   986
        /**************************************************************************
carlos@0
   987
        * Calculate Invoice Taxes and Totals
carlos@0
   988
        *************************************************************************/
carlos@0
   989
        DECLARE
carlos@0
   990
          Cur_Tax RECORD;
carlos@0
   991
          Cur_MultiTax RECORD;
carlos@0
   992
          xTaxBaseAmt NUMBER:=0;
carlos@0
   993
          xTaxAmt NUMBER:=0;
carlos@0
   994
          v_TaxNoRecalculable NUMBER:=0;
carlos@0
   995
          HeaderNotAdded BOOLEAN:=TRUE;
carlos@0
   996
          v_LineNo NUMBER:=0;
carlos@0
   997
          v_ActualBaseAmt NUMBER;
carlos@0
   998
          v_FirstLine BOOLEAN;
carlos@0
   999
          v_C_InvoiceTax_ID NUMBER(10);
carlos@0
  1000
        BEGIN
carlos@0
  1001
          v_ResultStr:='DeleteOldTaxes';
carlos@0
  1002
          DELETE FROM C_INVOICETAX
carlos@0
  1003
           WHERE C_Invoice_ID=v_Record_ID
carlos@0
  1004
             AND Recalculate = 'Y';
carlos@0
  1005
          -- For all Tax Rates
carlos@0
  1006
          v_ResultStr:='InsertNewTaxes';
carlos@0
  1007
          FOR Cur_Tax IN
carlos@0
  1008
            (SELECT l.C_Tax_ID,
carlos@0
  1009
              i.IsTaxIncluded,
carlos@0
  1010
              COALESCE(SUM(l.LineNetAmt), 0) + COALESCE(SUM(l.ChargeAmt), 0) AS LineNet,
carlos@0
  1011
              COALESCE(SUM(i.ChargeAmt), 0) AS HeaderNet,
carlos@0
  1012
              t.Rate,
carlos@0
  1013
              t.IsSummary,
carlos@0
  1014
              c.StdPrecision
carlos@0
  1015
            FROM C_INVOICE i,
carlos@0
  1016
              C_INVOICELINE l,
carlos@0
  1017
              C_TAX t,
carlos@0
  1018
              C_CURRENCY c
carlos@0
  1019
            WHERE i.C_Invoice_ID=l.C_Invoice_ID
carlos@0
  1020
              AND i.C_Invoice_ID=v_Record_ID -- Parameter
carlos@0
  1021
              AND l.C_Tax_ID=t.C_Tax_ID
carlos@0
  1022
              AND i.C_Currency_ID=c.C_Currency_ID
carlos@0
  1023
            GROUP BY l.C_Tax_ID,
carlos@0
  1024
              i.IsTaxIncluded,
carlos@0
  1025
              t.Rate,
carlos@0
  1026
              t.IsSummary,
carlos@0
  1027
              c.StdPrecision
carlos@0
  1028
            ORDER BY 3 DESC
carlos@0
  1029
            )
carlos@0
  1030
          LOOP
carlos@0
  1031
            xTaxBaseAmt:=Cur_Tax.LineNet;
carlos@0
  1032
            v_TotalLines:=v_TotalLines + xTaxBaseAmt; -- w/o Header Freight/Charge
carlos@0
  1033
            IF(HeaderNotAdded) THEN -- add header to first
carlos@0
  1034
              HeaderNotAdded:=FALSE;
carlos@0
  1035
              xTaxBaseAmt:=xTaxBaseAmt + Cur_Tax.HeaderNet;
carlos@0
  1036
            END IF;
carlos@0
  1037
            IF(Cur_Tax.IsSummary='N') THEN
carlos@0
  1038
              v_LineNo := v_LineNo + 10;
carlos@0
  1039
              xTaxAmt:=ROUND(xTaxBaseAmt * Cur_Tax.Rate / 100, Cur_Tax.StdPrecision) ;
carlos@0
  1040
              v_GrandTotal:=v_GrandTotal + xTaxBaseAmt + xTaxAmt;
carlos@0
  1041
              --
carlos@0
  1042
              Ad_Sequence_Next('C_InvoiceTax', v_Org_ID, v_C_InvoiceTax_ID);
carlos@0
  1043
              INSERT
carlos@0
  1044
              INTO C_INVOICETAX
carlos@0
  1045
                (
carlos@0
  1046
                  C_InvoiceTax_ID, C_Invoice_ID, C_Tax_ID, AD_Client_ID, AD_Org_ID,
carlos@0
  1047
                  IsActive, Created, CreatedBy, Updated,
carlos@0
  1048
                  UpdatedBy, TaxBaseAmt, TaxAmt, Line, Recalculate
carlos@0
  1049
                )
carlos@0
  1050
                VALUES
carlos@0
  1051
                (
carlos@0
  1052
                  v_C_InvoiceTax_ID, v_Record_ID, Cur_Tax.C_Tax_ID, v_Client_ID, v_Org_ID,
carlos@0
  1053
                   'Y', Now(), v_UpdatedBy, Now(),
carlos@0
  1054
                  v_UpdatedBy, xTaxBaseAmt, xTaxAmt, v_LineNo,'Y'
carlos@0
  1055
                )
carlos@0
  1056
                ;
carlos@0
  1057
            ELSE -- Multiple Taxes
carlos@0
  1058
              v_GrandTotal:=v_GrandTotal + xTaxBaseAmt;
carlos@0
  1059
              v_FirstLine := TRUE;
carlos@0
  1060
              FOR Cur_MultiTax IN
carlos@0
  1061
                (SELECT C_Tax_ID, Rate, CASCADE
carlos@0
  1062
                   FROM C_TAX
carlos@0
  1063
                  WHERE Parent_Tax_ID=Cur_Tax.C_Tax_ID
carlos@0
  1064
                  ORDER BY Line)
carlos@0
  1065
              LOOP
carlos@0
  1066
                v_LineNo := v_LineNo + 10;
carlos@0
  1067
                IF ((Cur_MultiTax.CASCADE = 'Y') AND (NOT v_FirstLine)) THEN   --cannot apply cascade to the first line
carlos@0
  1068
                  v_ActualBaseAmt := v_GrandTotal;
carlos@0
  1069
                ELSE
carlos@0
  1070
                  v_ActualBaseAmt := xTaxBaseAmt;
carlos@0
  1071
                END IF;
carlos@0
  1072
                v_FirstLine := FALSE;
carlos@0
  1073
carlos@0
  1074
                xTaxAmt:=ROUND(v_ActualBaseAmt * Cur_MultiTax.Rate / 100, Cur_Tax.StdPrecision) ;
carlos@0
  1075
                v_GrandTotal:=v_GrandTotal + xTaxAmt;
carlos@0
  1076
                --
carlos@0
  1077
                Ad_Sequence_Next('C_InvoiceTax', v_Org_ID, v_C_InvoiceTax_ID);
carlos@0
  1078
                INSERT
carlos@0
  1079
                INTO C_INVOICETAX
carlos@0
  1080
                  (
carlos@0
  1081
                    C_InvoiceTax_ID, C_Invoice_ID, C_Tax_ID, AD_Client_ID, AD_Org_ID,
carlos@0
  1082
                    IsActive, Created, CreatedBy, Updated,
carlos@0
  1083
                    UpdatedBy, TaxBaseAmt, TaxAmt, Line, Recalculate
carlos@0
  1084
                  )
carlos@0
  1085
                  VALUES
carlos@0
  1086
                  (
carlos@0
  1087
                    v_C_InvoiceTax_ID, v_Record_ID, Cur_MultiTax.C_Tax_ID, v_Client_ID, v_Org_ID,
carlos@0
  1088
                     'Y', Now(), v_UpdatedBy, Now(),
carlos@0
  1089
                    v_UpdatedBy, v_ActualBaseAmt, xTaxAmt, v_LineNo, 'Y'
carlos@0
  1090
                  )
carlos@0
  1091
                  ;
carlos@0
  1092
              END LOOP;
carlos@0
  1093
            END IF;
carlos@0
  1094
          END LOOP; -- Insert New Taxes
carlos@0
  1095
          -- Update Header
carlos@0
  1096
          SELECT COALESCE(SUM(TaxAmt),0)
carlos@0
  1097
            INTO v_TaxNoRecalculable
carlos@0
  1098
            FROM C_INVOICETAX
carlos@0
  1099
           WHERE C_Invoice_ID = v_Record_ID
carlos@0
  1100
             AND Recalculate = 'N';
carlos@0
  1101
carlos@0
  1102
          v_GrandTotal:=C_Currency_Round(v_GrandTotal+v_TaxNoRecalculable, v_Currency_ID, NULL) ;
carlos@0
  1103
          UPDATE C_INVOICE
carlos@0
  1104
            SET TotalLines=v_TotalLines,
carlos@0
  1105
            GrandTotal=v_GrandTotal
carlos@0
  1106
          WHERE C_Invoice_ID=v_Record_ID;
carlos@0
  1107
          DBMS_OUTPUT.PUT_LINE('GrandTotal=' || v_GrandTotal) ;
carlos@0
  1108
        END; -- Calculate Tax and Totals
carlos@0
  1109
        -- Synchronize Client/Org Ownership
carlos@0
  1110
        v_ResultStr:='SyncOwnershipClient';
carlos@0
  1111
        UPDATE C_INVOICELINE
carlos@0
  1112
          SET AD_Client_ID=v_Client_ID
carlos@0
  1113
        WHERE C_Invoice_ID=v_Record_ID
adrian@170
  1114
          AND AD_Client_ID&lt;&gt;v_Client_ID;
carlos@0
  1115
        v_ResultStr:='SyncOwnershipOrg';
carlos@0
  1116
        UPDATE C_INVOICELINE
carlos@0
  1117
          SET AD_Org_ID=v_Org_ID
carlos@0
  1118
        WHERE C_Invoice_ID=v_Record_ID
carlos@0
  1119
          AND C_Charge_ID IS NULL -- sync for products
adrian@170
  1120
          AND AD_Org_ID&lt;&gt;v_Org_ID;
carlos@0
  1121
      END IF;--FINISH_PROCESS
carlos@0
  1122
      IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
carlos@0
  1123
        /**************************************************************************
adrian@170
  1124
        * Is Approved:3
carlos@0
  1125
        *************************************************************************/
carlos@0
  1126
        DBMS_OUTPUT.PUT_LINE('Approval - IsApproved=' || v_IsApproved) ;
carlos@0
  1127
        IF(v_IsApproved='N') THEN
carlos@0
  1128
          -- Check if it needs to be approved
carlos@0
  1129
          v_ResultStr:='TestApproval DocType_ID=' || v_DocType_ID;
carlos@0
  1130
          SELECT IsApproved
carlos@0
  1131
          INTO v_IsApproved
carlos@0
  1132
          FROM C_DOCTYPE
carlos@0
  1133
          WHERE C_DocType_ID=v_DocType_ID;
carlos@0
  1134
          DBMS_OUTPUT.PUT_LINE('IsApproved=' || v_IsApproved) ;
adrian@170
  1135
          IF(v_IsApproved&lt;&gt;'N') THEN -- Approval needed
adrian@170
  1136
            IF(v_DocAction&lt;&gt;'AP') THEN
carlos@0
  1137
              UPDATE C_INVOICE  SET DocStatus='NA'  WHERE C_Invoice_ID=v_Record_ID;
carlos@0
  1138
              v_Message:='NotApproved';
carlos@0
  1139
              DBMS_OUTPUT.PUT_LINE('Approval needed') ;
carlos@0
  1140
              END_PROCESSING:=TRUE;
carlos@0
  1141
            END IF;
carlos@0
  1142
          ELSE
carlos@0
  1143
            UPDATE C_INVOICE  SET IsApproved='Y'  WHERE C_Invoice_ID=v_Record_ID;
carlos@0
  1144
          END IF;
carlos@0
  1145
        END IF;
carlos@0
  1146
      END IF;--FINISH_PROCESS
carlos@0
  1147
      IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
carlos@0
  1148
        /**************************************************************************
carlos@0
  1149
        * Update BP Statistics
carlos@0
  1150
        *************************************************************************/
carlos@0
  1151
        v_ResultStr:='Updating BPartners';
carlos@0
  1152
        -- First Sale
carlos@0
  1153
        UPDATE C_BPARTNER
carlos@0
  1154
          SET FirstSale=v_DateAcct
carlos@0
  1155
        WHERE C_BPartner_ID=v_BPartner_ID
carlos@0
  1156
          AND FirstSale IS NULL;
carlos@0
  1157
        -- Last Contact, Result
carlos@0
  1158
        UPDATE AD_USER
carlos@0
  1159
          SET LastContact=Now(),
carlos@0
  1160
          LastResult=v_DocumentNo
carlos@0
  1161
        WHERE AD_User_ID=v_BPartner_User_ID;
carlos@0
  1162
        -- Update total revenue and credit limit
carlos@0
  1163
        -- It is reversed in C_Allocation_Trg
carlos@0
  1164
        IF(v_IsSOTrx='Y') THEN
carlos@0
  1165
          UPDATE C_BPARTNER
carlos@0
  1166
            SET ActualLifeTimeValue=ActualLifeTimeValue +(v_Multiplier *  C_Base_Convert(v_GrandTotal, v_Currency_ID, v_Client_ID, v_DateAcct, v_Org_ID))
carlos@0
  1167
          WHERE C_BPartner_ID=v_BPartner_ID;
carlos@0
  1168
        END IF;
carlos@0
  1169
      END IF;--FINISH_PROCESS
carlos@0
  1170
      IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
carlos@0
  1171
        /**************************************************************************
carlos@0
  1172
        * Matching
carlos@0
  1173
        *************************************************************************/
carlos@0
  1174
        v_ResultStr:='Matching';
carlos@0
  1175
        IF(v_IsSOTrx='N') THEN
carlos@0
  1176
          DECLARE
carlos@0
  1177
            -- Invoice-Receipt Match
carlos@0
  1178
            Cur_ILines_Receipt RECORD;
carlos@0
  1179
            -- Invoice-PO Match
carlos@0
  1180
            Cur_ILines_PO RECORD;
carlos@0
  1181
            v_Qty NUMBER;
carlos@0
  1182
            v_MatchInv_ID NUMBER(10) ;
carlos@0
  1183
            v_MatchPO_ID NUMBER(10) ;
carlos@0
  1184
          BEGIN
carlos@0
  1185
            v_ResultStr:='MatchInv-Receipt';
carlos@0
  1186
            FOR Cur_ILines_Receipt IN
carlos@0
  1187
              (SELECT il.AD_Client_ID,
carlos@0
  1188
                il.AD_Org_ID,
carlos@0
  1189
                il.C_InvoiceLine_ID,
carlos@0
  1190
                ml.M_InOutLine_ID,
carlos@0
  1191
                ml.M_Product_ID,
carlos@0
  1192
                ml.MovementQty,
carlos@0
  1193
                il.QtyInvoiced,
carlos@0
  1194
                i.DateAcct
carlos@0
  1195
              FROM C_INVOICELINE il
carlos@0
  1196
              INNER JOIN M_INOUTLINE ml
carlos@0
  1197
                ON(il.M_InOutLine_ID=ml.M_InOutLine_ID)
carlos@0
  1198
              INNER JOIN C_INVOICE i
carlos@0
  1199
                ON(il.C_Invoice_ID=i.C_Invoice_ID)
carlos@0
  1200
              WHERE il.M_Product_ID=ml.M_Product_ID
carlos@0
  1201
                AND il.C_Invoice_ID=v_Record_ID
carlos@0
  1202
              )
carlos@0
  1203
            LOOP
carlos@0
  1204
              -- The min qty. Modified by Ismael Ciordia
carlos@0
  1205
              --v_Qty := Cur_ILines_Receipt.MovementQty;
adrian@170
  1206
              --IF (ABS(Cur_ILines_Receipt.MovementQty) &gt; ABS(Cur_ILines_Receipt.QtyInvoiced)) THEN
carlos@0
  1207
              v_Qty:=Cur_ILines_Receipt.QtyInvoiced;
carlos@0
  1208
              --END IF;
carlos@0
  1209
              Ad_Sequence_Next('M_MatchInv', Cur_ILines_Receipt.AD_Org_ID, v_MatchInv_ID) ;
carlos@0
  1210
              v_ResultStr:='InsertMatchInv ' || v_MatchInv_ID;
carlos@0
  1211
              DBMS_OUTPUT.PUT_LINE('  M_MatchInv_ID=' || v_MatchInv_ID || ' - ' || v_Qty) ;
carlos@0
  1212
              INSERT
carlos@0
  1213
              INTO M_MATCHINV
carlos@0
  1214
                (
carlos@0
  1215
                  M_MatchInv_ID, AD_Client_ID, AD_Org_ID, IsActive,
carlos@0
  1216
                  Created, CreatedBy, Updated, UpdatedBy,
carlos@0
  1217
                  M_InOutLine_ID, C_InvoiceLine_ID, M_Product_ID, DateTrx,
carlos@0
  1218
                  Qty, Processing, Processed, Posted
carlos@0
  1219
                )
carlos@0
  1220
                VALUES
carlos@0
  1221
                (
carlos@0
  1222
                  v_MatchInv_ID, Cur_ILines_Receipt.AD_Client_ID, Cur_ILines_Receipt.AD_Org_ID, 'Y',
carlos@0
  1223
                  Now(), 0, Now(), 0,
carlos@0
  1224
                  Cur_ILines_Receipt.M_InOutLine_ID, Cur_ILines_Receipt.C_InvoiceLine_ID, Cur_ILines_Receipt.M_Product_ID, Cur_ILines_Receipt.DateAcct,
carlos@0
  1225
                  v_Qty, 'N', 'Y', 'N'
carlos@0
  1226
                )
carlos@0
  1227
                ;
carlos@0
  1228
            END LOOP;
carlos@0
  1229
            v_ResultStr:='MatchInv-PO';
carlos@0
  1230
            FOR Cur_ILines_PO IN
carlos@0
  1231
              (SELECT il.AD_Client_ID,
carlos@0
  1232
                il.AD_Org_ID,
carlos@0
  1233
                il.C_InvoiceLine_ID,
carlos@0
  1234
                ol.C_OrderLine_ID,
carlos@0
  1235
                ol.M_Product_ID,
carlos@0
  1236
                ol.C_Charge_ID,
carlos@0
  1237
                ol.QtyOrdered,
carlos@0
  1238
                il.QtyInvoiced,
carlos@0
  1239
                i.DateAcct
carlos@0
  1240
              FROM C_INVOICELINE il
carlos@0
  1241
              INNER JOIN C_ORDERLINE ol
carlos@0
  1242
                ON(il.C_OrderLine_ID=ol.C_OrderLine_ID)
carlos@0
  1243
              INNER JOIN C_INVOICE i
carlos@0
  1244
                ON(il.C_Invoice_ID=i.C_Invoice_ID)
carlos@0
  1245
              WHERE(il.M_Product_ID=ol.M_Product_ID
carlos@0
  1246
                OR il.C_Charge_ID=ol.C_Charge_ID)
carlos@0
  1247
                AND il.C_Invoice_ID=v_Record_ID
carlos@0
  1248
              )
carlos@0
  1249
            LOOP
carlos@0
  1250
              -- The min qty. Modified by Ismael Ciordia
carlos@0
  1251
              --v_Qty := Cur_ILines_PO.QtyOrdered;
adrian@170
  1252
              --IF (ABS(Cur_ILines_PO.QtyOrdered) &gt; ABS(Cur_ILines_PO.QtyInvoiced)) THEN
carlos@0
  1253
              v_Qty:=Cur_ILines_PO.QtyInvoiced;
carlos@0
  1254
              --END IF;
carlos@0
  1255
              Ad_Sequence_Next('M_MatchPO', Cur_ILines_PO.AD_Org_ID, v_MatchPO_ID) ;
carlos@0
  1256
              v_ResultStr:='InsertMatchPO ' || v_MatchPO_ID;
carlos@0
  1257
              DBMS_OUTPUT.PUT_LINE('  M_MatchPO_ID=' || v_MatchPO_ID || ' - ' || v_Qty) ;
carlos@0
  1258
              INSERT
carlos@0
  1259
              INTO M_MATCHPO
carlos@0
  1260
                (
carlos@0
  1261
                  M_MatchPO_ID, AD_Client_ID, AD_Org_ID, IsActive,
carlos@0
  1262
                  Created, CreatedBy, Updated, UpdatedBy,
carlos@0
  1263
                  C_OrderLine_ID, M_InOutLine_ID, C_InvoiceLine_ID, M_Product_ID,
carlos@0
  1264
                  DateTrx, Qty, Processing, Processed,
carlos@0
  1265
                  Posted
carlos@0
  1266
                )
carlos@0
  1267
                VALUES
carlos@0
  1268
                (
carlos@0
  1269
                  v_MatchPO_ID, Cur_ILines_PO.AD_Client_ID, Cur_ILines_PO.AD_Org_ID, 'Y',
carlos@0
  1270
                  Now(), 0, Now(), 0,
carlos@0
  1271
                  Cur_ILines_PO.C_OrderLine_ID, NULL, Cur_ILines_PO.C_InvoiceLine_ID, Cur_ILines_PO.M_Product_ID,
carlos@0
  1272
                  Cur_ILines_PO.DateAcct, v_Qty, 'N', 'Y',
carlos@0
  1273
                   'N'
carlos@0
  1274
                )
carlos@0
  1275
                ;
carlos@0
  1276
            END LOOP;
carlos@0
  1277
          END;
carlos@0
  1278
        ELSE -- Actualiza las cantidades facturadas de los pedidos de venta, y las lineas de albaryn facturadas
carlos@0
  1279
          DECLARE
carlos@0
  1280
            CurLines RECORD;
carlos@0
  1281
            p_DateInvoiced DATE;
carlos@0
  1282
            v_totalQtyInvoiced NUMBER;
carlos@0
  1283
            v_deliveredQty NUMBER;
carlos@0
  1284
            v_inOutStatus CHAR(2) ;
carlos@0
  1285
          BEGIN
carlos@0
  1286
            SELECT DateInvoiced
carlos@0
  1287
            INTO p_DateInvoiced
carlos@0
  1288
            FROM C_INVOICE
carlos@0
  1289
            WHERE C_Invoice_ID=v_Record_ID;
carlos@0
  1290
            FOR CurLines IN
carlos@0
  1291
              (SELECT * FROM C_INVOICELINE  WHERE C_INVOICE_ID=v_Record_ID  ORDER BY line)
carlos@0
  1292
            LOOP
carlos@0
  1293
              IF(CurLines.C_OrderLine_ID IS NOT NULL) THEN
carlos@0
  1294
                UPDATE C_ORDERLINE
carlos@0
  1295
                  SET QtyInvoiced=QtyInvoiced + CurLines.QtyInvoiced,
carlos@0
  1296
                  DateInvoiced=p_DateInvoiced,
carlos@0
  1297
                  Updated=Now()
carlos@0
  1298
                WHERE C_OrderLine_ID=CurLines.C_OrderLine_ID;
carlos@0
  1299
              END IF;
carlos@0
  1300
              IF(CurLines.M_InOutLine_ID IS NOT NULL) THEN
carlos@0
  1301
                SELECT m.DOCSTATUS
carlos@0
  1302
                INTO v_inOutStatus
carlos@0
  1303
                FROM M_INOUT m,
carlos@0
  1304
                  M_INOUTLINE ml
carlos@0
  1305
                WHERE M.M_InOut_ID=ml.M_InOut_ID
carlos@0
  1306
                  AND ml.M_InOutLine_ID=CurLines.M_InOutLine_ID;
adrian@170
  1307
                IF(v_inOutStatus&lt;&gt;'RE') THEN
carlos@0
  1308
                  SELECT COALESCE(SUM(C_INVOICELINE.QTYINVOICED), 0)
carlos@0
  1309
                  INTO v_totalQtyInvoiced
carlos@0
  1310
                  FROM C_INVOICELINE,
carlos@0
  1311
                    C_INVOICE
carlos@0
  1312
                  WHERE C_INVOICE.C_Invoice_ID=C_INVOICELINE.C_Invoice_ID
carlos@0
  1313
                    AND C_INVOICE.Processed='Y'
carlos@0
  1314
                    AND C_INVOICELINE.M_InOutLine_ID=CurLines.M_InOutLine_ID;
carlos@0
  1315
                  v_totalQtyInvoiced:=v_totalQtyInvoiced + CurLines.QtyInvoiced;
carlos@0
  1316
                  SELECT MovementQty
carlos@0
  1317
                  INTO v_deliveredQty
carlos@0
  1318
                  FROM M_INOUTLINE
carlos@0
  1319
                  WHERE M_InOutLine_ID=CurLines.M_InOutLine_ID;
carlos@0
  1320
                  UPDATE M_INOUTLINE
carlos@0
  1321
                    SET IsInvoiced=(
carlos@0
  1322
                    CASE v_totalQtyInvoiced
carlos@0
  1323
                      WHEN 0
carlos@0
  1324
                      THEN 'N'
carlos@0
  1325
                      ELSE 'Y'
carlos@0
  1326
                    END
carlos@0
  1327
                    )
carlos@0
  1328
                  WHERE M_InOutLine_ID=CurLines.M_InOutLine_ID;
carlos@0
  1329
                END IF;
carlos@0
  1330
              END IF;
carlos@0
  1331
            END LOOP;
carlos@0
  1332
          END;
carlos@0
  1333
        END IF;
carlos@0
  1334
      END IF;--FINISH_PROCESS
carlos@0
  1335
      IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
carlos@0
  1336
        -- Modified by Ismael Ciordia
carlos@0
  1337
        -- Generate C_Debt_Payment linked to this invoice
carlos@0
  1338
        DECLARE
carlos@0
  1339
          v_totalCash NUMBER:=0;
carlos@0
  1340
          v_processed CHAR(1):='N';
carlos@0
  1341
          v_debtPaymentID NUMBER(10) ;
carlos@0
  1342
          v_amount NUMBER;
carlos@0
  1343
          v_cashBook NUMBER(10) ;
carlos@0
  1344
          v_bankAccount NUMBER(10) ;
carlos@0
  1345
          v_cash NUMBER(10) ;
carlos@0
  1346
          v_IsoCode C_CURRENCY.ISO_CODE%TYPE;
carlos@0
  1347
          v_CashLine NUMBER(10) ;
carlos@0
  1348
          v_line NUMBER(10) ;
carlos@0
  1349
          v_BPartnerName C_BPARTNER.NAME%TYPE;
carlos@0
  1350
          v_GenDP_Org NUMBER(10);
carlos@0
  1351
        BEGIN
carlos@0
  1352
          v_ResultStr:='Generating C_Debt_Payment';
carlos@0
  1353
          UPDATE C_DEBT_PAYMENT
carlos@0
  1354
            SET C_INVOICE_ID=v_Record_ID
carlos@0
  1355
          WHERE EXISTS
carlos@0
  1356
            (SELECT 1
carlos@0
  1357
            FROM C_ORDERLINE ol,
carlos@0
  1358
              C_INVOICELINE il
carlos@0
  1359
            WHERE ol.C_ORDERLINE_ID=il.C_ORDERLINE_ID
carlos@0
  1360
              AND il.C_INVOICE_ID=v_Record_ID
carlos@0
  1361
              AND ol.C_ORDER_ID=C_DEBT_PAYMENT.C_ORDER_ID
carlos@0
  1362
            )
carlos@0
  1363
            AND C_INVOICE_ID IS NULL;
carlos@0
  1364
          UPDATE C_DEBT_PAYMENT
carlos@0
  1365
            SET IsValid='Y'
carlos@0
  1366
          WHERE C_INVOICE_ID=v_Record_ID
carlos@0
  1367
            AND IsValid!='Y';
carlos@0
  1368
            
adrian@170
  1369
            -- Is it a Credit Memo:4
carlos@0
  1370
          SELECT DocBaseType
carlos@0
  1371
          INTO v_TargetDocBaseType
carlos@0
  1372
          FROM C_DOCTYPE
carlos@0
  1373
          WHERE C_DocType_ID=v_DocTypeTarget_ID;
carlos@0
  1374
          IF v_TargetDocBaseType in ('ARC','APC') THEN
carlos@0
  1375
            v_MultiplierARC:=-1;
carlos@0
  1376
          END IF;  
carlos@0
  1377
            
carlos@0
  1378
          --Sums debt payments from the order and the ones that have been inserted manually, added by ALO
carlos@0
  1379
          SELECT COALESCE(SUM(C_Currency_Round(C_Currency_Convert((Amount + WriteOffAmt)*v_MultiplierARC, C_Currency_ID, v_Currency_ID, v_DateInvoiced, NULL, v_Client_ID, v_Org_ID), v_Currency_ID, NULL)), 0)
carlos@0
  1380
          INTO v_totalCash
carlos@0
  1381
          FROM C_DEBT_PAYMENT_V dp
carlos@0
  1382
          WHERE C_INVOICE_ID=v_Record_ID;
adrian@170
  1383
          --Insert C_Debt_Payment if GrandTotal - v_totalCash &lt;&gt; 0;
adrian@170
  1384
          IF(v_GrandTotal&lt;&gt;v_totalCash) THEN
carlos@0
  1385
            DECLARE
carlos@0
  1386
              CUR_PAYMENTS RECORD;
carlos@0
  1387
              v_plannedDate DATE;
carlos@0
  1388
              v_pendingAmount NUMBER;
carlos@0
  1389
              v_paymentAmount NUMBER;
carlos@0
  1390
              v_GenDebt_PaymentID NUMBER;
carlos@0
  1391
              v_SettlementDocTypeID NUMBER(10) ;
carlos@0
  1392
              v_settlement_ID NUMBER(10) ;
carlos@0
  1393
       v_CB_Curr NUMBER;
carlos@0
  1394
              v_SDocument_No C_SETTLEMENT.DocumentNo%TYPE;
carlos@0
  1395
            BEGIN
carlos@0
  1396
              IF v_IsSOTrx ='Y' THEN
carlos@0
  1397
                v_pendingAmount:=v_GrandTotal - v_totalCash;
carlos@0
  1398
              ELSE
carlos@0
  1399
                v_pendingAmount:=v_GrandTotal + v_totalCash;
carlos@0
  1400
              END IF;
carlos@0
  1401
              FOR CUR_PAYMENTS IN
carlos@0
  1402
                (SELECT LINE,
carlos@0
  1403
                  PERCENTAGE,
carlos@0
  1404
                  ONREMAINDER,
carlos@0
  1405
                  EXCLUDETAX,
carlos@0
  1406
                  COALESCE(PAYMENTRULE, v_PaymentRule) AS PAYMENTRULE,
carlos@0
  1407
                  FIXMONTHDAY,
carlos@0
  1408
                  FIXMONTHDAY2,
carlos@0
  1409
                  FIXMONTHDAY3,
carlos@0
  1410
                  NETDAYS,
carlos@0
  1411
                  FIXMONTHOFFSET,
carlos@0
  1412
                  NETDAY,
carlos@0
  1413
                  ISNEXTBUSINESSDAY
carlos@0
  1414
                FROM C_PAYMENTTERMLINE
carlos@0
  1415
                WHERE C_PAYMENTTERM_ID=v_PaymentTerm
carlos@0
  1416
                UNION
carlos@0
  1417
                  -- Header of paymentTerm is processed at last
carlos@0
  1418
                SELECT 9999 AS LINE,
carlos@0
  1419
                  100 AS PERCENTAGE,
carlos@0
  1420
                   'Y' AS ONREMAINDER,
carlos@0
  1421
                   'N' AS EXCLUDETAX,
carlos@0
  1422
                  v_PaymentRule AS PAYMENTRULE,
carlos@0
  1423
                  FIXMONTHDAY,
carlos@0
  1424
                  FIXMONTHDAY2,
carlos@0
  1425
                  FIXMONTHDAY3,
carlos@0
  1426
                  NETDAYS,
carlos@0
  1427
                  FIXMONTHOFFSET,
carlos@0
  1428
                  NETDAY,
carlos@0
  1429
                  ISNEXTBUSINESSDAY
carlos@0
  1430
                FROM C_PAYMENTTERM
carlos@0
  1431
                WHERE C_PAYMENTTERM_ID=v_PaymentTerm
carlos@0
  1432
                ORDER BY LINE
carlos@0
  1433
                )
carlos@0
  1434
              LOOP
carlos@0
  1435
                IF(CUR_PAYMENTS.PaymentRule IN('B', 'C')) THEN
carlos@0
  1436
                  SELECT MAX(C_CashBook_ID)
carlos@0
  1437
                  INTO v_cashBook
carlos@0
  1438
                  FROM C_CASHBOOK
carlos@0
  1439
                  WHERE AD_Client_ID=v_Client_ID
carlos@0
  1440
                    AND isDefault='Y';
carlos@0
  1441
                  IF v_cashBook IS NULL THEN
carlos@0
  1442
                      RAISE_APPLICATION_ERROR(-20000, '@NoDefaultCashBook@');
carlos@0
  1443
                  END IF;
carlos@0
  1444
                  v_bankAccount:=NULL;
carlos@0
  1445
                ELSE
carlos@0
  1446
                  SELECT COALESCE((
carlos@0
  1447
                    CASE v_IsSOTrx
carlos@0
  1448
                      WHEN 'Y'
carlos@0
  1449
                      THEN SO_BankAccount_ID
carlos@0
  1450
                      ELSE PO_BankAccount_ID
carlos@0
  1451
                    END
carlos@0
  1452
                    ),
carlos@0
  1453
                    (SELECT MAX(C_BankAccount_ID)
carlos@0
  1454
                    FROM C_BANKACCOUNT
carlos@0
  1455
                    WHERE AD_Client_ID=v_Client_ID
carlos@0
  1456
                      AND isDefault='Y'
carlos@0
  1457
                    )
carlos@0
  1458
                    )
carlos@0
  1459
                  INTO v_bankAccount
carlos@0
  1460
                  FROM C_BPARTNER
carlos@0
  1461
                  WHERE c_BPartner_ID=v_BPartner_ID;
carlos@0
  1462
                  v_cashBook:=NULL;
carlos@0
  1463
                END IF;
carlos@0
  1464
                v_plannedDate:=C_Paymentduedate(v_BPartner_ID, v_IsSOTrx, CUR_PAYMENTS.FixMonthDay, CUR_PAYMENTS.FixMonthDay2, CUR_PAYMENTS.FixMonthDay3, CUR_PAYMENTS.NetDays, CUR_PAYMENTS.FixMonthOffset, CUR_PAYMENTS.NetDay, CUR_PAYMENTS.IsNextbusinessday, v_DateInvoiced) ;
carlos@0
  1465
                IF(CUR_PAYMENTS.EXCLUDETAX='Y') THEN
carlos@0
  1466
                  -- if excludeTax = 'Y', percentage is aplied on the TotalLines
carlos@0
  1467
                  v_paymentAmount:=C_Currency_Round((v_TotalLines) *CUR_PAYMENTS.PERCENTAGE/100, v_Currency_ID, NULL) ;
carlos@0
  1468
                ELSIF(CUR_PAYMENTS.ONREMAINDER='N') THEN
carlos@0
  1469
                  -- if onRemainder = 'N', percentage is aplied on the GrandTotal
carlos@0
  1470
                  v_paymentAmount:=C_Currency_Round((v_GrandTotal) *CUR_PAYMENTS.PERCENTAGE/100, v_Currency_ID, NULL) ;
carlos@0
  1471
                ELSE
carlos@0
  1472
                  v_paymentAmount:=C_Currency_Round((v_pendingAmount) *CUR_PAYMENTS.PERCENTAGE/100, v_Currency_ID, NULL) ;
carlos@0
  1473
                END IF;
carlos@0
  1474
                v_pendingAmount:=v_pendingAmount - v_paymentAmount;
carlos@0
  1475
                Ad_Sequence_Next('C_Debt_Payment', v_Record_ID, v_debtPaymentID) ;
carlos@0
  1476
                INSERT
carlos@0
  1477
                INTO C_DEBT_PAYMENT
carlos@0
  1478
                  (
carlos@0
  1479
                    C_DEBT_PAYMENT_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
  1480
                    CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
  1481
                    ISRECEIPT, C_SETTLEMENT_CANCEL_ID, C_SETTLEMENT_GENERATE_ID, DESCRIPTION,
carlos@0
  1482
                    C_INVOICE_ID, C_BPARTNER_ID, C_CURRENCY_ID, C_CASHLINE_ID,
carlos@0
  1483
                    C_BANKACCOUNT_ID, C_CASHBOOK_ID, PAYMENTRULE, ISPAID,
carlos@0
  1484
                    AMOUNT, WRITEOFFAMT, DATEPLANNED, ISMANUAL,
carlos@0
  1485
                    ISVALID, C_BANKSTATEMENTLINE_ID, CHANGESETTLEMENTCANCEL, CANCEL_PROCESSED,
carlos@0
  1486
                    GENERATE_PROCESSED, c_project_id, status, status_initial
carlos@0
  1487
                  )
carlos@0
  1488
                  VALUES
carlos@0
  1489
                  (
carlos@0
  1490
                    v_debtPaymentID, v_Client_ID, v_Org_ID, 'Y',
carlos@0
  1491
                    Now(), v_UpdatedBy, Now(), v_UpdatedBy,
carlos@0
  1492
                    v_IsSOTrx, NULL, NULL, '* ' || v_DocumentNo || ' * (' || v_BPartnerName ||( CASE WHEN v_POReference IS NULL THEN '' ELSE ' .Ref:'||TO_CHAR(v_POReference) END) || ' )',
carlos@0
  1493
                    v_Record_ID, v_BPartner_ID, v_Currency_ID, NULL,
carlos@0
  1494
                    v_bankAccount, v_cashBook, CUR_PAYMENTS.PaymentRule, 'N',
carlos@0
  1495
                    v_paymentAmount*v_MultiplierARC, 0, v_plannedDate, 'N',
carlos@0
  1496
                    'Y', NULL, 'N', 'N',
carlos@0
  1497
                    'N', v_C_Project_Id, 'DE', 'DE'
carlos@0
  1498
                  )
carlos@0
  1499
                  ;
carlos@0
  1500
                --AL
carlos@0
  1501
                --Looking for autogenerated debt-payments
carlos@0
  1502
                SELECT MAX(c_Debt_Payment_Id), MAX(ad_Org_ID)
carlos@0
  1503
                INTO v_GenDebt_PaymentID, v_GenDP_Org
carlos@0
  1504
                FROM C_DEBT_PAYMENT DP
carlos@0
  1505
                WHERE C_BPartner_ID=v_BPartner_ID
carlos@0
  1506
                  AND C_Debt_Payment_Status(C_Settlement_Cancel_ID, Cancel_Processed, Generate_Processed, IsPaid, IsValid, C_Cashline_ID, C_BankstatementLine_ID)='P'
carlos@0
  1507
                  AND(-1) *Amount=v_paymentAmount
carlos@0
  1508
                  AND c_currency_ID=v_Currency_ID
carlos@0
  1509
                  AND C_SETTLEMENT_GENERATE_ID IS NOT NULL
carlos@0
  1510
                  AND Ad_Isorgincluded(v_ad_Org_id, dp.ad_org_id,v_Client_ID) != -1
carlos@0
  1511
                  AND ad_client_id = v_Client_ID
carlos@0
  1512
                  AND EXISTS (SELECT 1
carlos@0
  1513
                                FROM C_SETTLEMENT S
carlos@0
  1514
                               WHERE DP.C_SETTLEMENT_GENERATE_ID = S.C_Settlement_ID
carlos@0
  1515
                                 AND IsGenerated = 'Y')
carlos@0
  1516
                  AND NOT EXISTS (SELECT 1
carlos@0
  1517
                                   FROM C_REMITTANCELINE rl
carlos@0
  1518
                                  WHERE rl.C_DEBT_PAYMENT_CANCELLED = DP.C_DEBT_PAYMENT_ID);
carlos@0
  1519
carlos@0
  1520
                IF v_GenDebt_PaymentID IS NOT NULL THEN
carlos@0
  1521
                  v_SettlementDocTypeID:=Ad_Get_Doctype(v_Client_ID, v_GenDP_Org, TO_CHAR('STT')) ;
carlos@0
  1522
                  Ad_Sequence_Next('C_Settlement', v_Record_ID, v_settlement_ID) ;
carlos@0
  1523
                  Ad_Sequence_Doctype(v_SettlementDocTypeID, v_Record_ID, 'Y', v_SDocument_No) ;
carlos@0
  1524
                  IF(v_SDocument_No IS NULL) THEN
carlos@0
  1525
                    Ad_Sequence_Doc('DocumentNo_C_Settlement', v_Client_ID, 'Y', v_SDocument_No) ;
carlos@0
  1526
                  END IF;
carlos@0
  1527
                  INSERT
carlos@0
  1528
                  INTO C_SETTLEMENT
carlos@0
  1529
                    (
carlos@0
  1530
                      C_SETTLEMENT_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
  1531
                      CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
  1532
                      DOCUMENTNO, DATETRX, DATEACCT, SETTLEMENTTYPE,
carlos@0
  1533
                      C_DOCTYPE_ID, PROCESSING, PROCESSED, POSTED,
carlos@0
  1534
                      C_CURRENCY_ID, ISGENERATED
carlos@0
  1535
                    )
carlos@0
  1536
                    /*, C_PROJECT_ID, C_CAMPAIGN_ID,
carlos@0
  1537
                    C_ACTIVITY_ID, USER1_ID, USER2_ID, CREATEFROM)*/
carlos@0
  1538
                    VALUES
carlos@0
  1539
                    (
carlos@0
  1540
                      v_Settlement_ID, v_Client_ID, v_GenDP_Org, 'Y',
carlos@0
  1541
                      Now(), v_UpdatedBy, Now(), v_UpdatedBy,
carlos@0
  1542
                      '*C*'||v_SDocument_No, TRUNC(Now()), TRUNC(Now()), 'C',
carlos@0
  1543
                      v_SettlementDocTypeID, 'N', 'N', 'N',
carlos@0
  1544
                      v_Currency_ID, 'Y'
carlos@0
  1545
                    )
carlos@0
  1546
                    ;
carlos@0
  1547
                  UPDATE C_DEBT_PAYMENT
carlos@0
  1548
                    SET C_Settlement_Cancel_Id=v_Settlement_ID,
carlos@0
  1549
                    Updated=Now(),
carlos@0
  1550
                    UpdatedBy=v_UpdatedBy
carlos@0
  1551
                  WHERE c_Debt_Payment_ID IN(v_genDebt_PaymentID, v_debtPaymentID) ;
carlos@0
  1552
                  C_Settlement_Post(NULL, v_Settlement_ID) ;
carlos@0
  1553
                END IF;
carlos@0
  1554
                --If Invoice.paymentRule = 'B', insert de cashline de tipo efecto apuntando al efecto
carlos@0
  1555
                IF(v_cashBook IS NOT NULL AND CUR_PAYMENTS.PaymentRule='B') THEN
carlos@0
  1556
                  SELECT MAX(C.C_CASH_ID)
carlos@0
  1557
                  INTO v_Cash
carlos@0
  1558
                  FROM C_CASH C
carlos@0
  1559
                  WHERE C.C_CASHBOOK_ID=v_cashBook
carlos@0
  1560
                    AND C.DATEACCT=v_DateAcct
carlos@0
  1561
                    AND C.PROCESSED='N';
carlos@0
  1562
carlos@0
  1563
         SELECT C_CURRENCY_ID
carlos@0
  1564
           INTO v_CB_Curr
carlos@0
  1565
           FROM C_CASHBOOK
carlos@0
  1566
         WHERE C_CASHBOOK_ID = v_cashBook;
carlos@0
  1567
carlos@0
  1568
                  IF(v_Cash IS NULL) THEN
carlos@0
  1569
                    v_ResultStr:='Creating C_Cash';
carlos@0
  1570
                    SELECT ISO_CODE
carlos@0
  1571
                    INTO v_IsoCode
carlos@0
  1572
                    FROM C_CURRENCY
carlos@0
  1573
                    WHERE C_Currency_ID=v_CB_Curr;
carlos@0
  1574
                    Ad_Sequence_Next('C_Cash', v_Record_ID, v_Cash) ;
carlos@0
  1575
                    INSERT
carlos@0
  1576
                    INTO C_CASH (
carlos@0
  1577
                        C_Cash_ID, AD_Client_ID, AD_Org_ID, IsActive,
carlos@0
  1578
                        Created, CreatedBy, Updated, UpdatedBy,
carlos@0
  1579
                        C_CashBook_ID, NAME, StatementDate, DateAcct,
carlos@0
  1580
                        BeginningBalance, EndingBalance, StatementDifference, Processing,
carlos@0
  1581
                        Processed, Posted )
carlos@0
  1582
                      VALUES (v_Cash, v_Client_ID, v_Org_ID, 'Y',
carlos@0
  1583
                        Now(), v_UpdatedBy, Now(), v_UpdatedBy,
carlos@0
  1584
                        v_cashBook, (TO_CHAR(v_DateAcct, 'YYYY-MM-DD') || ' ' || v_IsoCode), v_DateAcct, v_DateAcct,
carlos@0
  1585
                        0, 0, 0, 'N',
carlos@0
  1586
                         'N', 'N');
carlos@0
  1587
                  END IF; -- v_Cash IS NULL
carlos@0
  1588
                  v_ResultStr:='Creating C_CashLine';
carlos@0
  1589
                  Ad_Sequence_Next('C_CashLine', v_Record_ID, v_CashLine) ;
carlos@0
  1590
                  SELECT COALESCE(MAX(LINE), 0) + 10
carlos@0
  1591
                  INTO v_line
carlos@0
  1592
                  FROM C_CASHLINE
carlos@0
  1593
                  WHERE C_CASH_ID=v_Cash;
carlos@0
  1594
carlos@0
  1595
carlos@0
  1596
carlos@0
  1597
                  INSERT
carlos@0
  1598
                  INTO C_CASHLINE
carlos@0
  1599
                    (
carlos@0
  1600
                      C_CashLine_ID, AD_Client_ID, AD_Org_ID, IsActive,
carlos@0
  1601
                      Created, CreatedBy, Updated, UpdatedBy,
carlos@0
  1602
                      C_Cash_ID, C_Debt_Payment_ID, Line, Description,
carlos@0
  1603
                      Amount, CashType, C_Currency_ID, DiscountAmt,
carlos@0
  1604
                      WriteOffAmt, IsGenerated
carlos@0
  1605
                    )
carlos@0
  1606
                    VALUES
carlos@0
  1607
                    (
carlos@0
  1608
                      v_CashLine, v_Client_ID, v_Org_ID, 'Y',
carlos@0
  1609
                      Now(), v_UpdatedBy, Now(), v_UpdatedBy,
carlos@0
  1610
                      v_Cash, v_debtPaymentID, v_line, v_BPartnerName,
carlos@0
  1611
                      C_Currency_Convert((CASE v_IsSOTrx WHEN 'Y' THEN v_paymentAmount ELSE -v_paymentAmount END), v_Currency_ID, v_CB_Curr,v_DateAcct,NULL,v_Client_ID, v_Org_ID), 'P', v_Currency_ID, 0,
carlos@0
  1612
                      0, 'Y'
carlos@0
  1613
                    )
carlos@0
  1614
                    ;
carlos@0
  1615
                END IF; -- v_cashBook IS NOT NULL
carlos@0
  1616
              END LOOP;
carlos@0
  1617
            END;
adrian@170
  1618
          END IF; -- v_GrandTotal &lt;&gt; v_totalCash
carlos@0
  1619
        END;
carlos@0
  1620
        IF(NOT FINISH_PROCESS) THEN
carlos@0
  1621
          C_Bp_Socreditused_Refresh(v_BPartner_ID) ;
carlos@0
  1622
        END IF;
carlos@0
  1623
      END IF;--FINISH_PROCESS
carlos@0
  1624
      IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
carlos@0
  1625
        -- Finish up -------------------------------------------------------------
carlos@0
  1626
        UPDATE C_INVOICE
carlos@0
  1627
          SET DocStatus='CO',
carlos@0
  1628
          Processed='Y',
carlos@0
  1629
          DocAction='RE',
carlos@0
  1630
          Updated=Now()
carlos@0
  1631
        WHERE C_Invoice_ID=v_Record_ID;
carlos@0
  1632
        -- Post it
carlos@0
  1633
        IF(v_IsTransferred='N') THEN
carlos@0
  1634
          C_Invoice_Acctgen(NULL, v_Record_ID) ;
carlos@0
  1635
        END IF;
carlos@0
  1636
      END IF;--FINISH_PROCESS
carlos@0
  1637
      IF(NOT FINISH_PROCESS) THEN
carlos@0
  1638
        -- End Processing --------------------------------------------------------
adrian@170
  1639
        --&lt;&lt;END_PROCESSING&gt;&gt;
carlos@0
  1640
        v_ResultStr:='UnLockingInvoice';
carlos@0
  1641
        UPDATE C_INVOICE
carlos@0
  1642
          SET Processing='N',
carlos@0
  1643
          Updated=Now(),
carlos@0
  1644
          UpdatedBy=v_UpdatedBy
carlos@0
  1645
        WHERE C_Invoice_ID=v_Record_ID;
carlos@0
  1646
        -- Commented by cromero 19102006 IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
  1647
        -- Commented by cromero 19102006   COMMIT;
carlos@0
  1648
        -- Commented by cromero 19102006 END IF;
carlos@0
  1649
      END IF;--FINISH_PROCESS
adrian@170
  1650
    --&lt;&lt;FINISH_PROCESS&gt;&gt;
carlos@0
  1651
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
  1652
      --  Update AD_PInstance
carlos@0
  1653
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished - ' || v_Message) ;
carlos@0
  1654
      Ad_Update_Pinstance(p_PInstance_ID, v_UpdatedBy, 'N', v_Result, v_Message) ;
carlos@0
  1655
    ELSE
adrian@170
  1656
      DBMS_OUTPUT.PUT_LINE('--&lt;&lt;C_Invoive_Post finished&gt;&gt; ' || v_Message) ;
carlos@0
  1657
    END IF;
carlos@0
  1658
    RETURN;
carlos@0
  1659
  END; --BODY
carlos@0
  1660
EXCEPTION
carlos@0
  1661
WHEN OTHERS THEN
carlos@0
  1662
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
  1663
     v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
  1664
      DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
  1665
      IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
  1666
        ROLLBACK;
carlos@0
  1667
        Ad_Update_Pinstance(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
  1668
      ELSE
carlos@0
  1669
        DECLARE
carlos@0
  1670
          v_Code NUMBER:=SQLCODE;
carlos@0
  1671
        BEGIN
carlos@0
  1672
          INSERT
carlos@0
  1673
          INTO DBA_ERRORLOG
carlos@0
  1674
            (
carlos@0
  1675
              DBA_ErrorLog_ID, Created,
carlos@0
  1676
              Code, Msg,
carlos@0
  1677
              Info
carlos@0
  1678
            )
carlos@0
  1679
            VALUES
carlos@0
  1680
            (
carlos@0
  1681
              DBA_ErrorLog_Seq.NEXTVAL, Now(),
carlos@0
  1682
              v_Code, v_ResultStr,
carlos@0
  1683
               'C_Invoice_Post'
carlos@0
  1684
            )
carlos@0
  1685
            ;
carlos@0
  1686
    END;
carlos@0
  1687
    RAISE;
carlos@0
  1688
  END IF;
carlos@0
  1689
  -- Commented by cromero 19102006 RETURN;
adrian@170
  1690
END C_Invoice_Post
adrian@170
  1691
</body>
carlos@0
  1692
    </function>
adrian@94
  1693
  </database>