src-db/database/model/functions/M_INOUT_POST.xml
author Mikel Irurita <mikel.irurita@openbravo.com>
Thu, 27 Dec 2012 10:41:54 +0100
changeset 19131 f52f30e7dc2f
parent 19010 888d6b7e865a
child 19135 0bd62a50db37
permissions -rw-r--r--
Fixes issue 22678: ability to specify reversal document date when voiding
juanpablo@3490
     1
<?xml version="1.0"?>
juanpablo@3490
     2
  <database name="FUNCTION M_INOUT_POST">
juanpablo@3490
     3
    <function name="M_INOUT_POST" type="NULL">
juanpablo@3490
     4
      <parameter name="p_pinstance_id" type="VARCHAR" mode="in">
juanpablo@3490
     5
        <default/>
juanpablo@3490
     6
      </parameter>
juanpablo@3490
     7
      <parameter name="p_inout_id" type="VARCHAR" mode="in">
juanpablo@3490
     8
        <default/>
juanpablo@3490
     9
      </parameter>
gorkaion@239
    10
      <body><![CDATA[/*************************************************************************
juanpablo@771
    11
  * The contents of this file are subject to the Compiere Public
juanpablo@771
    12
  * License 1.1 ("License"); You may not use this file except in
juanpablo@771
    13
  * compliance with the License. You may obtain a copy of the License in
juanpablo@771
    14
  * the legal folder of your Openbravo installation.
carlos@0
    15
  * Software distributed under the License is distributed on an
carlos@0
    16
  * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
carlos@0
    17
  * implied. See the License for the specific language governing rights
carlos@0
    18
  * and limitations under the License.
juanpablo@778
    19
  * The Original Code is  Compiere  ERP &  Business Solution
juanpablo@771
    20
  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
carlos@0
    21
  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
carlos@0
    22
  * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
carlos@0
    23
  * All Rights Reserved.
ggi@6702
    24
  * Contributor(s): Openbravo SLU
ioritz@16496
    25
  * Contributions are Copyright (C) 2001-2012 Openbravo, S.L.U.
juanpablo@771
    26
  *
juanpablo@771
    27
  * Specifically, this derivative work is based upon the following Compiere
juanpablo@771
    28
  * file and version.
carlos@0
    29
  *************************************************************************
carlos@0
    30
  * $Id: M_InOut_Post.sql,v 1.8 2003/09/05 04:58:06 jjanke Exp $
carlos@0
    31
  ***
carlos@0
    32
  * Title: Post M_InOut_ID
carlos@0
    33
  * Description:
carlos@0
    34
  *  Action: COmplete
carlos@0
    35
  *  - Create Transaction
carlos@0
    36
  *    (only stocked products)
carlos@0
    37
  *  - Update Inventory (QtyReserved, QtyOnHand)
carlos@0
    38
  *    (only stocked products)
carlos@0
    39
  *  - Update OrderLine (QtyDelivered)
carlos@0
    40
  *
carlos@0
    41
  *  Action: Reverse Correction
carlos@0
    42
  *  - Create Header and lines with negative Quantities (and header amounts)
carlos@0
    43
  *  - Post it
carlos@0
    44
  ************************************************************************/
carlos@0
    45
  -- Logistice
carlos@0
    46
  v_ResultStr VARCHAR2(2000):='';
carlos@0
    47
  v_Message VARCHAR2(2000):='';
gorkaion@18652
    48
  v_Message_aux VARCHAR2(2000):='';
juanpablo@1605
    49
  v_Record_ID VARCHAR2(32);
juanpablo@1605
    50
  v_User VARCHAR2(32);
harikrishnan@7227
    51
  v_PUser VARCHAR2(32);
asier@1929
    52
  v_is_included NUMBER:=0;
asier@1929
    53
  v_DocType_ID VARCHAR2(32);
gorkaion@14547
    54
  v_isreturndoctype CHAR(1);
asier@1929
    55
  v_available_period NUMBER:=0;
asier@1929
    56
  v_is_ready AD_Org.IsReady%TYPE;
asier@1929
    57
  v_is_tr_allow AD_OrgType.IsTransactionsAllowed%TYPE;
asier@1929
    58
  v_DateAcct DATE;
victor@3065
    59
  v_isacctle AD_OrgType.IsAcctLegalEntity%TYPE;
victor@3065
    60
  v_org_bule_id AD_Org.AD_Org_ID%TYPE;
carlos@0
    61
  -- Parameter
carlos@0
    62
  TYPE RECORD IS REF CURSOR;
carlos@0
    63
    Cur_Parameter RECORD;
carlos@0
    64
    --
carlos@0
    65
    Cur_InOut RECORD;
carlos@0
    66
    Cur_InOutLine RECORD;
gorkaion@3170
    67
    Cur_Order RECORD;
carlos@0
    68
    --
carlos@0
    69
    v_Result NUMBER:=1;
juanpablo@1605
    70
    v_AD_Org_ID VARCHAR2(32);
juanpablo@1605
    71
    v_AD_Client_ID VARCHAR2(32);
juanpablo@1605
    72
    v_NextNo VARCHAR2(32);
carlos@0
    73
    v_Qty NUMBER;
carlos@0
    74
    v_QtyPO NUMBER;
carlos@0
    75
    v_QtySO NUMBER;
carlos@0
    76
    v_QuantityOrder NUMBER;
carlos@0
    77
    v_QuantityOrderPO NUMBER;
carlos@0
    78
    v_QuantityOrderSO NUMBER;
carlos@0
    79
    v_RDocumentNo VARCHAR2(40) ;
juanpablo@1605
    80
    v_RInOut_ID VARCHAR2(32);
carlos@0
    81
    v_IsStocked NUMBER;
juanpablo@1605
    82
    v_DoctypeReversed_ID VARCHAR2(32);
carlos@0
    83
    --MODIFIED BY F.IRIAZABAL
carlos@0
    84
    v_QtyOrder NUMBER;
carlos@0
    85
    v_ProductUOM NUMBER;
asier@2586
    86
    v_BreakDown VARCHAR2(60) ;
carlos@0
    87
    v_ActualQty NUMBER;
carlos@0
    88
    v_QtyAux NUMBER;
carlos@0
    89
    v_Count NUMBER:=0;
carlos@0
    90
    v_Line VARCHAR2(10) ;
gorkaion@3170
    91
    v_OrderID_old VARCHAR2(32);
harikrishnan@7945
    92
    Cur_MILines RECORD;
carlos@0
    93
    FINISH_PROCESS BOOLEAN:=false;
harikrishnan@5580
    94
    v_Aux NUMBER;
ioritz@17861
    95
    v_ProductName M_Product.name%TYPE;
gorkaion@18652
    96
    v_reservation_id    VARCHAR2(32);
mikel@19131
    97
    v_voidmovementdate M_Inout.MovementDate%TYPE;
mikel@19131
    98
    v_voiddate_acct M_Inout.DateAcct%TYPE;
mikel@19131
    99
carlos@0
   100
  BEGIN
carlos@0
   101
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   102
      --  Update AD_PInstance
carlos@0
   103
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
   104
      v_ResultStr:='PInstanceNotFound';
carlos@0
   105
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
   106
      -- Get Parameters
carlos@0
   107
      v_ResultStr:='ReadingParameters';
carlos@0
   108
      FOR Cur_Parameter IN
carlos@0
   109
        (SELECT i.Record_ID,
carlos@0
   110
          i.AD_User_ID,
carlos@0
   111
          p.ParameterName,
carlos@0
   112
          p.P_String,
carlos@0
   113
          p.P_Number,
carlos@0
   114
          p.P_Date
carlos@0
   115
        FROM AD_PInstance i
carlos@0
   116
        LEFT JOIN AD_PInstance_Para p
carlos@0
   117
          ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
   118
        WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
   119
        ORDER BY p.SeqNo
carlos@0
   120
        )
carlos@0
   121
      LOOP
carlos@0
   122
        v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
   123
        v_User:=Cur_Parameter.AD_User_ID;
mikel@19131
   124
        IF (Cur_Parameter.parametername = 'voidedDocumentDate') THEN
mikel@19131
   125
          v_voidmovementdate := TO_DATE(Cur_Parameter.p_string, 'YYYY-MM-DD');
mikel@19131
   126
        ELSIF (Cur_Parameter.parametername = 'voidedDocumentAcctDate') THEN
mikel@19131
   127
          v_voiddate_acct := TO_DATE(Cur_Parameter.p_string, 'YYYY-MM-DD');
mikel@19131
   128
        END IF;
carlos@0
   129
      END LOOP; -- Get Parameter
carlos@0
   130
      DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
carlos@0
   131
    ELSE
gorkaion@239
   132
      DBMS_OUTPUT.PUT_LINE('--<<M_InOut_Post>>') ;
carlos@0
   133
      v_Record_ID:=p_InOut_ID;
harikrishnan@7227
   134
      SELECT count(*),updatedby
harikrishnan@7227
   135
        INTO v_Count,v_User
rafael@5825
   136
        FROM M_InOut
harikrishnan@7227
   137
        WHERE M_InOut_ID=v_Record_ID
harikrishnan@7227
   138
        GROUP BY updatedby;
rafael@5825
   139
      IF v_Count=0 THEN
rafael@5825
   140
        FINISH_PROCESS:=true;
rafael@5825
   141
      END IF;
carlos@0
   142
    END IF;
carlos@0
   143
  BEGIN --BODY
rafael@5825
   144
  	IF(NOT FINISH_PROCESS) THEN
harikrishnan@7227
   145
  	  v_PUser:=v_User;
gorkaion@14547
   146
      SELECT io.AD_Client_ID, io.AD_Org_ID, io.CreatedBy, io.C_DocType_ID, io.DateAcct, dt.isreturn
gorkaion@14547
   147
        INTO v_AD_Client_ID, v_AD_Org_ID, v_User, v_DocType_ID, v_DateAcct, v_isreturndoctype
gorkaion@14547
   148
        FROM M_InOut io JOIN c_doctype dt ON io.c_doctype_id = dt.c_doctype_id
gorkaion@14547
   149
        WHERE io.M_InOut_ID=v_Record_ID;
harikrishnan@7227
   150
        IF(v_PUser IS NOT NULL) THEN
harikrishnan@7227
   151
        	v_User:=v_PUser;
harikrishnan@7227
   152
        END IF;
carlos@0
   153
      SELECT count(*)
carlos@0
   154
      INTO v_Count
rafael@5825
   155
      FROM AD_CLIENTINFO
rafael@5825
   156
      WHERE AD_CLIENT_ID=v_AD_Client_ID
rafael@5825
   157
        AND CHECKINOUTORG='Y';
gorkaion@239
   158
      IF v_Count>0 THEN
rafael@5825
   159
        v_ResultStr:='CheckingRestrictions - M_INOUT ORG IS IN C_BPARTNER ORG TREE';
rafael@5825
   160
        SELECT count(*)
rafael@5825
   161
        INTO v_Count
rafael@5825
   162
        FROM M_InOut m,
rafael@5825
   163
          C_BPartner bp
rafael@5825
   164
        WHERE m.M_InOut_ID=v_Record_ID
rafael@5825
   165
          AND m.C_BPARTNER_ID=bp.C_BPARTNER_ID
rafael@5825
   166
          AND AD_IsOrgIncluded(m.AD_ORG_ID, bp.AD_ORG_ID, bp.AD_CLIENT_ID)=-1;
rafael@5825
   167
        IF v_Count>0 THEN
rafael@5825
   168
          RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgBpartnerInout@') ;
rafael@5825
   169
        END IF;
carlos@0
   170
      END IF;
rafael@5825
   171
      
rafael@5825
   172
     v_ResultStr:='CheckingRestrictions';
rafael@5825
   173
     SELECT COUNT(*)
rafael@5825
   174
     INTO v_Count
rafael@5825
   175
     FROM C_DocType,
rafael@5825
   176
          M_InOut M
rafael@5825
   177
     WHERE M_Inout_ID = v_Record_ID
rafael@5825
   178
       AND C_DocType.DocBaseType IN ('MMR', 'MMS')
rafael@5825
   179
      AND C_DocType.IsSOTrx=M.IsSOTrx
rafael@5825
   180
      AND AD_ISORGINCLUDED(m.AD_Org_ID,C_DocType.AD_Org_ID, m.AD_Client_ID) <> -1
rafael@5825
   181
       AND M.C_DOCTYPE_ID=C_DocType.C_DOCTYPE_ID;
rafael@5825
   182
        IF v_Count=0 THEN
rafael@5825
   183
          RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgDoctypeShipment@') ;
rafael@5825
   184
        END IF;
rafael@5825
   185
        SELECT COUNT(*), MAX(M.line)
rafael@5825
   186
        INTO v_Count, v_line
rafael@5825
   187
        FROM M_InOutLine M,
rafael@5825
   188
          M_Product P
rafael@5825
   189
        WHERE M.M_PRODUCT_ID=P.M_PRODUCT_ID
rafael@5825
   190
          AND P.M_ATTRIBUTESET_ID IS NOT NULL
rafael@6091
   191
          AND (P.ATTRSETVALUETYPE IS NULL OR P.ATTRSETVALUETYPE <> 'F')
rafael@6104
   192
          AND (SELECT ISONEATTRSETVALREQUIRED FROM M_ATTRIBUTESET WHERE M_ATTRIBUTESET_ID = P.M_ATTRIBUTESET_ID) = 'Y'
rafael@5825
   193
          AND COALESCE(M.M_ATTRIBUTESETINSTANCE_ID, '0') = '0'
rafael@5825
   194
          AND M.M_INOUT_ID=v_Record_ID;
rafael@5825
   195
        IF v_Count<>0 THEN
rafael@6091
   196
          RAISE_APPLICATION_ERROR(-20000, '@Inline@'||' '||v_line||' '||'@productWithoutAttributeSet@') ;
rafael@5825
   197
        END IF;
rafael@5825
   198
        SELECT COUNT(*), MAX(M.line)
rafael@5825
   199
        INTO v_Count, v_Line
rafael@5825
   200
        FROM M_InOut I,
rafael@5825
   201
          M_InOutLine M,
rafael@5825
   202
          M_AttributeSetInstance P
rafael@5825
   203
        WHERE I.M_InOut_ID=M.M_InOut_ID
rafael@5825
   204
          AND M.M_AttributeSetInstance_ID=P.M_AttributeSetInstance_ID
rafael@5825
   205
          AND P.ISLOCKED='Y'
rafael@5825
   206
          AND I.ISSOTRX='Y'
rafael@5825
   207
          AND I.M_INOUT_ID=v_Record_ID;
rafael@5825
   208
        IF v_Count<>0 THEN
rafael@5825
   209
          RAISE_APPLICATION_ERROR(-20000, '@Inline@'||v_line||' '||'@lockedProduct@') ;
rafael@5825
   210
        END IF;
rafael@5825
   211
      -- check inout line instance location
rafael@5825
   212
        SELECT COUNT(*), MAX(M.line)
rafael@5825
   213
        INTO v_Count, v_Line
rafael@5825
   214
        FROM M_InOutLine M,
rafael@5825
   215
          M_Product P
rafael@5825
   216
        WHERE M.M_InOut_ID=v_Record_ID
rafael@5825
   217
          AND M.M_Locator_ID IS NULL
rafael@5825
   218
          AND p.m_product_id = m.m_product_id
rafael@5825
   219
          AND p.isstocked = 'Y'
rafael@5825
   220
          AND p.producttype = 'I';
rafael@5825
   221
        IF v_Count <> 0 THEN
rafael@5825
   222
          RAISE_APPLICATION_ERROR(-20000, '@Inline@'||v_line||' '||'@InoutLineWithoutLocator@') ;
rafael@5825
   223
        END IF;	  
rafael@5825
   224
      
gorkaion@14547
   225
    --Check negative quantities on return inouts
gorkaion@14547
   226
    IF (v_isreturndoctype = 'Y') THEN
gorkaion@14547
   227
      SELECT count(*) INTO v_count
gorkaion@14547
   228
      FROM m_inoutline iol JOIN c_orderline ol ON iol.c_orderline_id = ol.c_orderline_id
gorkaion@14547
   229
      WHERE iol.m_inout_id = v_record_id
gorkaion@14547
   230
        AND iol.movementqty > 0
ioritz@15868
   231
        AND canceled_inoutline_id IS NULL
gorkaion@14547
   232
        AND ol.c_order_discount_id IS NULL;
gorkaion@14547
   233
      IF (v_Count <> 0) THEN
gorkaion@14547
   234
        RAISE_APPLICATION_ERROR(-20000, '@ReturnInOutNegativeQty@');
gorkaion@14547
   235
      END IF;
gorkaion@14547
   236
    END IF;
rafael@5825
   237
        -- Process Shipments
rafael@5825
   238
  
pandeeswari@18808
   239
      SELECT COUNT(*)
pandeeswari@18808
   240
        INTO v_Count
pandeeswari@18808
   241
      FROM M_INOUT IO, M_INOUTLINE IOL
pandeeswari@18808
   242
      WHERE IO.M_INOUT_ID = IOL.M_INOUT_ID
pandeeswari@18808
   243
        AND AD_ISORGINCLUDED(IOL.AD_Org_ID, IO.AD_Org_ID, IO.AD_Client_ID) <> -1
pandeeswari@18808
   244
        AND IO.M_INOUT_ID = v_Record_ID;
pandeeswari@18808
   245
      IF (v_Count=0) THEN
pandeeswari@18808
   246
        RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgLines@') ;
pandeeswari@18808
   247
      END IF;
rafael@5825
   248
      
rafael@5825
   249
      -- Check the header belongs to a organization where transactions are posible and ready to use
rafael@5825
   250
      SELECT AD_Org.IsReady, Ad_OrgType.IsTransactionsAllowed
rafael@5825
   251
      INTO v_is_ready, v_is_tr_allow
rafael@5825
   252
      FROM M_INOUT, AD_Org, AD_OrgType
rafael@5825
   253
      WHERE AD_Org.AD_Org_ID=M_INOUT.AD_Org_ID
rafael@5825
   254
      AND AD_Org.AD_OrgType_ID=AD_OrgType.AD_OrgType_ID
rafael@5825
   255
      AND M_INOUT.M_INOUT_ID=v_Record_ID;
rafael@5825
   256
      IF (v_is_ready='N') THEN
rafael@5825
   257
        RAISE_APPLICATION_ERROR(-20000, '@OrgHeaderNotReady@');
carlos@0
   258
      END IF;
rafael@5825
   259
      IF (v_is_tr_allow='N') THEN
rafael@5825
   260
        RAISE_APPLICATION_ERROR(-20000, '@OrgHeaderNotTransAllowed@');
carlos@0
   261
      END IF;
rafael@5825
   262
        
rafael@5825
   263
      SELECT AD_ORG_CHK_DOCUMENTS('M_INOUT', 'M_INOUTLINE', v_Record_ID, 'M_INOUT_ID', 'M_INOUT_ID') INTO v_is_included FROM dual;
rafael@5825
   264
      IF (v_is_included=-1) THEN
rafael@5825
   265
        RAISE_APPLICATION_ERROR(-20000, '@LinesAndHeaderDifferentLEorBU@');
carlos@0
   266
      END IF;
asier@1929
   267
      
rafael@5825
   268
      -- Check the period control is opened (only if it is legal entity with accounting)
rafael@5825
   269
      -- Gets the BU or LE of the document
rafael@5825
   270
      SELECT AD_GET_DOC_LE_BU('M_INOUT', v_Record_ID, 'M_INOUT_ID', 'LE')
rafael@5825
   271
      INTO v_org_bule_id
victor@3065
   272
      FROM DUAL;
victor@3065
   273
      
rafael@5825
   274
      SELECT AD_OrgType.IsAcctLegalEntity
rafael@5825
   275
      INTO v_isacctle
rafael@5825
   276
      FROM AD_OrgType, AD_Org
rafael@5825
   277
      WHERE AD_Org.AD_OrgType_ID = AD_OrgType.AD_OrgType_ID
rafael@5825
   278
      AND AD_Org.AD_Org_ID=v_org_bule_id;
rafael@5825
   279
      
rafael@5825
   280
      IF (v_isacctle='Y') THEN    
rafael@5825
   281
        SELECT C_CHK_OPEN_PERIOD(v_AD_Org_ID, v_DateAcct, NULL, v_DocType_ID) 
rafael@5825
   282
        INTO v_available_period
rafael@5825
   283
        FROM DUAL;
rafael@5825
   284
        
rafael@5825
   285
        IF (v_available_period<>1) THEN
rafael@5825
   286
          RAISE_APPLICATION_ERROR(-20000, '@PeriodNotAvailable@');
rafael@5825
   287
        END IF;
rafael@5825
   288
      END IF;  
rafael@5825
   289
  
rafael@5825
   290
        FOR Cur_InOut IN
rafael@5825
   291
          (SELECT *
rafael@5825
   292
          FROM M_INOUT
rafael@5825
   293
          WHERE(M_InOut_ID=v_Record_ID
rafael@5825
   294
            OR(v_Record_ID IS NULL
rafael@5825
   295
            AND DocAction='CO'))
rafael@5825
   296
            AND IsActive='Y'  FOR UPDATE
rafael@5825
   297
          )
rafael@5825
   298
        LOOP
rafael@5825
   299
          DBMS_OUTPUT.PUT_LINE('Shipment_ID=' || Cur_InOut.M_InOut_ID || ', Doc=' || Cur_InOut.DocumentNo || ', Status=' || Cur_InOut.DocStatus || ', Action=' || Cur_InOut.DocAction) ;
rafael@5825
   300
          v_ResultStr:='HeaderLoop';
rafael@5825
   301
          /**
rafael@5825
   302
          * Shipment not processed
rafael@5825
   303
          */
rafael@5825
   304
          IF(Cur_InOut.Processed='N' AND Cur_InOut.DocStatus='DR' AND Cur_InOut.DocAction='CO') THEN
rafael@5825
   305
            -- For all active shipment lines
rafael@5825
   306
            v_ResultStr:='HeaderLoop-1';
rafael@5825
   307
            SELECT COUNT(*) INTO v_Aux
rafael@5825
   308
        FROM M_InOutLine
rafael@5825
   309
        WHERE M_InOut_ID = v_Record_ID;
rafael@5825
   310
        IF v_Aux=0 THEN
rafael@5825
   311
        RAISE_APPLICATION_ERROR(-20000, '@ReceiptWithoutLines@');
rafael@5825
   312
        END IF;
carlos@0
   313
          FOR Cur_InOutLine IN
carlos@0
   314
            (SELECT *
carlos@0
   315
            FROM M_INOUTLINE
carlos@0
   316
            WHERE M_InOut_ID=Cur_InOut.M_InOut_ID
carlos@0
   317
              AND IsActive='Y'  FOR UPDATE
carlos@0
   318
            )
carlos@0
   319
          LOOP
adrian@170
   320
            -- Incomming or Outgoing :1:2
carlos@0
   321
            v_Qty:=Cur_InOutLine.MovementQty;
carlos@0
   322
            v_QuantityOrder:=Cur_InOutLine.QuantityOrder;
carlos@0
   323
            IF(SUBSTR(Cur_InOut.MovementType, 2)='-') THEN
carlos@0
   324
              v_Qty:=- Cur_InOutLine.MovementQty;
carlos@0
   325
              v_QuantityOrder:=-Cur_InOutLine.QuantityOrder;
carlos@0
   326
            END IF;
carlos@0
   327
            IF(Cur_InOut.IsSOTrx='N') THEN
carlos@0
   328
              v_QtySO:=0;
carlos@0
   329
              v_QtyPO:=Cur_InOutLine.MovementQty;
carlos@0
   330
              v_QuantityOrderSO:=0;
carlos@0
   331
              v_QuantityOrderPO:=Cur_InOutLine.QuantityOrder;
carlos@0
   332
            ELSE
carlos@0
   333
              v_QtySO:=Cur_InOutLine.MovementQty;
carlos@0
   334
              v_QtyPO:=0;
carlos@0
   335
              v_QuantityOrderSO:=Cur_InOutLine.QuantityOrder;
carlos@0
   336
              v_QuantityOrderPO:=0;
carlos@0
   337
            END IF;
carlos@0
   338
            -- UOM Conversion
adrian@170
   339
            -- Is it a standard stocked product:3
carlos@0
   340
            SELECT COUNT(*)
carlos@0
   341
            INTO v_IsStocked
carlos@0
   342
            FROM M_PRODUCT
carlos@0
   343
            WHERE M_Product_ID=Cur_InOutLine.M_Product_ID
carlos@0
   344
              AND IsStocked='Y'
carlos@0
   345
              AND ProductType='I';
carlos@0
   346
            -- Create Transaction for stocked product
harikrishnan@8015
   347
            IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND v_IsStocked=1 AND Cur_InOutLine.IsDescription <> 'Y') THEN
gorkaion@18652
   348
              IF (cur_inout.issotrx = 'Y' AND cur_inoutline.c_orderline_id IS NOT NULL AND v_qty < 0 AND cur_inoutline.canceled_inoutline_id IS NULL) THEN
gorkaion@18652
   349
                -- Manage reservations.
gorkaion@18652
   350
                SELECT count(*), max(m_reservation_id)
gorkaion@18652
   351
                  INTO v_aux, v_reservation_id
gorkaion@18652
   352
                FROM m_reservation
gorkaion@18706
   353
                WHERE c_orderline_id = cur_inoutline.c_orderline_id;
gorkaion@18652
   354
                IF (v_aux > 1) THEN
gorkaion@18652
   355
                  RAISE_APPLICATION_ERROR(-20000, '@SOLineWithMoreThanOneOpenReservation@');
gorkaion@18652
   356
                ELSIF (v_aux = 1) THEN
gorkaion@18652
   357
                  M_RESERVATION_CONSUMPTION(v_reservation_id, cur_inoutline.m_locator_id, cur_inoutline.m_attributesetinstance_id, cur_inoutline.movementqty, v_user, v_result, v_message);
gorkaion@18652
   358
                END IF;
gorkaion@18706
   359
              ELSIF (cur_inout.issotrx = 'Y' AND cur_inoutline.c_orderline_id IS NOT NULL AND v_qty > 0 AND cur_inoutline.canceled_inoutline_id IS NOT NULL) THEN
gorkaion@18706
   360
                -- Undo reservation
gorkaion@18706
   361
                DECLARE
gorkaion@18706
   362
                  cur_released_stock RECORD;
gorkaion@18706
   363
                  v_qtyaux NUMBER;
gorkaion@18706
   364
                  v_undoqty NUMBER;
gorkaion@18706
   365
                BEGIN
gorkaion@18706
   366
                  SELECT count(*), max(m_reservation_id)
gorkaion@18706
   367
                    INTO v_aux, v_reservation_id
gorkaion@18706
   368
                  FROM m_reservation
gorkaion@18706
   369
                  WHERE c_orderline_id = cur_inoutline.c_orderline_id;
gorkaion@18706
   370
                  IF (v_aux > 1) THEN
gorkaion@18706
   371
                    RAISE_APPLICATION_ERROR(-20000, '@SOLineWithMoreThanOneOpenReservation@');
gorkaion@18706
   372
                  ELSIF (v_aux = 1) THEN
gorkaion@18706
   373
                    v_qtyaux := v_qty;
gorkaion@18706
   374
                    FOR cur_released_stock IN (
gorkaion@18706
   375
                        SELECT m_reservation_stock_id, quantity, releasedqty
gorkaion@18706
   376
                        FROM m_reservation_stock
gorkaion@18706
   377
                        WHERE m_locator_id = cur_inoutline.m_locator_id
gorkaion@18706
   378
                          AND COALESCE(m_attributesetinstance_id, '0') = COALESCE(cur_inoutline.m_attributesetinstance_id, '0')
gorkaion@18706
   379
                          AND m_reservation_id = v_reservation_id
gorkaion@18706
   380
                          AND COALESCE(releasedqty, 0) > 0
gorkaion@18706
   381
                        ORDER BY CASE isallocated WHEN 'N' THEN 0 ELSE 1 END
gorkaion@18706
   382
                    ) LOOP
gorkaion@18706
   383
                      v_undoqty := LEAST(v_qtyaux, cur_released_stock.releasedqty);
gorkaion@18706
   384
                      UPDATE m_reservation_stock
gorkaion@18706
   385
                      SET releasedqty = releasedqty - v_undoqty
gorkaion@18706
   386
                      WHERE m_reservation_stock_id = cur_released_stock.m_reservation_stock_id;
gorkaion@18706
   387
                      v_qtyaux := v_qtyaux - v_undoqty;
gorkaion@18706
   388
                    END LOOP;
gorkaion@18706
   389
                  END IF;
gorkaion@18706
   390
                END;
gorkaion@18681
   391
              ELSIF (cur_inout.issotrx = 'N') THEN
gorkaion@18681
   392
                -- Manage pre-reserves
gorkaion@18681
   393
                DECLARE
gorkaion@18681
   394
                  cur_reserve_stock RECORD;
gorkaion@18681
   395
                  v_pendingqty NUMBER;
gorkaion@18681
   396
                  v_qtyaux NUMBER;
gorkaion@18681
   397
                  v_res_stock_id VARCHAR2(32);
gorkaion@18681
   398
                BEGIN
gorkaion@18681
   399
                  v_pendingqty := v_qty;
gorkaion@18681
   400
                  FOR cur_reserve_stock IN (
gorkaion@18681
   401
                      SELECT rs.*
gorkaion@18681
   402
                      FROM m_reservation_stock rs JOIN m_reservation r ON rs.m_reservation_id = r.m_reservation_id
gorkaion@18681
   403
                      WHERE rs.c_orderline_id = cur_inoutline.c_orderline_id
gorkaion@18681
   404
                        AND rs.quantity <> COALESCE(rs.releasedqty, 0)
gorkaion@18681
   405
                        AND rs.m_locator_id IS NULL
gorkaion@18681
   406
                        AND r.res_status != 'CL'
gorkaion@18681
   407
                  ) LOOP
gorkaion@18681
   408
                    v_qtyaux := LEAST(cur_reserve_stock.quantity - COALESCE(cur_reserve_stock.releasedqty, 0), v_pendingqty);
gorkaion@18681
   409
                    -- Check if exists a reserved stock for the same orderline, attributes and locator in the reservation
gorkaion@18681
   410
                    SELECT count(*), max(m_reservation_stock_id) INTO v_aux, v_res_stock_id
gorkaion@18681
   411
                    FROM m_reservation_stock
gorkaion@18681
   412
                      WHERE c_orderline_id = cur_inoutline.c_orderline_id
gorkaion@18681
   413
                        AND m_locator_id = cur_inoutline.m_locator_id
gorkaion@18681
   414
                        AND m_reservation_id = cur_reserve_stock.m_reservation_id
gorkaion@18683
   415
                        AND isallocated = 'Y'
gorkaion@18681
   416
                        AND COALESCE(m_attributesetinstance_id, '0') = COALESCE(Cur_InOutLine.M_AttributeSetInstance_ID, '0');
gorkaion@18681
   417
                    -- UPDATE EXISTING PRERESERVED STOCK TO DECREASE RESERVED QTY
gorkaion@18681
   418
                    UPDATE m_reservation_stock
gorkaion@18681
   419
                    SET quantity = quantity - v_qtyaux
gorkaion@18681
   420
                    WHERE m_reservation_stock_id = cur_reserve_stock.m_reservation_stock_id;
gorkaion@18681
   421
                    -- INSERT OR UPDATE RESERVED STOCK BY SAME QUANTITY
gorkaion@18681
   422
                    IF (v_aux > 0) THEN
gorkaion@18681
   423
                      UPDATE m_reservation_stock
gorkaion@18681
   424
                      SET quantity = quantity + v_qtyaux
gorkaion@18681
   425
                      WHERE m_reservation_stock_id = v_res_stock_id;
gorkaion@18681
   426
                    ELSE
gorkaion@18681
   427
                      INSERT INTO m_reservation_stock(
gorkaion@18681
   428
                        m_reservation_stock_id, ad_client_id, ad_org_id, isactive,
gorkaion@18681
   429
                        created, createdby, updated, updatedby,
gorkaion@18681
   430
                        m_reservation_id, m_attributesetinstance_id, m_locator_id, c_orderline_id,
gorkaion@18681
   431
                        quantity, releasedqty, isallocated
gorkaion@18681
   432
                      ) VALUES (
gorkaion@18681
   433
                        get_uuid(), cur_reserve_stock.ad_client_id, cur_reserve_stock.ad_org_id, 'Y',
gorkaion@18681
   434
                        now(), v_user, now(), v_user,
gorkaion@18681
   435
                        cur_reserve_stock.m_reservation_id, cur_inoutline.m_attributesetinstance_id, cur_inoutline.m_locator_id, cur_inoutline.c_orderline_id,
gorkaion@18683
   436
                        v_qtyaux, 0, 'Y'
gorkaion@18681
   437
                      );
gorkaion@18681
   438
                    END IF;
gorkaion@18681
   439
                    v_pendingqty := v_pendingqty - v_qtyaux;
gorkaion@18681
   440
                    IF (v_pendingqty <= 0) THEN
gorkaion@18681
   441
                      EXIT;
gorkaion@18681
   442
                    END IF;
gorkaion@18681
   443
                  END LOOP;
gorkaion@18681
   444
                  DELETE FROM m_reservation_stock
gorkaion@18681
   445
                  WHERE c_orderline_id = cur_inoutline.c_orderline_id
gorkaion@18681
   446
                    AND quantity = 0
gorkaion@18681
   447
                    AND COALESCE(releasedqty, 0) = 0;
gorkaion@18681
   448
                END;
gorkaion@18652
   449
              END IF;
gorkaion@18652
   450
              
carlos@0
   451
              v_ResultStr:='CreateTransaction';
carlos@0
   452
              Ad_Sequence_Next('M_Transaction', Cur_InOutLine.AD_Org_ID, v_NextNo) ;
carlos@0
   453
              INSERT
carlos@0
   454
              INTO M_TRANSACTION
carlos@0
   455
                (
carlos@0
   456
                  M_Transaction_ID, M_InOutLine_ID, AD_Client_ID, AD_Org_ID,
carlos@0
   457
                  IsActive, Created, CreatedBy, Updated,
carlos@0
   458
                  UpdatedBy, MovementType, M_Locator_ID, M_Product_ID,
carlos@0
   459
                  M_AttributeSetInstance_ID, MovementDate, MovementQty, M_Product_UOM_ID,
carlos@0
   460
                  QuantityOrder, C_UOM_ID
carlos@0
   461
                )
carlos@0
   462
                VALUES
carlos@0
   463
                (
carlos@0
   464
                  v_NextNo, Cur_InOutLine.M_InOutLine_ID, Cur_InOutLine.AD_Client_ID, Cur_InOutLine.AD_Org_ID,
harikrishnan@7227
   465
                   'Y', now(), v_User, now(),
harikrishnan@7227
   466
                  v_User, Cur_InOut.MovementType, Cur_InOutLine.M_Locator_ID, Cur_InOutLine.M_Product_ID,
juanpablo@1605
   467
                  COALESCE(Cur_InOutLine.M_AttributeSetInstance_ID, '0'), Cur_InOut.MovementDate, v_Qty, Cur_InOutLine.M_Product_UOM_ID,
carlos@0
   468
                  v_QuantityOrder, Cur_InOutLine.C_UOM_ID
carlos@0
   469
                )
carlos@0
   470
                ;
carlos@0
   471
            END IF;
carlos@0
   472
            -- Create Asset
carlos@0
   473
            IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND Cur_InOut.IsSOTrx='Y') THEN
