src-db/database/model/functions/C_ORDER_POST1.xml
author Asier Lostalé <asier.lostale@openbravo.com>
Mon, 05 May 2008 06:59:24 +0000
changeset 799 fef2c5e2feb7
parent 756 ae11e4610537
child 955 cd59d5570056
permissions -rw-r--r--
Merged cleanup branch (r3931) with trunk
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">
antonio@735
     4
      <parameter name="p_pinstance_id" type="NUMERIC" mode="in">
antonio@735
     5
        <default/>
antonio@735
     6
      </parameter>
antonio@735
     7
      <parameter name="p_order_id" type="NUMERIC" 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
carlos@0
    12
* Version  1.0  (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.
carlos@0
    21
* The Initial Developer of the Original Code is Openbravo SL
carlos@0
    22
* All portions are Copyright (C) 2001-2006 Openbravo SL
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):='';
carlos@0
    29
  v_Record_ID NUMBER;
carlos@0
    30
  v_User NUMBER;
carlos@0
    31
  v_IsProcessing CHAR(1) ;
carlos@0
    32
  v_IsProcessed CHAR(1) ;
carlos@0
    33
  v_Result NUMBER:=1; -- Success
asier@799
    34
  v_DBA_ErrorLog_ID NUMBER;
carlos@0
    35
  -- Parameter
carlos@0
    36
  TYPE RECORD IS REF CURSOR;
carlos@0
    37
    Cur_Parameter RECORD;
carlos@0
    38
    -- Record Info
carlos@0
    39
    v_Client_ID NUMBER;
carlos@0
    40
    v_Org_ID NUMBER;
carlos@0
    41
	v_Org_Name VARCHAR2(60);
carlos@0
    42
    v_UpdatedBy NUMBER;
carlos@0
    43
    v_DocAction CHAR(2) ;
carlos@0
    44
    v_DocStatus CHAR(2) ;
carlos@0
    45
    v_InvoiceRule CHAR(1) ;
carlos@0
    46
    v_M_Warehouse_ID NUMBER;
carlos@0
    47
    v_DocType_ID NUMBER;
carlos@0
    48
    v_DocTypeTarget_ID NUMBER;
carlos@0
    49
    v_DocSubTypeSO CHAR(2) ;
carlos@0
    50
    v_IsBinding CHAR(1):='Y';
carlos@0
    51
    --
carlos@0
    52
    ToDeliver NUMBER;
carlos@0
    53
    ToInvoice NUMBER;
carlos@0
    54
    --
carlos@0
    55
    InOut_ID NUMBER;
carlos@0
    56
    Invoice_ID NUMBER;
carlos@0
    57
    --Added by P.SAROBE
carlos@0
    58
	v_documentno_Settlement VARCHAR2(40);
carlos@0
    59
	v_dateSettlement DATE;
carlos@0
    60
	v_Cancel_Processed CHAR(1);
carlos@0
    61
	v_nameBankstatement VARCHAR2 (60);
carlos@0
    62
	v_dateBankstatement DATE;
carlos@0
    63
	v_nameCash VARCHAR2 (60);
carlos@0
    64
	v_dateCash DATE;
carlos@0
    65
	v_Bankstatementline_ID NUMBER;
carlos@0
    66
	--Finish added by P.Sarobe
carlos@0
    67
    v_AuxCashLine_ID NUMBER;
carlos@0
    68
    v_CashLine_ID NUMBER;
carlos@0
    69
	v_ispaid CHAR(1);
carlos@0
    70
	v_Settlement_Cancel_ID NUMBER;
carlos@0
    71
    v_Cash_ID NUMBER:=NULL;
carlos@0
    72
    v_Line NUMBER:=0;
carlos@0
    73
    v_CashBook_ID NUMBER:=NULL;
carlos@0
    74
	v_Debtpayment_ID NUMBER;
carlos@0
    75
    v_ISO_Code VARCHAR(10) ;
carlos@0
    76
    v_DocumentNo VARCHAR2(200) ;
carlos@0
    77
    v_GrandTotal NUMBER;
carlos@0
    78
    v_Multiplier NUMBER:=1;
carlos@0
    79
    v_Date DATE;
carlos@0
    80
    v_WarehouseName VARCHAR2(60) ;
carlos@0
    81
    v_CashProcessed NUMBER;
carlos@0
    82
    v_count NUMBER;
carlos@379
    83
    v_isSoTrx CHAR(1) ;
carlos@0
    84
    v_Aux NUMBER;
carlos@0
    85
    v_c_Bpartner_Id NUMBER;
carlos@0
    86
    v_c_currency_id NUMBER;
carlos@0
    87
    v_C_PROJECT_ID NUMBER;
carlos@0
    88
    v_PriceList_ID NUMBER;
carlos@0
    89
    FINISH_PROCESS BOOLEAN:=FALSE;
carlos@0
    90
    END_PROCESSING BOOLEAN:=FALSE;
carlos@0
    91
    v_CBPartner_ID NUMBER;
carlos@0
    92
    rowcount NUMBER;
carlos@0
    93
  BEGIN
carlos@0
    94
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
    95
      --  Update AD_PInstance
carlos@0
    96
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
    97
      v_ResultStr:='PInstanceNotFound';
antonio@735
    98
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
    99
      -- Get Parameters
carlos@0
   100
      v_ResultStr:='ReadingParameters';
carlos@0
   101
      FOR Cur_Parameter IN
carlos@0
   102
        (SELECT i.Record_ID,
carlos@0
   103
          i.AD_User_ID,
carlos@0
   104
          p.ParameterName,
carlos@0
   105
          p.P_String,
carlos@0
   106
          p.P_Number,
carlos@0
   107
          p.P_Date
carlos@0
   108
        FROM AD_PINSTANCE i
carlos@0
   109
        LEFT JOIN AD_PINSTANCE_PARA p
carlos@0
   110
          ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
   111
        WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
   112
        ORDER BY p.SeqNo
carlos@0
   113
        )
carlos@0
   114
      LOOP
carlos@0
   115
        v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
   116
        v_User:=Cur_Parameter.AD_User_ID;
carlos@0
   117
      END LOOP; -- Get Parameter
carlos@0
   118
    ELSE
carlos@0
   119
      v_Record_ID:=p_Order_ID;
carlos@0
   120
      SELECT CREATEDBY INTO v_User  FROM C_ORDER  WHERE C_ORDER_ID=p_Order_ID;
carlos@0
   121
    END IF;
carlos@0
   122
    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
carlos@0
   123
  BEGIN --BODY
carlos@0
   124
    /**
carlos@0
   125
    * Read Order
carlos@0
   126
    */
carlos@0
   127
    v_ResultStr:='ReadingOrder';
carlos@0
   128
    SELECT Processing, Processed, DocAction, DocStatus,
asier@799
   129
      C_DocType_ID, C_DocTypeTarget_ID, AD_Client_ID,
carlos@0
   130
      AD_Org_ID, UpdatedBy, M_Warehouse_ID, TRUNC(DateOrdered),
carlos@0
   131
      Issotrx, c_Bpartner_Id, c_currency_id, C_PROJECT_ID,
carlos@0
   132
      C_BPartner_ID, M_PriceList_ID, invoicerule
carlos@0
   133
    INTO v_IsProcessing, v_IsProcessed, v_DocAction, v_DocStatus,
asier@799
   134
      v_DocType_ID, v_DocTypeTarget_ID, v_Client_ID,
carlos@0
   135
      v_Org_ID, v_UpdatedBy, v_M_Warehouse_ID, v_Date,
carlos@379
   136
      v_isSoTrx, v_c_Bpartner_Id, v_c_currency_id, v_C_PROJECT_ID,
carlos@0
   137
      v_CBPartner_ID, v_PriceList_ID, v_invoicerule
carlos@0
   138
    FROM C_ORDER
carlos@0
   139
    WHERE C_Order_ID=v_Record_ID  FOR UPDATE;
carlos@0
   140
    -- Get current DocSubTypeSO
carlos@0
   141
    SELECT DocSubTypeSO
carlos@0
   142
    INTO v_DocSubTypeSO
carlos@0
   143
    FROM C_DOCTYPE
carlos@0
   144
    WHERE C_DocType_ID=v_DocType_ID;
carlos@0
   145
	-- Get the name of the org of the Order. Added by P.Sarobe
carlos@0
   146
	SELECT name INTO v_Org_Name FROM AD_ORG WHERE ad_org_id = v_Org_ID;
asier@799
   147
    DBMS_OUTPUT.PUT_LINE('DocAction=' || v_DocAction || ', DocStatus=' || v_DocStatus || ', DocType_ID=' || v_DocType_ID || ', DocTypeTarget_ID=' || v_DocTypeTarget_ID || ', DocSubTypeSO=' || v_DocSubTypeSO) ;
carlos@0
   148
    /**
carlos@0
   149
    * Order Closed, Voided or Reversed - No action possible
carlos@0
   150
    */
carlos@0
   151
    IF(v_DocStatus IN('CL', 'VO', 'RE')) THEN
carlos@0
   152
      RAISE_APPLICATION_ERROR(-20000, '@AlreadyPosted@') ;
carlos@0
   153
    END IF;
carlos@0
   154
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   155
      /**
carlos@0
   156
      * Waiting on Prepayment  can only be closed
carlos@0
   157
      */
gorkaion@239
   158
      IF(v_DocStatus='WP' AND v_DocAction<>'CL') THEN
carlos@0
   159
        RAISE_APPLICATION_ERROR(-20000, '@WaitingPayment@') ;
carlos@0
   160
      END IF;
carlos@0
   161
    END IF;--FINISH_PROCESS
gorkaion@239
   162
    IF (v_DocSubTypeSO='PR' AND v_invoicerule <> 'I') THEN
carlos@0
   163
      RAISE_APPLICATION_ERROR(-20000, '@PrepayMustImmediate@');
carlos@0
   164
    END IF;
carlos@0
   165
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   166
      /**
carlos@0
   167
      * Unlock
carlos@0
   168
      */
carlos@0
   169
      IF(v_DocAction='XL') THEN
carlos@0
   170
        UPDATE C_ORDER
carlos@0
   171
          SET Processing='N',
carlos@0
   172
          DocAction='--',
antonio@735
   173
          Updated=now()
carlos@0
   174
        WHERE C_Order_ID=v_Record_ID;
carlos@0
   175
        FINISH_PROCESS:=TRUE;
carlos@0
   176
      END IF;
carlos@0
   177
    END IF;--FINISH_PROCESS
carlos@0
   178
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   179
      IF(v_IsProcessing='Y') THEN
carlos@0
   180
        RAISE_APPLICATION_ERROR(-20000, '@OtherProcessActive@') ;
carlos@0
   181
      END IF;
carlos@0
   182
    END IF;--FINISH_PROCESS
carlos@0
   183
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   184
      /**
carlos@0
   185
      * Re-activate
carlos@0
   186
      */
carlos@0
   187
      IF(v_DocAction='RE') THEN
carlos@0
   188
        --Verify not managed debtPayments added by ALO
carlos@0
   189
		--Added by P.Sarobe. New messages
carlos@0
   190
        SELECT max(c_debt_payment_id), COUNT(*)
carlos@0
   191
        INTO v_Debtpayment_ID, v_Aux
carlos@0
   192
        FROM C_DEBT_PAYMENT
carlos@0
   193
        WHERE C_Order_ID=v_Record_ID
carlos@0
   194
          AND C_Debt_Payment_Status(C_Settlement_Cancel_ID, Cancel_Processed, Generate_Processed, IsPaid, IsValid, C_CashLine_ID, C_BankStatementLine_ID)!='P';
carlos@0
   195
        IF v_Aux!=0 THEN
carlos@0
   196
		--Added by P.Sarobe. New messages
carlos@0
   197
		  SELECT c_Bankstatementline_Id, c_cashline_id, c_settlement_cancel_id, ispaid, cancel_processed
carlos@0
   198
		  INTO v_Bankstatementline_ID, v_CashLine_ID, v_Settlement_Cancel_ID, v_ispaid, v_Cancel_Processed
