src-db/database/model/functions/C_INVOICE_CREATE.xml
author RM packaging bot <staff.rm@openbravo.com>
Fri, 18 Jul 2014 09:47:52 +0000
changeset 23970 77b6251af841
parent 22873 4bce276a5b06
parent 23965 b94c7ad03a04
child 23983 d7c19111c4f8
permissions -rw-r--r--
Merge temporary head for 3.0PR14Q2.4
adrian@94
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION C_INVOICE_CREATE">
adrian@94
     3
    <function name="C_INVOICE_CREATE" type="NULL">
juanpablo@1605
     4
      <parameter name="p_pinstance_id" type="VARCHAR" mode="in">
antonio@735
     5
        <default/>
antonio@735
     6
      </parameter>
juanpablo@1605
     7
      <parameter name="p_invoice_id" type="VARCHAR" mode="out">
antonio@735
     8
        <default/>
antonio@735
     9
      </parameter>
juanpablo@1605
    10
      <parameter name="p_order_id" type="VARCHAR" mode="in">
antonio@735
    11
        <default><![CDATA[NULL]]></default>
antonio@735
    12
      </parameter>
gorkaion@239
    13
      <body><![CDATA[/*************************************************************************
juanpablo@771
    14
  * The contents of this file are subject to the Compiere Public
juanpablo@771
    15
  * License 1.1 ("License"); You may not use this file except in
juanpablo@771
    16
  * compliance with the License. You may obtain a copy of the License in
juanpablo@771
    17
  * the legal folder of your Openbravo installation.
adrian@94
    18
  * Software distributed under the License is distributed on an
adrian@94
    19
  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
adrian@94
    20
  * implied. See the License for the specific language governing rights
adrian@94
    21
  * and limitations under the License.
juanpablo@778
    22
  * The Original Code is  Compiere  ERP &  Business Solution
juanpablo@771
    23
  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
adrian@94
    24
  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
adrian@94
    25
  * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
adrian@94
    26
  * All Rights Reserved.
ggi@6701
    27
  * Contributor(s): Openbravo SLU
pandeeswari@22732
    28
  * Contributions are Copyright (C) 2001-2014 Openbravo, S.L.U.
juanpablo@771
    29
  *
juanpablo@771
    30
  * Specifically, this derivative work is based upon the following Compiere
juanpablo@771
    31
  * file and version.
adrian@94
    32
  *************************************************************************
adrian@94
    33
  * $Id: C_Invoice_Create.sql,v 1.12 2003/08/31 06:49:27 jjanke Exp $
adrian@94
    34
  ***
adrian@94
    35
  * Title: Create Invoice
adrian@94
    36
  * Description:
adrian@94
    37
  * - Based on Invoice Rules create Invoice
adrian@94
    38
  * - Update Order while creating the lines
adrian@94
    39
  ************************************************************************/
adrian@94
    40
  -- Logistice
adrian@94
    41
  v_ResultStr VARCHAR2(2000):='';
adrian@94
    42
  v_Message VARCHAR2(2000):='';
david@19193
    43
  v_MessageBPB VARCHAR2(2000):='';
adrian@94
    44
  -- Parameter
adrian@94
    45
  TYPE RECORD IS REF CURSOR;
adrian@94
    46
    Cur_Parameter RECORD;
juanpablo@1605
    47
    v_Record_ID VARCHAR2(32):=NULL;
juanpablo@1605
    48
    v_AD_Org_ID VARCHAR2(32):=NULL;
juanpablo@1605
    49
    v_C_Order_ID VARCHAR2(32):=NULL;
juanpablo@1605
    50
    v_BPartner_ID VARCHAR2(32):=NULL;
adrian@94
    51
    v_Selection VARCHAR2(1):='N';
adrian@94
    52
    v_DateInvoiced_aux DATE:=NULL;
adrian@94
    53
    v_DateOrdered DATE:=NULL;
adrian@94
    54
    v_InvoiceToDate DATE:=NULL;
phillip@2231
    55
    v_ADUserId VARCHAR2(32):=NULL;
adrian@94
    56
    --
adrian@94
    57
    v_DateInvoiced DATE;
adrian@94
    58
    v_NoRecords NUMBER:=0;
harikrishnan@7576
    59
    v_Message_Check NUMBER:=0;
david@3064
    60
    v_NoRecNotComplDelivered NUMBER:=0;
adrian@94
    61
    -- Orders to process
adrian@94
    62
    Cur_Order RECORD;
adrian@94
    63
    --
juanpablo@1605
    64
    v_NextNo VARCHAR2(32);
juanpablo@1605
    65
    v_DocType_ID VARCHAR2(32);
adrian@94
    66
    v_DocumentNo VARCHAR2(40) ;
adrian@94
    67
    v_Qty NUMBER;
adrian@94
    68
    --MODIFIED BY F.IRIAZABAL
adrian@94
    69
    v_QtyOrder NUMBER;
olga@3420
    70
    v_Reference VARCHAR2(2000):='';
adrian@94
    71
    v_ReferenceOrder CHAR(1) ;
asier@2586
    72
    v_DocSubTypeSO VARCHAR2(60) ;
adrian@94
    73
    --
adrian@94
    74
    v_LineNo NUMBER:=0;
adrian@94
    75
    v_count NUMBER:=0;
sandra@22266
    76
    v_isSchedule NUMBER:=0;
eduardo@18445
    77
    
eduardo@18445
    78
    v_isdeferredrevenue M_Product.IsDeferredRevenue%TYPE;
eduardo@18445
    79
    v_revplantype M_Product.RevPlanType%TYPE;
eduardo@18445
    80
    v_periodnumber M_Product.periodnumber%TYPE;
eduardo@18445
    81
    v_defaultperiod M_Product.defaultperiod%TYPE;
eduardo@18445
    82
 
eduardo@18445
    83
    v_isdeferredexpense M_Product.IsDeferredExpense%TYPE;
eduardo@18445
    84
    v_expplantype M_Product.ExpPlanType%TYPE;
eduardo@18445
    85
    v_periodnumber_exp M_Product.periodnumber_exp%TYPE;
eduardo@18445
    86
    v_defaultperiod_exp M_Product.defaultperiod_exp%TYPE;
eduardo@18445
    87
eduardo@18445
    88
    v_isdeferred_inv C_InvoiceLine.IsDeferred%TYPE;
eduardo@18445
    89
    v_defplantype_inv C_InvoiceLine.DefPlanType%TYPE;
eduardo@18445
    90
    v_periodnumber_inv C_InvoiceLine.periodnumber%TYPE;
eduardo@18445
    91
    v_period_inv C_InvoiceLine.c_period_id%TYPE;
david@19193
    92
david@19193
    93
    Cur_Bpblocked RECORD;
david@19193
    94
    
adrian@94
    95
    Cur_OrderLine_ISOPEN BOOLEAN:=false;
adrian@94
    96
    Cur_InOutLine_ISOPEN BOOLEAN:=false;
juanpablo@1605
    97
    v_AD_Client_ID VARCHAR2(32);
adrian@94
    98
    -- Order Lines not invoiced   Invoice Rule: Immediate
juanpablo@1605
    99
    CURSOR Cur_OrderLine (Order_ID VARCHAR) IS
adrian@94
   100
      SELECT *
adrian@94
   101
      FROM C_ORDERLINE l
gorkaion@239
   102
      WHERE QtyOrdered<>QtyInvoiced
adrian@94
   103
        AND C_Order_ID=Order_ID
adrian@94
   104
      ORDER BY Line  FOR UPDATE;
adrian@94
   105
      ptr_ol Cur_OrderLine%ROWTYPE;
adrian@94
   106
      -- Shipment Lines of Order Lines  Invoice Rule: Delivery
juanpablo@1605
   107
      CURSOR Cur_InOutLine (Order_ID VARCHAR) IS
adrian@94
   108
        SELECT ol.AD_Client_ID,
adrian@94
   109
          ol.AD_Org_ID,
adrian@94
   110
          sl.M_InOut_ID,
adrian@94
   111
          sl.M_InOutLine_ID,
adrian@94
   112
          ol.C_OrderLine_ID,
adrian@94
   113
          COALESCE(sl.Description, ol.Description) AS Description,
adrian@94
   114
          COALESCE(sl.M_Product_ID, ol.M_Product_ID) AS M_Product_ID,
adrian@94
   115
          COALESCE(sl.MovementQty, ol.QtyOrdered-ol.QtyInvoiced) AS MovementQty,
adrian@94
   116
          --MODIFIED BY F.IRIAZABAL
adrian@94
   117
          sl.QUANTITYORDER,
adrian@94
   118
          ol.PriceList,
adrian@94
   119
          ol.PriceActual,
adrian@94
   120
          ol.PriceLimit,
adrian@94
   121
          ol.C_Charge_ID,
adrian@94
   122
          ol.ChargeAmt,
adrian@94
   123
          COALESCE(sl.C_UOM_ID, ol.C_UOM_ID) AS C_UOM_ID,
adrian@94
   124
          --MODIFIED BY F.IRIAZABAL
adrian@94
   125
          sl.M_PRODUCT_UOM_ID,
adrian@94
   126
          ol.C_Tax_ID, --ol.IsTaxIncluded,
adrian@94
   127
          ol.Line,
adrian@94
   128
          ol.DirectShip,
ainhoa@3730
   129
          ol.PriceStd,
pandeeswari@22807
   130
          sl.m_attributesetinstance_id,
gorkaion@17356
   131
          ol.taxbaseamt,
david@18202
   132
          ol.gross_unit_price,
david@18202
   133
          ol.grosspricestd,
naiara@19828
   134
          ol.grosspricelist, 
sandra@20457
   135
          sl.explode,
sandra@20457
   136
          ol.C_Project_ID, 
sandra@20457
   137
          ol.C_Bpartner_ID, 
sandra@20457
   138
          ol.A_Asset_ID,
sandra@20457
   139
          ol.C_Costcenter_ID,
sandra@20457
   140
          ol.User1_ID, 
sandra@20457
   141
          ol.User2_ID
gorkaion@3677
   142
        FROM M_INOUT s, M_INOUTLINE sl
adrian@94
   143
        RIGHT JOIN C_ORDERLINE ol
adrian@94
   144
          ON sl.C_OrderLine_ID=ol.C_OrderLine_ID
adrian@94
   145
        WHERE sl.M_InOut_ID IS NOT NULL -- We need to have a shipment
adrian@94
   146
          AND ol.C_Order_ID=Order_ID -- parameter
gorkaion@3677
   147
          AND s.M_InOut_ID = sl.M_InOut_ID
gorkaion@3677
   148
          AND s.DocStatus = 'CO'
adrian@94
   149
          AND(sl.IsInvoiced IS NULL
adrian@94
   150
          OR sl.IsInvoiced='N')
adrian@94
   151
        ORDER BY ol.Line -- single Order
adrian@94
   152
          FOR UPDATE;
adrian@94
   153
        ptr_sl Cur_InOutLine%ROWTYPE;
adrian@94
   154
        Next_Order BOOLEAN:=FALSE;
adrian@94
   155
        Next_S_Line BOOLEAN:=FALSE;
adrian@94
   156
        Next_O_Line BOOLEAN:=FALSE;
adrian@94
   157
        FINISH_PROCESS BOOLEAN:=FALSE;
adrian@94
   158
      BEGIN
adrian@94
   159
        -- Process Parameters
adrian@94
   160
        IF(p_PInstance_ID IS NOT NULL) THEN
adrian@94
   161
          --  Check for serial execution
adrian@94
   162
          DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
adrian@94
   163
          SELECT COUNT(*)
adrian@94
   164
          INTO v_count
adrian@94
   165
          FROM AD_PINSTANCE
adrian@94
   166
          WHERE AD_PROCESS_ID IN
adrian@94
   167
            (SELECT AD_PROCESS_ID FROM AD_PINSTANCE WHERE AD_PInstance_ID=p_PInstance_ID)
adrian@94
   168
            AND IsProcessing='Y'
gorkaion@239
   169
            AND AD_PInstance_ID<>p_PInstance_ID;
gorkaion@239
   170
          IF(v_count>0) THEN
adrian@94
   171
            RAISE_APPLICATION_ERROR(-20000, '@SerialProcessStillRunning@') ;
adrian@94
   172
          END IF;
adrian@94
   173
          --  Update AD_PInstance
adrian@94
   174
          DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
adrian@94
   175
          v_ResultStr:='PInstanceNotFound';
antonio@735
   176
          AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
adrian@94
   177
          -- Get Parameters
adrian@94
   178
          v_ResultStr:='ReadingParameters';
adrian@94
   179
          v_C_Order_ID:=NULL;
adrian@94
   180
          FOR Cur_Parameter IN
adrian@94
   181
            (SELECT i.Record_ID,
phillip@2231
   182
              i.AD_User_ID,
adrian@94
   183
              p.ParameterName,
adrian@94
   184
              p.P_String,
adrian@94
   185
              p.P_Number,
adrian@94
   186
              p.P_Date
adrian@94
   187
            FROM AD_PINSTANCE i
adrian@94
   188
            LEFT JOIN AD_PINSTANCE_PARA p
adrian@94
   189
              ON i.AD_PInstance_ID=p.AD_PInstance_ID
adrian@94
   190
            WHERE i.AD_PInstance_ID=p_PInstance_ID
adrian@94
   191
            ORDER BY p.SeqNo
adrian@94
   192
            )
adrian@94
   193
          LOOP
adrian@94
   194
            v_Record_ID:=Cur_Parameter.Record_ID;
phillip@2231
   195
            v_ADUserId:=Cur_Parameter.AD_User_ID;
adrian@94
   196
            IF(Cur_Parameter.ParameterName='DateInvoiced') THEN
adrian@94
   197
              v_DateInvoiced_aux:=Cur_Parameter.P_Date;
adrian@94
   198
              DBMS_OUTPUT.PUT_LINE('  DateInvoiced=' || v_DateInvoiced_aux) ;
adrian@94
   199
            ELSIF(Cur_Parameter.ParameterName='InvoiceToDate') THEN
adrian@94
   200
              v_InvoiceToDate:=Cur_Parameter.P_Date;
adrian@94
   201
              DBMS_OUTPUT.PUT_LINE('  InvoiceToDate=' || v_InvoiceToDate) ;
adrian@94
   202
            ELSIF(Cur_Parameter.ParameterName='AD_Org_ID') THEN
juanpablo@1605
   203
              v_AD_Org_ID:=Cur_Parameter.P_String;
adrian@94
   204
              DBMS_OUTPUT.PUT_LINE('  AD_Org_ID=' || v_AD_Org_ID) ;
adrian@94
   205
            ELSIF(Cur_Parameter.ParameterName='C_Order_ID') THEN
juanpablo@1605
   206
              v_C_Order_ID:=Cur_Parameter.P_String;
adrian@94
   207
              DBMS_OUTPUT.PUT_LINE('  C_Order_ID=' || v_C_Order_ID) ;
adrian@94
   208
            ELSIF(Cur_Parameter.ParameterName='C_BPartner_ID') THEN
juanpablo@1605
   209
              v_BPartner_ID:=Cur_Parameter.P_String;
adrian@94
   210
              DBMS_OUTPUT.PUT_LINE('  C_BPartner_ID=' || v_BPartner_ID) ;
adrian@94
   211
            ELSIF(Cur_Parameter.ParameterName='Selection') THEN
adrian@94
   212
              v_Selection:=Cur_Parameter.P_String;
adrian@94
   213
              DBMS_OUTPUT.PUT_LINE('  Selection=' || v_Selection) ;
adrian@94
   214
            ELSE
adrian@94
   215
              DBMS_OUTPUT.PUT_LINE('*** Unknown Parameter=' || Cur_Parameter.ParameterName) ;
adrian@94
   216
            END IF;
adrian@94
   217
          END LOOP; -- Get Parameter
adrian@94
   218
        ELSE
gorkaion@239
   219
          DBMS_OUTPUT.PUT_LINE('--<<C_Invoive_Create>>') ;
adrian@94
   220
          v_C_Order_ID:=p_Order_ID;
adrian@94
   221
          v_DateInvoiced_aux:=NULL;
adrian@94
   222
          v_InvoiceToDate:=NULL;
adrian@94
   223
          v_BPartner_ID:=NULL;
adrian@94
   224
          v_Selection:='N';
sivaraman@7186
   225
          BEGIN
sivaraman@7186
   226
            SELECT COALESCE(UPDATEDBY,'0') INTO v_ADUserId  FROM C_ORDER  WHERE C_ORDER_ID=p_Order_ID;
sivaraman@7186
   227
          EXCEPTION
sivaraman@7186
   228
          WHEN OTHERS THEN
sivaraman@7186
   229
            v_ADUserId := '0';            
sivaraman@7186
   230
          END;
adrian@94
   231
        END IF;
adrian@94
   232
      BEGIN --BODY
adrian@94
   233
        /**
adrian@94
   234
        * Order Loop == all not completely invoiced orders == No Summary ==
adrian@94
   235
        */
adrian@94
   236
sandra@22266
   237
          DBMS_OUTPUT.PUT_LINE('  C_Order_ID=' || coalesce(v_C_Order_ID, 'null') || ', BPartner_ID=' || coalesce(v_BPartner_ID, 'null') || ', AD_Org_ID=' || coalesce(v_AD_Org_ID, 'null')) ;
adrian@94
   238
          -- For all Orders
david@19193
   239
          
david@19193
   240
          FOR Cur_Bpblocked IN(
david@19197
   241
            SELECT   bp.customer_blocking ,  bp.so_invoice_blocking , bp.name , bp.c_bpartner_id
david@19197
   242
            FROM C_ORDER o   -- Specific InProgress Order
david@19197
   243
              JOIN m_pricelist pl ON pl.m_pricelist_id = o.m_pricelist_id
david@19197
   244
              JOIN C_BPartner bp ON o.c_bpartner_id = bp.c_bpartner_id
david@19197
   245
            WHERE (bp.customer_blocking='Y' AND bp.so_invoice_blocking='Y')
david@19197
   246
            AND ((o.C_Order_ID=v_C_Order_ID 
david@19197
   247
                    AND v_Selection = 'N'
david@19197
   248
                    AND o.DocStatus IN('IP', 'CO')) -- OR all completed and closed orders
david@19197
   249
                OR(v_C_Order_ID IS NULL AND v_Selection = 'N'
david@19197
   250
                    AND o.DocStatus IN('CO', 'CL')
david@19197
   251
                    AND o.IsSOTrx='Y'  -- not for InvoiceSchedule
david@19197
   252
                    AND o.InvoiceRule<>'S'  -- of all or a specific business partner / organization
david@19197
   253
                    AND(v_BPartner_ID IS NULL OR o.C_BPartner_ID=v_BPartner_ID)
david@19197
   254
                    AND(v_InvoiceToDate IS NULL
david@19197
   255
                        OR o.DateOrdered<TRUNC(v_InvoiceToDate) +1)
david@19197
   256
                    AND(v_AD_Org_ID IS NULL OR o.AD_Org_ID=v_AD_Org_ID)) -- where there is something to invoice
david@19197
   257
                OR (v_Selection = 'Y'
david@19197
   258
                    AND o.ISSELECTED = 'Y'
david@19197
   259
                    AND o.DocStatus IN('CO', 'CL')
david@19197
   260
                    AND o.IsSOTrx='Y'  -- not for InvoiceSchedule
sandra@20116
   261
                    AND o.InvoiceRule NOT IN ('S','D')))  -- of all or a specific business partner / organization
david@19197
   262
                AND EXISTS
david@19197
   263
                    (SELECT *
david@19197
   264
                     FROM C_ORDERLINE l
david@19197
   265
                     WHERE o.C_Order_ID=l.C_Order_ID
david@19197
   266
                     AND l.QtyOrdered<>l.QtyInvoiced)
david@19197
   267
            group by bp.c_bpartner_id,bp.customer_blocking ,  bp.so_invoice_blocking ,  bp.name
david@19193
   268
          )
david@19193
   269
          LOOP    
david@19193
   270
            IF(v_Message NOT LIKE '%@ThebusinessPartner@%') THEN
david@19193
   271
             v_MessageBPB:=SUBSTR(v_MessageBPB ||' '||'@ThebusinessPartner@'||' '|| Cur_Bpblocked.name ||' '||'@BusinessPartnerBlocked2@', 0, 2000);
david@19193
   272
            ELSE
david@19193
   273
            v_MessageBPB:=SUBSTR(v_MessageBPB ||', '||'@ThebusinessPartner@'||' '|| Cur_Bpblocked.name ||' '||'@BusinessPartnerBlocked2@', 0, 2000);
david@19193
   274
            END IF;
david@19193
   275
          END LOOP;
david@19193
   276
          
adrian@94
   277
          FOR Cur_Order IN
david@19193
   278
            (SELECT o.*, pl.istaxincluded as taxincludedpl, bp.customer_blocking ,  bp.so_invoice_blocking , bp.name as bpname
david@19193
   279
             FROM C_ORDER o   -- Specific InProgress Order
david@19193
   280
             JOIN m_pricelist pl ON pl.m_pricelist_id = o.m_pricelist_id
david@19193
   281
             JOIN C_BPartner bp ON o.c_bpartner_id = bp.c_bpartner_id
david@19193
   282
              WHERE (bp.customer_blocking='N' OR bp.so_invoice_blocking='N')
david@19193
   283
              AND ((o.C_Order_ID=v_C_Order_ID 
david@19193
   284
              AND v_Selection = 'N'
adrian@94
   285
              AND o.DocStatus IN('IP', 'CO')) -- OR all completed and closed orders
adrian@94
   286
              OR(v_C_Order_ID IS NULL AND v_Selection = 'N'
adrian@94
   287
              AND o.DocStatus IN('CO', 'CL')
gorkaion@17356
   288
              AND o.IsSOTrx='Y'  -- not for InvoiceSchedule
gorkaion@17356
   289
              AND o.InvoiceRule<>'S'  -- of all or a specific business partner / organization
adrian@94
   290
              AND(v_BPartner_ID IS NULL
gorkaion@17356
   291
              OR o.C_BPartner_ID=v_BPartner_ID)
adrian@94
   292
              AND(v_InvoiceToDate IS NULL
gorkaion@17356
   293
              OR o.DateOrdered<TRUNC(v_InvoiceToDate) +1)
adrian@94
   294
              AND(v_AD_Org_ID IS NULL
gorkaion@17356
   295
              OR o.AD_Org_ID=v_AD_Org_ID)) -- where there is something to invoice
adrian@94
   296
              OR (v_Selection = 'Y'
david@19193
   297
              AND o.ISSELECTED = 'Y'
adrian@94
   298
              AND o.DocStatus IN('CO', 'CL')
gorkaion@17356
   299
              AND o.IsSOTrx='Y'  -- not for InvoiceSchedule
gorkaion@17356
   300
              AND o.InvoiceRule NOT IN ('S','D')  -- of all or a specific business partner / organization
sandra@20116
   301
              ))
david@19193
   302
              AND EXISTS
adrian@94
   303
              (SELECT *
adrian@94
   304
              FROM C_ORDERLINE l
adrian@94
   305
              WHERE o.C_Order_ID=l.C_Order_ID
gorkaion@239
   306
                AND l.QtyOrdered<>l.QtyInvoiced
sandra@20116
   307
              )
david@19193
   308
              ORDER BY o.DateOrdered,o.PriorityRule,
gorkaion@17356
   309
              o.C_BPartner_ID,
gorkaion@17356
   310
              o.DocumentNo
adrian@94
   311
            )
adrian@94
   312
          LOOP
david@19193
   313
            
adrian@94
   314
            DBMS_OUTPUT.PUT_LINE('Order ' || Cur_Order.DocumentNo || ', ID=' || Cur_Order.C_Order_ID) ;
gorkaion@239
   315
            -- Parameter Order Date => Invoice Date, Accounting Date
adrian@94
   316
            v_DateInvoiced:=v_DateInvoiced_aux;
adrian@94
   317
            IF(v_DateInvoiced IS NULL) THEN
adrian@94
   318
              SELECT MAX(s.movementDate)
adrian@94
   319
              INTO v_DateInvoiced
adrian@94
   320
              FROM M_INOUT s,
adrian@94
   321
                M_INOUTLINE sl,
adrian@94
   322
                C_ORDERLINE ol
adrian@94
   323
              WHERE s.M_InOut_ID=sl.M_InOut_ID
adrian@94
   324
                AND sl.C_OrderLine_ID=ol.C_OrderLine_ID
adrian@94
   325
                AND ol.C_Order_ID=Cur_Order.C_Order_ID;
adrian@94
   326
              IF(v_DateInvoiced IS NULL) THEN
adrian@94
   327
                v_DateInvoiced:=Cur_Order.DateOrdered;
adrian@94
   328
              END IF;
adrian@94
   329
            END IF;
adrian@94
   330
            /**
adrian@94
   331
            * Invoice Rules
adrian@94
   332
            */
adrian@94
   333
            -- (I)mmediate    -- Invoice the full/remaining order
adrian@94
   334
            IF(Cur_Order.InvoiceRule='I') THEN
adrian@94
   335
              DBMS_OUTPUT.PUT_LINE('Invoice Rule: Immediate') ;
adrian@94
   336
              v_ResultStr:='Fetching_OrderLine';
adrian@94
   337
              OPEN Cur_OrderLine(Cur_Order.C_Order_ID) ;
adrian@94
   338
              Cur_OrderLine_ISOPEN:=true;
adrian@94
   339
              FETCH Cur_OrderLine INTO ptr_ol;
adrian@94
   340
              IF(Cur_OrderLine%NOTFOUND) THEN
adrian@94
   341
                DBMS_OUTPUT.PUT_LINE('- No Lines -') ;
adrian@94
   342
                Next_Order:=TRUE;
adrian@94
   343
              END IF;
adrian@94
   344
              IF(NOT Next_Order) THEN
adrian@94
   345
                -- Shipment Reference (optional)
adrian@94
   346
                DECLARE
adrian@94
   347
                  Cur_DocTypeCOrder RECORD;
adrian@94
   348
                BEGIN
adrian@94
   349
                  FOR Cur_DocTypeCOrder IN
adrian@94
   350
                    (SELECT dt.PrintName || ' ' || ox.DocumentNo||' '||(
adrian@94
   351
                      CASE
adrian@94
   352
                        WHEN ox.PoReference IS NULL
adrian@94
   353
                        THEN NULL
adrian@94
   354
                        ELSE '('||ox.PoReference||')'
adrian@94
   355
                      END
adrian@94
   356
                      ) AS Reference
adrian@94
   357
                    FROM C_DOCTYPE dt,
adrian@94
   358
                      C_ORDER ox
adrian@94
   359
                    WHERE dt.C_DocType_ID=ox.C_Order_ID
adrian@94
   360
                      AND ox.C_Order_ID=Cur_Order.C_Order_ID
adrian@94
   361
                    ORDER BY ox.DocumentNo DESC
adrian@94
   362
                    )
adrian@94
   363
                  LOOP -- last shipment
adrian@94
   364
                    v_Reference:=Cur_DocTypeCOrder.Reference;
adrian@94
   365
                    EXIT;
adrian@94
   366
                  END LOOP;
adrian@94
   367
                EXCEPTION
adrian@94
   368
                WHEN OTHERS THEN
adrian@94
   369
                  DBMS_OUTPUT.PUT_LINE('- No ShipReference -') ;
adrian@94
   370
                END;
adrian@94
   371
              END IF;--Next_Order
adrian@94
   372
              -- After (D)elivery   -- Check M_InOut for invoice quantity
adrian@94
   373
              -- After (O)rder completely delivered
adrian@94
   374
            ELSIF((Cur_Order.InvoiceRule IN('D', 'O'))) THEN
adrian@94
   375
              DBMS_OUTPUT.PUT_LINE('Invoice Rule: Delivery and Order completely delivered') ;
adrian@94
   376
              IF(Cur_Order.InvoiceRule='O') THEN  SELECT COUNT(*)
david@3064
   377
                INTO v_NoRecNotComplDelivered
adrian@94
   378
                FROM C_ORDERLINE
adrian@94
   379
                WHERE C_Order_ID=Cur_Order.C_Order_ID
gorkaion@239
   380
                  AND qtyOrdered<>qtyDelivered;
david@3064
   381
                IF(v_NoRecNotComplDelivered>0) THEN
adrian@94
   382
                  DBMS_OUTPUT.PUT_LINE('Order not completely delivered') ;
adrian@94
   383
                  Next_Order:=TRUE;
adrian@94
   384
                END IF;
adrian@94
   385
              END IF;
adrian@94
   386
              IF(NOT Next_Order) THEN
adrian@94
   387
                v_ResultStr:='Fetching_ShipmentLine';
adrian@94
   388
                OPEN Cur_InOutLine(Cur_Order.C_Order_ID) ;
adrian@94
   389
                Cur_InOutLine_ISOPEN:=true;
adrian@94
   390
                FETCH Cur_InOutLine INTO ptr_sl;
adrian@94
   391
                IF(Cur_InOutLine%NOTFOUND) THEN
adrian@94
   392
                  DBMS_OUTPUT.PUT_LINE('- No Lines -') ;
adrian@94
   393
                  Next_Order:=TRUE;
adrian@94
   394
                END IF;
adrian@94
   395
              END IF;--Next_Order
adrian@94
   396
              IF(NOT Next_Order) THEN
adrian@94
   397
                -- Shipment Reference
adrian@94
   398
                v_ResultStr:='Ship Reference Order_ID=' || Cur_Order.C_Order_ID;
adrian@94
   399
                SELECT REFERENCE_ORDER
adrian@94
   400
                INTO v_ReferenceOrder
adrian@94
   401
                FROM AD_ORGINFO
adrian@94
   402
                WHERE AD_Org_ID=ptr_sl.AD_Org_ID;
adrian@94
   403
                IF(v_ReferenceOrder='Y') THEN
adrian@94
   404
                  DECLARE
adrian@94
   405
                    Cur_CDoctypeCOrder RECORD;
adrian@94
   406
                  BEGIN
adrian@94
   407
                    -- last shipment
adrian@94
   408
                    FOR Cur_CDoctypeCOrder IN
adrian@94
   409
                      (SELECT dt.PrintName || ' ' || ox.DocumentNo||' '||(
adrian@94
   410
                        CASE
adrian@94
   411
                          WHEN ox.PoReference IS NULL
adrian@94
   412
                          THEN NULL
adrian@94
   413
                          ELSE '('||ox.PoReference||')'
adrian@94
   414
                        END
adrian@94
   415
                        ) AS Reference
adrian@94
   416
                      FROM C_DOCTYPE dt,
adrian@94
   417
                        C_ORDER ox
adrian@94
   418
                      WHERE dt.C_DocType_ID=ox.C_Order_ID
adrian@94
   419
                        AND ox.C_Order_ID=Cur_Order.C_Order_ID
adrian@94
   420
                      ORDER BY ox.DocumentNo DESC
adrian@94
   421
                      )
adrian@94
   422
                    LOOP
adrian@94
   423
                      v_Reference:=Cur_CDoctypeCOrder.Reference;
adrian@94
   424
                      EXIT;
adrian@94
   425
                    END LOOP;
adrian@94
   426
                  END;
adrian@94
   427
                ELSE  SELECT dt.PrintName || ' ' || m.DocumentNo||' '||(
adrian@94
   428
                    CASE
adrian@94
   429
                      WHEN m.PoReference IS NULL
adrian@94
   430
                      THEN NULL
adrian@94
   431
                      ELSE '('||m.PoReference||')'
adrian@94
   432
                    END
adrian@94
   433
                    )
adrian@94
   434
                  INTO v_Reference
adrian@94
   435
                  FROM M_INOUT m,
adrian@94
   436
                    C_DOCTYPE dt
adrian@94
   437
                  WHERE m.C_DocType_ID=dt.C_DocType_ID
adrian@94
   438
                    AND m.M_InOut_ID=ptr_sl.M_InOut_ID;
adrian@94
   439
                END IF;
adrian@94
   440
              END IF;--Next_Order
adrian@94
   441
              -- Do not invoice
adrian@94
   442
            ELSIF(Cur_Order.InvoiceRule='N') THEN
adrian@94
   443
              DBMS_OUTPUT.PUT_LINE('Invoice Rule: Do not invoice') ;
adrian@94
   444
              Next_Order:=TRUE;
gorkaion@239
   445
              -- (S)chedule after Delivery -> Invoice Schedule (ignore here)
adrian@94
   446
            ELSE
adrian@94
   447
              Next_Order:=TRUE;
adrian@94
   448
            END IF;
adrian@94
   449
            IF(NOT Next_Order) THEN
adrian@94
   450
              /**
adrian@94
   451
              * Create Invoice Header ---------------------------------------
adrian@94
   452
              */
adrian@94
   453
              -- Get Order DocType Info - approved from Invoice DocType
sivaraman@7125
   454
              BEGIN
adrianromero@7306
   455
              v_DocType_ID:=NULL;
adrian@94
   456
              v_ResultStr:='GetDocTypeInfo - ' || Cur_Order.C_DocType_ID;  SELECT od.C_DocTypeInvoice_ID,
adrian@94
   457
                od.DocSubTypeSO
adrian@94
   458
              INTO v_DocType_ID,
adrian@94
   459
                v_DocSubTypeSO
adrian@94
   460
              FROM C_DOCTYPE od,
adrian@94
   461
                C_DOCTYPE ID
adrian@94
   462
              WHERE od.C_DocType_ID=Cur_Order.C_DocType_ID
sivaraman@7125
   463
                AND od.C_DocTypeInvoice_ID=ID.C_DocType_ID
sivaraman@7125
   464
                AND ID.IsActive='Y';
sivaraman@7125
   465
              EXCEPTION
sivaraman@7125
   466
              WHEN OTHERS THEN
adrianromero@7306
   467
                v_DocType_ID:=NULL;
adrianromero@7306
   468
              END;
adrianromero@7306
   469
              IF (v_DocType_ID IS NULL) THEN
sivaraman@7125
   470
                RAISE_APPLICATION_ERROR(-20000, '@NoDocumentTypeFound@') ;
adrianromero@7306
   471
              END IF;
adrian@94
   472
              --
adrian@94
   473
              Ad_Sequence_Next('C_Invoice', Cur_Order.AD_Client_ID, p_Invoice_ID) ;
adrian@94
   474
              Ad_Sequence_Doctype(v_DocType_ID, Cur_Order.AD_Client_ID, 'Y', v_DocumentNo) ;
adrian@94
   475
              IF(v_DocumentNo IS NULL) THEN
adrian@94
   476
                Ad_Sequence_Doc('DocumentNo_C_Invoice', Cur_Order.AD_Client_ID, 'Y', v_DocumentNo) ;
adrian@94
   477
              END IF;
adrian@94
   478
              --
adrian@94
   479
              DBMS_OUTPUT.PUT_LINE('  Invoice_ID=' || p_Invoice_ID || ' DocumentNo=' || v_DocumentNo) ;
adrian@94
   480
              v_ResultStr:='InsertInvoice ' || p_Invoice_ID;
adrian@94
   481
              INSERT
adrian@94
   482
              INTO C_INVOICE
adrian@94
   483
                (
adrian@94
   484
                  C_Invoice_ID, C_Order_ID, AD_Client_ID, AD_Org_ID,
adrian@94
   485
                  IsActive, Created, CreatedBy, Updated, UpdatedBy,
adrian@94
   486
                  IsSOTrx, DocumentNo, DocStatus, DocAction,
adrian@94
   487
                  Processing, Processed, C_DocType_ID, C_DocTypeTarget_ID,
asier@799
   488
                  Description, SalesRep_ID,
adrian@94
   489
                  DateInvoiced,
carlos@1259
   490
                  DatePrinted, IsPrinted, DateAcct, TaxDate, 
carlos@1259
   491
                  C_PaymentTerm_ID,
adrian@94
   492
                  C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID, POReference,
adrian@94
   493
                  DateOrdered, IsDiscountPrinted, C_Currency_ID, PaymentRule,
adrian@94
   494
                  C_Charge_ID, ChargeAmt, IsSelfService, TotalLines,
adrian@94
   495
                  GrandTotal, M_PriceList_ID, C_Campaign_ID, C_Project_ID,
eduardo@18851
   496
                  C_CostCenter_ID, A_Asset_ID,
eduardo@7028
   497
                  C_Activity_ID, AD_OrgTrx_ID, User1_ID, User2_ID, fin_paymentmethod_id
adrian@94
   498
                )
adrian@94
   499
                VALUES
adrian@94
   500
                (
adrian@94
   501
                  p_Invoice_ID, Cur_Order.C_Order_ID, Cur_Order.AD_Client_ID, Cur_Order.AD_Org_ID,
phillip@2196
   502
                  'Y', now(), v_ADUserId, now(), v_ADUserId , 
carlos@1259
   503
                  Cur_Order.IsSOTrx, v_DocumentNo, 'DR', 'CO', 
carlos@1259
   504
                  'N', 'N', v_DocType_ID, v_DocType_ID, 
carlos@1259
   505
                  Cur_Order.Description, Cur_Order.SalesRep_ID, 
carlos@1259
   506
                  v_DateInvoiced, 
carlos@1259
   507
                  NULL, 'N', v_DateInvoiced, v_DateInvoiced, -- DateInvoiced=DateAcct
adrian@94
   508
                  Cur_Order.C_PaymentTerm_ID, Cur_Order.C_BPartner_ID, Cur_Order.BillTo_ID, Cur_Order.AD_User_ID,
adrian@94
   509
                  Cur_Order.POReference, Cur_Order.DateOrdered, Cur_Order.IsDiscountPrinted, Cur_Order.C_Currency_ID,
adrian@94
   510
                  Cur_Order.PaymentRule, Cur_Order.C_Charge_ID, Cur_Order.ChargeAmt, Cur_Order.IsSelfService,
eduardo@18851
   511
                  0, 0, Cur_Order.M_PriceList_ID, Cur_Order.C_Campaign_ID, Cur_Order.C_Project_ID,
eduardo@18851
   512
                  Cur_Order.C_CostCenter_ID, Cur_Order.A_Asset_ID, 
eduardo@18851
   513
                  Cur_Order.C_Activity_ID, Cur_Order.AD_OrgTrx_ID, Cur_Order.User1_ID,
eduardo@7028
   514
                  Cur_Order.User2_ID, Cur_Order.fin_paymentmethod_id
adrian@94
   515
                )
adrian@94
   516
                ;
adrian@94
   517
              --
adrian@94
   518
              v_NoRecords:=v_NoRecords + 1;
adrian@94
   519
              v_LineNo:=0;
adrian@94
   520
              -- Insert Reference/Comment to Shipment for Warehouse Order/Pickup
rajagopal@3819
   521
rajagopal@3819
   522
		IF(v_Reference IS NOT NULL AND TRIM(v_Reference)!=''
adrian@94
   523
                /*AND v_DocSubTypeSO = 'WP'*/
adrian@94
   524
                ) THEN
adrian@94
   525
                v_ResultStr:='InsertShipmentReference';
adrian@94
   526
                Ad_Sequence_Next('C_InvoiceLine', Cur_Order.C_Order_ID, v_NextNo) ;
rajagopal@3819
   527
               INSERT
adrian@94
   528
                INTO C_INVOICELINE
adrian@94
   529
                  (
adrian@94
   530
                    C_InvoiceLine_ID, AD_Client_ID, AD_Org_ID, IsActive,
adrian@94
   531
                    Created, CreatedBy, Updated, UpdatedBy,
adrian@94
   532
                    C_Invoice_ID, C_OrderLine_ID, M_InOutLine_ID, Line,
adrian@94
   533
                    Description, M_Product_ID, QtyInvoiced, PriceList,
adrian@94
   534
                    PriceActual, PriceLimit, LineNetAmt, C_Charge_ID,
naiara@19828
   535
                    ChargeAmt, C_UOM_ID, C_Tax_ID, PriceStd, 
pandeeswari@20456
   536
                    explode, bom_parent_id,
pandeeswari@20456
   537
                    C_Project_ID, C_Bpartner_ID, A_Asset_ID,
pandeeswari@20456
   538
                    C_Costcenter_ID, User1_ID, User2_ID
adrian@94
   539
                  )
adrian@94
   540
                  VALUES
adrian@94
   541
                  (
adrian@94
   542
                    v_NextNo, Cur_Order.AD_Client_ID, Cur_Order.AD_Org_ID, 'Y',
phillip@2231
   543
                    now(), v_ADUserId, now(), v_ADUserId,
adrian@94
   544
                    p_Invoice_ID, NULL, NULL, v_LineNo,
adrian@94
   545
                    v_Reference, NULL, 0, 0,
adrian@94
   546
                    0, 0, 0, NULL,
naiara@19828
   547
                    0, NULL, NULL, 0, 
jon@20476
   548
                    ptr_sl.explode, (SELECT c_invoiceline_id 
naiara@19828
   549
                                            FROM c_invoiceline 
naiara@19828
   550
                                            WHERE c_invoice_id=p_Invoice_ID 
naiara@19828
   551
                                               and c_orderline_id = (SELECT BOM_parent_id 
naiara@19828
   552
                                                                     FROM c_orderline 
jon@20476
   553
                                                                     WHERE c_orderline_id = ptr_sl.c_orderline_id)),
jon@20476
   554
                    ptr_sl.C_Project_ID, ptr_sl.C_Bpartner_ID, ptr_sl.A_Asset_ID,
jon@20476
   555
                    ptr_sl.C_Costcenter_ID, ptr_sl.User1_ID, ptr_sl.User2_ID
adrian@94
   556
                  )
adrian@94
   557
                  ;
adrian@94
   558
              END IF;
adrian@94
   559
            END IF;--Next_Order
adrian@94
   560
            IF(NOT Next_Order) THEN
adrian@94
   561
              /**
adrian@94
   562
              * Create Lines
adrian@94
   563
              */
adrian@94
   564
              v_ResultStr:='InsertLines InvRule=' || Cur_Order.InvoiceRule;
adrian@94
   565
              -- (I)mmediate    -- Invoice the full/remaining order
adrian@94
   566
              IF(Cur_Order.InvoiceRule='I') THEN
adrian@94
   567
                /**
adrian@94
   568
                * Create Invoice Lines from Order Lines -------------------------
adrian@94
   569
                */
adrian@94
   570
                LOOP
adrian@94
   571
                  v_Qty:=ptr_ol.QtyOrdered - ptr_ol.QtyInvoiced;
adrian@94
   572
                  --MODIFIED BY F.IRIAZABAL
adrian@94
   573
                  v_QtyOrder:=ptr_ol.QuantityOrder;
adrian@94
   574
                  -- Don't copy zero product lines
adrian@94
   575
                  IF(v_Qty=0 AND ptr_ol.M_Product_ID IS NOT NULL) THEN
adrian@94
   576
                    DBMS_OUTPUT.PUT_LINE('- Skip 0 Qty line -') ;
adrian@94
   577
                    Next_O_Line:=TRUE;
adrian@94
   578
                  END IF;
adrian@94
   579
                  IF(NOT Next_O_Line) THEN
adrian@94
   580
                    --
adrian@94
   581
                    Ad_Sequence_Next('C_InvoiceLine', Cur_Order.C_Order_ID, v_NextNo) ;
adrian@94
   582
                    v_LineNo:=v_LineNo + 10;
adrian@94
   583
                    DBMS_OUTPUT.PUT_LINE('    Line ' || ptr_ol.Line || '  Qty=' || v_Qty) ;
adrian@94
   584
                    v_ResultStr:='CreateInvoiceLine from Order';
eduardo@18445
   585
	                  IF(ptr_ol.M_Product_ID IS NOT NULL) THEN
eduardo@18445
   586
											SELECT REVPLANTYPE, ISDEFERREDREVENUE, PERIODNUMBER, DEFAULTPERIOD,
eduardo@18445
   587
											EXPPLANTYPE, ISDEFERREDEXPENSE, PERIODNUMBER_EXP, DEFAULTPERIOD_EXP
eduardo@18445
   588
											INTO v_revplantype, v_isdeferredrevenue, v_periodnumber, v_defaultperiod,
eduardo@18445
   589
											v_expplantype, v_isdeferredexpense, v_periodnumber_exp, v_defaultperiod_exp
eduardo@18445
   590
											FROM M_PRODUCT
eduardo@18445
   591
											WHERE M_PRODUCT_ID = ptr_ol.M_Product_ID;
eduardo@18445
   592
											IF(Cur_Order.IsSOTrx = 'Y') THEN
eduardo@18445
   593
												v_period_inv:= c_get_default_period(p_Invoice_ID, v_defaultperiod);
eduardo@18445
   594
												IF(v_period_inv IS NOT NULL AND v_isdeferredrevenue = 'Y') THEN
eduardo@18445
   595
													v_isdeferred_inv := v_isdeferredrevenue;
eduardo@18445
   596
													v_defplantype_inv := v_revplantype;
eduardo@18445
   597
													v_periodnumber_inv := v_periodnumber;
eduardo@18445
   598
											  ELSE
eduardo@18445
   599
											  	v_period_inv := NULL;
eduardo@18445
   600
													v_isdeferred_inv := 'N';
eduardo@18445
   601
													v_defplantype_inv := NULL;
eduardo@18445
   602
													v_periodnumber_inv := NULL;
eduardo@18445
   603
												END IF;
eduardo@18445
   604
											ELSE
eduardo@18445
   605
												v_period_inv:= c_get_default_period(p_Invoice_ID, v_defaultperiod_exp);
eduardo@18445
   606
												IF(v_period_inv IS NOT NULL AND v_isdeferredexpense = 'Y') THEN
eduardo@18445
   607
													v_isdeferred_inv := v_isdeferredexpense;
eduardo@18445
   608
													v_defplantype_inv := v_expplantype;
eduardo@18445
   609
													v_periodnumber_inv := v_periodnumber_exp;
eduardo@18445
   610
											  ELSE
eduardo@18445
   611
											  	v_period_inv := NULL;
eduardo@18445
   612
													v_isdeferred_inv := 'N';
eduardo@18445
   613
													v_defplantype_inv := NULL;
eduardo@18445
   614
													v_periodnumber_inv := NULL;
eduardo@18445
   615
												END IF;
eduardo@18445
   616
											END IF;
eduardo@18445
   617
	                  END IF;
adrian@94
   618
                    INSERT
adrian@94
   619
                    INTO C_INVOICELINE
adrian@94
   620
                      (
adrian@94
   621
                        C_InvoiceLine_ID, AD_Client_ID, AD_Org_ID, IsActive,
adrian@94
   622
                        Created, CreatedBy, Updated, UpdatedBy,
adrian@94
   623
                        C_Invoice_ID, C_OrderLine_ID, M_InOutLine_ID, Line,
adrian@94
   624
                        Description, M_Product_ID, QtyInvoiced, PriceList,
adrian@94
   625
                        PriceActual, PriceLimit, LineNetAmt, C_Charge_ID,
adrian@94
   626
                        ChargeAmt, C_UOM_ID,
adrian@94
   627
                        C_Tax_ID,  --MODIFIED BY F.IRIAZABAL
ainhoa@3730
   628
                        QuantityOrder, M_Product_Uom_ID, PriceStd,
gorkaion@17356
   629
                        m_attributesetinstance_id, taxbaseamt,
david@18197
   630
                        gross_unit_price, line_gross_amount,
eduardo@18445
   631
                        grosspricestd, grosspricelist,
naiara@19828
   632
                        isdeferred, defplantype, periodnumber, c_period_id,
pandeeswari@20456
   633
                        explode, bom_parent_id,
pandeeswari@20456
   634
                        C_Project_ID, C_Bpartner_ID, A_Asset_ID,
pandeeswari@20456
   635
                        C_Costcenter_ID, User1_ID, User2_ID
adrian@94
   636
                      )
adrian@94
   637
                      VALUES
adrian@94
   638
                      (
adrian@94
   639
                        v_NextNo, ptr_ol.AD_Client_ID, ptr_ol.AD_Org_ID, 'Y',
phillip@2231
   640
                        now(), v_ADUserId, now(), v_ADUserId,
adrian@94
   641
                        p_Invoice_ID, ptr_ol.C_OrderLine_ID, NULL, v_LineNo,
adrian@94
   642
                        ptr_ol.Description, ptr_ol.M_Product_ID, v_Qty, ptr_ol.PriceList,
adrian@94
   643
                        ptr_ol.PriceActual, ptr_ol.PriceLimit, C_Currency_Round(v_Qty*ptr_ol.PriceActual, Cur_Order.C_Currency_ID, NULL), ptr_ol.C_Charge_ID,
adrian@94
   644
                        ptr_ol.ChargeAmt, ptr_ol.C_UOM_ID,
adrian@94
   645
                        ptr_ol.C_Tax_ID,  --MODIFIED BY F.IRIAZABAL
ainhoa@3730
   646
                        v_QtyOrder, ptr_ol.M_Product_Uom_ID, ptr_ol.PriceStd,
gorkaion@17356
   647
                        ptr_ol.m_attributesetinstance_id, ptr_ol.taxbaseamt,
david@18197
   648
                        ptr_ol.gross_unit_price, C_Currency_Round(v_Qty*ptr_ol.gross_unit_price, Cur_Order.C_Currency_ID, NULL),
eduardo@18445
   649
                        COALESCE(ptr_ol.grosspricestd,0), COALESCE(ptr_ol.grosspricelist,0),
naiara@19828
   650
                        v_isdeferred_inv, v_defplantype_inv, v_periodnumber_inv, v_period_inv,
naiara@19828
   651
                        ptr_ol.explode, (SELECT c_invoiceline_id 
naiara@19828
   652
                                            FROM c_invoiceline 
naiara@19828
   653
                                            WHERE c_invoice_id=p_Invoice_ID 
naiara@19828
   654
                                               and c_orderline_id = (SELECT BOM_parent_id 
naiara@19828
   655
                                                                     FROM c_orderline 
pandeeswari@20456
   656
                                                                     WHERE c_orderline_id = ptr_ol.c_orderline_id)),
pandeeswari@20456
   657
                        ptr_ol.C_Project_ID, ptr_ol.C_BPartner_ID, ptr_ol.A_Asset_ID,
pandeeswari@20456
   658
                        ptr_ol.C_Costcenter_ID, ptr_ol.User1_ID, ptr_ol.User2_ID
adrian@94
   659
                      )
adrian@94
   660
                      ;
adrian@94
   661
                  END IF;--Next_O_Line
gorkaion@239
   662
                  --<<Next_O_Line>>
adrian@94
   663
                  Next_O_Line:=FALSE;
adrian@94
   664
                  v_ResultStr:='Fetching_OrderLine(*)';
adrian@94
   665
                  FETCH Cur_OrderLine INTO ptr_ol;
adrian@94
   666
                  EXIT WHEN Cur_OrderLine%NOTFOUND;
adrian@94
   667
                END LOOP; -- Invoice Line from Order Lines
adrian@94
   668
                -- After (D)elivery  -- Check M_InOut for invoice quantity
adrian@94
   669
              ELSIF(Cur_Order.InvoiceRule IN('D', 'O')) THEN
adrian@94
   670
                /**
adrian@94
   671
                * Create Invoice Lines from Shipment Lines
adrian@94
   672
                */
adrian@94
   673
                LOOP
adrian@94
   674
                  -- Don't copy zero Product lines - or if already invoiced
adrian@94
   675
                  IF(ptr_sl.MovementQty=0 AND ptr_sl.M_Product_ID IS NOT NULL) THEN
adrian@94
   676
                    DBMS_OUTPUT.PUT_LINE('- Skip 0 Qty line -') ;
adrian@94
   677
                    Next_S_Line:=TRUE;
adrian@94
   678
                  END IF;
adrian@94
   679
                  IF(NOT Next_S_Line) THEN
adrian@94
   680
                    --
adrian@94
   681
                    Ad_Sequence_Next('C_InvoiceLine', Cur_Order.C_Order_ID, v_NextNo) ;
adrian@94
   682
                    v_LineNo:=v_LineNo + 10;
adrian@94
   683
                    DBMS_OUTPUT.PUT_LINE('    Line ' || ptr_sl.Line || '  Qty=' || ptr_sl.MovementQty) ;
adrian@94
   684
                    v_ResultStr:='CreateInvoiceLine from Shipment';
eduardo@18445
   685
                    IF(ptr_sl.M_Product_ID IS NOT NULL) THEN
eduardo@18445
   686
											SELECT REVPLANTYPE, ISDEFERREDREVENUE, PERIODNUMBER, DEFAULTPERIOD,
eduardo@18445
   687
											EXPPLANTYPE, ISDEFERREDEXPENSE, PERIODNUMBER_EXP, DEFAULTPERIOD_EXP
eduardo@18445
   688
											INTO v_revplantype, v_isdeferredrevenue, v_periodnumber, v_defaultperiod,
eduardo@18445
   689
											v_expplantype, v_isdeferredexpense, v_periodnumber_exp, v_defaultperiod_exp
eduardo@18445
   690
											FROM M_PRODUCT
eduardo@18445
   691
											WHERE M_PRODUCT_ID = ptr_sl.M_Product_ID;
eduardo@18445
   692
											IF(Cur_Order.IsSOTrx = 'Y') THEN
eduardo@18445
   693
												v_period_inv:= c_get_default_period(p_Invoice_ID, v_defaultperiod);
eduardo@18445
   694
												IF(v_period_inv IS NOT NULL AND v_isdeferredrevenue = 'Y') THEN
eduardo@18445
   695
													v_isdeferred_inv := v_isdeferredrevenue;
eduardo@18445
   696
													v_defplantype_inv := v_revplantype;
eduardo@18445
   697
													v_periodnumber_inv := v_periodnumber;
eduardo@18445
   698
											  ELSE
eduardo@18445
   699
											  	v_period_inv := NULL;
eduardo@18445
   700
													v_isdeferred_inv := 'N';
eduardo@18445
   701
													v_defplantype_inv := NULL;
eduardo@18445
   702
													v_periodnumber_inv := NULL;
eduardo@18445
   703
												END IF;
eduardo@18445
   704
											ELSE
eduardo@18445
   705
												v_period_inv:= c_get_default_period(p_Invoice_ID, v_defaultperiod_exp);
eduardo@18445
   706
												IF(v_period_inv IS NOT NULL AND v_isdeferredexpense = 'Y') THEN
eduardo@18445
   707
													v_isdeferred_inv := v_isdeferredexpense;
eduardo@18445
   708
													v_defplantype_inv := v_expplantype;
eduardo@18445
   709
													v_periodnumber_inv := v_periodnumber_exp;
eduardo@18445
   710
											  ELSE
eduardo@18445
   711
											  	v_period_inv := NULL;
eduardo@18445
   712
													v_isdeferred_inv := 'N';
eduardo@18445
   713
													v_defplantype_inv := NULL;
eduardo@18445
   714
													v_periodnumber_inv := NULL;
eduardo@18445
   715
												END IF;
eduardo@18445
   716
											END IF;
eduardo@18445
   717
	                  END IF;
eduardo@18445
   718
adrian@94
   719
                    INSERT
adrian@94
   720
                    INTO C_INVOICELINE
adrian@94
   721
                      (
adrian@94
   722
                        C_InvoiceLine_ID, AD_Client_ID, AD_Org_ID, IsActive,
adrian@94
   723
                        Created, CreatedBy, Updated, UpdatedBy,
adrian@94
   724
                        C_Invoice_ID, C_OrderLine_ID, M_InOutLine_ID, Line,
adrian@94
   725
                        Description, M_Product_ID, QtyInvoiced, PriceList,
adrian@94
   726
                        PriceActual, PriceLimit, LineNetAmt, C_Charge_ID,
adrian@94
   727
                        ChargeAmt, C_UOM_ID,
adrian@94
   728
                        C_Tax_ID,  --MODIFIED BY F.IRIAZABAL
ainhoa@3730
   729
                        QuantityOrder, M_Product_Uom_ID, PriceStd,
gorkaion@17356
   730
                        m_attributesetinstance_id, taxbaseamt,
david@18197
   731
                        gross_unit_price, line_gross_amount,
eduardo@18445
   732
                        grosspricestd, grosspricelist,
naiara@19828
   733
                        isdeferred, defplantype, periodnumber, c_period_id, 
pandeeswari@20456
   734
                        explode, bom_parent_id,
pandeeswari@20456
   735
                        C_Project_ID, C_Bpartner_ID, A_Asset_ID,
pandeeswari@20456
   736
                        C_Costcenter_ID, User1_ID, User2_ID
adrian@94
   737
                      )
adrian@94
   738
                      VALUES
adrian@94
   739
                      (
adrian@94
   740
                        v_NextNo, ptr_sl.AD_Client_ID, ptr_sl.AD_Org_ID, 'Y',
phillip@2231
   741
                        now(), v_ADUserId, now(), v_ADUserId,
adrian@94
   742
                        p_Invoice_ID, ptr_sl.C_OrderLine_ID, ptr_sl.M_InOutLine_ID, v_LineNo,
adrian@94
   743
                        ptr_sl.Description, ptr_sl.M_Product_ID, ptr_sl.MovementQty, ptr_sl.PriceList,
adrian@94
   744
                        ptr_sl.PriceActual, ptr_sl.PriceLimit, C_Currency_Round(ptr_sl.MovementQty*ptr_sl.PriceActual, Cur_Order.C_Currency_ID, NULL), ptr_sl.C_Charge_ID,
adrian@94
   745
                        ptr_sl.ChargeAmt, ptr_sl.C_UOM_ID,
adrian@94
   746
                        ptr_sl.C_Tax_ID,  --MODIFIED BY F.IRIAZABAL
ainhoa@3730
   747
                        ptr_sl.QuantityOrder, ptr_sl.M_Product_Uom_ID, ptr_sl.PriceStd,
gorkaion@17356
   748
                        ptr_sl.m_attributesetinstance_id, ptr_sl.taxbaseamt,
david@18197
   749
                        ptr_sl.gross_unit_price, C_Currency_Round(ptr_sl.MovementQty*ptr_sl.gross_unit_price, Cur_Order.C_Currency_ID, NULL),
eduardo@18445
   750
                        COALESCE(ptr_sl.grosspricestd,0), COALESCE(ptr_sl.grosspricelist,0),
naiara@19828
   751
                        v_isdeferred_inv, v_defplantype_inv, v_periodnumber_inv, v_period_inv,
naiara@19828
   752
                        ptr_sl.explode, (SELECT c_invoiceline_id 
naiara@19828
   753
                                            FROM c_invoiceline 
naiara@19828
   754
                                            WHERE c_invoice_id=p_Invoice_ID 
naiara@19828
   755
                                               and c_orderline_id = (SELECT BOM_parent_id 
naiara@19828
   756
                                                                     FROM c_orderline 
pandeeswari@20456
   757
                                                                     WHERE c_orderline_id = ptr_sl.c_orderline_id)),
sandra@20457
   758
                        ptr_sl.C_Project_ID, ptr_sl.C_BPartner_ID, ptr_sl.A_Asset_ID,
sandra@20457
   759
                        ptr_sl.C_Costcenter_ID, ptr_sl.User1_ID, ptr_sl.User2_ID
adrian@94
   760
                      )
adrian@94
   761
                      ;
adrian@94
   762
                  END IF;--Next_S_Line
gorkaion@239
   763
                  --<<Next_S_Line>>
adrian@94
   764
                  Next_S_Line:=FALSE;
adrian@94
   765
                  v_ResultStr:='Fetching_ShipmentLine(*)';
adrian@94
   766
                  FETCH Cur_InOutLine INTO ptr_sl;
adrian@94
   767
                  EXIT WHEN Cur_InOutLine%NOTFOUND;
adrian@94
   768
                END LOOP;
adrian@94
   769
              END IF;
adrian@94
   770
              -- Post it
adrianromero@6803
   771
              C_INVOICE_POST(NULL, p_Invoice_ID) ;
adrian@94
   772
			  --Added by p.Sarobe
juanpablo@1605
   773
			  IF(p_Invoice_ID='0') THEN
adrian@94
   774
          	  	  RAISE_APPLICATION_ERROR(-20000, '@InvoiceCreateFailed@');
adrian@94
   775
			  ELSE
adrian@94
   776
			  	  SELECT documentno
adrian@94
   777
	    	  	  INTO v_DocumentNo
adrian@94
   778
	    	  	  FROM C_INVOICE
adrian@94
   779
	    	  	  WHERE C_INVOICE_ID = p_Invoice_ID;
harikrishnan@7576
   780
	    	  	  IF(v_Message_Check <= 10) THEN
ioritz@19015
   781
	    	  	  	v_Message:=SUBSTR(v_Message||', '||'@InvoiceDocumentno@ ' || v_DocumentNo, 0, 2000);
harikrishnan@7576
   782
	    	  	  ELSE
harikrishnan@7576
   783
	    	  	  	IF(v_Message_Check = 11) THEN
ioritz@19015
   784
	    	  	  		v_Message :=SUBSTR(v_Message ||' @InvoiceNumberCheck@', 0, 2000);
harikrishnan@7576
   785
	    	  	  	END IF;
harikrishnan@7576
   786
	    	  	  END IF;
harikrishnan@7576
   787
	    	  	  v_Message_Check := v_Message_Check+1;
adrian@94
   788
        	  END IF;
adrian@94
   789
			  --Finished added by P.Sarobe
adrian@94
   790
            END IF;--Next_Order
gorkaion@239
   791
            --<<Next_Order>>
adrian@94
   792
            Next_Order:=FALSE;
adrian@94
   793
            v_ResultStr:='ClosingLine';
adrian@94
   794
            IF(Cur_OrderLine_ISOPEN) THEN
adrian@94
   795
              CLOSE Cur_OrderLine;
adrian@94
   796
              Cur_OrderLine_ISOPEN:=false;
adrian@94
   797
            END IF;
adrian@94
   798
            IF(Cur_InOutLine_ISOPEN) THEN
adrian@94
   799
              CLOSE Cur_InOutLine;
adrian@94
   800
              Cur_InOutLine_ISOPEN:=false;
adrian@94
   801
            END IF;
adrian@94
   802
          END LOOP; -- Order Loop
sandra@22266
   803
   
sandra@22266
   804
          SELECT count(*) 
sandra@22266
   805
          INTO v_isSchedule
pandeeswari@22807
   806
          FROM DUAL WHERE EXISTS (SELECT 1 FROM C_ORDER WHERE ISSELECTED = 'Y' AND INVOICERULE = 'S');
sandra@22266
   807
       
adrian@94
   808
        /**
adrian@94
   809
        * Invoice Schedule ======================================================
adrian@94
   810
        */
sandra@22266
   811
        IF(p_PInstance_ID IS NOT NULL AND v_C_Order_ID IS NULL AND (v_isSchedule > 0 OR v_Selection = 'N')) THEN -- Not when processing a single order
sandra@22266
   812
         DECLARE
adrian@94
   813
            -- Invoice Schedule Lines
adrian@94
   814
            Cur_InvoiceSchedule RECORD;
juanpablo@1605
   815
            v_Partner_ID VARCHAR2(32):='-1';
juanpablo@1605
   816
            v_InOut_ID VARCHAR2(32):='-1';
juanpablo@1605
   817
            v_gOrg_ID VARCHAR2(32) ;
juanpablo@1605
   818
            v_gOrder_ID VARCHAR2(32) ;
juanpablo@1605
   819
            v_gInOut_ID VARCHAR2(32) ;
juanpablo@1605
   820
            v_gBillTo_ID VARCHAR2(32) ;
juanpablo@1605
   821
            v_gBPartner_Location_ID VARCHAR2(32) ;
juanpablo@1605
   822
            v_gSalesRep_ID VARCHAR2(32) ;
adrianromero@8118
   823
            v_gPaymentRule VARCHAR2(60) ;
juanpablo@1605
   824
            v_gPaymentTerm_ID VARCHAR2(32) ;
juanpablo@1605
   825
            v_gCurrency_ID VARCHAR2(32) ;
juanpablo@1605
   826
            v_gCampaign_ID VARCHAR2(32) ;
juanpablo@1605
   827
            v_gProject_ID VARCHAR2(32) ;
juanpablo@1605
   828
            v_gActivitiy_ID VARCHAR2(32) ;
juanpablo@1605
   829
            v_gOrgTrx_ID VARCHAR2(32) ;
juanpablo@1605
   830
            v_gUser1_ID VARCHAR2(32) ;
juanpablo@1605
   831
            v_gUser2_ID VARCHAR2(32) ;
adrian@94
   832
            v_description NVARCHAR2(120) ;
gorkaion@14601
   833
            v_isfinaldoctype CHAR(1) := 'N';
gorkaion@17356
   834
            v_istaxincluded CHAR(1) := '0';
adrian@94
   835
          BEGIN
juanpablo@1605
   836
            p_Invoice_ID:='-1';
adrian@94
   837
            DBMS_OUTPUT.PUT_LINE('  BPartner_ID=' || v_BPartner_ID || ', AD_Org_ID=' || v_AD_Org_ID) ;
adrian@94
   838
            FOR Cur_InvoiceSchedule IN
adrian@94
   839
              (SELECT sh.AD_Client_ID,
adrian@94
   840
                sh.AD_Org_ID,
adrian@94
   841
                sh.M_InOut_ID,
adrian@94
   842
                sh.C_DocType_ID, (
adrian@94
   843
                CASE ao.Reference_Order
adrian@94
   844
                  WHEN 'Y'
adrian@94
   845
                  THEN o.DocumentNo
adrian@94
   846
                  ELSE sh.DocumentNo
adrian@94
   847
                END
adrian@94
   848
                ) AS DocumentNo,
sandra@16210
   849
                coalesce(sh.Description, o.Description) as description,
sandra@16210
   850
                coalesce(sh.C_BPartner_ID,o.c_bpartner_id) as c_bpartner_id,
pandeeswari@20456
   851
                ol.C_Bpartner_ID as line_c_bpartner_id,
adrian@94
   852
                o.BillTo_ID,
adrian@94
   853
                o.C_BPartner_Location_ID,
adrian@94
   854
                sh.AD_User_ID,
adrian@94
   855
                sh.DateOrdered,
sandra@16210
   856
                coalesce(sh.C_Order_ID,o.C_Order_ID) as C_Order_ID,
adrian@94
   857
                o.SalesRep_ID,
adrian@94
   858
                COALESCE(o.C_PaymentTerm_ID, bp.C_PaymentTerm_ID) AS C_PaymentTerm_ID,
adrian@94
   859
                COALESCE(o.IsDiscountPrinted, bp.IsDiscountPrinted) AS IsDiscountPrinted,
adrian@94
   860
                o.C_Currency_ID,
adrian@94
   861
                COALESCE(o.PaymentRule, bp.PaymentRule) AS PaymentRule,
adrian@94
   862
                COALESCE(o.M_PriceList_ID, bp.M_PriceList_ID) AS M_PriceList_ID,
adrian@94
   863
                o.C_Campaign_ID,
adrian@94
   864
                o.C_Project_ID,
eduardo@18851
   865
                ol.C_Project_ID as line_C_Project_ID,
adrian@94
   866
                o.C_Activity_ID,
adrian@94
   867
                o.AD_OrgTrx_ID,
adrian@94
   868
                o.User1_ID,
eduardo@18851
   869
                ol.User1_ID as line_User1_ID,
adrian@94
   870
                o.User2_ID,
eduardo@18851
   871
                ol.User2_ID as line_User2_ID,
eduardo@18851
   872
                o.C_CostCenter_ID,
eduardo@18851
   873
                ol.C_CostCenter_ID as line_C_CostCenter_ID,
eduardo@18851
   874
                o.A_Asset_ID,
eduardo@18851
   875
                ol.A_Asset_ID as line_A_Asset_ID,
adrian@94
   876
                bp.C_InvoiceSchedule_ID,
adrian@94
   877
                o.InvoiceRule,
ioritz@19732
   878
                RPAD(substr(bp.InvoiceGrouping, instr(bp.InvoiceGrouping, '_') + 1), 15, '0') AS InvoiceGrouping,
adrian@94
   879
                sh.POReference,
adrian@94
   880
                sh.C_Charge_ID,
adrian@94
   881
                COALESCE(sh.ChargeAmt, 0) AS ChargeAmt,
adrian@94
   882
                --
adrian@94
   883
                sl.AD_Org_ID AS Line_AD_Org_ID,
adrian@94
   884
                sl.M_InOutLine_ID,
adrian@94
   885
                sl.Line,
adrian@94
   886
                sl.Description AS Line_Description,
adrian@94
   887
                sl.C_OrderLine_ID,
adrian@94
   888
                sl.M_Product_ID,
adrian@94
   889
                sl.C_UOM_ID,
adrian@94
   890
                sl.MovementQty,
adrian@94
   891
                --MODIFIED BY F.IRIAZABAL
adrian@94
   892
                sl.QuantityOrder,
adrian@94
   893
                sl.M_Product_UOM_ID,
adrian@94
   894
                COALESCE(ol.PriceList, 0) AS PriceList,
adrian@94
   895
                COALESCE(ol.PriceActual, 0) AS PriceActual,
adrian@94
   896
                COALESCE(ol.PriceLimit, 0) AS PriceLimit,
adrian@94
   897
                ol.C_Tax_ID, --ol.IsTaxIncluded, -- Could be NULL !!
adrian@94
   898
                ol.C_Charge_ID AS Line_C_Charge_ID,
adrian@94
   899
                COALESCE(ol.ChargeAmt, 0) AS Line_ChargeAmt,
adrian@94
   900
                sh.movementDate,
ainhoa@3730
   901
                ol.PriceStd,
gorkaion@17356
   902
                ol.gross_unit_price,
pandeeswari@22807
   903
                sl.m_attributesetinstance_id,
gorkaion@17356
   904
                ol.taxbaseamt, o.fin_paymentmethod_id,
david@18202
   905
                pl.istaxincluded,
david@18202
   906
                ol.grosspricestd,
naiara@19828
   907
                ol.grosspricelist, 
naiara@19828
   908
                sl.explode, sl.bom_parent_id
adrian@94
   909
              FROM M_INOUT sh,
adrian@94
   910
                M_INOUTLINE sl,
adrian@94
   911
                C_ORDER o,
adrian@94
   912
                C_ORDERLINE ol,
gorkaion@17356
   913
                m_pricelist pl,
adrian@94
   914
                AD_ORGINFO ao,
adrian@94
   915
                C_BPARTNER bp
adrian@94
   916
              LEFT JOIN
adrian@94
   917
                (SELECT C_InvoiceSchedule_ID,
adrian@94
   918
                  C_Invoicetodate(InvoiceFrequency,(
adrian@94
   919
                  CASE InvoiceFrequency
adrian@94
   920
                    WHEN 'W'
adrian@94
   921
                    THEN TO_NUMBER(InvoiceWeekday)
adrian@94
   922
                    ELSE InvoiceDay
adrian@94
   923
                  END
adrian@94
   924
                  ), (
adrian@94
   925
                  CASE InvoiceFrequency
adrian@94
   926
                    WHEN 'W'
adrian@94
   927
                    THEN TO_NUMBER(InvoiceWeekDayCutoff)
adrian@94
   928
                    ELSE InvoiceDayCutoff
adrian@94
   929
                  END
antonio@735
   930
                  ), now()) AS DateLimit
adrian@94
   931
                FROM C_INVOICESCHEDULE
adrian@94
   932
                )
adrian@94
   933
                si
adrian@94
   934
                ON bp.C_InvoiceSchedule_ID=si.C_InvoiceSchedule_ID
david@19193
   935
              WHERE(bp.customer_blocking='N' OR bp.so_invoice_blocking='N')
david@19193
   936
                AND (sh.M_InOut_ID=sl.M_InOut_ID
adrian@94
   937
                AND sh.C_BPartner_ID=bp.C_BPartner_ID
gorkaion@2596
   938
                AND ol.C_Order_ID=o.C_Order_ID
adrian@94
   939
                AND sl.C_OrderLine_ID=ol.C_OrderLine_ID
gorkaion@17356
   940
                AND o.m_pricelist_id = pl.m_pricelist_id
adrian@94
   941
                AND sh.AD_Org_ID=ao.AD_Org_ID  -- for all BPartners or a specific
adrian@94
   942
                AND(v_BPartner_ID IS NULL
adrian@94
   943
                OR sh.C_BPartner_ID=v_BPartner_ID) -- for all Organizations or a specific
adrian@94
   944
                AND(v_AD_Org_ID IS NULL
adrian@94
   945
                OR sh.AD_Org_ID=v_AD_Org_ID) -- completed shipments
adrian@94
   946
                AND o.DocStatus IN('CO', 'CL')
adrian@94
   947
                AND o.IsSOTrx='Y'  -- completed orders
adrian@94
   948
                AND sh.DocStatus IN('CO', 'CL') -- we need to invoice - and not invoiced
gorkaion@239
   949
                AND ol.QtyOrdered<>ol.QtyInvoiced
gorkaion@239
   950
                AND sl.IsInvoiced<>'Y'  --  just invoice scheduled or after delivery orders (invoiceRule Scheduled or after delivery). (O)rder completely delivered pending!!!
adrian@94
   951
                AND o.InvoiceRule IN('S', 'D') --  not invoice logistic inouts
adrian@94
   952
                AND(sh.IsLogistic='N'
adrian@94
   953
                OR sh.IsLogistic IS NULL) --  enforce limitDate to invoice
adrian@94
   954
                AND(v_InvoiceToDate IS NULL
gorkaion@239
   955
                OR TRUNC(o.DateOrdered)<=v_InvoiceToDate) -- Selection
adrian@94
   956
                AND((v_Selection='Y'
adrian@94
   957
                AND o.IsSelected='Y') -- or scheduled
gorkaion@239
   958
                OR(v_Selection<>'Y'
adrian@94
   959
                AND(si.DateLimit IS NULL
david@19193
   960
                OR TRUNC(o.DateOrdered)<=si.DateLimit)) ))
gorkaion@17356
   961
              ORDER BY sh.C_BPartner_ID, pl.istaxincluded, (
ioritz@19732
   962
                CASE TO_CHAR(SUBSTR(RPAD(substr(bp.InvoiceGrouping, instr(bp.InvoiceGrouping, '_') + 1), 15, '0'), 1, 1))
adrian@94
   963
                  WHEN '0'
juanpablo@1605
   964
                  THEN '0'
adrian@94
   965
                  ELSE sh.AD_Org_ID
adrian@94
   966
                END
adrian@94
   967
                ) ||'-'|| (
ioritz@19732
   968
                CASE TO_CHAR(SUBSTR(RPAD(substr(bp.InvoiceGrouping, instr(bp.InvoiceGrouping, '_') + 1), 15, '0'), 2, 1))
adrian@94
   969
                  WHEN '0'
juanpablo@1605
   970
                  THEN '0'
adrian@94
   971
                  ELSE sh.C_Order_ID
adrian@94
   972
                END
adrian@94
   973
                ) ||'-'|| (
ioritz@19732
   974
                CASE TO_CHAR(SUBSTR(RPAD(substr(bp.InvoiceGrouping, instr(bp.InvoiceGrouping, '_') + 1), 15, '0'), 3, 1))
adrian@94
   975
                  WHEN '0'
juanpablo@1605
   976
                  THEN '0'
adrian@94
   977
                  ELSE sh.M_InOut_ID
adrian@94
   978
                END
adrian@94
   979
                ) ||'-'|| (
ioritz@19732
   980
                CASE TO_CHAR(SUBSTR(RPAD(substr(bp.InvoiceGrouping, instr(bp.InvoiceGrouping, '_') + 1), 15, '0'), 4, 1))
adrian@94
   981
                  WHEN '0'
juanpablo@1605
   982
                  THEN '0'
adrian@94
   983
                  ELSE o.BillTo_ID
adrian@94
   984
                END
adrian@94
   985
                ) ||'-'|| (
ioritz@19732
   986
                CASE TO_CHAR(SUBSTR(RPAD(substr(bp.InvoiceGrouping, instr(bp.InvoiceGrouping, '_') + 1), 15, '0'), 5, 1))
adrian@94
   987
                  WHEN '0'
juanpablo@1605
   988
                  THEN '0'
adrian@94
   989
                  ELSE o.C_BPartner_Location_ID
adrian@94
   990
                END
adrian@94
   991
                ) ||'-'|| (
ioritz@19732
   992
                CASE TO_CHAR(SUBSTR(RPAD(substr(bp.InvoiceGrouping, instr(bp.InvoiceGrouping, '_') + 1), 15, '0'), 6, 1))
adrian@94
   993
                  WHEN '0'
juanpablo@1605
   994
                  THEN '0'
adrian@94
   995
                  ELSE o.SalesRep_ID
adrian@94
   996
                END
adrian@94
   997
                ) ||'-'|| (
ioritz@19732
   998
                CASE TO_CHAR(SUBSTR(RPAD(substr(bp.InvoiceGrouping, instr(bp.InvoiceGrouping, '_') + 1), 15, '0'), 7, 1))
adrian@94
   999
                  WHEN '0'
adrian@94
  1000
                  THEN '0'
adrian@94
  1001
                  ELSE COALESCE(o.PaymentRule, bp.PaymentRule)
adrian@94
  1002
                END
adrian@94
  1003
                ) ||'-'|| (
ioritz@19732
  1004
                CASE TO_CHAR(SUBSTR(RPAD(substr(bp.InvoiceGrouping, instr(bp.InvoiceGrouping, '_') + 1), 15, '0'), 8, 1))
adrian@94
  1005
                  WHEN '0'
juanpablo@1605
  1006
                  THEN '0'
adrian@94
  1007
                  ELSE COALESCE(o.C_PaymentTerm_ID, bp.C_PaymentTerm_ID)
adrian@94
  1008
                END
adrian@94
  1009
                ) ||'-'|| (
ioritz@19732
  1010
                CASE TO_CHAR(SUBSTR(RPAD(substr(bp.InvoiceGrouping, instr(bp.InvoiceGrouping, '_') + 1), 15, '0'), 8, 1))
adrian@94
  1011
                  WHEN '0'
juanpablo@1605
  1012
                  THEN '0'
adrian@94
  1013
                  ELSE o.C_Currency_ID
adrian@94
  1014
                END
adrian@94
  1015
                ) ||'-'|| (
ioritz@19732
  1016
                CASE TO_CHAR(SUBSTR(RPAD(substr(bp.InvoiceGrouping, instr(bp.InvoiceGrouping, '_') + 1), 15, '0'), 10, 1))
adrian@94
  1017
                  WHEN '0'
juanpablo@1605
  1018
                  THEN '0'
adrian@94
  1019
                  ELSE o.C_Campaign_ID
adrian@94
  1020
                END
adrian@94
  1021
                ) ||'-'|| (
ioritz@19732
  1022
                CASE TO_CHAR(SUBSTR(RPAD(substr(bp.InvoiceGrouping, instr(bp.InvoiceGrouping, '_') + 1), 15, '0'), 11, 1))
adrian@94
  1023
                  WHEN '0'
juanpablo@1605
  1024
                  THEN '0'
adrian@94
  1025
                  ELSE o.C_Project_ID
adrian@94
  1026
                END
adrian@94
  1027
                ) ||'-'|| (
ioritz@19732
  1028
                CASE TO_CHAR(SUBSTR(RPAD(substr(bp.InvoiceGrouping, instr(bp.InvoiceGrouping, '_') + 1), 15, '0'), 12, 1))
adrian@94
  1029
                  WHEN '0'
juanpablo@1605
  1030
                  THEN '0'
adrian@94
  1031
                  ELSE o.C_Activity_ID
adrian@94
  1032
                END
adrian@94
  1033
                ) ||'-'|| (
ioritz@19732
  1034
                CASE TO_CHAR(SUBSTR(RPAD(substr(bp.InvoiceGrouping, instr(bp.InvoiceGrouping, '_') + 1), 15, '0'), 13, 1))
adrian@94
  1035
                  WHEN '0'
juanpablo@1605
  1036
                  THEN '0'
adrian@94
  1037
                  ELSE o.AD_OrgTrx_ID
adrian@94
  1038
                END
adrian@94
  1039
                ) ||'-'|| (
ioritz@19732
  1040
                CASE TO_CHAR(SUBSTR(RPAD(substr(bp.InvoiceGrouping, instr(bp.InvoiceGrouping, '_') + 1), 15, '0'), 14, 1))
adrian@94
  1041
                  WHEN '0'
juanpablo@1605
  1042
                  THEN '0'
adrian@94
  1043
                  ELSE o.User1_ID
adrian@94
  1044
                END
adrian@94
  1045
                ) ||'-'|| (
ioritz@19732
  1046
                CASE TO_CHAR(SUBSTR(RPAD(substr(bp.InvoiceGrouping, instr(bp.InvoiceGrouping, '_') + 1), 15, '0'), 15, 1))
adrian@94
  1047
                  WHEN '0'
juanpablo@1605
  1048
                  THEN '0'
adrian@94
  1049
                  ELSE o.User2_ID
adrian@94
  1050
                END
adrian@94
  1051
                ),
adrian@94
  1052
                sh.MovementDate,
adrian@94
  1053
                sh.M_InOut_ID,
adrian@94
  1054
                sl.Line
adrian@94
  1055
              )
adrian@94
  1056
            LOOP
adrian@94
  1057
              -- Invoice Date from Parameter                  -- or Order
antonio@735
  1058
              v_DateInvoiced:=COALESCE(v_DateInvoiced_aux, COALESCE(v_DateOrdered, now())) ; -- si.DateOrdered);
adrian@94
  1059
              --  Summary Invoice only if BP has Schedule setup and selected in invoice
gorkaion@239
  1060
              IF(NOT(Cur_InvoiceSchedule.C_InvoiceSchedule_ID IS NOT NULL AND Cur_InvoiceSchedule.InvoiceRule='S') AND v_InOut_ID<>Cur_InvoiceSchedule.M_InOut_ID) THEN -- and new document
juanpablo@1605
  1061
                v_Partner_ID:='-2'; -- indicate BP change to force new invoice
adrian@94
  1062
              END IF;
adrian@94
  1063
              -- BPartner changed - New Invoice
adrian@94
  1064
              /*
adrian@94
  1065
              1.-Order organization (AD_Org_ID)
adrian@94
  1066
              2.-Order (C_Order_ID)
adrian@94
  1067
              3.-Shipment (M_InOut_ID)
adrian@94
  1068
              4.-Invoice location (BillTo_ID)
adrian@94
  1069
              5.-Ship location (C_BPartner_Location_ID)
adrian@94
  1070
              6.-Sales representative (SalesRep_ID)
adrian@94
  1071
              7.-Payment rule (PaymentRule)
adrian@94
  1072
              8.-Payment term (C_PaymentTerm_ID)
adrian@94
  1073
              9.-Currency (C_Currency_ID)
adrian@94
  1074
              10.-Campaign (C_Campaign_ID)
adrian@94
  1075
              11.-Project (C_Project_ID)
adrian@94
  1076
              12.-Activity (C_Activity_ID)
adrian@94
  1077
              13.-Organization transaction (AD_OrgTrx_ID)
adrian@94
  1078
              14.-User dimension 1 (User1_ID)
adrian@94
  1079
              15.-User dimension 2 (User2_ID)
adrian@94
  1080
              */
gorkaion@17356
  1081
              IF((v_Partner_ID<>Cur_InvoiceSchedule.C_BPartner_ID) OR (v_istaxincluded <> cur_invoiceschedule.istaxincluded) OR(SUBSTR(Cur_InvoiceSchedule.InvoiceGrouping, 1, 1)='1' AND v_gOrg_ID<>Cur_InvoiceSchedule.AD_Org_ID) OR(SUBSTR(Cur_InvoiceSchedule.InvoiceGrouping, 2, 1)='1' AND v_gOrder_ID<>Cur_InvoiceSchedule.C_Order_ID) OR(SUBSTR(Cur_InvoiceSchedule.InvoiceGrouping, 3, 1)='1' AND v_gInOut_ID<>Cur_InvoiceSchedule.M_InOut_ID) OR(SUBSTR(Cur_InvoiceSchedule.InvoiceGrouping, 4, 1)='1' AND v_gBillTo_ID<>Cur_InvoiceSchedule.BillTo_ID) OR(SUBSTR(Cur_InvoiceSchedule.InvoiceGrouping, 5, 1)='1' AND v_gBPartner_Location_ID<>Cur_InvoiceSchedule.C_BPartner_Location_ID) OR(SUBSTR(Cur_InvoiceSchedule.InvoiceGrouping, 6, 1)='1' AND v_gSalesRep_ID<>Cur_InvoiceSchedule.SalesRep_ID) OR(SUBSTR(Cur_InvoiceSchedule.InvoiceGrouping, 7, 1)='1' AND v_gPaymentRule<>Cur_InvoiceSchedule.PaymentRule) OR(SUBSTR(Cur_InvoiceSchedule.InvoiceGrouping, 8, 1)='1' AND v_gPaymentTerm_ID<>Cur_InvoiceSchedule.C_PaymentTerm_ID) OR(SUBSTR(
adrianromero@7470
  1082
                Cur_InvoiceSchedule.InvoiceGrouping, 9, 1)='1' AND v_gCurrency_ID<>Cur_InvoiceSchedule.C_Currency_ID) OR(SUBSTR(Cur_InvoiceSchedule.InvoiceGrouping, 10, 1)='1' AND v_gCampaign_ID<>Cur_InvoiceSchedule.C_Campaign_ID) OR(SUBSTR(Cur_InvoiceSchedule.InvoiceGrouping, 11, 1)='1' AND ((v_gProject_ID<>Cur_InvoiceSchedule.C_Project_ID) OR (v_gProject_ID IS NULL AND Cur_InvoiceSchedule.C_Project_ID IS NOT NULL) OR (v_gProject_ID IS NOT NULL AND Cur_InvoiceSchedule.C_Project_ID IS NULL))) OR(SUBSTR(Cur_InvoiceSchedule.InvoiceGrouping, 12, 1)='1' AND v_gActivitiy_ID<>Cur_InvoiceSchedule.C_Activity_ID) OR(SUBSTR(Cur_InvoiceSchedule.InvoiceGrouping, 13, 1)='1' AND v_gOrgTrx_ID<>Cur_InvoiceSchedule.AD_OrgTrx_ID) OR(SUBSTR(Cur_InvoiceSchedule.InvoiceGrouping, 14, 1)='1' AND v_gUser1_ID<>Cur_InvoiceSchedule.User1_ID) OR(SUBSTR(Cur_InvoiceSchedule.InvoiceGrouping, 15, 1)='1' AND v_gUser2_ID<>Cur_InvoiceSchedule.User2_ID)) THEN
adrian@94
  1083
                v_Partner_ID:=Cur_InvoiceSchedule.C_BPartner_ID;
gorkaion@17356
  1084
                v_istaxincluded:=cur_invoiceschedule.istaxincluded;
adrian@94
  1085
                v_gOrg_ID:=Cur_InvoiceSchedule.AD_Org_ID;
adrian@94
  1086
                v_gOrder_ID:=Cur_InvoiceSchedule.C_Order_ID;
adrian@94
  1087
                v_gInOut_ID:=Cur_InvoiceSchedule.M_InOut_ID;
adrian@94
  1088
                v_gBillTo_ID:=Cur_InvoiceSchedule.BillTo_ID;
adrian@94
  1089
                v_gBPartner_Location_ID:=Cur_InvoiceSchedule.C_BPartner_Location_ID;
adrian@94
  1090
                v_gSalesRep_ID:=Cur_InvoiceSchedule.SalesRep_ID;
adrian@94
  1091
                v_gPaymentRule:=Cur_InvoiceSchedule.PaymentRule;
adrian@94
  1092
                v_gPaymentTerm_ID:=Cur_InvoiceSchedule.C_PaymentTerm_ID;
adrian@94
  1093
                v_gCurrency_ID:=Cur_InvoiceSchedule.C_Currency_ID;
adrian@94
  1094
                v_gCampaign_ID:=Cur_InvoiceSchedule.C_Campaign_ID;
adrian@94
  1095
                v_gProject_ID:=Cur_InvoiceSchedule.C_Project_ID;
adrian@94
  1096
                v_gActivitiy_ID:=Cur_InvoiceSchedule.C_Activity_ID;
adrian@94
  1097
                v_gOrgTrx_ID:=Cur_InvoiceSchedule.AD_OrgTrx_ID;
adrian@94
  1098
                v_gUser1_ID:=Cur_InvoiceSchedule.User1_ID;
adrian@94
  1099
                v_gUser2_ID:=Cur_InvoiceSchedule.User2_ID;
adrian@94
  1100
                IF(SUBSTR(Cur_InvoiceSchedule.InvoiceGrouping, 5, 1)='1') THEN  SELECT NAME
adrian@94
  1101
                  INTO v_description
adrian@94
  1102
                  FROM C_BPARTNER_LOCATION
adrian@94
  1103
                  WHERE C_BPARTNER_LOCATION_ID=v_gBPartner_Location_ID;
adrian@94
  1104
                ELSE
adrian@94
  1105
                  v_description:=NULL;
adrian@94
  1106
                END IF;
adrian@94
  1107
                -- Post it, if not first time
juanpablo@1605
  1108
                IF(p_Invoice_ID<>'-1') THEN
gorkaion@14601
  1109
                  Ad_Sequence_Doctype(v_DocType_ID, Cur_InvoiceSchedule.AD_Client_ID, 'Y', v_DocumentNo) ;
gorkaion@14601
  1110
                  IF(v_DocumentNo IS NULL) THEN
gorkaion@14601
  1111
                    Ad_Sequence_Doc('DocumentNo_C_Invoice', Cur_InvoiceSchedule.AD_Client_ID, 'Y', v_DocumentNo) ;
gorkaion@14601
  1112
                  END IF;
adrian@94
  1113
                  UPDATE C_INVOICE
adrian@94
  1114
                    SET DateInvoiced=v_DateInvoiced,
gorkaion@14601
  1115
                    DateAcct=v_DateInvoiced,
gorkaion@14601
  1116
                    documentno = v_DocumentNo
adrian@94
  1117
                  WHERE C_Invoice_ID=p_Invoice_ID;
adrianromero@6803
  1118
                  C_INVOICE_POST(NULL, p_Invoice_ID) ;
gorkaion@14601
  1119
                  v_isfinaldoctype := 'N';
adrian@94
  1120
                END IF;
adrian@94
  1121
                -- Get Document Type for Invoice
adrian@94
  1122
                v_ResultStr:='GetDocumentType';
adrian@94
  1123
                IF(Cur_InvoiceSchedule.C_Order_ID IS NULL) THEN
adrian@94
  1124
                  BEGIN
asier@799
  1125
                    v_DocType_ID:=Ad_Get_Doctype(Cur_InvoiceSchedule.AD_Client_ID, Cur_InvoiceSchedule.AD_Org_ID, 'ARI') ;                    --
adrian@94
  1126
                  EXCEPTION
adrian@94
  1127
                  WHEN OTHERS THEN
adrian@94
  1128
                    RAISE_APPLICATION_ERROR(-20000, '@NoDefaultInvoice@') ;
adrian@94
  1129
                  END;
adrian@94
  1130
                ELSE
sivaraman@7125
  1131
                  BEGIN
adrianromero@7306
  1132
                  v_DocType_ID:=NULL;
gorkaion@14601
  1133
                  SELECT od.C_DocTypeInvoice_ID, CASE od.isreturn WHEN 'Y' THEN 'N' ELSE 'Y' END
gorkaion@14601
  1134
                  INTO v_DocType_ID, v_isfinaldoctype
adrian@94
  1135
                  FROM C_DOCTYPE od,
adrian@94
  1136
                    C_DOCTYPE ID,
adrian@94
  1137
                    C_ORDER o
adrian@94
  1138
                  WHERE od.C_DocType_ID=o.C_DocType_ID
adrian@94
  1139
                    AND od.C_DocTypeInvoice_ID=ID.C_DocType_ID
sivaraman@7125
  1140
                    AND o.C_Order_ID=Cur_InvoiceSchedule.C_Order_ID
sivaraman@7125
  1141
                    AND ID.IsActive='Y';
sivaraman@7125
  1142
                  EXCEPTION
sivaraman@7125
  1143
                  WHEN OTHERS THEN
adrianromero@7306
  1144
                    v_DocType_ID:=NULL;
adrianromero@7306
  1145
                  END;
adrianromero@7306
  1146
                  IF (v_DocType_ID IS NULL) THEN
sivaraman@7125
  1147
                    RAISE_APPLICATION_ERROR(-20000, '@NoDocumentTypeFound@') ;
adrianromero@7306
  1148
                  END IF;
adrianromero@7306
  1149
adrian@94
  1150
                END IF;
adrian@94
  1151
                IF(NOT FINISH_PROCESS) THEN
adrian@94
  1152
                  -- Get other defaults
adrian@94
  1153
                  Ad_Sequence_Next('C_Invoice', Cur_InvoiceSchedule.AD_Client_ID, p_Invoice_ID) ;
gorkaion@14601
  1154
                  Ad_Sequence_Doctype(v_DocType_ID, Cur_InvoiceSchedule.AD_Client_ID, 'N', v_DocumentNo) ;
adrian@94
  1155
                  IF(v_DocumentNo IS NULL) THEN
gorkaion@14601
  1156
                    Ad_Sequence_Doc('DocumentNo_C_Invoice', Cur_InvoiceSchedule.AD_Client_ID, 'N', v_DocumentNo) ;
adrian@94
  1157
                  END IF;
adrian@94
  1158
                  --
adrian@94
  1159
                  DBMS_OUTPUT.PUT_LINE('  SumInvoice_ID=' || p_Invoice_ID || ' DocumentNo=' || v_DocumentNo) ;
adrian@94
  1160
                  v_ResultStr:='InsertSumInvoice ' || p_Invoice_ID;
gorkaion@14601
  1161
                  v_AD_Client_ID := Cur_InvoiceSchedule.AD_Client_ID;
adrian@94
  1162
                  INSERT
adrian@94
  1163
                  INTO C_INVOICE
adrian@94
  1164
                    (
adrian@94
  1165
                      C_Invoice_ID, C_Order_ID, AD_Client_ID, AD_Org_ID,
adrian@94
  1166
                      IsActive, Created, CreatedBy, Updated,
adrian@94
  1167
                      UpdatedBy, IsSOTrx, DocumentNo, DocStatus,
adrian@94
  1168
                      DocAction, Processing, Processed, C_DocType_ID,
asier@799
  1169
                      C_DocTypeTarget_ID, Description,
adrian@94
  1170
                      SalesRep_ID, DateInvoiced, DatePrinted, IsPrinted,
carlos@1259
  1171
                      DateAcct, TaxDate,
adrian@94
  1172
                      C_PaymentTerm_ID, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID,
adrian@94
  1173
                      POReference, DateOrdered, IsDiscountPrinted, C_Currency_ID,
adrian@94
  1174
                      PaymentRule, C_Charge_ID, ChargeAmt, IsSelfService,
adrian@94
  1175
                      TotalLines, GrandTotal, M_PriceList_ID, C_Campaign_ID,
adrian@94
  1176
                      C_Project_ID, C_Activity_ID, AD_OrgTrx_ID, User1_ID,
eduardo@18851
  1177
                      C_CostCenter_ID, A_Asset_ID, 
eduardo@7028
  1178
                      User2_ID, fin_paymentmethod_id
adrian@94
  1179
                    )
adrian@94
  1180
                    VALUES
adrian@94
  1181
                    (
adrian@94
  1182
                      p_Invoice_ID, Cur_InvoiceSchedule.C_Order_ID, Cur_InvoiceSchedule.AD_Client_ID, Cur_InvoiceSchedule.AD_Org_ID,
phillip@2231
  1183
                       'Y', now(), v_ADUserId, now(),
phillip@2231
  1184
                      v_ADUserId, 'Y', v_DocumentNo, 'DR',
adrian@94
  1185
                       'CO', 'N', 'N', v_DocType_ID,
jon@22873
  1186
                      v_DocType_ID, COALESCE(v_description, Cur_InvoiceSchedule.Description),
adrian@94
  1187
                      Cur_InvoiceSchedule.SalesRep_ID, v_DateInvoiced, NULL, 'N',
carlos@1259
  1188
                      v_DateInvoiced, v_DateInvoiced, -- DateInvoiced=DateAcct
adrian@94
  1189
                      Cur_InvoiceSchedule.C_PaymentTerm_ID, Cur_InvoiceSchedule.C_BPartner_ID, Cur_InvoiceSchedule.BillTo_ID, Cur_InvoiceSchedule.AD_User_ID,
adrian@94
  1190
                      Cur_InvoiceSchedule.POReference, Cur_InvoiceSchedule.DateOrdered, Cur_InvoiceSchedule.IsDiscountPrinted, Cur_InvoiceSchedule.C_Currency_ID,
adrian@94
  1191
                      Cur_InvoiceSchedule.PaymentRule, NULL, 0, 'N',
adrian@94
  1192
                      0, 0, Cur_InvoiceSchedule.M_PriceList_ID, Cur_InvoiceSchedule.C_Campaign_ID,
adrian@94
  1193
                      Cur_InvoiceSchedule.C_Project_ID, Cur_InvoiceSchedule.C_Activity_ID, Cur_InvoiceSchedule.AD_OrgTrx_ID, Cur_InvoiceSchedule.User1_ID,
eduardo@18851
  1194
                      Cur_InvoiceSchedule.C_CostCenter_ID, Cur_InvoiceSchedule.A_Asset_ID, 
eduardo@7028
  1195
                      Cur_InvoiceSchedule.User2_ID, Cur_InvoiceSchedule.fin_paymentmethod_id
adrian@94
  1196
                    )
adrian@94
  1197
                    ;
adrian@94
  1198
                  --
adrian@94
  1199
                  v_NoRecords:=v_NoRecords + 1;
adrian@94
  1200
                  v_LineNo:=0;
juanpablo@1605
  1201
                  v_InOut_ID:='-1';
carlos@17554
  1202
                  IF(v_Message NOT LIKE '%@InvoiceDocumentno@%') THEN
ioritz@19015
  1203
                      v_Message:=SUBSTR(v_Message||', '||'@InvoiceDocumentno@ ' || v_DocumentNo, 0, 2000);
carlos@17554
  1204
                  ELSE
ioritz@19015
  1205
                      v_Message:=SUBSTR(v_Message||', '|| v_DocumentNo, 0, 2000);
carlos@17554
  1206
                  END IF;
adrian@94
  1207
                END IF;
gorkaion@14601
  1208
              ELSIF (v_isfinaldoctype = 'N') THEN --If docType is not a final one update it
gorkaion@14601
  1209
                BEGIN
gorkaion@14601
  1210
                  v_DocType_ID:=NULL;
gorkaion@14601
  1211
                  SELECT od.C_DocTypeInvoice_ID, CASE od.isreturn WHEN 'Y' THEN 'N' ELSE 'Y' END
gorkaion@14601
  1212
                    INTO v_DocType_ID, v_isfinaldoctype
gorkaion@14601
  1213
                  FROM C_DOCTYPE od,
gorkaion@14601
  1214
                       C_DOCTYPE ID,
gorkaion@14601
  1215
                       C_ORDER o
gorkaion@14601
  1216
                  WHERE od.C_DocType_ID=o.C_DocType_ID
gorkaion@14601
  1217
                    AND od.C_DocTypeInvoice_ID=ID.C_DocType_ID
gorkaion@14601
  1218
                    AND o.C_Order_ID=Cur_InvoiceSchedule.C_Order_ID
gorkaion@14601
  1219
                    AND ID.IsActive='Y';
gorkaion@14601
  1220
                EXCEPTION
gorkaion@14601
  1221
                WHEN OTHERS THEN
gorkaion@14601
  1222
                  v_DocType_ID:=NULL;
gorkaion@14601
  1223
                END;
gorkaion@14601
  1224
                IF (v_DocType_ID IS NULL) THEN
gorkaion@14601
  1225
                  RAISE_APPLICATION_ERROR(-20000, '@NoDocumentTypeFound@') ;
gorkaion@14601
  1226
                END IF;
gorkaion@14601
  1227
                Ad_Sequence_Doctype(v_DocType_ID, Cur_InvoiceSchedule.AD_Client_ID, 'N', v_DocumentNo) ;
gorkaion@14601
  1228
                IF(v_DocumentNo IS NULL) THEN
gorkaion@14601
  1229
                  Ad_Sequence_Doc('DocumentNo_C_Invoice', Cur_InvoiceSchedule.AD_Client_ID, 'N', v_DocumentNo) ;
gorkaion@14601
  1230
                END IF;
gorkaion@14601
  1231
                UPDATE c_invoice
gorkaion@14601
  1232
                SET c_doctypetarget_id = v_DocType_ID,
gorkaion@14601
  1233
                    c_doctype_id = v_DocType_ID,
gorkaion@14601
  1234
                    documentno = v_DocumentNo
gorkaion@14601
  1235
                WHERE c_invoice_id = p_Invoice_ID;
adrian@94
  1236
              END IF;--FINISH_PROCESS
adrianromero@10099
  1237
adrianromero@10099
  1238
              v_InOut_ID:=Cur_InvoiceSchedule.M_InOut_ID;
adrianromero@10099
  1239
adrian@94
  1240
              IF(NOT FINISH_PROCESS) THEN
adrian@94
  1241
                -- Normal Line
adrian@94
  1242
                Ad_Sequence_Next('C_InvoiceLine', Cur_InvoiceSchedule.C_Order_ID, v_NextNo) ;
adrian@94
  1243
                v_LineNo:=v_LineNo + 10;
adrian@94
  1244
                DBMS_OUTPUT.PUT_LINE('    SumLine ' || Cur_InvoiceSchedule.Line || '  Qty=' || Cur_InvoiceSchedule.MovementQty) ;
adrian@94
  1245
                v_ResultStr:='CreateInvoiceLine_Sum';
eduardo@18445
  1246
                IF(Cur_InvoiceSchedule.M_Product_ID IS NOT NULL) THEN
eduardo@18447
  1247
									SELECT REVPLANTYPE, ISDEFERREDREVENUE, PERIODNUMBER, DEFAULTPERIOD
eduardo@18447
  1248
									INTO v_revplantype, v_isdeferredrevenue, v_periodnumber, v_defaultperiod
eduardo@18445
  1249
									FROM M_PRODUCT
eduardo@18445
  1250
									WHERE M_PRODUCT_ID = Cur_InvoiceSchedule.M_Product_ID;
eduardo@18447
  1251
									v_period_inv:= c_get_default_period(p_Invoice_ID, v_defaultperiod);
eduardo@18447
  1252
									IF(v_period_inv IS NOT NULL AND v_isdeferredrevenue = 'Y') THEN
eduardo@18447
  1253
										v_isdeferred_inv := v_isdeferredrevenue;
eduardo@18447
  1254
										v_defplantype_inv := v_revplantype;
eduardo@18447
  1255
										v_periodnumber_inv := v_periodnumber;
eduardo@18447
  1256
								  ELSE
eduardo@18447
  1257
								  	v_period_inv := NULL;
eduardo@18447
  1258
										v_isdeferred_inv := 'N';
eduardo@18447
  1259
										v_defplantype_inv := NULL;
eduardo@18447
  1260
										v_periodnumber_inv := NULL;
eduardo@18445
  1261
									END IF;
eduardo@18445
  1262
                END IF;
adrian@94
  1263
                INSERT
adrian@94
  1264
                INTO C_INVOICELINE
adrian@94
  1265
                  (
adrian@94
  1266
                    C_InvoiceLine_ID, AD_Client_ID, AD_Org_ID, IsActive,
adrian@94
  1267
                    Created, CreatedBy, Updated, UpdatedBy,
adrian@94
  1268
                    C_Invoice_ID, C_OrderLine_ID, M_InOutLine_ID, Line,
adrian@94
  1269
                    Description, M_Product_ID, QtyInvoiced, PriceList,
adrian@94
  1270
                    PriceActual, PriceLimit, LineNetAmt, C_Charge_ID,
adrian@94
  1271
                    ChargeAmt, C_UOM_ID,
adrian@94
  1272
                    C_Tax_ID,  --MODIFIED BY F.IRIAZABAL
ainhoa@3730
  1273
                    QuantityOrder, M_Product_Uom_ID, PriceStd,
gorkaion@17356
  1274
                    m_attributesetinstance_id, taxbaseamt,
david@18197
  1275
                    gross_unit_price, line_gross_amount,
eduardo@18445
  1276
                    grosspricestd, grosspricelist,
eduardo@18851
  1277
                    isdeferred, defplantype, periodnumber, c_period_id,
eduardo@18851
  1278
                    c_project_id, c_costcenter_id, a_asset_id,
pandeeswari@20456
  1279
                    user1_id, user2_id, c_bpartner_id,
naiara@19828
  1280
                    explode, bom_parent_id
adrian@94
  1281
                  )
adrian@94
  1282
                  VALUES
adrian@94
  1283
                  (
adrian@94
  1284
                    v_NextNo, Cur_InvoiceSchedule.AD_Client_ID, Cur_InvoiceSchedule.Line_AD_Org_ID, 'Y',
phillip@2231
  1285
                    now(), v_ADUserId, now(), v_ADUserId,
adrian@94
  1286
                    p_Invoice_ID, Cur_InvoiceSchedule.C_OrderLine_ID, Cur_InvoiceSchedule.M_InOutLine_ID, v_LineNo,
adrian@94
  1287
                    Cur_InvoiceSchedule.Line_Description, Cur_InvoiceSchedule.M_Product_ID, Cur_InvoiceSchedule.MovementQty, Cur_InvoiceSchedule.PriceList,
adrian@94
  1288
                    Cur_InvoiceSchedule.PriceActual, Cur_InvoiceSchedule.PriceLimit, C_Currency_Round(Cur_InvoiceSchedule.MovementQty*Cur_InvoiceSchedule.PriceActual, Cur_InvoiceSchedule.C_Currency_ID, NULL), Cur_InvoiceSchedule.Line_C_Charge_ID,
adrian@94
  1289
                    Cur_InvoiceSchedule.Line_ChargeAmt, Cur_InvoiceSchedule.C_UOM_ID,
adrian@94
  1290
                    Cur_InvoiceSchedule.C_Tax_ID,  --MODIFIED BY F.IRIAZABAL
ainhoa@3730
  1291
                    Cur_InvoiceSchedule.QuantityOrder, Cur_InvoiceSchedule.M_Product_Uom_ID, Cur_InvoiceSchedule.PriceStd,
gorkaion@17356
  1292
                    Cur_InvoiceSchedule.m_attributesetinstance_id, Cur_InvoiceSchedule.taxbaseamt,
david@18197
  1293
                    Cur_InvoiceSchedule.gross_unit_price, C_Currency_Round(Cur_InvoiceSchedule.MovementQty*Cur_InvoiceSchedule.gross_unit_price, Cur_InvoiceSchedule.C_Currency_ID, NULL),
eduardo@18445
  1294
                    COALESCE(Cur_InvoiceSchedule.grosspricestd,0), COALESCE(Cur_InvoiceSchedule.grosspricelist,0),
eduardo@18851
  1295
                    v_isdeferred_inv, v_defplantype_inv, v_periodnumber_inv, v_period_inv,
eduardo@18851
  1296
                    coalesce(Cur_InvoiceSchedule.line_c_project_id, Cur_InvoiceSchedule.c_project_id), coalesce(Cur_InvoiceSchedule.line_c_costcenter_id, Cur_InvoiceSchedule.c_costcenter_id), coalesce(Cur_InvoiceSchedule.line_a_asset_id,Cur_InvoiceSchedule.a_asset_id),
pandeeswari@20456
  1297
                    coalesce(Cur_InvoiceSchedule.line_user1_id, Cur_InvoiceSchedule.user1_id), coalesce(Cur_InvoiceSchedule.line_user2_id, Cur_InvoiceSchedule.user2_id), coalesce(Cur_InvoiceSchedule.line_c_bpartner_id, Cur_InvoiceSchedule.c_bpartner_id),
naiara@19828
  1298
                    Cur_InvoiceSchedule.explode, (SELECT c_invoiceline_id 
naiara@19828
  1299
                                            FROM c_invoiceline 
naiara@19828
  1300
                                            WHERE c_invoice_id=p_Invoice_ID 
naiara@19828
  1301
                                               and c_orderline_id = (SELECT BOM_parent_id 
naiara@19828
  1302
                                                                     FROM c_orderline 
naiara@19828
  1303
                                                                     WHERE c_orderline_id = Cur_InvoiceSchedule.c_orderline_id))
adrian@94
  1304
                  )
adrian@94
  1305
                  ;
antonio@735
  1306
                v_DateOrdered:=COALESCE(Cur_InvoiceSchedule.movementDate, COALESCE(Cur_InvoiceSchedule.DateOrdered, now())) ;
adrian@94
  1307
              END IF;--
adrian@94
  1308
            END LOOP; -- Invoice Schedule
adrian@94
  1309
            IF(NOT FINISH_PROCESS) THEN
adrian@94
  1310
              -- Post last invoice, if exists
juanpablo@1605
  1311
              IF(p_Invoice_ID<>'-1') THEN
antonio@735
  1312
                v_DateInvoiced:=COALESCE(v_DateInvoiced_aux, COALESCE(v_DateOrdered, now())) ;
gorkaion@14601
  1313
                Ad_Sequence_Doctype(v_DocType_ID, v_AD_Client_ID, 'Y', v_DocumentNo) ;
gorkaion@14601
  1314
                IF(v_DocumentNo IS NULL) THEN
gorkaion@14601
  1315
                  Ad_Sequence_Doc('DocumentNo_C_Invoice', v_AD_Client_ID, 'Y', v_DocumentNo) ;
gorkaion@14601
  1316
                END IF;
adrian@94
  1317
                UPDATE C_INVOICE
adrian@94
  1318
                  SET DateInvoiced=v_DateInvoiced,
gorkaion@14601
  1319
                  DateAcct=v_DateInvoiced,
gorkaion@14601
  1320
                  Documentno= v_DocumentNo
adrian@94
  1321
                WHERE C_Invoice_ID=p_Invoice_ID;
adrianromero@6803
  1322
                C_INVOICE_POST(NULL, p_Invoice_ID) ;
rakhee@4091
  1323
	            IF(p_Invoice_ID='0') THEN
rakhee@4091
  1324
          	  	  RAISE_APPLICATION_ERROR(-20000, '@InvoiceCreateFailed@');
rakhee@4091
  1325
			    ELSE
rakhee@4091
  1326
			  	  SELECT documentno
rakhee@4091
  1327
	    	  	  INTO v_DocumentNo
rakhee@4091
  1328
	    	  	  FROM C_INVOICE
rakhee@4091
  1329
	    	  	  WHERE C_INVOICE_ID = p_Invoice_ID;
carlos@17554
  1330
	    	  	  IF(v_Message NOT LIKE '%'||v_DocumentNo||'%') THEN
carlos@17554
  1331
	    	  	    IF(v_Message NOT LIKE '%@InvoiceDocumentno@%') THEN
ioritz@19015
  1332
       	    	  	        v_Message:=SUBSTR(v_Message||', '||'@InvoiceDocumentno@ ' || v_DocumentNo, 0, 2000);
carlos@17554
  1333
	    	  	    ELSE
ioritz@19015
  1334
	    	  	        v_Message:=SUBSTR(v_Message||', '|| v_DocumentNo, 0, 2000);
carlos@17554
  1335
	    	  	    END IF;
carlos@17554
  1336
	    	  	  END IF;
rakhee@4091
  1337
        	    END IF;
rakhee@4091
  1338
			  END IF;
adrian@94
  1339
            END IF;--FINISH_PROCESS
adrian@94
  1340
          END; -- Block
adrian@94
  1341
        END IF; -- PInstance not null
gorkaion@239
  1342
        --<<FINISH_PROCESS>>
david@19193
  1343
        v_Message:=SUBSTR('@Created@: ' || v_NoRecords||v_Message || '<br>' ||v_MessageBPB, 0, 2000);
adrian@94
  1344
        IF(p_PInstance_ID IS NOT NULL) THEN
adrian@94
  1345
          --  Update AD_PInstance
adrian@94
  1346
          DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
antonio@735
  1347
          AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 1, v_Message) ;
adrian@94
  1348
        ELSE
gorkaion@239
  1349
          DBMS_OUTPUT.PUT_LINE('--<<C_Invoive_Create finished>> ' || v_Message) ;
adrian@94
  1350
        END IF;
adrian@94
  1351
        RETURN;
adrian@94
  1352
      END; --BODY
adrian@94
  1353
    EXCEPTION
adrian@94
  1354
    WHEN OTHERS THEN
gorkaion@2596
  1355
    DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
adrian@94
  1356
     v_ResultStr:= '@ERROR=' || SQLERRM;
adrian@94
  1357
      DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
adrian@94
  1358
      IF(p_PInstance_ID IS NOT NULL) THEN
adrian@94
  1359
        ROLLBACK;
antonio@735
  1360
        AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
adrian@94
  1361
      ELSE
carlos@1273
  1362
        RAISE;
adrian@94
  1363
      END IF;
adrian@94
  1364
      p_Invoice_ID:=0; -- Error Indicator
adrian@94
  1365
      RETURN;
antonio@735
  1366
END C_INVOICE_CREATE
gorkaion@239
  1367
]]></body>
adrian@94
  1368
    </function>
adrian@94
  1369
  </database>