antonio@735
   474
              A_ASSET_CREATE(NULL, Cur_InOutLine.M_InOutLine_ID) ;
carlos@0
   475
            END IF;
carlos@0
   476
            v_ResultStr:='UpdateOrderLine';
carlos@0
   477
            IF(Cur_InOutLine.C_OrderLine_ID IS NOT NULL) THEN
carlos@0
   478
              -- stocked product
carlos@0
   479
              IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND v_IsStocked=1) THEN
carlos@0
   480
                -- Update OrderLine (if C-, Qty is negative)
carlos@0
   481
                UPDATE C_ORDERLINE
carlos@0
   482
                  SET QtyReserved=QtyReserved - v_QtyPO - v_QtySO,
carlos@0
   483
                  QtyDelivered=QtyDelivered + v_QtySO,
harikrishnan@7227
   484
                  Updated=now(),
harikrishnan@7227
   485
                  UpdatedBy=v_User
carlos@0
   486
                WHERE C_OrderLine_ID=Cur_InOutLine.C_OrderLine_ID;
carlos@0
   487
                -- Products not stocked
carlos@0
   488
              ELSE
carlos@0
   489
                -- Update OrderLine (if C-, Qty is negative)
carlos@0
   490
                UPDATE C_ORDERLINE
carlos@0
   491
                  SET QtyDelivered=QtyDelivered + v_QtySO,
