src-db/database/model/functions/C_ORDER_POST1.xml
author Unai Martirena <unai.martirena@openbravo.com>
Mon, 18 Apr 2016 18:51:42 +0200
changeset 29855 956cd99a1937
parent 28978 c348f263a0a4
child 29420 78f3b49e0167
child 29566 04d238173afa
permissions -rw-r--r--
Fixes issue 32697: Applied Product Services Code Review
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION C_ORDER_POST1">
adrian@94
     3
    <function name="C_ORDER_POST1" 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_order_id" type="VARCHAR" mode="in">
antonio@735
     8
        <default/>
antonio@735
     9
      </parameter>
miguel@23885
    10
      <parameter name="p_recalculatediscounts" type="VARCHAR" mode="in">
miguel@23885
    11
        <default><![CDATA[Y]]></default>
miguel@23885
    12
      </parameter>
gorkaion@239
    13
      <body><![CDATA[/*************************************************************************
carlos@0
    14
* The contents of this file are subject to the Openbravo  Public  License
priya@9072
    15
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
carlos@0
    16
* Version 1.1  with a permitted attribution clause; you may not  use this
carlos@0
    17
* file except in compliance with the License. You  may  obtain  a copy of
carlos@0
    18
* the License at http://www.openbravo.com/legal/license.html
carlos@0
    19
* Software distributed under the License  is  distributed  on  an "AS IS"
carlos@0
    20
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
carlos@0
    21
* License for the specific  language  governing  rights  and  limitations
carlos@0
    22
* under the License.
carlos@0
    23
* The Original Code is Openbravo ERP.
ggi@6701
    24
* The Initial Developer of the Original Code is Openbravo SLU
m@28978
    25
* All portions are Copyright (C) 2001-2016 Openbravo SLU
carlos@0
    26
* All Rights Reserved.
carlos@0
    27
* Contributor(s):  ______________________________________.
carlos@0
    28
************************************************************************/
carlos@0
    29
  -- Logistics
carlos@0
    30
  v_ResultStr VARCHAR2(2000):='';
carlos@0
    31
  v_Message VARCHAR2(2000):='';
juanpablo@1605
    32
  v_Record_ID VARCHAR2(32);
juanpablo@1605
    33
  v_User VARCHAR2(32);
carlos@0
    34
  v_IsProcessing CHAR(1) ;
asier@2586
    35
  v_IsProcessed VARCHAR(60) ;
carlos@0
    36
  v_Result NUMBER:=1; -- Success
asier@1929
    37
  v_is_included NUMBER:=0;
asier@1929
    38
  v_is_ready AD_Org.IsReady%TYPE;
asier@1929
    39
  v_is_tr_allow AD_OrgType.IsTransactionsAllowed%TYPE;
carlos@0
    40
  -- Parameter
carlos@0
    41
  TYPE RECORD IS REF CURSOR;
carlos@0
    42
    Cur_Parameter RECORD;
pandeeswari@18817
    43
    Cur_line RECORD;
harikrishnan@5813
    44
    Cur_Order RECORD;
gorkaion@14379
    45
  -- Record Info
gorkaion@14379
    46
  v_Client_ID VARCHAR2(32);
gorkaion@14379
    47
  v_Org_ID VARCHAR2(32);
pandeeswari@21373
    48
  v_Warehouse_Org VARCHAR2(32);
gorkaion@14379
    49
  v_Org_Name VARCHAR2(60);
gorkaion@14379
    50
  v_UpdatedBy VARCHAR2(32);
gorkaion@14379
    51
  v_DocAction VARCHAR(60) ;
gorkaion@14379
    52
  v_DocStatus VARCHAR(60) ;
gorkaion@14379
    53
  v_InvoiceRule VARCHAR(60) ;
gorkaion@14379
    54
  v_M_Warehouse_ID VARCHAR2(32);
gorkaion@14379
    55
  v_DocType_ID VARCHAR2(32);
gorkaion@14379
    56
  v_DocTypeTarget_ID VARCHAR2(32);
gorkaion@14379
    57
  v_DocSubTypeSO VARCHAR(60) ;
gorkaion@14379
    58
  v_DocSubTypeSOTarget VARCHAR(60) ;
gorkaion@14379
    59
  v_DocBaseTypeTarget VARCHAR(60);
gorkaion@14545
    60
  v_IsReturnDocType CHAR(1);
gorkaion@14379
    61
  v_IsBinding CHAR(1):='Y';
gorkaion@17342
    62
  v_istaxincluded CHAR(1);
gorkaion@14379
    63
  --
victor@24222
    64
  ToDeliver NUMBER:=0;
victor@24222
    65
  ToInvoice NUMBER:=0;
victor@24222
    66
  ToDeliverOrToInvoice NUMBER:=0;
gorkaion@14379
    67
  --
gorkaion@14379
    68
  InOut_ID VARCHAR2(32);
gorkaion@14379
    69
  Invoice_ID VARCHAR2(32);
gorkaion@14379
    70
  --Added by P.SAROBE
gorkaion@14379
    71
  v_documentno_Settlement VARCHAR2(40);
gorkaion@14379
    72
  v_dateSettlement DATE;
gorkaion@14379
    73
  v_Cancel_Processed VARCHAR(60);
gorkaion@14379
    74
  v_nameBankstatement VARCHAR2 (60);
gorkaion@14379
    75
  v_dateBankstatement DATE;
gorkaion@14379
    76
  v_nameCash VARCHAR2 (60);
gorkaion@14379
    77
  v_dateCash DATE;
gorkaion@14379
    78
  v_Bankstatementline_ID VARCHAR2(32);
gorkaion@14379
    79
  --Finish added by P.Sarobe
gorkaion@14379
    80
  v_AuxCashLine_ID VARCHAR2(32);
gorkaion@14379
    81
  v_CashLine_ID VARCHAR2(32);
gorkaion@14379
    82
  v_ispaid CHAR(1);
gorkaion@14379
    83
  v_Settlement_Cancel_ID VARCHAR2(32);
gorkaion@14379
    84
  v_Cash_ID VARCHAR2(32):=NULL;
gorkaion@14379
    85
  v_Line NUMBER:=0;
gorkaion@14379
    86
  v_CashBook_ID VARCHAR2(32):=NULL;
gorkaion@14379
    87
  v_Debtpayment_ID VARCHAR2(32);
gorkaion@14379
    88
  v_ISO_Code VARCHAR(10) ;
gorkaion@14379
    89
  v_DocumentNo VARCHAR2(200) ;
gorkaion@14379
    90
  v_GrandTotal NUMBER;
gorkaion@14379
    91
  v_Multiplier NUMBER:=1;
gorkaion@14379
    92
  v_Date DATE;
gorkaion@14379
    93
  v_WarehouseName VARCHAR2(60) ;
gorkaion@14379
    94
  v_CashProcessed NUMBER;
gorkaion@14379
    95
  v_count NUMBER;
gorkaion@14379
    96
  v_isSoTrx CHAR(1) ;
gorkaion@14379
    97
  v_Aux NUMBER;
gorkaion@14379
    98
  v_c_Bpartner_ID VARCHAR2(32);
gorkaion@14379
    99
  v_c_currency_ID VARCHAR2(32);
gorkaion@14379
   100
  v_C_PROJECT_ID VARCHAR2(32);
gorkaion@14379
   101
  v_PriceList_ID VARCHAR2(32);
gorkaion@14379
   102
  FINISH_PROCESS BOOLEAN:=FALSE;
gorkaion@14379
   103
  END_PROCESSING BOOLEAN:=FALSE;
gorkaion@14379
   104
  v_CBPartner_ID VARCHAR2(32);
gorkaion@14379
   105
  rowcount NUMBER;
gorkaion@14379
   106
gorkaion@14379
   107
  v_CumDiscount NUMBER;
gorkaion@14379
   108
  v_OldCumDiscount NUMBER;
gorkaion@14379
   109
  v_OrderLineSeqNo NUMBER;
gorkaion@14379
   110
  Cur_COrderDiscount RECORD;
gorkaion@14379
   111
  Cur_TaxDiscount RECORD;
gorkaion@14379
   112
  v_OrderLine VARCHAR2(32);
gorkaion@14379
   113
  v_Discount NUMBER;
gorkaion@14379
   114
  v_precision NUMBER;
gorkaion@14379
   115
  Cur_OrderLine RECORD;
gorkaion@14379
   116
  v_DiscountExist NUMBER;
mikel@17452
   117
  v_gross_unit_price NUMBER;
mikel@17452
   118
  v_line_gross_amount NUMBER;
pandeeswari@18817
   119
  v_acctAmount NUMBER;
david@18487
   120
  v_reject_reason VARCHAR(32);
gorkaion@18749
   121
  v_dummy VARCHAR2(2000);
david@19193
   122
  v_bpartner_blocked VARCHAR2(1):='N';
david@19193
   123
  v_orderBlocking VARCHAR2(1):='N';
sandra@20001
   124
  v_bpartner_name c_bpartner.name%TYPE;
gorkaion@20940
   125
  v_productname m_product.name%TYPE;
david@19193
   126
victor@21513
   127
  v_iscashvat C_Order.IsCashVat%TYPE;
miguel@23885
   128
  v_recalculateDiscounts VARCHAR(1):='Y';
atul@24147
   129
  v_isactive VARCHAR2(1):='N';
victor@21513
   130
carlos@0
   131
  BEGIN
gorkaion@14379
   132
    IF (p_PInstance_ID IS NOT NULL) THEN
carlos@0
   133
      --  Update AD_PInstance
carlos@0
   134
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
   135
      v_ResultStr:='PInstanceNotFound';
antonio@735
   136
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
   137
      -- Get Parameters
carlos@0
   138
      v_ResultStr:='ReadingParameters';
carlos@0
   139
      FOR Cur_Parameter IN
gorkaion@14379
   140
        (SELECT i.Record_ID, i.AD_User_ID, p.ParameterName, p.P_String,
gorkaion@14379
   141
          p.P_Number, p.P_Date
carlos@0
   142
        FROM AD_PINSTANCE i
carlos@0
   143
        LEFT JOIN AD_PINSTANCE_PARA p
carlos@0
   144
          ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
   145
        WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
   146
        ORDER BY p.SeqNo
carlos@0
   147
        )
carlos@0
   148
      LOOP
carlos@0
   149
        v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
   150
        v_User:=Cur_Parameter.AD_User_ID;
carlos@0
   151
      END LOOP; -- Get Parameter
carlos@0
   152
    ELSE
carlos@0
   153
      v_Record_ID:=p_Order_ID;
miguel@23885
   154
      v_recalculateDiscounts := p_recalculateDiscounts;
carlos@0
   155
      SELECT CREATEDBY INTO v_User  FROM C_ORDER  WHERE C_ORDER_ID=p_Order_ID;
carlos@0
   156
    END IF;
carlos@0
   157
    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
carlos@0
   158
  BEGIN --BODY
pandeeswari@18817
   159
  
pandeeswari@18817
   160
  /*Orderline acct dimension*/
eduardo@27017
   161
  SELECT C_ORDER.ISSOTRX , C_ORDER.c_bpartner_id, DocAction
eduardo@27017
   162
  INTO v_IsSOTrx, v_c_Bpartner_ID, v_DocAction
pandeeswari@18817
   163
  FROM C_ORDER
pandeeswari@18817
   164
  WHERE C_ORDER_ID = v_Record_ID;
david@19193
   165
  
pandeeswari@18817
   166
  IF (v_IsSOTrx = 'N') THEN
pandeeswari@18817
   167
    FOR Cur_line IN
pandeeswari@18817
   168
      (SELECT C_ORDERLINE.C_OrderLine_ID,
pandeeswari@18817
   169
       C_ORDERLINE.LinenetAmt
pandeeswari@18817
   170
       FROM C_ORDERLINE
pandeeswari@18817
   171
       WHERE C_Order_ID = v_Record_ID
pandeeswari@18817
   172
      )
pandeeswari@18817
   173
    LOOP
pandeeswari@18817
   174
      SELECT SUM(Amt) INTO v_acctAmount
pandeeswari@18817
   175
      FROM C_ORDERLINE_ACCTDIMENSION
pandeeswari@18817
   176
      WHERE C_OrderLine_ID = Cur_line.C_OrderLine_ID;
pandeeswari@18817
   177
      IF (v_acctAmount <> Cur_line.LinenetAmt) THEN
pandeeswari@18817
   178
        v_Message:='@QuantitiesNotMatch@';
pandeeswari@18817
   179
        RAISE_APPLICATION_ERROR(-20000, '@QuantitiesNotMatch@') ;
pandeeswari@18817
   180
      END IF;
pandeeswari@18817
   181
    END LOOP;
eduardo@27017
   182
  ELSE
eduardo@27017
   183
     IF (v_DocAction IN ('CO', 'PR')) THEN
unai@29855
   184
       SELECT COUNT(1) 
unai@29855
   185
         INTO v_Count
unai@29855
   186
       FROM DUAL 
unai@29855
   187
       WHERE EXISTS(SELECT 1
unai@29855
   188
		      FROM C_ORDERLINE, M_PRODUCT
unai@29855
   189
		      WHERE C_ORDERLINE.M_PRODUCT_ID = M_PRODUCT.M_PRODUCT_ID 
unai@29855
   190
		      AND ISLINKEDTOPRODUCT = 'Y'
unai@29855
   191
		      AND C_ORDER_ID = v_Record_ID
unai@29855
   192
		      AND NOT EXISTS (SELECT 1 FROM C_ORDERLINE_SERVICERELATION WHERE C_ORDERLINE_SERVICERELATION.C_ORDERLINE_ID = C_ORDERLINE.C_ORDERLINE_ID));
unai@29855
   193
       IF (v_Count <> 0) THEN
eduardo@27017
   194
         v_Message:='@MissingServiceRelation@';
eduardo@27017
   195
         RAISE_APPLICATION_ERROR(-20000, '@MissingServiceRelation@') ;
eduardo@27017
   196
       END IF;
eduardo@27017
   197
     END IF;
pandeeswari@18817
   198
  END IF;
atul@24147
   199
atul@24147
   200
  /* Check active business partner*/
atul@24147
   201
  SELECT isactive INTO v_isactive
atul@24147
   202
  FROM C_Bpartner
atul@24147
   203
  WHERE C_Bpartner_ID = v_C_Bpartner_ID;
atul@24147
   204
atul@24147
   205
  IF(v_isactive = 'N') THEN
atul@24147
   206
    RAISE_APPLICATION_ERROR(-20000, '@InActiveBusinessPartner@');
atul@24147
   207
  END IF;
pandeeswari@18817
   208
  
carlos@0
   209
    /**
carlos@0
   210
    * Read Order
carlos@0
   211
    */
carlos@0
   212
    v_ResultStr:='ReadingOrder';
carlos@0
   213
    SELECT Processing, Processed, DocAction, DocStatus,
gorkaion@17342
   214
      C_DocType_ID, C_DocTypeTarget_ID, c_order.AD_Client_ID,
gorkaion@17342
   215
      c_order.AD_Org_ID, c_order.UpdatedBy, M_Warehouse_ID, TRUNC(DateOrdered),
gorkaion@17342
   216
      Issotrx, c_Bpartner_Id, c_order.c_currency_id, C_PROJECT_ID,
sandra@25786
   217
      C_BPartner_ID, M_PriceList_ID, invoicerule, c_order.IsCashVat
gorkaion@14389
   218
    INTO v_IsProcessing, v_IsProcessed, v_DocAction, v_DocStatus,
asier@799
   219
      v_DocType_ID, v_DocTypeTarget_ID, v_Client_ID,
carlos@0
   220
      v_Org_ID, v_UpdatedBy, v_M_Warehouse_ID, v_Date,
carlos@379
   221
      v_isSoTrx, v_c_Bpartner_Id, v_c_currency_id, v_C_PROJECT_ID,
sandra@25786
   222
      v_CBPartner_ID, v_PriceList_ID, v_invoicerule, v_iscashvat
sandra@25786
   223
    FROM C_ORDER
sandra@25786
   224
    WHERE C_Order_ID=v_Record_ID  FOR UPDATE  ;
sandra@25786
   225
sandra@25786
   226
    SELECT m_pricelist.istaxincluded
sandra@25786
   227
    INTO v_istaxincluded
sandra@25786
   228
    FROM m_pricelist 
sandra@25786
   229
    WHERE m_pricelist_id= v_PriceList_ID;
sandra@25786
   230
    
carlos@0
   231
    -- Get current DocSubTypeSO
carlos@0
   232
    SELECT DocSubTypeSO
gorkaion@14545
   233
      INTO v_DocSubTypeSO
carlos@0
   234
    FROM C_DOCTYPE
carlos@0
   235
    WHERE C_DocType_ID=v_DocType_ID;
gorkaion@14545
   236
    SELECT DocSubTypeSO, isreturn
gorkaion@14545
   237
      INTO v_DocSubTypeSOTarget, v_isreturndoctype
gorkaion@14545
   238
    FROM C_DOCTYPE
gorkaion@14545
   239
    WHERE C_DocType_ID=v_DocTypetarget_ID;
david@19193
   240
atul@25058
   241
  IF(v_isreturndoctype='Y' AND v_DocAction='CO') THEN
atul@25058
   242
    DECLARE
atul@25058
   243
    v_RefReturnQty NUMBER:=0;
atul@25058
   244
    v_RefMovementQty NUMBER:=0;
atul@25058
   245
    BEGIN
atul@25058
   246
      v_Message:=NULL;
atul@25058
   247
      FOR Cur_line IN
atul@25058
   248
        (SELECT OL.M_INOUTLINE_ID, OL.LINE, OL.QTYORDERED
atul@25058
   249
        FROM C_ORDERLINE OL
atul@25058
   250
        WHERE OL.C_Order_ID = v_Record_ID
atul@25058
   251
        AND OL.M_INOUTLINE_ID IS NOT NULL
atul@25058
   252
        )
atul@25058
   253
      LOOP
atul@25058
   254
        SELECT COALESCE(SUM(OL.QtyOrdered),0) INTO v_RefReturnQty
atul@25058
   255
        FROM C_ORDERLINE OL, C_ORDER O
atul@25058
   256
        WHERE OL.M_INOUTLINE_ID = Cur_line.M_INOUTLINE_ID
atul@25058
   257
        AND OL.C_ORDER_ID = O.C_ORDER_ID
atul@25058
   258
        AND O.PROCESSED='Y';
atul@25058
   259
        SELECT MovementQty INTO v_RefMovementQty
atul@25058
   260
        FROM M_INOUTLINE WHERE M_INOUTLINE_ID = Cur_line.M_INOUTLINE_ID;
atul@25058
   261
        v_RefReturnQty:=ABS(v_RefReturnQty) + ABS(Cur_Line.QtyOrdered);
atul@25058
   262
        IF(v_RefReturnQty > v_RefMovementQty) THEN
atul@25058
   263
          IF(v_Message IS NULL) THEN
atul@25058
   264
            v_Message:=Cur_line.LINE;
atul@25058
   265
          ELSE
atul@25058
   266
            v_Message:=v_Message||', '||Cur_line.LINE;
atul@25058
   267
          END IF;
atul@25058
   268
        END IF;
atul@25058
   269
      END LOOP;
atul@25058
   270
      IF(v_Message IS NOT NULL) THEN
atul@25058
   271
        RAISE_APPLICATION_ERROR(-20000,'@ReturnQtyMismatch@'||v_Message);
atul@25058
   272
      END IF;
atul@25058
   273
    END;
atul@25058
   274
  END IF;
atul@25058
   275
pandeeswari@21356
   276
    --Check whether warehouse belongs to the organization.
pandeeswari@21356
   277
    SELECT count(AD_ORG_ID)
pandeeswari@21356
   278
    INTO v_count
pandeeswari@21356
   279
    FROM AD_Org_Warehouse
pandeeswari@21356
   280
    WHERE M_Warehouse_ID=v_M_Warehouse_ID
pandeeswari@21356
   281
    AND AD_Org_ID = v_Org_ID;
pandeeswari@21356
   282
pandeeswari@21335
   283
    IF v_count = 0 AND v_IsSOTrx = 'Y' THEN
pandeeswari@21356
   284
    RAISE_APPLICATION_ERROR(-20000,'@WrongWarehouse@') ;
pandeeswari@21356
   285
    END IF;
pandeeswari@21356
   286
pandeeswari@21373
   287
    SELECT AD_Org_ID
pandeeswari@21373
   288
    INTO v_Warehouse_Org
pandeeswari@21373
   289
    FROM M_Warehouse
pandeeswari@21373
   290
    WHERE M_Warehouse_ID = v_M_Warehouse_ID;
pandeeswari@21373
   291
pandeeswari@21373
   292
    IF(ad_org_isinnaturaltree(v_Warehouse_Org, v_Org_ID, v_Client_ID) = 'N' AND v_isSoTrx = 'N') THEN
pandeeswari@21373
   293
      RAISE_APPLICATION_ERROR(-20000,'@WrongWarehouse@');
pandeeswari@21373
   294
    END IF;
pandeeswari@21373
   295
david@19193
   296
    SELECT CASE WHEN (m.ISSOTRX='Y') THEN customer_blocking  ELSE vendor_blocking END ,  
david@19193
   297
    CASE WHEN (m.ISSOTRX='Y') THEN so_order_blocking ELSE po_order_blocking  END, name, DocAction
david@19193
   298
      INTO v_bpartner_blocked, v_orderBlocking, v_bpartner_name, v_DocAction
david@19193
   299
    FROM C_ORDER m, C_BPartner bp
david@19193
   300
    WHERE m.c_bpartner_id=bp.c_bpartner_id
david@19193
   301
    AND m.C_ORDER_ID=v_Record_ID
david@19193
   302
    AND m.C_BPARTNER_ID=v_c_Bpartner_ID;
david@19193
   303
    IF (v_DocAction = 'CO' AND v_bpartner_blocked = 'Y' AND v_orderBlocking = 'Y' AND v_isreturndoctype='N' ) THEN
david@19193
   304
      RAISE_APPLICATION_ERROR(-20000,'@ThebusinessPartner@'||' '|| v_bpartner_name ||' '||'@BusinessPartnerBlocked@');
david@19193
   305
    END IF;
gorkaion@14379
   306
    -- Get the name of the org of the Order. Added by P.Sarobe
gorkaion@14379
   307
    SELECT name INTO v_Org_Name FROM AD_ORG WHERE ad_org_id = v_Org_ID;
asier@799
   308
    DBMS_OUTPUT.PUT_LINE('DocAction=' || v_DocAction || ', DocStatus=' || v_DocStatus || ', DocType_ID=' || v_DocType_ID || ', DocTypeTarget_ID=' || v_DocTypeTarget_ID || ', DocSubTypeSO=' || v_DocSubTypeSO) ;
gorkaion@14379
   309
gorkaion@20972
   310
    SELECT count(*) INTO v_count
gorkaion@20972
   311
    FROM dual
gorkaion@20972
   312
    WHERE EXISTS (
gorkaion@20972
   313
        SELECT 1
gorkaion@20972
   314
        FROM c_orderline ol JOIN m_product p ON ol.m_product_id = p.m_product_id
gorkaion@20972
   315
        WHERE ol.c_order_id = v_record_id
gorkaion@20972
   316
          AND p.isgeneric = 'Y');
gorkaion@20972
   317
    IF (v_count > 0) THEN
gorkaion@20972
   318
      SELECT max(p.name) INTO v_productname
gorkaion@20972
   319
      FROM c_orderline ol JOIN m_product p ON ol.m_product_id = p.m_product_id
gorkaion@20972
   320
      WHERE ol.c_order_id = v_record_id
gorkaion@20972
   321
        AND p.isgeneric = 'Y';
gorkaion@20940
   322
      RAISE_APPLICATION_ERROR(-20000, '@CannotUseGenericProduct@ ' || v_productName);
gorkaion@20940
   323
    END IF;
gorkaion@20940
   324
gorkaion@18752
   325
    /** 
gorkaion@18752
   326
    * Quotations
gorkaion@18752
   327
    */
gorkaion@18752
   328
    IF (v_DocSubTypeSOTarget = 'OB'AND v_DocAction = 'RJ') THEN
gorkaion@18752
   329
      SELECT c_reject_reason_id
gorkaion@18752
   330
        INTO v_reject_reason
gorkaion@18752
   331
      FROM C_ORDER
gorkaion@18752
   332
      WHERE C_Order_ID=v_Record_ID;
gorkaion@18752
   333
      IF (v_reject_reason IS NULL) THEN
gorkaion@18752
   334
        RAISE_APPLICATION_ERROR(-20000, '@NoRejectReason@') ;
gorkaion@18752
   335
      END IF;
david@20461
   336
      /*
david@20461
   337
        * Undo inventory reservation
david@20461
   338
        */
david@20461
   339
        BEGIN
david@20461
   340
          v_ResultStr:='ReserveInventory';
david@20461
   341
          -- Set reserved quantity to 0
david@20461
   342
          UPDATE C_ORDERLINE
david@20461
   343
          SET QtyReserved = 0,
david@20461
   344
              Updated=now(),
david@20461
   345
              UpdatedBy=v_User
david@20461
   346
          WHERE c_orderline_id IN (select c_orderline_id
david@20461
   347
                                   from c_orderline
david@20461
   348
                                   where c_order_id = v_Record_id);
david@20461
   349
        END;
gorkaion@18752
   350
      UPDATE C_ORDER
gorkaion@18752
   351
      SET DocStatus='CJ',
gorkaion@18752
   352
          DocAction='--',
gorkaion@18752
   353
          Processed='Y',
gorkaion@18752
   354
          Updated=now(),
gorkaion@18752
   355
          UpdatedBy=v_User
gorkaion@18752
   356
      WHERE C_Order_ID=v_Record_ID;
gorkaion@18752
   357
      IF (p_PInstance_ID IS NOT NULL) THEN
gorkaion@18752
   358
      	--  Update AD_PInstance
gorkaion@18752
   359
      	DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished - ' || v_Message) ;
gorkaion@18752
   360
        AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
gorkaion@18752
   361
      END IF;
gorkaion@18752
   362
      RETURN;
gorkaion@18752
   363
    END IF;
gorkaion@18752
   364
gorka@1069
   365
    /**
gorka@1069
   366
    * Check if order has lines
gorka@1069
   367
    */
pandeeswari@22015
   368
    IF (v_DocAction = 'CO' OR v_DocAction = 'PR') THEN
gorka@1069
   369
      SELECT COUNT(*)
gorka@1069
   370
        INTO v_Aux
gorka@1069
   371
       FROM C_ORDERLINE
gorka@1069
   372
       WHERE C_ORDER_ID = v_Record_ID;
gorka@1069
   373
       IF v_Aux=0 THEN
gorka@1069
   374
         RAISE_APPLICATION_ERROR(-20000, '@OrderWithoutLines@');
gorka@1069
   375
       END IF;
gorka@1069
   376
    END IF;
gorkaion@14379
   377
victor@21513
   378
    -- Check the cash vat flag for all the taxes matches the order one
victor@21513
   379
    IF (v_DocAction IN ('CO', 'PR')) THEN
victor@21513
   380
      select count(1)
victor@21513
   381
      into v_Aux
victor@21513
   382
      from c_ordertax ot inner join c_tax t on (ot.c_tax_id = t.c_tax_id)
victor@21513
   383
      where ot.c_order_id = v_Record_ID
victor@21909
   384
      and t.iswithholdingtax = 'N'
victor@21909
   385
      and t.rate <> 0
victor@21513
   386
      and t.IsCashVat <> v_iscashvat;
victor@21513
   387
victor@21513
   388
      IF (v_Aux > 0) THEN
victor@21513
   389
        RAISE_APPLICATION_ERROR(-20000, '@CashVATNotMatch@');
victor@21513
   390
      END IF;
victor@21513
   391
    END IF;
victor@21513
   392
carlos@0
   393
    /**
carlos@0
   394
    * Order Closed, Voided or Reversed - No action possible
carlos@0
   395
    */
carlos@0
   396
    IF(v_DocStatus IN('CL', 'VO', 'RE')) THEN
carlos@0
   397
      RAISE_APPLICATION_ERROR(-20000, '@AlreadyPosted@') ;
carlos@0
   398
    END IF;
gorkaion@14379
   399
gorkaion@14379
   400
    /**
gorkaion@14379
   401
    * Waiting on Prepayment  can only be closed
gorkaion@14379
   402
    */
gorkaion@14379
   403
    IF(v_DocStatus='WP' AND v_DocAction<>'CL') THEN
gorkaion@14379
   404
      RAISE_APPLICATION_ERROR(-20000, '@WaitingPayment@') ;
gorkaion@14379
   405
    END IF;
gorkaion@14379
   406
ainhoa@3126
   407
    IF (v_DocSubTypeSOTarget='PR' AND v_invoicerule <> 'I') THEN
carlos@0
   408
      RAISE_APPLICATION_ERROR(-20000, '@PrepayMustImmediate@');
carlos@0
   409
    END IF;
gorkaion@14379
   410
    /**
gorkaion@14379
   411
    * Unlock
gorkaion@14379
   412
    */
gorkaion@14379
   413
    IF(v_DocAction='XL') THEN
gorkaion@14379
   414
      UPDATE C_ORDER
gorkaion@14379
   415
        SET Processing='N',
gorkaion@14379
   416
        DocAction='--',
gorkaion@14379
   417
        Updated=now(),
gorkaion@14379
   418
        UpdatedBy=v_User
gorkaion@14379
   419
      WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
   420
      FINISH_PROCESS:=TRUE;
gorkaion@14379
   421
    END IF;
carlos@0
   422
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   423
      IF(v_IsProcessing='Y') THEN
carlos@0
   424
        RAISE_APPLICATION_ERROR(-20000, '@OtherProcessActive@') ;
carlos@0
   425
      END IF;
gorkaion@14379
   426
carlos@0
   427
      /**
carlos@0
   428
      * Re-activate
carlos@0
   429
      */
gorkaion@14379
   430
      IF (v_DocAction='RE') THEN
gorkaion@14389
   431
        IF (v_DocSubTypeSO IN ('WR', 'WI')) THEN
gorkaion@3677
   432
          RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@3677
   433
        END IF;
carlos@0
   434
        --Verify not managed debtPayments added by ALO
gorkaion@14379
   435
        --Added by P.Sarobe. New messages
carlos@0
   436
        SELECT max(c_debt_payment_id), COUNT(*)
gorkaion@14379
   437
          INTO v_Debtpayment_ID, v_Aux
carlos@0
   438
        FROM C_DEBT_PAYMENT
carlos@0
   439
        WHERE C_Order_ID=v_Record_ID
carlos@0
   440
          AND C_Debt_Payment_Status(C_Settlement_Cancel_ID, Cancel_Processed, Generate_Processed, IsPaid, IsValid, C_CashLine_ID, C_BankStatementLine_ID)!='P';
gorkaion@14379
   441
        IF (v_Aux != 0) THEN
gorkaion@14379
   442
          --Added by P.Sarobe. New messages
gorkaion@14379
   443
          SELECT c_Bankstatementline_Id, c_cashline_id, c_settlement_cancel_id, ispaid, cancel_processed
gorkaion@14379
   444
            INTO v_Bankstatementline_ID, v_CashLine_ID, v_Settlement_Cancel_ID, v_ispaid, v_Cancel_Processed
gorkaion@14379
   445
          FROM C_DEBT_PAYMENT WHERE C_Debt_Payment_ID = v_Debtpayment_ID;
gorkaion@14379
   446
gorkaion@14379
   447
          IF (v_Bankstatementline_ID IS NOT NULL) THEN
gorkaion@14379
   448
            SELECT C_BANKSTATEMENT.NAME, C_BANKSTATEMENT.STATEMENTDATE
gorkaion@14379
   449
              INTO v_nameBankstatement, v_dateBankstatement
gorkaion@14379
   450
            FROM C_BANKSTATEMENT, C_BANKSTATEMENTLINE
gorkaion@14379
   451
            WHERE C_BANKSTATEMENT.C_BANKSTATEMENT_ID = C_BANKSTATEMENTLINE.C_BANKSTATEMENT_ID
gorkaion@14379
   452
              AND C_BANKSTATEMENTLINE.C_BANKSTATEMENTLINE_ID = v_Bankstatementline_ID;
gorkaion@14379
   453
            RAISE_APPLICATION_ERROR(-20000, '@ManagedDebtPaymentOrderBank@'||v_nameBankstatement||' '||'@Bydate@'||v_dateBankstatement) ;
gorkaion@14379
   454
          END IF;
gorkaion@14379
   455
          IF (v_CashLine_ID IS NOT NULL) THEN
gorkaion@14379
   456
            SELECT C_CASH.NAME, C_CASH.STATEMENTDATE
gorkaion@14379
   457
              INTO v_nameCash, v_dateCash
gorkaion@14379
   458
            FROM C_CASH, C_CASHLINE
gorkaion@14379
   459
            WHERE C_CASH.C_CASH_ID = C_CASHLINE.C_CASH_ID
gorkaion@14379
   460
              AND C_CASHLINE.C_CASHLINE_ID = v_CashLine_ID;
gorkaion@14379
   461
            RAISE_APPLICATION_ERROR(-20000, '@ManagedDebtPaymentOrderCash@'||v_nameCash||' '||'@Bydate@'||v_dateCash) ;
gorkaion@14379
   462
          END IF;
gorkaion@14379
   463
          IF (v_Cancel_Processed='Y' AND v_ispaid='N') THEN
gorkaion@14379
   464
            SELECT documentno, datetrx
gorkaion@14379
   465
              INTO v_documentno_Settlement, v_dateSettlement
gorkaion@14379
   466
            FROM C_SETTLEMENT
gorkaion@14379
   467
            WHERE C_SETTLEMENT_ID = v_Settlement_Cancel_ID;
gorkaion@14379
   468
            RAISE_APPLICATION_ERROR(-20000, '@ManagedDebtPaymentOrderCancel@'||v_documentno_Settlement||' '||'@Bydate@'||v_dateSettlement) ;
gorkaion@14379
   469
          END IF;
carlos@0
   470
        END IF;
gorkaion@14379
   471
gorkaion@14379
   472
        DBMS_OUTPUT.PUT_LINE('Re-Activating ' || v_DocSubTypeSO || ': ' || v_Record_ID) ;
gorkaion@14379
   473
        IF(v_DocSubTypeSO IN ('WI', 'WP', 'WR')) THEN
gorkaion@14379
   474
          -- Cancel existing Deli very + Invoice Documents
gorkaion@14379
   475
          M_INOUT_CANCEL(NULL, v_Record_ID) ;
gorkaion@14379
   476
          IF (v_DocSubTypeSO<>'WP') THEN
gorkaion@14379
   477
            C_INVOICE_CANCEL(NULL, v_Record_ID);
carlos@0
   478
          END IF;
gorkaion@14379
   479
        END IF;
gorkaion@14379
   480
        -- Update Order
gorkaion@14379
   481
        v_ResultStr:='ReActivate';
gorkaion@14379
   482
        UPDATE C_ORDER
gorkaion@14379
   483
        SET DocStatus='IP', -- In Progress
gorkaion@14389
   484
            DocAction='CO',
carlos@0
   485
            Processing='N',
carlos@0
   486
            Processed='N',
harikrishnan@7209
   487
            Updated=now(),
harikrishnan@7209
   488
            UpdatedBy=v_User
gorkaion@14379
   489
        WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
   490
        UPDATE M_INOUTLINE SET C_ORDERLINE_ID = NULL
gorkaion@14379
   491
        WHERE (SELECT DISTINCT A.DOCSTATUS FROM M_INOUT A, M_INOUTLINE B, C_ORDERLINE C
gorkaion@14379
   492
               WHERE A.M_INOUT_ID = B.M_INOUT_ID AND B.C_ORDERLINE_ID = C.C_ORDERLINE_ID
gorkaion@14379
   493
                 AND C.C_ORDER_DISCOUNT_ID IS NOT NULL AND C.C_ORDER_ID = v_Record_ID) = 'VO'
gorkaion@14379
   494
                 AND C_ORDERLINE_ID IN (SELECT C_ORDERLINE_ID FROM C_ORDERLINE WHERE C_ORDER_DISCOUNT_ID IS NOT NULL 
gorkaion@14379
   495
                 AND C_ORDER_ID = v_Record_ID);
gorkaion@14379
   496
        DELETE
gorkaion@14379
   497
        FROM C_ORDERLINE
gorkaion@14379
   498
        WHERE C_ORDER_DISCOUNT_ID IS NOT NULL
victor@3593
   499
          AND C_ORDER_ID=v_Record_ID;
gorkaion@14379
   500
        --ADDED BY E.ARGAL
gorkaion@14379
   501
        --Invalidate debt payments added by ALO
gorkaion@14379
   502
        UPDATE C_DEBT_PAYMENT
gorkaion@14379
   503
        SET IsValid='N',
harikrishnan@7209
   504
            Updated=now(),
harikrishnan@7209
   505
            UpdatedBy=v_User
gorkaion@14379
   506
        WHERE C_Order_ID=v_Record_ID
gorkaion@14379
   507
          AND IsAutomaticGenerated='N';
carlos@0
   508
gorkaion@14379
   509
        DELETE
gorkaion@14379
   510
        FROM C_CASHLINE
gorkaion@14379
   511
        WHERE C_Debt_Payment_Id IN
carlos@0
   512
            (SELECT C_DEBT_PAYMENT_ID
carlos@0
   513
            FROM C_DEBT_PAYMENT
carlos@0
   514
            WHERE C_Order_ID=v_Record_ID
carlos@0
   515
              AND COALESCE(IsAutomaticGenerated, 'Y')='Y'
gorkaion@14379
   516
            );
gorkaion@14379
   517
        DELETE
gorkaion@14379
   518
        FROM C_DEBT_PAYMENT
gorkaion@14379
   519
        WHERE C_Order_ID=v_Record_ID
gorkaion@14379
   520
          AND COALESCE(IsAutomaticGenerated, 'Y')='Y';
gorkaion@14379
   521
        /*
gorkaion@14379
   522
        * Undo inventory reservation
gorkaion@14379
   523
        */
gorkaion@14379
   524
        DECLARE
gorkaion@14379
   525
          Cur_ResLine RECORD;
gorkaion@14379
   526
          v_QtySO NUMBER; -- Reserved
gorkaion@14379
   527
          v_QtyOrderSO NUMBER;
gorkaion@14379
   528
          v_QtyPO NUMBER; -- Ordered
gorkaion@14379
   529
          v_QtyOrderPO NUMBER;
gorkaion@14379
   530
        BEGIN
gorkaion@14379
   531
          v_ResultStr:='ReserveInventory';
gorkaion@14379
   532
          -- For all lines needing reservation
gorkaion@14379
   533
          FOR Cur_ResLine IN
gorkaion@14379
   534
            (SELECT l.M_Warehouse_ID, l.M_Product_ID, l.M_AttributeSetInstance_ID, l.C_OrderLine_ID,
gorkaion@14379
   535
                 l.QtyOrdered AS Qty, l.QUANTITYORDER, l.qtyreserved, l.qtydelivered,
gorkaion@14379
   536
                l.C_UOM_ID, l.M_PRODUCT_UOM_ID
gorkaion@14379
   537
             FROM C_ORDERLINE l, M_PRODUCT p
gorkaion@14379
   538
             WHERE l.C_Order_ID=v_Record_ID  -- Reserve Products (not: services, null products)
gorkaion@14379
   539
               AND l.M_Product_ID=p.M_Product_ID
gorkaion@14379
   540
               AND p.IsStocked='Y'
sandra@25786
   541
               AND p.ProductType='I' 
gorkaion@14379
   542
             )
carlos@0
   543
            LOOP
gorkaion@14379
   544
            -- Qty corrected for SO/PO
gorkaion@14389
   545
            IF (v_DocSubTypeSO IS NULL) THEN
gorkaion@14379
   546
              v_QtySO:=0;
gorkaion@14379
   547
              v_QtyOrderSO:=NULL;
gorkaion@14379
   548
              v_QtyPO:=Cur_ResLine.qtydelivered-Cur_ResLine.qty;
gorkaion@14379
   549
              v_QtyOrderPO:=NULL;
gorkaion@14379
   550
              IF (Cur_ResLine.QtyDelivered=0) THEN
gorkaion@14379
   551
                v_QtyOrderPO := -Cur_ResLine.QuantityOrder;
gorkaion@14379
   552
              ELSIF Cur_ResLine.M_Product_UOM_ID IS NOT NULL THEN
gorkaion@14379
   553
                v_QtyOrderPO := -C_Uom_Convert(v_QtyPO, Cur_ResLine.C_UOM_ID, Cur_ResLine.M_Product_UOM_ID, 'Y');
carlos@0
   554
              END IF;
gorkaion@14379
   555
            ELSE
gorkaion@14379
   556
              v_QtySO:=-Cur_ResLine.QtyReserved;
gorkaion@14379
   557
              IF (Cur_ResLine.QtyReserved=Cur_ResLine.Qty) THEN
gorkaion@14379
   558
                v_QtyOrderSO := -Cur_ResLine.QuantityOrder;
gorkaion@14379
   559
              ELSIF Cur_ResLine.M_Product_UOM_ID IS NOT NULL THEN
gorkaion@14379
   560
                v_QtyOrderSO := -C_Uom_Convert(v_QtySO, Cur_ResLine.C_UOM_ID, Cur_ResLine.M_Product_UOM_ID, 'Y');
harikrishnan@7882
   561
              END IF;
gorkaion@14379
   562
              v_QtyPO:=0;
gorkaion@14379
   563
              v_QtyOrderPO:=NULL;
gorkaion@14379
   564
            END IF;
jon@21226
   565
            IF ((v_DocStatus<>'IP' OR v_DocAction<>'CO') AND COALESCE(v_DocSubTypeSO, '')<>'OB') THEN
gorkaion@14379
   566
              M_UPDATE_STORAGE_PENDING(v_Client_ID, v_Org_ID, v_UpdatedBy, Cur_ResLine.M_Product_ID, Cur_ResLine.M_Warehouse_ID, Cur_ResLine.M_AttributeSetInstance_ID, Cur_ResLine.C_UOM_ID, Cur_ResLine.M_PRODUCT_UOM_ID, v_QtySO, v_QtyOrderSO, v_QtyPO, v_QtyOrderPO) ;
gorkaion@14379
   567
            END IF;
gorkaion@14379
   568
          END LOOP;
gorkaion@2658
   569
          -- Set reserved quantity to 0
gorkaion@2658
   570
          UPDATE C_ORDERLINE
harikrishnan@7209
   571
          SET QtyReserved = 0,
gorkaion@14379
   572
              Updated=now(),
gorkaion@14379
   573
              UpdatedBy=v_User
gorkaion@2658
   574
          WHERE c_orderline_id IN (select c_orderline_id
gorkaion@2658
   575
                                   from c_orderline
gorkaion@2658
   576
                                   where c_order_id = v_Record_id);
mikel@17452
   577
        END;
gorkaion@18717
   578
        /**
gorkaion@18717
   579
        *  Manage Stock Reservations
gorkaion@18717
   580
        */
gorkaion@18749
   581
        SELECT COUNT(1) INTO v_count
gorkaion@18749
   582
        FROM ad_preference
gorkaion@18749
   583
        WHERE property = 'StockReservations';
gorkaion@18749
   584
        IF (v_count > 1) THEN
gorkaion@18749
   585
          v_dummy := AD_GET_PREFERENCE_VALUE('StockReservations', 'Y', v_client_id, v_org_id, NULL, NULL, NULL);
gorkaion@18749
   586
        ELSIF (v_count = 1) THEN
gorkaion@18749
   587
          UPDATE c_orderline
gorkaion@18749
   588
          SET so_res_status = NULL
gorkaion@18749
   589
          WHERE c_order_id = v_Record_id;
gorkaion@18749
   590
          UPDATE c_order
gorkaion@18749
   591
          SET so_res_status = NULL
gorkaion@18749
   592
          WHERE c_order_id = v_record_id;
gorkaion@18749
   593
        END IF;
gorkaion@2658
   594
gorkaion@14379
   595
        --ADDED BY P.SAROBE but to be deprecated 26052007
gorkaion@14379
   596
        SELECT MAX(C_CASHLINE_ID)
carlos@0
   597
          INTO v_CashLine_ID
gorkaion@14379
   598
        FROM C_CASHLINE
gorkaion@14379
   599
        WHERE C_ORDER_ID=v_Record_ID;
gorkaion@14379
   600
        IF (v_CashLine_ID IS NOT NULL) THEN
gorkaion@14379
   601
          SELECT PROCESSED
carlos@0
   602
            INTO v_IsProcessed
gorkaion@14379
   603
          FROM C_CASH, C_CASHLINE
gorkaion@14379
   604
          WHERE C_CASH.C_CASH_ID=C_CASHLINE.C_CASH_ID
gorkaion@14379
   605
            AND C_CASHLINE_ID=v_CashLine_ID;
gorkaion@14379
   606
          IF (v_IsProcessed='N') THEN
gorkaion@14379
   607
            DELETE FROM C_CASHLINE WHERE C_CASHLINE_ID=v_CashLine_ID;
gorkaion@14379
   608
          ELSE
gorkaion@14379
   609
            SELECT C_CASH.NAME, C_CASH.STATEMENTDATE, C_CASHLINE.LINE
gorkaion@14379
   610
              INTO v_nameCash, v_dateCash, v_Line
gorkaion@14379
   611
            FROM C_CASH, C_CASHLINE
gorkaion@14379
   612
            WHERE C_CASH.C_CASH_ID = C_CASHLINE.C_CASH_ID
gorkaion@14379
   613
              AND C_CASHLINE.C_CASHLINE_ID = v_CashLine_ID;
gorkaion@14379
   614
            RAISE_APPLICATION_ERROR(-20000, '@Ordercahslineprocessed@'||v_nameCash||' '||'@Bydate@'||v_dateCash||' '||'@line@'||v_Line) ;
gorkaion@14379
   615
          END IF;
carlos@0
   616
        END IF;
carlos@0
   617
carlos@0
   618
        UPDATE C_ORDER
gorkaion@14379
   619
        SET DocStatus='DR', -- Draft
gorkaion@14389
   620
            DocAction='CO',
carlos@0
   621
            Processing='N',
harikrishnan@7209
   622
            Updated=now(),
harikrishnan@7209
   623
            UpdatedBy=v_User
gorkaion@14379
   624
        WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
   625
carlos@0
   626
        FINISH_PROCESS:=TRUE;
carlos@0
   627
      END IF;
carlos@0
   628
    END IF;--FINISH_PROCESS
gorkaion@14379
   629
    IF (NOT FINISH_PROCESS) THEN
gorkaion@14379
   630
      SELECT COUNT(*)
gorkaion@14379
   631
        INTO v_Count
gorkaion@14379
   632
      FROM C_ORDER C, C_DOCTYPE
gorkaion@14389
   633
      WHERE C_DocType.DocBaseType IN ('SOO', 'POO')
gorkaion@14379
   634
        AND C_DocType.IsSOTrx=C.ISSOTRX
gorkaion@14379
   635
        AND AD_ISORGINCLUDED(C.AD_Org_ID,C_DocType.AD_Org_ID, C.AD_Client_ID) <> -1
gorkaion@14379
   636
        AND C.C_DOCTYPETARGET_ID = C_DOCTYPE.C_DOCTYPE_ID
gorkaion@14379
   637
        AND C.C_ORDER_ID = v_Record_ID;
gorkaion@14379
   638
      IF (v_Count=0) THEN
gorkaion@14379
   639
        RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgDoctypeOrder@') ;
carlos@0
   640
      END IF;
carlos@0
   641
pandeeswari@18807
   642
      SELECT COUNT(*)
pandeeswari@18807
   643
        INTO v_Count
pandeeswari@18807
   644
      FROM C_ORDER C, C_ORDERLINE OL
pandeeswari@18807
   645
      WHERE C.C_ORDER_ID = OL.C_ORDER_ID
eduardo@19389
   646
        AND AD_ISORGINCLUDED(OL.AD_Org_ID, C.AD_Org_ID, C.AD_Client_ID) = -1
pandeeswari@18807
   647
        AND C.C_ORDER_ID = v_Record_ID;
eduardo@19389
   648
      IF (v_Count>0) THEN
pandeeswari@18807
   649
        RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgLines@') ;
pandeeswari@18807
   650
      END IF;
pandeeswari@18807
   651
      
carlos@0
   652
carlos@0
   653
      /**
carlos@0
   654
      * Close Order - prepare
carlos@0
   655
      */
ioritz@18392
   656
      DECLARE
ioritz@18392
   657
          Cur_Inventory RECORD;
ioritz@18392
   658
          v_QtyOrdered NUMBER;
ioritz@18392
   659
          v_QuantityOrder NUMBER;
ioritz@18392
   660
          v_linenetamt NUMBER;
ioritz@18587
   661
          v_linegrossamt NUMBER;
ioritz@18392
   662
          v_ProductUOM M_PRODUCT_UOM.C_UOM_ID%TYPE;
ioritz@18392
   663
      BEGIN
ioritz@18392
   664
ioritz@18392
   665
        -- When closing the order it calculates the difference between the ordered and received/delivered quantities ant it
ioritz@18392
   666
        -- updates the m_storage_pending.
ioritz@18392
   667
        IF (v_DocAction='CL') THEN
ioritz@18392
   668
          -- Cancel undelivered Items
ioritz@18392
   669
          IF (v_isSoTrx='Y') THEN --Sales orders
ioritz@18392
   670
            FOR Cur_Inventory IN (
ioritz@18587
   671
              SELECT QtyInvoiced, QtyDelivered ,QtyOrdered, QuantityOrder, priceactual, gross_unit_price,
ioritz@18392
   672
	      
ioritz@18392
   673
	      C_ORDERLINE_ID AS ID,
ioritz@18392
   674
	      M_Product_ID,
ioritz@18392
   675
	      M_Warehouse_ID,
ioritz@18392
   676
	      M_AttributeSetInstance_ID,
ioritz@18392
   677
	      C_UOM_ID,
ioritz@18392
   678
	      M_PRODUCT_UOM_ID,
ioritz@18392
   679
          C_Currency_ID
ioritz@18392
   680
	      FROM C_ORDERLINE
ioritz@18392
   681
	      WHERE C_Order_ID=v_Record_ID
ioritz@18392
   682
                AND QtyOrdered <> (SELECT (CASE WHEN (qtyinvoiced = 0) THEN QtyDelivered ELSE 
ioritz@18392
   683
			  	    (CASE WHEN (QtyDelivered = 0) THEN qtyinvoiced ELSE 
javier@17550
   684
					(CASE WHEN (QtyDelivered < 0 AND qtyinvoiced < 0) THEN LEAST(QtyDelivered, qtyinvoiced) ELSE GREATEST(QtyDelivered, qtyinvoiced) END) END) END)
ioritz@18392
   685
                                  FROM C_ORDERLINE COL
ioritz@18392
   686
                                  WHERE COL.C_ORDERLINE_ID = C_ORDERLINE.C_ORDERLINE_ID)
javier@19470
   687
                AND m_product_id IS NOT NULL
ioritz@18392
   688
            )
ioritz@18392
   689
            LOOP
ioritz@18392
   690
              v_QtyOrdered := CASE WHEN (Cur_Inventory.QtyDelivered < 0) THEN LEAST(Cur_Inventory.QtyDelivered, Cur_Inventory.QtyInvoiced) ELSE GREATEST(Cur_Inventory.QtyDelivered, Cur_Inventory.QtyInvoiced) END;
ioritz@18587
   691
              v_linenetamt := ROUND(v_QtyOrdered * Cur_Inventory.priceactual, C_GET_CURRENCY_PRECISION(Cur_Inventory.C_Currency_ID, 'A'));
ioritz@18587
   692
              v_linegrossamt := ROUND(v_QtyOrdered * Cur_Inventory.gross_unit_price, C_GET_CURRENCY_PRECISION(Cur_Inventory.C_Currency_ID, 'A'));
ioritz@18392
   693
              SELECT MAX(UOM.C_UOM_ID)
ioritz@18392
   694
              INTO v_ProductUOM
ioritz@18392
   695
              FROM M_PRODUCT_UOM UOM 
ioritz@18392
   696
              WHERE UOM.M_PRODUCT_UOM_ID=Cur_Inventory.M_PRODUCT_UOM_ID;
ioritz@18392
   697
              v_QuantityOrder := CASE WHEN Cur_Inventory.M_PRODUCT_UOM_ID IS NULL THEN Cur_Inventory.QuantityOrder
ioritz@18392
   698
                                ELSE (c_uom_convert((CASE WHEN (Cur_Inventory.QtyDelivered < 0) THEN LEAST(Cur_Inventory.QtyDelivered, Cur_Inventory.QtyInvoiced) ELSE GREATEST(Cur_Inventory.QtyDelivered, Cur_Inventory.QtyInvoiced) END),
ioritz@18392
   699
                    Cur_Inventory.C_UOM_ID, v_ProductUOM,'Y')) END;
ioritz@18392
   700
ioritz@18392
   701
              IF (Cur_Inventory.QtyOrdered <> v_QtyOrdered) THEN
ioritz@18392
   702
                M_UPDATE_STORAGE_PENDING(v_Client_ID, v_Org_ID, v_UpdatedBy, Cur_Inventory.M_Product_ID, Cur_Inventory.M_Warehouse_ID, Cur_Inventory.M_AttributeSetInstance_ID,
ioritz@18392
   703
                Cur_Inventory.C_UOM_ID, Cur_Inventory.M_PRODUCT_UOM_ID, -(Cur_Inventory.QtyOrdered - v_QtyOrdered), -(Cur_Inventory.QuantityOrder - v_QuantityOrder), 0, null);
ioritz@18392
   704
              END IF;
ioritz@18392
   705
ioritz@18392
   706
              -- UPDATE C_ORDERLINE
ioritz@18392
   707
              UPDATE C_ORDERLINE
ioritz@18392
   708
              SET QtyOrdered=v_QtyOrdered,
ioritz@18392
   709
                linenetamt=v_linenetamt,
ioritz@18587
   710
                line_gross_amount=v_linegrossamt,
ioritz@18392
   711
                QuantityOrder=v_QuantityOrder,
ioritz@18392
   712
                Updated=now()
ioritz@18392
   713
              WHERE C_ORDERLINE_ID = Cur_Inventory.ID;
ioritz@18392
   714
            END LOOP;
ioritz@18392
   715
            -- For Purchase orders, M_MatchPO table used. Notice that only delivered lines(C_Invoiceline_Id is null) using
ioritz@18392
   716
          ELSE
ioritz@18392
   717
            FOR Cur_Inventory IN (
ioritz@18392
   718
              SELECT 
ioritz@18392
   719
                COALESCE((SELECT SUM(CASE WHEN M_MATCHPO.M_INOUTLINE_ID IS NULL THEN M_MATCHPO.QTY ELSE 0 END)
ioritz@18392
   720
                FROM M_MATCHPO
ioritz@18392
   721
                WHERE M_MATCHPO.C_ORDERLINE_ID=C_ORDERLINE.C_ORDERLINE_ID), 0) AS QtyDelivered,
ioritz@18392
   722
		COALESCE((SELECT   SUM(CASE WHEN M_MATCHPO.C_INVOICELINE_ID IS NULL THEN M_MATCHPO.QTY ELSE 0 END)
ioritz@18392
   723
                FROM M_MATCHPO
ioritz@18392
   724
                WHERE M_MATCHPO.C_ORDERLINE_ID=C_ORDERLINE.C_ORDERLINE_ID), 0) AS QtyInvoiced,
ioritz@18392
   725
                QtyOrdered,
ioritz@18392
   726
                QuantityOrder,
ioritz@18392
   727
		priceactual,
ioritz@18587
   728
		gross_unit_price,
ioritz@18392
   729
		linenetamt,
ioritz@18392
   730
                C_ORDERLINE_ID AS ID,
ioritz@18392
   731
	        M_Product_ID,
ioritz@18392
   732
	        M_Warehouse_ID,
ioritz@18392
   733
	        M_AttributeSetInstance_ID,
ioritz@18392
   734
	        C_UOM_ID,
ioritz@18392
   735
	        M_PRODUCT_UOM_ID,
ioritz@18392
   736
	        C_Currency_ID
ioritz@18392
   737
              FROM C_ORDERLINE
ioritz@18392
   738
              WHERE C_ORDERLINE.C_ORDER_ID=v_Record_ID
ioritz@18392
   739
                AND qtyordered <> COALESCE((
ioritz@18392
   740
                    SELECT (CASE WHEN (SUM(CASE WHEN M_MATCHPO.M_INOUTLINE_ID IS NULL THEN M_MATCHPO.QTY ELSE 0 END) = 0) 
javier@17550
   741
				THEN SUM(CASE WHEN M_MATCHPO.C_INVOICELINE_ID IS NULL THEN M_MATCHPO.QTY ELSE 0 END) ELSE 
javier@17550
   742
					(CASE WHEN (SUM(CASE WHEN M_MATCHPO.C_INVOICELINE_ID IS NULL THEN M_MATCHPO.QTY ELSE 0 END) = 0) 
javier@17550
   743
						THEN SUM(CASE WHEN M_MATCHPO.M_INOUTLINE_ID IS NULL THEN M_MATCHPO.QTY ELSE 0 END)  
javier@17550
   744
						ELSE (CASE WHEN (SUM(CASE WHEN M_MATCHPO.M_INOUTLINE_ID IS NULL THEN M_MATCHPO.QTY ELSE 0 END) < 0 AND SUM(CASE WHEN M_MATCHPO.C_INVOICELINE_ID IS NULL THEN M_MATCHPO.QTY ELSE 0 END) < 0)  
javier@17550
   745
							THEN LEAST(SUM(CASE WHEN M_MATCHPO.M_INOUTLINE_ID IS NULL THEN M_MATCHPO.QTY ELSE 0 END),SUM(CASE WHEN M_MATCHPO.C_INVOICELINE_ID IS NULL THEN M_MATCHPO.QTY ELSE 0 END)) 
javier@17550
   746
							ELSE GREATEST(SUM(CASE WHEN M_MATCHPO.M_INOUTLINE_ID IS NULL THEN M_MATCHPO.QTY ELSE 0 END),SUM(CASE WHEN M_MATCHPO.C_INVOICELINE_ID IS NULL THEN M_MATCHPO.QTY ELSE 0 END)) END) 
javier@17550
   747
					END)
javier@17550
   748
				END)
ioritz@18392
   749
                    FROM M_MATCHPO
ioritz@18392
   750
                    WHERE M_MATCHPO.C_ORDERLINE_ID=C_ORDERLINE.C_ORDERLINE_ID
ioritz@18392
   751
                    ), 0)
ioritz@18392
   752
            )
ioritz@18392
   753
            LOOP
ioritz@18392
   754
              v_QtyOrdered := COALESCE(CASE WHEN (Cur_Inventory.QtyDelivered < 0) THEN LEAST(Cur_Inventory.QtyDelivered, Cur_Inventory.QtyInvoiced) ELSE GREATEST(Cur_Inventory.QtyDelivered, Cur_Inventory.QtyInvoiced) END, 0);
ioritz@18587
   755
              v_linenetamt := ROUND(COALESCE(v_QtyOrdered, 0) * Cur_Inventory.priceactual, C_GET_CURRENCY_PRECISION(Cur_Inventory.C_Currency_ID, 'A'));
ioritz@18587
   756
              v_linegrossamt := ROUND(COALESCE(v_QtyOrdered, 0) * Cur_Inventory.gross_unit_price, C_GET_CURRENCY_PRECISION(Cur_Inventory.C_Currency_ID, 'A'));
ioritz@18392
   757
              SELECT MAX(UOM.C_UOM_ID)
ioritz@18392
   758
              INTO v_ProductUOM
ioritz@18392
   759
              FROM M_PRODUCT_UOM UOM 
ioritz@18392
   760
              WHERE UOM.M_PRODUCT_UOM_ID=Cur_Inventory.M_PRODUCT_UOM_ID;
ioritz@18392
   761
              v_QuantityOrder := CASE WHEN Cur_Inventory.M_PRODUCT_UOM_ID IS NULL THEN Cur_Inventory.QuantityOrder
ioritz@18392
   762
                                ELSE (COALESCE(c_uom_convert((CASE WHEN (Cur_Inventory.QtyDelivered < 0) THEN LEAST(Cur_Inventory.QtyDelivered, Cur_Inventory.QtyInvoiced) ELSE GREATEST(Cur_Inventory.QtyDelivered, Cur_Inventory.QtyInvoiced) END),
ioritz@18392
   763
                    Cur_Inventory.C_UOM_ID, v_ProductUOM,'Y'), 0)) END;
ioritz@18392
   764
              IF (Cur_Inventory.QtyOrdered <> v_QtyOrdered) THEN
ioritz@18392
   765
                M_UPDATE_STORAGE_PENDING(v_Client_ID, v_Org_ID, v_UpdatedBy, Cur_Inventory.M_Product_ID, Cur_Inventory.M_Warehouse_ID, Cur_Inventory.M_AttributeSetInstance_ID,
ioritz@18392
   766
                Cur_Inventory.C_UOM_ID, Cur_Inventory.M_PRODUCT_UOM_ID, 0, null, -(Cur_Inventory.QtyOrdered - v_QtyOrdered), -(Cur_Inventory.QuantityOrder - v_QuantityOrder));
ioritz@18392
   767
              END IF;
ioritz@18392
   768
ioritz@18392
   769
              -- UPDATE C_ORDERLINE
ioritz@18392
   770
              UPDATE C_ORDERLINE
ioritz@18392
   771
              SET QtyOrdered=v_QtyOrdered,
ioritz@18392
   772
                linenetamt=v_linenetamt,
ioritz@18587
   773
                line_gross_amount=v_linegrossamt,
ioritz@18392
   774
                QuantityOrder=v_QuantityOrder,
ioritz@18392
   775
                Updated=now()
ioritz@18392
   776
              WHERE C_ORDERLINE_ID = Cur_Inventory.ID;
ioritz@18392
   777
            END LOOP;
ioritz@18392
   778
          END IF;
ioritz@18392
   779
          -- if there is no change, the tax calculation, etc. is not needed.
ioritz@18392
   780
          -- potential problem, if posted (i.e. encumbered) for full amount
ioritz@18392
   781
          -- and the rest then cancelled out.
carlos@0
   782
        END IF;
ioritz@18392
   783
      END;
gorkaion@14379
   784
gorkaion@14379
   785
      /**
gorkaion@14379
   786
      *Update Product purchasing Plan Table
gorkaion@14545
   787
      * Return Material orders do not update the last price.
gorkaion@14379
   788
      */
gorkaion@14545
   789
      IF (v_isSoTrx ='N' AND v_isreturndoctype = 'N') THEN
gorkaion@14379
   790
        FOR Cur_OrderLine IN (SELECT * FROM C_ORDERLINE WHERE C_Order_Id =  v_Record_ID)
gorkaion@14379
   791
        LOOP
harikrishnan@7808
   792
          UPDATE M_PRODUCT_PO SET PriceLastPO=Cur_OrderLine.PriceActual 
gorkaion@14389
   793
          Where C_BPARTNER_ID = v_CBPartner_ID AND M_PRODUCT_ID = Cur_OrderLine.M_PRODUCT_ID
harikrishnan@7808
   794
          AND Ad_Isorgincluded(Cur_OrderLine.AD_ORG_ID,AD_ORG_ID, Cur_OrderLine.AD_Client_ID) <> -1;
gorkaion@14379
   795
        END LOOP;
gorkaion@14379
   796
      END IF;
carlos@0
   797
      /**
carlos@0
   798
      * Void Order - prepare
carlos@0
   799
      */
gorkaion@14379
   800
      IF (v_DocAction='VO') THEN
carlos@0
   801
        -- Cancel all Items
carlos@0
   802
        UPDATE C_ORDERLINE
gorkaion@14389
   803
          SET QtyOrdered=0,
gorkaion@14389
   804
          --MODIFIED BY F.IRIAZABAL
gorkaion@14389
   805
          QuantityOrder = CASE WHEN M_PRODUCT_UOM_ID IS NULL THEN NULL ELSE 0 END,
gorkaion@14389
   806
          LineNetAmt=0,
gorkaion@14389
   807
          Updated=now()
carlos@0
   808
        WHERE C_Order_ID=v_Record_ID
gorkaion@239
   809
          AND QtyOrdered<>0;
carlos@0
   810
      END IF;
gorkaion@14379
   811
gorkaion@14379
   812
     /**************************************************************************
carlos@0
   813
      * Start Processing ------------------------------------------------------
carlos@0
   814
      *************************************************************************/
asier@1929
   815
      -- Check the header belongs to a organization where transactions are posible and ready to use
asier@1929
   816
      SELECT AD_Org.IsReady, Ad_OrgType.IsTransactionsAllowed
gorkaion@14379
   817
        INTO v_is_ready, v_is_tr_allow
asier@1929
   818
      FROM C_ORDER, AD_Org, AD_OrgType
asier@1929
   819
      WHERE AD_Org.AD_Org_ID=C_ORDER.AD_Org_ID
gorkaion@14379
   820
        AND AD_Org.AD_OrgType_ID=AD_OrgType.AD_OrgType_ID
gorkaion@14379
   821
        AND C_ORDER.C_ORDER_ID=v_Record_ID;
asier@1929
   822
      IF (v_is_ready='N') THEN
asier@1929
   823
        RAISE_APPLICATION_ERROR(-20000, '@OrgHeaderNotReady@');
asier@1929
   824
      END IF;
asier@1929
   825
      IF (v_is_tr_allow='N') THEN
asier@1929
   826
        RAISE_APPLICATION_ERROR(-20000, '@OrgHeaderNotTransAllowed@');
asier@1929
   827
      END IF;
asier@1929
   828
      SELECT AD_ORG_CHK_DOCUMENTS('C_ORDER', 'C_ORDERLINE', v_Record_ID, 'C_ORDER_ID', 'C_ORDER_ID') INTO v_is_included FROM dual;
asier@1929
   829
      IF (v_is_included=-1) THEN
asier@1929
   830
        RAISE_APPLICATION_ERROR(-20000, '@LinesAndHeaderDifferentLEorBU@');
asier@1929
   831
      END IF;
gorkaion@14379
   832
gorkaion@14379
   833
      IF (p_PInstance_ID IS NOT NULL) THEN
carlos@0
   834
        v_ResultStr:='LockingOrder';
carlos@0
   835
        UPDATE C_ORDER  SET Processing='Y'  WHERE C_Order_ID=v_Record_ID;
carlos@0
   836
        COMMIT;
gorkaion@14379
   837
        -- Now, needs to go to END_PROCESSING to unlock
carlos@0
   838
      END IF;
carlos@0
   839
      /**
gorkaion@14389
   840
      * Allowed Actions:  AProve, COmplete, PRocess, CLose, VOid
carlos@0
   841
      */
gorkaion@14389
   842
      IF (v_DocAction IN('AP', 'CO', 'PR', 'CL', 'VO')) THEN
carlos@0
   843
        NULL;
carlos@0
   844
      ELSE
carlos@0
   845
        RAISE_APPLICATION_ERROR(-20000, '@ActionNotAllowedHere@') ;
carlos@0
   846
      END IF;
gorkaion@14379
   847
gorkaion@14389
   848
      SELECT COUNT(*)
gorkaion@14389
   849
        INTO v_count
carlos@0
   850
      FROM AD_CLIENTINFO
carlos@0
   851
      WHERE AD_CLIENT_ID=v_Client_ID
carlos@0
   852
        AND CHECKORDERORG='Y';
gorkaion@14379
   853
      IF (v_count > 0) THEN
carlos@0
   854
        v_ResultStr:='CheckingRestrictions - C_ORDER ORG IS IN C_BPARTNER ORG TREE';
carlos@0
   855
        SELECT COUNT(*)
gorkaion@14379
   856
          INTO v_count
gorkaion@14379
   857
        FROM C_ORDER c, C_BPARTNER bp
carlos@0
   858
        WHERE c.C_Order_ID=v_Record_ID
carlos@0
   859
          AND c.C_BPARTNER_ID=bp.C_BPARTNER_ID
carlos@0
   860
          AND Ad_Isorgincluded(c.AD_ORG_ID, bp.AD_ORG_ID, bp.AD_CLIENT_ID)=-1;
gorkaion@14379
   861
        IF (v_count > 0) THEN
carlos@0
   862
          RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgBpartnerOrder@') ;
carlos@0
   863
        END IF;
carlos@0
   864
      END IF;
adrianromero@11736
   865
victor@3593
   866
     /**************************************************************************
asier@18341
   867
      * Calculate promotions                                                   
asier@18341
   868
      *************************************************************************/
miguel@23885
   869
      IF (v_DocAction = 'CO' AND v_isreturndoctype = 'N' AND v_recalculateDiscounts = 'Y') THEN
asier@18341
   870
         M_PROMOTION_CALCULATE('O', v_Record_ID, v_User);
asier@18341
   871
      END IF;
asier@18341
   872
asier@18341
   873
     /**************************************************************************
gorkaion@14379
   874
      * Calculate Discounts
gorkaion@14379
   875
      *************************************************************************/
adrianromero@11444
   876
miguel@23885
   877
      -- if sales order was created from quotation with "firm quotation" check
miguel@23885
   878
      -- then discounts are not recalculated
miguel@23885
   879
      IF (v_recalculateDiscounts = 'Y') THEN
miguel@23885
   880
        -- Delete first previous discounts (if possible) and then recalculate them
miguel@23885
   881
        UPDATE C_ORDER
miguel@23885
   882
        SET DocStatus='IP', -- In progress
miguel@23885
   883
            Processing='N',
miguel@23885
   884
            Processed='N',
miguel@23885
   885
            Updated=now(),
miguel@23885
   886
            UpdatedBy=v_User
miguel@23885
   887
        WHERE C_Order_ID=v_Record_ID;
adrianromero@11444
   888
miguel@23885
   889
        DELETE
miguel@23885
   890
        FROM C_ORDERLINE
miguel@23885
   891
        WHERE C_ORDER_DISCOUNT_ID IS NOT NULL
miguel@23885
   892
          AND C_ORDER_ID = v_Record_ID
miguel@23885
   893
          AND NOT EXISTS (SELECT C_INVOICELINE_ID FROM C_INVOICELINE WHERE C_INVOICELINE.C_ORDERLINE_ID = C_ORDERLINE.C_ORDERLINE_ID)
miguel@23885
   894
          AND NOT EXISTS (SELECT M_INOUTLINE_ID FROM M_INOUTLINE WHERE M_INOUTLINE.C_ORDERLINE_ID = C_ORDERLINE.C_ORDERLINE_ID)
miguel@23885
   895
          AND NOT EXISTS (SELECT M_MATCHPO_ID FROM M_MATCHPO WHERE M_MATCHPO.C_ORDERLINE_ID = C_ORDERLINE.C_ORDERLINE_ID);
adrianromero@11736
   896
miguel@23885
   897
        UPDATE C_ORDERLINE
miguel@23885
   898
        SET pricelist = 0, priceactual = 0, pricelimit = 0, linenetamt = 0, pricestd = 0
miguel@23885
   899
        WHERE C_ORDER_DISCOUNT_ID IS NOT NULL
miguel@23885
   900
          AND C_ORDER_ID=v_Record_ID;
adrianromero@11736
   901
miguel@23885
   902
        UPDATE C_ORDER
miguel@23885
   903
        SET DocStatus=v_DocStatus, -- restore
miguel@23885
   904
            Processing=v_IsProcessing,
miguel@23885
   905
            Processed=v_IsProcessed,
miguel@23885
   906
            Updated=now(),
miguel@23885
   907
            UpdatedBy=v_User
miguel@23885
   908
        WHERE C_Order_ID=v_Record_ID;
adrianromero@11736
   909
miguel@23885
   910
        v_CumDiscount:=0;
miguel@23885
   911
        v_OldCumDiscount:=0;
miguel@23885
   912
        v_Line:=10;
miguel@23885
   913
        v_precision:=0;
miguel@23885
   914
        SELECT MAX(LINE)
miguel@23885
   915
          INTO v_OrderLineSeqNo
miguel@23885
   916
        FROM C_ORDERLINE
miguel@23885
   917
        WHERE C_ORDER_ID=v_Record_ID;
miguel@23885
   918
        SELECT PricePrecision INTO v_precision
miguel@23885
   919
        FROM C_ORDER o, C_CURRENCY c
miguel@23885
   920
        WHERE o.C_ORDER_ID = v_Record_ID AND  o.C_CURRENCY_ID = c.C_CURRENCY_ID;
miguel@23885
   921
        FOR Cur_COrderDiscount IN
miguel@23885
   922
           (SELECT C_ORDER_DISCOUNT.C_ORDER_DISCOUNT_ID, C_DISCOUNT.DISCOUNT, C_DISCOUNT.M_PRODUCT_ID, C_DISCOUNT.NAME,
miguel@23885
   923
              C_ORDER_DISCOUNT.CASCADE, C_DISCOUNT.C_DISCOUNT_ID, M_PRODUCT.C_UOM_ID
miguel@23885
   924
            FROM C_ORDER_DISCOUNT, C_DISCOUNT, M_PRODUCT
miguel@23885
   925
            WHERE C_ORDER_DISCOUNT.C_DISCOUNT_ID=C_DISCOUNT.C_DISCOUNT_ID
miguel@23885
   926
              AND C_DISCOUNT.M_PRODUCT_ID=M_PRODUCT.M_PRODUCT_ID
miguel@23885
   927
              AND C_ORDER_DISCOUNT.C_ORDER_ID=v_Record_ID
miguel@23885
   928
              AND C_ORDER_DISCOUNT.ISACTIVE='Y'
miguel@23885
   929
              AND C_DISCOUNT.ISACTIVE='Y'
miguel@23885
   930
            ORDER BY C_ORDER_DISCOUNT.LINE
victor@3593
   931
            )
gorkaion@14379
   932
        LOOP
miguel@23885
   933
          v_CumDiscount:=(1-v_OldCumDiscount) * Cur_COrderDiscount.Discount/100;
miguel@23885
   934
          v_OldCumDiscount:=v_OldCumDiscount + v_CumDiscount;
miguel@23885
   935
          FOR Cur_TaxDiscount IN
miguel@23885
   936
             (SELECT C_ORDERLINE.C_TAX_ID,
miguel@23885
   937
                SUM(C_ORDERLINE.LINENETAMT) AS LINENETAMT,
miguel@23885
   938
                SUM(C_ORDERLINE.LINE_GROSS_AMOUNT) AS LINEGROSSAMT
miguel@23885
   939
              FROM C_ORDERLINE
miguel@23885
   940
              WHERE C_ORDER_ID=v_Record_ID
miguel@23885
   941
                AND C_ORDERLINE.LINENETAMT<>0
miguel@23885
   942
                AND C_ORDER_DISCOUNT_ID IS NULL
miguel@23885
   943
              GROUP BY C_TAX_ID
miguel@23885
   944
              )
miguel@23885
   945
          LOOP
miguel@23885
   946
            IF (v_istaxincluded = 'Y') THEN
miguel@23885
   947
              IF (Cur_COrderDiscount.CASCADE='Y') THEN
miguel@23885
   948
                v_line_gross_amount:=(-1) * Cur_TaxDiscount.LINEGROSSAMT * v_CumDiscount;
miguel@23885
   949
              ELSE
miguel@23885
   950
                v_line_gross_amount:=(-1) * Cur_TaxDiscount.LINEGROSSAMT * Cur_COrderDiscount.Discount/100;
miguel@23885
   951
              END IF;
miguel@23885
   952
              v_Discount:= C_GET_NET_PRICE_FROM_GROSS(Cur_TaxDiscount.C_TAX_ID, v_line_gross_amount, 0, v_precision, 1);
miguel@23885
   953
              v_gross_unit_price:= v_line_gross_amount;
mikel@17452
   954
            ELSE
miguel@23885
   955
              IF (Cur_COrderDiscount.CASCADE='Y') THEN
miguel@23885
   956
                v_Discount:=(-1) * Cur_TaxDiscount.LINENETAMT * v_CumDiscount;
miguel@23885
   957
              ELSE
miguel@23885
   958
                v_Discount:=(-1) * Cur_TaxDiscount.LINENETAMT * Cur_COrderDiscount.Discount/100;
miguel@23885
   959
              END IF;
miguel@23885
   960
              v_gross_unit_price:= 0;
miguel@23885
   961
              v_line_gross_amount:= 0;
mikel@17452
   962
            END IF;
adrianromero@11736
   963
miguel@23885
   964
            SELECT COUNT(*) INTO v_DiscountExist FROM C_ORDERLINE
adrianromero@11736
   965
            WHERE C_ORDERLINE.C_ORDER_DISCOUNT_ID = Cur_COrderDiscount.C_ORDER_DISCOUNT_ID
gorkaion@14379
   966
              AND C_ORDERLINE.C_TAX_ID = Cur_TaxDiscount.C_TAX_ID;
miguel@23885
   967
miguel@23885
   968
            IF (v_DiscountExist = 0) THEN
miguel@23885
   969
              v_OrderLineSeqNo:=10 + v_OrderLineSeqNo;
miguel@23885
   970
              v_OrderLine:=get_uuid();
miguel@23885
   971
              INSERT INTO c_orderline
miguel@23885
   972
                (
miguel@23885
   973
                  c_orderline_id, ad_client_id, ad_org_id, isactive, created, createdby,
miguel@23885
   974
                  updated, updatedby, c_order_id, line, c_bpartner_id, c_bpartner_location_id,
miguel@23885
   975
                  dateordered, datepromised, datedelivered, dateinvoiced, description,
miguel@23885
   976
                  m_product_id, m_warehouse_id, directship, c_uom_id, qtyordered,
miguel@23885
   977
                  qtyreserved, qtydelivered, qtyinvoiced, m_shipper_id, c_currency_id,
miguel@23885
   978
                  pricelist, priceactual, pricelimit, linenetamt, discount, freightamt,
miguel@23885
   979
                  c_charge_id, chargeamt, c_tax_id, s_resourceassignment_id, ref_orderline_id,
miguel@23885
   980
                  m_attributesetinstance_id, isdescription, quantityorder, m_product_uom_id,
miguel@23885
   981
                  m_offer_id, pricestd, C_ORDER_DISCOUNT_ID,
miguel@23885
   982
                  gross_unit_price, taxbaseamt,line_gross_amount
miguel@23885
   983
                )
miguel@23885
   984
              VALUES
miguel@23885
   985
                (
miguel@23885
   986
                  v_OrderLine, v_Client_ID, v_Org_ID, 'Y', now(), v_UpdatedBy,
miguel@23885
   987
                  now(), v_UpdatedBy, v_Record_ID, v_OrderLineSeqNo, NULL, NULL,
miguel@23885
   988
                  now(), now(), now(), now(), Cur_COrderDiscount.NAME,
miguel@23885
   989
                  Cur_COrderDiscount.M_PRODUCT_ID, v_M_Warehouse_ID, 'N', Cur_COrderDiscount.C_UOM_ID, 1,
miguel@23885
   990
                  0, 0, 0, NULL, v_c_currency_id,
miguel@23885
   991
                  ROUND(v_Discount,v_precision), ROUND(v_Discount,v_precision), ROUND(v_Discount,v_precision), ROUND(v_Discount,v_precision), 0, 0,
miguel@23885
   992
                  NULL, NULL, Cur_TaxDiscount.C_TAX_ID, NULL, NULL,
miguel@23885
   993
                  NULL, 'N', NULL, NULL,
miguel@23885
   994
                  NULL, ROUND(v_Discount,v_precision), NULL,
miguel@23885
   995
                  ROUND(v_gross_unit_price, v_precision), ROUND(v_Discount,v_precision),ROUND(v_line_gross_amount, v_precision)
miguel@23885
   996
                );
miguel@23885
   997
miguel@23885
   998
              UPDATE C_ORDERLINE
miguel@23885
   999
              SET C_ORDER_DISCOUNT_ID=Cur_COrderDiscount.C_ORDER_DISCOUNT_ID
miguel@23885
  1000
              WHERE C_ORDERLINE_ID=v_OrderLine;
miguel@23885
  1001
            ELSE
miguel@23885
  1002
              UPDATE C_ORDERLINE 
miguel@23885
  1003
              SET pricelist = ROUND(v_Discount,v_precision), priceactual = ROUND(v_Discount,v_precision), pricelimit = ROUND(v_Discount,v_precision), linenetamt = ROUND(v_Discount,v_precision), pricestd = ROUND(v_Discount,v_precision)
miguel@23885
  1004
              WHERE C_ORDERLINE.C_ORDER_DISCOUNT_ID = Cur_COrderDiscount.C_ORDER_DISCOUNT_ID
miguel@23885
  1005
                AND C_ORDERLINE.C_TAX_ID = Cur_TaxDiscount.C_TAX_ID;
miguel@23885
  1006
            END IF;
miguel@23885
  1007
          END LOOP;
miguel@23885
  1008
          v_Line:=v_Line + 10;
gorkaion@14379
  1009
        END LOOP;
miguel@23885
  1010
      END IF;
victor@3593
  1011
carlos@0
  1012
      /**
carlos@0
  1013
      * Convert to Target DocType
carlos@0
  1014
      */
carlos@0
  1015
      DECLARE
asier@2586
  1016
        v_DocSubTypeSO_Target VARCHAR(60) ;
carlos@0
  1017
      BEGIN
carlos@0
  1018
        v_ResultStr:='ConvertingDocType';
gorkaion@14379
  1019
        IF (v_DocType_ID <> v_DocTypeTarget_ID) THEN
carlos@0
  1020
          -- New
gorkaion@14379
  1021
          IF (v_DocStatus='DR' OR v_DocType_ID='0') THEN
carlos@0
  1022
            -- Update to Target Document Type
gorkaion@239
  1023
            WHILE(v_DocType_ID<>v_DocTypeTarget_ID)
carlos@0
  1024
            LOOP
gorkaion@14379
  1025
              BEGIN
gorkaion@14379
  1026
                v_ResultStr:='UpdateDocType';
gorkaion@14379
  1027
                UPDATE C_ORDER
gorkaion@14379
  1028
                SET C_DocType_ID=v_DocTypeTarget_ID,
gorkaion@14379
  1029
                    Updated=now(),
gorkaion@14379
  1030
                    UpdatedBy=v_User
gorkaion@14379
  1031
                WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
  1032
                v_DocType_ID:=v_DocTypeTarget_ID;
gorkaion@14379
  1033
              EXCEPTION
gorkaion@14379
  1034
              WHEN OTHERS THEN
gorkaion@14379
  1035
                v_ResultStr:='UpdateDocumentNo';
gorkaion@14379
  1036
                UPDATE C_ORDER  SET DocumentNo=DocumentNo || '.'  WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
  1037
              END;
gorkaion@14379
  1038
            END LOOP;
gorkaion@14379
  1039
          ELSE
gorkaion@14379
  1040
            v_ResultStr:='GetTargetDocType';
gorkaion@14379
  1041
            SELECT DocSubTypeSO
gorkaion@14379
  1042
              INTO v_DocSubTypeSO_Target
gorkaion@14379
  1043
            FROM C_DOCTYPE
gorkaion@14379
  1044
            WHERE C_DocType_ID=v_DocTypeTarget_ID;
gorkaion@14379
  1045
            DBMS_OUTPUT.PUT_LINE('Changing DocType from ' || v_DocSubTypeSO || ' to ' || v_DocSubTypeSO_Target) ;
gorkaion@14379
  1046
            -- Change Offer to anything, Change InProcess to anything
gorkaion@14379
  1047
            IF (v_DocSubTypeSO IN('ON', 'OB') OR v_DocStatus='IP') THEN
gorkaion@14379
  1048
              -- Update to Target Document Type
gorkaion@14379
  1049
              WHILE(v_DocType_ID<>v_DocTypeTarget_ID)
gorkaion@14379
  1050
              LOOP
gorkaion@14379
  1051
                BEGIN
gorkaion@14379
  1052
                  v_ResultStr:='UpdateDocType';
gorkaion@14379
  1053
                  UPDATE C_ORDER
gorkaion@14379
  1054
                  SET C_DocType_ID=v_DocTypeTarget_ID,
gorkaion@14379
  1055
                      Updated=now(),
gorkaion@14379
  1056
                      UpdatedBy=v_User
gorkaion@14379
  1057
                  WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
  1058
                  v_DocType_ID:=v_DocTypeTarget_ID;
gorkaion@14379
  1059
                EXCEPTION
gorkaion@14379
  1060
                WHEN OTHERS THEN
gorkaion@14379
  1061
                  v_ResultStr:='UpdateDocumentNo';
gorkaion@14379
  1062
                  UPDATE C_ORDER  SET DocumentNo=DocumentNo || '.'  WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
  1063
                END;
gorkaion@14379
  1064
              END LOOP;
gorkaion@14379
  1065
            ELSE
gorkaion@14379
  1066
              -- Change Back
carlos@0
  1067
              UPDATE C_ORDER
gorkaion@14379
  1068
                SET C_DocTypeTarget_ID=v_DocType_ID
gorkaion@14379
  1069
              WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
  1070
              RAISE_APPLICATION_ERROR(-20000, '@CannotChangeDocType@') ;
gorkaion@14379
  1071
            END IF;
gorkaion@14379
  1072
          END IF;
gorkaion@14379
  1073
        END IF; -- C_DocType_ID <> C_DocTypeTarget_ID
gorkaion@14379
  1074
      END; -- Conversion
gorkaion@14379
  1075
gorkaion@14379
  1076
    /**
gorkaion@14379
  1077
      * Get DocSubTypeSO + Is it Binding :1:2
gorkaion@14379
  1078
      */
gorkaion@14379
  1079
      v_ResultStr:='TestBinding DocType_ID=' || v_DocType_ID;
gorkaion@14379
  1080
      SELECT CASE DocSubTypeSO WHEN 'ON' THEN 'N' ELSE 'Y' END, DocSubTypeSO
gorkaion@14379
  1081
        INTO v_IsBinding, v_DocSubTypeSO
gorkaion@14379
  1082
      FROM C_DOCTYPE
gorkaion@14379
  1083
      WHERE C_DocType_ID=v_DocType_ID;
gorkaion@14379
  1084
      DBMS_OUTPUT.PUT_LINE('DocSubTypeSO=' || v_DocSubTypeSO || ' IsBinding=' || v_IsBinding) ;
gorkaion@14379
  1085
gorkaion@14379
  1086
    /**************************************************************************
gorkaion@14379
  1087
      * Resolve not-stocked BOMs
gorkaion@14379
  1088
      *************************************************************************/
gorkaion@14379
  1089
      DECLARE
gorkaion@14379
  1090
        -- Order Lines with non-stocked BOMs
gorkaion@14379
  1091
        CUR_BOM_Line RECORD;
gorkaion@14379
  1092
        -- BOM Product List
gorkaion@14379
  1093
        CountNo NUMBER;
gorkaion@14379
  1094
      BEGIN
naiara@19828
  1095
        FOR CUR_BOM_Line IN
naiara@19828
  1096
          (SELECT l.c_orderline_id
naiara@19828
  1097
           FROM C_ORDERLINE l
naiara@19828
  1098
           WHERE l.C_Order_ID=v_Record_ID
naiara@19828
  1099
             AND l.IsActive='Y'
naiara@19828
  1100
             AND l.explode='N'
naiara@19828
  1101
             AND EXISTS
gorkaion@14379
  1102
               (SELECT *
gorkaion@14379
  1103
                FROM M_PRODUCT p
gorkaion@14379
  1104
                WHERE l.M_Product_ID=p.M_Product_ID
gorkaion@14379
  1105
                  AND p.IsBOM='Y'
gorkaion@14379
  1106
                  AND p.IsStocked='N'
gorkaion@14379
  1107
                )
naiara@19828
  1108
              ORDER BY l.Line
gorkaion@14379
  1109
             )
naiara@19828
  1110
        LOOP
naiara@19828
  1111
          M_EXPLODEBOMNOTSTOCK(null, CUR_BOM_Line.c_orderline_ID);
naiara@19828
  1112
        END LOOP;
gorkaion@14379
  1113
      END;
gorkaion@14379
  1114
    /**************************************************************************
gorkaion@14379
  1115
      * Always check and (un) Reserve Inventory  (counterpart: M_InOut_Post)
gorkaion@14379
  1116
      *************************************************************************/
ioritz@18392
  1117
      IF (v_DocAction<>'CL') THEN
ioritz@18392
  1118
        DECLARE
ioritz@18392
  1119
          Cur_ResLine RECORD;
gorkaion@14379
  1120
ioritz@18392
  1121
          v_QtySO       NUMBER; -- Reserved
ioritz@18392
  1122
          v_QtyOrderSO  NUMBER;
ioritz@18392
  1123
          v_QtyPO       NUMBER; -- Ordered
ioritz@18392
  1124
          v_QtyOrderPO  NUMBER;
ioritz@18392
  1125
        BEGIN
ioritz@18392
  1126
          v_ResultStr := 'ReserveInventory';
ioritz@18392
  1127
          -- For all lines needing reservation
ioritz@18392
  1128
          FOR Cur_ResLine IN (SELECT l.M_Warehouse_ID, l.M_Product_ID, l.M_AttributeSetInstance_ID, l.C_OrderLine_ID,
ioritz@18392
  1129
                -- Target Level = 0 if DirectShip='Y' or Binding='N'
ioritz@18392
  1130
                (CASE l.DirectShip WHEN 'Y' THEN 0 ELSE (CASE v_IsBinding WHEN 'N' THEN 0 ELSE l.QtyOrdered END) END)
ioritz@18392
  1131
                 -l.QtyReserved-l.QtyDelivered AS Qty, l.QUANTITYORDER,
ioritz@18392
  1132
                l.QtyReserved, l.QtyDelivered, l.DatePromised, l.C_UOM_ID,
ioritz@18392
  1133
                l.M_PRODUCT_UOM_ID
ioritz@18392
  1134
              FROM C_ORDERLINE l, M_PRODUCT p
ioritz@18392
  1135
              WHERE l.C_Order_ID=v_Record_ID
ioritz@18392
  1136
                -- Reserve Products (not: services, null products) --
ioritz@18392
  1137
                AND l.M_Product_ID=p.M_Product_ID
ioritz@18392
  1138
                AND p.IsStocked='Y' AND p.ProductType='I'
ioritz@18392
  1139
                -- Target Level = 0 if DirectShip='Y' or Binding='N'
ioritz@18392
  1140
                AND (CASE l.DirectShip WHEN 'Y' THEN 0 ELSE (CASE v_IsBinding WHEN 'N' THEN 0 ELSE l.QtyOrdered END)END)
sandra@25786
  1141
                -l.QtyReserved-l.QtyDelivered <> 0)
ioritz@18392
  1142
          LOOP
ioritz@18392
  1143
          
ioritz@18392
  1144
            -- Qty corrected for SO/PO
ioritz@18392
  1145
            IF (v_DocSubTypeSO IS NOT NULL) THEN
ioritz@18392
  1146
              v_QtySO   := Cur_ResLine.Qty;
ioritz@18392
  1147
              v_QtyOrderSO := NULL;
ioritz@18392
  1148
              IF (Cur_ResLine.QtyReserved = 0 AND Cur_ResLine.QtyDelivered = 0) THEN
ioritz@18392
  1149
                v_QtyOrderSO := Cur_ResLine.QuantityOrder;
ioritz@18392
  1150
              ELSIF (Cur_ResLine.M_Product_UOM_ID IS NOT NULL) THEN
ioritz@18392
  1151
                v_QtyOrderSO := C_Uom_Convert(v_QtySO, Cur_ResLine.C_UOM_ID, Cur_ResLine.M_Product_UOM_ID, 'Y');
ioritz@18392
  1152
              END IF;
ioritz@18392
  1153
              v_QtyPO   := 0;
ioritz@18392
  1154
              v_QtyOrderPO := NULL;
ioritz@18392
  1155
            ELSE -- PO
ioritz@18392
  1156
              v_QtySO := 0;
ioritz@18392
  1157
              v_QtyOrderSO := NULL;
ioritz@18392
  1158
              v_QtyPO := Cur_ResLine.Qty;
ioritz@18392
  1159
              v_QtyOrderPO := NULL;
ioritz@18392
  1160
              IF (Cur_ResLine.QtyReserved = 0 AND Cur_ResLine.QtyDelivered = 0) THEN
ioritz@18392
  1161
                v_QtyOrderPO := Cur_ResLine.QuantityOrder;
ioritz@18392
  1162
              ELSIF (Cur_ResLine.M_Product_UOM_ID IS NOT NULL) THEN
ioritz@18392
  1163
                v_QtyOrderPO := C_Uom_Convert(v_QtyPO, Cur_ResLine.C_UOM_ID, Cur_ResLine.M_Product_UOM_ID, 'Y');
ioritz@18392
  1164
              END IF;
gorkaion@14379
  1165
            END IF;
jon@21226
  1166
            IF ((v_DocStatus<>'IP' OR v_DocAction<>'CO') AND COALESCE(v_DocSubTypeSO, '')<>'OB') THEN
ioritz@18392
  1167
              M_UPDATE_STORAGE_PENDING(v_Client_ID, v_Org_ID, v_UpdatedBy, Cur_ResLine.M_Product_ID, Cur_ResLine.M_Warehouse_ID, Cur_ResLine.M_AttributeSetInstance_ID,
ioritz@18392
  1168
              Cur_ResLine.C_UOM_ID, Cur_ResLine.M_PRODUCT_UOM_ID, v_QtySO, v_QtyOrderSO, v_QtyPO, v_QtyOrderPO);
gorkaion@14379
  1169
            END IF;
ioritz@18392
  1170
            DBMS_OUTPUT.PUT_LINE('Reserved Warehouse=' || Cur_ResLine.M_Warehouse_ID || ', Product=' || Cur_ResLine.M_Product_ID || ', Attrib=' || Cur_ResLine.M_AttributeSetInstance_ID || ', Qty=' || v_QtySO || '/' || v_QtyPO);
gorkaion@14379
  1171
ioritz@18392
  1172
            -- Update Order Line
ioritz@18392
  1173
            IF (v_DocSubTypeSO IS NOT NULL) THEN
ioritz@18392
  1174
              UPDATE C_ORDERLINE
ioritz@18392
  1175
              SET QtyReserved = QtyReserved + v_QtySO
ioritz@18392
  1176
              WHERE C_OrderLine_ID = Cur_ResLine.C_OrderLine_ID;
gorkaion@14379
  1177
            END IF;
ioritz@18392
  1178
             rowcount:=SQL%ROWCOUNT;
ioritz@18392
  1179
            IF (rowcount <> 1) THEN
ioritz@18392
  1180
              IF (p_PInstance_ID IS NOT NULL) THEN
ioritz@18392
  1181
                ROLLBACK;
ioritz@18392
  1182
                v_ResultStr := 'LockingOrder';
ioritz@18392
  1183
                UPDATE C_ORDER
ioritz@18392
  1184
                SET Processing = 'N',
ioritz@18392
  1185
                Updated=now(),
ioritz@18392
  1186
                UpdatedBy=v_User
ioritz@18392
  1187
                WHERE C_Order_ID = v_Record_ID;
ioritz@18392
  1188
                RAISE NO_DATA_FOUND;
ioritz@18392
  1189
                COMMIT;
ioritz@18392
  1190
              END IF;
ioritz@18392
  1191
              RAISE_APPLICATION_ERROR(-20000, '@20011@');
ioritz@18392
  1192
            END IF;
ioritz@18392
  1193
          END LOOP; -- For all lines needing reservation
ioritz@18392
  1194
        END;
ioritz@18392
  1195
      END IF; -- Reserve Inventory
javier@17550
  1196
gorkaion@18662
  1197
     /**************************************************************************
gorkaion@18717
  1198
      * Stock Reservations management.
gorkaion@18662
  1199
      *************************************************************************/
gorkaion@18749
  1200
      SELECT COUNT(1) INTO v_count
gorkaion@18749
  1201
      FROM ad_preference
gorkaion@18749
  1202
      WHERE property = 'StockReservations';
gorkaion@18749
  1203
      IF (v_count > 0) THEN
gorkaion@18749
  1204
        IF (v_count > 1) THEN
gorkaion@18749
  1205
          v_dummy := AD_GET_PREFERENCE_VALUE('StockReservations', 'Y', v_client_id, v_org_id, NULL, NULL, NULL);
gorkaion@18749
  1206
        END IF;
gorkaion@18749
  1207
        DECLARE
gorkaion@18749
  1208
          v_reservation_id      VARCHAR2(32);
gorkaion@18749
  1209
          v_quantity            NUMBER;
gorkaion@18749
  1210
          v_reservedqty         NUMBER;
gorkaion@18749
  1211
          v_releasedqty         NUMBER;
gorkaion@18749
  1212
          v_allocated           NUMBER;
gorkaion@18749
  1213
          v_pendingtounreserve  NUMBER;
gorkaion@18749
  1214
          v_qtyaux              NUMBER;
gorkaion@18749
  1215
          v_res_status          M_RESERVATION.RES_STATUS%TYPE;
gorkaion@18749
  1216
          v_linecount           NUMBER;
gorkaion@18749
  1217
          v_creservedcount      NUMBER;
gorkaion@18749
  1218
          v_preservedcount      NUMBER;
gorkaion@18749
  1219
          
gorkaion@18749
  1220
          cur_res_stock         RECORD;
gorkaion@18749
  1221
        BEGIN
gorkaion@18749
  1222
          IF (v_issotrx = 'Y') THEN
gorkaion@18749
  1223
            FOR cur_orderline IN (
gorkaion@18749
  1224
                SELECT ol.c_orderline_id, ol.create_reservation, ol.qtyordered
gorkaion@18749
  1225
                FROM c_orderline ol
gorkaion@18749
  1226
                    JOIN m_product p ON ol.m_product_id = p.m_product_id
carlos@20772
  1227
                    LEFT JOIN m_reservation r ON ol.c_orderline_id = r.c_orderline_id
gorkaion@18749
  1228
                WHERE ol.c_order_id = v_record_id
carlos@20772
  1229
                  AND ((
carlos@20772
  1230
                      ol.qtyordered > 0
carlos@20772
  1231
                      AND p.isstocked = 'Y'
carlos@20772
  1232
                      AND p.producttype = 'I'
carlos@20772
  1233
                    ) OR (
carlos@20772
  1234
                      r.m_reservation_id IS NOT NULL
carlos@20772
  1235
                    )
carlos@20772
  1236
                  )
gorkaion@18749
  1237
            ) LOOP
gorkaion@18749
  1238
              SELECT count(*), max(m_reservation_id)
gorkaion@18749
  1239
                INTO v_aux, v_reservation_id
gorkaion@18749
  1240
              FROM m_reservation
gorkaion@18749
  1241
              WHERE c_orderline_id = cur_orderline.c_orderline_id;
gorkaion@18749
  1242
              -- Initialize so_res_status
gorkaion@18749
  1243
              UPDATE c_orderline
gorkaion@18749
  1244
              SET so_res_status = 'NR'
gorkaion@18749
  1245
              WHERE c_orderline_id = cur_orderline.c_orderline_id;
gorkaion@18749
  1246
              IF (v_aux > 1) THEN
gorkaion@18749
  1247
                RAISE_APPLICATION_ERROR(-20000, '@SOLineWithMoreThanOneOpenReservation@');
gorkaion@18749
  1248
              ELSIF (v_aux = 1) THEN
gorkaion@18749
  1249
                -- Update reservation when possible.
gorkaion@18749
  1250
                -- Read reservation.
gorkaion@18749
  1251
                SELECT r.quantity, r.reservedqty, r.releasedqty, r.res_status,
gorkaion@18749
  1252
                      COALESCE(SUM(CASE rs.isallocated WHEN 'Y' THEN rs.quantity - COALESCE(rs.releasedqty, 0) ELSE 0 END), 0)
gorkaion@18749
  1253
                  INTO v_quantity, v_reservedqty, v_releasedqty, v_res_status,
gorkaion@18749
  1254
                      v_allocated
gorkaion@18749
  1255
                FROM m_reservation r
gorkaion@18749
  1256
                    LEFT JOIN m_reservation_stock rs ON r.m_reservation_id = rs.m_reservation_id
gorkaion@18749
  1257
                WHERE r.m_reservation_id = v_reservation_id
gorkaion@18749
  1258
                GROUP BY r.quantity, r.reservedqty, r.releasedqty, r.res_status;
gorkaion@18749
  1259
                IF (v_quantity != cur_orderline.qtyordered) THEN
m@28978
  1260
                  IF (v_allocated <> 0) THEN
gorkaion@18749
  1261
                    RAISE_APPLICATION_ERROR(-20000, '@ThereIsMoreAllocatedQtyThanOrdered@');
gorkaion@18749
  1262
                  END IF;
gorkaion@18749
  1263
                  IF (cur_orderline.qtyordered < v_releasedqty) THEN
gorkaion@18749
  1264
                    RAISE_APPLICATION_ERROR(-20000, '@CannotOrderLessThanReleasedQty@');
gorkaion@18749
  1265
                  END IF;
gorkaion@18749
  1266
                  IF (cur_orderline.qtyordered < v_reservedqty) THEN
gorkaion@18749
  1267
                    --Reservation quantity to decrease with more reserved quantity than new quantity, unreserve stock
gorkaion@18749
  1268
                    v_pendingtounreserve := v_reservedqty - cur_orderline.qtyordered;
gorkaion@18749
  1269
                    FOR cur_res_stock IN (
gorkaion@18749
  1270
                        SELECT m_reservation_stock_id, quantity - COALESCE(releasedqty, 0) AS reservedqty
gorkaion@18749
  1271
                        FROM m_reservation_stock
gorkaion@18749
  1272
                        WHERE m_reservation_id = v_reservation_id
gorkaion@18749
  1273
                        ORDER BY COALESCE(releasedqty, 0), quantity - COALESCE(releasedqty, 0)
gorkaion@18749
  1274
                    ) LOOP
gorkaion@18749
  1275
                      v_qtyaux := LEAST(v_pendingtounreserve, cur_res_stock.reservedqty);
gorkaion@18749
  1276
                      UPDATE m_reservation_stock
gorkaion@18749
  1277
                      SET quantity = quantity - v_qtyaux,
gorkaion@18749
  1278
                          updated = now(),
gorkaion@18749
  1279
                          updatedby = v_user
gorkaion@18749
  1280
                      WHERE m_reservation_stock_id = cur_res_stock.m_reservation_stock_id;
gorkaion@18749
  1281
                      v_reservedqty := v_reservedqty - v_qtyaux;
gorkaion@18749
  1282
                      v_pendingtounreserve := v_pendingtounreserve - v_qtyaux;
gorkaion@18749
  1283
                      IF (v_pendingtounreserve = 0) THEN
gorkaion@18749
  1284
                        EXIT;
gorkaion@18749
  1285
                      END IF;
gorkaion@18749
  1286
                    END LOOP;
gorkaion@18749
  1287
                    IF (v_pendingtounreserve > 0) THEN
gorkaion@18749
  1288
                      RAISE_APPLICATION_ERROR(-20000, '@CouldNotUnreserveNeededQty@');
gorkaion@18749
  1289
                    END IF;
gorkaion@18749
  1290
                    -- Delete reservation lines with quantity zero.
gorkaion@18749
  1291
                    DELETE FROM m_reservation_stock
gorkaion@18749
  1292
                    WHERE quantity = 0
gorkaion@18749
  1293
                      AND COALESCE(releasedqty, 0) = 0
gorkaion@18749
  1294
                      AND m_reservation_id = v_reservation_id;
gorkaion@18749
  1295
                  END IF;
gorkaion@18749
  1296
                  UPDATE m_reservation
gorkaion@18749
  1297
                  SET quantity = cur_orderline.qtyordered,
gorkaion@18749
  1298
                      updated = now(),
gorkaion@18749
  1299
                      updatedby = v_user
gorkaion@18749
  1300
                  WHERE m_reservation_id = v_reservation_id;
gorkaion@18749
  1301
                  IF (v_quantity < cur_orderline.qtyordered AND v_res_status = 'CO') THEN
gorkaion@18749
  1302
                    --Reservation processed with higher quantity. Try to reserve the new quantity.
gorkaion@18749
  1303
                    M_RESERVE_STOCK_AUTO(v_reservation_id, v_user, v_reservedqty);
gorkaion@18749
  1304
                  END IF;
gorkaion@18662
  1305
                END IF;
gorkaion@18749
  1306
                
gorkaion@18749
  1307
                IF (v_res_status <> 'DR') THEN
gorkaion@18749
  1308
                  -- Update so_res_status
gorkaion@18749
  1309
                  UPDATE c_orderline
gorkaion@18749
  1310
                  SET so_res_status = CASE WHEN cur_orderline.qtyordered = v_reservedqty THEN 'CR'
atul@24210
  1311
					   WHEN cur_orderline.qtyordered > v_reservedqty AND v_reservedqty > 0 THEN 'PR'
atul@24210
  1312
                                           ELSE 'NR'
gorkaion@18749
  1313
                                      END
gorkaion@18749
  1314
                  WHERE c_orderline_id = cur_orderline.c_orderline_id;
gorkaion@18662
  1315
                END IF;
gorkaion@18749
  1316
              ELSIF (cur_orderline.create_reservation = 'CRP') THEN
gorkaion@18749
  1317
                M_CREATE_RESERVE_FROM_SOL(cur_orderline.c_orderline_id, 'Y', v_user, v_reservation_id);
gorkaion@18749
  1318
              ELSIF (cur_orderline.create_reservation = 'CR') THEN
gorkaion@18749
  1319
                M_CREATE_RESERVE_FROM_SOL(cur_orderline.c_orderline_id, 'N', v_user, v_reservation_id);
gorkaion@18662
  1320
              END IF;
gorkaion@18749
  1321
            END LOOP;
gorkaion@18749
  1322
            SELECT COUNT(*), SUM(CASE ol.so_res_status WHEN 'CR' THEN 1 ELSE 0 END), SUM(CASE ol.so_res_status WHEN 'PR' THEN 1 ELSE 0 END)
gorkaion@18749
  1323
              INTO v_linecount, v_creservedcount, v_preservedcount
gorkaion@18749
  1324
            FROM c_orderline ol
gorkaion@18749
  1325
                JOIN m_product p ON ol.m_product_id = p.m_product_id
gorkaion@18749
  1326
            WHERE ol.c_order_id = v_record_id
gorkaion@18749
  1327
              AND ol.qtyordered > 0
gorkaion@18749
  1328
              AND p.isstocked = 'Y'
gorkaion@18749
  1329
              AND p.producttype = 'I';
gorkaion@18749
  1330
            UPDATE c_order
gorkaion@18749
  1331
            SET so_res_status = CASE WHEN v_linecount = v_creservedcount THEN 'CR'
gorkaion@18749
  1332
                                     WHEN v_creservedcount + v_preservedcount > 0 THEN 'PR'
gorkaion@18749
  1333
                                     ELSE 'NR'
gorkaion@18749
  1334
                                END
gorkaion@18749
  1335
            WHERE c_order_id = v_record_id;
gorkaion@18749
  1336
          END IF;
gorkaion@18749
  1337
        END;
gorkaion@18749
  1338
      END IF;
adrianromero@27256
  1339
      
gorkaion@14379
  1340
      -- Synchronize Client/Org Ownership
gorkaion@14379
  1341
      UPDATE C_ORDERLINE
pandeeswari@18807
  1342
      SET AD_Client_ID=v_Client_ID
gorkaion@14379
  1343
      WHERE C_Order_ID=v_Record_ID
pandeeswari@18807
  1344
        AND (AD_Client_ID<>v_Client_ID) ;
gorkaion@14379
  1345
miguel@24752
  1346
      IF (v_docaction = 'CO' AND v_issotrx = 'N') THEN
miguel@24752
  1347
          UPDATE m_transaction
miguel@24752
  1348
          SET checkpricedifference = 'Y'
miguel@24752
  1349
          WHERE m_transaction_id IN (
miguel@24752
  1350
            SELECT trx.m_transaction_id 
miguel@24752
  1351
            FROM c_orderline  ol
miguel@24752
  1352
                 JOIN m_matchpo mpo ON mpo.c_orderline_id = ol.c_orderline_id
miguel@24752
  1353
                 JOIN m_transaction trx ON mpo.m_inoutline_id = trx.m_inoutline_id
miguel@24752
  1354
            WHERE trx.iscostcalculated = 'Y' AND  ol.c_order_id = v_record_id);
miguel@24752
  1355
      END IF;
miguel@24752
  1356
gorkaion@14379
  1357
    /**************************************************************************
gorkaion@14379
  1358
     * Order Complete:5 - Something to do:6
gorkaion@14379
  1359
     */
gorkaion@14379
  1360
      BEGIN
gorkaion@14379
  1361
        v_ResultStr:='OrderCompleteCheck';
victor@24222
  1362
        SELECT COUNT(*) INTO ToDeliverOrToInvoice FROM DUAL
victor@24157
  1363
        WHERE 0 <> ANY (select QtyOrdered - QtyDelivered from c_orderline where c_order_id = v_Record_ID)
victor@24157
  1364
        OR    0 <> ANY (select QtyOrdered - QtyInvoiced  from c_orderline where c_order_id = v_Record_ID);
victor@24222
  1365
        -- If something to deliver or to invoice, then ToDeliverOrToInvoice = 1
victor@24222
  1366
        IF (ToDeliverOrToInvoice = 0) THEN
gorkaion@14379
  1367
          DBMS_OUTPUT.PUT_LINE('OrderComplete') ;
gorkaion@14379
  1368
          IF (v_DocAction='CL') THEN
gorkaion@14379
  1369
            END_PROCESSING:=TRUE;
gorkaion@14379
  1370
          ELSIF (v_DocAction='VO') THEN
gorkaion@14379
  1371
            UPDATE C_ORDER
gorkaion@14379
  1372
            SET DocStatus='VO',
gorkaion@14379
  1373
                DocAction='--',
gorkaion@14379
  1374
                Processed='Y',
harikrishnan@7209
  1375
                Updated=now(),
gorkaion@14379
  1376
                UpdatedBy=v_User
gorkaion@14379
  1377
            WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
  1378
            END_PROCESSING:=TRUE;
gorkaion@14379
  1379
          ELSE
gorkaion@14379
  1380
            UPDATE C_ORDER
gorkaion@14379
  1381
            SET DocStatus='CO',
gorkaion@14379
  1382
                DocAction='--',
gorkaion@14379
  1383
                Processed='Y',
gorkaion@14379
  1384
                Updated=now(),
gorkaion@14379
  1385
                UpdatedBy=v_User
gorkaion@14379
  1386
            WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
  1387
            END_PROCESSING:=TRUE;
gorkaion@14379
  1388
          END IF;
gorkaion@14379
  1389
          IF (NOT END_PROCESSING) THEN
gorkaion@14379
  1390
            RAISE_APPLICATION_ERROR(-20000, '@AlreadyPosted@');
gorkaion@14379
  1391
          END IF;--END_PROCESSING
gorkaion@14379
  1392
        END IF;
gorkaion@14379
  1393
      END;
gorkaion@14379
  1394
    END IF;--FINISH_PROCESS
gorkaion@14379
  1395
    IF (NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
gorkaion@14379
  1396
     /**
gorkaion@14379
  1397
      * In Progress -----------------------------------------------------------
gorkaion@14379
  1398
      */
gorkaion@14379
  1399
      UPDATE C_ORDER
gorkaion@14379
  1400
      SET DocStatus='IP',
gorkaion@14379
  1401
          DateAcct=DateOrdered,
gorkaion@14379
  1402
          Updated=now(),
gorkaion@14379
  1403
          UpdatedBy=v_User
gorkaion@14379
  1404
      WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
  1405
      IF (p_PInstance_ID IS NOT NULL) THEN
gorkaion@14379
  1406
        COMMIT;
gorkaion@14379
  1407
      END IF;
gorkaion@14379
  1408
gorkaion@14379
  1409
    /**
gorkaion@14379
  1410
      * Finished with processing
gorkaion@14379
  1411
      */
gorkaion@14379
  1412
      IF (v_DocAction='PR') THEN
gorkaion@14379
  1413
        v_ResultStr:='FinishProcessing';
gorkaion@14379
  1414
        UPDATE C_ORDER
gorkaion@14379
  1415
        SET DocStatus='IP',
gorkaion@14379
  1416
            DocAction='CO',
gorkaion@14379
  1417
            Processed='N',
gorkaion@14379
  1418
            Updated=now(),
gorkaion@14379
  1419
            UpdatedBy=v_User
gorkaion@14379
  1420
        WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
  1421
        -- C_Order_PickList(NULL, v_Record_ID);  -- Print PickList
gorkaion@14379
  1422
        END_PROCESSING:=TRUE;
gorkaion@14379
  1423
      END IF;
gorkaion@14379
  1424
    END IF;--FINISH_PROCESS
gorkaion@14379
  1425
    IF (NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
gorkaion@14379
  1426
    /**************************************************************************
gorkaion@14379
  1427
      * Prepayment Order  Create Invoice
gorkaion@14379
  1428
      *************************************************************************/
gorkaion@14379
  1429
      IF (v_DocSubTypeSO='PR' AND v_DocStatus<>'WP') THEN
gorkaion@14379
  1430
        DBMS_OUTPUT.PUT_LINE('Create PreInvoice - ' || v_Record_ID) ;
gorkaion@14379
  1431
        v_ResultStr:='CreatePreInvoice';
gorkaion@14379
  1432
        C_Invoice_Create(NULL, Invoice_ID, v_Record_ID) ;
gorkaion@14379
  1433
        DBMS_OUTPUT.PUT_LINE('  PreInvoice - ' || Invoice_ID) ;
gorkaion@14379
  1434
        IF (Invoice_ID='0') THEN
gorkaion@14379
  1435
          RAISE_APPLICATION_ERROR(-20000, '@PreInvoiceCreateFailed@');
gorkaion@14379
  1436
        END IF;
gorkaion@14379
  1437
        C_INVOICE_POST(NULL, Invoice_ID) ;
gorkaion@14379
  1438
        --
gorkaion@14379
  1439
        UPDATE C_ORDER
gorkaion@14379
  1440
        SET DocStatus='WP',
gorkaion@14379
  1441
            DocAction='--',
gorkaion@14379
  1442
            Processed='Y',
gorkaion@14379
  1443
            Updated=now(),
gorkaion@14379
  1444
            UpdatedBy=v_User
gorkaion@14379
  1445
        WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
  1446
        --
gorkaion@14379
  1447
        END_PROCESSING:=TRUE;
gorkaion@14379
  1448
      END IF;
gorkaion@14379
  1449
      IF (NOT END_PROCESSING) THEN
gorkaion@14379
  1450
       /**
gorkaion@14379
  1451
        * Deliver Direct Shipments
gorkaion@14379
  1452
        */
gorkaion@14379
  1453
        v_ResultStr:='NonInventoryDelivery';
gorkaion@14379
  1454
        UPDATE C_ORDERLINE
gorkaion@14379
  1455
        SET QtyDelivered=QtyOrdered
gorkaion@14379
  1456
        WHERE DirectShip='Y'
gorkaion@14379
  1457
          AND C_Order_ID=v_Record_ID;
gorkaion@14379
  1458
      END IF;--END_PROCESSING
gorkaion@14379
  1459
    END IF;--FINISH_PROCESS
gorkaion@14379
  1460
    IF (NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
gorkaion@14379
  1461
     /**************************************************************************
gorkaion@14379
  1462
      * Will-Call + Walk In Processing
gorkaion@14379
  1463
      * --
gorkaion@14379
  1464
      * (W)illCall(I)nvoice - (W)illCall(P)ickup - (W)alkIn(R)eceipt
gorkaion@14379
  1465
      * --
gorkaion@14379
  1466
      *************************************************************************/
gorkaion@14379
  1467
      IF (v_DocSubTypeSO IN('WI', 'WP', 'WR')) THEN
gorkaion@14379
  1468
       /************
gorkaion@14379
  1469
        * Shipment
gorkaion@14379
  1470
        */
gorkaion@14379
  1471
        DBMS_OUTPUT.PUT_LINE('Create Shipment - ' || v_Record_ID) ;
gorkaion@14379
  1472
        v_ResultStr:='CreateShipment';
gorkaion@14379
  1473
gorkaion@14379
  1474
        M_Inout_Create(NULL, InOut_ID, v_Record_ID, NULL, 'Y') ; -- Force Delivery
gorkaion@14379
  1475
gorkaion@14379
  1476
        DBMS_OUTPUT.PUT_LINE('  Shipment - ' || InOut_ID) ;
gorkaion@14379
  1477
        IF (InOut_ID='0') THEN
gorkaion@14379
  1478
          RAISE_APPLICATION_ERROR(-20000, '@InOutCreateFailed@');
carlos@0
  1479
        ELSE
gorkaion@14379
  1480
          SELECT documentno
gorkaion@14379
  1481
            INTO v_DocumentNo
gorkaion@14379
  1482
          FROM M_INOUT
gorkaion@14379
  1483
          WHERE M_INOUT_ID = InOut_ID;
gorkaion@14379
  1484
          v_Message:='@InoutDocumentno@ ' || v_DocumentNo || ' @beenCreated@';
gorkaion@14379
  1485
        END IF;
gorkaion@14379
  1486
        IF (v_DocSubTypeSO IN('WI', 'WR')) THEN
gorkaion@14379
  1487
         /************
gorkaion@14379
  1488
          * Invoice
gorkaion@14379
  1489
          */
gorkaion@14379
  1490
          DBMS_OUTPUT.PUT_LINE('Create Invoice - ' || v_Record_ID) ;
gorkaion@14379
  1491
          v_ResultStr:='CreateInvoice';
gorkaion@14379
  1492
          C_Invoice_Create(NULL, Invoice_ID, v_Record_ID) ;
gorkaion@14379
  1493
          DBMS_OUTPUT.PUT_LINE('  Invoice - ' || Invoice_ID) ;
gorkaion@14379
  1494
          IF (Invoice_ID IS NULL OR Invoice_ID='0') THEN
gorkaion@14379
  1495
            RAISE_APPLICATION_ERROR(-20000, '@InvoiceCreateFailed@');
gorkaion@14379
  1496
          ELSE
gorkaion@14379
  1497
            SELECT documentno
gorkaion@14379
  1498
              INTO v_DocumentNo
gorkaion@14379
  1499
            FROM C_INVOICE
gorkaion@14379
  1500
            WHERE C_INVOICE_ID = Invoice_ID;
gorkaion@14379
  1501
            v_Message:=v_Message||' , '||'@InvoiceDocumentno@ ' || v_DocumentNo || ' @invbeenCreated@';
gorkaion@14379
  1502
          END IF;
carlos@0
  1503
        END IF;
carlos@0
  1504
      END IF;
carlos@0
  1505
gorkaion@14379
  1506
     /**
gorkaion@14379
  1507
      * Final Completeness check
gorkaion@14379
  1508
      */
victor@24222
  1509
      SELECT COUNT(*) INTO ToDeliver FROM DUAL
victor@24157
  1510
      WHERE 0 <> ANY (select QtyOrdered - QtyDelivered from c_orderline where c_order_id = v_Record_ID);
victor@24222
  1511
      SELECT COUNT(*) INTO ToInvoice FROM DUAL
victor@24157
  1512
      WHERE 0 <> ANY (select QtyOrdered - QtyInvoiced from c_orderline where c_order_id = v_Record_ID);
gorkaion@14379
  1513
      DBMS_OUTPUT.PUT_LINE('To deliver - ' || ToDeliver) ;
gorkaion@14379
  1514
      DBMS_OUTPUT.PUT_LINE('ToInvoice - ' || ToInvoice) ;
gorkaion@14379
  1515
      DBMS_OUTPUT.PUT_LINE('v_DocSubTypeSO - ' || v_DocSubTypeSO) ;
gorkaion@14379
  1516
      -- Nothing to Deliver + Invoice for (W)illCall(I)nvoice and (W)alkIn(R)eceipt
gorkaion@14379
  1517
      IF (v_DocSubTypeSO IN ('WI', 'WR') AND ToDeliver=0 AND ToInvoice=0) THEN
carlos@0
  1518
        UPDATE C_ORDER
gorkaion@14379
  1519
        SET DocStatus='CO',
gorkaion@14379
  1520
            DocAction='--',
gorkaion@14379
  1521
            IsDelivered='Y',
gorkaion@14379
  1522
            IsInvoiced='Y',
gorkaion@14379
  1523
            Processed='Y',
gorkaion@14379
  1524
            Updated=now(),
gorkaion@14379
  1525
            UpdatedBy=v_User
carlos@0
  1526
        WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
  1527
        DBMS_OUTPUT.PUT_LINE('DocAction - ' || v_DocAction) ;
gorkaion@14379
  1528
        IF (v_DocAction='VO') THEN
gorkaion@14379
  1529
          UPDATE C_ORDER  SET DocStatus='VO'  WHERE C_Order_ID=v_Record_ID;
carlos@0
  1530
        END IF;
carlos@0
  1531
      END IF;
gorkaion@14379
  1532
      -- Nothing to Deliver for (W)illCall(P)ickup (Invoice generated independently)
gorkaion@14379
  1533
      IF (v_DocSubTypeSO='WP' AND ToDeliver=0) THEN
gorkaion@14379
  1534
        UPDATE C_ORDER
gorkaion@14379
  1535
        SET DocStatus='CO',
gorkaion@14379
  1536
            DocAction='--',
gorkaion@14379
  1537
            IsDelivered='Y',
gorkaion@14379
  1538
            Processed='Y',
gorkaion@14379
  1539
            Updated=now(),
gorkaion@14379
  1540
            UpdatedBy=v_User
gorkaion@14379
  1541
        WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
  1542
        IF (v_DocAction='VO') THEN
gorkaion@14379
  1543
          UPDATE C_ORDER  SET DocStatus='VO'  WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
  1544
        END IF;
gorkaion@14379
  1545
      END IF;
gorkaion@14379
  1546
      -- We are done with standard sales orders
gorkaion@14545
  1547
      IF (v_DocSubTypeSO = 'RM' OR v_isreturndoctype = 'Y') THEN
harikrishnan@5813
  1548
gorkaion@14379
  1549
        FOR Cur_Order IN( SELECT ol.qtyordered, ol.c_order_discount_id FROM C_order o,C_orderline ol
gorkaion@14379
  1550
            WHERE o.C_Order_ID = v_Record_ID
gorkaion@14379
  1551
              AND ol.C_Order_ID = o.C_Order_ID)
carlos@0
  1552
        LOOP
gorkaion@14379
  1553
          IF (Cur_Order.qtyordered >0 AND Cur_Order.c_order_discount_id IS NULL) THEN 
gorkaion@14379
  1554
            RAISE_APPLICATION_ERROR(-20000, '@ReturnMaterialOrderType@') ;
carlos@0
  1555
          END IF;
carlos@0
  1556
        END LOOP;
carlos@0
  1557
      END IF;
gorkaion@14540
  1558
      IF (v_DocAction IN('CO', 'CL', 'VO') AND v_DocSubTypeSO IN('SO','RM')) THEN
gorkaion@14379
  1559
        UPDATE C_ORDER
gorkaion@14379
  1560
        SET DocStatus='CO',
gorkaion@14379
  1561
            DocAction='--',
gorkaion@14379
  1562
            Processed='Y',
gorkaion@14379
  1563
            Updated=now(),
gorkaion@14379
  1564
            UpdatedBy=v_User
gorkaion@14379
  1565
        WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
  1566
      END IF;
gorkaion@14379
  1567
      -- Purchase Orders
gorkaion@14379
  1568
      IF (v_DocAction IN('CO', 'CL', 'VO') AND v_DocSubTypeSO IS NULL) THEN
gorkaion@14379
  1569
        UPDATE C_ORDER
gorkaion@14379
  1570
        SET DocStatus='CO',
gorkaion@14379
  1571
            DocAction='--',
gorkaion@14379
  1572
            Processed='Y',
gorkaion@14379
  1573
            Updated=now(),
gorkaion@14379
  1574
            UpdatedBy=v_User
gorkaion@14379
  1575
        WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
  1576
      END IF;
david@18487
  1577
      IF (v_DocAction IN('CO') AND v_DocSubTypeSO IN('OB')) THEN
david@18487
  1578
        UPDATE C_ORDER
david@18487
  1579
        SET DocStatus='UE',
david@18487
  1580
            DocAction='--',
david@18487
  1581
            Processed='Y',
david@18487
  1582
            Updated=now(),
david@18487
  1583
            UpdatedBy=v_User
david@18487
  1584
        WHERE C_Order_ID=v_Record_ID;
david@18487
  1585
      END IF;
gorkaion@14379
  1586
      -- Only create cash entry if docAction is Complete
gorkaion@14379
  1587
      IF (v_DocAction NOT IN('CO')) THEN
gorkaion@14379
  1588
        END_PROCESSING:=TRUE;
gorkaion@14379
  1589
      END IF;
gorkaion@14379
  1590
    END IF;--FINISH_PROCESS
gorkaion@14379
  1591
    IF (NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
gorkaion@14379
  1592
     /**************************************************************************
gorkaion@14379
  1593
      * Create default Cash entry
carlos@0
  1594
      *************************************************************************/
carlos@0
  1595
      DECLARE
gorkaion@14379
  1596
        v_PaymentRule VARCHAR(60) ;
gorkaion@14379
  1597
        CUR_CB RECORD;
gorkaion@14379
  1598
        v_debtPaymentID varchar2(32);
gorkaion@14379
  1599
        v_totalCash NUMBER;
gorkaion@14379
  1600
        v_CB_Curr varchar2(32);
carlos@0
  1601
      BEGIN
gorkaion@14379
  1602
       /* ALO
gorkaion@14379
  1603
        */
gorkaion@14379
  1604
        UPDATE C_DEBT_PAYMENT  SET IsValid='Y'  WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
  1605
        SELECT C_ORDER.PAYMENTRULE,
unai@14890
  1606
            (CASE 
unai@14890
  1607
		WHEN (length(C_ORDER.DOCUMENTNO||' - '||C_BPARTNER.NAME||' - '||C_ORDER.GRANDTOTAL) > 200) 
unai@14890
  1608
			THEN substr(C_ORDER.DOCUMENTNO||' - '||C_BPARTNER.NAME||' - '||C_ORDER.GRANDTOTAL,1,197)||'...'
unai@14890
  1609
		ELSE 
unai@14890
  1610
			C_ORDER.DOCUMENTNO||' - '||C_BPARTNER.NAME||' - '||C_ORDER.GRANDTOTAL
unai@14890
  1611
	    END) AS CONCATENATION,
gorkaion@14379
  1612
            C_ORDER.GRANDTOTAL,
gorkaion@14379
  1613
            C_ORDER.M_WAREHOUSE_ID
gorkaion@14379
  1614
          INTO v_PaymentRule,
gorkaion@14379
  1615
            v_DocumentNo,
gorkaion@14379
  1616
            v_GrandTotal,
gorkaion@14379
  1617
            v_M_Warehouse_ID
gorkaion@14379
  1618
        FROM C_ORDER, C_BPARTNER
gorkaion@14379
  1619
        WHERE C_ORDER.C_BPARTNER_ID=C_BPARTNER.C_BPARTNER_ID
gorkaion@14379
  1620
          AND C_ORDER_ID=v_Record_ID;
gorkaion@14379
  1621
        SELECT MAX(NAME)
gorkaion@14379
  1622
          INTO v_WarehouseName
gorkaion@14379
  1623
        FROM M_WAREHOUSE
gorkaion@14379
  1624
        WHERE M_WAREHOUSE_ID=v_M_Warehouse_ID;
gorkaion@14379
  1625
gorkaion@14379
  1626
        --PaymentRule C, WI and WR have already created DP, this won't be inserted in cashline
gorkaion@14379
  1627
        --because it will processed with the invoice.
gorkaion@14379
  1628
        IF (v_PaymentRule='C') AND (v_DocSubTypeSO NOT IN ('WI', 'WR') OR v_DocSubTypeSO IS NULL) THEN
gorkaion@14379
  1629
          IF (v_CashLine_ID IS NULL OR v_CashLine_ID='0') THEN
gorkaion@14379
  1630
            -- Create CashLine
gorkaion@14379
  1631
            -- Find Defaylt CashBook
gorkaion@14379
  1632
            v_ResultStr:='Find C_CashBook Org_ID=' || v_Org_ID;
gorkaion@14379
  1633
            BEGIN
gorkaion@14379
  1634
              -- First active default Book of Org
gorkaion@14379
  1635
              FOR CUR_CB IN
gorkaion@14379
  1636
                 (SELECT cb.C_CashBook_ID, c.ISO_Code, cb.NAME, cb.c_currency_id
gorkaion@14379
  1637
                  FROM C_CASHBOOK cb, C_CURRENCY c
gorkaion@14379
  1638
                  WHERE cb.AD_Org_ID=v_Org_ID
gorkaion@14379
  1639
                    AND cb.C_Currency_ID=c.C_Currency_ID
gorkaion@14379
  1640
                    AND cb.IsActive='Y'
gorkaion@14379
  1641
                  ORDER BY cb.IsDefault DESC
gorkaion@14379
  1642
                 )
gorkaion@14379
  1643
              LOOP
gorkaion@14379
  1644
                IF (v_CashBook_ID IS NULL) THEN
gorkaion@14379
  1645
                  v_CashBook_ID:=CUR_CB.C_CashBook_ID;
gorkaion@14379
  1646
                  v_ISO_Code:=CUR_CB.ISO_Code;
gorkaion@14379
  1647
                  v_CB_Curr:=CUR_CB.C_Currency_ID;
gorkaion@14379
  1648
                ELSIF (CUR_CB.NAME=v_WarehouseName) THEN
gorkaion@14379
  1649
                  v_CashBook_ID:=CUR_CB.C_CashBook_ID;
gorkaion@14379
  1650
                  v_ISO_Code:=CUR_CB.ISO_Code;
gorkaion@14379
  1651
                  v_CB_Curr:=CUR_CB.C_Currency_ID;
gorkaion@14379
  1652
                END IF;
gorkaion@14379
  1653
              END LOOP;
gorkaion@14379
  1654
            END;
gorkaion@14379
  1655
            IF (v_CashBook_ID IS NULL) THEN
gorkaion@14379
  1656
              RAISE_APPLICATION_ERROR(-20000, '@CashBookPRSCnotfoundOrg@' || v_Org_Name) ;
gorkaion@14379
  1657
            END IF;
gorkaion@14379
  1658
            DBMS_OUTPUT.PUT_LINE('CashBook_ID=' || v_CashBook_ID) ;
gorkaion@14379
  1659
            -- Find/Create Cash Journal
gorkaion@14379
  1660
            v_ResultStr:='Find C_Cash for ' || v_Date;
gorkaion@14379
  1661
            DECLARE
gorkaion@14379
  1662
              Cur_CashId RECORD;
gorkaion@14379
  1663
            BEGIN
gorkaion@14379
  1664
              FOR Cur_CashId IN
gorkaion@14379
  1665
                 (SELECT C_Cash_ID AS Cash_ID
gorkaion@14379
  1666
                  FROM C_CASH
gorkaion@14379
  1667
                  WHERE C_CashBook_ID=v_CashBook_ID
gorkaion@14379
  1668
                    AND TRUNC(StatementDate)=v_Date
gorkaion@14379
  1669
                    AND Processed='N'
gorkaion@14379
  1670
                 )
gorkaion@14379
  1671
              LOOP
gorkaion@14379
  1672
                v_Cash_ID:=Cur_CashId.Cash_ID;
gorkaion@14379
  1673
                EXIT;
gorkaion@14379
  1674
              END LOOP;
gorkaion@14379
  1675
            EXCEPTION
gorkaion@14379
  1676
              WHEN NO_DATA_FOUND THEN
gorkaion@14379
  1677
                NULL;
gorkaion@14379
  1678
            END;
gorkaion@14379
  1679
           /**************************************************************************
gorkaion@14379
  1680
            * Credit Multiplier
gorkaion@14379
  1681
            *************************************************************************/
gorkaion@14379
  1682
            DECLARE
gorkaion@14379
  1683
              v_DocBaseType C_DOCTYPE.DocBaseType%TYPE;
gorkaion@14379
  1684
            BEGIN
gorkaion@14379
  1685
              -- Is it a Credit Memo?
gorkaion@14379
  1686
              SELECT DocBaseType
gorkaion@14379
  1687
                INTO v_DocBaseType
gorkaion@14379
  1688
              FROM C_DOCTYPE
gorkaion@14379
  1689
              WHERE C_DocType_ID=v_DocType_ID;
gorkaion@14379
  1690
              IF (v_DocBaseType IN('ARC', 'API')) THEN
gorkaion@14379
  1691
                v_Multiplier:=-1;
gorkaion@14379
  1692
              END IF;
gorkaion@14379
  1693
            END;
gorkaion@14379
  1694
gorkaion@14379
  1695
            IF (v_Cash_ID IS NULL) THEN
gorkaion@14379
  1696
              v_ResultStr:='Create C_Cash';
gorkaion@14379
  1697
              Ad_Sequence_Next('C_Cash', v_Org_ID, v_Cash_ID) ;
gorkaion@14379
  1698
              INSERT
gorkaion@14379
  1699
              INTO C_CASH
gorkaion@14379
  1700
                (
gorkaion@14379
  1701
                  C_Cash_ID, AD_Client_ID, AD_Org_ID, IsActive,
gorkaion@14379
  1702
                  Created, CreatedBy, Updated, UpdatedBy,
gorkaion@14379
  1703
                  C_CashBook_ID, NAME, StatementDate, DateAcct,
gorkaion@14379
  1704
                  BeginningBalance, EndingBalance, StatementDifference, Processing,
gorkaion@14379
  1705
                  Processed, Posted
gorkaion@14379
  1706
                )
gorkaion@14379
  1707
              VALUES
gorkaion@14379
  1708
                (
gorkaion@14379
  1709
                  v_Cash_ID, v_Client_ID, v_Org_ID, 'Y',
gorkaion@14379
  1710
                  now(), v_UpdatedBy, now(), v_UpdatedBy,
gorkaion@14379
  1711
                  v_CashBook_ID, TO_CHAR(v_Date, 'YYYY-MM-DD') || ' ' || v_ISO_Code, v_Date, v_Date,
gorkaion@14379
  1712
                  0, 0, 0, 'N',
gorkaion@14379
  1713
                  'N', 'N'
gorkaion@14379
  1714
                )
gorkaion@14379
  1715
              ;
gorkaion@14379
  1716
            END IF;
gorkaion@14379
  1717
           /*
gorkaion@14379
  1718
            Create a debt payment for paymentrule=C
gorkaion@14379
  1719
            Note: for WI and WR we have already created an invoice and its DP, we only have to link it
gorkaion@14379
  1720
            */
gorkaion@14379
  1721
            SELECT COALESCE(SUM(C_Currency_Round(C_Currency_Convert((Amount + WriteOffAmt), C_Currency_ID, v_CB_Curr, v_Date, NULL, v_Client_ID, v_Org_ID), v_c_Currency_ID, NULL)), 0)
gorkaion@14379
  1722
              INTO v_totalCash
gorkaion@14379
  1723
            FROM C_DEBT_PAYMENT_V dp
gorkaion@14379
  1724
            WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
  1725
gorkaion@14379
  1726
            Ad_Sequence_Next('C_Debt_Payment', v_Record_ID, v_debtPaymentID) ;
gorkaion@14379
  1727
            INSERT INTO C_DEBT_PAYMENT
gorkaion@14379
  1728
               (C_DEBT_PAYMENT_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
gorkaion@14379
  1729
                CREATED, CREATEDBY, UPDATED, UPDATEDBY,
gorkaion@14379
  1730
                ISRECEIPT, C_SETTLEMENT_CANCEL_ID, C_SETTLEMENT_GENERATE_ID, DESCRIPTION,
gorkaion@14379
  1731
                C_ORDER_ID, C_BPARTNER_ID, C_CURRENCY_ID, C_CASHLINE_ID,
gorkaion@14379
  1732
                C_BANKACCOUNT_ID, C_CASHBOOK_ID, PAYMENTRULE, ISPAID,
gorkaion@14379
  1733
                AMOUNT, WRITEOFFAMT, DATEPLANNED, ISMANUAL,
gorkaion@14379
  1734
                ISVALID, C_BANKSTATEMENTLINE_ID, CHANGESETTLEMENTCANCEL, CANCEL_PROCESSED,
gorkaion@14379
  1735
                GENERATE_PROCESSED, c_project_id,IsAutomaticGenerated, STATUS_INITIAL)
gorkaion@14379
  1736
            VALUES
gorkaion@14379
  1737
               (v_debtPaymentID, v_Client_ID, v_Org_ID, 'Y',
gorkaion@14379
  1738
                now(), v_UpdatedBy, now(), v_UpdatedBy,
gorkaion@14379
  1739
                v_isSoTrx, NULL, NULL, '',
gorkaion@14379
  1740
                v_Record_ID, v_c_Bpartner_Id, v_c_currency_id, null,
gorkaion@14379
  1741
                NULL, v_CashBook_ID, 'C', 'N',
gorkaion@14379
  1742
                (v_GrandTotal-v_totalCash), 0, v_Date, 'N',
gorkaion@14379
  1743
                'Y', NULL, 'N', 'N',
gorkaion@14379
  1744
                'N', v_C_PROJECT_ID,'Y', 'DE');
gorkaion@14379
  1745
gorkaion@14379
  1746
            DBMS_OUTPUT.PUT_LINE('  Cash_ID=' || v_Cash_ID) ;
gorkaion@14379
  1747
            -- Create CashJournal Line in invoice currency
gorkaion@14379
  1748
            v_ResultStr:='Create C_CashLine';
gorkaion@14379
  1749
            Ad_Sequence_Next('C_CashLine', v_Org_ID, v_CashLine_ID) ;
gorkaion@14379
  1750
gorkaion@14379
  1751
            SELECT COALESCE(MAX(Line), 0) +10
gorkaion@14379
  1752
              INTO v_Line
gorkaion@14379
  1753
            FROM C_CASHLINE
gorkaion@14379
  1754
            WHERE C_Cash_ID=v_Cash_ID;
gorkaion@14379
  1755
            --
gorkaion@14379
  1756
            INSERT
gorkaion@14379
  1757
            INTO C_CASHLINE
gorkaion@14379
  1758
              (
gorkaion@14379
  1759
                C_CashLine_ID, AD_Client_ID, AD_Org_ID, IsActive,
gorkaion@14379
  1760
                Created, CreatedBy, Updated, UpdatedBy,
gorkaion@14379
  1761
                C_Cash_ID, C_Debt_Payment_ID, Line, Description,
gorkaion@14379
  1762
                Amount, CashType, DiscountAmt, WriteOffAmt,
gorkaion@14379
  1763
                IsGenerated
gorkaion@14379
  1764
              )
gorkaion@14379
  1765
            VALUES
gorkaion@14379
  1766
              (
gorkaion@14379
  1767
                v_CashLine_ID, v_Client_ID, v_Org_ID, 'Y',
gorkaion@14379
  1768
                now(), v_UpdatedBy, now(), v_UpdatedBy,
gorkaion@14379
  1769
                v_Cash_ID, v_debtPaymentID, v_Line, v_DocumentNo,
gorkaion@14379
  1770
                (v_GrandTotal-v_totalCash) * (CASE WHEN v_isSoTrx='N' THEN -1 ELSE 1 END), 'P', 0, 0,
gorkaion@14379
  1771
                'Y'
gorkaion@14379
  1772
              )
gorkaion@14379
  1773
            ;
gorkaion@14379
  1774
            DBMS_OUTPUT.PUT_LINE('  CashLine_ID=' || v_CashLine_ID) ;
gorkaion@14379
  1775
gorkaion@14379
  1776
          END IF; -- CashLine_ID IS NULL OR CashLine_ID = '0'
gorkaion@14379
  1777
        END IF; -- v_PaymentRule = 'C'
carlos@0
  1778
      END;
gorkaion@14379
  1779
    END IF;--FINISH_PROCESS
gorkaion@14379
  1780
    IF (NOT FINISH_PROCESS) THEN
gorkaion@14379
  1781
      -- End Processing --------------------------------------------------------
gorkaion@14379
  1782
      --<<END_PROCESSING>>
gorkaion@14379
  1783
      -- Cloase Order
gorkaion@14379
  1784
      IF (v_DocAction='CL') THEN
gorkaion@14379
  1785
        UPDATE C_ORDER
gorkaion@14379
  1786
        SET DocStatus='CL',
gorkaion@14379
  1787
            DocAction='--',
gorkaion@14379
  1788
            Processed='Y'
gorkaion@14379
  1789
        WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
  1790
      END IF;
gorkaion@14379
  1791
    END IF;--FINISH_PROCESS
carlos@0
  1792
gorkaion@14379
  1793
    --C_Order_Post - Finish_Process Extension Point
gorkaion@14379
  1794
    SELECT count(*) INTO v_count
gorkaion@14379
  1795
    FROM DUAL
gorkaion@14379
  1796
    where exists (select 1 from ad_ep_procedures where ad_extension_points_id = 'CB68FC0E8A4547D9943C785761977E77');
gorkaion@14379
  1797
    IF (v_count=1) THEN
gorkaion@14379
  1798
      DECLARE
gorkaion@14379
  1799
        v_ep_instance VARCHAR2(32);
gorkaion@14379
  1800
        v_extension_point_id VARCHAR2(32) := 'CB68FC0E8A4547D9943C785761977E77';
gorkaion@14379
  1801
      BEGIN
gorkaion@14379
  1802
        v_ep_instance := get_uuid();
gorkaion@14379
  1803
        AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'Record_ID',
gorkaion@14379
  1804
          v_record_id, NULL, NULL, NULL, NULL, NULL, NULL);
gorkaion@14379
  1805
        AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'DocAction',
gorkaion@14379
  1806
          v_DocAction, NULL, NULL, NULL, NULL, NULL, NULL);
gorkaion@14379
  1807
        AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'User',
gorkaion@14379
  1808
          v_User, NULL, NULL, NULL, NULL, NULL, NULL);
gorkaion@14379
  1809
        AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'Message',
gorkaion@14379
  1810
          NULL, NULL, NULL, NULL, NULL, NULL, v_Message);
gorkaion@14379
  1811
        AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'Result',
gorkaion@14379
  1812
          NULL, NULL, v_result, NULL, NULL, NULL, NULL);
gorkaion@14379
  1813
        AD_EXTENSION_POINT_HANDLER(v_ep_instance, v_extension_point_id);
gorkaion@14379
  1814
        SELECT p_number INTO v_Result
gorkaion@14379
  1815
        FROM ad_ep_instance_para
gorkaion@14379
  1816
        WHERE ad_ep_instance_id = v_ep_instance
gorkaion@14379
  1817
          AND parametername LIKE 'Result';
gorkaion@14379
  1818
        SELECT p_text INTO v_Message
gorkaion@14379
  1819
        FROM ad_ep_instance_para
gorkaion@14379
  1820
        WHERE ad_ep_instance_id = v_ep_instance
gorkaion@14379
  1821
          AND parametername LIKE 'Message';
carlos@0
  1822
gorkaion@14379
  1823
        DELETE FROM ad_ep_instance_para
gorkaion@14379
  1824
        WHERE ad_ep_instance_id = v_ep_instance;
gorkaion@14379
  1825
      END;
gorkaion@14379
  1826
    END IF;
carlos@0
  1827
gorkaion@14379
  1828
    IF (NOT FINISH_PROCESS) THEN
gorkaion@14379
  1829
      IF (p_PInstance_ID IS NOT NULL) THEN
gorkaion@14379
  1830
        v_ResultStr:='UnLockingOrder';
gorkaion@14379
  1831
        UPDATE C_ORDER
gorkaion@14379
  1832
        SET Processing='N',
gorkaion@14379
  1833
            Updated=now(),
gorkaion@14379
  1834
            UpdatedBy=v_User
gorkaion@14379
  1835
        WHERE C_Order_ID=v_Record_ID;
gorkaion@14379
  1836
        COMMIT;
carlos@0
  1837
      END IF;
gorkaion@14379
  1838
    END IF;--FINISH_PROCESS
carlos@0
  1839
gorkaion@14379
  1840
    --<<FINISH_PROCESS>>
gorkaion@14379
  1841
    IF (p_PInstance_ID IS NOT NULL) THEN
gorkaion@14379
  1842
      --  Update AD_PInstance
gorkaion@14379
  1843
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished - ' || v_Message) ;
gorkaion@14379
  1844
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
gorkaion@14379
  1845
    END IF;
gorkaion@14379
  1846
    RETURN;
gorkaion@14379
  1847
  END; --BODY
carlos@0
  1848
EXCEPTION
carlos@0
  1849
WHEN OTHERS THEN
carlos@0
  1850
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
  1851
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
  1852
  IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
  1853
    ROLLBACK;
carlos@0
  1854
    --Inserted by Carlos Romero 062706
carlos@0
  1855
    UPDATE C_ORDER  SET Processing='N'  WHERE C_Order_ID=v_Record_ID;
carlos@0
  1856
    DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
antonio@735
  1857
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
  1858
  ELSE
carlos@0
  1859
    RAISE;
carlos@0
  1860
  END IF;
carlos@0
  1861
  RETURN;
antonio@735
  1862
END C_ORDER_POST1
gorkaion@239
  1863
]]></body>
carlos@0
  1864
    </function>
adrian@94
  1865
  </database>