database/model/functions/MA_WORKREQUIREMENT_PROCESS.xml
author Gorka Ion Damián <gorkaion.damian@openbravo.com>
Fri, 14 Dec 2007 16:21:33 +0000
changeset 239 a30acae847e1
parent 170 4b5493776a7f
child 735 daced7e311c9
permissions -rw-r--r--
Added CDATA to views, functions and triggers.
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION MA_WORKREQUIREMENT_PROCESS">
adrian@94
     3
    <function name="MA_WORKREQUIREMENT_PROCESS" type="NULL">
adrian@94
     4
      <parameter name="p_PInstance_ID" type="NUMERIC" mode="in"/>
gorkaion@239
     5
      <body><![CDATA[/*************************************************************************
carlos@0
     6
* The contents of this file are subject to the Openbravo  Public  License
carlos@0
     7
* Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
carlos@0
     8
* Version 1.1  with a permitted attribution clause; you may not  use this
carlos@0
     9
* file except in compliance with the License. You  may  obtain  a copy of
carlos@0
    10
* the License at http://www.openbravo.com/legal/license.html
carlos@0
    11
* Software distributed under the License  is  distributed  on  an "AS IS"
carlos@0
    12
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
carlos@0
    13
* License for the specific  language  governing  rights  and  limitations
carlos@0
    14
* under the License.
carlos@0
    15
* The Original Code is Openbravo ERP.
carlos@0
    16
* The Initial Developer of the Original Code is Openbravo SL
carlos@0
    17
* All portions are Copyright (C) 2001-2006 Openbravo SL
carlos@0
    18
* All Rights Reserved.
carlos@0
    19
* Contributor(s):  ______________________________________.
carlos@0
    20
************************************************************************/
carlos@0
    21
  --  Logistice
carlos@0
    22
  v_ResultStr VARCHAR2(2000):='';
carlos@0
    23
  v_Message VARCHAR2(2000):='';
carlos@0
    24
  --  Parameter
carlos@0
    25
  TYPE RECORD IS REF CURSOR;
carlos@0
    26
    Cur_Parameter RECORD;
carlos@0
    27
    --
carlos@0
    28
    v_Record_ID NUMBER:=NULL;
carlos@0
    29
    v_User_ID NUMBER;
carlos@0
    30
    v_Org_ID NUMBER;
carlos@0
    31
    v_Client_ID NUMBER;
carlos@0
    32
    v_WrPhase_ID NUMBER;
carlos@0
    33
    v_WrPhaseProd_ID NUMBER;
carlos@0
    34
    --  Sequences to process
carlos@0
    35
    Cur_Sequence RECORD;
carlos@0
    36
    --
carlos@0
    37
    v_ProcessPlan_ID NUMBER;
carlos@0
    38
    v_ExplodePhases CHAR;
carlos@0
    39
    v_Quantity NUMBER;
carlos@0
    40
    v_Multiplier NUMBER;
carlos@0
    41
    v_Processed CHAR;
carlos@0
    42
    v_LaunchDate DATE;
carlos@0
    43
    -- WRPhases to process
carlos@0
    44
    Cur_WRPhase RECORD;
carlos@0
    45
    --  Sequence Products to process
carlos@0
    46
    Cur_SeqProduct RECORD;
carlos@0
    47
    --ptr_sp  Cur_SeqProduct%ROWTYPE;
carlos@0
    48
    FINISH_PROCESS BOOLEAN:=false;
carlos@0
    49
  BEGIN
carlos@0
    50
    --  Process Parameters
carlos@0
    51
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
    52
      --  Update AD_PInstance
carlos@0
    53
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
    54
      v_ResultStr:='PInstanceNotFound';
carlos@0
    55
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
    56
      --  Get Parameters
carlos@0
    57
      v_ResultStr:='ReadingParameters';
carlos@0
    58
      FOR Cur_Parameter IN
carlos@0
    59
        (SELECT i.Record_ID,
carlos@0
    60
          i.AD_User_ID,
carlos@0
    61
          i.AD_Org_ID,
carlos@0
    62
          i.AD_Client_ID,
carlos@0
    63
          p.ParameterName,
carlos@0
    64
          p.P_String,
carlos@0
    65
          p.P_Number,
carlos@0
    66
          p.P_Date
carlos@0
    67
        FROM AD_PInstance i
carlos@0
    68
        LEFT JOIN AD_PInstance_Para p
carlos@0
    69
          ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
    70
        WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
    71
        ORDER BY p.SeqNo
carlos@0
    72
        )
carlos@0
    73
      LOOP
carlos@0
    74
        v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
    75
        v_User_ID:=Cur_Parameter.AD_User_ID;
carlos@0
    76
        v_Org_ID:=Cur_Parameter.AD_Org_ID;
carlos@0
    77
        v_Client_ID:=Cur_Parameter.AD_Client_ID;
carlos@0
    78
      END LOOP; --  Get Parameter
carlos@0
    79
    ELSE
gorkaion@239
    80
      DBMS_OUTPUT.PUT_LINE('--<<MA_WorkRequirement_Process>>') ;
carlos@0
    81
    END IF;
carlos@0
    82
  BEGIN --BODY
carlos@0
    83
    -- Check if all the required fields are filled
carlos@0
    84
    v_ResultStr:='CheckingArguments';
carlos@0
    85
    SELECT MA_ProcessPlan_ID,
carlos@0
    86
      Quantity,
carlos@0
    87
      Processed,
carlos@0
    88
      ExplodePhases,
carlos@0
    89
      LaunchDate
carlos@0
    90
    INTO v_ProcessPlan_ID,
carlos@0
    91
      v_Quantity,
carlos@0
    92
      v_Processed,
carlos@0
    93
      v_ExplodePhases,
carlos@0
    94
      v_LaunchDate
carlos@0
    95
    FROM MA_WorkRequirement
carlos@0
    96
    WHERE MA_WorkRequirement_ID=v_Record_ID;
carlos@0
    97
    --Check if it hasn't already been processed
carlos@0
    98
    IF v_Processed='Y' THEN
carlos@0
    99
      v_ResultStr:='Work Requirement is Processed';
carlos@0
   100
      DBMS_OUTPUT.PUT_LINE('Work Requirement is already Processed') ;
carlos@0
   101
      v_Message:='@WorkRequirementProcessed@';
carlos@0
   102
      FINISH_PROCESS:=true;
carlos@0
   103
    END IF;
carlos@0
   104
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   105
      IF(v_ProcessPlan_ID IS NULL OR v_Quantity IS NULL) THEN
carlos@0
   106
        DBMS_OUTPUT.PUT_LINE('Faltan argumentos') ;
carlos@0
   107
        FINISH_PROCESS:=true;
carlos@0
   108
      END IF;
carlos@0
   109
    END IF; --FINISH_PROCESS
carlos@0
   110
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   111
      IF(v_ExplodePhases='Y') THEN
carlos@0
   112
        -- Data insertion begins
carlos@0
   113
        FOR Cur_Sequence IN
carlos@0
   114
          (SELECT s.MA_Sequence_ID,
carlos@0
   115
            s.MA_Process_ID,
carlos@0
   116
            s.Name,
carlos@0
   117
            s.CostCenterUse,
carlos@0
   118
            s.Preptime,
carlos@0
   119
            s.Multiplier,
carlos@0
   120
            s.SeqNo,
carlos@0
   121
            s.NoQty,
carlos@0
   122
            s.GroupUse,
carlos@0
   123
            s.Outsourced
carlos@0
   124
          FROM MA_Sequence s,
carlos@0
   125
            MA_ProcessPlan_Version p
carlos@0
   126
          WHERE s.MA_PROCESSPLAN_VERSION_ID=p.MA_PROCESSPLAN_VERSION_ID
carlos@0
   127
            AND p.MA_ProcessPlan_ID=v_ProcessPlan_ID
gorkaion@239
   128
            AND p.DATEFROM<=v_LaunchDate
gorkaion@239
   129
            AND p.DATETO>v_LaunchDate
carlos@0
   130
          )
carlos@0
   131
        LOOP
carlos@0
   132
          v_ResultStr:='InsertPhase';
carlos@0
   133
          Ad_Sequence_Next('MA_WRPhase', v_Client_ID, v_WrPhase_ID) ;
carlos@0
   134
          v_Multiplier:=Cur_Sequence.Multiplier;
carlos@0
   135
          --OPEN Cur_SeqProduct (Cur_Sequence.MA_Sequence_ID);
carlos@0
   136
          --FETCH Cur_SeqProduct INTO ptr_sp;
carlos@0
   137
          INSERT
carlos@0
   138
          INTO MA_WRPHASE
carlos@0
   139
            (
carlos@0
   140
              MA_Wrphase_ID, MA_Workrequirement_ID, MA_Sequence_ID, AD_Client_ID,
carlos@0
   141
              AD_Org_ID, Isactive, Created, Createdby,
carlos@0
   142
              Updated, Updatedby, MA_Process_ID, CostCenterUse,
carlos@0
   143
              Preptime, Quantity, Donequantity,
carlos@0
   144
              SeqNo, Closed, NoQty, GroupUse, Outsourced
carlos@0
   145
            )
carlos@0
   146
            VALUES
carlos@0
   147
            (
carlos@0
   148
              v_WrPhase_ID, v_Record_ID, Cur_Sequence.MA_Sequence_ID, v_Client_ID,
carlos@0
   149
              v_Org_ID, 'Y', now(), v_User_ID,
carlos@0
   150
              now(), v_User_ID, Cur_Sequence.MA_Process_ID, Cur_Sequence.CostCenterUse,
carlos@0
   151
              Cur_Sequence.Preptime, v_Multiplier*v_Quantity, 0,
carlos@0
   152
              Cur_Sequence.SeqNo, 'N', Cur_Sequence.NoQty, Cur_Sequence.GroupUse, Cur_Sequence.Outsourced
carlos@0
   153
            )
carlos@0
   154
            ;
carlos@0
   155
          FOR Cur_SeqProduct IN
carlos@0
   156
            (SELECT *
carlos@0
   157
            FROM MA_SequenceProduct
carlos@0
   158
            WHERE MA_Sequence_ID=Cur_Sequence.MA_Sequence_ID
carlos@0
   159
            )
carlos@0
   160
          LOOP
carlos@0
   161
            v_ResultStr:='InsertProduct';
carlos@0
   162
            Ad_Sequence_Next('MA_WRPhaseProduct', v_Client_ID, v_WrPhaseProd_ID) ;
carlos@0
   163
            DBMS_OUTPUT.PUT_LINE('Valores:' || v_WrPhaseProd_ID ||' '|| v_WrPhase_ID||' '|| v_Client_ID||' '|| v_Org_ID||' '|| v_User_ID||' '|| Cur_SeqProduct.M_Product_ID||' '|| Cur_SeqProduct.Productiontype||' '|| Cur_SeqProduct.Quantity) ;
carlos@0
   164
            v_ResultStr:='InsertProduct2';
carlos@0
   165
            INSERT
carlos@0
   166
            INTO MA_WRPHASEPRODUCT
carlos@0
   167
              (
carlos@0
   168
                MA_Wrphaseproduct_ID, MA_Wrphase_ID, AD_Client_ID, AD_Org_ID,
carlos@0
   169
                Isactive, Created, Createdby, Updated,
carlos@0
   170
                Updatedby, M_Product_ID, Productiontype, MovementQty,
carlos@0
   171
                ComponentCost, C_UOM_ID, consumeRM, decreaseInd,
carlos@0
   172
                quantityorder, M_Product_UOM_ID
carlos@0
   173
              )
carlos@0
   174
              VALUES
carlos@0
   175
              (
carlos@0
   176
                v_WrPhaseProd_ID, v_WrPhase_ID, v_Client_ID, v_Org_ID,
carlos@0
   177
                 'Y', now(), v_User_ID, now(),
carlos@0
   178
                v_User_ID, Cur_SeqProduct.M_Product_ID, Cur_SeqProduct.Productiontype, Cur_SeqProduct.Quantity,
carlos@0
   179
                Cur_SeqProduct.ComponentCost, Cur_SeqProduct.C_UOM_ID, Cur_SeqProduct.consumeRM, Cur_SeqProduct.decreaseInd,
carlos@0
   180
                Cur_SeqProduct.quantityorder, Cur_SeqProduct.M_Product_UOM_ID
carlos@0
   181
              )
carlos@0
   182
              ;
carlos@0
   183
          END LOOP;
carlos@0
   184
        END LOOP;
carlos@0
   185
      ELSE
carlos@0
   186
        -- Data insertion begins
carlos@0
   187
        FOR Cur_WRPhase IN
carlos@0
   188
          (SELECT MA_WRPhase_ID,
carlos@0
   189
            MA_Sequence_ID,
carlos@0
   190
            quantity
carlos@0
   191
          FROM MA_WRPhase
carlos@0
   192
          WHERE MA_WorkRequirement_ID=v_Record_ID
carlos@0
   193
          )
carlos@0
   194
        LOOP
carlos@0
   195
          FOR Cur_SeqProduct IN
carlos@0
   196
            (SELECT *
carlos@0
   197
            FROM MA_SequenceProduct
carlos@0
   198
            WHERE MA_Sequence_ID=Cur_WRPhase.MA_Sequence_ID
carlos@0
   199
            )
carlos@0
   200
          LOOP
carlos@0
   201
            v_ResultStr:='NInsertProduct';
carlos@0
   202
            Ad_Sequence_Next('MA_WRPhaseProduct', v_Client_ID, v_WrPhaseProd_ID) ;
carlos@0
   203
            --DBMS_OUTPUT.PUT_LINE('Valores:' || v_WrPhaseProd_ID ||' '|| v_WrPhase_ID||' '|| v_Client_ID||' '|| v_Org_ID||' '|| v_User_ID||' '|| Cur_SeqProduct.Name||' '|| Cur_SeqProduct.M_Product_ID||' '|| Cur_SeqProduct.Productiontype||' '|| Cur_SeqProduct.Quantity);
carlos@0
   204
            v_ResultStr:='NInsertProduct2';
carlos@0
   205
            INSERT
carlos@0
   206
            INTO MA_WRPHASEPRODUCT
carlos@0
   207
              (
carlos@0
   208
                MA_Wrphaseproduct_ID, MA_Wrphase_ID, AD_Client_ID, AD_Org_ID,
carlos@0
   209
                Isactive, Created, Createdby, Updated,
carlos@0
   210
                Updatedby, M_Product_ID, Productiontype, MovementQty,
carlos@0
   211
                ComponentCost, C_UOM_ID, consumeRM, decreaseInd,
carlos@0
   212
                QUANTITYORDER, M_PRODUCT_UOM_ID
carlos@0
   213
              )
carlos@0
   214
              VALUES
carlos@0
   215
              (
carlos@0
   216
                v_WrPhaseProd_ID, Cur_WRPhase.MA_WrPhase_ID, v_Client_ID, v_Org_ID,
carlos@0
   217
                 'Y', now(), v_User_ID, now(),
carlos@0
   218
                v_User_ID, Cur_SeqProduct.M_Product_ID, Cur_SeqProduct.Productiontype, Cur_SeqProduct.Quantity,
carlos@0
   219
                Cur_SeqProduct.ComponentCost, Cur_SeqProduct.C_UOM_ID, Cur_SeqProduct.consumeRM, Cur_SeqProduct.decreaseInd,
carlos@0
   220
                Cur_SeqProduct.quantityorder, Cur_SeqProduct.M_Product_UOM_ID
carlos@0
   221
              )
carlos@0
   222
              ;
carlos@0
   223
          END LOOP;
carlos@0
   224
        END LOOP;
carlos@0
   225
      END IF;
carlos@0
   226
    END IF; --FINISH_PROCESS
carlos@0
   227
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   228
      UPDATE MA_WorkRequirement
carlos@0
   229
        SET Processed='Y'
carlos@0
   230
      WHERE MA_WorkRequirement_ID=v_Record_ID;
carlos@0
   231
    END IF; --FINISH_PROCESS
gorkaion@239
   232
    --<<FINISH_PROCESS>>
carlos@0
   233
    --v_Message := v_Message || '@Created@: ' || v_NoRecords;
carlos@0
   234
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   235
      --  Update AD_PInstance
carlos@0
   236
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
carlos@0
   237
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 1, v_Message) ;
carlos@0
   238
    ELSE
gorkaion@239
   239
      DBMS_OUTPUT.PUT_LINE('--<<MA_WorkRequirement_Process finished>> ' || v_Message) ;
carlos@0
   240
    END IF;
carlos@0
   241
    RETURN;
carlos@0
   242
  END; --BODY
carlos@0
   243
EXCEPTION
carlos@0
   244
WHEN OTHERS THEN
carlos@0
   245
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   246
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   247
  IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   248
    ROLLBACK;
carlos@0
   249
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   250
  ELSE
carlos@0
   251
    DECLARE
carlos@0
   252
      v_Code NUMBER:=SQLCode;
carlos@0
   253
    BEGIN
carlos@0
   254
      INSERT
carlos@0
   255
      INTO DBA_ErrorLog
carlos@0
   256
        (
carlos@0
   257
          DBA_ErrorLog_ID, Created,
carlos@0
   258
          Code, Msg,
carlos@0
   259
          Info
carlos@0
   260
        )
carlos@0
   261
        VALUES
carlos@0
   262
        (
carlos@0
   263
          DBA_ErrorLog_Seq.NextVal, now(),
carlos@0
   264
          v_Code, v_ResultStr,
carlos@0
   265
           'MA_WorkRequirement_Post'
carlos@0
   266
        )
carlos@0
   267
        ;
carlos@0
   268
    END;
carlos@0
   269
  END IF;
carlos@0
   270
  --p_Invoice_ID := 0;        --  Error Indicator
carlos@0
   271
  RETURN;
adrian@94
   272
END MA_WorkRequirement_Process
adrian@170
   273
gorkaion@239
   274
gorkaion@239
   275
]]></body>
carlos@0
   276
    </function>
adrian@94
   277
  </database>