harikrishnan@7227
   492
                  Updated=now(),
harikrishnan@7227
   493
                  UpdatedBy=v_User
carlos@0
   494
                WHERE C_OrderLine_ID=Cur_InOutLine.C_OrderLine_ID;
carlos@0
   495
              END IF;
carlos@0
   496
            END IF;
carlos@0
   497
            IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND v_IsStocked=1) THEN
carlos@0
   498
              M_Check_Stock(Cur_InOutLine.M_Product_ID, v_AD_Client_ID, v_AD_Org_ID, v_Result, v_Message) ;
carlos@0
   499
              IF v_Result=0 THEN
ioritz@16496
   500
                SELECT name INTO v_ProductName FROM M_Product WHERE M_Product_id = Cur_InOutLine.M_Product_ID;
ioritz@16496
   501
			    RAISE_APPLICATION_ERROR(-20000, v_Message||' '||'@line@'||' '||Cur_InOutLine.line||', '||'@Product@'||' '||v_ProductName) ;
carlos@0
   502
              END IF;
carlos@0
   503
            END IF;
carlos@0
   504
          END LOOP; -- For all InOut Lines
carlos@0
   505
          /*******************
carlos@0
   506
          * PO Matching
carlos@0
   507
          ******************/
carlos@0
   508
          IF(Cur_InOut.IsSOTrx='N') THEN
