src-db/database/model/functions/M_INOUT_POST.xml
author Gorka Ion Damián <gorkaion.damian@openbravo.com>
Thu, 05 Nov 2009 10:18:37 +0100
changeset 5377 d65b8c735066
parent 4923 7185146af9f0
child 5506 0e8f7295dc15
permissions -rw-r--r--
Fixed issue 10845.Added new restrictions to sales documents.
-Not possible to modify a draft sales order line with delivered or invoiced
qty.
-Not possible to void a shipment with invoiced qty for a sales order with
invoice rule after delivery.
-Not possible to complete an sales invoice if new invoiced qty will be higher
than ordered qty.
-Not possible to have a sales order line with invoiced qty higher than ordered
qty.
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.
carlos@0
    24
  * Contributor(s): Openbravo SL
asier@2586
    25
  * Contributions are Copyright (C) 2001-2009 Openbravo, S.L.
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):='';
juanpablo@1605
    48
  v_Record_ID VARCHAR2(32);
juanpablo@1605
    49
  v_User VARCHAR2(32);
asier@1929
    50
  v_is_included NUMBER:=0;
asier@1929
    51
  v_DocType_ID VARCHAR2(32);
asier@1929
    52
  v_available_period NUMBER:=0;
asier@1929
    53
  v_is_ready AD_Org.IsReady%TYPE;
asier@1929
    54
  v_is_tr_allow AD_OrgType.IsTransactionsAllowed%TYPE;
asier@1929
    55
  v_DateAcct DATE;
victor@3065
    56
  v_isacctle AD_OrgType.IsAcctLegalEntity%TYPE;
victor@3065
    57
  v_org_bule_id AD_Org.AD_Org_ID%TYPE;
carlos@0
    58
  -- Parameter
carlos@0
    59
  TYPE RECORD IS REF CURSOR;
carlos@0
    60
    Cur_Parameter RECORD;
carlos@0
    61
    --
carlos@0
    62
    Cur_InOut RECORD;
carlos@0
    63
    Cur_InOutLine RECORD;
gorkaion@3170
    64
    Cur_Order RECORD;
carlos@0
    65
    --
carlos@0
    66
    v_Result NUMBER:=1;
juanpablo@1605
    67
    v_AD_Org_ID VARCHAR2(32);
juanpablo@1605
    68
    v_AD_Client_ID VARCHAR2(32);
juanpablo@1605
    69
    v_NextNo VARCHAR2(32);
carlos@0
    70
    v_Qty NUMBER;
carlos@0
    71
    v_QtyPO NUMBER;
carlos@0
    72
    v_QtySO NUMBER;
carlos@0
    73
    v_QuantityOrder NUMBER;
carlos@0
    74
    v_QuantityOrderPO NUMBER;
carlos@0
    75
    v_QuantityOrderSO NUMBER;
carlos@0
    76
    v_RDocumentNo VARCHAR2(40) ;
juanpablo@1605
    77
    v_RInOut_ID VARCHAR2(32);
carlos@0
    78
    v_IsStocked NUMBER;
juanpablo@1605
    79
    v_DoctypeReversed_ID VARCHAR2(32);
carlos@0
    80
    --MODIFIED BY F.IRIAZABAL
carlos@0
    81
    v_QtyOrder NUMBER;
carlos@0
    82
    v_ProductUOM NUMBER;
asier@2586
    83
    v_BreakDown VARCHAR2(60) ;
carlos@0
    84
    v_ActualQty NUMBER;
carlos@0
    85
    v_QtyAux NUMBER;
carlos@0
    86
    v_Count NUMBER:=0;
carlos@0
    87
    v_Line VARCHAR2(10) ;
gorkaion@3170
    88
    v_OrderID_old VARCHAR2(32);
carlos@0
    89
    FINISH_PROCESS BOOLEAN:=false;
carlos@0
    90
  BEGIN
carlos@0
    91
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
    92
      --  Update AD_PInstance
carlos@0
    93
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
    94
      v_ResultStr:='PInstanceNotFound';
carlos@0
    95
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
    96
      -- Get Parameters
carlos@0
    97
      v_ResultStr:='ReadingParameters';
carlos@0
    98
      FOR Cur_Parameter IN
carlos@0
    99
        (SELECT i.Record_ID,
carlos@0
   100
          i.AD_User_ID,
carlos@0
   101
          p.ParameterName,
carlos@0
   102
          p.P_String,
carlos@0
   103
          p.P_Number,
carlos@0
   104
          p.P_Date
carlos@0
   105
        FROM AD_PInstance i
carlos@0
   106
        LEFT JOIN AD_PInstance_Para p
carlos@0
   107
          ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
   108
        WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
   109
        ORDER BY p.SeqNo
carlos@0
   110
        )
carlos@0
   111
      LOOP
carlos@0
   112
        v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
   113
        v_User:=Cur_Parameter.AD_User_ID;
carlos@0
   114
      END LOOP; -- Get Parameter
carlos@0
   115
      DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
carlos@0
   116
    ELSE
gorkaion@239
   117
      DBMS_OUTPUT.PUT_LINE('--<<M_InOut_Post>>') ;
carlos@0
   118
      v_Record_ID:=p_InOut_ID;
carlos@0
   119
    END IF;
carlos@0
   120
  BEGIN --BODY
asier@1929
   121
  	SELECT AD_Client_ID, AD_Org_ID, CreatedBy, C_DocType_ID, DateAcct
asier@1929
   122
      INTO v_AD_Client_ID, v_AD_Org_ID, v_User, v_DocType_ID, v_DateAcct
carlos@0
   123
      FROM M_InOut
carlos@0
   124
      WHERE M_InOut_ID=v_Record_ID;
