src-db/database/model/functions/C_ORDER_POST1.xml
author Antonio Moreno <antonio.moreno@openbravo.com>
Wed, 23 Apr 2008 17:34:12 +0000
changeset 756 ae11e4610537
parent 735 database/model/functions/C_ORDER_POST1.xml@daced7e311c9
child 799 fef2c5e2feb7
permissions -rw-r--r--
Moved database folder to src-db folder
<?xml version="1.0"?>
  <database name="FUNCTION C_ORDER_POST1">
    <function name="C_ORDER_POST1" type="NULL">
      <parameter name="p_pinstance_id" type="NUMERIC" mode="in">
        <default/>
      </parameter>
      <parameter name="p_order_id" type="NUMERIC" mode="in">
        <default/>
      </parameter>
      <body><![CDATA[/*************************************************************************
* The contents of this file are subject to the Openbravo  Public  License
* Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
* Version 1.1  with a permitted attribution clause; you may not  use this
* file except in compliance with the License. You  may  obtain  a copy of
* the License at http://www.openbravo.com/legal/license.html
* Software distributed under the License  is  distributed  on  an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific  language  governing  rights  and  limitations
* under the License.
* The Original Code is Openbravo ERP.
* The Initial Developer of the Original Code is Openbravo SL
* All portions are Copyright (C) 2001-2006 Openbravo SL
* All Rights Reserved.
* Contributor(s):  ______________________________________.
************************************************************************/
  -- Logistics
  v_ResultStr VARCHAR2(2000):='';
  v_Message VARCHAR2(2000):='';
  v_Record_ID NUMBER;
  v_User NUMBER;
  v_IsProcessing CHAR(1) ;
  v_IsProcessed CHAR(1) ;
  v_Result NUMBER:=1; -- Success
  -- Parameter
  TYPE RECORD IS REF CURSOR;
    Cur_Parameter RECORD;
    -- Record Info
    v_Client_ID NUMBER;
    v_Org_ID NUMBER;
	v_Org_Name VARCHAR2(60);
    v_UpdatedBy NUMBER;
    v_DocAction CHAR(2) ;
    v_DocStatus CHAR(2) ;
    v_InvoiceRule CHAR(1) ;
    v_M_Warehouse_ID NUMBER;
    v_DocType_ID NUMBER;
    v_DocTypeTarget_ID NUMBER;
    v_DocSubTypeSO CHAR(2) ;
    v_IsBinding CHAR(1):='Y';
    v_IsApproved CHAR(1) ;
    --
    ToDeliver NUMBER;
    ToInvoice NUMBER;
    --
    InOut_ID NUMBER;
    Invoice_ID NUMBER;
    --Added by P.SAROBE
	v_documentno_Settlement VARCHAR2(40);
	v_dateSettlement DATE;
	v_Cancel_Processed CHAR(1);
	v_nameBankstatement VARCHAR2 (60);
	v_dateBankstatement DATE;
	v_nameCash VARCHAR2 (60);
	v_dateCash DATE;
	v_Bankstatementline_ID NUMBER;
	--Finish added by P.Sarobe
    v_AuxCashLine_ID NUMBER;
    v_CashLine_ID NUMBER;
	v_ispaid CHAR(1);
	v_Settlement_Cancel_ID NUMBER;
    v_Cash_ID NUMBER:=NULL;
    v_Line NUMBER:=0;
    v_CashBook_ID NUMBER:=NULL;
	v_Debtpayment_ID NUMBER;
    v_ISO_Code VARCHAR(10) ;
    v_DocumentNo VARCHAR2(200) ;
    v_GrandTotal NUMBER;
    v_Multiplier NUMBER:=1;
    v_Date DATE;
    v_WarehouseName VARCHAR2(60) ;
    v_CashProcessed NUMBER;
    v_count NUMBER;
    v_isSoTrx CHAR(1) ;
    v_Aux NUMBER;
    v_c_Bpartner_Id NUMBER;
    v_c_currency_id NUMBER;
    v_C_PROJECT_ID NUMBER;
    v_PriceList_ID NUMBER;
    FINISH_PROCESS BOOLEAN:=FALSE;
    END_PROCESSING BOOLEAN:=FALSE;
    v_CBPartner_ID NUMBER;
    rowcount NUMBER;
  BEGIN
    IF(p_PInstance_ID IS NOT NULL) THEN
      --  Update AD_PInstance
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
      v_ResultStr:='PInstanceNotFound';
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
      -- Get Parameters
      v_ResultStr:='ReadingParameters';
      FOR Cur_Parameter IN
        (SELECT i.Record_ID,
          i.AD_User_ID,
          p.ParameterName,
          p.P_String,
          p.P_Number,
          p.P_Date
        FROM AD_PINSTANCE i
        LEFT JOIN AD_PINSTANCE_PARA p
          ON i.AD_PInstance_ID=p.AD_PInstance_ID
        WHERE i.AD_PInstance_ID=p_PInstance_ID
        ORDER BY p.SeqNo
        )
      LOOP
        v_Record_ID:=Cur_Parameter.Record_ID;
        v_User:=Cur_Parameter.AD_User_ID;
      END LOOP; -- Get Parameter
    ELSE
      v_Record_ID:=p_Order_ID;
      SELECT CREATEDBY INTO v_User  FROM C_ORDER  WHERE C_ORDER_ID=p_Order_ID;
    END IF;
    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
  BEGIN --BODY
    /**
    * Read Order
    */
    v_ResultStr:='ReadingOrder';
    SELECT Processing, Processed, DocAction, DocStatus,
      C_DocType_ID, C_DocTypeTarget_ID, IsApproved, AD_Client_ID,
      AD_Org_ID, UpdatedBy, M_Warehouse_ID, TRUNC(DateOrdered),
      Issotrx, c_Bpartner_Id, c_currency_id, C_PROJECT_ID,
      C_BPartner_ID, M_PriceList_ID, invoicerule
    INTO v_IsProcessing, v_IsProcessed, v_DocAction, v_DocStatus,
      v_DocType_ID, v_DocTypeTarget_ID, v_IsApproved, v_Client_ID,
      v_Org_ID, v_UpdatedBy, v_M_Warehouse_ID, v_Date,
      v_isSoTrx, v_c_Bpartner_Id, v_c_currency_id, v_C_PROJECT_ID,
      v_CBPartner_ID, v_PriceList_ID, v_invoicerule
    FROM C_ORDER
    WHERE C_Order_ID=v_Record_ID  FOR UPDATE;
    -- Get current DocSubTypeSO
    SELECT DocSubTypeSO
    INTO v_DocSubTypeSO
    FROM C_DOCTYPE
    WHERE C_DocType_ID=v_DocType_ID;
	-- Get the name of the org of the Order. Added by P.Sarobe
	SELECT name INTO v_Org_Name FROM AD_ORG WHERE ad_org_id = v_Org_ID;
    DBMS_OUTPUT.PUT_LINE('DocAction=' || v_DocAction || ', DocStatus=' || v_DocStatus || ', DocType_ID=' || v_DocType_ID || ', DocTypeTarget_ID=' || v_DocTypeTarget_ID || ', IsApproved=' || v_IsApproved || ', DocSubTypeSO=' || v_DocSubTypeSO) ;
    /**
    * Order Closed, Voided or Reversed - No action possible
    */
    IF(v_DocStatus IN('CL', 'VO', 'RE')) THEN
      RAISE_APPLICATION_ERROR(-20000, '@AlreadyPosted@') ;
    END IF;
    IF(NOT FINISH_PROCESS) THEN
      /**
      * Waiting on Prepayment  can only be closed
      */
      IF(v_DocStatus='WP' AND v_DocAction<>'CL') THEN
        RAISE_APPLICATION_ERROR(-20000, '@WaitingPayment@') ;
      END IF;
    END IF;--FINISH_PROCESS
    IF (v_DocSubTypeSO='PR' AND v_invoicerule <> 'I') THEN
      RAISE_APPLICATION_ERROR(-20000, '@PrepayMustImmediate@');
    END IF;
    IF(NOT FINISH_PROCESS) THEN
      /**
      * Unlock
      */
      IF(v_DocAction='XL') THEN
        UPDATE C_ORDER
          SET Processing='N',
          DocAction='--',
          Updated=now()
        WHERE C_Order_ID=v_Record_ID;
        FINISH_PROCESS:=TRUE;
      END IF;
    END IF;--FINISH_PROCESS
    IF(NOT FINISH_PROCESS) THEN
      IF(v_IsProcessing='Y') THEN
        RAISE_APPLICATION_ERROR(-20000, '@OtherProcessActive@') ;
      END IF;
    END IF;--FINISH_PROCESS
    IF(NOT FINISH_PROCESS) THEN
      /**
      * Re-activate
      */
      IF(v_DocAction='RE') THEN
        --Verify not managed debtPayments added by ALO
		--Added by P.Sarobe. New messages
        SELECT max(c_debt_payment_id), COUNT(*)
        INTO v_Debtpayment_ID, v_Aux
        FROM C_DEBT_PAYMENT
        WHERE C_Order_ID=v_Record_ID
          AND C_Debt_Payment_Status(C_Settlement_Cancel_ID, Cancel_Processed, Generate_Processed, IsPaid, IsValid, C_CashLine_ID, C_BankStatementLine_ID)!='P';
        IF v_Aux!=0 THEN
		--Added by P.Sarobe. New messages
		  SELECT c_Bankstatementline_Id, c_cashline_id, c_settlement_cancel_id, ispaid, cancel_processed
		  INTO v_Bankstatementline_ID, v_CashLine_ID, v_Settlement_Cancel_ID, v_ispaid, v_Cancel_Processed
		  FROM C_DEBT_PAYMENT WHERE C_Debt_Payment_ID = v_Debtpayment_ID;
		  	   IF v_Bankstatementline_ID IS NOT NULL THEN
			   	 SELECT C_BANKSTATEMENT.NAME, C_BANKSTATEMENT.STATEMENTDATE
				 INTO v_nameBankstatement, v_dateBankstatement
				 FROM C_BANKSTATEMENT, C_BANKSTATEMENTLINE
				 WHERE C_BANKSTATEMENT.C_BANKSTATEMENT_ID = C_BANKSTATEMENTLINE.C_BANKSTATEMENT_ID
				 AND C_BANKSTATEMENTLINE.C_BANKSTATEMENTLINE_ID = v_Bankstatementline_ID;
          		 RAISE_APPLICATION_ERROR(-20000, '@ManagedDebtPaymentOrderBank@'||v_nameBankstatement||' '||'@Bydate@'||v_dateBankstatement) ;
			   END IF;
			   IF v_CashLine_ID IS NOT NULL THEN
			   	 SELECT C_CASH.NAME, C_CASH.STATEMENTDATE
				 INTO v_nameCash, v_dateCash
				 FROM C_CASH, C_CASHLINE
				 WHERE C_CASH.C_CASH_ID = C_CASHLINE.C_CASH_ID
				 AND C_CASHLINE.C_CASHLINE_ID = v_CashLine_ID;
          		 RAISE_APPLICATION_ERROR(-20000, '@ManagedDebtPaymentOrderCash@'||v_nameCash||' '||'@Bydate@'||v_dateCash) ;
			   END IF;
			   IF v_Cancel_Processed='Y' AND v_ispaid='N' THEN
			   	 SELECT documentno, datetrx
				 INTO v_documentno_Settlement, v_dateSettlement
				 FROM C_SETTLEMENT
				 WHERE C_SETTLEMENT_ID = v_Settlement_Cancel_ID;
				 RAISE_APPLICATION_ERROR(-20000, '@ManagedDebtPaymentOrderCancel@'||v_documentno_Settlement||' '||'@Bydate@'||v_dateSettlement) ;
			   END IF;
        END IF;
        IF(NOT FINISH_PROCESS) THEN
          DBMS_OUTPUT.PUT_LINE('Re-Activating ' || v_DocSubTypeSO || ': ' || v_Record_ID) ;
          IF(v_DocSubTypeSO IN('WI', 'WP', 'WR')) THEN
            -- Cancel existing Deli very + Invoice Documents
            M_INOUT_CANCEL(NULL, v_Record_ID) ;
            IF(v_DocSubTypeSO<>'WP') THEN
              C_INVOICE_CANCEL(NULL, v_Record_ID);
            END IF;
          END IF;
          -- Update Order
          v_ResultStr:='ReActivate';
          UPDATE C_ORDER
            SET DocStatus='IP', -- In Progress
            DocAction='CO',
            Processing='N',
            Processed='N',
            Updated=now()
          WHERE C_Order_ID=v_Record_ID;
          --ADDED BY E.ARGAL
          --Invalidate debt payments added by ALO
          UPDATE C_DEBT_PAYMENT
            SET IsValid='N'
          WHERE C_Order_ID=v_Record_ID
            AND IsAutomaticGenerated='N';

          DELETE
          FROM C_CASHLINE
          WHERE C_Debt_Payment_Id IN
            (SELECT C_DEBT_PAYMENT_ID
            FROM C_DEBT_PAYMENT
            WHERE C_Order_ID=v_Record_ID
              AND COALESCE(IsAutomaticGenerated, 'Y')='Y'
            )
            ;
          DELETE
          FROM C_DEBT_PAYMENT
          WHERE C_Order_ID=v_Record_ID
            AND COALESCE(IsAutomaticGenerated, 'Y')='Y';
          DECLARE
            Cur_ResLine RECORD;
            v_QtySO NUMBER; -- Reserved
            v_QtyOrderSO NUMBER;
            v_QtyPO NUMBER; -- Ordered
            v_QtyOrderPO NUMBER;
          BEGIN
            v_ResultStr:='ReserveInventory';
            -- For all lines needing reservation
            FOR Cur_ResLine IN
              (SELECT l.M_Warehouse_ID,
                l.M_Product_ID,
                l.M_AttributeSetInstance_ID,
                l.C_OrderLine_ID,
                -- Target Level = 0 if DirectShip='Y' or Binding='N'
                l.QtyOrdered AS Qty,
                l.QUANTITYORDER,
                l.C_UOM_ID,
                l.M_PRODUCT_UOM_ID
              FROM C_ORDERLINE l,
                M_PRODUCT p
              WHERE l.C_Order_ID=v_Record_ID  -- Reserve Products (not: services, null products) --
                AND l.M_Product_ID=p.M_Product_ID
                AND p.IsStocked='Y'
                AND p.ProductType='I'  FOR UPDATE
              )
            LOOP
              -- Qty corrected for SO/PO
              IF(v_DocSubTypeSO IS NULL) THEN
                v_QtySO:=0;
                v_QtyOrderSO:=NULL;
                v_QtyPO:=-Cur_ResLine.Qty;
                v_QtyOrderPO:=-Cur_ResLine.QuantityOrder;
              END IF;
              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) ;
            END LOOP;
          END;
        END IF;--FINISH_PROCESS
        IF(NOT FINISH_PROCESS) THEN
          --ADDED BY P.SAROBE but to be deprecated 26052007
          SELECT MAX(C_CASHLINE_ID)
          INTO v_CashLine_ID
          FROM C_CASHLINE
          WHERE C_ORDER_ID=v_Record_ID;
          IF(v_CashLine_ID IS NOT NULL) THEN
            SELECT PROCESSED
            INTO v_IsProcessed
            FROM C_CASH,
              C_CASHLINE
            WHERE C_CASH.C_CASH_ID=C_CASHLINE.C_CASH_ID
              AND C_CASHLINE_ID=v_CashLine_ID;
            IF(v_IsProcessed='N') THEN
              DELETE FROM C_CASHLINE WHERE C_CASHLINE_ID=v_CashLine_ID;
            ELSE
			  SELECT C_CASH.NAME, C_CASH.STATEMENTDATE, C_CASHLINE.LINE
				 INTO v_nameCash, v_dateCash, v_Line
				 FROM C_CASH, C_CASHLINE
				 WHERE C_CASH.C_CASH_ID = C_CASHLINE.C_CASH_ID
				 AND C_CASHLINE.C_CASHLINE_ID = v_CashLine_ID;
              RAISE_APPLICATION_ERROR(-20000, '@Ordercahslineprocessed@'||v_nameCash||' '||'@Bydate@'||v_dateCash||' '||'@line@'||v_Line) ;
              /*  ALO
              SELECT CB.C_CASHBOOK_ID, C_CURRENCY.ISO_CODE
              INTO v_CashBook_ID, v_ISO_Code
              FROM C_CASH C, C_CASHLINE CL, C_CASHBOOK CB, C_CURRENCY
              WHERE C.C_CASH_ID = CL.C_CASH_ID
              AND C.C_CASHBOOK_ID = CB.C_CASHBOOK_ID
              AND CB.C_CURRENCY_ID = C_CURRENCY.C_CURRENCY_ID
              AND CL.C_CASHLINE_ID = v_CashLine_ID;
              SELECT MAX(C.C_CASH_ID) INTO v_Cash_ID
              FROM C_CASH C
              WHERE C.C_CASHBOOK_ID=v_CashBook_ID
              AND C.DATEACCT = v_Date
              AND C.PROCESSED = 'N';
              IF (v_Cash_Id IS NULL) THEN
              v_ResultStr := 'Create C_Cash';
              Ad_Sequence_Next('C_Cash', v_Org_ID, v_Cash_ID);
              INSERT INTO C_CASH
              (C_Cash_ID,AD_Client_ID, AD_Org_ID,
              IsActive, Created,CreatedBy, Updated,UpdatedBy,
              C_CashBook_ID, Name,
              StatementDate,DateAcct,
              BeginningBalance,EndingBalance,StatementDifference,
              Processing,Processed,Posted)
              VALUES
              (v_Cash_ID, v_Client_ID, v_Org_ID,
              'Y', now(),UpdatedBy, now(),UpdatedBy,
              v_CashBook_ID, TO_CHAR(v_Date, 'YYYY-MM-DD') || ' ' || v_ISO_Code,
              v_Date, v_Date,
              0,0,0, 'N','N','N');
              END IF;
              v_ResultStr := 'Create negative C_CashLine';
              v_AuxCashLine_ID := v_CashLine_ID;
              Ad_Sequence_Next('C_CashLine', v_Org_ID, v_CashLine_ID);
              SELECT COALESCE(MAX(LINE),0) + 10 INTO v_Line FROM C_CASH, C_CASHLINE
              WHERE C_CASH.C_CASH_ID = C_CASHLINE.C_CASH_ID
              AND C_CASH.C_CASH_ID = v_Cash_ID;
              INSERT INTO C_CASHLINE
              (C_CashLine_ID,AD_Client_ID,AD_Org_ID,
              IsActive,Created,CreatedBy,Updated,UpdatedBy,
              C_Cash_ID, C_Order_ID,
              Line, Description,Amount,CashType,
              DiscountAmt, WriteOffAmt, IsGenerated)
              SELECT v_CashLine_ID, AD_Client_ID, AD_Org_ID, IsActive,Created,CreatedBy,Updated,UpdatedBy,
              v_Cash_ID, C_Order_ID, v_Line, Description,-Amount,CashType,
              DiscountAmt, WriteOffAmt, IsGenerated
              FROM C_CASHLINE
              WHERE C_CASHLINE_ID= v_AuxCashLine_ID;
              */
            END IF;
          END IF;--FINISH_PROCESS
        END IF;

        UPDATE C_ORDER
            SET DocStatus='DR', -- Draft
            DocAction='CO',
            Processing='N',
            Updated=now()
          WHERE C_Order_ID=v_Record_ID;
        FINISH_PROCESS:=TRUE;

      END IF;
    END IF;--FINISH_PROCESS
    IF(NOT FINISH_PROCESS) THEN
    SELECT COUNT(*)
     INTO v_Count
    FROM C_ORDER C,
         C_DOCTYPE
   WHERE C_DocType.DocBaseType IN ('SOO', 'POO')
     AND C_DocType.IsSOTrx=C.ISSOTRX
    AND AD_ISORGINCLUDED(C.AD_Org_ID,C_DocType.AD_Org_ID, C.AD_Client_ID) <> -1
    AND C.C_DOCTYPETARGET_ID = C_DOCTYPE.C_DOCTYPE_ID
    AND C.C_ORDER_ID = v_Record_ID;
   IF v_Count=0 THEN
		RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgDoctypeOrder@') ;
      END IF;

  -- Set org lines like the headear
     UPDATE C_ORDERLINE
      SET AD_ORG_ID = (SELECT AD_ORG_ID FROM C_ORDER WHERE C_ORDER_ID = v_Record_ID)
    WHERE C_ORDER_ID = v_Record_ID;

      /**
      * Close Order - prepare
      */
      IF(v_DocAction='CL') THEN
        -- Cancel undelivered Items
        IF(v_isSoTrx='Y') THEN --Sales orders
          UPDATE C_ORDERLINE
            SET QtyOrdered=QtyDelivered,
            --MODIFIED BY P.SAROBE
            linenetamt=QtyDelivered*priceactual,
            --MODIFIED BY F.IRIAZABAL
            QuantityOrder=QuantityOrder,
            Updated=now()
          WHERE C_Order_ID=v_Record_ID
            AND QtyOrdered<>QtyDelivered;
          -- For Purchase orders, M_MatchPO table used. Notice that only delivered lines(C_Invoiceline_Id is null) using
        ELSE
          UPDATE C_ORDERLINE
            SET QTYORDERED=COALESCE
            (
              (SELECT SUM(M_MATCHPO.QTY)
              FROM M_MATCHPO
              WHERE M_MATCHPO.C_ORDERLINE_ID=C_ORDERLINE.C_ORDERLINE_ID
                AND M_MATCHPO.C_INVOICELINE_ID IS NULL
              )
              , 0
            )
            ,
            QuantityOrder=C_ORDERLINE.QuantityOrder,
            Updated=now(),
            linenetamt=COALESCE
            (
              (SELECT SUM(M_MATCHPO.QTY)
              FROM M_MATCHPO
              WHERE M_MATCHPO.C_ORDERLINE_ID=C_ORDERLINE.C_ORDERLINE_ID
                AND M_MATCHPO.C_INVOICELINE_ID IS NULL
              )
              , 0
            )
            *priceactual
          WHERE C_ORDERLINE.C_ORDER_ID=v_Record_ID;
        END IF;
        -- if there is no change, the tax calculation, etc. is not needed.
        -- potential problem, if posted (i.e. encumbered) for full amount
        -- and the rest then cancelled out.
      END IF;
    END IF;--FINISH_PROCESS
    IF(NOT FINISH_PROCESS) THEN
      /**
      * Void Order - prepare
      */
      IF(v_DocAction='VO') THEN
        -- Cancel all Items
        UPDATE C_ORDERLINE
          SET QtyOrdered=0,
          --MODIFIED BY F.IRIAZABAL
          QuantityOrder=
          CASE WHEN M_PRODUCT_UOM_ID IS NULL THEN NULL ELSE 0
          END
          ,
          LineNetAmt=0,
          Updated=now()
        WHERE C_Order_ID=v_Record_ID
          AND QtyOrdered<>0;
      END IF;
    END IF;--FINISH_PROCESS
    IF(NOT FINISH_PROCESS) THEN
 /**************************************************************************
  * Start Processing ------------------------------------------------------
  *************************************************************************/
    /**************************************************************************
      * Start Processing ------------------------------------------------------
      *************************************************************************/
      IF(p_PInstance_ID IS NOT NULL) THEN
        v_ResultStr:='LockingOrder';
        UPDATE C_ORDER  SET Processing='Y'  WHERE C_Order_ID=v_Record_ID;
        COMMIT;
      END IF;
      -- Now, needs to go to END_PROCESSING to unlock
      /**
      * Allowed Actions:  AProve, COmplete, PRocess, CLose, VOid
      */
      IF(v_DocAction IN('AP', 'CO', 'PR', 'CL', 'VO')) THEN
        NULL;
      ELSE
        RAISE_APPLICATION_ERROR(-20000, '@ActionNotAllowedHere@') ;
      END IF;
    END IF;--END_PROCESSING
    IF(NOT END_PROCESSING AND NOT FINISH_PROCESS) THEN
      SELECT COUNT(*)
      INTO v_count
      FROM AD_CLIENTINFO
      WHERE AD_CLIENT_ID=v_Client_ID
        AND CHECKORDERORG='Y';
      IF v_count>0 THEN
        v_ResultStr:='CheckingRestrictions - C_ORDER ORG IS IN C_BPARTNER ORG TREE';
        SELECT COUNT(*)
        INTO v_count
        FROM C_ORDER c,
          C_BPARTNER bp
        WHERE c.C_Order_ID=v_Record_ID
          AND c.C_BPARTNER_ID=bp.C_BPARTNER_ID
          AND Ad_Isorgincluded(c.AD_ORG_ID, bp.AD_ORG_ID, bp.AD_CLIENT_ID)=-1;
        IF v_count>0 THEN
          RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgBpartnerOrder@') ;
        END IF;
      END IF;
    END IF;--END_PROCESSING
    IF(NOT END_PROCESSING AND NOT FINISH_PROCESS) THEN
      /**
      * Convert to Target DocType
      */
      DECLARE
        v_DocSubTypeSO_Target CHAR(2) ;
      BEGIN
        v_ResultStr:='ConvertingDocType';
        IF(v_DocType_ID<>v_DocTypeTarget_ID) THEN
          -- New
          IF(v_DocStatus='DR' OR v_DocType_ID=0) THEN
            -- Update to Target Document Type
            WHILE(v_DocType_ID<>v_DocTypeTarget_ID)
            LOOP
            BEGIN
              v_ResultStr:='UpdateDocType';
              UPDATE C_ORDER
                SET C_DocType_ID=v_DocTypeTarget_ID
              WHERE C_Order_ID=v_Record_ID;
              v_DocType_ID:=v_DocTypeTarget_ID;
            EXCEPTION
            WHEN OTHERS THEN
              v_ResultStr:='UpdateDocumentNo';
              UPDATE C_ORDER  SET DocumentNo=DocumentNo || '.'  WHERE C_Order_ID=v_Record_ID;
            END;
          END LOOP;
        ELSE
          v_ResultStr:='GetTargetDocType';
          SELECT DocSubTypeSO
          INTO v_DocSubTypeSO_Target
          FROM C_DOCTYPE
          WHERE C_DocType_ID=v_DocTypeTarget_ID;
          DBMS_OUTPUT.PUT_LINE('Changing DocType from ' || v_DocSubTypeSO || ' to ' || v_DocSubTypeSO_Target) ;
          -- Change Offer to anything, Change InProcess to anything
          IF(v_DocSubTypeSO IN('ON', 'OB') OR v_DocStatus='IP') THEN
            -- Update to Target Document Type
            WHILE(v_DocType_ID<>v_DocTypeTarget_ID)
            LOOP
            BEGIN
              v_ResultStr:='UpdateDocType';
              UPDATE C_ORDER
                SET C_DocType_ID=v_DocTypeTarget_ID
              WHERE C_Order_ID=v_Record_ID;
              v_DocType_ID:=v_DocTypeTarget_ID;
            EXCEPTION
            WHEN OTHERS THEN
              v_ResultStr:='UpdateDocumentNo';
              UPDATE C_ORDER  SET DocumentNo=DocumentNo || '.'  WHERE C_Order_ID=v_Record_ID;
            END;
          END LOOP;
        ELSE
          -- Change Back
          UPDATE C_ORDER
            SET C_DocTypeTarget_ID=v_DocType_ID
          WHERE C_Order_ID=v_Record_ID;
          RAISE_APPLICATION_ERROR(-20000, '@CannotChangeDocType@') ;
        END IF;
      END IF;
    END IF; -- C_DocType_ID <> C_DocTypeTarget_ID
  END; -- Conversion
END IF;--FINISH_PROCESS
IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
  /**
  * Get DocSubTypeSO + Is it Binding :1:2
  */
  v_ResultStr:='TestBinding DocType_ID=' || v_DocType_ID;
  SELECT
    CASE DocSubTypeSO WHEN 'ON' THEN 'N' ELSE 'Y'
    END
    ,
    DocSubTypeSO
  INTO v_IsBinding,
    v_DocSubTypeSO
  FROM C_DOCTYPE
  WHERE C_DocType_ID=v_DocType_ID;
  DBMS_OUTPUT.PUT_LINE('DocSubTypeSO=' || v_DocSubTypeSO || ' IsBinding=' || v_IsBinding) ;
END IF;--FINISH_PROCESS
IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
 /**************************************************************************
  * Resolve not-stocked BOMs
  *************************************************************************/
  DECLARE
    -- Order Lines with non-stocked BOMs
    CUR_BOM_Line RECORD;
    -- BOM Product List
    CUR_BOM RECORD;
    --
    CountNo NUMBER;
    v_PriceList_Version_ID NUMBER;
    NextNo NUMBER;
    v_Line NUMBER;
    v_FreightAmt NUMBER;
    v_ChargeAmt NUMBER;
    v_Parner_Id NUMBER;
 --v_PriceList_ID NUMBER;
    --
  BEGIN
    LOOP
      -- How many BOMs do we have:3
      SELECT COUNT(*)
      INTO CountNo
      FROM C_ORDERLINE l
      WHERE l.C_Order_ID=v_Record_ID
        AND EXISTS
        (SELECT *
        FROM M_PRODUCT p
        WHERE l.M_Product_ID=p.M_Product_ID
          AND p.IsBOM='Y'
          AND p.IsStocked='N'
        )
        ;
      -- Nothing to do:4
      EXIT WHEN CountNo=0;
      DBMS_OUTPUT.PUT_LINE('  BOMs to resolve=' || CountNo) ;
      DECLARE
        Cur_MPriceListVersion RECORD;
      BEGIN
        -- last shipment
        FOR Cur_MPriceListVersion IN
          (SELECT COALESCE(v.M_PriceList_Version_ID, 0) AS PriceList_Version_ID, o.M_PriceList_ID
          FROM M_PRICELIST_VERSION v,
            C_ORDER o
          WHERE v.M_PriceList_ID=o.M_PriceList_ID
            AND v.ValidFrom<=o.DateOrdered
            AND v.IsActive='Y'
            AND o.C_Order_ID=v_Record_ID
          ORDER BY v.ValidFrom DESC
          )
        LOOP
          v_PriceList_Version_ID:=Cur_MPriceListVersion.PriceList_Version_ID;
         -- v_PriceList_ID := Cur_MPriceListVersion.M_PriceList_Id;
          EXIT;
        END LOOP;
      END;
      -- Get Price List Version
      -- Replace Lines
      FOR CUR_BOM_Line IN
        (SELECT *
        FROM C_ORDERLINE l
        WHERE l.C_Order_ID=v_Record_ID
          AND IsActive='Y'
          AND EXISTS
          (SELECT *
          FROM M_PRODUCT p
          WHERE l.M_Product_ID=p.M_Product_ID
            AND p.IsBOM='Y'
            AND p.IsStocked='N'
          )
        ORDER BY l.Line  FOR UPDATE
        )
      LOOP
        v_Line:=CUR_BOM_Line.Line;
        -- One Time variables
        v_FreightAmt:=CUR_BOM_Line.FreightAmt;
        v_ChargeAmt:=CUR_BOM_Line.ChargeAmt;
        -- Create New Lines
        FOR CUR_BOM IN
          (SELECT b.M_ProductBOM_ID,
            p.C_UOM_ID,
            b.BOMQty,
            b.Description
          FROM M_PRODUCT_BOM b
          INNER JOIN M_PRODUCT p
            ON(b.M_ProductBOM_ID=p.M_Product_ID)
          WHERE b.M_Product_ID=CUR_BOM_Line.M_Product_ID
          ORDER BY Line
          )
        LOOP
          Ad_Sequence_Next('C_OrderLine', CUR_BOM_Line.AD_Client_ID, NextNo) ;
          v_Line:=v_Line + 1;
          INSERT
          INTO C_ORDERLINE
            (
              C_OrderLine_ID, AD_Client_ID, AD_Org_ID, IsActive,
              Created, CreatedBy, Updated, UpdatedBy,
              C_Order_ID, Line, C_BPartner_ID, C_BPartner_Location_ID,
              DateOrdered, DatePromised, DateDelivered, DateInvoiced,
              Description, M_Product_ID, M_Warehouse_ID, DirectShip,
              C_UOM_ID, QtyOrdered, QtyReserved, QtyDelivered,
              QtyInvoiced, M_Shipper_ID, C_Currency_ID,
     PriceList,
              PriceActual,
     PriceLimit,
     LineNetAmt, Discount,
              FreightAmt, C_Charge_ID, ChargeAmt, C_Tax_ID,
              Lot,
              SerNo,  --MODIFIED BY F.IRIAZABAL
              QuantityOrder, M_Product_Uom_ID, PriceStd
            )
            VALUES
            (
              NextNo, CUR_BOM_Line.AD_Client_ID, CUR_BOM_Line.AD_Org_ID, CUR_BOM_Line.IsActive,
              now(), 0, now(), 0,
              CUR_BOM_Line.C_Order_ID, v_Line, CUR_BOM_Line.C_BPartner_ID, CUR_BOM_Line.C_BPartner_Location_ID,
              CUR_BOM_Line.DateOrdered, CUR_BOM_Line.DatePromised, CUR_BOM_Line.DateDelivered, CUR_BOM_Line.DateInvoiced,
              CUR_BOM.Description, CUR_BOM.M_ProductBOM_ID, CUR_BOM_Line.M_Warehouse_ID, CUR_BOM_Line.DirectShip,
              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,
              CUR_BOM_Line.QtyInvoiced*CUR_BOM.BOMQty, CUR_BOM_Line.M_Shipper_ID, CUR_BOM_Line.C_Currency_ID,
     Bom_Pricelist(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID),
              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),
     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,
              v_FreightAmt, CUR_BOM_Line.C_Charge_ID, v_ChargeAmt, CUR_BOM_Line.C_Tax_ID,
              NULL,
              NULL,  --MODIFIED BY F.IRIAZABAL
              CUR_BOM_Line.QuantityOrder, CUR_BOM_Line.M_Product_UOM_ID, Bom_Pricestd(CUR_BOM.M_ProductBOM_ID, v_PriceList_Version_ID)
            )
            ; -- no Lot/SerNo for BOMs
          -- One Time variables
          v_FreightAmt:=0;
          v_ChargeAmt:=0;
        END LOOP; -- Create New Lines
        -- Convert into Comment Line
        UPDATE C_ORDERLINE
          SET M_Product_ID=NULL,
          PriceList=0,
          PriceActual=0,
          PriceLimit=0,
          LineNetAmt=0,
          FreightAmt=0,
          ChargeAmt=0,
          Description=
          (SELECT p.NAME || ' ' || C_ORDERLINE.Description
          FROM M_PRODUCT p
          WHERE p.M_Product_ID=CUR_BOM_Line.M_Product_ID
          )
          ,
          PriceStd=0,
          M_Offer_ID=NULL
        WHERE C_OrderLine_ID=CUR_BOM_Line.C_OrderLine_ID;
      END LOOP; -- Replace Lines
    END LOOP; -- BOM Loop
  END;