carlos@0
   509
            DECLARE
carlos@0
   510
              Cur_SLines RECORD;
carlos@0
   511
              Cur_ILines RECORD;
carlos@0
   512
              v_Qty NUMBER;
juanpablo@1605
   513
              v_MatchPO_ID VARCHAR2(32) ;
juanpablo@1605
   514
              v_MatchInv_ID VARCHAR2(32) ;
carlos@0
   515
            BEGIN
carlos@0
   516
              v_ResultStr:='MatchPO';
carlos@0
   517
              FOR Cur_SLines IN
carlos@0
   518
                (SELECT sl.AD_Client_ID,
carlos@0
   519
                  sl.AD_Org_ID,
carlos@0
   520
                  ol.C_OrderLine_ID,
carlos@0
   521
                  sl.M_InOutLine_ID,
carlos@0
   522
                  sl.M_Product_ID,
carlos@0
   523
                  sl.M_AttributeSetInstance_ID,
carlos@0
   524
                  sl.MovementQty,
carlos@0
   525
                  ol.QtyOrdered
carlos@0
   526
                FROM M_INOUTLINE sl,
carlos@0
   527
                  C_ORDERLINE ol
carlos@0
   528
                WHERE sl.C_OrderLine_ID=ol.C_OrderLine_ID
carlos@0
   529
                  AND sl.M_Product_ID=ol.M_Product_ID  --    AND   sl.M_AttributeSetInstance_ID=ol.M_AttributeSetInstance_ID
carlos@0
   530
                  AND sl.M_InOut_ID=Cur_InOut.M_InOut_ID
carlos@0
   531
                )