carlos@0
   125
    SELECT count(*)
carlos@0
   126
    INTO v_Count
carlos@0
   127
    FROM AD_CLIENTINFO
carlos@0
   128
    WHERE AD_CLIENT_ID=v_AD_Client_ID
carlos@0
   129
      AND CHECKINOUTORG='Y';
gorkaion@239
   130
    IF v_Count>0 THEN
carlos@0
   131
      v_ResultStr:='CheckingRestrictions - M_INOUT ORG IS IN C_BPARTNER ORG TREE';
carlos@0
   132
      SELECT count(*)
carlos@0
   133
      INTO v_Count
carlos@0
   134
      FROM M_InOut m,
carlos@0
   135
        C_BPartner bp
carlos@0
   136
      WHERE m.M_InOut_ID=v_Record_ID
carlos@0
   137
        AND m.C_BPARTNER_ID=bp.C_BPARTNER_ID
carlos@0
   138
        AND AD_IsOrgIncluded(m.AD_ORG_ID, bp.AD_ORG_ID, bp.AD_CLIENT_ID)=-1;
gorkaion@239
   139
      IF v_Count>0 THEN
carlos@0
   140
        RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgBpartnerInout@') ;
carlos@0
   141
      END IF;
carlos@0
   142
    END IF;
carlos@0
   143
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   144
      v_ResultStr:='CheckingRestrictions';
carlos@0
   145
      SELECT COUNT(*)
carlos@0
   146
   INTO v_Count
carlos@0
   147
   FROM C_DocType,
carlos@0
   148
        M_InOut M
carlos@0
   149
   WHERE M_Inout_ID = v_Record_ID
carlos@0
   150
     AND C_DocType.DocBaseType IN ('MMR', 'MMS')
carlos@0
   151
    AND C_DocType.IsSOTrx=M.IsSOTrx
gorkaion@239
   152
    AND AD_ISORGINCLUDED(m.AD_Org_ID,C_DocType.AD_Org_ID, m.AD_Client_ID) <> -1
carlos@0
   153
     AND M.C_DOCTYPE_ID=C_DocType.C_DOCTYPE_ID;
carlos@0
   154
      IF v_Count=0 THEN
carlos@0
   155
        RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgDoctypeShipment@') ;
carlos@0
   156
      END IF;
carlos@0
   157
      SELECT COUNT(*), MAX(M.line)
carlos@0
   158
      INTO v_Count, v_line
carlos@0
   159
      FROM M_InOutLine M,
carlos@0
   160
        M_Product P
carlos@0
   161
      WHERE M.M_PRODUCT_ID=P.M_PRODUCT_ID
carlos@0
   162
        AND P.M_ATTRIBUTESET_ID IS NOT NULL
rafael@4923
   163
        AND P.M_ATTRIBUTESETINSTANCE_ID IS NULL
juanpablo@1605
   164
        AND COALESCE(M.M_ATTRIBUTESETINSTANCE_ID, '0') = '0'
carlos@0
   165
        AND M.M_INOUT_ID=v_Record_ID;
gorkaion@239
   166
      IF v_Count<>0 THEN
gorkaion@1261
   167
        RAISE_APPLICATION_ERROR(-20000, '@Inline@ '||v_line||' '||'@productWithoutAttributeSet@') ;
carlos@0
   168
      END IF;
carlos@0
   169
      SELECT COUNT(*), MAX(M.line)
carlos@0
   170
      INTO v_Count, v_Line
carlos@0
   171
      FROM M_InOut I,
carlos@0
   172
        M_InOutLine M,
carlos@0
   173
        M_AttributeSetInstance P
carlos@0
   174
      WHERE I.M_InOut_ID=M.M_InOut_ID
carlos@0
   175
        AND M.M_AttributeSetInstance_ID=P.M_AttributeSetInstance_ID
carlos@0
   176
        AND P.ISLOCKED='Y'
carlos@0
   177
        AND I.ISSOTRX='Y'
carlos@0
   178
        AND I.M_INOUT_ID=v_Record_ID;
gorkaion@239
   179
      IF v_Count<>0 THEN
carlos@0
   180
        RAISE_APPLICATION_ERROR(-20000, '@Inline@'||v_line||' '||'@lockedProduct@') ;
carlos@0
   181
      END IF;
artem@3467
   182
	  -- check inout line instance location
artem@3467
   183
      SELECT COUNT(*), MAX(M.line)
artem@3467
   184
      INTO v_Count, v_Line
gorkaion@3897
   185
      FROM M_InOutLine M,
gorkaion@3897
   186
        M_Product P
gorkaion@3897
   187
      WHERE M.M_InOut_ID=v_Record_ID
gorkaion@3897
   188
		    AND M.M_Locator_ID IS NULL
gorkaion@3897
   189
        AND p.m_product_id = m.m_product_id
gorkaion@3897
   190
        AND p.isstocked = 'Y'
gorkaion@3897
   191
        AND p.producttype = 'I';
artem@3467
   192
      IF v_Count <> 0 THEN
artem@3467
   193
        RAISE_APPLICATION_ERROR(-20000, '@Inline@'||v_line||' '||'@InoutLineWithoutLocator@') ;
artem@3467
   194
      END IF;	  
artem@3467
   195
	  
carlos@0
   196
      -- Process Shipments
carlos@0
   197
carlos@511
   198
   -- Set org lines like the header
carlos@511
   199
     UPDATE M_INOUTLINE
carlos@0
   200
      SET AD_ORG_ID = (SELECT AD_ORG_ID FROM M_INOUT WHERE M_INOUT_ID = v_Record_ID)
carlos@0
   201
    WHERE M_INOUT_ID = v_Record_ID;
asier@1929
   202
    
