src-db/database/model/functions/M_INOUT_POST.xml
author Asier Lostalé <asier.lostale@openbravo.com>
Mon, 05 May 2008 06:59:24 +0000
changeset 799 fef2c5e2feb7
parent 785 8dba91261590
child 1027 2e9052d7de0e
permissions -rw-r--r--
Merged cleanup branch (r3931) with trunk
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION M_INOUT_POST">
adrian@94
     3
    <function name="M_INOUT_POST" type="NULL">
antonio@735
     4
      <parameter name="p_pinstance_id" type="NUMERIC" mode="in">
antonio@735
     5
        <default/>
antonio@735
     6
      </parameter>
antonio@735
     7
      <parameter name="p_inout_id" type="NUMERIC" mode="in">
antonio@735
     8
        <default/>
antonio@735
     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
juanpablo@785
    25
  * Contributions are Copyright (C) 2001-2008 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):='';
carlos@0
    48
  v_Record_ID NUMBER;
carlos@0
    49
  v_User NUMBER;
carlos@0
    50
  -- Parameter
carlos@0
    51
  TYPE RECORD IS REF CURSOR;
carlos@0
    52
    Cur_Parameter RECORD;
carlos@0
    53
    --
carlos@0
    54
    Cur_InOut RECORD;
carlos@0
    55
    Cur_InOutLine RECORD;
carlos@0
    56
    --
carlos@0
    57
    v_Result NUMBER:=1;
carlos@0
    58
    v_AD_Org_ID NUMBER;
carlos@0
    59
    v_AD_Client_ID NUMBER;
carlos@0
    60
    v_NextNo NUMBER;
carlos@0
    61
    v_Qty NUMBER;
carlos@0
    62
    v_QtyPO NUMBER;
carlos@0
    63
    v_QtySO NUMBER;
carlos@0
    64
    v_QuantityOrder NUMBER;
carlos@0
    65
    v_QuantityOrderPO NUMBER;
carlos@0
    66
    v_QuantityOrderSO NUMBER;
carlos@0
    67
    v_RDocumentNo VARCHAR2(40) ;
carlos@0
    68
    v_RInOut_ID NUMBER;
carlos@0
    69
    v_IsStocked NUMBER;
carlos@0
    70
    v_DoctypeReversed_ID NUMBER;
carlos@0
    71
    --MODIFIED BY F.IRIAZABAL
carlos@0
    72
    v_QtyOrder NUMBER;
carlos@0
    73
    v_ProductUOM NUMBER;
carlos@0
    74
    v_BreakDown CHAR(1) ;
carlos@0
    75
    v_ActualQty NUMBER;
carlos@0
    76
    v_QtyAux NUMBER;
carlos@0
    77
    v_Count NUMBER:=0;
carlos@0
    78
    v_Line VARCHAR2(10) ;
carlos@0
    79
    FINISH_PROCESS BOOLEAN:=false;
asier@799
    80
    v_DBA_ErrorLog_ID NUMBER;
carlos@0
    81
  BEGIN
carlos@0
    82
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
    83
      --  Update AD_PInstance
carlos@0
    84
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
    85
      v_ResultStr:='PInstanceNotFound';
carlos@0
    86
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
    87
      -- Get Parameters
carlos@0
    88
      v_ResultStr:='ReadingParameters';
carlos@0
    89
      FOR Cur_Parameter IN
carlos@0
    90
        (SELECT i.Record_ID,
carlos@0
    91
          i.AD_User_ID,
carlos@0
    92
          p.ParameterName,
carlos@0
    93
          p.P_String,
carlos@0
    94
          p.P_Number,
carlos@0
    95
          p.P_Date
carlos@0
    96
        FROM AD_PInstance i
carlos@0
    97
        LEFT JOIN AD_PInstance_Para p
carlos@0
    98
          ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
    99
        WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
   100
        ORDER BY p.SeqNo
carlos@0
   101
        )
carlos@0
   102
      LOOP
carlos@0
   103
        v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
   104
        v_User:=Cur_Parameter.AD_User_ID;
carlos@0
   105
      END LOOP; -- Get Parameter
carlos@0
   106
      DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
carlos@0
   107
    ELSE
gorkaion@239
   108
      DBMS_OUTPUT.PUT_LINE('--<<M_InOut_Post>>') ;
carlos@0
   109
      v_Record_ID:=p_InOut_ID;
carlos@0
   110
    END IF;
carlos@0
   111
  BEGIN --BODY
carlos@0
   112
  	SELECT AD_Client_ID, AD_Org_ID, CreatedBy
carlos@0
   113
      INTO v_AD_Client_ID, v_AD_Org_ID, v_User