END IF;--FINISH_PROCESS
IF(NOT END_PROCESSING AND NOT FINISH_PROCESS) THEN
 /**************************************************************************
  * Always check and (un) Reserve Inventory  (counterpart: M_InOut_Post)
  *************************************************************************/
 DECLARE
  Cur_ResLine RECORD;

  v_QtySO       NUMBER; -- Reserved
  v_QtyOrderSO     NUMBER;
  v_QtyPO       NUMBER; -- Ordered
  v_QtyOrderPO     NUMBER;
 BEGIN
  v_ResultStr := 'ReserveInventory';
  -- For all lines needing reservation
  FOR Cur_ResLine IN (SELECT l.M_Warehouse_ID, l.M_Product_ID, l.M_AttributeSetInstance_ID, l.C_OrderLine_ID,
        -- Target Level = 0 if DirectShip='Y' or Binding='N'
        (CASE l.DirectShip WHEN 'Y' THEN 0 ELSE (CASE v_IsBinding WHEN 'N' THEN 0 ELSE l.QtyOrdered END) END)
         -l.QtyReserved-l.QtyDelivered AS Qty, l.QUANTITYORDER,
         l.QtyReserved, l.QtyDelivered, l.DatePromised, l.C_UOM_ID,
         l.M_PRODUCT_UOM_ID
       FROM C_ORDERLINE l, M_PRODUCT p
       WHERE l.C_Order_ID=v_Record_ID
         -- Reserve Products (not: services, null products) --
         AND l.M_Product_ID=p.M_Product_ID
         AND p.IsStocked='Y' AND p.ProductType='I'
         -- Target Level = 0 if DirectShip='Y' or Binding='N'
         AND (CASE l.DirectShip WHEN 'Y' THEN 0 ELSE (CASE v_IsBinding WHEN 'N' THEN 0 ELSE l.QtyOrdered END)END)
         -l.QtyReserved-l.QtyDelivered <> 0
       FOR UPDATE) LOOP
   -- Qty corrected for SO/PO
   IF (v_DocSubTypeSO IS NOT NULL) THEN
    v_QtySO   := Cur_ResLine.Qty;
    v_QtyOrderSO := NULL;
    IF Cur_ResLine.QtyReserved=0 AND Cur_ResLine.QtyDelivered=0 THEN
       v_QtyOrderSO := Cur_ResLine.QuantityOrder;
    ELSIF Cur_ResLine.M_Product_UOM_ID IS NOT NULL THEN
       v_QtyOrderSO := C_Uom_Convert(v_QtySO, Cur_ResLine.C_UOM_ID, Cur_ResLine.M_Product_UOM_ID, 'Y');
    END IF;
    v_QtyPO   := 0;
    v_QtyOrderPO := NULL;
   ELSE -- PO
    v_QtySO := 0;
    v_QtyOrderSO := NULL;
    v_QtyPO := Cur_ResLine.Qty;
    v_QtyOrderPO := NULL;
    IF Cur_ResLine.QtyReserved=0 AND Cur_ResLine.QtyDelivered=0 THEN
       v_QtyOrderPO := Cur_ResLine.QuantityOrder;
    ELSIF Cur_ResLine.M_Product_UOM_ID IS NOT NULL THEN
       v_QtyOrderPO := C_Uom_Convert(v_QtyPO, Cur_ResLine.C_UOM_ID, Cur_ResLine.M_Product_UOM_ID, 'Y');
    END IF;
   END IF;
   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);
   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);

   -- Update Order Line
   IF (v_DocSubTypeSO IS NOT NULL) THEN
      UPDATE C_ORDERLINE
      SET QtyReserved = QtyReserved + v_QtySO
    WHERE C_OrderLine_ID = Cur_ResLine.C_OrderLine_ID;
   END IF;
   rowcount:=SQL%ROWCOUNT;
   IF (rowcount <> 1) THEN
       IF (p_PInstance_ID IS NOT NULL) THEN
 ROLLBACK;
     v_ResultStr := 'LockingOrder';
     UPDATE C_ORDER
       SET Processing = 'N'
     WHERE C_Order_ID = v_Record_ID;
          RAISE NO_DATA_FOUND;
 COMMIT;
    END IF;
    RAISE_APPLICATION_ERROR(-20011, 'Did not update Line');
   END IF;
  END LOOP; -- For all lines needing reservation
 END; -- Reserve Inventory
  END IF;--FINISH_PROCESS
  IF (NOT FINISH_PROCESS) THEN
 /**************************************************************************
  * Calculate Taxes and Totals
  *************************************************************************/
   DECLARE
    Cur_Tax RECORD;
    Cur_MultiTax RECORD;
    xTotalLines NUMBER:=0;
    v_TaxBaseAmt NUMBER:=0;
    xTaxAmt NUMBER:=0;
    xGrandTotal NUMBER:=0;
    HeaderNotAdded BOOLEAN:=TRUE;
    v_FirstLine BOOLEAN;
    v_ActualBaseAmt NUMBER;
    v_lineNo NUMBER:= 0;
  BEGIN
    v_ResultStr:='DeleteOldTaxes';
    DELETE FROM C_ORDERTAX  WHERE C_Order_ID=v_Record_ID;
    -- For all Tax Rates
    v_ResultStr:='InsertNewTaxes';
    FOR Cur_Tax IN
      (SELECT l.C_Tax_ID, o.IsTaxIncluded,
        SUM(l.LineNetAmt) + SUM(l.FreightAmt) + SUM(l.ChargeAmt) AS LineNet,
        SUM(o.FreightAmt) + SUM(o.ChargeAmt) AS HeaderNet,
        t.Rate, t.IsSummary, c.StdPrecision
      FROM C_ORDER o,  C_ORDERLINE l,
           C_TAX t, C_CURRENCY c
      WHERE o.C_Order_ID=l.C_Order_ID
        AND o.C_Order_ID=v_Record_ID -- Parameter
        AND l.C_Tax_ID=t.C_Tax_ID
        AND o.C_Currency_ID=c.C_Currency_ID
      GROUP BY l.C_Tax_ID,
        o.IsTaxIncluded,
        t.Rate,
        t.IsSummary,
        c.StdPrecision
      ORDER BY 3 DESC
      )
    LOOP
      v_TaxBaseAmt:=Cur_Tax.LineNet;
      xTotalLines:=xTotalLines + v_TaxBaseAmt; -- w/o Header Freight/Charge

      IF(HeaderNotAdded) THEN --  add header net to first tax
        HeaderNotAdded:=FALSE;
        v_TaxBaseAmt:=v_TaxBaseAmt + Cur_Tax.HeaderNet;
      END IF;
      IF(Cur_Tax.IsSummary='N') THEN
        v_LineNo := v_LineNo + 10;
        xTaxAmt:=ROUND(v_TaxBaseAmt * Cur_Tax.Rate / 100, Cur_Tax.StdPrecision) ;
        xGrandTotal:=xGrandTotal + v_TaxBaseAmt + xTaxAmt;
        --
        INSERT
        INTO C_ORDERTAX
          (
            C_Order_ID, C_Tax_ID, AD_Client_ID, AD_Org_ID,
            IsActive, Created, CreatedBy, Updated,
            UpdatedBy, TaxBaseAmt, TaxAmt, Line
          )
          VALUES
          (
            v_Record_ID, Cur_Tax.C_Tax_ID, v_Client_ID, v_Org_ID,
             'Y', now(), v_UpdatedBy, now(),
            v_UpdatedBy, v_TaxBaseAmt, xTaxAmt, v_LineNo
          )
          ;
      ELSE -- Multiple Taxes
        xGrandTotal:=xGrandTotal + v_TaxBaseAmt;
        v_FirstLine := TRUE;
        FOR Cur_MultiTax IN
                          (SELECT C_Tax_ID, Rate, CASCADE
                             FROM C_TAX
                            WHERE Parent_Tax_ID=Cur_Tax.C_Tax_ID
                            ORDER BY Line)
        LOOP
          v_LineNo := v_LineNo + 10;
          IF ((Cur_MultiTax.CASCADE = 'Y') AND (NOT v_FirstLine)) THEN   --cannot apply cascade to the first line
            v_ActualBaseAmt := xGrandTotal;
          ELSE
            v_ActualBaseAmt := v_TaxBaseAmt;
          END IF;
          xTaxAmt:=ROUND(v_ActualBaseAmt * Cur_MultiTax.Rate / 100, Cur_Tax.StdPrecision) ;
          v_FirstLine := FALSE;
          xGrandTotal:=xGrandTotal + xTaxAmt;
          --
          INSERT INTO C_ORDERTAX
            (
              C_Order_ID, C_Tax_ID, AD_Client_ID, AD_Org_ID,
              IsActive, Created, CreatedBy, Updated,
              UpdatedBy, TaxBaseAmt, TaxAmt, Line
            )
            VALUES
            (
              v_Record_ID, Cur_MultiTax.C_Tax_ID, v_Client_ID, v_Org_ID,
               'Y', now(), v_UpdatedBy, now(),
              v_UpdatedBy, v_ActualBaseAmt, xTaxAmt, v_LineNo
            );
        END LOOP;
      END IF;
    END LOOP; -- Insert New Taxes
    -- Update Header
    UPDATE C_ORDER
      SET TotalLines=xTotalLines,
      GrandTotal=xGrandTotal
    WHERE C_Order_ID=v_Record_ID;
    DBMS_OUTPUT.PUT_LINE('GrandTotal=' || xGrandTotal) ;
  END; -- Calculate Tax and Totals
  -- Synchronize Client/Org Ownership
  UPDATE C_ORDERLINE
    SET AD_Client_ID=v_Client_ID,
    AD_Org_ID=v_Org_ID
  WHERE C_Order_ID=v_Record_ID
    AND(AD_Client_ID<>v_Client_ID
    OR AD_Org_ID<>v_Org_ID) ;