asier@1929
   203
    -- Check the header belongs to a organization where transactions are posible and ready to use
asier@1929
   204
    SELECT AD_Org.IsReady, Ad_OrgType.IsTransactionsAllowed
asier@1929
   205
    INTO v_is_ready, v_is_tr_allow
asier@1929
   206
    FROM M_INOUT, AD_Org, AD_OrgType
asier@1929
   207
    WHERE AD_Org.AD_Org_ID=M_INOUT.AD_Org_ID
asier@1929
   208
    AND AD_Org.AD_OrgType_ID=AD_OrgType.AD_OrgType_ID
asier@1929
   209
    AND M_INOUT.M_INOUT_ID=v_Record_ID;
asier@1929
   210
    IF (v_is_ready='N') THEN
asier@1929
   211
      RAISE_APPLICATION_ERROR(-20000, '@OrgHeaderNotReady@');
asier@1929
   212
    END IF;
asier@1929
   213
    IF (v_is_tr_allow='N') THEN
asier@1929
   214
      RAISE_APPLICATION_ERROR(-20000, '@OrgHeaderNotTransAllowed@');
asier@1929
   215
    END IF;
asier@1929
   216
      
asier@1929
   217
    SELECT AD_ORG_CHK_DOCUMENTS('M_INOUT', 'M_INOUTLINE', v_Record_ID, 'M_INOUT_ID', 'M_INOUT_ID') INTO v_is_included FROM dual;
asier@1929
   218
    IF (v_is_included=-1) THEN
asier@1929
   219
      RAISE_APPLICATION_ERROR(-20000, '@LinesAndHeaderDifferentLEorBU@');
asier@1929
   220
    END IF;
asier@1929
   221
    
victor@3065
   222
    -- Check the period control is opened (only if it is legal entity with accounting)
victor@3065
   223
    -- Gets the BU or LE of the document
victor@3162
   224
    SELECT AD_GET_DOC_LE_BU('M_INOUT', v_Record_ID, 'M_INOUT_ID', 'LE')
victor@3065
   225
    INTO v_org_bule_id
victor@3065
   226
    FROM DUAL;
asier@1929
   227
    
victor@3065
   228
    SELECT AD_OrgType.IsAcctLegalEntity
victor@3065
   229
    INTO v_isacctle
victor@3065
   230
    FROM AD_OrgType, AD_Org
victor@3065
   231
    WHERE AD_Org.AD_OrgType_ID = AD_OrgType.AD_OrgType_ID
victor@3065
   232
    AND AD_Org.AD_Org_ID=v_org_bule_id;
victor@3065
   233
    
victor@3090
   234
    IF (v_isacctle='Y') THEN    
victor@3090
   235
      SELECT C_CHK_OPEN_PERIOD(v_AD_Org_ID, v_DateAcct, NULL, v_DocType_ID) 
victor@3065
   236
      INTO v_available_period
victor@3065
   237
      FROM DUAL;
victor@3065
   238
      
victor@3065
   239
      IF (v_available_period<>1) THEN
victor@3065
   240
        RAISE_APPLICATION_ERROR(-20000, '@PeriodNotAvailable@');
victor@3065
   241
      END IF;
victor@3065
   242
    END IF;  
carlos@0
   243
carlos@0
   244
      FOR Cur_InOut IN
carlos@0
   245
        (SELECT *
carlos@0
   246
        FROM M_INOUT
carlos@0
   247
        WHERE(M_InOut_ID=v_Record_ID
carlos@0
   248
          OR(v_Record_ID IS NULL
carlos@0
   249
          AND DocAction='CO'))
carlos@0
   250
          AND IsActive='Y'  FOR UPDATE
carlos@0
   251
        )
carlos@0
   252
      LOOP
carlos@0
   253
        DBMS_OUTPUT.PUT_LINE('Shipment_ID=' || Cur_InOut.M_InOut_ID || ', Doc=' || Cur_InOut.DocumentNo || ', Status=' || Cur_InOut.DocStatus || ', Action=' || Cur_InOut.DocAction) ;
carlos@0
   254
        v_ResultStr:='HeaderLoop';
carlos@0
   255
        /**
carlos@0
   256
        * Shipment not processed
carlos@0
   257
        */
carlos@0
   258
        IF(Cur_InOut.Processed='N' AND Cur_InOut.DocStatus='DR' AND Cur_InOut.DocAction='CO') THEN
carlos@0
   259
          -- For all active shipment lines
carlos@0
   260
          v_ResultStr:='HeaderLoop-1';
carlos@0
   261
          FOR Cur_InOutLine IN
carlos@0
   262
            (SELECT *
carlos@0
   263
            FROM M_INOUTLINE
carlos@0
   264
            WHERE M_InOut_ID=Cur_InOut.M_InOut_ID
carlos@0
   265
              AND IsActive='Y'  FOR UPDATE
carlos@0
   266
            )
carlos@0
   267
          LOOP
adrian@170
   268
            -- Incomming or Outgoing :1:2
carlos@0
   269
            v_Qty:=Cur_InOutLine.MovementQty;
carlos@0
   270
            v_QuantityOrder:=Cur_InOutLine.QuantityOrder;
carlos@0
   271
            IF(SUBSTR(Cur_InOut.MovementType, 2)='-') THEN
carlos@0
   272
              v_Qty:=- Cur_InOutLine.MovementQty;
carlos@0
   273
              v_QuantityOrder:=-Cur_InOutLine.QuantityOrder;
carlos@0
   274
            END IF;
carlos@0
   275
            IF(Cur_InOut.IsSOTrx='N') THEN
carlos@0
   276
              v_QtySO:=0;
carlos@0
   277
              v_QtyPO:=Cur_InOutLine.MovementQty;