carlos@0
   199
		  FROM C_DEBT_PAYMENT WHERE C_Debt_Payment_ID = v_Debtpayment_ID;
carlos@0
   200
		  	   IF v_Bankstatementline_ID IS NOT NULL THEN
carlos@0
   201
			   	 SELECT C_BANKSTATEMENT.NAME, C_BANKSTATEMENT.STATEMENTDATE
carlos@0
   202
				 INTO v_nameBankstatement, v_dateBankstatement
carlos@0
   203
				 FROM C_BANKSTATEMENT, C_BANKSTATEMENTLINE
carlos@0
   204
				 WHERE C_BANKSTATEMENT.C_BANKSTATEMENT_ID = C_BANKSTATEMENTLINE.C_BANKSTATEMENT_ID
carlos@0
   205
				 AND C_BANKSTATEMENTLINE.C_BANKSTATEMENTLINE_ID = v_Bankstatementline_ID;
carlos@0
   206
          		 RAISE_APPLICATION_ERROR(-20000, '@ManagedDebtPaymentOrderBank@'||v_nameBankstatement||' '||'@Bydate@'||v_dateBankstatement) ;
carlos@0
   207
			   END IF;
carlos@0
   208
			   IF v_CashLine_ID IS NOT NULL THEN
carlos@0
   209
			   	 SELECT C_CASH.NAME, C_CASH.STATEMENTDATE
carlos@0
   210
				 INTO v_nameCash, v_dateCash
carlos@0
   211
				 FROM C_CASH, C_CASHLINE
carlos@0
   212
				 WHERE C_CASH.C_CASH_ID = C_CASHLINE.C_CASH_ID
carlos@0
   213
				 AND C_CASHLINE.C_CASHLINE_ID = v_CashLine_ID;
carlos@0
   214
          		 RAISE_APPLICATION_ERROR(-20000, '@ManagedDebtPaymentOrderCash@'||v_nameCash||' '||'@Bydate@'||v_dateCash) ;
carlos@0
   215
			   END IF;
carlos@0
   216
			   IF v_Cancel_Processed='Y' AND v_ispaid='N' THEN
carlos@0
   217
			   	 SELECT documentno, datetrx
carlos@0
   218
				 INTO v_documentno_Settlement, v_dateSettlement
carlos@0
   219
				 FROM C_SETTLEMENT
carlos@0
   220
				 WHERE C_SETTLEMENT_ID = v_Settlement_Cancel_ID;
carlos@0
   221
				 RAISE_APPLICATION_ERROR(-20000, '@ManagedDebtPaymentOrderCancel@'||v_documentno_Settlement||' '||'@Bydate@'||v_dateSettlement) ;
carlos@0
   222
			   END IF;
carlos@0
   223
        END IF;
carlos@0
   224
        IF(NOT FINISH_PROCESS) THEN
carlos@0
   225
          DBMS_OUTPUT.PUT_LINE('Re-Activating ' || v_DocSubTypeSO || ': ' || v_Record_ID) ;
carlos@0
   226
          IF(v_DocSubTypeSO IN('WI', 'WP', 'WR')) THEN
carlos@0
   227
            -- Cancel existing Deli very + Invoice Documents
antonio@735
   228
            M_INOUT_CANCEL(NULL, v_Record_ID) ;
gorkaion@239
   229
            IF(v_DocSubTypeSO<>'WP') THEN
antonio@735
   230
              C_INVOICE_CANCEL(NULL, v_Record_ID);
carlos@0
   231
            END IF;
carlos@0
   232
          END IF;
carlos@0
   233
          -- Update Order
carlos@0
   234
          v_ResultStr:='ReActivate';
carlos@0
   235
          UPDATE C_ORDER
carlos@0
   236
            SET DocStatus='IP', -- In Progress
carlos@0
   237
            DocAction='CO',
carlos@0
   238
            Processing='N',
carlos@0
   239
            Processed='N',
antonio@735
   240
            Updated=now()
carlos@0
   241
          WHERE C_Order_ID=v_Record_ID;
carlos@0
   242
          --ADDED BY E.ARGAL
carlos@0
   243
          --Invalidate debt payments added by ALO
carlos@0
   244
          UPDATE C_DEBT_PAYMENT
carlos@0
   245
            SET IsValid='N'
carlos@0
   246
          WHERE C_Order_ID=v_Record_ID
carlos@0
   247
            AND IsAutomaticGenerated='N';
carlos@0
   248
carlos@0
   249
          DELETE
carlos@0
   250
          FROM C_CASHLINE
carlos@0
   251
          WHERE C_Debt_Payment_Id IN
carlos@0
   252
            (SELECT C_DEBT_PAYMENT_ID
carlos@0
   253
            FROM C_DEBT_PAYMENT
carlos@0
   254
            WHERE C_Order_ID=v_Record_ID
carlos@0
   255
              AND COALESCE(IsAutomaticGenerated, 'Y')='Y'
carlos@0
   256
            )
carlos@0
   257
            ;
carlos@0
   258
          DELETE
carlos@0
   259
          FROM C_DEBT_PAYMENT
carlos@0
   260
          WHERE C_Order_ID=v_Record_ID
carlos@0
   261
            AND COALESCE(IsAutomaticGenerated, 'Y')='Y';
carlos@0
   262
          DECLARE
carlos@0
   263
            Cur_ResLine RECORD;
carlos@0
   264
            v_QtySO NUMBER; -- Reserved
carlos@0
   265
            v_QtyOrderSO NUMBER;
carlos@0
   266
            v_QtyPO NUMBER; -- Ordered
carlos@0
   267
            v_QtyOrderPO NUMBER;
carlos@0
   268
          BEGIN
carlos@0
   269
            v_ResultStr:='ReserveInventory';
carlos@0
   270
            -- For all lines needing reservation
carlos@0
   271
            FOR Cur_ResLine IN
carlos@0
   272
              (SELECT l.M_Warehouse_ID,
carlos@0
   273
                l.M_Product_ID,
carlos@0
   274
                l.M_AttributeSetInstance_ID,
carlos@0
   275
                l.C_OrderLine_ID,
carlos@0
   276
                -- Target Level = 0 if DirectShip='Y' or Binding='N'
carlos@0
   277
                l.QtyOrdered AS Qty,
carlos@0
   278
                l.QUANTITYORDER,
carlos@0
   279
                l.C_UOM_ID,
carlos@0
   280
                l.M_PRODUCT_UOM_ID
carlos@0
   281
              FROM C_ORDERLINE l,
carlos@0
   282
                M_PRODUCT p
carlos@0
   283
              WHERE l.C_Order_ID=v_Record_ID  -- Reserve Products (not: services, null products) --
carlos@0
   284
                AND l.M_Product_ID=p.M_Product_ID
carlos@0
   285
                AND p.IsStocked='Y'
carlos@0
   286
                AND p.ProductType='I'  FOR UPDATE
carlos@0
   287
              )
carlos@0
   288
            LOOP
carlos@0
   289
              -- Qty corrected for SO/PO
carlos@0
   290
              IF(v_DocSubTypeSO IS NULL) THEN
carlos@0
   291
                v_QtySO:=0;
carlos@0
   292
                v_QtyOrderSO:=NULL;
carlos@0
   293
                v_QtyPO:=-Cur_ResLine.Qty;
carlos@0
   294
                v_QtyOrderPO:=-Cur_ResLine.QuantityOrder;
carlos@0
   295
              END IF;
antonio@735
   296
              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) ;
carlos@0
   297
            END LOOP;
carlos@0
   298
          END;
carlos@0
   299
        END IF;--FINISH_PROCESS
carlos@0
   300
        IF(NOT FINISH_PROCESS) THEN
carlos@0
   301
          --ADDED BY P.SAROBE but to be deprecated 26052007
carlos@0
   302
          SELECT MAX(C_CASHLINE_ID)
carlos@0
   303
          INTO v_CashLine_ID
carlos@0
   304
          FROM C_CASHLINE
carlos@0
   305
          WHERE C_ORDER_ID=v_Record_ID;
carlos@0
   306
          IF(v_CashLine_ID IS NOT NULL) THEN
carlos@0
   307
            SELECT PROCESSED
carlos@0
   308
            INTO v_IsProcessed
carlos@0
   309
            FROM C_CASH,
carlos@0
   310
              C_CASHLINE
carlos@0
   311
            WHERE C_CASH.C_CASH_ID=C_CASHLINE.C_CASH_ID
carlos@0
   312
              AND C_CASHLINE_ID=v_CashLine_ID;
carlos@0
   313
            IF(v_IsProcessed='N') THEN
carlos@0
   314
              DELETE FROM C_CASHLINE WHERE C_CASHLINE_ID=v_CashLine_ID;
carlos@0
   315
            ELSE
carlos@0
   316
			  SELECT C_CASH.NAME, C_CASH.STATEMENTDATE, C_CASHLINE.LINE
carlos@0
   317
				 INTO v_nameCash, v_dateCash, v_Line
carlos@0
   318
				 FROM C_CASH, C_CASHLINE
carlos@0
   319
				 WHERE C_CASH.C_CASH_ID = C_CASHLINE.C_CASH_ID
carlos@0
   320
				 AND C_CASHLINE.C_CASHLINE_ID = v_CashLine_ID;
carlos@0
   321
              RAISE_APPLICATION_ERROR(-20000, '@Ordercahslineprocessed@'||v_nameCash||' '||'@Bydate@'||v_dateCash||' '||'@line@'||v_Line) ;
carlos@0
   322
              /*  ALO
carlos@0
   323
              SELECT CB.C_CASHBOOK_ID, C_CURRENCY.ISO_CODE
carlos@0
   324
              INTO v_CashBook_ID, v_ISO_Code
carlos@0
   325
              FROM C_CASH C, C_CASHLINE CL, C_CASHBOOK CB, C_CURRENCY
carlos@0
   326
              WHERE C.C_CASH_ID = CL.C_CASH_ID
carlos@0
   327
              AND C.C_CASHBOOK_ID = CB.C_CASHBOOK_ID
carlos@0
   328
              AND CB.C_CURRENCY_ID = C_CURRENCY.C_CURRENCY_ID
carlos@0
   329
              AND CL.C_CASHLINE_ID = v_CashLine_ID;
carlos@0
   330
              SELECT MAX(C.C_CASH_ID) INTO v_Cash_ID
carlos@0
   331
              FROM C_CASH C
carlos@0
   332
              WHERE C.C_CASHBOOK_ID=v_CashBook_ID
carlos@0
   333
              AND C.DATEACCT = v_Date
carlos@0
   334
              AND C.PROCESSED = 'N';
carlos@0
   335
              IF (v_Cash_Id IS NULL) THEN
carlos@0
   336
              v_ResultStr := 'Create C_Cash';
carlos@0
   337
              Ad_Sequence_Next('C_Cash', v_Org_ID, v_Cash_ID);
carlos@0
   338
              INSERT INTO C_CASH
carlos@0
   339
              (C_Cash_ID,AD_Client_ID, AD_Org_ID,
carlos@0
   340
              IsActive, Created,CreatedBy, Updated,UpdatedBy,
carlos@0
   341
              C_CashBook_ID, Name,
carlos@0
   342
              StatementDate,DateAcct,
carlos@0
   343
              BeginningBalance,EndingBalance,StatementDifference,
carlos@0
   344
              Processing,Processed,Posted)
carlos@0
   345
              VALUES
carlos@0
   346
              (v_Cash_ID, v_Client_ID, v_Org_ID,
carlos@0
   347
              'Y', now(),UpdatedBy, now(),UpdatedBy,
carlos@0
   348
              v_CashBook_ID, TO_CHAR(v_Date, 'YYYY-MM-DD') || ' ' || v_ISO_Code,
carlos@0
   349
              v_Date, v_Date,
carlos@0
   350
              0,0,0, 'N','N','N');
carlos@0
   351
              END IF;
carlos@0
   352
              v_ResultStr := 'Create negative C_CashLine';
carlos@0
   353
              v_AuxCashLine_ID := v_CashLine_ID;
carlos@0
   354
              Ad_Sequence_Next('C_CashLine', v_Org_ID, v_CashLine_ID);
carlos@0
   355
              SELECT COALESCE(MAX(LINE),0) + 10 INTO v_Line FROM C_CASH, C_CASHLINE
carlos@0
   356
              WHERE C_CASH.C_CASH_ID = C_CASHLINE.C_CASH_ID
carlos@0
   357
              AND C_CASH.C_CASH_ID = v_Cash_ID;
carlos@0
   358
              INSERT INTO C_CASHLINE
carlos@0
   359
              (C_CashLine_ID,AD_Client_ID,AD_Org_ID,
carlos@0
   360
              IsActive,Created,CreatedBy,Updated,UpdatedBy,
carlos@0
   361
              C_Cash_ID, C_Order_ID,
carlos@0
   362
              Line, Description,Amount,CashType,
carlos@0
   363
              DiscountAmt, WriteOffAmt, IsGenerated)
carlos@0
   364
              SELECT v_CashLine_ID, AD_Client_ID, AD_Org_ID, IsActive,Created,CreatedBy,Updated,UpdatedBy,
carlos@0
   365
              v_Cash_ID, C_Order_ID, v_Line, Description,-Amount,CashType,
carlos@0
   366
              DiscountAmt, WriteOffAmt, IsGenerated
carlos@0
   367
              FROM C_CASHLINE
carlos@0
   368
              WHERE C_CASHLINE_ID= v_AuxCashLine_ID;
carlos@0
   369
              */
