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