carlos@0
   278
              v_QuantityOrderSO:=0;
carlos@0
   279
              v_QuantityOrderPO:=Cur_InOutLine.QuantityOrder;
carlos@0
   280
            ELSE
carlos@0
   281
              v_QtySO:=Cur_InOutLine.MovementQty;
carlos@0
   282
              v_QtyPO:=0;
carlos@0
   283
              v_QuantityOrderSO:=Cur_InOutLine.QuantityOrder;
carlos@0
   284
              v_QuantityOrderPO:=0;
carlos@0
   285
            END IF;
carlos@0
   286
            -- UOM Conversion
adrian@170
   287
            -- Is it a standard stocked product:3
carlos@0
   288
            SELECT COUNT(*)
carlos@0
   289
            INTO v_IsStocked
carlos@0
   290
            FROM M_PRODUCT
carlos@0
   291
            WHERE M_Product_ID=Cur_InOutLine.M_Product_ID
carlos@0
   292
              AND IsStocked='Y'
carlos@0
   293
              AND ProductType='I';
carlos@0
   294
            -- Create Transaction for stocked product
carlos@0
   295
            IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND v_IsStocked=1) THEN
carlos@0
   296
              v_ResultStr:='CreateTransaction';
carlos@0
   297
              Ad_Sequence_Next('M_Transaction', Cur_InOutLine.AD_Org_ID, v_NextNo) ;
carlos@0
   298
              INSERT
carlos@0
   299
              INTO M_TRANSACTION
carlos@0
   300
                (
carlos@0
   301
                  M_Transaction_ID, M_InOutLine_ID, AD_Client_ID, AD_Org_ID,
carlos@0
   302
                  IsActive, Created, CreatedBy, Updated,
carlos@0
   303
                  UpdatedBy, MovementType, M_Locator_ID, M_Product_ID,
carlos@0
   304
                  M_AttributeSetInstance_ID, MovementDate, MovementQty, M_Product_UOM_ID,
carlos@0
   305
                  QuantityOrder, C_UOM_ID
carlos@0
   306
                )
carlos@0
   307
                VALUES
carlos@0
   308
                (
carlos@0
   309
                  v_NextNo, Cur_InOutLine.M_InOutLine_ID, Cur_InOutLine.AD_Client_ID, Cur_InOutLine.AD_Org_ID,
carlos@0
   310
                   'Y', now(), Cur_InOutLine.UpdatedBy, now(),
carlos@0
   311
                  Cur_InOutLine.UpdatedBy, Cur_InOut.MovementType, Cur_InOutLine.M_Locator_ID, Cur_InOutLine.M_Product_ID,
juanpablo@1605
   312
                  COALESCE(Cur_InOutLine.M_AttributeSetInstance_ID, '0'), Cur_InOut.MovementDate, v_Qty, Cur_InOutLine.M_Product_UOM_ID,
carlos@0
   313
                  v_QuantityOrder, Cur_InOutLine.C_UOM_ID
carlos@0
   314
                )
carlos@0
   315
                ;
carlos@0
   316
            END IF;
carlos@0
   317
            -- Create Asset
carlos@0
   318
            IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND Cur_InOut.IsSOTrx='Y') THEN
antonio@735
   319
              A_ASSET_CREATE(NULL, Cur_InOutLine.M_InOutLine_ID) ;
carlos@0
   320
            END IF;
carlos@0
   321
            v_ResultStr:='UpdateOrderLine';
carlos@0
   322
            IF(Cur_InOutLine.C_OrderLine_ID IS NOT NULL) THEN
carlos@0
   323
              -- stocked product
carlos@0
   324
              IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND v_IsStocked=1) THEN
carlos@0
   325
                -- Update OrderLine (if C-, Qty is negative)
carlos@0
   326
                UPDATE C_ORDERLINE
carlos@0
   327
                  SET QtyReserved=QtyReserved - v_QtyPO - v_QtySO,
carlos@0
   328
                  QtyDelivered=QtyDelivered + v_QtySO,
carlos@0
   329
                  Updated=now()
carlos@0
   330
                WHERE C_OrderLine_ID=Cur_InOutLine.C_OrderLine_ID;
carlos@0
   331
                -- Products not stocked
carlos@0
   332
              ELSE
carlos@0
   333
                -- Update OrderLine (if C-, Qty is negative)
carlos@0
   334
                UPDATE C_ORDERLINE
carlos@0
   335
                  SET QtyDelivered=QtyDelivered + v_QtySO,
carlos@0
   336
                  Updated=now()
carlos@0
   337
                WHERE C_OrderLine_ID=Cur_InOutLine.C_OrderLine_ID;
carlos@0
   338
              END IF;
carlos@0
   339
            END IF;
carlos@0
   340
            IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND v_IsStocked=1) THEN
carlos@0
   341
              M_Check_Stock(Cur_InOutLine.M_Product_ID, v_AD_Client_ID, v_AD_Org_ID, v_Result, v_Message) ;
carlos@0
   342
              IF v_Result=0 THEN
carlos@0
   343
			    RAISE_APPLICATION_ERROR(-20000, v_Message||' '||'@line@'||' '||Cur_InOutLine.line) ;
carlos@0
   344
              END IF;
carlos@0
   345
            END IF;
carlos@0
   346
          END LOOP; -- For all InOut Lines
carlos@0
   347
          /*******************
carlos@0
   348
          * PO Matching
carlos@0
   349
          ******************/
carlos@0
   350
          IF(Cur_InOut.IsSOTrx='N') THEN
carlos@0
   351
            DECLARE
carlos@0
   352
              Cur_SLines RECORD;
carlos@0
   353
              Cur_ILines RECORD;
