src-db/database/model/functions/C_ORDER_PO_CREATE.xml
author Asier Lostalé <asier.lostale@openbravo.com>
Mon, 26 May 2008 11:24:39 +0000
changeset 1027 2e9052d7de0e
parent 799 fef2c5e2feb7
child 1605 8a0fe0193bef
permissions -rw-r--r--
fixed bug 0000399: Remove insertions in AD_PInstance_Log table
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION C_ORDER_PO_CREATE">
adrian@94
     3
    <function name="C_ORDER_PO_CREATE" type="NULL">
antonio@735
     4
      <parameter name="p_pinstance_id" type="NUMERIC" mode="in">
antonio@735
     5
        <default/>
antonio@735
     6
      </parameter>
gorkaion@239
     7
      <body><![CDATA[/*************************************************************************
juanpablo@771
     8
  * The contents of this file are subject to the Compiere Public
juanpablo@771
     9
  * License 1.1 ("License"); You may not use this file except in
juanpablo@771
    10
  * compliance with the License. You may obtain a copy of the License in
juanpablo@771
    11
  * the legal folder of your Openbravo installation.
carlos@0
    12
  * Software distributed under the License is distributed on an
carlos@0
    13
  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
carlos@0
    14
  * implied. See the License for the specific language governing rights
carlos@0
    15
  * and limitations under the License.
juanpablo@778
    16
  * The Original Code is  Compiere  ERP &  Business Solution
juanpablo@771
    17
  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
carlos@0
    18
  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
carlos@0
    19
  * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
carlos@0
    20
  * All Rights Reserved.
carlos@0
    21
  * Contributor(s): Openbravo SL
juanpablo@785
    22
  * Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
juanpablo@771
    23
  *
juanpablo@771
    24
  * Specifically, this derivative work is based upon the following Compiere
juanpablo@771
    25
  * file and version.
carlos@0
    26
  *************************************************************************
carlos@0
    27
  * $Id: C_Order_PO_Create.sql,v 1.4 2003/05/26 22:31:16 jjanke Exp $
carlos@0
    28
  ***
carlos@0
    29
  * Title: Create PO from SO
carlos@0
    30
  * Description:
carlos@0
    31
  ************************************************************************/
carlos@0
    32
  -- Logistice
carlos@0
    33
  v_ResultStr VARCHAR2(2000):='';
carlos@0
    34
  v_Message VARCHAR2(2000):='';
carlos@0
    35
  v_Result NUMBER:=1; -- 0=failure
carlos@0
    36
  v_Record_ID NUMBER;
carlos@0
    37
  v_AD_User_ID NUMBER;
carlos@0
    38
  -- Parameter
carlos@0
    39
  TYPE RECORD IS REF CURSOR;
carlos@0
    40
    Cur_Parameter RECORD;
carlos@0
    41
    -- Parameter Variables
carlos@0
    42
    v_DateOrdered_From DATE;
carlos@0
    43
    v_DateOrdered_To DATE;
carlos@0
    44
    v_C_BPartner_ID NUMBER;
carlos@0
    45
    v_Vendor_ID NUMBER;
carlos@0
    46
    v_C_Order_ID NUMBER;
carlos@0
    47
    --
carlos@0
    48
    v_Created NUMBER:=0;
carlos@0
    49
    FINISH_PROCESS BOOLEAN:=false;
asier@799
    50
    v_PInstance_Log_ID NUMBER;
asier@799
    51
    v_Client_ID NUMBER;
asier@799
    52
    
carlos@0
    53
  BEGIN
carlos@0
    54
    --  Update AD_PInstance
carlos@0
    55
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
    56
    v_ResultStr:='PInstanceNotFound';
carlos@0
    57
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
    58
  BEGIN --BODY
carlos@0
    59
    -- Get Parameters
carlos@0
    60
    v_ResultStr:='ReadingParameters';
carlos@0
    61
    FOR Cur_Parameter IN
carlos@0
    62
      (SELECT i.Record_ID,
carlos@0
    63
        i.AD_User_ID,
carlos@0
    64
        p.ParameterName,
carlos@0
    65
        p.P_String,
carlos@0
    66
        p.P_Number,
carlos@0
    67
        p.P_Date,
carlos@0
    68
        p.P_Date_To
carlos@0
    69
      FROM AD_PInstance i
carlos@0
    70
      LEFT JOIN AD_PInstance_Para p
carlos@0
    71
        ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
    72
      WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
    73
      ORDER BY p.SeqNo
carlos@0
    74
      )
carlos@0
    75
    LOOP
carlos@0
    76
      v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
    77
      v_AD_User_ID:=Cur_Parameter.AD_User_ID;
carlos@0
    78
      IF(Cur_Parameter.ParameterName='DateOrdered') THEN
carlos@0
    79
        v_DateOrdered_From:=Cur_Parameter.P_Date;
carlos@0
    80
        v_DateOrdered_To:=Cur_Parameter.P_Date_To;
carlos@0
    81
        DBMS_OUTPUT.PUT_LINE('  DateOrdered=' || v_DateOrdered_From || '-' || v_DateOrdered_To) ;
carlos@0
    82
      ELSIF(Cur_Parameter.ParameterName='C_BPartner_ID') THEN
carlos@0
    83
        v_C_BPartner_ID:=Cur_Parameter.P_Number;
carlos@0
    84
        DBMS_OUTPUT.PUT_LINE('  C_BPartner_ID=' || v_C_BPartner_ID) ;
carlos@0
    85
      ELSIF(Cur_Parameter.ParameterName='Vendor_ID') THEN
carlos@0
    86
        v_Vendor_ID:=Cur_Parameter.P_Number;
carlos@0
    87
        DBMS_OUTPUT.PUT_LINE('  Vendor_ID=' || v_Vendor_ID) ;
carlos@0
    88
      ELSIF(Cur_Parameter.ParameterName='C_Order_ID') THEN
carlos@0
    89
        v_C_Order_ID:=Cur_Parameter.P_Number;
carlos@0
    90
        DBMS_OUTPUT.PUT_LINE('  C_Order_ID=' || v_C_Order_ID) ;
carlos@0
    91
      ELSE
carlos@0
    92
        DBMS_OUTPUT.PUT_LINE('*** Unknown Parameter=' || Cur_Parameter.ParameterName) ;
carlos@0
    93
      END IF;
carlos@0
    94
    END LOOP; -- Get Parameter
carlos@0
    95
    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
carlos@0
    96
    -- Check that we have some restrictions
carlos@0
    97
    v_ResultStr:='CheckRestriction';
carlos@0
    98
    IF(v_DateOrdered_From IS NULL AND v_DateOrdered_To IS NULL  AND v_C_BPartner_ID IS NULL AND v_Vendor_ID IS NULL  AND v_C_Order_ID IS NULL) THEN
carlos@0
    99
      RAISE_APPLICATION_ERROR(-20000, '@NoParametersSelected@');
carlos@0
   100
    END IF;
carlos@0
   101
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   102
      DECLARE
carlos@0
   103
        Cur_SO RECORD;
carlos@0
   104
        --
carlos@0
   105
        Cur_SOL RECORD;
carlos@0
   106
        --
carlos@0
   107
        v_C_PaymentTerm_ID NUMBER:=NULL;
carlos@0
   108
        v_C_DocType_ID NUMBER:=NULL;
carlos@0
   109
        v_M_PriceList_Version_ID NUMBER:=NULL;
carlos@0
   110
        v_C_Tax_ID NUMBER:=NULL;
carlos@0
   111
        --
carlos@0
   112
        v_BillTo_ID NUMBER;
carlos@0
   113
        v_C_BPartner_Location_ID NUMBER;
carlos@0
   114
        --
carlos@0
   115
        v_PO_Order_ID NUMBER;
carlos@0
   116
        v_DocumentNo NVARCHAR2(30) ;
carlos@0
   117
        v_NextID NUMBER;
carlos@0
   118
        v_Lines NUMBER:=0;
carlos@0
   119
        --
carlos@0
   120
      BEGIN
carlos@0
   121
        v_ResultStr:='StartLoop';
carlos@0
   122
        FOR Cur_SO IN
carlos@0
   123
          (SELECT so.AD_Client_ID,
carlos@0
   124
            so.AD_Org_ID,
carlos@0
   125
            so.UpdatedBy,
carlos@0
   126
            so.Description,
carlos@0
   127
            so.SalesRep_ID,
carlos@0
   128
            so.DateOrdered,
carlos@0
   129
            so.DatePromised,
carlos@0
   130
            so.DateAcct,
carlos@0
   131
            so.C_Order_ID,
carlos@0
   132
            so.DocumentNo,
carlos@0
   133
            so.PriorityRule,
carlos@0
   134
            so.M_WareHouse_ID,
carlos@0
   135
            so.C_Campaign_ID,
carlos@0
   136
            so.C_Project_ID,
carlos@0
   137
            so.C_Activity_ID,
carlos@0
   138
            po.C_BPartner_ID,
carlos@0
   139
            v.PO_PaymentTerm_ID,
carlos@0
   140
            v.PaymentRulePO,
carlos@0
   141
            v.POReference,
carlos@0
   142
            pl.M_PriceList_ID,
carlos@0
   143
            pl.C_Currency_ID
carlos@0
   144
          FROM C_ORDER so
carlos@0
   145
          INNER JOIN C_ORDERLINE sol
carlos@0
   146
            ON(so.C_Order_ID=sol.C_Order_ID)
carlos@0
   147
          INNER JOIN M_PRODUCT_PO po
carlos@0
   148
            ON(sol.M_Product_ID=po.M_Product_ID)
carlos@0
   149
          INNER JOIN C_BPARTNER v
carlos@0
   150
            ON(po.C_BPartner_ID=v.C_BPartner_ID)
carlos@0
   151
          INNER JOIN M_PRICELIST pl
carlos@0
   152
            ON(v.PO_PriceList_ID=pl.M_PriceList_ID)
carlos@0
   153
          WHERE po.IsCurrentVendor='Y'  -- Optional Order No
carlos@0
   154
            AND(v_C_Order_ID IS NULL
carlos@0
   155
            OR so.C_Order_ID=v_C_Order_ID) -- Optional Sales BPartner
carlos@0
   156
            AND(v_C_BPartner_ID IS NULL
carlos@0
   157
            OR so.C_BPartner_ID=v_C_BPartner_ID) -- Optional Purchase BPartner
carlos@0
   158
            AND(v_Vendor_ID IS NULL
carlos@0
   159
            OR po.C_BPartner_ID=v_Vendor_ID) -- Optional Date Range
carlos@0
   160
            AND(v_DateOrdered_From IS NULL
carlos@0
   161
            OR so.DateOrdered BETWEEN v_DateOrdered_From AND v_DateOrdered_To) -- Don't create twice
carlos@0
   162
            AND sol.Ref_OrderLine_ID IS NULL
carlos@0
   163
          GROUP BY so.AD_Client_ID,
carlos@0
   164
            so.AD_Org_ID,
carlos@0
   165
            so.UpdatedBy,
carlos@0
   166
            so.Description,
carlos@0
   167
            so.SalesRep_ID,
carlos@0
   168
            so.DateOrdered,
carlos@0
   169
            so.DatePromised,
carlos@0
   170
            so.DateAcct,
carlos@0
   171
            so.C_Order_ID,
carlos@0
   172
            so.DocumentNo,
carlos@0
   173
            so.PriorityRule,
carlos@0
   174
            so.M_WareHouse_ID,
carlos@0
   175
            so.C_Campaign_ID,
carlos@0
   176
            so.C_Project_ID,
carlos@0
   177
            so.C_Activity_ID,
carlos@0
   178
            po.C_BPartner_ID,
carlos@0
   179
            v.PO_PaymentTerm_ID,
carlos@0
   180
            v.PaymentRulePO,
carlos@0
   181
            v.POReference,
carlos@0
   182
            pl.M_PriceList_ID,
carlos@0
   183
            pl.C_Currency_ID
carlos@0
   184
          ORDER BY po.C_BPartner_ID
carlos@0
   185
          )
carlos@0
   186
        LOOP
carlos@0
   187
          -- All Payment Types are the same for the same Client
carlos@0
   188
          IF(v_C_PaymentTerm_ID IS NULL) THEN
carlos@0
   189
            v_ResultStr:='DefaultPaymentTerm';
carlos@0
   190
            SELECT MAX(C_PaymentTerm_ID)
carlos@0
   191
            INTO v_C_PaymentTerm_ID
carlos@0
   192
            FROM C_PAYMENTTERM
carlos@0
   193
            WHERE IsDefault='Y'
carlos@0
   194
              AND AD_Client_ID=Cur_SO.AD_Client_ID;
carlos@0
   195
            IF(v_C_PaymentTerm_ID IS NULL) THEN
carlos@0
   196
              RAISE_APPLICATION_ERROR(-20000, '@DefaultPaymenttermNotdefined@');
carlos@0
   197
            END IF;
carlos@0
   198
          END IF;
carlos@0
   199
          -- All Document Types are the same for the same Client
carlos@0
   200
          IF(v_C_DocType_ID IS NULL) THEN
carlos@0
   201
            v_ResultStr:='DocType';
carlos@0
   202
            v_C_DocType_ID:=Ad_Get_DocType(Cur_SO.AD_Client_ID, Cur_SO.AD_Org_ID, 'POO') ;
carlos@0
   203
            IF(v_C_DocType_ID IS NULL) THEN
carlos@0
   204
              RAISE_APPLICATION_ERROR(-20000, '@PODocumenttypenotfound@');
carlos@0
   205
            END IF;
carlos@0
   206
          END IF;
carlos@0
   207
          -- Tax Default for Client
carlos@0
   208
          IF(v_C_Tax_ID IS NULL) THEN
carlos@0
   209
            v_ResultStr:='DefaultTax';
carlos@0
   210
            SELECT MAX(C_Tax_ID)
carlos@0
   211
            INTO v_C_Tax_ID
carlos@0
   212
            FROM C_TAX
carlos@0
   213
            WHERE IsDefault='Y'
carlos@0
   214
              AND AD_Client_ID=Cur_SO.AD_Client_ID;
carlos@0
   215
            IF(v_C_Tax_ID IS NULL) THEN
carlos@0
   216
             RAISE_APPLICATION_ERROR(-20000, '@TaxNotFound@');
carlos@0
   217
            END IF;
carlos@0
   218
          END IF;
carlos@0
   219
          v_ResultStr:='BPLocation_B';
carlos@0
   220
          SELECT MAX(C_BPartner_Location_ID)
carlos@0
   221
          INTO v_BillTo_ID
carlos@0
   222
          FROM C_BPARTNER_LOCATION
carlos@0
   223
          WHERE IsBillTo='Y'
carlos@0
   224
            AND C_BPartner_ID=Cur_SO.C_BPartner_ID;
carlos@0
   225
          v_ResultStr:='BPLocation_S';
carlos@0
   226
          SELECT MAX(C_BPartner_Location_ID)
carlos@0
   227
          INTO v_C_BPartner_Location_ID
carlos@0
   228
          FROM C_BPARTNER_LOCATION
carlos@0
   229
          WHERE IsShipTo='Y'
carlos@0
   230
            AND C_BPartner_ID=Cur_SO.C_BPartner_ID;
carlos@0
   231
          -- What PO PriceListVersion to use (first record only)
carlos@0
   232
          v_ResultStr:='PriceListVersion';
carlos@0
   233
          DECLARE
carlos@0
   234
            Cur_PLV RECORD;
carlos@0
   235
          BEGIN
carlos@0
   236
            FOR Cur_PLV IN
carlos@0
   237
              (SELECT plv.M_PriceList_Version_ID
carlos@0
   238
              FROM C_ORDERLINE sol
carlos@0
   239
              INNER JOIN M_PRODUCT_PO po
carlos@0
   240
                ON(sol.M_Product_ID=po.M_Product_ID)
carlos@0
   241
              INNER JOIN C_BPARTNER v
carlos@0
   242
                ON(po.C_BPartner_ID=v.C_BPartner_ID)
carlos@0
   243
              INNER JOIN M_PRICELIST_VERSION plv
carlos@0
   244
                ON(v.PO_PriceList_ID=plv.M_PriceList_ID
gorkaion@239
   245
                AND plv.ValidFrom<sol.DateOrdered)
carlos@0
   246
              WHERE sol.C_Order_ID=Cur_SO.C_Order_ID
carlos@0
   247
                AND v.C_BPartner_ID=Cur_SO.C_BPartner_ID
carlos@0
   248
              ORDER BY plv.ValidFrom DESC
carlos@0
   249
              )
carlos@0
   250
            LOOP
carlos@0
   251
              IF(v_M_PriceList_Version_ID IS NULL) THEN
carlos@0
   252
                v_M_PriceList_Version_ID:=Cur_PLV.M_PriceList_Version_ID;
carlos@0
   253
              END IF;
carlos@0
   254
            END LOOP;
carlos@0
   255
          END;
carlos@0
   256
          IF(v_M_PriceList_Version_ID IS NULL) THEN
carlos@0
   257
            v_Message:='No valid Price List Version for Order ' || Cur_SO.DocumentNo;
carlos@0
   258
            v_Result:=0;
carlos@0
   259
            FINISH_PROCESS:=true;
carlos@0
   260
            EXIT;
carlos@0
   261
          END IF;
carlos@0
   262
          --
carlos@0
   263
          v_ResultStr:='ID/DocNo';
carlos@0
   264
          Ad_Sequence_Next('C_Order', Cur_SO.AD_Client_ID, v_PO_Order_ID) ;
carlos@0
   265
          Ad_Sequence_Doctype(v_C_DocType_ID, Cur_SO.AD_Client_ID, 'Y', v_DocumentNo) ;
carlos@0
   266
          IF(v_DocumentNo IS NULL) THEN
carlos@0
   267
            Ad_Sequence_Doc('DocumentNo_C_Order', Cur_SO.AD_Client_ID, 'Y', v_DocumentNo) ;
carlos@0
   268
          END IF;
carlos@0
   269
          --
carlos@0
   270
          v_ResultStr:='InsertPOrder';
carlos@0
   271
          INSERT
carlos@0
   272
          INTO C_ORDER
carlos@0
   273
            (
carlos@0
   274
              AD_Client_ID, AD_Org_ID, IsActive, Created,
carlos@0
   275
              CreatedBy, Updated, UpdatedBy, C_Order_ID,
carlos@0
   276
              IsSOTrx, DocumentNo, DocStatus, DocAction,
carlos@0
   277
              Processing, Processed, C_DocType_ID, C_DocTypeTarget_ID,
asier@799
   278
              Description,
asier@799
   279
              IsDelivered, IsInvoiced, IsPrinted,
carlos@0
   280
              IsSelected, SalesRep_ID, DateOrdered, DatePromised,
carlos@0
   281
              DatePrinted, DateAcct, C_BPartner_ID, BillTo_ID,
carlos@0
   282
              C_BPartner_Location_ID, AD_User_ID, POReference, IsDiscountPrinted,
carlos@0
   283
              C_Currency_ID, PaymentRule, C_PaymentTerm_ID, InvoiceRule,
carlos@0
   284
              DeliveryRule, FreightCostRule, FreightAmt, DeliveryViaRule,
carlos@0
   285
              M_Shipper_ID, C_Charge_ID, ChargeAmt, PriorityRule,
carlos@0
   286
              TotalLines, GrandTotal,
carlos@0
   287
              M_WareHouse_ID, M_PriceList_ID, IsTaxIncluded,
carlos@0
   288
              C_Campaign_ID, C_Project_ID, C_Activity_ID, Posted
carlos@0
   289
            )
carlos@0
   290
            VALUES
carlos@0
   291
            (
carlos@0
   292
              Cur_SO.AD_Client_ID, Cur_SO.AD_Org_ID, 'Y', now(),
carlos@0
   293
              Cur_SO.UpdatedBy, now(), Cur_SO.UpdatedBy, v_PO_Order_ID,
carlos@0
   294
               'N', v_DocumentNo, 'DR', 'CO',
carlos@0
   295
               'N', 'N', 0, v_C_DocType_ID,
asier@799
   296
              Cur_SO.DocumentNo || ': ' || Cur_SO.Description,
asier@799
   297
              'N', 'N', 'N',
carlos@0
   298
               'N', Cur_SO.SalesRep_ID, Cur_SO.DateOrdered, Cur_SO.DatePromised,
carlos@0
   299
              NULL, Cur_SO.DateAcct, Cur_SO.C_BPartner_ID, v_BillTo_ID,
carlos@0
   300
              v_C_BPartner_Location_ID, NULL, Cur_SO.POReference, 'Y',
carlos@0
   301
              Cur_SO.C_Currency_ID, COALESCE(Cur_SO.PaymentRulePO, 'P'), COALESCE(Cur_SO.PO_PaymentTerm_ID, v_C_PaymentTerm_ID), 'D',
carlos@0
   302
              'A', 'I', 0, 'P',
carlos@0
   303
              NULL, NULL, 0, Cur_SO.PriorityRule,
carlos@0
   304
              0, 0,
carlos@0
   305
              -- M_WareHouse_ID, M_PriceList_ID, IsTaxIncluded,
carlos@0
   306
              Cur_SO.M_WareHouse_ID, Cur_SO.M_PriceList_ID, 'N',
carlos@0
   307
              -- C_Campaign_ID, C_Project_ID, C_Activity_ID, Posted, C_Payment_ID, C_CashLine_ID)
carlos@0
   308
              Cur_SO.C_Campaign_ID, Cur_SO.C_Project_ID, Cur_SO.C_Activity_ID, 'N'
carlos@0
   309
            )
carlos@0
   310
            ;
carlos@0
   311
          v_ResultStr:='InsertPOrderLine';
carlos@0
   312
          v_Lines:=0; -- should ne one line ony.
carlos@0
   313
          FOR Cur_SOL IN
carlos@0
   314
            (SELECT sol.AD_Client_ID,
carlos@0
   315
              sol.AD_Org_ID,
carlos@0
   316
              sol.C_OrderLine_ID,
carlos@0
   317
              sol.Line,
carlos@0
   318
              sol.DateOrdered,
carlos@0
   319
              sol.DatePromised,
carlos@0
   320
              sol.M_Product_ID,
carlos@0
   321
              sol.C_UOM_ID,
carlos@0
   322
              sol.QtyOrdered,
carlos@0
   323
              sol.M_Warehouse_ID,
carlos@0
   324
              sol.DirectShip,
carlos@0
   325
              sol.Description,
carlos@0
   326
              sol.m_attributesetinstance_id,
carlos@0
   327
              pp.PriceList,
carlos@0
   328
              pp.PriceStd,
carlos@0
   329
              pp.PriceLimit  --MODIFIED BY F.IRIAZABAL
carlos@0
   330
              ,
carlos@0
   331
              sol.QUANTITYORDER,
carlos@0
   332
              sol.M_PRODUCT_UOM_ID
carlos@0
   333
            FROM C_ORDERLINE sol
carlos@0
   334
            INNER JOIN C_ORDER so
carlos@0
   335
              ON(sol.C_Order_ID=so.C_Order_ID)
carlos@0
   336
            INNER JOIN M_PRODUCT_PO po
carlos@0
   337
              ON(sol.M_Product_ID=po.M_Product_ID)
carlos@0
   338
            INNER JOIN C_BPARTNER v
carlos@0
   339
              ON(po.C_BPartner_ID=v.C_BPartner_ID)
carlos@0
   340
            INNER JOIN M_PRICELIST pl
carlos@0
   341
              ON(v.PO_PriceList_ID=pl.M_PriceList_ID)
carlos@0
   342
            INNER JOIN M_PRODUCTPRICE pp
carlos@0
   343
              ON(sol.M_Product_ID=pp.M_Product_ID
carlos@0
   344
              AND M_PriceList_Version_ID=v_M_PriceList_Version_ID) -- #2
carlos@0
   345
            WHERE sol.C_Order_ID=Cur_SO.C_Order_ID -- #1
carlos@0
   346
              AND po.IsCurrentVendor='Y'  -- Optional Order No
carlos@0
   347
              AND(v_C_Order_ID IS NULL
carlos@0
   348
              OR so.C_Order_ID=v_C_Order_ID) -- Optional Sales BPartner
carlos@0
   349
              AND(v_C_BPartner_ID IS NULL
carlos@0
   350
              OR so.C_BPartner_ID=v_C_BPartner_ID) -- Optional Purchase BPartner
carlos@0
   351
              AND(v_Vendor_ID IS NULL
carlos@0
   352
              OR po.C_BPartner_ID=v_Vendor_ID) -- Optional Date Range
carlos@0
   353
              AND(v_DateOrdered_From IS NULL
carlos@0
   354
              OR so.DateOrdered BETWEEN v_DateOrdered_From AND v_DateOrdered_To) -- Don't create twice
carlos@0
   355
              AND po.C_BPartner_ID=Cur_SO.C_BPartner_ID
carlos@0
   356
              AND sol.Ref_OrderLine_ID IS NULL
carlos@0
   357
            ORDER BY sol.Line
carlos@0
   358
            )
carlos@0
   359
          LOOP
carlos@0
   360
            Ad_Sequence_Next('C_OrderLine', Cur_SO.AD_Client_ID, v_NextID) ;
carlos@0
   361
            INSERT
carlos@0
   362
            INTO C_ORDERLINE
carlos@0
   363
              (
carlos@0
   364
                AD_Client_ID, AD_Org_ID, IsActive, Created,
carlos@0
   365
                CreatedBy, Updated, UpdatedBy, C_Order_ID,
carlos@0
   366
                C_OrderLine_ID, Line, C_BPartner_ID, C_BPartner_Location_ID,
carlos@0
   367
                DateOrdered, DatePromised, DateDelivered, DateInvoiced,
carlos@0
   368
                Description, M_Product_ID, M_AttributeSetInstance_ID, M_WareHouse_ID,
carlos@0
   369
                DirectShip, C_UOM_ID, QtyOrdered, QtyReserved,
carlos@0
   370
                QtyDelivered, QtyInvoiced, M_Shipper_ID, C_Currency_ID,
carlos@0
   371
                PriceList, PriceActual, PriceLimit, LineNetAmt,
carlos@0
   372
                Discount, FreightAmt, C_Charge_ID, ChargeAmt,
asier@799
   373
                C_Tax_ID, S_ResourceAssignment_ID,
carlos@0
   374
                Ref_OrderLine_ID,  --MODIFIED BY F.IRIAZABAL
carlos@0
   375
                QuantityOrder, M_Product_UOM_ID, PriceStd, M_Offer_ID
carlos@0
   376
              )
carlos@0
   377
              VALUES
carlos@0
   378
              (
carlos@0
   379
                Cur_SOL.AD_Client_ID, Cur_SOL.AD_Org_ID, 'Y', now(),
carlos@0
   380
                Cur_SO.UpdatedBy, now(), Cur_SO.UpdatedBy, v_PO_Order_ID,
carlos@0
   381
                v_NextID, Cur_SOL.Line, Cur_SO.C_BPartner_ID, v_C_BPartner_Location_ID,
carlos@0
   382
                Cur_SOL.DateOrdered, Cur_SOL.DatePromised, NULL, NULL,
carlos@0
   383
                Cur_SOL.Description, Cur_SOL.M_Product_ID, Cur_SOL.M_AttributeSetInstance_ID, Cur_SOL.M_WareHouse_ID,
carlos@0
   384
                Cur_SOL.DirectShip, Cur_SOL.C_UOM_ID, Cur_SOL.QtyOrdered, 0,
carlos@0
   385
                0, 0, NULL, Cur_SO.C_Currency_ID,
carlos@0
   386
                Cur_SOL.PriceList, Cur_SOL.PriceStd, Cur_SOL.PriceLimit, ROUND(Cur_SOL.QtyOrdered*Cur_SOL.PriceStd, 2),
carlos@0
   387
                NULL, 0, NULL, 0,
asier@799
   388
                v_C_Tax_ID, NULL,
carlos@0
   389
                Cur_SOL.C_OrderLine_ID,  --MODIFIED BY F.IRIAZABAL
carlos@0
   390
                Cur_SOL.QUANTITYORDER, Cur_SOL.M_PRODUCT_UOM_ID, Cur_SOL.PriceStd, NULL
carlos@0
   391
              )
carlos@0
   392
              ;
carlos@0
   393
            --
carlos@0
   394
            UPDATE C_ORDERLINE
carlos@0
   395
              SET Ref_OrderLine_ID=v_NextID
carlos@0
   396
            WHERE C_OrderLine_ID=Cur_SOL.C_OrderLine_ID;
carlos@0
   397
            --
carlos@0
   398
            v_Lines:=v_Lines + 1;
carlos@0
   399
          END LOOP; -- For all SO Lines
asier@1027
   400
          
carlos@0
   401
          v_Created:=v_Created + 1;
carlos@0
   402
        END LOOP; -- For all SO
carlos@0
   403
      END;
carlos@0
   404
    END IF;--FINISH_PROCESS
carlos@0
   405
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   406
      v_Message:='@Created@=' || v_Created;
carlos@0
   407
    END IF; --FINISH_PROCESS
gorkaion@239
   408
    --<<FINISH_PROCESS>>
carlos@0
   409
    --  Update AD_PInstance
carlos@0
   410
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
carlos@0
   411
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
carlos@0
   412
    RETURN;
carlos@0
   413
  END; --BODY
carlos@0
   414
EXCEPTION
carlos@0
   415
WHEN OTHERS THEN
carlos@0
   416
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   417
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   418
  ROLLBACK;
carlos@0
   419
  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   420
  RETURN;
adrian@94
   421
END C_ORDER_PO_CREATE
gorkaion@239
   422
]]></body>
adrian@94
   423
    </function>
adrian@94
   424
  </database>