carlos@0
   114
      FROM M_InOut
carlos@0
   115
      WHERE M_InOut_ID=v_Record_ID;
carlos@0
   116
    SELECT count(*)
carlos@0
   117
    INTO v_Count
carlos@0
   118
    FROM AD_CLIENTINFO
carlos@0
   119
    WHERE AD_CLIENT_ID=v_AD_Client_ID
carlos@0
   120
      AND CHECKINOUTORG='Y';
gorkaion@239
   121
    IF v_Count>0 THEN
carlos@0
   122
      v_ResultStr:='CheckingRestrictions - M_INOUT ORG IS IN C_BPARTNER ORG TREE';
carlos@0
   123
      SELECT count(*)
carlos@0
   124
      INTO v_Count
carlos@0
   125
      FROM M_InOut m,
carlos@0
   126
        C_BPartner bp
carlos@0
   127
      WHERE m.M_InOut_ID=v_Record_ID
carlos@0
   128
        AND m.C_BPARTNER_ID=bp.C_BPARTNER_ID
carlos@0
   129
        AND AD_IsOrgIncluded(m.AD_ORG_ID, bp.AD_ORG_ID, bp.AD_CLIENT_ID)=-1;
gorkaion@239
   130
      IF v_Count>0 THEN
carlos@0
   131
        RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgBpartnerInout@') ;
carlos@0
   132
      END IF;
carlos@0
   133
    END IF;
carlos@0
   134
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   135
      v_ResultStr:='CheckingRestrictions';
carlos@0
   136
      SELECT COUNT(*)
carlos@0
   137
   INTO v_Count
carlos@0
   138
   FROM C_DocType,
carlos@0
   139
        M_InOut M
carlos@0
   140
   WHERE M_Inout_ID = v_Record_ID
carlos@0
   141
     AND C_DocType.DocBaseType IN ('MMR', 'MMS')
carlos@0
   142
    AND C_DocType.IsSOTrx=M.IsSOTrx
gorkaion@239
   143
    AND AD_ISORGINCLUDED(m.AD_Org_ID,C_DocType.AD_Org_ID, m.AD_Client_ID) <> -1
carlos@0
   144
     AND M.C_DOCTYPE_ID=C_DocType.C_DOCTYPE_ID;
carlos@0
   145
      IF v_Count=0 THEN
carlos@0
   146
        RAISE_APPLICATION_ERROR(-20000, '@NotCorrectOrgDoctypeShipment@') ;
carlos@0
   147
      END IF;
carlos@0
   148
      SELECT COUNT(*), MAX(M.line)
carlos@0
   149
      INTO v_Count, v_line
carlos@0
   150
      FROM M_InOutLine M,
carlos@0
   151
        M_Product P
carlos@0
   152
      WHERE M.M_PRODUCT_ID=P.M_PRODUCT_ID
carlos@0
   153
        AND P.M_ATTRIBUTESET_ID IS NOT NULL
carlos@0
   154
        AND M.M_ATTRIBUTESETINSTANCE_ID IS NULL
carlos@0
   155
        AND M.M_INOUT_ID=v_Record_ID;
gorkaion@239
   156
      IF v_Count<>0 THEN
carlos@0
   157
        RAISE_APPLICATION_ERROR(-20000, '@Inline@'||v_line||' '||'@productWithoutAttributeSet@') ;
carlos@0
   158
      END IF;
carlos@0
   159
      SELECT COUNT(*), MAX(M.line)
carlos@0
   160
      INTO v_Count, v_Line
carlos@0
   161
      FROM M_InOut I,
carlos@0
   162
        M_InOutLine M,
carlos@0
   163
        M_AttributeSetInstance P
carlos@0
   164
      WHERE I.M_InOut_ID=M.M_InOut_ID
carlos@0
   165
        AND M.M_AttributeSetInstance_ID=P.M_AttributeSetInstance_ID
carlos@0
   166
        AND P.ISLOCKED='Y'
carlos@0
   167
        AND I.ISSOTRX='Y'
carlos@0
   168
        AND I.M_INOUT_ID=v_Record_ID;
gorkaion@239
   169
      IF v_Count<>0 THEN
carlos@0
   170
        RAISE_APPLICATION_ERROR(-20000, '@Inline@'||v_line||' '||'@lockedProduct@') ;
carlos@0
   171
      END IF;
carlos@0
   172
      -- Process Shipments
carlos@0
   173
carlos@511
   174
   -- Set org lines like the header
carlos@511
   175
     UPDATE M_INOUTLINE
carlos@0
   176
      SET AD_ORG_ID = (SELECT AD_ORG_ID FROM M_INOUT WHERE M_INOUT_ID = v_Record_ID)