carlos@0
   354
              v_Qty NUMBER;
juanpablo@1605
   355
              v_MatchPO_ID VARCHAR2(32) ;
juanpablo@1605
   356
              v_MatchInv_ID VARCHAR2(32) ;
carlos@0
   357
            BEGIN
carlos@0
   358
              v_ResultStr:='MatchPO';
carlos@0
   359
              FOR Cur_SLines IN
carlos@0
   360
                (SELECT sl.AD_Client_ID,
carlos@0
   361
                  sl.AD_Org_ID,
carlos@0
   362
                  ol.C_OrderLine_ID,
carlos@0
   363
                  sl.M_InOutLine_ID,
carlos@0
   364
                  sl.M_Product_ID,
carlos@0
   365
                  sl.M_AttributeSetInstance_ID,
carlos@0
   366
                  sl.MovementQty,
carlos@0
   367
                  ol.QtyOrdered
carlos@0
   368
                FROM M_INOUTLINE sl,
carlos@0
   369
                  C_ORDERLINE ol
carlos@0
   370
                WHERE sl.C_OrderLine_ID=ol.C_OrderLine_ID
carlos@0
   371
                  AND sl.M_Product_ID=ol.M_Product_ID  --    AND   sl.M_AttributeSetInstance_ID=ol.M_AttributeSetInstance_ID
carlos@0
   372
                  AND sl.M_InOut_ID=Cur_InOut.M_InOut_ID
carlos@0
   373
                )
carlos@0
   374
              LOOP
carlos@0
   375
                Ad_Sequence_Next('M_MatchPO', Cur_SLines.AD_Org_ID, v_MatchPO_ID) ;
carlos@0
   376
                -- The min qty. Modified by Ismael Ciordia
carlos@0
   377
                v_Qty:=Cur_SLines.MovementQty;
gorkaion@239
   378
                --IF (ABS(Cur_SLines.MovementQty) > ABS(Cur_SLines.QtyOrdered)) THEN
carlos@0
   379
                -- v_Qty := Cur_SLines.QtyOrdered;
carlos@0
   380
                --END IF;
carlos@0
   381
                v_ResultStr:='InsertMatchPO ' || v_MatchPO_ID;
carlos@0
   382
                INSERT
carlos@0
   383
                INTO M_MATCHPO
carlos@0
   384
                  (
carlos@0
   385
                    M_MatchPO_ID, AD_Client_ID, AD_Org_ID, IsActive,
carlos@0
   386
                    Created, CreatedBy, Updated, UpdatedBy,
carlos@0
   387
                    M_InOutLine_ID, C_OrderLine_ID, M_Product_ID, DateTrx,
carlos@0
   388
                    Qty, Processing, Processed, Posted
carlos@0
   389
                  )
carlos@0
   390
                  VALUES
carlos@0
   391
                  (
carlos@0
   392
                    v_MatchPO_ID, Cur_SLines.AD_Client_ID, Cur_SLines.AD_Org_ID, 'Y',
juanpablo@1605
   393
                    now(), '0', now(), '0',
carlos@0
   394
                    Cur_SLines.M_InOutLine_ID, Cur_SLines.C_OrderLine_ID, Cur_SLines.M_Product_ID, now(),
carlos@0
   395
                    v_Qty, 'N', 'Y', 'N'
carlos@0
   396
                  )
carlos@0
   397
                  ;
carlos@0
   398
              END LOOP;
carlos@0
   399
              v_ResultStr:='MatchInv';
carlos@0
   400
              FOR Cur_ILines IN
carlos@0
   401
                (SELECT sl.AD_Client_ID,
carlos@0
   402
                  sl.AD_Org_ID,
carlos@0
   403
                  il.C_InvoiceLine_ID,
carlos@0
   404
                  sl.M_InOutLine_ID,
carlos@0
   405
                  sl.M_Product_ID,
carlos@0
   406
                  sl.M_AttributeSetInstance_ID,
carlos@0
   407
                  sl.MovementQty,
carlos@0
   408
                  il.QTYINVOICED
carlos@0
   409
                FROM M_INOUTLINE sl,
carlos@0
   410
                  C_INVOICELINE il
carlos@0
   411
                WHERE sl.M_InOutLine_ID=il.M_InOutLine_ID
carlos@0
   412
                  AND sl.M_InOut_ID=Cur_InOut.M_InOut_ID
carlos@0
   413
                )
carlos@0
   414
              LOOP
carlos@0
   415
                Ad_Sequence_Next('M_MatchInv', Cur_ILines.AD_Org_ID, v_MatchInv_ID) ;
carlos@0
   416
                -- The min qty. Modified by Ismael Ciordia
carlos@0
   417
                v_Qty:=Cur_ILines.MovementQty;
gorkaion@239
   418
                --IF (ABS(Cur_ILines.MovementQty) > ABS(Cur_ILines.QtyInvoiced)) THEN
carlos@0
   419
                -- v_Qty := Cur_ILines.QtyInvoiced;
carlos@0
   420
                --END IF;
carlos@0
   421
                v_ResultStr:='InsertMatchPO ' || v_MatchPO_ID;
carlos@0
   422
                INSERT
carlos@0
   423
                INTO M_MATCHINV
carlos@0
   424
                  (
carlos@0
   425
                    M_MATCHINV_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   426
                    CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   427
                    M_INOUTLINE_ID, C_INVOICELINE_ID, M_PRODUCT_ID, DATETRX,
carlos@0
   428
                    QTY, PROCESSING, PROCESSED, POSTED
carlos@0
   429
                  )
carlos@0
   430
                  VALUES