carlos@0
   532
              LOOP
david@19010
   533
              
david@19010
   534
								SELECT M_MATCHPO_ID
david@19010
   535
								INTO v_MatchPO_ID
david@19010
   536
								FROM M_MATCHPO
david@19010
   537
								WHERE C_ORDERLINE_ID = Cur_SLines.C_OrderLine_ID
david@19010
   538
								  AND M_INOUTLINE_ID = Cur_SLines.M_InOutLine_ID;             
david@19010
   539
david@19010
   540
								v_Qty:=Cur_SLines.MovementQty;
david@19010
   541
                	--IF (ABS(Cur_SLines.MovementQty) > ABS(Cur_SLines.QtyOrdered)) THEN
david@19010
   542
                	-- v_Qty := Cur_SLines.QtyOrdered;
david@19010
   543
                	--END IF;
david@19010
   544
david@19010
   545
								IF (v_MatchPO_ID IS NULL) THEN
david@19010
   546
                	Ad_Sequence_Next('M_MatchPO', Cur_SLines.AD_Org_ID, v_MatchPO_ID) ;
david@19010
   547
                	-- The min qty. Modified by Ismael Ciordia
david@19010
   548
                	v_ResultStr:='InsertMatchPO ' || v_MatchPO_ID;
david@19010
   549
                	INSERT
david@19010
   550
                	INTO M_MATCHPO
david@19010
   551
                  	(
david@19010
   552
                    	M_MatchPO_ID, AD_Client_ID, AD_Org_ID, IsActive,
david@19010
   553
                    	Created, CreatedBy, Updated, UpdatedBy,
david@19010
   554
                    	M_InOutLine_ID, C_OrderLine_ID, M_Product_ID, DateTrx,
david@19010
   555
                    	Qty, Processing, Processed, Posted
david@19010
   556
                  	)
david@19010
   557
                  	VALUES
david@19010
   558
                  	(
david@19010
   559
                    	v_MatchPO_ID, Cur_SLines.AD_Client_ID, Cur_SLines.AD_Org_ID, 'Y',
david@19010
   560
                    	now(), v_User, now(), v_User,
david@19010
   561
                    	Cur_SLines.M_InOutLine_ID, Cur_SLines.C_OrderLine_ID, Cur_SLines.M_Product_ID, now(),
david@19010
   562
                    	v_Qty, 'N', 'Y', 'N'
david@19010
   563
                  	)
david@19010
   564
                  	;
david@19010
   565
                 ELSE
david@19010
   566
                	v_ResultStr:='UpdateMatchPO ' || v_MatchPO_ID;
david@19010
   567
                	UPDATE M_MATCHPO
david@19010
   568
                	SET DateTrx = now(), Qty =v_Qty, Processing = 'N', Processed = 'Y', Posted='N'
david@19010
   569
                	WHERE M_MATCHPO_ID = v_MatchPO_ID;
david@19010
   570
								 END IF;
carlos@0
   571
              END LOOP;
carlos@0
   572
              v_ResultStr:='MatchInv';
carlos@0
   573
              FOR Cur_ILines IN