carlos@0
   177
    WHERE M_INOUT_ID = v_Record_ID;
carlos@0
   178
carlos@0
   179
      FOR Cur_InOut IN
carlos@0
   180
        (SELECT *
carlos@0
   181
        FROM M_INOUT
carlos@0
   182
        WHERE(M_InOut_ID=v_Record_ID
carlos@0
   183
          OR(v_Record_ID IS NULL
carlos@0
   184
          AND DocAction='CO'))
carlos@0
   185
          AND IsActive='Y'  FOR UPDATE
carlos@0
   186
        )
carlos@0
   187
      LOOP
carlos@0
   188
        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
   189
        v_ResultStr:='HeaderLoop';
carlos@0
   190
        /**
carlos@0
   191
        * Shipment not processed
carlos@0
   192
        */
carlos@0
   193
        IF(Cur_InOut.Processed='N' AND Cur_InOut.DocStatus='DR' AND Cur_InOut.DocAction='CO') THEN
carlos@0
   194
          -- For all active shipment lines
carlos@0
   195
          v_ResultStr:='HeaderLoop-1';
carlos@0
   196
          FOR Cur_InOutLine IN
carlos@0
   197
            (SELECT *
carlos@0
   198
            FROM M_INOUTLINE
carlos@0
   199
            WHERE M_InOut_ID=Cur_InOut.M_InOut_ID
carlos@0
   200
              AND IsActive='Y'  FOR UPDATE
carlos@0
   201
            )
carlos@0
   202
          LOOP
adrian@170
   203
            -- Incomming or Outgoing :1:2
carlos@0
   204
            v_Qty:=Cur_InOutLine.MovementQty;
carlos@0
   205
            v_QuantityOrder:=Cur_InOutLine.QuantityOrder;
carlos@0
   206
            IF(SUBSTR(Cur_InOut.MovementType, 2)='-') THEN
carlos@0
   207
              v_Qty:=- Cur_InOutLine.MovementQty;
carlos@0
   208
              v_QuantityOrder:=-Cur_InOutLine.QuantityOrder;
carlos@0
   209
            END IF;
carlos@0
   210
            IF(Cur_InOut.IsSOTrx='N') THEN
carlos@0
   211
              v_QtySO:=0;
carlos@0
   212
              v_QtyPO:=Cur_InOutLine.MovementQty;
carlos@0
   213
              v_QuantityOrderSO:=0;
carlos@0
   214
              v_QuantityOrderPO:=Cur_InOutLine.QuantityOrder;
carlos@0
   215
            ELSE
carlos@0
   216
              v_QtySO:=Cur_InOutLine.MovementQty;
carlos@0
   217
              v_QtyPO:=0;
carlos@0
   218
              v_QuantityOrderSO:=Cur_InOutLine.QuantityOrder;
carlos@0
   219
              v_QuantityOrderPO:=0;
carlos@0
   220
            END IF;
carlos@0
   221
            -- UOM Conversion
adrian@170
   222
            -- Is it a standard stocked product:3
carlos@0
   223
            SELECT COUNT(*)
carlos@0
   224
            INTO v_IsStocked
carlos@0
   225
            FROM M_PRODUCT
carlos@0
   226
            WHERE M_Product_ID=Cur_InOutLine.M_Product_ID
carlos@0
   227
              AND IsStocked='Y'
carlos@0
   228
              AND ProductType='I';
carlos@0
   229
            -- Create Transaction for stocked product
carlos@0
   230
            IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND v_IsStocked=1) THEN
carlos@0
   231
              v_ResultStr:='CreateTransaction';
carlos@0
   232
              Ad_Sequence_Next('M_Transaction', Cur_InOutLine.AD_Org_ID, v_NextNo) ;
carlos@0
   233
              INSERT
carlos@0
   234
              INTO M_TRANSACTION
carlos@0
   235
                (
carlos@0
   236
                  M_Transaction_ID, M_InOutLine_ID, AD_Client_ID, AD_Org_ID,
carlos@0
   237
                  IsActive, Created, CreatedBy, Updated,
carlos@0
   238
                  UpdatedBy, MovementType, M_Locator_ID, M_Product_ID,
carlos@0
   239
                  M_AttributeSetInstance_ID, MovementDate, MovementQty, M_Product_UOM_ID,
carlos@0
   240
                  QuantityOrder, C_UOM_ID
carlos@0
   241
                )
carlos@0
   242
                VALUES