carlos@0
   431
                  (
carlos@0
   432
                    v_MatchInv_ID, Cur_ILines.AD_Client_ID, Cur_ILines.AD_Org_ID, 'Y',
juanpablo@1605
   433
                    now(), '0', now(), '0',
carlos@0
   434
                    Cur_ILines.M_InOutLine_ID, Cur_ILines.C_InvoiceLine_ID, Cur_ILines.M_Product_ID, now(),
carlos@0
   435
                    v_Qty, 'N', 'Y', 'N'
carlos@0
   436
                  )
carlos@0
   437
                  ;
carlos@0
   438
              END LOOP;
carlos@0
   439
            END;
gorkaion@3170
   440
          ELSE
gorkaion@3170
   441
            v_ResultStr:='Check delivery rule for sales orders';
gorkaion@3170
   442
            v_Message:='';
gorkaion@3170
   443
            v_orderid_old:='0';
gorkaion@3170
   444
            FOR Cur_Order IN 
gorkaion@3170
   445
              (SELECT c_order.deliveryrule, m_inoutline.line, c_order.c_order_id,
gorkaion@3170
   446
                      c_order.documentno, c_orderline.line as orderline
gorkaion@3170
   447
               FROM M_InOutLine, C_Orderline, C_Order
gorkaion@3170
   448
               WHERE M_Inoutline.c_orderline_id = c_orderline.c_orderline_id
gorkaion@3170
   449
                 AND c_orderline.c_order_id = c_order.c_order_id
gorkaion@3170
   450
                 AND m_inoutline.m_inout_id = cur_inout.m_inout_id
gorkaion@3170
   451
                 AND ((c_order.deliveryrule = 'O'
gorkaion@3170
   452
                      AND EXISTS (SELECT 1 FROM C_OrderLine ol
gorkaion@3170
   453
                                  WHERE ol.C_Order_ID = C_order.c_order_id
gorkaion@3170
   454
                                    and ol.qtyordered > ol.qtydelivered ))
gorkaion@3170
   455
                      OR (c_order.deliveryrule = 'L' 
gorkaion@3170
   456
                          AND c_orderline.qtyordered > c_orderline.qtydelivered))
gorkaion@3170
   457
               ORDER BY c_order.c_order_id, c_orderline.line) LOOP
gorkaion@3170
   458
              --Order lines not completely delivered with delivery rule O or L
gorkaion@3170
   459
              IF (v_orderid_old <> cur_order.c_order_id OR cur_order.deliveryrule <> 'O' ) THEN
gorkaion@3170
   460
                v_Message := COALESCE(v_Message,'') || '@Shipment@' || ' ' || cur_inout.documentno;
gorkaion@3170
   461
                v_Message := v_Message || ' ' || '@line@' || ' ' || cur_order.line || ': ';
gorkaion@3170
   462
                v_Message := v_Message || '@SalesOrderDocumentno@' || cur_order.documentno;
gorkaion@3170
   463
                IF (cur_order.deliveryrule = 'O') THEN
gorkaion@3170
   464
                  v_Message := v_Message || ' ' || '@notCompleteDeliveryRuleOrder@' || '<br>';
gorkaion@3170
   465
                ELSE
gorkaion@3170
   466
                  v_Message := v_Message || ' ' || '@line@' || ' ' || cur_order.orderline;
gorkaion@3170
   467
                  v_Message := v_Message || ' ' || '@notCompleteDeliveryRuleLine@' || '<br>';
gorkaion@3170
   468
                END IF;
gorkaion@3170
   469
              END IF;
gorkaion@3170
   470
              v_orderid_old := cur_order.c_order_id;
gorkaion@3170
   471
            END LOOP;
rafael@3205
   472
            IF (v_Message IS NOT NULL AND v_Message <> '') THEN
gorkaion@3170
   473
              RAISE_APPLICATION_ERROR(-20000, v_message);
gorkaion@3170
   474
            END IF;
carlos@0
   475
          END IF;
carlos@0
   476
          -- Close Shipment
carlos@0
   477
          v_ResultStr:='CloseShipment';
carlos@0
   478
          UPDATE M_INOUT
carlos@0
   479
            SET Processed='Y',
carlos@0
   480
            DocStatus='CO',
carlos@0
   481
            DocAction='--',
carlos@0
   482
            Updated=now()
carlos@0
   483
          WHERE M_INOUT.M_INOUT_ID=Cur_InOut.M_INOUT_ID;
carlos@0
   484
          --
asier@1027
   485
          
asier@1027
   486
         
carlos@0
   487
          -- Not Processed + Complete --
carlos@0
   488
          /**
carlos@0
   489
          * Reverse Correction
carlos@0
   490
          */
carlos@0
   491
        ELSIF(Cur_InOut.DocStatus='CO' AND Cur_InOut.DocAction='RC') THEN
gorkaion@5377
   492
          --Check that there isn't any line with an invoice if the order's 
gorkaion@5377
   493
          --invoice rule is after delivery
gorkaion@5377
   494
          select count(*), max(line) into v_count, v_line
gorkaion@5377
   495
          from (
gorkaion@5377
   496
          SELECT m_inoutline.m_inoutline_id, m_inoutline.line
gorkaion@5377
   497
          from m_inoutline, c_order, c_orderline, c_invoiceline, m_inout, c_invoice
gorkaion@5377
   498
          where m_inoutline.c_orderline_id = c_orderline.c_orderline_id
gorkaion@5377
   499
            and c_orderline.c_order_id = c_order.c_order_id
gorkaion@5377
   500
            and c_orderline.c_orderline_id = c_invoiceline.c_orderline_id
gorkaion@5377
   501
            and m_inoutline.m_inout_id = m_inout.m_inout_id
gorkaion@5377
   502
            and c_invoiceline.c_invoice_id = c_invoice.c_invoice_id
gorkaion@5377
   503
            and m_inout.m_inout_id = Cur_InOut.m_inout_id
gorkaion@5377
   504
            and m_inout.issotrx = 'Y'
gorkaion@5377
   505
            and c_order.invoicerule in ('D', 'O', 'S')
gorkaion@5377
   506
            and c_invoice.processed='Y'
gorkaion@5377
   507
          group by m_inoutline.m_inoutline_id, m_inoutline.line
gorkaion@5377
   508
          having sum(c_invoiceline.qtyinvoiced) > 0
gorkaion@5377
   509
          ) a;