carlos@0
   370
            END IF;
carlos@0
   371
          END IF;--FINISH_PROCESS
carlos@0
   372
        END IF;
carlos@0
   373
carlos@0
   374
        UPDATE C_ORDER
carlos@0
   375
            SET DocStatus='DR', -- Draft
carlos@0
   376
            DocAction='CO',
carlos@0
   377
            Processing='N',
antonio@735
   378
            Updated=now()
carlos@0
   379
          WHERE C_Order_ID=v_Record_ID;
carlos@0
   380
        FINISH_PROCESS:=TRUE;
carlos@0
   381
carlos@0
   382
      END IF;
carlos@0
   383
    END IF;--FINISH_PROCESS
carlos@0
   384
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   385
    SELECT COUNT(*)
carlos@0
   386
     INTO v_Count
carlos@0
   387
    FROM C_ORDER C,
carlos@0
   388
         C_DOCTYPE
carlos@0
   389
   WHERE C_DocType.DocBaseType IN ('SOO', 'POO')
carlos@0
   390
     AND C_DocType.IsSOTrx=C.ISSOTRX
gorkaion@239
   391
    AND AD_ISORGINCLUDED(C.AD_Org_ID,C_DocType.AD_Org_ID, C.AD_Client_ID) <> -1
carlos@0
   392
    AND C.C_DOCTYPETARGET_ID = C_DOCTYPE.C_DOCTYPE_ID
carlos@0
   393
    AND C.C_ORDER_ID = v_Record_ID;
carlos@0
   394
   IF v_Count=0 THEN
carlos@0
   395
		RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgDoctypeOrder@') ;
carlos@0
   396
      END IF;
carlos@0
   397
carlos@0
   398
  -- Set org lines like the headear
carlos@0
   399
     UPDATE C_ORDERLINE
carlos@0
   400
      SET AD_ORG_ID = (SELECT AD_ORG_ID FROM C_ORDER WHERE C_ORDER_ID = v_Record_ID)
carlos@0
   401
    WHERE C_ORDER_ID = v_Record_ID;
carlos@0
   402
carlos@0
   403
      /**
carlos@0
   404
      * Close Order - prepare
carlos@0
   405
      */
carlos@0
   406
      IF(v_DocAction='CL') THEN
carlos@0
   407
        -- Cancel undelivered Items
carlos@379
   408
        IF(v_isSoTrx='Y') THEN --Sales orders
carlos@0
   409
          UPDATE C_ORDERLINE
carlos@0
   410
            SET QtyOrdered=QtyDelivered,
carlos@0
   411
            --MODIFIED BY P.SAROBE
carlos@0
   412
            linenetamt=QtyDelivered*priceactual,
carlos@0
   413
            --MODIFIED BY F.IRIAZABAL
carlos@0
   414
            QuantityOrder=QuantityOrder,
antonio@735
   415
            Updated=now()
carlos@0
   416
          WHERE C_Order_ID=v_Record_ID
gorkaion@239
   417
            AND QtyOrdered<>QtyDelivered;
carlos@0
   418
          -- For Purchase orders, M_MatchPO table used. Notice that only delivered lines(C_Invoiceline_Id is null) using
carlos@0
   419
        ELSE
carlos@0
   420
          UPDATE C_ORDERLINE
carlos@0
   421
            SET QTYORDERED=COALESCE
carlos@0
   422
            (
carlos@0
   423
              (SELECT SUM(M_MATCHPO.QTY)
carlos@0
   424
              FROM M_MATCHPO
carlos@0
   425
              WHERE M_MATCHPO.C_ORDERLINE_ID=C_ORDERLINE.C_ORDERLINE_ID
carlos@0
   426
                AND M_MATCHPO.C_INVOICELINE_ID IS NULL
carlos@0
   427
              )
carlos@0
   428
              , 0
carlos@0
   429
            )
carlos@0
   430
            ,
carlos@0
   431
            QuantityOrder=C_ORDERLINE.QuantityOrder,
antonio@735
   432
            Updated=now(),
carlos@0
   433
            linenetamt=COALESCE
carlos@0
   434
            (
carlos@0
   435
              (SELECT SUM(M_MATCHPO.QTY)
carlos@0
   436
              FROM M_MATCHPO
carlos@0
   437
              WHERE M_MATCHPO.C_ORDERLINE_ID=C_ORDERLINE.C_ORDERLINE_ID
carlos@0
   438
                AND M_MATCHPO.C_INVOICELINE_ID IS NULL
carlos@0
   439
              )
carlos@0
   440
              , 0
carlos@0
   441
            )
carlos@0
   442
            *priceactual
carlos@0
   443
          WHERE C_ORDERLINE.C_ORDER_ID=v_Record_ID;
carlos@0
   444
        END IF;
carlos@0
   445
        -- if there is no change, the tax calculation, etc. is not needed.
carlos@0
   446
        -- potential problem, if posted (i.e. encumbered) for full amount
carlos@0
   447
        -- and the rest then cancelled out.
carlos@0
   448
      END IF;
carlos@0
   449
    END IF;--FINISH_PROCESS
carlos@0
   450
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   451
      /**
carlos@0
   452
      * Void Order - prepare
carlos@0
   453
      */
carlos@0
   454
      IF(v_DocAction='VO') THEN
carlos@0
   455
        -- Cancel all Items
carlos@0
   456
        UPDATE C_ORDERLINE
carlos@0
   457
          SET QtyOrdered=0,
carlos@0
   458
          --MODIFIED BY F.IRIAZABAL
carlos@0
   459
          QuantityOrder=
carlos@0
   460
          CASE WHEN M_PRODUCT_UOM_ID IS NULL THEN NULL ELSE 0
carlos@0
   461
          END
carlos@0
   462
          ,
carlos@0
   463
          LineNetAmt=0,
antonio@735
   464
          Updated=now()
carlos@0
   465
        WHERE C_Order_ID=v_Record_ID
gorkaion@239
   466
          AND QtyOrdered<>0;
carlos@0
   467
      END IF;
carlos@0
   468
    END IF;--FINISH_PROCESS
carlos@0
   469
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   470
 /**************************************************************************
carlos@0
   471
  * Start Processing ------------------------------------------------------
carlos@0
   472
  *************************************************************************/
carlos@0
   473
    /**************************************************************************
carlos@0
   474
      * Start Processing ------------------------------------------------------
carlos@0
   475
      *************************************************************************/
carlos@0
   476
      IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   477
        v_ResultStr:='LockingOrder';
carlos@0
   478
        UPDATE C_ORDER  SET Processing='Y'  WHERE C_Order_ID=v_Record_ID;
carlos@0
   479
        COMMIT;
carlos@0
   480
      END IF;
carlos@0
   481
      -- Now, needs to go to END_PROCESSING to unlock
carlos@0
   482
      /**
carlos@0
   483
      * Allowed Actions:  AProve, COmplete, PRocess, CLose, VOid
carlos@0
   484
      */
carlos@0
   485
      IF(v_DocAction IN('AP', 'CO', 'PR', 'CL', 'VO')) THEN
carlos@0
   486
        NULL;
carlos@0
   487
      ELSE
carlos@0
   488
        RAISE_APPLICATION_ERROR(-20000, '@ActionNotAllowedHere@') ;
carlos@0
   489
      END IF;
carlos@0
   490
    END IF;--END_PROCESSING
carlos@0
   491
    IF(NOT END_PROCESSING AND NOT FINISH_PROCESS) THEN
carlos@0
   492
      SELECT COUNT(*)
carlos@0
   493
      INTO v_count
carlos@0
   494
      FROM AD_CLIENTINFO
carlos@0
   495
      WHERE AD_CLIENT_ID=v_Client_ID
carlos@0
   496
        AND CHECKORDERORG='Y';
gorkaion@239
   497
      IF v_count>0 THEN
carlos@0
   498
        v_ResultStr:='CheckingRestrictions - C_ORDER ORG IS IN C_BPARTNER ORG TREE';
carlos@0
   499
        SELECT COUNT(*)
carlos@0
   500
        INTO v_count
carlos@0
   501
        FROM C_ORDER c,
carlos@0
   502
          C_BPARTNER bp
carlos@0
   503
        WHERE c.C_Order_ID=v_Record_ID
carlos@0
   504
          AND c.C_BPARTNER_ID=bp.C_BPARTNER_ID
carlos@0
   505
          AND Ad_Isorgincluded(c.AD_ORG_ID, bp.AD_ORG_ID, bp.AD_CLIENT_ID)=-1;
gorkaion@239
   506
        IF v_count>0 THEN
carlos@0
   507
          RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgBpartnerOrder@') ;
carlos@0
   508
        END IF;
carlos@0
   509
      END IF;
carlos@0
   510
    END IF;--END_PROCESSING
carlos@0
   511
    IF(NOT END_PROCESSING AND NOT FINISH_PROCESS) THEN
carlos@0
   512
      /**
carlos@0
   513
      * Convert to Target DocType
carlos@0
   514
      */
carlos@0
   515
      DECLARE
carlos@0
   516
        v_DocSubTypeSO_Target CHAR(2) ;
carlos@0
   517
      BEGIN
carlos@0
   518
        v_ResultStr:='ConvertingDocType';
gorkaion@239
   519
        IF(v_DocType_ID<>v_DocTypeTarget_ID) THEN
carlos@0
   520
          -- New
carlos@0
   521
          IF(v_DocStatus='DR' OR v_DocType_ID=0) THEN
carlos@0
   522
            -- Update to Target Document Type
gorkaion@239
   523
            WHILE(v_DocType_ID<>v_DocTypeTarget_ID)
carlos@0
   524
            LOOP
carlos@0
   525
            BEGIN
carlos@0
   526
              v_ResultStr:='UpdateDocType';
carlos@0
   527
              UPDATE C_ORDER
carlos@0
   528
                SET C_DocType_ID=v_DocTypeTarget_ID
carlos@0
   529
              WHERE C_Order_ID=v_Record_ID;
carlos@0
   530
              v_DocType_ID:=v_DocTypeTarget_ID;
carlos@0
   531
            EXCEPTION
carlos@0
   532
            WHEN OTHERS THEN
carlos@0
   533
              v_ResultStr:='UpdateDocumentNo';
carlos@0
   534
              UPDATE C_ORDER  SET DocumentNo=DocumentNo || '.'  WHERE C_Order_ID=v_Record_ID;
carlos@0
   535
            END;
carlos@0
   536
          END LOOP;
carlos@0
   537
        ELSE
carlos@0
   538
          v_ResultStr:='GetTargetDocType';
carlos@0
   539
          SELECT DocSubTypeSO
carlos@0
   540
          INTO v_DocSubTypeSO_Target
carlos@0
   541
          FROM C_DOCTYPE