carlos@0
   243
                (
carlos@0
   244
                  v_NextNo, Cur_InOutLine.M_InOutLine_ID, Cur_InOutLine.AD_Client_ID, Cur_InOutLine.AD_Org_ID,
carlos@0
   245
                   'Y', now(), Cur_InOutLine.UpdatedBy, now(),
carlos@0
   246
                  Cur_InOutLine.UpdatedBy, Cur_InOut.MovementType, Cur_InOutLine.M_Locator_ID, Cur_InOutLine.M_Product_ID,
carlos@0
   247
                  COALESCE(Cur_InOutLine.M_AttributeSetInstance_ID, 0), Cur_InOut.MovementDate, v_Qty, Cur_InOutLine.M_Product_UOM_ID,
carlos@0
   248
                  v_QuantityOrder, Cur_InOutLine.C_UOM_ID
carlos@0
   249
                )
carlos@0
   250
                ;
carlos@0
   251
            END IF;
carlos@0
   252
            -- Create Asset
carlos@0
   253
            IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND Cur_InOut.IsSOTrx='Y') THEN
antonio@735
   254
              A_ASSET_CREATE(NULL, Cur_InOutLine.M_InOutLine_ID) ;
carlos@0
   255
            END IF;
carlos@0
   256
            v_ResultStr:='UpdateOrderLine';
carlos@0
   257
            IF(Cur_InOutLine.C_OrderLine_ID IS NOT NULL) THEN
carlos@0
   258
              -- stocked product
carlos@0
   259
              IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND v_IsStocked=1) THEN
carlos@0
   260
                -- Update OrderLine (if C-, Qty is negative)
carlos@0
   261
                UPDATE C_ORDERLINE
carlos@0
   262
                  SET QtyReserved=QtyReserved - v_QtyPO - v_QtySO,
carlos@0
   263
                  QtyDelivered=QtyDelivered + v_QtySO,
carlos@0
   264
                  Updated=now()
carlos@0
   265
                WHERE C_OrderLine_ID=Cur_InOutLine.C_OrderLine_ID;
carlos@0
   266
                -- Products not stocked
carlos@0
   267
              ELSE
carlos@0
   268
                -- Update OrderLine (if C-, Qty is negative)
carlos@0
   269
                UPDATE C_ORDERLINE
carlos@0
   270
                  SET QtyDelivered=QtyDelivered + v_QtySO,
carlos@0
   271
                  Updated=now()
carlos@0
   272
                WHERE C_OrderLine_ID=Cur_InOutLine.C_OrderLine_ID;
carlos@0
   273
              END IF;
carlos@0
   274
            END IF;
carlos@0
   275
            IF(Cur_InOutLine.M_Product_ID IS NOT NULL AND v_IsStocked=1) THEN
carlos@0
   276
              M_Check_Stock(Cur_InOutLine.M_Product_ID, v_AD_Client_ID, v_AD_Org_ID, v_Result, v_Message) ;
carlos@0
   277
              IF v_Result=0 THEN
carlos@0
   278
			    RAISE_APPLICATION_ERROR(-20000, v_Message||' '||'@line@'||' '||Cur_InOutLine.line) ;
carlos@0
   279
              END IF;
carlos@0
   280
            END IF;
carlos@0
   281
          END LOOP; -- For all InOut Lines
carlos@0
   282
          /*******************
carlos@0
   283
          * PO Matching
carlos@0
   284
          ******************/
carlos@0
   285
          IF(Cur_InOut.IsSOTrx='N') THEN
carlos@0
   286
            DECLARE
carlos@0
   287
              Cur_SLines RECORD;
carlos@0
   288
              Cur_ILines RECORD;
carlos@0
   289
              v_Qty NUMBER;
carlos@0
   290
              v_MatchPO_ID NUMBER(10) ;
carlos@0
   291
              v_MatchInv_ID NUMBER(10) ;
carlos@0
   292
            BEGIN
carlos@0
   293
              v_ResultStr:='MatchPO';
carlos@0
   294
              FOR Cur_SLines IN
carlos@0
   295
                (SELECT sl.AD_Client_ID,
carlos@0
   296
                  sl.AD_Org_ID,
carlos@0
   297
                  ol.C_OrderLine_ID,
carlos@0
   298
                  sl.M_InOutLine_ID,
carlos@0
   299
                  sl.M_Product_ID,
carlos@0
   300
                  sl.M_AttributeSetInstance_ID,
carlos@0
   301
                  sl.MovementQty,
carlos@0
   302
                  ol.QtyOrdered
carlos@0
   303
                FROM M_INOUTLINE sl,
carlos@0
   304
                  C_ORDERLINE ol
carlos@0
   305
                WHERE sl.C_OrderLine_ID=ol.C_OrderLine_ID
carlos@0
   306
                  AND sl.M_Product_ID=ol.M_Product_ID  --    AND   sl.M_AttributeSetInstance_ID=ol.M_AttributeSetInstance_ID
carlos@0
   307
                  AND sl.M_InOut_ID=Cur_InOut.M_InOut_ID
carlos@0
   308
                )