gorkaion@5377
   510
          IF (v_count > 0 ) THEN
gorkaion@5377
   511
            v_Message := '@InoutDocumentno@' || ': ' || Cur_InOut.DocumentNo || ' ' || '@line@' || ': ' || v_line || '. ';
gorkaion@5377
   512
            v_Message := v_Message || '@VoidShipmentInvoiced@';
gorkaion@5377
   513
            RAISE_APPLICATION_ERROR(-20000, v_Message);
gorkaion@5377
   514
          END IF;
carlos@0
   515
          v_ResultStr:='CreateInOut';
carlos@0
   516
          SELECT COALESCE(C_DOCTYPE_REVERSED_ID, C_DOCTYPE_ID)
carlos@0
   517
          INTO v_DoctypeReversed_ID
carlos@0
   518
          FROM C_DOCTYPE
carlos@0
   519
          WHERE C_DOCTYPE_ID=Cur_InOut.C_DocType_ID;
carlos@0
   520
          Ad_Sequence_Next('M_InOut', Cur_InOut.M_InOut_ID, v_RInOut_ID) ; -- Get RInOut_ID
carlos@0
   521
          Ad_Sequence_Doctype(v_DoctypeReversed_ID, Cur_InOut.M_InOut_ID, 'Y', v_RDocumentNo) ; -- Get RDocumentNo
carlos@0
   522
          IF(v_RDocumentNo IS NULL) THEN
carlos@0
   523
            AD_Sequence_Doc('DocumentNo_M_InOut', Cur_InOut.AD_Client_ID, 'Y', v_RDocumentNo) ;
carlos@0
   524
          END IF;
carlos@0
   525
          -- Indicate that it is invoiced (i.e. not printed on invoices)
carlos@0
   526
          v_ResultStr:='SetInvoiced';
carlos@0
   527
          UPDATE M_INOUTLINE  SET IsInvoiced='Y'  WHERE M_InOut_ID=Cur_InOut.M_InOut_ID;
carlos@0
   528
          --
carlos@0
   529
          DBMS_OUTPUT.PUT_LINE('Reverse InOut_ID=' || v_RInOut_ID || ' DocumentNo=' || v_RDocumentNo) ;
carlos@0
   530
          v_ResultStr:='InsertInOut Reverse ' || v_RInOut_ID;
carlos@0
   531
          INSERT
carlos@0
   532
          INTO M_INOUT
carlos@0
   533
            (
carlos@0
   534
              M_InOut_ID, C_Order_ID, IsSOTrx, AD_Client_ID,
carlos@0
   535
              AD_Org_ID, IsActive, Created, CreatedBy,
carlos@0
   536
              Updated, UpdatedBy, DocumentNo, C_DocType_ID,
carlos@0
   537
              Description, IsPrinted, MovementType, MovementDate,
carlos@0
   538
              DateAcct, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID,
carlos@0
   539
              M_Warehouse_ID, POReference, DateOrdered, DeliveryRule,
carlos@0
   540
              FreightCostRule, FreightAmt, C_Project_ID, C_Activity_ID,
carlos@0
   541
              C_Campaign_ID, AD_OrgTrx_ID, User1_ID, User2_ID,
carlos@0
   542
              DeliveryViaRule, M_Shipper_ID, C_Charge_ID, ChargeAmt,
carlos@0
   543
              PriorityRule, DocStatus, DocAction, Processing,
carlos@0
   544
              Processed, ISLOGISTIC, salesrep_id
carlos@0
   545
            )
carlos@0
   546
            VALUES
carlos@0
   547
            (
carlos@0
   548
              v_RInOut_ID, Cur_InOut.C_Order_ID, Cur_InOut.IsSOTrx, Cur_InOut.AD_Client_ID,
juanpablo@1605
   549
              Cur_InOut.AD_Org_ID, 'Y', now(), '0',
juanpablo@1605
   550
              now(), '0', v_RDocumentNo, v_DoctypeReversed_ID,
carlos@0
   551
               '(*R*: ' || Cur_InOut.DocumentNo || ') ' || Cur_InOut.Description, 'N', Cur_InOut.MovementType, Cur_InOut.MovementDate,
carlos@0
   552
              Cur_InOut.DateAcct, Cur_InOut.C_BPartner_ID, Cur_InOut.C_BPartner_Location_ID, Cur_InOut.AD_User_ID,
carlos@0
   553
              Cur_InOut.M_Warehouse_ID, Cur_InOut.POReference, Cur_InOut.DateOrdered, Cur_InOut.DeliveryRule,
carlos@0
   554
              Cur_InOut.FreightCostRule, Cur_InOut.FreightAmt * -1, Cur_InOut.C_Project_ID, Cur_InOut.C_Activity_ID,
carlos@0
   555
              Cur_InOut.C_Campaign_ID, Cur_InOut.AD_OrgTrx_ID, Cur_InOut.User1_ID, Cur_InOut.User2_ID,
carlos@0
   556
              Cur_InOut.DeliveryViaRule, Cur_InOut.M_Shipper_ID, Cur_InOut.C_Charge_ID, Cur_InOut.ChargeAmt * -1,
carlos@0
   557
              Cur_InOut.PriorityRule, 'DR', 'CO', 'N',
carlos@0
   558
               'N', Cur_InOut.islogistic, Cur_InOut.salesrep_id
carlos@0
   559
            )
