src-db/database/model/functions/MA_PRODUCTIONRUN_STANDARD.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Wed, 03 Sep 2008 17:55:37 +0000
changeset 1605 8a0fe0193bef
parent 799 fef2c5e2feb7
child 2152 8294b27ce2a6
permissions -rw-r--r--
Merge r2.5x intro trunk
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION MA_PRODUCTIONRUN_STANDARD">
adrian@94
     3
    <function name="MA_PRODUCTIONRUN_STANDARD" type="NULL">
juanpablo@1605
     4
      <parameter name="p_pinstance_id" type="VARCHAR" mode="in">
antonio@735
     5
        <default/>
antonio@735
     6
      </parameter>
gorkaion@239
     7
      <body><![CDATA[/*************************************************************************
carlos@0
     8
* The contents of this file are subject to the Openbravo  Public  License
carlos@0
     9
* Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
carlos@0
    10
* Version 1.1  with a permitted attribution clause; you may not  use this
carlos@0
    11
* file except in compliance with the License. You  may  obtain  a copy of
carlos@0
    12
* the License at http://www.openbravo.com/legal/license.html
carlos@0
    13
* Software distributed under the License  is  distributed  on  an "AS IS"
carlos@0
    14
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
carlos@0
    15
* License for the specific  language  governing  rights  and  limitations
carlos@0
    16
* under the License.
carlos@0
    17
* The Original Code is Openbravo ERP.
carlos@0
    18
* The Initial Developer of the Original Code is Openbravo SL
carlos@0
    19
* All portions are Copyright (C) 2001-2006 Openbravo SL
carlos@0
    20
* All Rights Reserved.
carlos@0
    21
* Contributor(s):  ______________________________________.
carlos@0
    22
************************************************************************/
carlos@0
    23
  --  Logistice
carlos@0
    24
  v_ResultStr VARCHAR2(2000):='';
carlos@0
    25
  v_Message VARCHAR2(2000):='';
carlos@0
    26
  v_result NUMBER:=1;
carlos@0
    27
  --  Parameter
carlos@0
    28
  TYPE RECORD IS REF CURSOR;
carlos@0
    29
    Cur_Parameter RECORD;
carlos@0
    30
    --
juanpablo@1605
    31
    v_Record_ID VARCHAR2(32):=NULL;
juanpablo@1605
    32
    v_User_ID VARCHAR2(32);
juanpablo@1605
    33
    v_Org_ID VARCHAR2(32);
juanpablo@1605
    34
    v_PlanOrg_ID VARCHAR2(32);
juanpablo@1605
    35
    v_Client_ID VARCHAR2(32);
juanpablo@1605
    36
    v_WRPhase_ID VARCHAR2(32);
carlos@0
    37
    v_DoneQuantity NUMBER;
juanpablo@1605
    38
    v_Process_ID VARCHAR2(32);
juanpablo@1605
    39
    v_Toolset_ID VARCHAR2(32);
juanpablo@1605
    40
    v_ToolsetUsed_ID VARCHAR2(32);
juanpablo@1605
    41
    v_ProductionLine_ID VARCHAR2(32);
juanpablo@1605
    42
    v_GlobalUse_ID VARCHAR2(32);
carlos@0
    43
    v_Processed CHAR;
carlos@0
    44
    v_NoQty CHAR;
carlos@0
    45
    v_GlobalUse CHAR;
carlos@0
    46
    v_Qty NUMBER;
carlos@0
    47
    v_QtyAcumulated NUMBER;
carlos@0
    48
    v_QtyOrder NUMBER;
carlos@0
    49
    v_Count NUMBER;
juanpablo@1605
    50
    v_Product_old VARCHAR2(32);
juanpablo@1605
    51
    v_UOM_old VARCHAR2(32);
juanpablo@1605
    52
    v_Product_UOM_old VARCHAR2(32);
carlos@0
    53
    v_QtyStorage NUMBER:=0;
carlos@0
    54
    v_Line NUMBER;
carlos@0
    55
    v_CostCenterCost NUMBER;
carlos@0
    56
    v_CostCenterUse NUMBER;
carlos@0
    57
    v_Calculated CHAR(1);
carlos@0
    58
    v_ByDefault CHAR(1);
carlos@0
    59
    --  Toolset Types to process
carlos@0
    60
    Cur_ToolsetProcess RECORD;
carlos@0
    61
    --  WRPhase products created
carlos@0
    62
    Cur_WRPProduct RECORD;
carlos@0
    63
    --  WRPhase products useed
carlos@0
    64
    Cur_WRPUsed RECORD;
carlos@0
    65
    --  Stocked Products
juanpablo@1605
    66
    CURSOR Cur_Stock (v_Product_old VARCHAR, v_UOM_old VARCHAR, v_Product_UOM_old VARCHAR, v_Org_old VARCHAR) IS
carlos@0
    67
      SELECT T.M_PRODUCT_ID, T.C_UOM_ID, T.M_LOCATOR_ID, T.M_ATTRIBUTESETINSTANCE_ID,
carlos@0
    68
        T.M_PRODUCT_UOM_ID, COALESCE(T.QtyOnHand, 0) AS Qty, T.QtyOrderOnHand AS QtyOrder,
carlos@0
    69
        U.STDPRECISION
carlos@0
    70
      FROM M_LOCATOR L, M_WAREHOUSE W, M_STORAGE_DETAIL T LEFT JOIN M_PRODUCT_UOM PU ON T.M_PRODUCT_UOM_ID = PU.M_PRODUCT_UOM_ID
carlos@0
    71
                         LEFT JOIN C_UOM U ON PU.C_UOM_ID = U.C_UOM_ID
carlos@0
    72
      WHERE T.M_PRODUCT_ID=v_Product_old
carlos@0
    73
        AND T.C_UOM_ID=v_UOM_old
carlos@0
    74
        AND (v_Product_UOM_old IS NULL OR T.M_Product_UOM_id = v_Product_UOM_old)
carlos@0
    75
        AND T.M_LOCATOR_ID=L.M_LOCATOR_ID
carlos@0
    76
        AND L.M_WAREHOUSE_ID = W.M_WAREHOUSE_ID
gorkaion@239
    77
        AND AD_ISORGINCLUDED(v_Org_old,W.AD_ORG_ID,W.AD_CLIENT_ID)<>-1
gorkaion@239
    78
        AND COALESCE(T.QtyOnHand, 0)>0
carlos@0
    79
      ORDER BY L.PRIORITYNO, T.M_ATTRIBUTESETINSTANCE_ID, T.M_PRODUCT_UOM_ID;
carlos@0
    80
      v_storage CUR_STOCK%ROWTYPE;
carlos@0
    81
      NEXT_PRODUCT BOOLEAN:=false;
carlos@0
    82
      Cur_Stock_ISOPEN BOOLEAN:=false;
carlos@0
    83
    BEGIN
carlos@0
    84
      --  Process Parameters
carlos@0
    85
      IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
    86
        --  Update AD_PInstance
carlos@0
    87
        DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
    88
        v_ResultStr:='PInstanceNotFound';
carlos@0
    89
        AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
    90
        --  Get Parameters
carlos@0
    91
        v_ResultStr:='ReadingParameters';
carlos@0
    92
        FOR Cur_Parameter IN
carlos@0
    93
          (SELECT i.Record_ID, i.AD_User_ID, i.AD_Org_ID, i.AD_Client_ID, p.ParameterName,
carlos@0
    94
            p.P_String, p.P_Number, p.P_Date
carlos@0
    95
          FROM AD_PInstance i LEFT JOIN AD_PInstance_Para p ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
    96
          WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
    97
          ORDER BY p.SeqNo )
carlos@0
    98
        LOOP
carlos@0
    99
          v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
   100
          v_User_ID:=Cur_Parameter.AD_User_ID;
carlos@0
   101
          v_Org_ID:=Cur_Parameter.AD_Org_ID;
carlos@0
   102
          v_Client_ID:=Cur_Parameter.AD_Client_ID;
carlos@0
   103
        END LOOP; --  Get Parameter
carlos@0
   104
      ELSE
gorkaion@239
   105
        DBMS_OUTPUT.PUT_LINE('--<<MA_ProductionRun_Standard>>') ;
carlos@0
   106
      END IF;
carlos@0
   107
    BEGIN --BODY
carlos@0
   108
      v_ResultStr:='Getting DoneQuantity and process_ID';
carlos@0
   109
      SELECT M_ProductionPlan.AD_Org_ID, M_ProductionPlan.MA_WRPhase_ID,
carlos@0
   110
             M_ProductionPlan.ProductionQty, M_ProductionPlan.Processed,
carlos@0
   111
             COALESCE(MA_CostCenter.CALCULATED, 'N') AS CALCULATED, COALESCE(MA_CostCenter.BYDEFAULT, 'N') AS BYDEFAULT
carlos@0
   112
      INTO v_PlanOrg_ID, v_WRPhase_ID, v_DoneQuantity, v_Processed,
carlos@0
   113
           v_Calculated, v_ByDefault
carlos@0
   114
      FROM M_ProductionPlan LEFT JOIN MA_CostCenter_Version ON M_ProductionPlan.MA_CostCenter_Version_ID = MA_CostCenter_Version.MA_CostCenter_Version_ID
carlos@0
   115
                            LEFT JOIN MA_CostCenter ON MA_CostCenter_version.MA_CostCenter_ID = MA_CostCenter.MA_CostCenter_ID
carlos@0
   116
      WHERE M_ProductionPlan_ID=v_Record_ID;
carlos@0
   117
      SELECT MA_Process_ID, NoQty INTO v_Process_ID, v_NoQty
carlos@0
   118
      FROM MA_WRPhase
carlos@0
   119
      WHERE MA_WRPhase_ID=v_WRPhase_ID;
carlos@0
   120
      IF v_Processed='Y' THEN
carlos@0
   121
        v_ResultStr:='Production Run is Processed';
carlos@0
   122
        DBMS_OUTPUT.PUT_LINE('Production Run is already processed') ;
carlos@0
   123
        v_Message:='@ProductionRunProcessed@';
carlos@0
   124
        RAISE_APPLICATION_ERROR(-20000, v_Message);
carlos@0
   125
      END IF;
carlos@0
   126
        --Insert toolsets if necessary
carlos@0
   127
        v_ResultStr:='ToolsetProcess Processing';
carlos@0
   128
        FOR Cur_ToolsetProcess IN
carlos@0
   129
          (SELECT MA_Toolset_Type_ID, UseCoef
carlos@0
   130
          FROM MA_ToolsetProcess
carlos@0
   131
          WHERE MA_Process_ID=v_Process_ID)
carlos@0
   132
        LOOP
carlos@0
   133
          DECLARE
carlos@0
   134
            Cur_MAToolSet RECORD;
carlos@0
   135
          BEGIN
carlos@0
   136
            --It's taken the toolset with higher number of uses
carlos@0
   137
            FOR Cur_MAToolSet IN
carlos@0
   138
              (SELECT MA_Toolset_ID AS Toolset_ID
carlos@0
   139
              FROM MA_Toolset
carlos@0
   140
              WHERE MA_Toolset_Type_ID=Cur_ToolsetProcess.MA_Toolset_Type_ID
carlos@0
   141
                AND Discarded='N'
carlos@0
   142
              ORDER BY NumberUses DESC
carlos@0
   143
              )
carlos@0
   144
            LOOP
carlos@0
   145
              v_Toolset_ID:=Cur_MAToolSet.Toolset_ID;
carlos@0
   146
              EXIT;
carlos@0
   147
            END LOOP;
carlos@0
   148
          END;
carlos@0
   149
          Ad_Sequence_Next('MA_ToolsetUsed', v_Client_ID, v_ToolsetUsed_ID) ;
carlos@0
   150
          INSERT
carlos@0
   151
          INTO MA_ToolsetUsed
carlos@0
   152
            (
carlos@0
   153
              MA_ToolsetUsed_ID, AD_Client_ID, AD_Org_ID, Created,
carlos@0
   154
              Createdby, Isactive, M_ProductionPlan_ID, MA_Toolset_ID,
carlos@0
   155
              Updated, Updatedby, Used
carlos@0
   156
            )
carlos@0
   157
            VALUES
carlos@0
   158
            (
carlos@0
   159
              v_ToolsetUsed_ID, v_Client_ID, v_Org_ID, now(),
carlos@0
   160
              v_User_ID, 'Y', v_Record_ID, v_Toolset_ID,
carlos@0
   161
              now(), v_User_ID, Cur_ToolsetProcess.UseCoef*v_DoneQuantity
carlos@0
   162
            );
carlos@0
   163
        END LOOP;
carlos@0
   164
        --Insert Products
gorkaion@337
   165
        v_Line:=-10;
carlos@0
   166
        v_ResultStr:='ProdRunProduct P+ Processing';
carlos@0
   167
        FOR Cur_WRPProduct IN
carlos@0
   168
          (SELECT wrpp.*, COALESCE(po.M_Locator_ID, p.M_Locator_ID) AS M_Locator_ID
carlos@0
   169
          FROM M_Product p, MA_WRPhaseProduct wrpp
carlos@0
   170
            left join M_Product_Org po on wrpp.M_Product_ID = po.M_Product_ID
carlos@0
   171
                                    AND po.AD_Org_ID = v_PlanOrg_ID
carlos@0
   172
          WHERE MA_WRPhase_ID=v_WRPhase_ID
carlos@0
   173
            AND ProductionType='+'
carlos@0
   174
            AND wrpp.M_PRODUCT_ID=p.M_Product_ID
carlos@0
   175
          )
carlos@0
   176
        LOOP
carlos@0
   177
          Ad_Sequence_Next('M_ProductionLine', v_Client_ID, v_ProductionLine_ID) ;
gorkaion@337
   178
          v_Line:=v_Line + 10;
carlos@0
   179
          INSERT
carlos@0
   180
          INTO M_ProductionLine
carlos@0
   181
            (
carlos@0
   182
              M_ProductionLine_ID, AD_Client_ID, AD_Org_ID, Created,
carlos@0
   183
              Createdby, Isactive, M_ProductionPlan_ID, M_Product_ID,
carlos@0
   184
              Productiontype, MovementQty, Updated, Updatedby,
carlos@0
   185
              C_UOM_ID, M_Locator_ID, Line
carlos@0
   186
            )
carlos@0
   187
            VALUES
carlos@0
   188
            (
carlos@0
   189
              v_ProductionLine_ID, v_Client_ID, v_Org_ID, now(),
carlos@0
   190
              v_User_ID, 'Y', v_Record_ID, Cur_WRPProduct.M_Product_ID,
carlos@0
   191
              Cur_WRPProduct.ProductionType, (CASE v_NoQty WHEN 'Y' THEN 0 ELSE Cur_WRPProduct.MovementQty*v_DoneQuantity END), now(), v_User_ID,
carlos@0
   192
              Cur_WRPProduct.C_UOM_ID,
carlos@0
   193
              COALESCE(Cur_WRPProduct.M_Locator_ID, (SELECT MIN(M_LOCATOR_ID)
carlos@0
   194
                                                    FROM M_LOCATOR L, M_WAREHOUSE W
carlos@0
   195
                                                    WHERE L.M_WAREHOUSE_ID = W.M_WAREHOUSE_ID
carlos@0
   196
                                                    AND W.AD_ORG_ID = v_PlanOrg_ID)),
carlos@0
   197
              v_Line
carlos@0
   198
            )
carlos@0
   199
            ;
gorkaion@337
   200
          --v_Line:=v_Line + 10;
carlos@0
   201
        END LOOP;
carlos@0
   202
        -- Insert production plan for used products P-
carlos@0
   203
        SELECT wrp.GROUPUSE INTO v_GlobalUse
carlos@0
   204
        FROM MA_WRPhase wrp, M_ProductionPlan pp
carlos@0
   205
        WHERE wrp.MA_WRPHASE_ID=pp.MA_WRPHASE_ID
carlos@0
   206
          AND pp.M_PRODUCTIONPLAN_ID=v_Record_ID;
carlos@0
   207
        FOR Cur_WRPUsed IN
carlos@0
   208
          (SELECT wrpp.*, p.IsStocked, pp.AD_Client_ID AS Client_ID, pp.AD_Org_ID AS Org_ID,
carlos@0
   209
            p.M_LOCATOR_ID, pp.M_PRODUCTION_ID, p.Name as pname
carlos@0
   210
          FROM MA_WRPhaseProduct wrpp, M_Product p, M_ProductionPlan pp
carlos@0
   211
          WHERE pp.M_PRODUCTIONPLAN_ID=v_Record_ID
carlos@0
   212
            AND pp.MA_WRPHASE_ID=wrpp.MA_WRPHASE_ID
carlos@0
   213
            AND p.M_PRODUCT_ID=wrpp.M_PRODUCT_ID
carlos@0
   214
            AND wrpp.PRODUCTIONTYPE='-'
carlos@0
   215
          )
carlos@0
   216
        LOOP
carlos@0
   217
          v_ResultStr:='CreatingLine Products';
carlos@0
   218
          IF(Cur_WRPUsed.IsStocked='Y') THEN
carlos@0
   219
            v_QtyAcumulated:=0;
gorkaion@239
   220
            IF(NOT Cur_Stock_ISOPEN) OR(v_Product_old<>Cur_WRPUsed.M_Product_ID  OR v_UOM_old<>Cur_WRPUsed.C_UOM_ID) THEN
carlos@0
   221
              v_Product_old:=Cur_WRPUsed.M_Product_ID;
carlos@0
   222
              v_UOM_old:=Cur_WRPUsed.C_UOM_ID;
carlos@0
   223
              v_Product_UOM_old := Cur_WRPUsed.M_Product_UOM_ID;
carlos@0
   224
              IF(Cur_Stock_ISOPEN) THEN
carlos@0
   225
                CLOSE Cur_Stock;
carlos@0
   226
                Cur_Stock_ISOPEN:=false;
carlos@0
   227
              END IF;
carlos@0
   228
              OPEN CUR_STOCK(v_Product_old, v_UOM_old, v_Product_UOM_old, v_PlanOrg_ID) ;
carlos@0
   229
              Cur_Stock_ISOPEN:=true;
carlos@0
   230
              FETCH CUR_STOCK INTO v_storage;
carlos@0
   231
              IF CUR_STOCK%NOTFOUND THEN
carlos@0
   232
                NEXT_PRODUCT:=true;
carlos@0
   233
              END IF;
carlos@0
   234
              v_QtyStorage:=v_storage.Qty;
carlos@0
   235
            END IF;
carlos@0
   236
            IF(NOT NEXT_PRODUCT) THEN
carlos@0
   237
              LOOP
carlos@0
   238
                v_Qty:=LEAST(v_QtyStorage, v_DoneQuantity*Cur_WRPUsed.MovementQty - v_QtyAcumulated) ;
carlos@0
   239
                v_QtyStorage:=v_QtyStorage - v_Qty;
carlos@0
   240
                v_QtyAcumulated:=v_QtyAcumulated + v_Qty;
carlos@0
   241
                v_QtyOrder:=ROUND(v_storage.QtyOrder*(v_Qty/v_storage.Qty),v_storage.stdprecision) ;
carlos@0
   242
                IF v_storage.M_Locator_ID IS NULL THEN
carlos@0
   243
                  v_Message:='El producto ' || Cur_WRPUsed.pname || ' no tiene almacen definido.';
carlos@0
   244
                RAISE_APPLICATION_ERROR(-20000, v_Message);
carlos@0
   245
                END IF;
carlos@0
   246
                  IF(v_GlobalUse='Y') THEN
carlos@0
   247
                    SELECT count(*)
carlos@0
   248
                    INTO v_Count
carlos@0
   249
                    FROM MA_GlobalUse
carlos@0
   250
                    WHERE M_Production_ID=Cur_WRPUsed.M_Production_ID
carlos@0
   251
                      AND M_Product_ID=Cur_WRPUsed.M_Product_ID;
carlos@0
   252
                    IF(v_Count=0) THEN
carlos@0
   253
                      AD_Sequence_Next('MA_GlobalUse', Cur_WRPUsed.Client_ID, v_GlobalUse_ID) ;
carlos@0
   254
                      INSERT
carlos@0
   255
                      INTO MA_GlobalUse
carlos@0
   256
                        (
carlos@0
   257
                          MA_GlobalUse_ID, M_Production_ID, AD_Client_ID, AD_Org_ID,
carlos@0
   258
                          IsActive, Created, CreatedBy, Updated,
carlos@0
   259
                          UpdatedBy, M_Product_ID, C_UOM_ID, MovementQty,
carlos@0
   260
                          M_Locator_ID, M_AttributeSetInstance_ID, M_Product_UOM_ID,
carlos@0
   261
                          QuantityOrder
carlos@0
   262
                        )
carlos@0
   263
                        VALUES
carlos@0
   264
                        (
carlos@0
   265
                          v_GlobalUse_ID, Cur_WRPUsed.M_Production_ID, Cur_WRPUsed.Client_ID, Cur_WRPUsed.Org_ID,
juanpablo@1605
   266
                           'Y', now(), '0', now(),
juanpablo@1605
   267
                          '0', Cur_WRPUsed.M_Product_ID, Cur_WRPUsed.C_UOM_ID, 0,
carlos@0
   268
                          v_storage.M_Locator_ID, v_storage.M_AttributeSetInstance_ID, v_storage.M_Product_UOM_ID,
carlos@0
   269
                          (CASE
carlos@0
   270
                             WHEN v_QtyOrder is null THEN null
carlos@0
   271
                             ELSE 0
carlos@0
   272
                           END
carlos@0
   273
                          )
carlos@0
   274
                        );
carlos@0
   275
                    END IF;
carlos@0
   276
                    EXIT;
carlos@0
   277
                  ELSE
gorkaion@337
   278
                    v_Line := v_Line + 10;
carlos@0
   279
                    AD_Sequence_Next('M_ProductionLine', Cur_WRPUsed.Client_ID, v_ProductionLine_ID) ;
carlos@0
   280
                    INSERT
carlos@0
   281
                    INTO M_ProductionLine
carlos@0
   282
                      (
carlos@0
   283
                        M_ProductionLine_ID, M_ProductionPlan_ID, Line, AD_Client_ID,
carlos@0
   284
                        AD_Org_ID, IsActive, Created, CreatedBy,
carlos@0
   285
                        Updated, UpdatedBy, M_Product_ID, C_UOM_ID,
carlos@0
   286
                        MovementQty, ProductionType, M_Locator_ID, M_AttributeSetInstance_ID,
carlos@0
   287
                        M_Product_UOM_ID, QuantityOrder
carlos@0
   288
                      )
carlos@0
   289
                      VALUES
carlos@0
   290
                      (
carlos@0
   291
                        v_ProductionLine_ID, v_Record_ID, v_Line, Cur_WRPUsed.Client_ID,
carlos@0
   292
                        Cur_WRPUsed.Org_ID, 'Y', now(), 0,
carlos@0
   293
                        now(), 0, Cur_WRPUsed.M_Product_ID, Cur_WRPUsed.C_UOM_ID,
carlos@0
   294
                        (CASE v_NoQty WHEN 'Y' THEN 0 ELSE v_Qty END), '-', v_storage.M_Locator_ID, v_storage.M_AttributeSetInstance_ID,
carlos@0
   295
                        v_storage.M_Product_UOM_ID,
carlos@0
   296
                        (CASE v_NoQty
carlos@0
   297
                          WHEN 'Y' THEN (CASE
carlos@0
   298
                                           WHEN v_QtyOrder IS null THEN null
carlos@0
   299
                                           ELSE 0
carlos@0
   300
                                         END)
carlos@0
   301
                          ELSE v_QtyOrder
carlos@0
   302
                         END)
carlos@0
   303
                      )
carlos@0
   304
                      ;
gorkaion@239
   305
                    IF(v_QtyAcumulated>=v_DoneQuantity*Cur_WRPUsed.MovementQty) THEN
carlos@0
   306
                      EXIT;
carlos@0
   307
                    END IF;
carlos@0
   308
                    v_QtyStorage:=0;
carlos@0
   309
                    v_ResultStr:='FetchingData';
carlos@0
   310
                    FETCH CUR_STOCK INTO v_storage;
carlos@0
   311
                    EXIT WHEN CUR_STOCK%NOTFOUND;
carlos@0
   312
                    v_QtyStorage:=v_storage.Qty;
carlos@0
   313
                  END IF;
carlos@0
   314
              END LOOP;
carlos@0
   315
            END IF; --NEXT_PRODUCT
gorkaion@239
   316
            IF (v_QtyAcumulated < v_DoneQuantity*Cur_WRPUsed.MovementQty) THEN
carlos@0
   317
              v_Message := '@NotEnoughStocked@: ' || Cur_WRPUsed.pname;
carlos@0
   318
            RAISE_APPLICATION_ERROR(-20000, v_Message);
carlos@0
   319
            END IF;
carlos@0
   320
          ELSE
carlos@0
   321
            IF Cur_WRPUsed.M_Locator_ID IS NULL THEN
carlos@0
   322
              v_Message:='El producto ' || Cur_WRPUsed.pname || ' no tiene almacen definido.';
carlos@0
   323
            RAISE_APPLICATION_ERROR(-20000, v_Message);
carlos@0
   324
            END IF;
carlos@0
   325
          IF(v_GlobalUse='Y') THEN
carlos@0
   326
            SELECT count(*)
carlos@0
   327
                INTO v_Count
carlos@0
   328
                FROM MA_GlobalUse
carlos@0
   329
                WHERE M_Production_ID=Cur_WRPUsed.M_Production_ID
carlos@0
   330
                  AND M_Product_ID=Cur_WRPUsed.M_Product_ID;
carlos@0
   331
                IF(v_Count=0) THEN
carlos@0
   332
                  AD_Sequence_Next('MA_GlobalUse', Cur_WRPUsed.Client_ID, v_GlobalUse_ID) ;
carlos@0
   333
                  INSERT
carlos@0
   334
                  INTO MA_GlobalUse
carlos@0
   335
                    (
carlos@0
   336
                      MA_GlobalUse_ID, M_Production_ID, AD_Client_ID, AD_Org_ID,
carlos@0
   337
                      IsActive, Created, CreatedBy, Updated,
carlos@0
   338
                      UpdatedBy, M_Product_ID, C_UOM_ID, MovementQty,
carlos@0
   339
                      M_Locator_ID
carlos@0
   340
                    )
carlos@0
   341
                    VALUES
carlos@0
   342
                    (
carlos@0
   343
                      v_GlobalUse_ID, Cur_WRPUsed.M_Production_ID, Cur_WRPUsed.Client_ID, Cur_WRPUsed.Org_ID,
juanpablo@1605
   344
                       'Y', now(), '0', now(),
juanpablo@1605
   345
                      '0', Cur_WRPUsed.M_Product_ID, Cur_WRPUsed.C_UOM_ID, 0,
carlos@0
   346
                      Cur_WRPUsed.M_Locator_ID
carlos@0
   347
                    )
carlos@0
   348
                    ;
carlos@0
   349
                END IF;
carlos@0
   350
              ELSE
gorkaion@337
   351
                v_Line := v_Line + 10;
carlos@0
   352
                AD_Sequence_Next('M_ProductionLine', Cur_WRPUsed.Client_ID, v_ProductionLine_ID) ;
carlos@0
   353
                INSERT
carlos@0
   354
                INTO M_ProductionLine
carlos@0
   355
                  (
carlos@0
   356
                    M_ProductionLine_ID, M_ProductionPlan_ID, Line, AD_Client_ID,
carlos@0
   357
                    AD_Org_ID, IsActive, Created, CreatedBy,
carlos@0
   358
                    Updated, UpdatedBy, M_Product_ID, MovementQty,
carlos@0
   359
                    ProductionType, C_UOM_ID, M_Locator_ID
carlos@0
   360
                  )
carlos@0
   361
                  VALUES
carlos@0
   362
                  (
carlos@0
   363
                    v_ProductionLine_ID, v_Record_ID, v_Line, Cur_WRPUsed.Client_ID,
juanpablo@1605
   364
                    Cur_WRPUsed.Org_ID, 'Y', now(), '0',
juanpablo@1605
   365
                    now(), '0', Cur_WRPUsed.M_Product_ID,( CASE v_NoQty WHEN 'Y' THEN 0 ELSE v_DoneQuantity*Cur_WRPUsed.MovementQty END),
carlos@0
   366
                    '-', Cur_WRPUsed.C_UOM_ID, Cur_WRPUsed.M_Locator_ID
carlos@0
   367
                  )
carlos@0
   368
                  ;
carlos@0
   369
              END IF;
carlos@0
   370
          END IF;
gorkaion@239
   371
            --<<NEXT_PRODUCT>>
carlos@0
   372
            NEXT_PRODUCT:=false;
gorkaion@337
   373
            --v_Line:=v_Line + 10;
carlos@0
   374
        END LOOP; --Used Products
carlos@0
   375
        --Insert salary categories.
carlos@0
   376
carlos@0
   377
        INSERT INTO MA_PL_EMPLOYEE
carlos@0
   378
        (MA_PL_EMPLOYEE_ID,
carlos@0
   379
         CREATED, CREATEDBY, UPDATED, UPDATEDBY, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
asier@799
   380
         M_PRODUCTIONPLAN_ID, C_SALARY_CATEGORY_ID, QUANTITY)
carlos@0
   381
        SELECT AD_Get_Next_Sequence('MA_PL_Employee', v_Client_ID),
carlos@0
   382
         now(), v_User_ID, now(), v_User_ID, v_Client_ID, v_Org_ID, 'Y',
asier@799
   383
         v_Record_ID, C_Salary_Category_ID, Quantity
carlos@0
   384
        FROM MA_COSTCENTER_EMPLOYEE cce, M_ProductionPlan pp
carlos@0
   385
        WHERE cce.MA_Costcenter_version_ID = pp.MA_COstCenter_Version_ID
carlos@0
   386
          AND pp.M_ProductionPlan_ID = v_Record_ID
carlos@0
   387
          AND cce.IsActive = 'Y';
carlos@0
   388
carlos@0
   389
        --Insert machines.
carlos@0
   390
carlos@0
   391
        INSERT INTO MA_PL_MACHINE
carlos@0
   392
        (MA_PL_MACHINE_ID,
carlos@0
   393
         CREATED,CREATEDBY,UPDATED,UPDATEDBY,AD_CLIENT_ID,AD_ORG_ID,ISACTIVE,
carlos@0
   394
         M_PRODUCTIONPLAN_ID,MA_MACHINE_ID,USE)
carlos@0
   395
        SELECT AD_Get_Next_Sequence('MA_PL_Machine', v_Client_ID),
carlos@0
   396
         now(), v_User_ID, now(), v_User_ID, v_Client_ID, v_Org_ID, 'Y',
carlos@0
   397
         v_Record_ID, MA_Machine_ID, Use
carlos@0
   398
        FROM MA_COSTCENTER_MACHINE ccm, M_ProductionPlan pp
carlos@0
   399
        WHERE ccm.MA_Costcenter_version_ID = pp.MA_CostCenter_Version_ID
carlos@0
   400
          AND pp.M_ProductionPlan_ID = v_Record_ID
carlos@0
   401
          AND ccm.IsActive = 'Y';
carlos@0
   402
carlos@0
   403
        --Insert indirect costs
carlos@0
   404
carlos@0
   405
        INSERT INTO MA_PL_IC
carlos@0
   406
        (MA_PL_IC_ID,
carlos@0
   407
         CREATED,CREATEDBY,UPDATED,UPDATEDBY,AD_CLIENT_ID,AD_ORG_ID,ISACTIVE,
carlos@0
   408
         M_PRODUCTIONPLAN_ID,MA_INDIRECT_COST_ID)
carlos@0
   409
        SELECT AD_Get_Next_Sequence('MA_PL_IC', v_Client_ID),
carlos@0
   410
         now(), v_User_ID, now(), v_User_ID, v_Client_ID, v_Org_ID, 'Y',
carlos@0
   411
         v_Record_ID, MA_Indirect_Cost_ID
carlos@0
   412
        FROM MA_COSTCENTER_IC ccic, M_ProductionPlan pp
carlos@0
   413
        WHERE ccic.MA_Costcenter_version_ID = pp.MA_CostCenter_Version_ID
carlos@0
   414
          AND pp.M_ProductionPlan_ID = v_Record_ID
carlos@0
   415
          AND ccic.IsActive = 'Y';
carlos@0
   416
carlos@0
   417
        IF (v_Calculated = 'Y') THEN
carlos@0
   418
          IF (v_ByDefault = 'N') THEN
ivan@291
   419
            SELECT TO_NUMBER((M_Production.ENDTIME - M_Production.STARTTIME)*24) INTO v_CostCenterUse
carlos@0
   420
            FROM M_Production, M_ProductionPlan
carlos@0
   421
            WHERE M_Production.M_Production_ID = M_ProductionPlan.M_PRODUCTION_ID
carlos@0
   422
              AND M_ProductionPlan.M_PRODUCTIONPLAN_ID = v_Record_ID;
carlos@0
   423
          ELSE
carlos@0
   424
            SELECT M_ProductionPlan.PRODUCTIONQTY * MA_WRPhase.COSTCENTERUSE INTO v_CostCenterUse
carlos@0
   425
            FROM M_PRoductionPlan, MA_WRPhase
carlos@0
   426
            WHERE M_ProductionPlan.M_PRODUCTIONPLAN_ID = v_Record_ID
carlos@0
   427
              AND M_ProductionPlan.MA_WRPHASE_ID = MA_WRPhase.MA_WRPHASE_ID;
carlos@0
   428
          END IF;
carlos@0
   429
          UPDATE M_ProductionPlan
carlos@0
   430
          SET MA_CostCenterUse = v_CostCenterUse
carlos@0
   431
          WHERE M_ProductionPlan_ID = v_Record_ID;
carlos@0
   432
        END IF;
carlos@0
   433
carlos@0
   434
carlos@0
   435
        IF(CUR_STOCK_ISOPEN) THEN
carlos@0
   436
          CLOSE CUR_STOCK;
carlos@0
   437
          CUR_STOCK_ISOPEN:=false;
carlos@0
   438
        END IF;
carlos@0
   439
carlos@0
   440
        UPDATE M_PRODUCTIONPLAN
carlos@0
   441
          SET Processed='Y'
carlos@0
   442
        WHERE M_ProductionPlan_ID=v_Record_ID;
gorkaion@239
   443
      --<<FINISH_PROCESS>>
carlos@0
   444
      v_ResultStr:='Ending';
carlos@0
   445
      --v_Message := v_Message || '@Created@: ' || v_NoRecords;
carlos@0
   446
      IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   447
        --  Update AD_PInstance
carlos@0
   448
        DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
carlos@0
   449
        AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
carlos@0
   450
      ELSE
gorkaion@239
   451
        DBMS_OUTPUT.PUT_LINE('--<<MA_ProductionRun_Standard finished>> ' || v_Message) ;
carlos@0
   452
      END IF;
carlos@0
   453
      RETURN;
carlos@0
   454
    END; --BODY
carlos@0
   455
EXCEPTION
carlos@0
   456
  WHEN OTHERS THEN
carlos@0
   457
    DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   458
    v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   459
    DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   460
    ROLLBACK;
carlos@0
   461
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   462
    RETURN;
antonio@735
   463
END MA_PRODUCTIONRUN_STANDARD
gorkaion@239
   464
]]></body>
adrian@94
   465
    </function>
adrian@94
   466
  </database>