carlos@0
   542
          WHERE C_DocType_ID=v_DocTypeTarget_ID;
carlos@0
   543
          DBMS_OUTPUT.PUT_LINE('Changing DocType from ' || v_DocSubTypeSO || ' to ' || v_DocSubTypeSO_Target) ;
carlos@0
   544
          -- Change Offer to anything, Change InProcess to anything
carlos@0
   545
          IF(v_DocSubTypeSO IN('ON', 'OB') OR v_DocStatus='IP') THEN
carlos@0
   546
            -- Update to Target Document Type
gorkaion@239
   547
            WHILE(v_DocType_ID<>v_DocTypeTarget_ID)
carlos@0
   548
            LOOP
carlos@0
   549
            BEGIN
carlos@0
   550
              v_ResultStr:='UpdateDocType';
carlos@0
   551
              UPDATE C_ORDER
carlos@0
   552
                SET C_DocType_ID=v_DocTypeTarget_ID
carlos@0
   553
              WHERE C_Order_ID=v_Record_ID;
carlos@0
   554
              v_DocType_ID:=v_DocTypeTarget_ID;
carlos@0
   555
            EXCEPTION
carlos@0
   556
            WHEN OTHERS THEN
carlos@0
   557
              v_ResultStr:='UpdateDocumentNo';
carlos@0
   558
              UPDATE C_ORDER  SET DocumentNo=DocumentNo || '.'  WHERE C_Order_ID=v_Record_ID;
carlos@0
   559
            END;
carlos@0
   560
          END LOOP;
carlos@0
   561
        ELSE
carlos@0
   562
          -- Change Back
carlos@0
   563
          UPDATE C_ORDER
carlos@0
   564
            SET C_DocTypeTarget_ID=v_DocType_ID
carlos@0
   565
          WHERE C_Order_ID=v_Record_ID;
carlos@0
   566
          RAISE_APPLICATION_ERROR(-20000, '@CannotChangeDocType@') ;
carlos@0
   567
        END IF;
carlos@0
   568
      END IF;
gorkaion@239
   569
    END IF; -- C_DocType_ID <> C_DocTypeTarget_ID
carlos@0
   570
  END; -- Conversion
carlos@0
   571
END IF;--FINISH_PROCESS
carlos@0
   572
IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
carlos@0
   573
  /**
adrian@170
   574
  * Get DocSubTypeSO + Is it Binding :1:2
carlos@0
   575
  */
carlos@0
   576
  v_ResultStr:='TestBinding DocType_ID=' || v_DocType_ID;
carlos@0
   577
  SELECT
carlos@0
   578
    CASE DocSubTypeSO WHEN 'ON' THEN 'N' ELSE 'Y'
carlos@0
   579
    END
carlos@0
   580
    ,
carlos@0
   581
    DocSubTypeSO
carlos@0
   582
  INTO v_IsBinding,
carlos@0
   583
    v_DocSubTypeSO
carlos@0
   584
  FROM C_DOCTYPE
carlos@0
   585
  WHERE C_DocType_ID=v_DocType_ID;
carlos@0
   586
  DBMS_OUTPUT.PUT_LINE('DocSubTypeSO=' || v_DocSubTypeSO || ' IsBinding=' || v_IsBinding) ;
carlos@0
   587
END IF;--FINISH_PROCESS
carlos@0
   588
IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
carlos@0
   589
 /**************************************************************************
carlos@0
   590
  * Resolve not-stocked BOMs
carlos@0
   591
  *************************************************************************/
carlos@0
   592
  DECLARE
carlos@0
   593
    -- Order Lines with non-stocked BOMs
carlos@0
   594
    CUR_BOM_Line RECORD;
carlos@0
   595
    -- BOM Product List
carlos@0
   596
    CUR_BOM RECORD;
carlos@0
   597
    --
carlos@0
   598
    CountNo NUMBER;
carlos@0
   599
    v_PriceList_Version_ID NUMBER;
carlos@0
   600
    NextNo NUMBER;
carlos@0
   601
    v_Line NUMBER;
carlos@0
   602
    v_FreightAmt NUMBER;
carlos@0
   603
    v_ChargeAmt NUMBER;
carlos@0
   604
    v_Parner_Id NUMBER;
carlos@0
   605
 --v_PriceList_ID NUMBER;
carlos@0
   606
    --
carlos@0
   607
  BEGIN
carlos@0
   608
    LOOP
adrian@170
   609
      -- How many BOMs do we have:3
carlos@0
   610
      SELECT COUNT(*)
carlos@0
   611
      INTO CountNo
carlos@0
   612
      FROM C_ORDERLINE l
carlos@0
   613
      WHERE l.C_Order_ID=v_Record_ID
carlos@0
   614
        AND EXISTS
carlos@0
   615
        (SELECT *
carlos@0
   616
        FROM M_PRODUCT p
carlos@0
   617
        WHERE l.M_Product_ID=p.M_Product_ID
carlos@0
   618
          AND p.IsBOM='Y'
carlos@0
   619
          AND p.IsStocked='N'
carlos@0
   620
        )
carlos@0
   621
        ;
adrian@170
   622
      -- Nothing to do:4
carlos@3
   623
      EXIT WHEN CountNo=0;
carlos@0
   624
      DBMS_OUTPUT.PUT_LINE('  BOMs to resolve=' || CountNo) ;
carlos@0
   625
      DECLARE
carlos@0
   626
        Cur_MPriceListVersion RECORD;
carlos@0
   627
      BEGIN
carlos@0
   628
        -- last shipment
carlos@0
   629
        FOR Cur_MPriceListVersion IN
carlos@0
   630
          (SELECT COALESCE(v.M_PriceList_Version_ID, 0) AS PriceList_Version_ID, o.M_PriceList_ID
carlos@0
   631
          FROM M_PRICELIST_VERSION v,
carlos@0
   632
            C_ORDER o
carlos@0
   633
          WHERE v.M_PriceList_ID=o.M_PriceList_ID
gorkaion@239
   634
            AND v.ValidFrom<=o.DateOrdered
carlos@0
   635
            AND v.IsActive='Y'
carlos@0
   636
            AND o.C_Order_ID=v_Record_ID
carlos@0
   637
          ORDER BY v.ValidFrom DESC
carlos@0
   638
          )
carlos@0
   639
        LOOP
carlos@0
   640
          v_PriceList_Version_ID:=Cur_MPriceListVersion.PriceList_Version_ID;
carlos@0
   641
         -- v_PriceList_ID := Cur_MPriceListVersion.M_PriceList_Id;
carlos@0
   642
          EXIT;
carlos@0
   643
        END LOOP;
carlos@0
   644
      END;
carlos@0
   645
      -- Get Price List Version
carlos@0
   646
      -- Replace Lines
carlos@0
   647
      FOR CUR_BOM_Line IN
carlos@0
   648
        (SELECT *
carlos@0
   649
        FROM C_ORDERLINE l
carlos@0
   650
        WHERE l.C_Order_ID=v_Record_ID
carlos@0
   651
          AND IsActive='Y'
carlos@0
   652
          AND EXISTS
carlos@0
   653
          (SELECT *
carlos@0
   654
          FROM M_PRODUCT p
carlos@0
   655
          WHERE l.M_Product_ID=p.M_Product_ID
carlos@0
   656
            AND p.IsBOM='Y'
carlos@0
   657
            AND p.IsStocked='N'
carlos@0
   658
          )
carlos@0
   659
        ORDER BY l.Line  FOR UPDATE
carlos@0
   660
        )
carlos@0
   661
      LOOP
carlos@0
   662
        v_Line:=CUR_BOM_Line.Line;
carlos@0
   663
        -- One Time variables
carlos@0
   664
        v_FreightAmt:=CUR_BOM_Line.FreightAmt;
carlos@0
   665
        v_ChargeAmt:=CUR_BOM_Line.ChargeAmt;
carlos@0
   666
        -- Create New Lines
carlos@0
   667
        FOR CUR_BOM IN
carlos@0
   668
          (SELECT b.M_ProductBOM_ID,
carlos@0
   669
            p.C_UOM_ID,
carlos@0
   670
            b.BOMQty,
carlos@0
   671
            b.Description
carlos@0
   672
          FROM M_PRODUCT_BOM b
carlos@0
   673
          INNER JOIN M_PRODUCT p
carlos@0
   674
            ON(b.M_ProductBOM_ID=p.M_Product_ID)
carlos@0
   675
          WHERE b.M_Product_ID=CUR_BOM_Line.M_Product_ID
carlos@0
   676
          ORDER BY Line
carlos@0
   677
          )
carlos@0
   678
        LOOP
carlos@0
   679
          Ad_Sequence_Next('C_OrderLine', CUR_BOM_Line.AD_Client_ID, NextNo) ;
carlos@0
   680
          v_Line:=v_Line + 1;
carlos@0
   681
          INSERT
carlos@0
   682
          INTO C_ORDERLINE
carlos@0
   683
            (
carlos@0
   684
              C_OrderLine_ID, AD_Client_ID, AD_Org_ID, IsActive,
carlos@0
   685
              Created, CreatedBy, Updated, UpdatedBy,
carlos@0
   686
              C_Order_ID, Line, C_BPartner_ID, C_BPartner_Location_ID,
carlos@0
   687
              DateOrdered, DatePromised, DateDelivered, DateInvoiced,
carlos@0
   688
              Description, M_Product_ID, M_Warehouse_ID, DirectShip,
carlos@0
   689
              C_UOM_ID, QtyOrdered, QtyReserved, QtyDelivered,
carlos@0
   690
              QtyInvoiced, M_Shipper_ID, C_Currency_ID,
carlos@0
   691
     PriceList,
carlos@0
   692
              PriceActual,
carlos@0
   693
     PriceLimit,
carlos@0
   694
     LineNetAmt, Discount,
carlos@0
   695
              FreightAmt, C_Charge_ID, ChargeAmt, C_Tax_ID,
carlos@0
   696
              QuantityOrder, M_Product_Uom_ID, PriceStd
carlos@0
   697
            )
carlos@0
   698
            VALUES
carlos@0
   699
            (
carlos@0
   700
              NextNo, CUR_BOM_Line.AD_Client_ID, CUR_BOM_Line.AD_Org_ID, CUR_BOM_Line.IsActive,
antonio@735
   701
              now(), 0, now(), 0,
carlos@0
   702
              CUR_BOM_Line.C_Order_ID, v_Line, CUR_BOM_Line.C_BPartner_ID, CUR_BOM_Line.C_BPartner_Location_ID,
carlos@0
   703
              CUR_BOM_Line.DateOrdered, CUR_BOM_Line.DatePromised, CUR_BOM_Line.DateDelivered, CUR_BOM_Line.DateInvoiced,
carlos@0
   704
              CUR_BOM.Description, CUR_BOM.M_ProductBOM_ID, CUR_BOM_Line.M_Warehouse_ID, CUR_BOM_Line.DirectShip,
carlos@0
   705
              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,
carlos@0
   706
              CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, CUR_BOM_Line.M_Shipper_ID, CUR_BOM_Line.C_Currency_ID,
carlos@0
   707
     Bom_Pricelist(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID),
carlos@0
   708
              M_Get_Offers_Price(CUR_BOM_Line.DateOrdered, v_CBPartner_ID, CUR_BOM.M_ProductBOM_ID, Bom_Pricestd(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID), CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, v_PriceList_ID), Bom_Pricelimit(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID),
carlos@0
   709
     M_Get_Offers_Price(CUR_BOM_Line.DateOrdered, v_CBPartner_ID, CUR_BOM.M_ProductBOM_ID, Bom_Pricestd(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID), CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, v_PriceList_ID) * CUR_BOM_Line.QtyOrdered*CUR_BOM.BOMQty, CUR_BOM_Line.Discount,
carlos@0
   710
              v_FreightAmt, CUR_BOM_Line.C_Charge_ID, v_ChargeAmt, CUR_BOM_Line.C_Tax_ID,
carlos@0
   711
              CUR_BOM_Line.QuantityOrder, CUR_BOM_Line.M_Product_UOM_ID, Bom_Pricestd(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID)
carlos@0
   712
            )