END IF;--FINISH_PROCESS
IF(NOT END_PROCESSING AND NOT FINISH_PROCESS) THEN
  /**************************************************************************
  * Order Complete:5 - Something to do:6
  */
  BEGIN
    v_ResultStr:='OrderCompleteCheck';
    SELECT SUM(QtyOrdered*C_OrderLine_ID) -SUM(QtyDelivered*C_OrderLine_ID),
      SUM(QtyOrdered*C_OrderLine_ID) -SUM(QtyInvoiced*C_OrderLine_ID)
    INTO ToDeliver,
      ToInvoice
    FROM C_ORDERLINE
    WHERE C_Order_ID=v_Record_ID;
    -- If no lines, ToDeliver is NULL
    IF((ToDeliver=0 AND ToInvoice=0) OR(ToDeliver IS NULL AND ToInvoice IS NULL)) THEN
      DBMS_OUTPUT.PUT_LINE('OrderComplete') ;
      IF(v_DocAction='CL') THEN
        END_PROCESSING:=TRUE;
      ELSIF(v_DocAction='VO') THEN
        UPDATE C_ORDER
          SET DocStatus='VO',
          DocAction='--',
          Processed='Y',
          Updated=now()
        WHERE C_Order_ID=v_Record_ID;
		END_PROCESSING:=TRUE;
      ELSE
        UPDATE C_ORDER
          SET DocStatus='CO',
          DocAction='--',
          Processed='Y',
          Updated=now()
        WHERE C_Order_ID=v_Record_ID;
		END_PROCESSING:=TRUE;
      END IF;
      IF(NOT END_PROCESSING) THEN
        RAISE_APPLICATION_ERROR(-20000, '@AlreadyPosted@');
      END IF;--END_PROCESSING
    END IF;
  END;