carlos@0
   309
              LOOP
carlos@0
   310
                Ad_Sequence_Next('M_MatchPO', Cur_SLines.AD_Org_ID, v_MatchPO_ID) ;
carlos@0
   311
                -- The min qty. Modified by Ismael Ciordia
carlos@0
   312
                v_Qty:=Cur_SLines.MovementQty;
gorkaion@239
   313
                --IF (ABS(Cur_SLines.MovementQty) > ABS(Cur_SLines.QtyOrdered)) THEN
carlos@0
   314
                -- v_Qty := Cur_SLines.QtyOrdered;
carlos@0
   315
                --END IF;
carlos@0
   316
                v_ResultStr:='InsertMatchPO ' || v_MatchPO_ID;
carlos@0
   317
                INSERT
carlos@0
   318
                INTO M_MATCHPO
carlos@0
   319
                  (
carlos@0
   320
                    M_MatchPO_ID, AD_Client_ID, AD_Org_ID, IsActive,
carlos@0
   321
                    Created, CreatedBy, Updated, UpdatedBy,
carlos@0
   322
                    M_InOutLine_ID, C_OrderLine_ID, M_Product_ID, DateTrx,
carlos@0
   323
                    Qty, Processing, Processed, Posted
carlos@0
   324
                  )
carlos@0
   325
                  VALUES
carlos@0
   326
                  (
carlos@0
   327
                    v_MatchPO_ID, Cur_SLines.AD_Client_ID, Cur_SLines.AD_Org_ID, 'Y',
carlos@0
   328
                    now(), 0, now(), 0,
carlos@0
   329
                    Cur_SLines.M_InOutLine_ID, Cur_SLines.C_OrderLine_ID, Cur_SLines.M_Product_ID, now(),
carlos@0
   330
                    v_Qty, 'N', 'Y', 'N'
carlos@0
   331
                  )
carlos@0
   332
                  ;
carlos@0
   333
              END LOOP;
carlos@0
   334
              v_ResultStr:='MatchInv';
carlos@0
   335
              FOR Cur_ILines IN
carlos@0
   336
                (SELECT sl.AD_Client_ID,
carlos@0
   337
                  sl.AD_Org_ID,
carlos@0
   338
                  il.C_InvoiceLine_ID,
carlos@0
   339
                  sl.M_InOutLine_ID,
carlos@0
   340
                  sl.M_Product_ID,
carlos@0
   341
                  sl.M_AttributeSetInstance_ID,
carlos@0
   342
                  sl.MovementQty,
carlos@0
   343
                  il.QTYINVOICED
carlos@0
   344
                FROM M_INOUTLINE sl,
carlos@0
   345
                  C_INVOICELINE il
carlos@0
   346
                WHERE sl.M_InOutLine_ID=il.M_InOutLine_ID
carlos@0
   347
                  AND sl.M_InOut_ID=Cur_InOut.M_InOut_ID
carlos@0
   348
                )
carlos@0
   349
              LOOP
carlos@0
   350
                Ad_Sequence_Next('M_MatchInv', Cur_ILines.AD_Org_ID, v_MatchInv_ID) ;
carlos@0
   351
                -- The min qty. Modified by Ismael Ciordia
carlos@0
   352
                v_Qty:=Cur_ILines.MovementQty;
gorkaion@239
   353
                --IF (ABS(Cur_ILines.MovementQty) > ABS(Cur_ILines.QtyInvoiced)) THEN
carlos@0
   354
                -- v_Qty := Cur_ILines.QtyInvoiced;
carlos@0
   355
                --END IF;
carlos@0
   356
                v_ResultStr:='InsertMatchPO ' || v_MatchPO_ID;
carlos@0
   357
                INSERT
carlos@0
   358
                INTO M_MATCHINV
carlos@0
   359
                  (
carlos@0
   360
                    M_MATCHINV_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
carlos@0
   361
                    CREATED, CREATEDBY, UPDATED, UPDATEDBY,
carlos@0
   362
                    M_INOUTLINE_ID, C_INVOICELINE_ID, M_PRODUCT_ID, DATETRX,
carlos@0
   363
                    QTY, PROCESSING, PROCESSED, POSTED
carlos@0
   364
                  )
carlos@0
   365
                  VALUES
carlos@0
   366
                  (
carlos@0
   367
                    v_MatchInv_ID, Cur_ILines.AD_Client_ID, Cur_ILines.AD_Org_ID, 'Y',
carlos@0
   368
                    now(), 0, now(), 0,
carlos@0
   369
                    Cur_ILines.M_InOutLine_ID, Cur_ILines.C_InvoiceLine_ID, Cur_ILines.M_Product_ID, now(),
carlos@0
   370
                    v_Qty, 'N', 'Y', 'N'
carlos@0
   371
                  )