carlos@0
   713
            ; -- no Lot/SerNo for BOMs
carlos@0
   714
          -- One Time variables
carlos@0
   715
          v_FreightAmt:=0;
carlos@0
   716
          v_ChargeAmt:=0;
carlos@0
   717
        END LOOP; -- Create New Lines
carlos@0
   718
        -- Convert into Comment Line
carlos@0
   719
        UPDATE C_ORDERLINE
carlos@0
   720
          SET M_Product_ID=NULL,
carlos@0
   721
          PriceList=0,
carlos@0
   722
          PriceActual=0,
carlos@0
   723
          PriceLimit=0,
carlos@0
   724
          LineNetAmt=0,
carlos@0
   725
          FreightAmt=0,
carlos@0
   726
          ChargeAmt=0,
carlos@0
   727
          Description=
carlos@0
   728
          (SELECT p.NAME || ' ' || C_ORDERLINE.Description
carlos@0
   729
          FROM M_PRODUCT p
carlos@0
   730
          WHERE p.M_Product_ID=CUR_BOM_Line.M_Product_ID
carlos@0
   731
          )
carlos@0
   732
          ,
carlos@0
   733
          PriceStd=0,
carlos@0
   734
          M_Offer_ID=NULL
carlos@0
   735
        WHERE C_OrderLine_ID=CUR_BOM_Line.C_OrderLine_ID;
carlos@0
   736
      END LOOP; -- Replace Lines
carlos@0
   737
    END LOOP; -- BOM Loop
carlos@0
   738
  END;
carlos@0
   739
END IF;--FINISH_PROCESS
carlos@0
   740
IF(NOT END_PROCESSING AND NOT FINISH_PROCESS) THEN
carlos@0
   741
 /**************************************************************************
carlos@0
   742
  * Always check and (un) Reserve Inventory  (counterpart: M_InOut_Post)
carlos@0
   743
  *************************************************************************/
carlos@0
   744
 DECLARE
carlos@0
   745
  Cur_ResLine RECORD;
carlos@0
   746
carlos@0
   747
  v_QtySO       NUMBER; -- Reserved
carlos@0
   748
  v_QtyOrderSO     NUMBER;
carlos@0
   749
  v_QtyPO       NUMBER; -- Ordered
carlos@0
   750
  v_QtyOrderPO     NUMBER;
carlos@0
   751
 BEGIN
carlos@0
   752
  v_ResultStr := 'ReserveInventory';
carlos@0
   753
  -- For all lines needing reservation
carlos@0
   754
  FOR Cur_ResLine IN (SELECT l.M_Warehouse_ID, l.M_Product_ID, l.M_AttributeSetInstance_ID, l.C_OrderLine_ID,
carlos@0
   755
        -- Target Level = 0 if DirectShip='Y' or Binding='N'
carlos@0
   756
        (CASE l.DirectShip WHEN 'Y' THEN 0 ELSE (CASE v_IsBinding WHEN 'N' THEN 0 ELSE l.QtyOrdered END) END)
carlos@0
   757
         -l.QtyReserved-l.QtyDelivered AS Qty, l.QUANTITYORDER,
carlos@0
   758
         l.QtyReserved, l.QtyDelivered, l.DatePromised, l.C_UOM_ID,
carlos@0
   759
         l.M_PRODUCT_UOM_ID
carlos@0
   760
       FROM C_ORDERLINE l, M_PRODUCT p
carlos@0
   761
       WHERE l.C_Order_ID=v_Record_ID
carlos@0
   762
         -- Reserve Products (not: services, null products) --
carlos@0
   763
         AND l.M_Product_ID=p.M_Product_ID
carlos@0
   764
         AND p.IsStocked='Y' AND p.ProductType='I'
carlos@0
   765
         -- Target Level = 0 if DirectShip='Y' or Binding='N'
carlos@0
   766
         AND (CASE l.DirectShip WHEN 'Y' THEN 0 ELSE (CASE v_IsBinding WHEN 'N' THEN 0 ELSE l.QtyOrdered END)END)
gorkaion@239
   767
         -l.QtyReserved-l.QtyDelivered <> 0
carlos@0
   768
       FOR UPDATE) LOOP
carlos@0
   769
   -- Qty corrected for SO/PO
carlos@0
   770
   IF (v_DocSubTypeSO IS NOT NULL) THEN
carlos@0
   771
    v_QtySO   := Cur_ResLine.Qty;
carlos@0
   772
    v_QtyOrderSO := NULL;
carlos@0
   773
    IF Cur_ResLine.QtyReserved=0 AND Cur_ResLine.QtyDelivered=0 THEN
carlos@0
   774
       v_QtyOrderSO := Cur_ResLine.QuantityOrder;
carlos@0
   775
    ELSIF Cur_ResLine.M_Product_UOM_ID IS NOT NULL THEN
carlos@0
   776
       v_QtyOrderSO := C_Uom_Convert(v_QtySO, Cur_ResLine.C_UOM_ID, Cur_ResLine.M_Product_UOM_ID, 'Y');
carlos@0
   777
    END IF;
carlos@0
   778
    v_QtyPO   := 0;
carlos@0
   779
    v_QtyOrderPO := NULL;
carlos@0
   780
   ELSE -- PO
carlos@0
   781
    v_QtySO := 0;
carlos@0
   782
    v_QtyOrderSO := NULL;
carlos@0
   783
    v_QtyPO := Cur_ResLine.Qty;
carlos@0
   784
    v_QtyOrderPO := NULL;
carlos@0
   785
    IF Cur_ResLine.QtyReserved=0 AND Cur_ResLine.QtyDelivered=0 THEN
carlos@0
   786
       v_QtyOrderPO := Cur_ResLine.QuantityOrder;
carlos@0
   787
    ELSIF Cur_ResLine.M_Product_UOM_ID IS NOT NULL THEN
carlos@0
   788
       v_QtyOrderPO := C_Uom_Convert(v_QtyPO, Cur_ResLine.C_UOM_ID, Cur_ResLine.M_Product_UOM_ID, 'Y');
carlos@0
   789
    END IF;
carlos@0
   790
   END IF;
antonio@735
   791
   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,
carlos@0
   792
          Cur_ResLine.C_UOM_ID, Cur_ResLine.M_PRODUCT_UOM_ID, v_QtySO, v_QtyOrderSO, v_QtyPO, v_QtyOrderPO);
carlos@0
   793
   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);
carlos@0
   794
carlos@0
   795
   -- Update Order Line
carlos@0
   796
   IF (v_DocSubTypeSO IS NOT NULL) THEN
carlos@0
   797
      UPDATE C_ORDERLINE
carlos@0
   798
      SET QtyReserved = QtyReserved + v_QtySO
carlos@0
   799
    WHERE C_OrderLine_ID = Cur_ResLine.C_OrderLine_ID;
carlos@0
   800
   END IF;
carlos@0
   801
   rowcount:=SQL%ROWCOUNT;
gorkaion@239
   802
   IF (rowcount <> 1) THEN
carlos@0
   803
       IF (p_PInstance_ID IS NOT NULL) THEN
carlos@0
   804
 ROLLBACK;
carlos@0
   805
     v_ResultStr := 'LockingOrder';
carlos@0
   806
     UPDATE C_ORDER
carlos@0
   807
       SET Processing = 'N'
carlos@0
   808
     WHERE C_Order_ID = v_Record_ID;
carlos@0
   809
          RAISE NO_DATA_FOUND;
carlos@0
   810
 COMMIT;
carlos@0
   811
    END IF;
carlos@0
   812
    RAISE_APPLICATION_ERROR(-20011, 'Did not update Line');
carlos@0
   813
   END IF;
carlos@0
   814
  END LOOP; -- For all lines needing reservation
carlos@0
   815
 END; -- Reserve Inventory
carlos@0
   816
  END IF;--FINISH_PROCESS
carlos@0
   817
  IF (NOT FINISH_PROCESS) THEN
carlos@0
   818
 /**************************************************************************
carlos@0
   819
  * Calculate Taxes and Totals
carlos@0
   820
  *************************************************************************/
carlos@0
   821
   DECLARE
carlos@0
   822
    Cur_Tax RECORD;
carlos@0
   823
    Cur_MultiTax RECORD;
carlos@0
   824
    xTotalLines NUMBER:=0;
carlos@0
   825
    v_TaxBaseAmt NUMBER:=0;
carlos@0
   826
    xTaxAmt NUMBER:=0;
carlos@0
   827
    xGrandTotal NUMBER:=0;
carlos@0
   828
    HeaderNotAdded BOOLEAN:=TRUE;
carlos@0
   829
    v_FirstLine BOOLEAN;
carlos@0
   830
    v_ActualBaseAmt NUMBER;
antonio@735
   831
    v_lineNo NUMBER:= 0;
carlos@0
   832
  BEGIN
carlos@0
   833
    v_ResultStr:='DeleteOldTaxes';
carlos@0
   834
    DELETE FROM C_ORDERTAX  WHERE C_Order_ID=v_Record_ID;
carlos@0
   835
    -- For all Tax Rates
carlos@0
   836
    v_ResultStr:='InsertNewTaxes';
carlos@0
   837
    FOR Cur_Tax IN
carlos@0
   838
      (SELECT l.C_Tax_ID, o.IsTaxIncluded,
carlos@0
   839
        SUM(l.LineNetAmt) + SUM(l.FreightAmt) + SUM(l.ChargeAmt) AS LineNet,
carlos@0
   840
        SUM(o.FreightAmt) + SUM(o.ChargeAmt) AS HeaderNet,
carlos@0
   841
        t.Rate, t.IsSummary, c.StdPrecision
carlos@0
   842
      FROM C_ORDER o,  C_ORDERLINE l,
carlos@0
   843
           C_TAX t, C_CURRENCY c
carlos@0
   844
      WHERE o.C_Order_ID=l.C_Order_ID
carlos@0
   845
        AND o.C_Order_ID=v_Record_ID -- Parameter
carlos@0
   846
        AND l.C_Tax_ID=t.C_Tax_ID
carlos@0
   847
        AND o.C_Currency_ID=c.C_Currency_ID
carlos@0
   848
      GROUP BY l.C_Tax_ID,
carlos@0
   849
        o.IsTaxIncluded,
carlos@0
   850
        t.Rate,
carlos@0
   851
        t.IsSummary,
carlos@0
   852
        c.StdPrecision
carlos@0
   853
      ORDER BY 3 DESC
carlos@0
   854
      )
carlos@0
   855
    LOOP
carlos@0
   856
      v_TaxBaseAmt:=Cur_Tax.LineNet;
carlos@0
   857
      xTotalLines:=xTotalLines + v_TaxBaseAmt; -- w/o Header Freight/Charge
carlos@0
   858
carlos@0
   859
      IF(HeaderNotAdded) THEN --  add header net to first tax
carlos@0
   860
        HeaderNotAdded:=FALSE;
carlos@0
   861
        v_TaxBaseAmt:=v_TaxBaseAmt + Cur_Tax.HeaderNet;
carlos@0
   862
      END IF;
carlos@0
   863
      IF(Cur_Tax.IsSummary='N') THEN
carlos@0
   864
        v_LineNo := v_LineNo + 10;
carlos@0
   865
        xTaxAmt:=ROUND(v_TaxBaseAmt * Cur_Tax.Rate / 100, Cur_Tax.StdPrecision) ;
carlos@0
   866
        xGrandTotal:=xGrandTotal + v_TaxBaseAmt + xTaxAmt;
carlos@0
   867
        --
carlos@0
   868
        INSERT
carlos@0
   869
        INTO C_ORDERTAX
carlos@0
   870
          (
carlos@0
   871
            C_Order_ID, C_Tax_ID, AD_Client_ID, AD_Org_ID,
carlos@0
   872
            IsActive, Created, CreatedBy, Updated,
carlos@0
   873
            UpdatedBy, TaxBaseAmt, TaxAmt, Line
carlos@0
   874
          )
carlos@0
   875
          VALUES