END IF;--FINISH_PROCESS
IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
  /**
  * In Progress -----------------------------------------------------------
  */
  UPDATE C_ORDER
    SET DocStatus='IP',
    DateAcct=DateOrdered,
    Updated=now()
  WHERE C_Order_ID=v_Record_ID;
  IF(p_PInstance_ID IS NOT NULL) THEN
    COMMIT;
  END IF;
  /************
  * Is Approved:7
  ***********/
  DBMS_OUTPUT.PUT_LINE('Approval - IsApproved=' || v_IsApproved) ;
  v_ResultStr:='Approval';
  IF(v_IsApproved='N') THEN
    -- Check if it needs to be approved
    v_ResultStr:='TestApproval DocType_ID=' || v_DocType_ID;
    SELECT IsApproved
    INTO v_IsApproved
    FROM C_DOCTYPE
    WHERE C_DocType_ID=v_DocType_ID;
    DBMS_OUTPUT.PUT_LINE('IsApproved=' || v_IsApproved) ;
    IF(v_IsApproved<>'N') THEN -- Approval needed
      IF(v_DocAction<>'AP') THEN
        UPDATE C_ORDER
          SET DocStatus='NA',
          DocAction='AP',
          Processed='N',
          Updated=now()
        WHERE C_Order_ID=v_Record_ID;
        DBMS_OUTPUT.PUT_LINE('Approval needed') ;
        RAISE_APPLICATION_ERROR(-20000, '@NotApprovedOrder@');
      END IF;
    ELSE
      UPDATE C_ORDER  SET IsApproved='Y'  WHERE C_Order_ID=v_Record_ID;
    END IF;
  END IF;
