src-db/database/model/functions/MA_WORKEFFORT_VALIDATE.xml
author rafaeldemiguel <rdm@openbravo.com>
Thu, 22 Sep 2011 18:28:46 +0200
changeset 14983 fcf3ccf492f3
parent 9072 79e55dfc8af0
child 15001 a7a8ccfb1923
permissions -rw-r--r--
[Production-Development] Triggers, DisplayLogics and ReadonlyLogics for windows WorkEffort and Production Run
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION MA_WORKEFFORT_VALIDATE">
adrian@94
     3
    <function name="MA_WORKEFFORT_VALIDATE" 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
priya@9072
     9
* Version  1.1  (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.
ggi@6701
    18
* The Initial Developer of the Original Code is Openbravo SLU
rdm@14983
    19
* All portions are Copyright (C) 2001-2011 Openbravo SLU
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
  -- Parameter
carlos@0
    27
  TYPE RECORD IS REF CURSOR;
carlos@0
    28
    Cur_Parameter RECORD;
carlos@0
    29
    Cur_Toolset RECORD;
gorkaion@2007
    30
    Cur_PP RECORD;
carlos@0
    31
    --
juanpablo@1605
    32
    v_Record_ID VARCHAR2(32):=NULL;
juanpablo@1605
    33
    v_User_ID VARCHAR2(32);
juanpablo@1605
    34
    v_Org_ID VARCHAR2(32);
juanpablo@1605
    35
    v_Client_ID VARCHAR2(32);
carlos@0
    36
    v_count NUMBER;
carlos@0
    37
--    v_count2 NUMBER;
juanpablo@1605
    38
--    v_Production_ID VARCHAR2(32);
carlos@0
    39
    v_ProductionDate DATE;
carlos@0
    40
--    v_Name NVARCHAR2(60) ;
carlos@0
    41
    -- p_ppline            NUMBER;
carlos@0
    42
    -- p_plline          NUMBER;
juanpablo@1605
    43
--    v_ProductionPlan_ID VARCHAR2(32);
juanpablo@1605
    44
--    v_ProductionLine_ID VARCHAR2(32);
carlos@0
    45
--    v_Quantity NUMBER;
juanpablo@1605
    46
--    v_UOM_ID VARCHAR2(32);
carlos@0
    47
--    v_PInstance NUMBER;
carlos@0
    48
--    v_doneQty NUMBER;
carlos@0
    49
--    v_needQty NUMBER;
carlos@0
    50
    v_QtyOnHand NUMBER;
carlos@0
    51
    p_Processed CHAR;
carlos@0
    52
    v_ProductQty NUMBER;
juanpablo@1605
    53
    v_NextNo VARCHAR2(32);
carlos@0
    54
    Quantity NUMBER:=99999; -- unlimited
carlos@0
    55
    v_Result NUMBER:=1;
carlos@0
    56
carlos@0
    57
    CUR_PL_Post RECORD;
carlos@0
    58
    Cur_ProductionPlan RECORD;
carlos@0
    59
carlos@0
    60
    FINISH_PROCESS BOOLEAN:=FALSE;
asier@1929
    61
    v_is_included NUMBER:=0;
asier@1929
    62
    v_available_period NUMBER:=0;
asier@1929
    63
    v_is_ready AD_Org.IsReady%TYPE;
asier@1929
    64
    v_is_tr_allow AD_OrgType.IsTransactionsAllowed%TYPE;
victor@3065
    65
    v_isacctle AD_OrgType.IsAcctLegalEntity%TYPE;
victor@3065
    66
    v_org_bule_id AD_Org.AD_Org_ID%TYPE;
asier@1929
    67
    v_AD_Org_ID VARCHAR2(32);
rafael@6085
    68
    v_prodline NUMBER;
rafael@6085
    69
    v_line NUMBER;
rdm@14983
    70
    v_AD_Pinstance_ID VARCHAR2(32);
carlos@0
    71
  BEGIN
carlos@0
    72
    -- Process Parameters
carlos@0
    73
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
    74
      --  Update AD_PInstance
carlos@0
    75
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
    76
      v_ResultStr:='PInstanceNotFound';
antonio@735
    77
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
    78
      -- Get Parameters
carlos@0
    79
      v_ResultStr:='ReadingParameters';
carlos@0
    80
      FOR Cur_Parameter IN
carlos@0
    81
        (SELECT i.Record_ID, i.AD_User_ID, i.AD_Org_ID, i.AD_Client_ID, p.ParameterName,
carlos@0
    82
          p.P_String, p.P_Number, p.P_Date
carlos@0
    83
        FROM AD_PINSTANCE i
carlos@0
    84
        LEFT JOIN AD_PINSTANCE_PARA p
carlos@0
    85
          ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
    86
        WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
    87
        ORDER BY p.SeqNo) LOOP
carlos@0
    88
        v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
    89
        v_User_ID:=Cur_Parameter.AD_User_ID;
carlos@0
    90
        v_Org_ID:=Cur_Parameter.AD_Org_ID;
carlos@0
    91
        v_Client_ID:=Cur_Parameter.AD_Client_ID;
carlos@0
    92
      END LOOP; -- Get Parameter
carlos@0
    93
    ELSE
gorkaion@239
    94
      DBMS_OUTPUT.PUT_LINE('--<<MA_WorkEffort_Validate>>') ;
carlos@0
    95
    END IF;
carlos@0
    96
  BEGIN --BODY
asier@1929
    97
    SELECT Processed, MOVEMENTDATE, AD_ORG_ID
asier@1929
    98
    INTO p_Processed, v_ProductionDate, v_AD_Org_ID
carlos@0
    99
    FROM M_PRODUCTION
carlos@0
   100
    WHERE M_Production_ID=v_Record_ID;
carlos@0
   101
    --Check if is has been already processed
carlos@0
   102
    IF(p_Processed='Y') THEN
carlos@0
   103
      v_ResultStr:='Work Effort is already validated';
carlos@0
   104
      v_Message:='@WorkEffortValidated@';
carlos@0
   105
      RAISE_APPLICATION_ERROR(-20000, v_Message);
carlos@0
   106
    END IF;
carlos@0
   107
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   108
      -- Check that there are some parts of the job and that theay are processed
asier@1929
   109
      
asier@1929
   110
      -- Check the header belongs to a organization where transactions are posible and ready to use
asier@1929
   111
      SELECT AD_Org.IsReady, Ad_OrgType.IsTransactionsAllowed
asier@1929
   112
      INTO v_is_ready, v_is_tr_allow
asier@1929
   113
      FROM M_PRODUCTION, AD_Org, AD_OrgType
asier@1929
   114
      WHERE AD_Org.AD_Org_ID=M_PRODUCTION.AD_Org_ID
asier@1929
   115
      AND AD_Org.AD_OrgType_ID=AD_OrgType.AD_OrgType_ID
asier@1929
   116
      AND M_PRODUCTION.M_PRODUCTION_ID=v_Record_ID;
asier@1929
   117
      IF (v_is_ready='N') THEN
asier@1929
   118
        RAISE_APPLICATION_ERROR(-20000, '@OrgHeaderNotReady@');
asier@1929
   119
      END IF;
asier@1929
   120
      IF (v_is_tr_allow='N') THEN
asier@1929
   121
        RAISE_APPLICATION_ERROR(-20000, '@OrgHeaderNotTransAllowed@');
asier@1929
   122
      END IF;
asier@1929
   123
      
gorkaion@2007
   124
      SELECT AD_ORG_CHK_DOCUMENTS('M_PRODUCTION', 'M_PRODUCTIONPLAN', v_Record_ID, 'M_PRODUCTION_ID', 'M_PRODUCTION_ID') INTO v_is_included FROM dual;
asier@1929
   125
      IF (v_is_included=-1) THEN
asier@1929
   126
        RAISE_APPLICATION_ERROR(-20000, '@LinesAndHeaderDifferentLEorBU@');
asier@1929
   127
      END IF;
gorkaion@2007
   128
      FOR CUR_PP IN
gorkaion@2007
   129
        (SELECT M_PRODUCTIONPLAN_ID
gorkaion@2007
   130
         FROM M_PRODUCTIONPLAN
gorkaion@2007
   131
         WHERE M_PRODUCTION_ID = v_Record_ID) LOOP
gorkaion@2007
   132
        SELECT AD_ORG_CHK_DOCUMENTS('M_PRODUCTIONPLAN', 'M_PRODUCTIONLINE', CUR_PP.M_PRODUCTIONPLAN_ID, 'M_PRODUCTIONPLAN_ID', 'M_PRODUCTIONPLAN_ID') INTO v_is_included FROM dual;
gorkaion@2007
   133
        IF (v_is_included=-1) THEN
gorkaion@2007
   134
          RAISE_APPLICATION_ERROR(-20000, '@LinesAndHeaderDifferentLEorBU@');
gorkaion@2007
   135
        END IF;
victor@3065
   136
      END LOOP;      
asier@1929
   137
      
victor@3065
   138
      -- Check the period control is opened (only if it is legal entity with accounting)
victor@3065
   139
      -- Gets the BU or LE of the document
victor@3162
   140
      SELECT AD_GET_DOC_LE_BU('M_PRODUCTION', v_Record_ID, 'M_PRODUCTION_ID', 'LE')
victor@3065
   141
      INTO v_org_bule_id
victor@3065
   142
      FROM DUAL;
asier@1929
   143
      
victor@3065
   144
      SELECT AD_OrgType.IsAcctLegalEntity
victor@3065
   145
      INTO v_isacctle
victor@3065
   146
      FROM AD_OrgType, AD_Org
victor@3065
   147
      WHERE AD_Org.AD_OrgType_ID = AD_OrgType.AD_OrgType_ID
victor@3065
   148
      AND AD_Org.AD_Org_ID=v_org_bule_id;
victor@3065
   149
      
victor@3065
   150
      IF (v_isacctle='Y') THEN
victor@3065
   151
        SELECT C_CHK_OPEN_PERIOD(v_AD_Org_ID, v_ProductionDate, 'MMP', NULL) 
victor@3065
   152
        INTO v_available_period
victor@3065
   153
        FROM DUAL;
victor@3065
   154
        
victor@3065
   155
        IF (v_available_period<>1) THEN
victor@3065
   156
          RAISE_APPLICATION_ERROR(-20000, '@PeriodNotAvailable@');
victor@3065
   157
        END IF;
asier@1929
   158
      END IF;
asier@1929
   159
      
victor@3065
   160
      
carlos@0
   161
      v_ResultStr:='Checking ProductionRun';
carlos@0
   162
      SELECT COUNT(*)
carlos@0
   163
      INTO v_count
carlos@0
   164
      FROM M_PRODUCTIONPLAN
carlos@0
   165
      WHERE M_Production_ID=v_Record_ID;
carlos@0
   166
      IF(v_count=0) THEN
carlos@0
   167
        v_ResultStr := 'There are not Production Runs to validate';
carlos@0
   168
        v_Message:='@NoProductionRun@';
carlos@0
   169
        RAISE_APPLICATION_ERROR(-20000, v_Message);
carlos@0
   170
      END IF;
carlos@0
   171
    END IF; --FINISH_PROCESS
carlos@0
   172
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   173
      SELECT COUNT(*)
carlos@0
   174
      INTO v_count
carlos@0
   175
      FROM M_PRODUCTIONPLAN pp
carlos@0
   176
      WHERE M_Production_ID=v_Record_ID
carlos@0
   177
        AND PROCESSED='N';
gorkaion@239
   178
      IF(v_count<>0) THEN
carlos@0
   179
        v_Message:='@ProducionRunNoProcessed@';
carlos@0
   180
        RAISE_APPLICATION_ERROR(-20000, v_Message);
carlos@0
   181
        FINISH_PROCESS:=TRUE;
carlos@0
   182
      END IF;
carlos@0
   183
    END IF; --FINISH_PROCESS
carlos@0
   184
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   185
      --Check if there's any global use production phase.
carlos@0
   186
      SELECT COUNT(*)
carlos@0
   187
      INTO v_count
carlos@0
   188
      FROM M_PRODUCTIONPLAN pp,
carlos@0
   189
        MA_WRPHASE wp
carlos@0
   190
      WHERE pp.MA_WRPHASE_ID=wp.MA_WRPHASE_ID
carlos@0
   191
        AND pp.M_PRODUCTION_ID=v_Record_ID
carlos@0
   192
        AND wp.GROUPUSE='Y';
gorkaion@239
   193
      IF(v_count<>0) THEN
carlos@0
   194
        v_ResultStr:='Distributing product of global use';
antonio@735
   195
        MA_GLOBALUSE_DISTRIBUTE(v_Client_ID, v_Org_ID, v_User_ID, v_Record_ID) ;
carlos@0
   196
      END IF;
carlos@0
   197
    END IF; --FINISH_PROCESS
carlos@0
   198
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   199
      --Check if all the ProductionLine have a warehouse
carlos@0
   200
      v_ResultStr:='Checking Locators';
carlos@0
   201
      SELECT COUNT(*)
carlos@0
   202
      INTO v_count
carlos@0
   203
      FROM M_PRODUCTIONLINE l,
carlos@0
   204
        M_PRODUCTIONPLAN p
carlos@0
   205
      WHERE l.M_ProductionPlan_ID=p.M_ProductionPlan_ID
carlos@0
   206
        AND p.M_Production_ID=v_Record_ID
carlos@0
   207
        AND l.M_Locator_ID IS NULL;
gorkaion@239
   208
      IF(v_count<>0) THEN
carlos@0
   209
        v_ResultStr := 'There are produts without Locator';
carlos@0
   210
        v_Message:='@NeedLocator@';
carlos@0
   211
        RAISE_APPLICATION_ERROR(-20000, v_Message);
carlos@0
   212
        FINISH_PROCESS:=TRUE;
carlos@0
   213
      END IF;
carlos@0
   214
    END IF; --FINISH_PROCESS
carlos@0
   215
    --Check if exist any product in a productionplan with the same locator and productiontype P+ and P-
carlos@0
   216
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   217
      SELECT COUNT(*)
carlos@0
   218
      INTO v_count
carlos@0
   219
      FROM M_PRODUCTIONLINE pl, M_PRODUCTIONPLAN pp
carlos@0
   220
      WHERE pl.PRODUCTIONTYPE='+'
carlos@0
   221
        AND pl.M_PRODUCTIONPLAN_ID = pp.M_PRODUCTIONPLAN_ID
carlos@0
   222
        AND pp.M_PRODUCTION_ID = v_Record_ID
carlos@0
   223
        AND EXISTS
carlos@0
   224
        (SELECT 1
carlos@0
   225
        FROM M_PRODUCTIONLINE
carlos@0
   226
        WHERE M_PRODUCTIONPLAN_ID=pl.M_PRODUCTIONPLAN_ID
carlos@0
   227
          AND M_PRODUCT_ID=pl.M_PRODUCT_ID
carlos@0
   228
          AND PRODUCTIONTYPE='-'
carlos@0
   229
        )
carlos@0
   230
        ;
gorkaion@239
   231
      IF(v_count<>0) THEN
carlos@0
   232
        ROLLBACK;
carlos@0
   233
        v_ResultStr := 'Same product in a production plan as P+ and P-';
carlos@0
   234
        v_Message:='@P+AndP-InSamePlan@';
carlos@0
   235
        RAISE_APPLICATION_ERROR(-20000, v_Message);
carlos@0
   236
        FINISH_PROCESS:=TRUE;
carlos@0
   237
      END IF;
carlos@0
   238
    END IF; --FINISH_PROCESS
carlos@0
   239
    IF (NOT FINISH_PROCESS) THEN
rafael@6085
   240
      SELECT COUNT(*), MAX(pp.line), MAX(PL.LINE)
rafael@6085
   241
      INTO v_Count, v_prodline, v_line
carlos@0
   242
      FROM M_PRODUCTIONPLAN PP, M_PRODUCTIONLINE PL, M_PRODUCT P
carlos@0
   243
      WHERE PL.M_PRODUCT_ID=P.M_PRODUCT_ID
carlos@0
   244
        AND P.M_ATTRIBUTESET_ID IS NOT NULL
rafael@6085
   245
        AND (P.ATTRSETVALUETYPE IS NULL OR P.ATTRSETVALUETYPE <> 'F')
rafael@6098
   246
        AND (SELECT ISONEATTRSETVALREQUIRED FROM M_ATTRIBUTESET WHERE M_ATTRIBUTESET_ID = P.M_ATTRIBUTESET_ID) = 'Y'
juanpablo@1605
   247
        AND COALESCE(PL.M_ATTRIBUTESETINSTANCE_ID, '0') = '0'
carlos@0
   248
        AND PL.M_ProductionPlan_ID=pp.M_ProductionPlan_ID
carlos@0
   249
        AND pp.M_Production_ID=v_Record_ID;
gorkaion@239
   250
      IF v_Count<>0 THEN
rafael@6085
   251
        RAISE_APPLICATION_ERROR(-20000, '@inProductionPlan@'||' '||'@line@'||' '||v_prodline||', '||'@Inline@'||' '||v_line||' '||'@productWithoutAttributeSet@') ;
carlos@0
   252
        FINISH_PROCESS:=TRUE;
carlos@0
   253
      END IF;
carlos@0
   254
    END IF;
carlos@0
   255
    -- All Production Lines
carlos@0
   256
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   257
      FOR CUR_PL_Post IN
carlos@0
   258
        (SELECT pl.M_ProductionLine_ID, pl.AD_Client_ID, pl.AD_Org_ID, p.MovementDate,
carlos@0
   259
          pl.M_Product_ID, pl.M_AttributeSetInstance_ID, pl.MovementQty, pl.M_Locator_ID,
carlos@0
   260
          pl.M_Product_UOM_ID, pl.QuantityOrder, pl.C_UOM_ID, pl.LINE, pl.PRODUCTIONTYPE,
carlos@0
   261
          pr.ISSTOCKED, pr.PRODUCTTYPE
carlos@0
   262
        FROM M_PRODUCTION p, M_PRODUCTIONLINE pl, M_PRODUCTIONPLAN pp, M_PRODUCT pr
carlos@0
   263
        WHERE p.M_Production_ID=pp.M_Production_ID
carlos@0
   264
          AND pp.M_ProductionPlan_ID=pl.M_ProductionPlan_ID
carlos@0
   265
          AND pp.M_Production_ID=v_Record_ID
carlos@0
   266
          AND pl.M_PRODUCT_ID=pr.M_PRODUCT_ID
sivaraman@7675
   267
          AND pr.ISSTOCKED='Y'
carlos@0
   268
        ORDER BY pp.Line, pl.Line)
carlos@0
   269
      LOOP
carlos@0
   270
        -- M_ProductionLine_ID, AD_Client_ID, AD_Org_ID, MovementDate, M_Product_ID, MovementQty, M_Locator_ID
carlos@0
   271
        -- DBMS_OUTPUT.PUT_LINE('ProductionLine=' || CUR_PL_Post.M_ProductionLine_ID);
gorkaion@1787
   272
        -- DBMS_OUTPUT.PUT_LINE('  Qty=' || CUR_PL_Post.MovementQty || ', OnHand=' || M_BOM_Qty_OnHand(CUR_PL_Post.M_Product_ID, NULL, CUR_PL_Post.M_Locator_ID));
carlos@0
   273
        -- Check Stock levels for reductions
carlos@0
   274
        -- Check, if product exists and if it is stocked
carlos@0
   275
        -- Unlimited capacity if no item
gorkaion@239
   276
        IF(CUR_PL_Post.ProductType<>'I' OR CUR_PL_Post.IsStocked='N') THEN
carlos@0
   277
          v_QtyOnHand:=Quantity;
carlos@0
   278
          -- Stocked item
carlos@0
   279
        ELSIF(CUR_PL_Post.IsStocked='Y') THEN
carlos@0
   280
          -- Get ProductQty
carlos@0
   281
          SELECT COALESCE(SUM(QtyOnHand), 0)
carlos@0
   282
          INTO v_ProductQty
carlos@0
   283
          FROM M_STORAGE_DETAIL s
carlos@0
   284
          WHERE M_Product_ID=CUR_PL_Post.M_Product_ID
carlos@0
   285
            AND EXISTS
carlos@0
   286
            (SELECT * FROM M_LOCATOR l WHERE s.M_Locator_ID=l.M_Locator_ID)
carlos@0
   287
            ;
carlos@0
   288
          --DBMS_OUTPUT.PUT_LINE('Qty=' || ProductQty);
carlos@0
   289
          v_QtyOnHand:=v_ProductQty;
carlos@0
   290
        END IF;
carlos@0
   291
        -- Create Transaction Entry
carlos@0
   292
        v_ResultStr:='CreateTransaction';
carlos@0
   293
        Ad_Sequence_Next('M_Transaction', v_Org_ID, v_NextNo) ;
carlos@0
   294
        INSERT
carlos@0
   295
        INTO M_TRANSACTION
carlos@0
   296
          (
carlos@0
   297
            M_Transaction_ID, M_ProductionLine_ID, AD_Client_ID, AD_Org_ID,
carlos@0
   298
            IsActive, Created, CreatedBy, Updated,
carlos@0
   299
            UpdatedBy, MovementType, M_Locator_ID, M_Product_ID,
carlos@0
   300
            M_AttributeSetInstance_ID, MovementDate, MovementQty, M_Product_UOM_ID,
carlos@0
   301
            QuantityOrder, C_UOM_ID
carlos@0
   302
          )
carlos@0
   303
          VALUES
carlos@0
   304
          (
carlos@0
   305
            v_NextNo, CUR_PL_Post.M_ProductionLine_ID, CUR_PL_Post.AD_Client_ID, CUR_PL_Post.AD_Org_ID,
antonio@735
   306
             'Y', now(), v_User_ID, now(),
carlos@0
   307
            v_User_ID, 'P+', CUR_PL_Post.M_Locator_ID, CUR_PL_Post.M_Product_ID,
juanpablo@1605
   308
            COALESCE(CUR_PL_Post.M_AttributeSetInstance_ID, '0'), -- not distinguishing between assemby/disassembly
carlos@0
   309
            CUR_PL_Post.MovementDate,(CASE CUR_PL_Post.PRODUCTIONTYPE WHEN '+' THEN CUR_PL_Post.MovementQty ELSE -CUR_PL_Post.MOVEMENTQTY END), CUR_PL_Post.M_Product_UOM_ID,(CASE CUR_PL_Post.PRODUCTIONTYPE WHEN '+' THEN CUR_PL_Post.QUANTITYORDER ELSE -CUR_PL_Post.QuantityOrder END), CUR_PL_Post.C_UOM_ID
carlos@0
   310
          )
carlos@0
   311
          ;
gorkaion@239
   312
        IF(CUR_PL_Post.IsStocked<>'N') THEN
carlos@0
   313
          M_Check_Stock(CUR_PL_Post.M_Product_ID, v_Client_ID, v_Org_ID, v_Result, v_Message) ;
sivaraman@6775
   314
          IF (v_Result=0) THEN
sivaraman@6775
   315
            v_Message:=v_Message || ' @Inline@ ' || CUR_PL_Post.line;
sivaraman@6775
   316
            RAISE_APPLICATION_ERROR(-20000, v_Message);
sivaraman@6775
   317
          END IF;
carlos@0
   318
        END IF;
carlos@0
   319
      END LOOP;
carlos@0
   320
    END IF; --FINISH_PROCESS
carlos@0
   321
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   322
        --Calculate Production Cost
carlos@0
   323
        Ma_Production_Cost(v_Record_ID, v_User_ID, v_Message);
carlos@0
   324
    END IF; --FINISH_PROCESS
carlos@0
   325
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   326
      -- Update donequantity on workrequirements
carlos@0
   327
      v_ResultStr:='Updating workrequirement';
carlos@0
   328
      FOR Cur_ProductionPlan IN
carlos@0
   329
        (SELECT *  FROM M_PRODUCTIONPLAN pp  WHERE pp.M_PRODUCTION_ID=v_Record_ID)
carlos@0
   330
      LOOP
carlos@0
   331
        UPDATE MA_WRPHASE
rdm@14983
   332
          SET DONEQUANTITY=DONEQUANTITY + Cur_ProductionPlan.PRODUCTIONQTY, RUNTIME=Cur_ProductionPlan.RUNTIME
carlos@0
   333
        WHERE MA_WRPhase_ID=Cur_ProductionPlan.MA_WRPHASE_ID;
rdm@14983
   334
        -- Close Phase if its marked
rdm@14983
   335
	IF (Cur_ProductionPlan.CLOSEPHASE='Y') THEN
rdm@14983
   336
	  v_AD_Pinstance_ID := GET_UUID();
rdm@14983
   337
	  INSERT INTO AD_PINSTANCE 
rdm@14983
   338
	  (AD_PINSTANCE_ID, AD_PROCESS_ID, RECORD_ID, ISACTIVE, 
rdm@14983
   339
	  AD_USER_ID, AD_CLIENT_ID, AD_ORG_ID, CREATED, CREATEDBY,
rdm@14983
   340
	  UPDATED, UPDATEDBY)
rdm@14983
   341
	  VALUES
rdm@14983
   342
	  (v_AD_Pinstance_ID,'800118',Cur_ProductionPlan.MA_WRPHASE_ID,'Y',
rdm@14983
   343
	  v_User_ID, v_Client_ID, v_Org_ID, now(), v_User_ID,
rdm@14983
   344
	  now(), v_User_ID
rdm@14983
   345
	  );
rdm@14983
   346
	  MA_WRPHASE_CLOSE(v_AD_Pinstance_ID);
rdm@14983
   347
	END IF;
carlos@0
   348
      END LOOP;
carlos@0
   349
      -- Indicate that we are done
carlos@0
   350
      UPDATE M_PRODUCTION
carlos@0
   351
        SET Processed='Y',
antonio@735
   352
        updated=now(),
carlos@0
   353
        updatedby=v_User_ID
rdm@14983
   354
      WHERE M_Production_ID=v_Record_ID;    
carlos@0
   355
    END IF; --FINISH_PROCESS
carlos@0
   356
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   357
      v_ResultStr:='Updating used toolsets';
carlos@0
   358
      FOR Cur_Toolset IN
carlos@0
   359
        (SELECT MA_TOOLSETUSED.*
antonio@737
   360
         FROM MA_TOOLSETUSED, M_PRODUCTIONPLAN
carlos@0
   361
         WHERE MA_TOOLSETUSED.M_PRODUCTIONPLAN_ID = M_PRODUCTIONPLAN.M_PRODUCTIONPLAN_ID
carlos@0
   362
           AND M_PRODUCTIONPLAN.M_PRODUCTION_ID = v_Record_ID) LOOP
carlos@0
   363
        UPDATE MA_TOOLSET
carlos@0
   364
        SET NUMBERUSES = NUMBERUSES + Cur_Toolset.USED
carlos@0
   365
        WHERE MA_TOOLSET_ID = Cur_Toolset.MA_TOOLSET_ID;
carlos@0
   366
      END LOOP;
carlos@0
   367
    END IF;
gorkaion@239
   368
    --<<FINISH_PROCESS>>
carlos@0
   369
    --v_Message := v_Message || '@Created@: ' || v_NoRecords;
carlos@0
   370
      --  Update AD_PInstance
carlos@0
   371
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
antonio@735
   372
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
carlos@0
   373
    RETURN;
carlos@0
   374
  END; --BODY
carlos@0
   375
EXCEPTION
carlos@0
   376
WHEN OTHERS THEN
carlos@0
   377
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   378
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   379
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   380
  ROLLBACK;
antonio@735
   381
  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   382
  RETURN;
antonio@735
   383
END MA_WORKEFFORT_VALIDATE
gorkaion@239
   384
]]></body>
adrian@94
   385
    </function>
adrian@94
   386
  </database>