carlos@0
   876
          (
carlos@0
   877
            v_Record_ID, Cur_Tax.C_Tax_ID, v_Client_ID, v_Org_ID,
antonio@735
   878
             'Y', now(), v_UpdatedBy, now(),
carlos@0
   879
            v_UpdatedBy, v_TaxBaseAmt, xTaxAmt, v_LineNo
carlos@0
   880
          )
carlos@0
   881
          ;
carlos@0
   882
      ELSE -- Multiple Taxes
carlos@0
   883
        xGrandTotal:=xGrandTotal + v_TaxBaseAmt;
carlos@0
   884
        v_FirstLine := TRUE;
carlos@0
   885
        FOR Cur_MultiTax IN
carlos@0
   886
                          (SELECT C_Tax_ID, Rate, CASCADE
carlos@0
   887
                             FROM C_TAX
carlos@0
   888
                            WHERE Parent_Tax_ID=Cur_Tax.C_Tax_ID
carlos@0
   889
                            ORDER BY Line)
carlos@0
   890
        LOOP
carlos@0
   891
          v_LineNo := v_LineNo + 10;
carlos@0
   892
          IF ((Cur_MultiTax.CASCADE = 'Y') AND (NOT v_FirstLine)) THEN   --cannot apply cascade to the first line
carlos@0
   893
            v_ActualBaseAmt := xGrandTotal;
carlos@0
   894
          ELSE
carlos@0
   895
            v_ActualBaseAmt := v_TaxBaseAmt;
carlos@0
   896
          END IF;
carlos@0
   897
          xTaxAmt:=ROUND(v_ActualBaseAmt * Cur_MultiTax.Rate / 100, Cur_Tax.StdPrecision) ;
carlos@0
   898
          v_FirstLine := FALSE;
carlos@0
   899
          xGrandTotal:=xGrandTotal + xTaxAmt;
carlos@0
   900
          --
carlos@0
   901
          INSERT INTO C_ORDERTAX
carlos@0
   902
            (
carlos@0
   903
              C_Order_ID, C_Tax_ID, AD_Client_ID, AD_Org_ID,
carlos@0
   904
              IsActive, Created, CreatedBy, Updated,
carlos@0
   905
              UpdatedBy, TaxBaseAmt, TaxAmt, Line
carlos@0
   906
            )
carlos@0
   907
            VALUES
carlos@0
   908
            (
carlos@0
   909
              v_Record_ID, Cur_MultiTax.C_Tax_ID, v_Client_ID, v_Org_ID,
antonio@735
   910
               'Y', now(), v_UpdatedBy, now(),
carlos@0
   911
              v_UpdatedBy, v_ActualBaseAmt, xTaxAmt, v_LineNo
carlos@0
   912
            );
carlos@0
   913
        END LOOP;
carlos@0
   914
      END IF;
carlos@0
   915
    END LOOP; -- Insert New Taxes
carlos@0
   916
    -- Update Header
carlos@0
   917
    UPDATE C_ORDER
carlos@0
   918
      SET TotalLines=xTotalLines,
carlos@0
   919
      GrandTotal=xGrandTotal
carlos@0
   920
    WHERE C_Order_ID=v_Record_ID;
carlos@0
   921
    DBMS_OUTPUT.PUT_LINE('GrandTotal=' || xGrandTotal) ;
carlos@0
   922
  END; -- Calculate Tax and Totals
carlos@0
   923
  -- Synchronize Client/Org Ownership
carlos@0
   924
  UPDATE C_ORDERLINE
carlos@0
   925
    SET AD_Client_ID=v_Client_ID,
carlos@0
   926
    AD_Org_ID=v_Org_ID
carlos@0
   927
  WHERE C_Order_ID=v_Record_ID
gorkaion@239
   928
    AND(AD_Client_ID<>v_Client_ID
gorkaion@239
   929
    OR AD_Org_ID<>v_Org_ID) ;
carlos@0
   930
END IF;--FINISH_PROCESS
carlos@0
   931
IF(NOT END_PROCESSING AND NOT FINISH_PROCESS) THEN
carlos@0
   932
  /**************************************************************************
adrian@170
   933
  * Order Complete:5 - Something to do:6
carlos@0
   934
  */
carlos@0
   935
  BEGIN
carlos@0
   936
    v_ResultStr:='OrderCompleteCheck';
carlos@0
   937
    SELECT SUM(QtyOrdered*C_OrderLine_ID) -SUM(QtyDelivered*C_OrderLine_ID),
carlos@0
   938
      SUM(QtyOrdered*C_OrderLine_ID) -SUM(QtyInvoiced*C_OrderLine_ID)
carlos@0
   939
    INTO ToDeliver,
carlos@0
   940
      ToInvoice
carlos@0
   941
    FROM C_ORDERLINE
carlos@0
   942
    WHERE C_Order_ID=v_Record_ID;
carlos@0
   943
    -- If no lines, ToDeliver is NULL
carlos@0
   944
    IF((ToDeliver=0 AND ToInvoice=0) OR(ToDeliver IS NULL AND ToInvoice IS NULL)) THEN
carlos@0
   945
      DBMS_OUTPUT.PUT_LINE('OrderComplete') ;
carlos@0
   946
      IF(v_DocAction='CL') THEN
carlos@0
   947
        END_PROCESSING:=TRUE;
carlos@0
   948
      ELSIF(v_DocAction='VO') THEN
carlos@0
   949
        UPDATE C_ORDER
carlos@0
   950
          SET DocStatus='VO',
carlos@0
   951
          DocAction='--',
carlos@0
   952
          Processed='Y',
antonio@735
   953
          Updated=now()
carlos@0
   954
        WHERE C_Order_ID=v_Record_ID;
carlos@0
   955
		END_PROCESSING:=TRUE;
carlos@0
   956
      ELSE
carlos@0
   957
        UPDATE C_ORDER
carlos@0
   958
          SET DocStatus='CO',
carlos@0
   959
          DocAction='--',
carlos@0
   960
          Processed='Y',
antonio@735
   961
          Updated=now()
carlos@0
   962
        WHERE C_Order_ID=v_Record_ID;
carlos@0
   963
		END_PROCESSING:=TRUE;
carlos@0
   964
      END IF;
carlos@0
   965
      IF(NOT END_PROCESSING) THEN
carlos@0
   966
        RAISE_APPLICATION_ERROR(-20000, '@AlreadyPosted@');
carlos@0
   967
      END IF;--END_PROCESSING
carlos@0
   968
    END IF;
carlos@0
   969
  END;
carlos@0
   970
END IF;--FINISH_PROCESS
carlos@0
   971
IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
carlos@0
   972
  /**
carlos@0
   973
  * In Progress -----------------------------------------------------------
carlos@0
   974
  */
carlos@0
   975
  UPDATE C_ORDER
carlos@0
   976
    SET DocStatus='IP',
carlos@0
   977
    DateAcct=DateOrdered,
antonio@735
   978
    Updated=now()
carlos@0
   979
  WHERE C_Order_ID=v_Record_ID;
carlos@0
   980
  IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   981
    COMMIT;
carlos@0
   982
  END IF;
carlos@0
   983
END IF;--FINISH_PROCESS
carlos@0
   984
IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
carlos@0
   985
  /**
carlos@0
   986
  * Finished with processing
carlos@0
   987
  */
carlos@0
   988
  IF(v_DocAction='PR') THEN
carlos@0
   989
    v_ResultStr:='FinishProcessing';
carlos@0
   990
    UPDATE C_ORDER
carlos@0
   991
      SET DocStatus='IP',
carlos@0
   992
      DocAction='CO',
carlos@0
   993
      Processed='N',
antonio@735
   994
      Updated=now()
carlos@0
   995
    WHERE C_Order_ID=v_Record_ID;
carlos@0
   996
    -- C_Order_PickList(NULL, v_Record_ID);  -- Print PickList
carlos@0
   997
    END_PROCESSING:=TRUE;
carlos@0
   998
  END IF;
carlos@0
   999
END IF;--FINISH_PROCESS
carlos@0
  1000
IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
carlos@0
  1001
  /**************************************************************************
carlos@0
  1002
  * Prepayment Order  Create Invoice
carlos@0
  1003
  *************************************************************************/
gorkaion@239
  1004
  IF(v_DocSubTypeSO='PR' AND v_DocStatus<>'WP') THEN
carlos@0
  1005
    DBMS_OUTPUT.PUT_LINE('Create PreInvoice - ' || v_Record_ID) ;
carlos@0
  1006
    v_ResultStr:='CreatePreInvoice';
antonio@394
  1007
    C_Invoice_Create(NULL, Invoice_ID, v_Record_ID) ;
carlos@0
  1008
    DBMS_OUTPUT.PUT_LINE('  PreInvoice - ' || Invoice_ID) ;
carlos@0
  1009
    IF(Invoice_ID=0) THEN
carlos@0
  1010
      RAISE_APPLICATION_ERROR(-20000, '@PreInvoiceCreateFailed@');
carlos@0
  1011
    END IF;
carlos@0
  1012
    IF(NOT END_PROCESSING) THEN
antonio@735
  1013
      C_INVOICE_POST(NULL, Invoice_ID) ;
carlos@0
  1014
      --
carlos@0
  1015
      UPDATE C_ORDER
carlos@0
  1016
        SET DocStatus='WP',
carlos@0
  1017
        DocAction='--',
carlos@0
  1018
        Processed='Y',
antonio@735
  1019
        Updated=now()
carlos@0
  1020
      WHERE C_Order_ID=v_Record_ID;
carlos@0
  1021
      --
carlos@0
  1022
    END IF;--END_PROCESSING
carlos@0
  1023
    END_PROCESSING:=TRUE;
carlos@0
  1024
  END IF;
carlos@0
  1025
  IF(NOT END_PROCESSING) THEN
carlos@0
  1026
    /**
carlos@0
  1027
    * Deliver Direct Shipments
carlos@0
  1028
    */
carlos@0
  1029
    v_ResultStr:='NonInventoryDelivery';
carlos@0
  1030
    UPDATE C_ORDERLINE
carlos@0
  1031
      SET QtyDelivered=QtyOrdered
carlos@0
  1032
    WHERE DirectShip='Y'
carlos@0
  1033
      AND C_Order_ID=v_Record_ID;
carlos@0
  1034
  END IF;--END_PROCESSING
carlos@0
  1035
END IF;--FINISH_PROCESS
carlos@0
  1036
IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
carlos@0
  1037
  /**************************************************************************
carlos@0
  1038
  * Will-Call + Walk In Processing
carlos@0
  1039
  * --
carlos@0
  1040
  * (W)illCall(I)nvoice - (W)illCall(P)ickup - (W)alkIn(R)eceipt
carlos@0
  1041
  * --
carlos@0
  1042
  *************************************************************************/
carlos@0
  1043
  IF(v_DocSubTypeSO IN('WI', 'WP', 'WR')) THEN
carlos@0
  1044
    /************
carlos@0
  1045
    * Shipment
carlos@0
  1046
    */
carlos@0
  1047
    DBMS_OUTPUT.PUT_LINE('Create Shipment - ' || v_Record_ID) ;
carlos@0
  1048
    v_ResultStr:='CreateShipment';
antonio@735
  1049
    M_Inout_Create(NULL, InOut_ID, v_Record_ID, NULL, 'Y') ; -- Force Delivery
carlos@0
  1050
    DBMS_OUTPUT.PUT_LINE('  Shipment - ' || InOut_ID) ;
carlos@0
  1051
    IF(InOut_ID=0) THEN
carlos@0
  1052
      RAISE_APPLICATION_ERROR(-20000, '@InOutCreateFailed@');
carlos@0
  1053
	ELSE
carlos@0
  1054
	SELECT documentno
carlos@0
  1055
	INTO v_DocumentNo
carlos@0
  1056
	FROM M_INOUT
carlos@0
  1057
	WHERE M_INOUT_ID = InOut_ID;
carlos@0
  1058
	v_Message:='@InoutDocumentno@ ' || v_DocumentNo || ' @beenCreated@';
carlos@0
  1059
    END IF;
carlos@0
  1060
    IF(NOT END_PROCESSING) THEN
carlos@0
  1061
      IF(v_DocSubTypeSO IN('WI', 'WR')) THEN