carlos@0
   372
                  ;
carlos@0
   373
              END LOOP;
carlos@0
   374
            END;
carlos@0
   375
          END IF;
carlos@0
   376
          -- Close Shipment
carlos@0
   377
          v_ResultStr:='CloseShipment';
carlos@0
   378
          UPDATE M_INOUT
carlos@0
   379
            SET Processed='Y',
carlos@0
   380
            DocStatus='CO',
carlos@0
   381
            DocAction='--',
carlos@0
   382
            Updated=now()
carlos@0
   383
          WHERE M_INOUT.M_INOUT_ID=Cur_InOut.M_INOUT_ID;
carlos@0
   384
          --
carlos@0
   385
          v_ResultStr:='LogEntry';
carlos@0
   386
          IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   387
            INSERT
carlos@0
   388
            INTO AD_PINSTANCE_LOG
carlos@0
   389
              (
carlos@0
   390
                AD_PInstance_ID, Log_ID,
carlos@0
   391
                P_ID, P_Msg
carlos@0
   392
              )
carlos@0
   393
              VALUES
carlos@0
   394
              (
carlos@0
   395
                p_PInstance_ID, Cur_InOut.M_InOut_ID,
carlos@0
   396
                Cur_InOut.M_InOut_ID, Cur_InOut.DocAction || ': ' || Cur_InOut.DocumentNo
carlos@0
   397
              )
carlos@0
   398
              ;
carlos@0
   399
          END IF;
carlos@0
   400
          -- Not Processed + Complete --
carlos@0
   401
          /**
carlos@0
   402
          * Reverse Correction
carlos@0
   403
          */
carlos@0
   404
        ELSIF(Cur_InOut.DocStatus='CO' AND Cur_InOut.DocAction='RC') THEN
carlos@0
   405
          v_ResultStr:='CreateInOut';
carlos@0
   406
          SELECT COALESCE(C_DOCTYPE_REVERSED_ID, C_DOCTYPE_ID)
carlos@0
   407
          INTO v_DoctypeReversed_ID
carlos@0
   408
          FROM C_DOCTYPE
carlos@0
   409
          WHERE C_DOCTYPE_ID=Cur_InOut.C_DocType_ID;
carlos@0
   410
          Ad_Sequence_Next('M_InOut', Cur_InOut.M_InOut_ID, v_RInOut_ID) ; -- Get RInOut_ID
carlos@0
   411
          Ad_Sequence_Doctype(v_DoctypeReversed_ID, Cur_InOut.M_InOut_ID, 'Y', v_RDocumentNo) ; -- Get RDocumentNo
carlos@0
   412
          IF(v_RDocumentNo IS NULL) THEN
carlos@0
   413
            AD_Sequence_Doc('DocumentNo_M_InOut', Cur_InOut.AD_Client_ID, 'Y', v_RDocumentNo) ;
carlos@0
   414
          END IF;
carlos@0
   415
          -- Indicate that it is invoiced (i.e. not printed on invoices)
carlos@0
   416
          v_ResultStr:='SetInvoiced';
carlos@0
   417
          UPDATE M_INOUTLINE  SET IsInvoiced='Y'  WHERE M_InOut_ID=Cur_InOut.M_InOut_ID;
carlos@0
   418
          --
carlos@0
   419
          DBMS_OUTPUT.PUT_LINE('Reverse InOut_ID=' || v_RInOut_ID || ' DocumentNo=' || v_RDocumentNo) ;
carlos@0
   420
          v_ResultStr:='InsertInOut Reverse ' || v_RInOut_ID;
carlos@0
   421
          INSERT
carlos@0
   422
          INTO M_INOUT
carlos@0
   423
            (
carlos@0
   424
              M_InOut_ID, C_Order_ID, IsSOTrx, AD_Client_ID,
carlos@0
   425
              AD_Org_ID, IsActive, Created, CreatedBy,
carlos@0
   426
              Updated, UpdatedBy, DocumentNo, C_DocType_ID,
carlos@0
   427
              Description, IsPrinted, MovementType, MovementDate,
carlos@0
   428
              DateAcct, C_BPartner_ID, C_BPartner_Location_ID, AD_User_ID,
carlos@0
   429
              M_Warehouse_ID, POReference, DateOrdered, DeliveryRule,
carlos@0
   430
              FreightCostRule, FreightAmt, C_Project_ID, C_Activity_ID,
carlos@0
   431
              C_Campaign_ID, AD_OrgTrx_ID, User1_ID, User2_ID,
carlos@0
   432
              DeliveryViaRule, M_Shipper_ID, C_Charge_ID, ChargeAmt,
carlos@0
   433
              PriorityRule, DocStatus, DocAction, Processing,
carlos@0
   434
              Processed, ISLOGISTIC, salesrep_id
carlos@0
   435
            )