END IF;--FINISH_PROCESS
IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
  /**
  * Finished with processing
  */
  IF(v_DocAction='PR') THEN
    v_ResultStr:='FinishProcessing';
    UPDATE C_ORDER
      SET DocStatus='IP',
      DocAction='CO',
      Processed='N',
      Updated=now()
    WHERE C_Order_ID=v_Record_ID;
    -- C_Order_PickList(NULL, v_Record_ID);  -- Print PickList
    END_PROCESSING:=TRUE;
  END IF;
END IF;--FINISH_PROCESS
IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
  /**************************************************************************
  * Prepayment Order  Create Invoice
  *************************************************************************/
  IF(v_DocSubTypeSO='PR' AND v_DocStatus<>'WP') THEN
    DBMS_OUTPUT.PUT_LINE('Create PreInvoice - ' || v_Record_ID) ;
    v_ResultStr:='CreatePreInvoice';
    C_Invoice_Create(NULL, Invoice_ID, v_Record_ID) ;
    DBMS_OUTPUT.PUT_LINE('  PreInvoice - ' || Invoice_ID) ;
    IF(Invoice_ID=0) THEN
      RAISE_APPLICATION_ERROR(-20000, '@PreInvoiceCreateFailed@');
    END IF;
    IF(NOT END_PROCESSING) THEN
      C_INVOICE_POST(NULL, Invoice_ID) ;
      --
      UPDATE C_ORDER
        SET DocStatus='WP',
        DocAction='--',
        Processed='Y',
        Updated=now()
      WHERE C_Order_ID=v_Record_ID;
      --
    END IF;--END_PROCESSING
    END_PROCESSING:=TRUE;
  END IF;
  IF(NOT END_PROCESSING) THEN
    /**
    * Deliver Direct Shipments
    */
    v_ResultStr:='NonInventoryDelivery';
    UPDATE C_ORDERLINE
      SET QtyDelivered=QtyOrdered
    WHERE DirectShip='Y'
      AND C_Order_ID=v_Record_ID;
  END IF;--END_PROCESSING