carlos@0
  1062
        /************
carlos@0
  1063
        * Invoice
carlos@0
  1064
        */
carlos@0
  1065
        DBMS_OUTPUT.PUT_LINE('Create Invoice - ' || v_Record_ID) ;
carlos@0
  1066
        v_ResultStr:='CreateInvoice';
antonio@735
  1067
        C_Invoice_Create(NULL, Invoice_ID, v_Record_ID) ;
carlos@0
  1068
        DBMS_OUTPUT.PUT_LINE('  Invoice - ' || Invoice_ID) ;
carlos@0
  1069
        IF(Invoice_ID IS NULL OR Invoice_ID=0) THEN
carlos@0
  1070
          RAISE_APPLICATION_ERROR(-20000, '@InvoiceCreateFailed@');
carlos@0
  1071
		ELSE
carlos@0
  1072
		SELECT documentno
carlos@0
  1073
	    INTO v_DocumentNo
carlos@0
  1074
	    FROM C_INVOICE
carlos@0
  1075
	    WHERE C_INVOICE_ID = Invoice_ID;
carlos@0
  1076
	    v_Message:=v_Message||' , '||'@InvoiceDocumentno@ ' || v_DocumentNo || ' @invbeenCreated@';
carlos@0
  1077
        END IF;
carlos@0
  1078
      END IF;
carlos@0
  1079
    END IF;--END_PROCESSING
carlos@0
  1080
  END IF;
carlos@0
  1081
END IF;--FINISH_PROCESS
carlos@0
  1082
IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
carlos@0
  1083
  /**
carlos@0
  1084
  * Final Completeness check
carlos@0
  1085
  */
carlos@0
  1086
  SELECT SUM(QtyOrdered*C_OrderLine_ID) -SUM(QtyDelivered*C_OrderLine_ID),
carlos@0
  1087
    SUM(QtyOrdered*C_OrderLine_ID) -SUM(QtyInvoiced*C_OrderLine_ID)
carlos@0
  1088
  INTO ToDeliver,
carlos@0
  1089
    ToInvoice
carlos@0
  1090
  FROM C_ORDERLINE
carlos@0
  1091
  WHERE C_Order_ID=v_Record_ID;
carlos@0
  1092
  DBMS_OUTPUT.PUT_LINE('To deliver - ' || ToDeliver) ;
carlos@0
  1093
  DBMS_OUTPUT.PUT_LINE('ToInvoice - ' || ToInvoice) ;
carlos@0
  1094
  DBMS_OUTPUT.PUT_LINE('v_DocSubTypeSO - ' || v_DocSubTypeSO) ;
carlos@0
  1095
  -- Nothing to Deliver + Invoice for (W)illCall(I)nvoice and (W)alkIn(R)eceipt
carlos@0
  1096
  IF(v_DocSubTypeSO IN('WI', 'WR') AND ToDeliver=0 AND ToInvoice=0) THEN
carlos@0
  1097
    UPDATE C_ORDER
carlos@0
  1098
      SET DocStatus='CO',
carlos@0
  1099
      DocAction='--',
carlos@0
  1100
      IsDelivered='Y',
carlos@0
  1101
      IsInvoiced='Y',
carlos@0
  1102
      Processed='Y',
antonio@735
  1103
      Updated=now()
carlos@0
  1104
    WHERE C_Order_ID=v_Record_ID;
carlos@0
  1105
    DBMS_OUTPUT.PUT_LINE('DocAction - ' || v_DocAction) ;
carlos@0
  1106
    IF(v_DocAction='VO') THEN
carlos@0
  1107
      UPDATE C_ORDER  SET DocStatus='VO'  WHERE C_Order_ID=v_Record_ID;
carlos@0
  1108
    END IF;
carlos@0
  1109
  END IF;
carlos@0
  1110
  -- Nothing to Deliver for (W)illCall(P)ickup (Invoice generated independently)
carlos@0
  1111
  IF(v_DocSubTypeSO='WP' AND ToDeliver=0) THEN
carlos@0
  1112
    UPDATE C_ORDER
carlos@0
  1113
      SET DocStatus='CO',
carlos@0
  1114
      DocAction='--',
carlos@0
  1115
      IsDelivered='Y',
carlos@0
  1116
      Processed='Y',
antonio@735
  1117
      Updated=now()
carlos@0
  1118
    WHERE C_Order_ID=v_Record_ID;
carlos@0
  1119
    IF(v_DocAction='VO') THEN
carlos@0
  1120
      UPDATE C_ORDER  SET DocStatus='VO'  WHERE C_Order_ID=v_Record_ID;
carlos@0
  1121
    END IF;
carlos@0
  1122
  END IF;
carlos@0
  1123
  -- We are done with standard sales orders
carlos@0
  1124
  IF(v_DocAction IN('CO', 'CL', 'VO') AND v_DocSubTypeSO='SO') THEN
carlos@0
  1125
    UPDATE C_ORDER
carlos@0
  1126
      SET DocStatus='CO',
carlos@0
  1127
      DocAction='--',
carlos@0
  1128
      Processed='Y',
antonio@735
  1129
      Updated=now()
carlos@0
  1130
    WHERE C_Order_ID=v_Record_ID;
carlos@0
  1131
  END IF;
carlos@0
  1132
  -- Purchase Orders
carlos@0
  1133
  IF(v_DocAction IN('CO', 'CL', 'VO') AND v_DocSubTypeSO IS NULL) THEN
carlos@0
  1134
    UPDATE C_ORDER
carlos@0
  1135
      SET DocStatus='CO',
carlos@0
  1136
      DocAction='--',
carlos@0
  1137
      Processed='Y',
antonio@735
  1138
      Updated=now()
carlos@0
  1139
    WHERE C_Order_ID=v_Record_ID;
carlos@0
  1140
  END IF;
carlos@0
  1141
  -- Only create cash entry if docAction is Complete
carlos@0
  1142
  IF(v_DocAction NOT IN('CO')) THEN
carlos@0
  1143
    END_PROCESSING:=TRUE;
carlos@0
  1144
  END IF;
carlos@0
  1145
END IF;--FINISH_PROCESS
carlos@0
  1146
IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
carlos@0
  1147
  /**************************************************************************
carlos@0
  1148
  * Create default Cash entry
carlos@0
  1149
  *************************************************************************/
carlos@0
  1150
  DECLARE
carlos@0
  1151
    v_PaymentRule CHAR(1) ;
carlos@0
  1152
    CUR_CB RECORD;
carlos@0
  1153
    v_debtPaymentID NUMBER;
carlos@0
  1154
    v_totalCash NUMBER;
antonio@735
  1155
  v_CB_Curr NUMBER;
carlos@0
  1156
  BEGIN
carlos@0
  1157
    /* ALO
carlos@0
  1158
    */
carlos@0
  1159
    UPDATE C_DEBT_PAYMENT  SET IsValid='Y'  WHERE C_Order_ID=v_Record_ID;
carlos@0
  1160
    SELECT C_ORDER.PAYMENTRULE,
carlos@0
  1161
      C_ORDER.DOCUMENTNO||' - '||C_BPARTNER.NAME||' - '||C_ORDER.GRANDTOTAL,
carlos@0
  1162
      C_ORDER.GRANDTOTAL,
carlos@0
  1163
      C_ORDER.M_WAREHOUSE_ID
carlos@0
  1164
    INTO v_PaymentRule,
carlos@0
  1165
      v_DocumentNo,
carlos@0
  1166
      v_GrandTotal,
carlos@0
  1167
      v_M_Warehouse_ID
carlos@0
  1168
    FROM C_ORDER,
carlos@0
  1169
      C_BPARTNER
carlos@0
  1170
    WHERE C_ORDER.C_BPARTNER_ID=C_BPARTNER.C_BPARTNER_ID
carlos@0
  1171
      AND C_ORDER_ID=v_Record_ID;
carlos@0
  1172
    SELECT MAX(NAME)
carlos@0
  1173
    INTO v_WarehouseName
carlos@0
  1174
    FROM M_WAREHOUSE
carlos@0
  1175
    WHERE M_WAREHOUSE_ID=v_M_Warehouse_ID;
carlos@0
  1176
carlos@0
  1177
    --PaymentRule C, WI and WR have already created DP, this won't be inserted in cashline
carlos@0
  1178
    --because it will processed with the invoice.
carlos@0
  1179
    IF(v_PaymentRule='C') AND (v_DocSubTypeSO NOT IN ('WI', 'WR') OR v_DocSubTypeSO IS NULL) THEN
carlos@0
  1180
      IF(v_CashLine_ID IS NULL OR v_CashLine_ID=0) THEN
carlos@0
  1181
        -- Create CashLine
carlos@0
  1182
        -- Find Defaylt CashBook
carlos@0
  1183
        v_ResultStr:='Find C_CashBook Org_ID=' || v_Org_ID;
carlos@0
  1184
      BEGIN
carlos@0
  1185
        -- First active default Book of Org
carlos@0
  1186
        FOR CUR_CB IN
carlos@0
  1187
          (SELECT cb.C_CashBook_ID,
carlos@0
  1188
            c.ISO_Code,
carlos@0
  1189
            cb.NAME,
carlos@0
  1190
      cb.c_currency_id
carlos@0
  1191
          FROM C_CASHBOOK cb,
carlos@0
  1192
            C_CURRENCY c
carlos@0
  1193
          WHERE cb.AD_Org_ID=v_Org_ID
carlos@0
  1194
            AND cb.C_Currency_ID=c.C_Currency_ID
carlos@0
  1195
            AND cb.IsActive='Y'
carlos@0
  1196
          ORDER BY cb.IsDefault DESC
carlos@0
  1197
          )
carlos@0
  1198
        LOOP
carlos@0
  1199
          IF(v_CashBook_ID IS NULL) THEN
carlos@0
  1200
            v_CashBook_ID:=CUR_CB.C_CashBook_ID;
carlos@0
  1201
            v_ISO_Code:=CUR_CB.ISO_Code;
carlos@0
  1202
      v_CB_Curr:=CUR_CB.C_Currency_ID;
carlos@0
  1203
          ELSIF(CUR_CB.NAME=v_WarehouseName) THEN
carlos@0
  1204
            v_CashBook_ID:=CUR_CB.C_CashBook_ID;
carlos@0
  1205
            v_ISO_Code:=CUR_CB.ISO_Code;
carlos@0
  1206
      v_CB_Curr:=CUR_CB.C_Currency_ID;
carlos@0
  1207
          END IF;
carlos@0
  1208
        END LOOP;
carlos@0
  1209
      END;
carlos@0
  1210
      IF(v_CashBook_ID IS NULL) THEN
carlos@0
  1211
        RAISE_APPLICATION_ERROR(-20000, '@CashBookPRSCnotfoundOrg@' || v_Org_Name) ;
carlos@0
  1212
      END IF;
carlos@0
  1213
      DBMS_OUTPUT.PUT_LINE('CashBook_ID=' || v_CashBook_ID) ;
carlos@0
  1214
      -- Find/Create Cash Journal
carlos@0
  1215
      v_ResultStr:='Find C_Cash for ' || v_Date;
carlos@0
  1216
      DECLARE
carlos@0
  1217
        Cur_CashId RECORD;
carlos@0
  1218
      BEGIN
carlos@0
  1219
        FOR Cur_CashId IN
carlos@0
  1220
          (SELECT C_Cash_ID AS Cash_ID
carlos@0
  1221
          FROM C_CASH
carlos@0
  1222
          WHERE C_CashBook_ID=v_CashBook_ID
carlos@0
  1223
            AND TRUNC(StatementDate)=v_Date
carlos@0
  1224
            AND Processed='N'
carlos@0
  1225
          )
carlos@0
  1226
        LOOP
carlos@0
  1227
          v_Cash_ID:=Cur_CashId.Cash_ID;
carlos@0
  1228
          EXIT;
carlos@0
  1229
        END LOOP;
carlos@0
  1230
      EXCEPTION
carlos@0
  1231
      WHEN NO_DATA_FOUND THEN
carlos@0
  1232
        NULL;
carlos@0
  1233
      END;