carlos@0
   436
            VALUES
carlos@0
   437
            (
carlos@0
   438
              v_RInOut_ID, Cur_InOut.C_Order_ID, Cur_InOut.IsSOTrx, Cur_InOut.AD_Client_ID,
carlos@0
   439
              Cur_InOut.AD_Org_ID, 'Y', now(), 0,
carlos@0
   440
              now(), 0, v_RDocumentNo, v_DoctypeReversed_ID,
carlos@0
   441
               '(*R*: ' || Cur_InOut.DocumentNo || ') ' || Cur_InOut.Description, 'N', Cur_InOut.MovementType, Cur_InOut.MovementDate,
carlos@0
   442
              Cur_InOut.DateAcct, Cur_InOut.C_BPartner_ID, Cur_InOut.C_BPartner_Location_ID, Cur_InOut.AD_User_ID,
carlos@0
   443
              Cur_InOut.M_Warehouse_ID, Cur_InOut.POReference, Cur_InOut.DateOrdered, Cur_InOut.DeliveryRule,
carlos@0
   444
              Cur_InOut.FreightCostRule, Cur_InOut.FreightAmt * -1, Cur_InOut.C_Project_ID, Cur_InOut.C_Activity_ID,
carlos@0
   445
              Cur_InOut.C_Campaign_ID, Cur_InOut.AD_OrgTrx_ID, Cur_InOut.User1_ID, Cur_InOut.User2_ID,
carlos@0
   446
              Cur_InOut.DeliveryViaRule, Cur_InOut.M_Shipper_ID, Cur_InOut.C_Charge_ID, Cur_InOut.ChargeAmt * -1,
carlos@0
   447
              Cur_InOut.PriorityRule, 'DR', 'CO', 'N',
carlos@0
   448
               'N', Cur_InOut.islogistic, Cur_InOut.salesrep_id
carlos@0
   449
            )
carlos@0
   450
            ;
carlos@0
   451
          v_ResultStr:='InsertInOutLine';
carlos@0
   452
          FOR Cur_InOutLine IN
carlos@0
   453
            (SELECT *
carlos@0
   454
            FROM M_INOUTLINE
carlos@0
   455
            WHERE M_InOut_ID=Cur_InOut.M_InOut_ID
carlos@0
   456
              AND IsActive='Y'  FOR UPDATE
carlos@0
   457
            )
carlos@0
   458
          LOOP
carlos@0
   459
            -- Create InOut Line
carlos@0
   460
            Ad_Sequence_Next('M_InOutLine', Cur_InOut.M_InOut_ID, v_NextNo) ;
carlos@0
   461
            v_ResultStr:='CreateInOutLine';
carlos@0
   462
            INSERT
carlos@0
   463
            INTO M_INOUTLINE
carlos@0
   464
              (
carlos@0
   465
                M_InOutLine_ID, Line, M_InOut_ID, C_OrderLine_ID,
carlos@0
   466
                AD_Client_ID, AD_Org_ID, IsActive, Created,
carlos@0
   467
                CreatedBy, Updated, UpdatedBy, M_Product_ID,
carlos@0
   468
                M_AttributeSetInstance_ID, C_UOM_ID, M_Locator_ID, MovementQty,
asier@799
   469
                Description, IsInvoiced,  --MODIFIED BY F.IRIAZABAL
carlos@0
   470
                QuantityOrder, M_Product_UOM_ID
carlos@0
   471
              )
carlos@0
   472
              VALUES
carlos@0
   473
              (
carlos@0
   474
                v_NextNo, Cur_InOutLine.Line, v_RInOut_ID, Cur_InOutLine.C_OrderLine_ID,
carlos@0
   475
                Cur_InOut.AD_Client_ID, Cur_InOut.AD_Org_ID, 'Y', now(),
carlos@0
   476
                0, now(), 0, Cur_InOutLine.M_Product_ID,
carlos@0
   477
                Cur_InOutLine.M_AttributeSetInstance_ID, Cur_InOutLine.C_UOM_ID, Cur_InOutLine.M_Locator_ID, Cur_InOutLine.MovementQty * -1,
asier@799
   478
                 '*R*: ' || Cur_InOutLine.Description, Cur_InOutLine.IsInvoiced, --MODIFIED BY F.IRIAZABAL
carlos@0
   479
                Cur_InOutLine.QuantityOrder * -1, Cur_InOutLine.M_PRODUCT_UOM_ID
carlos@0
   480
              )