END IF;--FINISH_PROCESS
IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
  /**************************************************************************
  * Will-Call + Walk In Processing
  * --
  * (W)illCall(I)nvoice - (W)illCall(P)ickup - (W)alkIn(R)eceipt
  * --
  *************************************************************************/
  IF(v_DocSubTypeSO IN('WI', 'WP', 'WR')) THEN
    /************
    * Shipment
    */
    DBMS_OUTPUT.PUT_LINE('Create Shipment - ' || v_Record_ID) ;
    v_ResultStr:='CreateShipment';
    M_Inout_Create(NULL, InOut_ID, v_Record_ID, NULL, 'Y') ; -- Force Delivery
    DBMS_OUTPUT.PUT_LINE('  Shipment - ' || InOut_ID) ;
    IF(InOut_ID=0) THEN
      RAISE_APPLICATION_ERROR(-20000, '@InOutCreateFailed@');
	ELSE
	SELECT documentno
	INTO v_DocumentNo
	FROM M_INOUT
	WHERE M_INOUT_ID = InOut_ID;
	v_Message:='@InoutDocumentno@ ' || v_DocumentNo || ' @beenCreated@';
    END IF;
    IF(NOT END_PROCESSING) THEN
      IF(v_DocSubTypeSO IN('WI', 'WR')) THEN
        /************
        * Invoice
        */
        DBMS_OUTPUT.PUT_LINE('Create Invoice - ' || v_Record_ID) ;
        v_ResultStr:='CreateInvoice';
        C_Invoice_Create(NULL, Invoice_ID, v_Record_ID) ;
        DBMS_OUTPUT.PUT_LINE('  Invoice - ' || Invoice_ID) ;
        IF(Invoice_ID IS NULL OR Invoice_ID=0) THEN
          RAISE_APPLICATION_ERROR(-20000, '@InvoiceCreateFailed@');
		ELSE
		SELECT documentno
	    INTO v_DocumentNo
	    FROM C_INVOICE
	    WHERE C_INVOICE_ID = Invoice_ID;
	    v_Message:=v_Message||' , '||'@InvoiceDocumentno@ ' || v_DocumentNo || ' @invbeenCreated@';
        END IF;
      END IF;
    END IF;--END_PROCESSING
  END IF;