carlos@0
   574
                (SELECT sl.AD_Client_ID,
carlos@0
   575
                  sl.AD_Org_ID,
carlos@0
   576
                  il.C_InvoiceLine_ID,
carlos@0
   577
                  sl.M_InOutLine_ID,
carlos@0
   578
                  sl.M_Product_ID,
carlos@0
   579
                  sl.M_AttributeSetInstance_ID,
carlos@0
   580
                  sl.MovementQty,
david@7233
   581
                  il.QTYINVOICED,
david@7233
   582
                  i.DateAcct
carlos@0
   583
                FROM M_INOUTLINE sl,
david@7233
   584
                  C_INVOICE i,
carlos@0
   585
                  C_INVOICELINE il
carlos@0
   586
                WHERE sl.M_InOutLine_ID=il.M_InOutLine_ID
carlos@0
   587
                  AND sl.M_InOut_ID=Cur_InOut.M_InOut_ID
david@7233
   588
                  AND i.C_INVOICE_ID = il.C_INVOICE_ID
carlos@0
   589
                )
carlos@0
   590
              LOOP
carlos@0
   591
                Ad_Sequence_Next('M_MatchInv', Cur_ILines.AD_Org_ID, v_MatchInv_ID) ;
carlos@0
   592
                -- The min qty. Modified by Ismael Ciordia
carlos@0
   593
                v_Qty:=Cur_ILines.MovementQty;
gorkaion@239
   594
                --IF (ABS(Cur_ILines.MovementQty) > ABS(Cur_ILines.QtyInvoiced)) THEN
carlos@0
   595
                -- v_Qty := Cur_ILines.QtyInvoiced;
carlos@0
   596
                --END IF;
carlos@0
   597
                v_ResultStr:='InsertMatchPO ' || v_MatchPO_ID;
carlos@0
   598
                INSERT
carlos@0
   599
                INTO M_MATCHINV
carlos@0
   600
                  (
carlos@0
   601
                    M_MATCHINV_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   602
                    CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   603
                    M_INOUTLINE_ID, C_INVOICELINE_ID, M_PRODUCT_ID, DATETRX,
carlos@0
   604
                    QTY, PROCESSING, PROCESSED, POSTED
carlos@0
   605
                  )
carlos@0
   606
                  VALUES
carlos@0
   607
                  (
carlos@0
   608
                    v_MatchInv_ID, Cur_ILines.AD_Client_ID, Cur_ILines.AD_Org_ID, 'Y',
harikrishnan@7227
   609
                    now(), v_User, now(), v_User,
david@7233
   610
                    Cur_ILines.M_InOutLine_ID, Cur_ILines.C_InvoiceLine_ID, Cur_ILines.M_Product_ID, Cur_ILines.DateAcct,
carlos@0
   611
                    v_Qty, 'N', 'Y', 'N'
carlos@0
   612
                  )
carlos@0
   613
                  ;
carlos@0
   614
              END LOOP;
carlos@0
   615
            END;
gorkaion@3170
   616
          ELSE
gorkaion@3170
   617
            v_ResultStr:='Check delivery rule for sales orders';
gorkaion@18652
   618
            v_Message_aux:='';
gorkaion@3170
   619
            v_orderid_old:='0';
gorkaion@3170
   620
            FOR Cur_Order IN 
gorkaion@3170
   621
              (SELECT c_order.deliveryrule, m_inoutline.line, c_order.c_order_id,
gorkaion@3170
   622
                      c_order.documentno, c_orderline.line as orderline
gorkaion@3170
   623
               FROM M_InOutLine, C_Orderline, C_Order
gorkaion@3170
   624
               WHERE M_Inoutline.c_orderline_id = c_orderline.c_orderline_id
gorkaion@3170
   625
                 AND c_orderline.c_order_id = c_order.c_order_id
gorkaion@3170
   626
                 AND m_inoutline.m_inout_id = cur_inout.m_inout_id
gorkaion@3170
   627
                 AND ((c_order.deliveryrule = 'O'
gorkaion@3170
   628
                      AND EXISTS (SELECT 1 FROM C_OrderLine ol
gorkaion@3170
   629
                                  WHERE ol.C_Order_ID = C_order.c_order_id
gorkaion@3170
   630
                                    and ol.qtyordered > ol.qtydelivered ))
gorkaion@3170
   631
                      OR (c_order.deliveryrule = 'L' 
gorkaion@3170
   632
                          AND c_orderline.qtyordered > c_orderline.qtydelivered))
gorkaion@3170
   633
               ORDER BY c_order.c_order_id, c_orderline.line) LOOP
gorkaion@3170
   634
              --Order lines not completely delivered with delivery rule O or L
gorkaion@3170
   635
              IF (v_orderid_old <> cur_order.c_order_id OR cur_order.deliveryrule <> 'O' ) THEN
gorkaion@18652
   636
                v_Message_aux := COALESCE(v_Message_aux,'') || '@Shipment@' || ' ' || cur_inout.documentno;
gorkaion@18652
   637
                v_Message_aux := v_Message_aux || ' ' || '@line@' || ' ' || cur_order.line || ': ';
gorkaion@18652
   638
                v_Message_aux := v_Message_aux || '@SalesOrderDocumentno@' || cur_order.documentno;
gorkaion@3170
   639
                IF (cur_order.deliveryrule = 'O') THEN
gorkaion@18652
   640
                  v_Message_aux := v_Message_aux || ' ' || '@notCompleteDeliveryRuleOrder@' || '<br>';
gorkaion@3170
   641
                ELSE
gorkaion@18652
   642
                  v_Message_aux := v_Message_aux || ' ' || '@line@' || ' ' || cur_order.orderline;
gorkaion@18652
   643
                  v_Message_aux := v_Message_aux || ' ' || '@notCompleteDeliveryRuleLine@' || '<br>';
gorkaion@3170
   644
                END IF;
gorkaion@3170
   645
              END IF;
gorkaion@3170
   646
              v_orderid_old := cur_order.c_order_id;
gorkaion@3170
   647
            END LOOP;
gorkaion@18652
   648
            IF (v_Message_aux IS NOT NULL AND v_Message_aux <> '') THEN
gorkaion@18652
   649
              RAISE_APPLICATION_ERROR(-20000, v_Message_aux);
gorkaion@3170
   650
            END IF;
carlos@0
   651
          END IF;
carlos@0
   652
          -- Close Shipment
carlos@0
   653
          v_ResultStr:='CloseShipment';
carlos@0
   654
          UPDATE M_INOUT
carlos@0
   655
            SET Processed='Y',
carlos@0
   656
            DocStatus='CO',
carlos@0
   657
            DocAction='--',
harikrishnan@7227
   658
            Updated=now(),
harikrishnan@7227
   659
            UpdatedBy=v_User
carlos@0
   660
          WHERE M_INOUT.M_INOUT_ID=Cur_InOut.M_INOUT_ID;
carlos@0
   661
          --
asier@1027
   662
          
asier@1027
   663
         
carlos@0
   664
          -- Not Processed + Complete --
carlos@0
   665
          /**
carlos@0
   666
          * Reverse Correction
carlos@0
   667
          */
carlos@0
   668
        ELSIF(Cur_InOut.DocStatus='CO' AND Cur_InOut.DocAction='RC') THEN
javier@17645
   669
          --Check if the m_inoutlines has an invoice lines related. In this case is not possible to void the m_inout.
javier@17645
   670
	  SELECT COUNT(*)
javier@17645
   671
          INTO v_count
javier@17645
   672
          FROM M_INOUTLINE MIOL 
javier@17645
   673
              JOIN C_INVOICELINE CIL ON MIOL.M_INOUTLINE_ID=CIL.M_INOUTLINE_ID 
javier@17645
   674
              JOIN C_INVOICE CI ON CI.C_INVOICE_ID=CIL.C_INVOICE_ID
javier@17645
   675
          WHERE M_INOUT_ID=Cur_InOut.m_inout_id
javier@17645
   676
          AND CI.DOCSTATUS <> 'VO';
javier@17645
   677
          IF (v_count <> 0) THEN
javier@17645
   678
	     RAISE_APPLICATION_ERROR(-20000,'@VoidShipmentWithRelatedInvoice@');
javier@17645
   679
          END IF;
gorkaion@5377
   680
          --Check that there isn't any line with an invoice if the order's 
gorkaion@5377
   681
          --invoice rule is after delivery
gorkaion@5377
   682
          select count(*), max(line) into v_count, v_line
gorkaion@5377
   683
          from (
gorkaion@5377
   684
          SELECT m_inoutline.m_inoutline_id, m_inoutline.line
gorkaion@5377
   685
          from m_inoutline, c_order, c_orderline, c_invoiceline, m_inout, c_invoice
gorkaion@5377
   686
          where m_inoutline.c_orderline_id = c_orderline.c_orderline_id
gorkaion@5377
   687
            and c_orderline.c_order_id = c_order.c_order_id
gorkaion@5377
   688
            and c_orderline.c_orderline_id = c_invoiceline.c_orderline_id
gorkaion@5377
   689
            and m_inoutline.m_inout_id = m_inout.m_inout_id
gorkaion@5377
   690
            and c_invoiceline.c_invoice_id = c_invoice.c_invoice_id
gorkaion@5377
   691
            and m_inout.m_inout_id = Cur_InOut.m_inout_id
gorkaion@5377
   692
            and m_inout.issotrx = 'Y'
gorkaion@5377
   693
            and c_order.invoicerule in ('D', 'O', 'S')
gorkaion@5377
   694
            and c_invoice.processed='Y'
gorkaion@5377
   695
          group by m_inoutline.m_inoutline_id, m_inoutline.line
rafael@5506
   696
          having sum(c_invoiceline.qtyinvoiced) <> 0
gorkaion@5377
   697
          ) a;