carlos@0
  1234
      /**************************************************************************
carlos@0
  1235
      * Credit Multiplier
carlos@0
  1236
      *************************************************************************/
carlos@0
  1237
      DECLARE
carlos@0
  1238
        v_DocBaseType C_DOCTYPE.DocBaseType%TYPE;
carlos@0
  1239
      BEGIN
carlos@0
  1240
        -- Is it a Credit Memo?
carlos@0
  1241
        SELECT DocBaseType
carlos@0
  1242
        INTO v_DocBaseType
carlos@0
  1243
        FROM C_DOCTYPE
carlos@0
  1244
        WHERE C_DocType_ID=v_DocType_ID;
carlos@0
  1245
        IF(v_DocBaseType IN('ARC', 'API')) THEN
carlos@0
  1246
          v_Multiplier:=-1;
carlos@0
  1247
        END IF;
carlos@0
  1248
      END;
carlos@0
  1249
carlos@0
  1250
carlos@0
  1251
carlos@0
  1252
      IF(v_Cash_ID IS NULL) THEN
carlos@0
  1253
        v_ResultStr:='Create C_Cash';
carlos@0
  1254
        Ad_Sequence_Next('C_Cash', v_Org_ID, v_Cash_ID) ;
carlos@0
  1255
        INSERT
carlos@0
  1256
        INTO C_CASH
carlos@0
  1257
          (
carlos@0
  1258
            C_Cash_ID, AD_Client_ID, AD_Org_ID, IsActive,
carlos@0
  1259
            Created, CreatedBy, Updated, UpdatedBy,
carlos@0
  1260
            C_CashBook_ID, NAME, StatementDate, DateAcct,
carlos@0
  1261
            BeginningBalance, EndingBalance, StatementDifference, Processing,
carlos@0
  1262
            Processed, Posted
carlos@0
  1263
          )
carlos@0
  1264
          VALUES
carlos@0
  1265
          (
carlos@0
  1266
            v_Cash_ID, v_Client_ID, v_Org_ID, 'Y',
antonio@735
  1267
            now(), v_UpdatedBy, now(), v_UpdatedBy,
carlos@0
  1268
            v_CashBook_ID, TO_CHAR(v_Date, 'YYYY-MM-DD') || ' ' || v_ISO_Code, v_Date, v_Date,
carlos@0
  1269
            0, 0, 0, 'N',
carlos@0
  1270
             'N', 'N'
carlos@0
  1271
          )
carlos@0
  1272
          ;
carlos@0
  1273
      END IF;
carlos@0
  1274
      /*
carlos@0
  1275
      Create a debt payment for paymentrule=C
carlos@0
  1276
carlos@0
  1277
      Note: for WI and WR we have already created an invoice and its DP, we only have to link it
carlos@0
  1278
      */
carlos@0
  1279
      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)
carlos@0
  1280
      INTO v_totalCash
carlos@0
  1281
      FROM C_DEBT_PAYMENT_V dp
carlos@0
  1282
      WHERE C_Order_ID=v_Record_ID;
carlos@0
  1283
carlos@0
  1284
/*
carlos@0
  1285
      SELECT COUNT(*)
carlos@0
  1286
        INTO v_Aux
carlos@0
  1287
        FROM C_DEBT_PAYMENT DP,
carlos@0
  1288
             C_INVOICE I
carlos@0
  1289
       WHERE I.C_ORDER_ID = v_Record_ID
carlos@0
  1290
         AND DP.C_INVOICE_ID = I.C_INVOICE_ID;
carlos@0
  1291
carlos@0
  1292
      IF v_Aux != 0 THEN -- DP already created
carlos@0
  1293
        SELECT dp.C_DEBT_PAYMENT_ID
carlos@0
  1294
          INTO v_debtPaymentID
carlos@0
  1295
          FROM C_DEBT_PAYMENT DP,
carlos@0
  1296
               C_INVOICE I
carlos@0
  1297
         WHERE I.C_ORDER_ID = v_Record_ID
carlos@0
  1298
           AND DP.C_INVOICE_ID = I.C_INVOICE_ID;
carlos@0
  1299
carlos@0
  1300
        UPDATE C_DEBT_PAYMENT
carlos@0
  1301
           SET C_ORDER_ID = v_Record_ID
carlos@0
  1302
         WHERE C_DEBT_PAYMENT_ID = v_debtPaymentID;
carlos@0
  1303
      ELSE     */
carlos@0
  1304
        Ad_Sequence_Next('C_Debt_Payment', v_Record_ID, v_debtPaymentID) ;
carlos@0
  1305
        INSERT INTO C_DEBT_PAYMENT
carlos@0
  1306
         (C_DEBT_PAYMENT_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
  1307
          CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
  1308
          ISRECEIPT, C_SETTLEMENT_CANCEL_ID, C_SETTLEMENT_GENERATE_ID, DESCRIPTION,
carlos@0
  1309
          C_ORDER_ID, C_BPARTNER_ID, C_CURRENCY_ID, C_CASHLINE_ID,
carlos@0
  1310
          C_BANKACCOUNT_ID, C_CASHBOOK_ID, PAYMENTRULE, ISPAID,
carlos@0
  1311
          AMOUNT, WRITEOFFAMT, DATEPLANNED, ISMANUAL,
carlos@0
  1312
          ISVALID, C_BANKSTATEMENTLINE_ID, CHANGESETTLEMENTCANCEL, CANCEL_PROCESSED,
carlos@0
  1313
          GENERATE_PROCESSED, c_project_id,IsAutomaticGenerated, STATUS_INITIAL)
carlos@0
  1314
        VALUES
carlos@0
  1315
         (v_debtPaymentID, v_Client_ID, v_Org_ID, 'Y',
antonio@735
  1316
          now(), v_UpdatedBy, now(), v_UpdatedBy,
carlos@379
  1317
          v_isSoTrx, NULL, NULL, '',
carlos@0
  1318
          v_Record_ID, v_c_Bpartner_Id, v_c_currency_id, null,
carlos@0
  1319
          NULL, v_CashBook_ID, 'C', 'N',
carlos@0
  1320
          (v_GrandTotal-v_totalCash), 0, v_Date, 'N',
carlos@0
  1321
           'Y', NULL, 'N', 'N',
carlos@0
  1322
           'N', v_C_PROJECT_ID,'Y', 'DE');
carlos@0
  1323
    --  END IF;
carlos@0
  1324
carlos@0
  1325
      DBMS_OUTPUT.PUT_LINE('  Cash_ID=' || v_Cash_ID) ;
carlos@0
  1326
      -- Create CashJournal Line in invoice currency
carlos@0
  1327
      v_ResultStr:='Create C_CashLine';
carlos@0
  1328
      Ad_Sequence_Next('C_CashLine', v_Org_ID, v_CashLine_ID) ;
carlos@0
  1329
carlos@0
  1330
      SELECT COALESCE(MAX(Line), 0) +10
carlos@0
  1331
      INTO v_Line
carlos@0
  1332
      FROM C_CASHLINE
carlos@0
  1333
      WHERE C_Cash_ID=v_Cash_ID;
carlos@0
  1334
      --
carlos@0
  1335
      INSERT
carlos@0
  1336
      INTO C_CASHLINE
carlos@0
  1337
        (
carlos@0
  1338
          C_CashLine_ID, AD_Client_ID, AD_Org_ID, IsActive,
carlos@0
  1339
          Created, CreatedBy, Updated, UpdatedBy,
carlos@0
  1340
          C_Cash_ID, C_Debt_Payment_ID, Line, Description,
carlos@0
  1341
          Amount, CashType, DiscountAmt, WriteOffAmt,
carlos@0
  1342
          IsGenerated
carlos@0
  1343
        )
carlos@0
  1344
        VALUES
carlos@0
  1345
        (
carlos@0
  1346
          v_CashLine_ID, v_Client_ID, v_Org_ID, 'Y',
antonio@735
  1347
          now(), v_UpdatedBy, now(), v_UpdatedBy,
carlos@0
  1348
          v_Cash_ID, v_debtPaymentID, v_Line, v_DocumentNo,
carlos@379
  1349
          (v_GrandTotal-v_totalCash) * (CASE WHEN v_isSoTrx='N' THEN -1 ELSE 1 END), 'P', 0, 0,
carlos@0
  1350
           'Y'
carlos@0
  1351
        )
carlos@0
  1352
        ;
carlos@0
  1353
      DBMS_OUTPUT.PUT_LINE('  CashLine_ID=' || v_CashLine_ID) ;
carlos@0
  1354
carlos@0
  1355
carlos@0
  1356
    END IF; -- CashLine_ID IS NULL OR CashLine_ID = 0
carlos@0
  1357
  END IF; -- v_PaymentRule = 'C'
carlos@0
  1358
END;
carlos@0
  1359
END IF;--FINISH_PROCESS
carlos@0
  1360
IF(NOT FINISH_PROCESS) THEN
carlos@0
  1361
  -- End Processing --------------------------------------------------------
gorkaion@239
  1362
  --<<END_PROCESSING>>
carlos@0
  1363
  -- Cloase Order
carlos@0
  1364
  IF(v_DocAction='CL') THEN
carlos@0
  1365
    UPDATE C_ORDER
carlos@0
  1366
      SET DocStatus='CL',
carlos@0
  1367
      DocAction='--',
carlos@0
  1368
      Processed='Y'
carlos@0
  1369
    WHERE C_Order_ID=v_Record_ID;
carlos@0
  1370
  END IF;
carlos@0
  1371
  IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
  1372
    v_ResultStr:='UnLockingOrder';
carlos@0
  1373
    UPDATE C_ORDER
carlos@0
  1374
      SET Processing='N',
antonio@735
  1375
      Updated=now(),
carlos@0
  1376
      UpdatedBy=v_User
carlos@0
  1377
    WHERE C_Order_ID=v_Record_ID;
carlos@0
  1378
    COMMIT;
carlos@0
  1379
  END IF;
carlos@0
  1380
END IF;--FINISH_PROCESS
gorkaion@239
  1381
--<<FINISH_PROCESS>>
carlos@0
  1382
IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
  1383
  --  Update AD_PInstance
carlos@0
  1384
  DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished - ' || v_Message) ;
antonio@735
  1385
  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
carlos@0
  1386
END IF;
carlos@0
  1387
RETURN;
carlos@0
  1388
END; --BODY
carlos@0
  1389
EXCEPTION
carlos@0
  1390
WHEN OTHERS THEN
carlos@0
  1391
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
  1392
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
  1393
  IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
  1394
    ROLLBACK;
carlos@0
  1395
    --Inserted by Carlos Romero 062706
carlos@0
  1396
    UPDATE C_ORDER  SET Processing='N'  WHERE C_Order_ID=v_Record_ID;
carlos@0
  1397
    DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
antonio@735
  1398
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
  1399
  ELSE
carlos@0
  1400
    DECLARE
carlos@0
  1401
      v_Code NUMBER:=SQLCODE;
carlos@0
  1402
    BEGIN
asier@799
  1403
      AD_SEQUENCE_NEXT('DBA_ERRORLOG', v_Client_ID, v_DBA_ErrorLog_ID);
carlos@0
  1404
      INSERT
carlos@0
  1405
      INTO DBA_ERRORLOG
carlos@0
  1406
        (
carlos@0
  1407
          DBA_ErrorLog_ID, Created,
carlos@0
  1408
          Code, Msg,
carlos@0
  1409
          Info
carlos@0
  1410
        )
carlos@0
  1411
        VALUES
carlos@0
  1412
        (
asier@799
  1413
          v_DBA_ErrorLog_ID, now(),
carlos@0
  1414
          v_Code, v_ResultStr,
carlos@0
  1415
           'C_Order_Post'
carlos@0
  1416
        )
carlos@0
  1417
        ;
carlos@0
  1418
    END;
carlos@0
  1419
    RAISE;
carlos@0
  1420
  END IF;
carlos@0
  1421
  RETURN;
antonio@735
  1422
END C_ORDER_POST1
gorkaion@239
  1423
]]></body>
carlos@0
  1424
    </function>
adrian@94
  1425
  </database>