END IF;--FINISH_PROCESS
IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
  /**
  * Final Completeness check
  */
  SELECT SUM(QtyOrdered*C_OrderLine_ID) -SUM(QtyDelivered*C_OrderLine_ID),
    SUM(QtyOrdered*C_OrderLine_ID) -SUM(QtyInvoiced*C_OrderLine_ID)
  INTO ToDeliver,
    ToInvoice
  FROM C_ORDERLINE
  WHERE C_Order_ID=v_Record_ID;
  DBMS_OUTPUT.PUT_LINE('To deliver - ' || ToDeliver) ;
  DBMS_OUTPUT.PUT_LINE('ToInvoice - ' || ToInvoice) ;
  DBMS_OUTPUT.PUT_LINE('v_DocSubTypeSO - ' || v_DocSubTypeSO) ;
  -- Nothing to Deliver + Invoice for (W)illCall(I)nvoice and (W)alkIn(R)eceipt
  IF(v_DocSubTypeSO IN('WI', 'WR') AND ToDeliver=0 AND ToInvoice=0) THEN
    UPDATE C_ORDER
      SET DocStatus='CO',
      DocAction='--',
      IsDelivered='Y',
      IsInvoiced='Y',
      Processed='Y',
      Updated=now()
    WHERE C_Order_ID=v_Record_ID;
    DBMS_OUTPUT.PUT_LINE('DocAction - ' || v_DocAction) ;
    IF(v_DocAction='VO') THEN
      UPDATE C_ORDER  SET DocStatus='VO'  WHERE C_Order_ID=v_Record_ID;
    END IF;
  END IF;
  -- Nothing to Deliver for (W)illCall(P)ickup (Invoice generated independently)
  IF(v_DocSubTypeSO='WP' AND ToDeliver=0) THEN
    UPDATE C_ORDER
      SET DocStatus='CO',
      DocAction='--',
      IsDelivered='Y',
      Processed='Y',
      Updated=now()
    WHERE C_Order_ID=v_Record_ID;
    IF(v_DocAction='VO') THEN
      UPDATE C_ORDER  SET DocStatus='VO'  WHERE C_Order_ID=v_Record_ID;
    END IF;
  END IF;
  -- We are done with standard sales orders
  IF(v_DocAction IN('CO', 'CL', 'VO') AND v_DocSubTypeSO='SO') THEN
    UPDATE C_ORDER
      SET DocStatus='CO',
      DocAction='--',
      Processed='Y',
      Updated=now()
    WHERE C_Order_ID=v_Record_ID;
  END IF;
  -- Purchase Orders
  IF(v_DocAction IN('CO', 'CL', 'VO') AND v_DocSubTypeSO IS NULL) THEN
    UPDATE C_ORDER
      SET DocStatus='CO',
      DocAction='--',
      Processed='Y',
      Updated=now()
    WHERE C_Order_ID=v_Record_ID;
  END IF;
  -- Only create cash entry if docAction is Complete
  IF(v_DocAction NOT IN('CO')) THEN
    END_PROCESSING:=TRUE;
  END IF;
END IF;--FINISH_PROCESS
IF(NOT FINISH_PROCESS AND NOT END_PROCESSING) THEN
  /**************************************************************************
  * Create default Cash entry
  *************************************************************************/
  DECLARE
    v_PaymentRule CHAR(1) ;
    CUR_CB RECORD;
    v_debtPaymentID NUMBER;
    v_totalCash NUMBER;
  v_CB_Curr NUMBER;
  BEGIN
    /* ALO
    */
    UPDATE C_DEBT_PAYMENT  SET IsValid='Y'  WHERE C_Order_ID=v_Record_ID;
    SELECT C_ORDER.PAYMENTRULE,
      C_ORDER.DOCUMENTNO||' - '||C_BPARTNER.NAME||' - '||C_ORDER.GRANDTOTAL,
      C_ORDER.GRANDTOTAL,
      C_ORDER.M_WAREHOUSE_ID
    INTO v_PaymentRule,
      v_DocumentNo,
      v_GrandTotal,
      v_M_Warehouse_ID
    FROM C_ORDER,
      C_BPARTNER
    WHERE C_ORDER.C_BPARTNER_ID=C_BPARTNER.C_BPARTNER_ID
      AND C_ORDER_ID=v_Record_ID;
    SELECT MAX(NAME)
    INTO v_WarehouseName
    FROM M_WAREHOUSE
    WHERE M_WAREHOUSE_ID=v_M_Warehouse_ID;

    --PaymentRule C, WI and WR have already created DP, this won't be inserted in cashline
    --because it will processed with the invoice.
    IF(v_PaymentRule='C') AND (v_DocSubTypeSO NOT IN ('WI', 'WR') OR v_DocSubTypeSO IS NULL) THEN
      IF(v_CashLine_ID IS NULL OR v_CashLine_ID=0) THEN
        -- Create CashLine
        -- Find Defaylt CashBook
        v_ResultStr:='Find C_CashBook Org_ID=' || v_Org_ID;
      BEGIN
        -- First active default Book of Org
        FOR CUR_CB IN
          (SELECT cb.C_CashBook_ID,
            c.ISO_Code,
            cb.NAME,
      cb.c_currency_id
          FROM C_CASHBOOK cb,
            C_CURRENCY c
          WHERE cb.AD_Org_ID=v_Org_ID
            AND cb.C_Currency_ID=c.C_Currency_ID
            AND cb.IsActive='Y'
          ORDER BY cb.IsDefault DESC
          )
        LOOP
          IF(v_CashBook_ID IS NULL) THEN
            v_CashBook_ID:=CUR_CB.C_CashBook_ID;
            v_ISO_Code:=CUR_CB.ISO_Code;
      v_CB_Curr:=CUR_CB.C_Currency_ID;
          ELSIF(CUR_CB.NAME=v_WarehouseName) THEN
            v_CashBook_ID:=CUR_CB.C_CashBook_ID;
            v_ISO_Code:=CUR_CB.ISO_Code;
      v_CB_Curr:=CUR_CB.C_Currency_ID;
          END IF;
        END LOOP;
      END;
      IF(v_CashBook_ID IS NULL) THEN
        RAISE_APPLICATION_ERROR(-20000, '@CashBookPRSCnotfoundOrg@' || v_Org_Name) ;
      END IF;
      DBMS_OUTPUT.PUT_LINE('CashBook_ID=' || v_CashBook_ID) ;
      -- Find/Create Cash Journal
      v_ResultStr:='Find C_Cash for ' || v_Date;
      DECLARE
        Cur_CashId RECORD;
      BEGIN
        FOR Cur_CashId IN
          (SELECT C_Cash_ID AS Cash_ID
          FROM C_CASH
          WHERE C_CashBook_ID=v_CashBook_ID
            AND TRUNC(StatementDate)=v_Date
            AND Processed='N'
          )
        LOOP
          v_Cash_ID:=Cur_CashId.Cash_ID;
          EXIT;
        END LOOP;
      EXCEPTION
      WHEN NO_DATA_FOUND THEN
        NULL;
      END;
      /**************************************************************************
      * Credit Multiplier
      *************************************************************************/
      DECLARE
        v_DocBaseType C_DOCTYPE.DocBaseType%TYPE;
      BEGIN
        -- Is it a Credit Memo?
        SELECT DocBaseType
        INTO v_DocBaseType
        FROM C_DOCTYPE
        WHERE C_DocType_ID=v_DocType_ID;
        IF(v_DocBaseType IN('ARC', 'API')) THEN
          v_Multiplier:=-1;
        END IF;
      END;



      IF(v_Cash_ID IS NULL) THEN
        v_ResultStr:='Create C_Cash';
        Ad_Sequence_Next('C_Cash', v_Org_ID, v_Cash_ID) ;
        INSERT
        INTO C_CASH
          (
            C_Cash_ID, AD_Client_ID, AD_Org_ID, IsActive,
            Created, CreatedBy, Updated, UpdatedBy,
            C_CashBook_ID, NAME, StatementDate, DateAcct,
            BeginningBalance, EndingBalance, StatementDifference, Processing,
            Processed, Posted
          )
          VALUES
          (
            v_Cash_ID, v_Client_ID, v_Org_ID, 'Y',
            now(), v_UpdatedBy, now(), v_UpdatedBy,
            v_CashBook_ID, TO_CHAR(v_Date, 'YYYY-MM-DD') || ' ' || v_ISO_Code, v_Date, v_Date,
            0, 0, 0, 'N',
             'N', 'N'
          )
          ;
      END IF;
      /*
      Create a debt payment for paymentrule=C

      Note: for WI and WR we have already created an invoice and its DP, we only have to link it
      */
      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)
      INTO v_totalCash
      FROM C_DEBT_PAYMENT_V dp
      WHERE C_Order_ID=v_Record_ID;