gorkaion@5377
   698
          IF (v_count > 0 ) THEN
gorkaion@5377
   699
            v_Message := '@InoutDocumentno@' || ': ' || Cur_InOut.DocumentNo || ' ' || '@line@' || ': ' || v_line || '. ';
gorkaion@5377
   700
            v_Message := v_Message || '@VoidShipmentInvoiced@';
gorkaion@5377
   701
            RAISE_APPLICATION_ERROR(-20000, v_Message);
gorkaion@5377
   702
          END IF;
carlos@0
   703
          v_ResultStr:='CreateInOut';
carlos@0
   704
          SELECT COALESCE(C_DOCTYPE_REVERSED_ID, C_DOCTYPE_ID)
carlos@0
   705
          INTO v_DoctypeReversed_ID
carlos@0
   706
          FROM C_DOCTYPE
carlos@0
   707
          WHERE C_DOCTYPE_ID=Cur_InOut.C_DocType_ID;
carlos@0
   708
          Ad_Sequence_Next('M_InOut', Cur_InOut.M_InOut_ID, v_RInOut_ID) ; -- Get RInOut_ID
carlos@0
   709
          Ad_Sequence_Doctype(v_DoctypeReversed_ID, Cur_InOut.M_InOut_ID, 'Y', v_RDocumentNo) ; -- Get RDocumentNo
carlos@0
   710
          IF(v_RDocumentNo IS NULL) THEN
carlos@0
   711
            AD_Sequence_Doc('DocumentNo_M_InOut', Cur_InOut.AD_Client_ID, 'Y', v_RDocumentNo) ;
carlos@0
   712
          END IF;
carlos@0
   713
          -- Indicate that it is invoiced (i.e. not printed on invoices)
carlos@0
   714
          v_ResultStr:='SetInvoiced';
harikrishnan@7227
   715
          UPDATE M_INOUTLINE  SET IsInvoiced='Y',Updated=now(),UpdatedBy=v_User  WHERE M_InOut_ID=Cur_InOut.M_InOut_ID;
carlos@0
   716
          --
carlos@0
   717
          DBMS_OUTPUT.PUT_LINE('Reverse InOut_ID=' || v_RInOut_ID || ' DocumentNo=' || v_RDocumentNo) ;
carlos@0
   718
          v_ResultStr:='InsertInOut Reverse ' || v_RInOut_ID;
carlos@0
   719
          INSERT
carlos@0
   720
          INTO M_INOUT
carlos@0
   721
            (
carlos@0
   722
              M_InOut_ID, C_Order_ID, IsSOTrx, AD_Client_ID,
carlos@0
   723
              AD_Org_ID, IsActive, Created, CreatedBy,
carlos@0
   724
              Updated, UpdatedBy, DocumentNo, C_DocType_ID,
carlos@0
   725
              Description, IsPrinted, MovementType, MovementDate,
carlos@0
   726
              DateAcct, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID,
carlos@0
   727
              M_Warehouse_ID, POReference, DateOrdered, DeliveryRule,
carlos@0
   728
              FreightCostRule, FreightAmt, C_Project_ID, C_Activity_ID,
carlos@0
   729
              C_Campaign_ID, AD_OrgTrx_ID, User1_ID, User2_ID,
eduardo@18857
   730
              C_Costcenter_ID, A_Asset_ID,
carlos@0
   731
              DeliveryViaRule, M_Shipper_ID, C_Charge_ID, ChargeAmt,
carlos@0
   732
              PriorityRule, DocStatus, DocAction, Processing,
carlos@0
   733
              Processed, ISLOGISTIC, salesrep_id
carlos@0
   734
            )
carlos@0
   735
            VALUES
carlos@0
   736
            (
carlos@0
   737
              v_RInOut_ID, Cur_InOut.C_Order_ID, Cur_InOut.IsSOTrx, Cur_InOut.AD_Client_ID,
harikrishnan@7227
   738
              Cur_InOut.AD_Org_ID, 'Y', now(), v_User,
harikrishnan@7227
   739
              now(), v_User, v_RDocumentNo, v_DoctypeReversed_ID,
unai@15382
   740
               '(*R*: ' || Cur_InOut.DocumentNo || ') ' || COALESCE(TO_CHAR(Cur_InOut.Description), ''), 'N', Cur_InOut.MovementType, Cur_InOut.MovementDate,
carlos@0
   741
              Cur_InOut.DateAcct, Cur_InOut.C_BPartner_ID, Cur_InOut.C_BPartner_Location_ID, Cur_InOut.AD_User_ID,
carlos@0
   742
              Cur_InOut.M_Warehouse_ID, Cur_InOut.POReference, Cur_InOut.DateOrdered, Cur_InOut.DeliveryRule,
carlos@0
   743
              Cur_InOut.FreightCostRule, Cur_InOut.FreightAmt * -1, Cur_InOut.C_Project_ID, Cur_InOut.C_Activity_ID,
carlos@0
   744
              Cur_InOut.C_Campaign_ID, Cur_InOut.AD_OrgTrx_ID, Cur_InOut.User1_ID, Cur_InOut.User2_ID,
eduardo@18857
   745
              Cur_InOut.C_Costcenter_ID, Cur_InOut.A_Asset_ID,
carlos@0
   746
              Cur_InOut.DeliveryViaRule, Cur_InOut.M_Shipper_ID, Cur_InOut.C_Charge_ID, Cur_InOut.ChargeAmt * -1,
carlos@0
   747
              Cur_InOut.PriorityRule, 'DR', 'CO', 'N',
carlos@0
   748
               'N', Cur_InOut.islogistic, Cur_InOut.salesrep_id
carlos@0
   749
            )
carlos@0
   750
            ;
carlos@0
   751
          v_ResultStr:='InsertInOutLine';
carlos@0
   752
          FOR Cur_InOutLine IN
carlos@0
   753
            (SELECT *
carlos@0
   754
            FROM M_INOUTLINE
carlos@0
   755
            WHERE M_InOut_ID=Cur_InOut.M_InOut_ID
carlos@0
   756
              AND IsActive='Y'  FOR UPDATE
carlos@0
   757
            )
carlos@0
   758
          LOOP
carlos@0
   759
            -- Create InOut Line
carlos@0
   760
            Ad_Sequence_Next('M_InOutLine', Cur_InOut.M_InOut_ID, v_NextNo) ;
carlos@0
   761
            v_ResultStr:='CreateInOutLine';
carlos@0
   762
            INSERT
carlos@0
   763
            INTO M_INOUTLINE
carlos@0
   764
              (
carlos@0
   765
                M_InOutLine_ID, Line, M_InOut_ID, C_OrderLine_ID,
carlos@0
   766
                AD_Client_ID, AD_Org_ID, IsActive, Created,
carlos@0
   767
                CreatedBy, Updated, UpdatedBy, M_Product_ID,
carlos@0
   768
                M_AttributeSetInstance_ID, C_UOM_ID, M_Locator_ID, MovementQty,
asier@799
   769
                Description, IsInvoiced,  --MODIFIED BY F.IRIAZABAL
gorkaion@15487
   770
                QuantityOrder, M_Product_UOM_ID, IsDescription,
eduardo@18857
   771
                canceled_inoutline_id, A_Asset_ID, C_Project_ID, C_BPartner_ID,
eduardo@18857
   772
                User1_ID, User2_ID, C_CostCenter_ID
carlos@0
   773
              )
carlos@0
   774
              VALUES
carlos@0
   775
              (
carlos@0
   776
                v_NextNo, Cur_InOutLine.Line, v_RInOut_ID, Cur_InOutLine.C_OrderLine_ID,
carlos@0
   777
                Cur_InOut.AD_Client_ID, Cur_InOut.AD_Org_ID, 'Y', now(),
harikrishnan@7227
   778
                v_User, now(), v_User, Cur_InOutLine.M_Product_ID,
carlos@0
   779
                Cur_InOutLine.M_AttributeSetInstance_ID, Cur_InOutLine.C_UOM_ID, Cur_InOutLine.M_Locator_ID, Cur_InOutLine.MovementQty * -1,
unai@15382
   780
                 '*R*: ' || COALESCE(TO_CHAR(Cur_InOutLine.Description), ''), Cur_InOutLine.IsInvoiced, --MODIFIED BY F.IRIAZABAL
gorkaion@15487
   781
                Cur_InOutLine.QuantityOrder * -1, Cur_InOutLine.M_PRODUCT_UOM_ID, Cur_InOutLine.IsDescription,
eduardo@18857
   782
                Cur_InOutLine.M_InOutLine_ID, Cur_InOutLine.A_Asset_ID, Cur_InOutLine.C_Project_ID, Cur_InOutLine.C_BPartner_ID,
eduardo@18857
   783
                Cur_InOutLine.User1_ID, Cur_InOutLine.User2_ID, Cur_InOutLine.C_CostCenter_ID
carlos@0
   784
              )