carlos@0
   560
            ;
carlos@0
   561
          v_ResultStr:='InsertInOutLine';
carlos@0
   562
          FOR Cur_InOutLine IN
carlos@0
   563
            (SELECT *
carlos@0
   564
            FROM M_INOUTLINE
carlos@0
   565
            WHERE M_InOut_ID=Cur_InOut.M_InOut_ID
carlos@0
   566
              AND IsActive='Y'  FOR UPDATE
carlos@0
   567
            )
carlos@0
   568
          LOOP
carlos@0
   569
            -- Create InOut Line
carlos@0
   570
            Ad_Sequence_Next('M_InOutLine', Cur_InOut.M_InOut_ID, v_NextNo) ;
carlos@0
   571
            v_ResultStr:='CreateInOutLine';
carlos@0
   572
            INSERT
carlos@0
   573
            INTO M_INOUTLINE
carlos@0
   574
              (
carlos@0
   575
                M_InOutLine_ID, Line, M_InOut_ID, C_OrderLine_ID,
carlos@0
   576
                AD_Client_ID, AD_Org_ID, IsActive, Created,
carlos@0
   577
                CreatedBy, Updated, UpdatedBy, M_Product_ID,
carlos@0
   578
                M_AttributeSetInstance_ID, C_UOM_ID, M_Locator_ID, MovementQty,
asier@799
   579
                Description, IsInvoiced,  --MODIFIED BY F.IRIAZABAL
carlos@0
   580
                QuantityOrder, M_Product_UOM_ID
carlos@0
   581
              )
carlos@0
   582
              VALUES
carlos@0
   583
              (
carlos@0
   584
                v_NextNo, Cur_InOutLine.Line, v_RInOut_ID, Cur_InOutLine.C_OrderLine_ID,
carlos@0
   585
                Cur_InOut.AD_Client_ID, Cur_InOut.AD_Org_ID, 'Y', now(),
juanpablo@1605
   586
                '0', now(), '0', Cur_InOutLine.M_Product_ID,
carlos@0
   587
                Cur_InOutLine.M_AttributeSetInstance_ID, Cur_InOutLine.C_UOM_ID, Cur_InOutLine.M_Locator_ID, Cur_InOutLine.MovementQty * -1,
asier@799
   588
                 '*R*: ' || Cur_InOutLine.Description, Cur_InOutLine.IsInvoiced, --MODIFIED BY F.IRIAZABAL
carlos@0
   589
                Cur_InOutLine.QuantityOrder * -1, Cur_InOutLine.M_PRODUCT_UOM_ID
carlos@0
   590
              )
carlos@0
   591
              ;
carlos@0
   592
          END LOOP;
carlos@0
   593
          -- Close Order
carlos@0
   594
          v_ResultStr:='CloseInOut';
carlos@0
   595
          UPDATE M_INOUT
carlos@0
   596
            SET Description=COALESCE(TO_CHAR(Description), '') || ' (*R*=' || v_RDocumentNo || ')',
carlos@0
   597
            Processed='Y',
ainhoa@2454
   598
            DocStatus='VO', -- it IS reversed
carlos@0
   599
            DocAction='--',
carlos@0
   600
            Updated=now(),
carlos@0
   601
            UpdatedBy=v_User
carlos@0
   602
          WHERE M_INOUT.M_INOUT_ID=Cur_InOut.M_INOUT_ID;
asier@1027
   603
         
carlos@0
   604
          -- Post Reversal
carlos@0
   605
          v_ResultStr:='PostReversal';
antonio@735
   606
          M_INOUT_POST(NULL, v_RInOut_ID) ;
carlos@0
   607
          -- Indicate as Reversal Transaction
carlos@0
   608
          v_ResultStr:='IndicateReversal';
carlos@0
   609
          UPDATE M_INOUT
carlos@0
   610
            SET Updated=now(),
carlos@0
   611
            UpdatedBy=v_User,
ainhoa@2454
   612
            DocStatus='VO' -- the reversal transaction
carlos@0
   613
          WHERE M_InOut_ID=v_RInOut_ID;
carlos@0
   614
        END IF; -- ReverseCorrection
carlos@0
   615
      END LOOP; -- InOut Header
carlos@0
   616
      /**
carlos@0
   617
      * Transaction End
carlos@0
   618
      */
carlos@0
   619
      v_ResultStr:='Fini';
carlos@0
   620
    END IF; --FINISH_PROCESS
gorkaion@239
   621
    --<<FINISH_PROCESS>>
carlos@0
   622
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   623
      --  Update AD_PInstance
carlos@0
   624
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
carlos@0
   625
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
carlos@0
   626
    ELSE
gorkaion@239
   627
      DBMS_OUTPUT.PUT_LINE('--<<M_InOut_Post finished>>') ;
carlos@0
   628
    END IF;
carlos@0
   629
    RETURN;
carlos@0
   630
  END; --BODY
carlos@0
   631
EXCEPTION
carlos@0
   632
WHEN OTHERS THEN
carlos@0
   633
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   634
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   635
  IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   636
    ROLLBACK;
carlos@0
   637
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   638
  ELSE
carlos@0
   639
    RAISE;
carlos@0
   640
  END IF;
carlos@0
   641
  RETURN;
antonio@735
   642
END M_INOUT_POST
juanpablo@3490
   643
]]></body>
juanpablo@3490
   644
    </function>
juanpablo@3490
   645
  </database>