/*
      SELECT COUNT(*)
        INTO v_Aux
        FROM C_DEBT_PAYMENT DP,
             C_INVOICE I
       WHERE I.C_ORDER_ID = v_Record_ID
         AND DP.C_INVOICE_ID = I.C_INVOICE_ID;

      IF v_Aux != 0 THEN -- DP already created
        SELECT dp.C_DEBT_PAYMENT_ID
          INTO v_debtPaymentID
          FROM C_DEBT_PAYMENT DP,
               C_INVOICE I
         WHERE I.C_ORDER_ID = v_Record_ID
           AND DP.C_INVOICE_ID = I.C_INVOICE_ID;

        UPDATE C_DEBT_PAYMENT
           SET C_ORDER_ID = v_Record_ID
         WHERE C_DEBT_PAYMENT_ID = v_debtPaymentID;
      ELSE     */
        Ad_Sequence_Next('C_Debt_Payment', v_Record_ID, v_debtPaymentID) ;
        INSERT INTO C_DEBT_PAYMENT
         (C_DEBT_PAYMENT_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
          CREATED, CREATEDBY, UPDATED, UPDATEDBY,
          ISRECEIPT, C_SETTLEMENT_CANCEL_ID, C_SETTLEMENT_GENERATE_ID, DESCRIPTION,
          C_ORDER_ID, C_BPARTNER_ID, C_CURRENCY_ID, C_CASHLINE_ID,
          C_BANKACCOUNT_ID, C_CASHBOOK_ID, PAYMENTRULE, ISPAID,
          AMOUNT, WRITEOFFAMT, DATEPLANNED, ISMANUAL,
          ISVALID, C_BANKSTATEMENTLINE_ID, CHANGESETTLEMENTCANCEL, CANCEL_PROCESSED,
          GENERATE_PROCESSED, c_project_id,IsAutomaticGenerated, STATUS_INITIAL)
        VALUES
         (v_debtPaymentID, v_Client_ID, v_Org_ID, 'Y',
          now(), v_UpdatedBy, now(), v_UpdatedBy,
          v_isSoTrx, NULL, NULL, '',
          v_Record_ID, v_c_Bpartner_Id, v_c_currency_id, null,
          NULL, v_CashBook_ID, 'C', 'N',
          (v_GrandTotal-v_totalCash), 0, v_Date, 'N',
           'Y', NULL, 'N', 'N',
           'N', v_C_PROJECT_ID,'Y', 'DE');
    --  END IF;

      DBMS_OUTPUT.PUT_LINE('  Cash_ID=' || v_Cash_ID) ;
      -- Create CashJournal Line in invoice currency
      v_ResultStr:='Create C_CashLine';
      Ad_Sequence_Next('C_CashLine', v_Org_ID, v_CashLine_ID) ;

      SELECT COALESCE(MAX(Line), 0) +10
      INTO v_Line
      FROM C_CASHLINE
      WHERE C_Cash_ID=v_Cash_ID;
      --
      INSERT
      INTO C_CASHLINE
        (
          C_CashLine_ID, AD_Client_ID, AD_Org_ID, IsActive,
          Created, CreatedBy, Updated, UpdatedBy,
          C_Cash_ID, C_Debt_Payment_ID, Line, Description,
          Amount, CashType, DiscountAmt, WriteOffAmt,
          IsGenerated
        )
        VALUES
        (
          v_CashLine_ID, v_Client_ID, v_Org_ID, 'Y',
          now(), v_UpdatedBy, now(), v_UpdatedBy,
          v_Cash_ID, v_debtPaymentID, v_Line, v_DocumentNo,
          (v_GrandTotal-v_totalCash) * (CASE WHEN v_isSoTrx='N' THEN -1 ELSE 1 END), 'P', 0, 0,
           'Y'
        )
        ;
      DBMS_OUTPUT.PUT_LINE('  CashLine_ID=' || v_CashLine_ID) ;


    END IF; -- CashLine_ID IS NULL OR CashLine_ID = 0
  END IF; -- v_PaymentRule = 'C'
END;
END IF;--FINISH_PROCESS
IF(NOT FINISH_PROCESS) THEN
  -- End Processing --------------------------------------------------------
  --<<END_PROCESSING>>
  -- Cloase Order
  IF(v_DocAction='CL') THEN
    UPDATE C_ORDER
      SET DocStatus='CL',
      DocAction='--',
      Processed='Y'
    WHERE C_Order_ID=v_Record_ID;
  END IF;
  IF(p_PInstance_ID IS NOT NULL) THEN
    v_ResultStr:='UnLockingOrder';
    UPDATE C_ORDER
      SET Processing='N',
      Updated=now(),
      UpdatedBy=v_User
    WHERE C_Order_ID=v_Record_ID;
    COMMIT;
  END IF;
END IF;--FINISH_PROCESS
--<<FINISH_PROCESS>>
IF(p_PInstance_ID IS NOT NULL) THEN
  --  Update AD_PInstance
  DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished - ' || v_Message) ;
  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
END IF;
RETURN;
END; --BODY
EXCEPTION
WHEN OTHERS THEN
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
  v_ResultStr:= '@ERROR=' || SQLERRM;
  IF(p_PInstance_ID IS NOT NULL) THEN
    ROLLBACK;
    --Inserted by Carlos Romero 062706
    UPDATE C_ORDER  SET Processing='N'  WHERE C_Order_ID=v_Record_ID;
    DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
  ELSE
    DECLARE
      v_Code NUMBER:=SQLCODE;
    BEGIN
      INSERT
      INTO DBA_ERRORLOG
        (
          DBA_ErrorLog_ID, Created,
          Code, Msg,
          Info
        )
        VALUES
        (
          DBA_ErrorLog_Seq.NEXTVAL, now(),
          v_Code, v_ResultStr,
           'C_Order_Post'
        )
        ;
    END;
    RAISE;
  END IF;
  RETURN;
END C_ORDER_POST1
]]></body>
    </function>
  </database>