carlos@0
   785
              ;
david@7235
   786
            INSERT INTO M_MATCHINV
david@7235
   787
              (M_MATCHINV_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY,
david@7235
   788
              M_INOUTLINE_ID, C_INVOICELINE_ID, M_PRODUCT_ID, DATETRX, QTY, PROCESSING, PROCESSED, POSTED)
david@7235
   789
            SELECT
david@7235
   790
              GET_UUID(), MI.AD_CLIENT_ID, MI.AD_ORG_ID, MI.ISACTIVE, now(), '0', now(), '0',
david@7240
   791
              v_NextNo, MI.C_INVOICELINE_ID, MI.M_PRODUCT_ID, MI.DATETRX, -MI.QTY, 'N', 'Y', 'N'
david@7235
   792
            FROM M_MATCHINV MI
david@7235
   793
            WHERE MI.M_INOUTLINE_ID = Cur_InOutLine.M_InOutLine_ID;
carlos@0
   794
          END LOOP;
carlos@0
   795
          -- Close Order
carlos@0
   796
          v_ResultStr:='CloseInOut';
carlos@0
   797
          UPDATE M_INOUT
carlos@0
   798
            SET Description=COALESCE(TO_CHAR(Description), '') || ' (*R*=' || v_RDocumentNo || ')',
carlos@0
   799
            Processed='Y',
ainhoa@2454
   800
            DocStatus='VO', -- it IS reversed
carlos@0
   801
            DocAction='--',
carlos@0
   802
            Updated=now(),
carlos@0
   803
            UpdatedBy=v_User
carlos@0
   804
          WHERE M_INOUT.M_INOUT_ID=Cur_InOut.M_INOUT_ID;
unai@15382
   805
unai@15382
   806
	   FOR Cur_InOutLine IN
unai@15382
   807
            (SELECT *
unai@15382
   808
            FROM M_INOUTLINE
unai@15382
   809
            WHERE M_InOut_ID=Cur_InOut.M_InOut_ID
unai@15382
   810
              AND IsActive='Y'  FOR UPDATE
unai@15382
   811
            )
unai@15382
   812
	  LOOP
unai@15382
   813
            UPDATE M_INOUTLINE
unai@15382
   814
              SET Description=COALESCE(TO_CHAR(Cur_InOutLine.Description), '') || ' : *R*',
unai@15382
   815
              Updated=now(),
unai@15382
   816
              UpdatedBy=v_User
unai@15382
   817
            WHERE M_INOUTLINE.M_INOUTLINE_ID=Cur_InOutLine.M_INOUTLINE_ID;
unai@15382
   818
          END LOOP;
unai@15382
   819
          
carlos@0
   820
          -- Post Reversal
carlos@0
   821
          v_ResultStr:='PostReversal';
mikel@19131
   822
          -- Update reversal goods dates
mikel@19131
   823
          IF (v_voidmovementdate IS NOT NULL) THEN
mikel@19131
   824
            UPDATE M_INOUT SET MovementDate = v_voidmovementdate WHERE M_INOUT_ID = v_RInOut_ID;
mikel@19131
   825
          END IF;
mikel@19131
   826
          IF (v_voiddate_acct IS NOT NULL) THEN
mikel@19131
   827
            UPDATE M_INOUT SET DateAcct = v_voiddate_acct WHERE M_INOUT_ID = v_RInOut_ID;
mikel@19131
   828
          END IF;
antonio@735
   829
          M_INOUT_POST(NULL, v_RInOut_ID) ;
carlos@0
   830
          -- Indicate as Reversal Transaction
carlos@0
   831
          v_ResultStr:='IndicateReversal';
carlos@0
   832
          UPDATE M_INOUT
carlos@0
   833
            SET Updated=now(),
carlos@0
   834
            UpdatedBy=v_User,
ainhoa@2454
   835
            DocStatus='VO' -- the reversal transaction
carlos@0
   836
          WHERE M_InOut_ID=v_RInOut_ID;
carlos@0
   837
        END IF; -- ReverseCorrection
adrianromero@7702
   838
adrianromero@7702
   839
        --M_Inout_Post - Finish_Process Extension Point
adrianromero@7702
   840
        --Extension point at the end of the M_Inout_Post. It has 5 available parameters Record_ID, DocAction, User, Message and Result
adrianromero@7702
   841
        SELECT count(*) INTO v_count
adrianromero@7702
   842
        FROM DUAL
adrianromero@7702
   843
        where exists (select 1 from ad_ep_procedures where ad_extension_points_id = '5A7C6972321E42C2A5A8E9D6D73E6A7C');
adrianromero@7702
   844
        IF (v_count=1) THEN
adrianromero@7702
   845
          DECLARE
adrianromero@7702
   846
            v_ep_instance VARCHAR2(32);
adrianromero@7702
   847
            v_extension_point_id VARCHAR2(32) := '5A7C6972321E42C2A5A8E9D6D73E6A7C';
adrianromero@7702
   848
          BEGIN
adrianromero@7702
   849
            v_ep_instance := get_uuid();
adrianromero@7702
   850
            AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'Record_ID',
adrianromero@7702
   851
              v_record_id, NULL, NULL, NULL, NULL, NULL, NULL);
adrianromero@7702
   852
            AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'DocAction',
adrianromero@7702
   853
              Cur_InOut.DocAction, NULL, NULL, NULL, NULL, NULL, NULL);
adrianromero@7702
   854
            AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'User',
adrianromero@7702
   855
              v_User, NULL, NULL, NULL, NULL, NULL, NULL);
adrianromero@7702
   856
            AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'Message',
adrianromero@7702
   857
              NULL, NULL, NULL, NULL, NULL, NULL, v_Message);
adrianromero@7702
   858
            AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'Result',
adrianromero@7702
   859
              NULL, NULL, v_Result, NULL, NULL, NULL, NULL);
adrianromero@7702
   860
            AD_EXTENSION_POINT_HANDLER(v_ep_instance, v_extension_point_id);
adrianromero@7702
   861
            SELECT p_number INTO v_Result
adrianromero@7702
   862
            FROM ad_ep_instance_para
adrianromero@7702
   863
            WHERE ad_ep_instance_id = v_ep_instance
adrianromero@7702
   864
              AND parametername LIKE 'Result';
adrianromero@7702
   865
            SELECT p_text INTO v_Message
adrianromero@7702
   866
            FROM ad_ep_instance_para
adrianromero@7702
   867
            WHERE ad_ep_instance_id = v_ep_instance
adrianromero@7702
   868
              AND parametername LIKE 'Message';
adrianromero@7702
   869
adrianromero@7702
   870
            DELETE FROM ad_ep_instance_para
adrianromero@7702
   871
            WHERE ad_ep_instance_id = v_ep_instance;
adrianromero@7702
   872
          END;
adrianromero@7702
   873
        END IF;
adrianromero@7702
   874
carlos@0
   875
      END LOOP; -- InOut Header
carlos@0
   876
      /**
carlos@0
   877
      * Transaction End
carlos@0
   878
      */
carlos@0
   879
      v_ResultStr:='Fini';
carlos@0
   880
    END IF; --FINISH_PROCESS
gorkaion@239
   881
    --<<FINISH_PROCESS>>
carlos@0
   882
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   883
      --  Update AD_PInstance
carlos@0
   884
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
carlos@0
   885
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
carlos@0
   886
    ELSE
gorkaion@239
   887
      DBMS_OUTPUT.PUT_LINE('--<<M_InOut_Post finished>>') ;
carlos@0
   888
    END IF;
carlos@0
   889
    RETURN;
carlos@0
   890
  END; --BODY
carlos@0
   891
EXCEPTION
carlos@0
   892
WHEN OTHERS THEN
carlos@0
   893
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   894
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   895
  IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   896
    ROLLBACK;
carlos@0
   897
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   898
  ELSE
carlos@0
   899
    RAISE;
carlos@0
   900
  END IF;
carlos@0
   901
  RETURN;
antonio@735
   902
END M_INOUT_POST
juanpablo@3490
   903
]]></body>
juanpablo@3490
   904
    </function>
juanpablo@3490
   905
  </database>