carlos@0
   481
              ;
carlos@0
   482
          END LOOP;
carlos@0
   483
          -- Close Order
carlos@0
   484
          v_ResultStr:='CloseInOut';
carlos@0
   485
          UPDATE M_INOUT
carlos@0
   486
            SET Description=COALESCE(TO_CHAR(Description), '') || ' (*R*=' || v_RDocumentNo || ')',
carlos@0
   487
            Processed='Y',
carlos@0
   488
            DocStatus='RE', -- it IS reversed
carlos@0
   489
            DocAction='--',
carlos@0
   490
            Updated=now(),
carlos@0
   491
            UpdatedBy=v_User
carlos@0
   492
          WHERE M_INOUT.M_INOUT_ID=Cur_InOut.M_INOUT_ID;
carlos@0
   493
          v_ResultStr:='LogEntry';
carlos@0
   494
          IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   495
            INSERT
carlos@0
   496
            INTO AD_PINSTANCE_LOG
carlos@0
   497
              (
carlos@0
   498
                AD_PInstance_ID, Log_ID,
carlos@0
   499
                P_ID, P_Msg
carlos@0
   500
              )
carlos@0
   501
              VALUES
carlos@0
   502
              (
carlos@0
   503
                p_PInstance_ID, Cur_InOut.M_InOut_ID,
carlos@0
   504
                Cur_InOut.M_InOut_ID, Cur_InOut.DocAction || ': ' || Cur_InOut.DocumentNo
carlos@0
   505
              )
carlos@0
   506
              ;
carlos@0
   507
          END IF;
carlos@0
   508
          -- Post Reversal
carlos@0
   509
          v_ResultStr:='PostReversal';
antonio@735
   510
          M_INOUT_POST(NULL, v_RInOut_ID) ;
carlos@0
   511
          -- Indicate as Reversal Transaction
carlos@0
   512
          v_ResultStr:='IndicateReversal';
carlos@0
   513
          UPDATE M_INOUT
carlos@0
   514
            SET Updated=now(),
carlos@0
   515
            UpdatedBy=v_User,
carlos@0
   516
            DocStatus='RE' -- the reversal transaction
carlos@0
   517
          WHERE M_InOut_ID=v_RInOut_ID;
carlos@0
   518
        END IF; -- ReverseCorrection
carlos@0
   519
      END LOOP; -- InOut Header
carlos@0
   520
      /**
carlos@0
   521
      * Transaction End
carlos@0
   522
      */
carlos@0
   523
      v_ResultStr:='Fini';
carlos@0
   524
    END IF; --FINISH_PROCESS
gorkaion@239
   525
    --<<FINISH_PROCESS>>
carlos@0
   526
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   527
      --  Update AD_PInstance
carlos@0
   528
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
carlos@0
   529
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
carlos@0
   530
    ELSE
gorkaion@239
   531
      DBMS_OUTPUT.PUT_LINE('--<<M_InOut_Post finished>>') ;
carlos@0
   532
    END IF;
carlos@0
   533
    RETURN;
carlos@0
   534
  END; --BODY
carlos@0
   535
EXCEPTION
carlos@0
   536
WHEN OTHERS THEN
carlos@0
   537
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   538
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   539
  IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   540
    ROLLBACK;
carlos@0
   541
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   542
  ELSE
carlos@0
   543
    DECLARE
antonio@735
   544
      v_Code NUMBER:=SQLCODE;
carlos@0
   545
    BEGIN
asier@799
   546
      AD_SEQUENCE_NEXT('DBA_ERRORLOG', v_AD_Client_ID, v_DBA_ErrorLog_ID);
carlos@0
   547
      INSERT
carlos@0
   548
      INTO DBA_ERRORLOG
carlos@0
   549
        (
carlos@0
   550
          DBA_ErrorLog_ID, Created,
carlos@0
   551
          Code, Msg,
carlos@0
   552
          Info
carlos@0
   553
        )
carlos@0
   554
        VALUES
carlos@0
   555
        (
asier@799
   556
          v_DBA_ErrorLog_ID, now(),
carlos@0
   557
          v_Code, v_ResultStr,
carlos@0
   558
           'M_InOut_Post'
carlos@0
   559
        )
carlos@0
   560
        ;
carlos@0
   561
    END;
carlos@0
   562
    RAISE;
carlos@0
   563
  END IF;
carlos@0
   564
  RETURN;
antonio@735
   565
END M_INOUT_POST
gorkaion@239
   566
]]></body>
adrian@94
   567
    </function>
adrian@94
   568
  </database>