database/model/functions/MA_WORKREQUIREMENT_PROCESS.xml
author Antonio Moreno <antonio.moreno@openbravo.com>
Tue, 22 Apr 2008 13:22:11 +0000
changeset 735 daced7e311c9
parent 239 a30acae847e1
permissions -rw-r--r--
Structure after exporting from PostgreSQL with new dbsourcemanager
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">
antonio@735
     4
      <parameter name="p_pinstance_id" type="NUMERIC" 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
  --  Parameter
carlos@0
    27
  TYPE RECORD IS REF CURSOR;
carlos@0
    28
    Cur_Parameter RECORD;
carlos@0
    29
    --
carlos@0
    30
    v_Record_ID NUMBER:=NULL;
carlos@0
    31
    v_User_ID NUMBER;
carlos@0
    32
    v_Org_ID NUMBER;
carlos@0
    33
    v_Client_ID NUMBER;
carlos@0
    34
    v_WrPhase_ID NUMBER;
carlos@0
    35
    v_WrPhaseProd_ID NUMBER;
carlos@0
    36
    --  Sequences to process
carlos@0
    37
    Cur_Sequence RECORD;
carlos@0
    38
    --
carlos@0
    39
    v_ProcessPlan_ID NUMBER;
carlos@0
    40
    v_ExplodePhases CHAR;
carlos@0
    41
    v_Quantity NUMBER;
carlos@0
    42
    v_Multiplier NUMBER;
carlos@0
    43
    v_Processed CHAR;
carlos@0
    44
    v_LaunchDate DATE;
carlos@0
    45
    -- WRPhases to process
carlos@0
    46
    Cur_WRPhase RECORD;
carlos@0
    47
    --  Sequence Products to process
carlos@0
    48
    Cur_SeqProduct RECORD;
carlos@0
    49
    --ptr_sp  Cur_SeqProduct%ROWTYPE;
carlos@0
    50
    FINISH_PROCESS BOOLEAN:=false;
carlos@0
    51
  BEGIN
carlos@0
    52
    --  Process Parameters
carlos@0
    53
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
    54
      --  Update AD_PInstance
carlos@0
    55
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
    56
      v_ResultStr:='PInstanceNotFound';
carlos@0
    57
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
    58
      --  Get Parameters
carlos@0
    59
      v_ResultStr:='ReadingParameters';
carlos@0
    60
      FOR Cur_Parameter IN
carlos@0
    61
        (SELECT i.Record_ID,
carlos@0
    62
          i.AD_User_ID,
carlos@0
    63
          i.AD_Org_ID,
carlos@0
    64
          i.AD_Client_ID,
carlos@0
    65
          p.ParameterName,
carlos@0
    66
          p.P_String,
carlos@0
    67
          p.P_Number,
carlos@0
    68
          p.P_Date
carlos@0
    69
        FROM AD_PInstance i
carlos@0
    70
        LEFT JOIN AD_PInstance_Para p
carlos@0
    71
          ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
    72
        WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
    73
        ORDER BY p.SeqNo
carlos@0
    74
        )
carlos@0
    75
      LOOP
carlos@0
    76
        v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
    77
        v_User_ID:=Cur_Parameter.AD_User_ID;
carlos@0
    78
        v_Org_ID:=Cur_Parameter.AD_Org_ID;
carlos@0
    79
        v_Client_ID:=Cur_Parameter.AD_Client_ID;
carlos@0
    80
      END LOOP; --  Get Parameter
carlos@0
    81
    ELSE
gorkaion@239
    82
      DBMS_OUTPUT.PUT_LINE('--<<MA_WorkRequirement_Process>>') ;
carlos@0
    83
    END IF;
carlos@0
    84
  BEGIN --BODY
carlos@0
    85
    -- Check if all the required fields are filled
carlos@0
    86
    v_ResultStr:='CheckingArguments';
carlos@0
    87
    SELECT MA_ProcessPlan_ID,
carlos@0
    88
      Quantity,
carlos@0
    89
      Processed,
carlos@0
    90
      ExplodePhases,
carlos@0
    91
      LaunchDate
carlos@0
    92
    INTO v_ProcessPlan_ID,
carlos@0
    93
      v_Quantity,
carlos@0
    94
      v_Processed,
carlos@0
    95
      v_ExplodePhases,
carlos@0
    96
      v_LaunchDate
carlos@0
    97
    FROM MA_WorkRequirement
carlos@0
    98
    WHERE MA_WorkRequirement_ID=v_Record_ID;
carlos@0
    99
    --Check if it hasn't already been processed
carlos@0
   100
    IF v_Processed='Y' THEN
carlos@0
   101
      v_ResultStr:='Work Requirement is Processed';
carlos@0
   102
      DBMS_OUTPUT.PUT_LINE('Work Requirement is already Processed') ;
carlos@0
   103
      v_Message:='@WorkRequirementProcessed@';
carlos@0
   104
      FINISH_PROCESS:=true;
carlos@0
   105
    END IF;
carlos@0
   106
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   107
      IF(v_ProcessPlan_ID IS NULL OR v_Quantity IS NULL) THEN
carlos@0
   108
        DBMS_OUTPUT.PUT_LINE('Faltan argumentos') ;
carlos@0
   109
        FINISH_PROCESS:=true;
carlos@0
   110
      END IF;
carlos@0
   111
    END IF; --FINISH_PROCESS
carlos@0
   112
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   113
      IF(v_ExplodePhases='Y') THEN
carlos@0
   114
        -- Data insertion begins
carlos@0
   115
        FOR Cur_Sequence IN
carlos@0
   116
          (SELECT s.MA_Sequence_ID,
carlos@0
   117
            s.MA_Process_ID,
carlos@0
   118
            s.Name,
carlos@0
   119
            s.CostCenterUse,
carlos@0
   120
            s.Preptime,
carlos@0
   121
            s.Multiplier,
carlos@0
   122
            s.SeqNo,
carlos@0
   123
            s.NoQty,
carlos@0
   124
            s.GroupUse,
carlos@0
   125
            s.Outsourced
carlos@0
   126
          FROM MA_Sequence s,
carlos@0
   127
            MA_ProcessPlan_Version p
carlos@0
   128
          WHERE s.MA_PROCESSPLAN_VERSION_ID=p.MA_PROCESSPLAN_VERSION_ID
carlos@0
   129
            AND p.MA_ProcessPlan_ID=v_ProcessPlan_ID
gorkaion@239
   130
            AND p.DATEFROM<=v_LaunchDate
gorkaion@239
   131
            AND p.DATETO>v_LaunchDate
carlos@0
   132
          )
carlos@0
   133
        LOOP
carlos@0
   134
          v_ResultStr:='InsertPhase';
carlos@0
   135
          Ad_Sequence_Next('MA_WRPhase', v_Client_ID, v_WrPhase_ID) ;
carlos@0
   136
          v_Multiplier:=Cur_Sequence.Multiplier;
carlos@0
   137
          --OPEN Cur_SeqProduct (Cur_Sequence.MA_Sequence_ID);
carlos@0
   138
          --FETCH Cur_SeqProduct INTO ptr_sp;
carlos@0
   139
          INSERT
carlos@0
   140
          INTO MA_WRPHASE
carlos@0
   141
            (
carlos@0
   142
              MA_Wrphase_ID, MA_Workrequirement_ID, MA_Sequence_ID, AD_Client_ID,
carlos@0
   143
              AD_Org_ID, Isactive, Created, Createdby,
carlos@0
   144
              Updated, Updatedby, MA_Process_ID, CostCenterUse,
carlos@0
   145
              Preptime, Quantity, Donequantity,
carlos@0
   146
              SeqNo, Closed, NoQty, GroupUse, Outsourced
carlos@0
   147
            )
carlos@0
   148
            VALUES
carlos@0
   149
            (
carlos@0
   150
              v_WrPhase_ID, v_Record_ID, Cur_Sequence.MA_Sequence_ID, v_Client_ID,
carlos@0
   151
              v_Org_ID, 'Y', now(), v_User_ID,
carlos@0
   152
              now(), v_User_ID, Cur_Sequence.MA_Process_ID, Cur_Sequence.CostCenterUse,
carlos@0
   153
              Cur_Sequence.Preptime, v_Multiplier*v_Quantity, 0,
carlos@0
   154
              Cur_Sequence.SeqNo, 'N', Cur_Sequence.NoQty, Cur_Sequence.GroupUse, Cur_Sequence.Outsourced
carlos@0
   155
            )
carlos@0
   156
            ;
carlos@0
   157
          FOR Cur_SeqProduct IN
carlos@0
   158
            (SELECT *
carlos@0
   159
            FROM MA_SequenceProduct
carlos@0
   160
            WHERE MA_Sequence_ID=Cur_Sequence.MA_Sequence_ID
carlos@0
   161
            )
carlos@0
   162
          LOOP
carlos@0
   163
            v_ResultStr:='InsertProduct';
carlos@0
   164
            Ad_Sequence_Next('MA_WRPhaseProduct', v_Client_ID, v_WrPhaseProd_ID) ;
carlos@0
   165
            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
   166
            v_ResultStr:='InsertProduct2';
carlos@0
   167
            INSERT
carlos@0
   168
            INTO MA_WRPHASEPRODUCT
carlos@0
   169
              (
carlos@0
   170
                MA_Wrphaseproduct_ID, MA_Wrphase_ID, AD_Client_ID, AD_Org_ID,
carlos@0
   171
                Isactive, Created, Createdby, Updated,
carlos@0
   172
                Updatedby, M_Product_ID, Productiontype, MovementQty,
carlos@0
   173
                ComponentCost, C_UOM_ID, consumeRM, decreaseInd,
carlos@0
   174
                quantityorder, M_Product_UOM_ID
carlos@0
   175
              )
carlos@0
   176
              VALUES
carlos@0
   177
              (
carlos@0
   178
                v_WrPhaseProd_ID, v_WrPhase_ID, v_Client_ID, v_Org_ID,
carlos@0
   179
                 'Y', now(), v_User_ID, now(),
carlos@0
   180
                v_User_ID, Cur_SeqProduct.M_Product_ID, Cur_SeqProduct.Productiontype, Cur_SeqProduct.Quantity,
carlos@0
   181
                Cur_SeqProduct.ComponentCost, Cur_SeqProduct.C_UOM_ID, Cur_SeqProduct.consumeRM, Cur_SeqProduct.decreaseInd,
carlos@0
   182
                Cur_SeqProduct.quantityorder, Cur_SeqProduct.M_Product_UOM_ID
carlos@0
   183
              )
carlos@0
   184
              ;
carlos@0
   185
          END LOOP;
carlos@0
   186
        END LOOP;
carlos@0
   187
      ELSE
carlos@0
   188
        -- Data insertion begins
carlos@0
   189
        FOR Cur_WRPhase IN
carlos@0
   190
          (SELECT MA_WRPhase_ID,
carlos@0
   191
            MA_Sequence_ID,
carlos@0
   192
            quantity
carlos@0
   193
          FROM MA_WRPhase
carlos@0
   194
          WHERE MA_WorkRequirement_ID=v_Record_ID
carlos@0
   195
          )
carlos@0
   196
        LOOP
carlos@0
   197
          FOR Cur_SeqProduct IN
carlos@0
   198
            (SELECT *
carlos@0
   199
            FROM MA_SequenceProduct
carlos@0
   200
            WHERE MA_Sequence_ID=Cur_WRPhase.MA_Sequence_ID
carlos@0
   201
            )
carlos@0
   202
          LOOP
carlos@0
   203
            v_ResultStr:='NInsertProduct';
carlos@0
   204
            Ad_Sequence_Next('MA_WRPhaseProduct', v_Client_ID, v_WrPhaseProd_ID) ;
carlos@0
   205
            --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
   206
            v_ResultStr:='NInsertProduct2';
carlos@0
   207
            INSERT
carlos@0
   208
            INTO MA_WRPHASEPRODUCT
carlos@0
   209
              (
carlos@0
   210
                MA_Wrphaseproduct_ID, MA_Wrphase_ID, AD_Client_ID, AD_Org_ID,
carlos@0
   211
                Isactive, Created, Createdby, Updated,
carlos@0
   212
                Updatedby, M_Product_ID, Productiontype, MovementQty,
carlos@0
   213
                ComponentCost, C_UOM_ID, consumeRM, decreaseInd,
carlos@0
   214
                QUANTITYORDER, M_PRODUCT_UOM_ID
carlos@0
   215
              )
carlos@0
   216
              VALUES
carlos@0
   217
              (
carlos@0
   218
                v_WrPhaseProd_ID, Cur_WRPhase.MA_WrPhase_ID, v_Client_ID, v_Org_ID,
carlos@0
   219
                 'Y', now(), v_User_ID, now(),
carlos@0
   220
                v_User_ID, Cur_SeqProduct.M_Product_ID, Cur_SeqProduct.Productiontype, Cur_SeqProduct.Quantity,
carlos@0
   221
                Cur_SeqProduct.ComponentCost, Cur_SeqProduct.C_UOM_ID, Cur_SeqProduct.consumeRM, Cur_SeqProduct.decreaseInd,
carlos@0
   222
                Cur_SeqProduct.quantityorder, Cur_SeqProduct.M_Product_UOM_ID
carlos@0
   223
              )
carlos@0
   224
              ;
carlos@0
   225
          END LOOP;
carlos@0
   226
        END LOOP;
carlos@0
   227
      END IF;
carlos@0
   228
    END IF; --FINISH_PROCESS
carlos@0
   229
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   230
      UPDATE MA_WorkRequirement
carlos@0
   231
        SET Processed='Y'
carlos@0
   232
      WHERE MA_WorkRequirement_ID=v_Record_ID;
carlos@0
   233
    END IF; --FINISH_PROCESS
gorkaion@239
   234
    --<<FINISH_PROCESS>>
carlos@0
   235
    --v_Message := v_Message || '@Created@: ' || v_NoRecords;
carlos@0
   236
    IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   237
      --  Update AD_PInstance
carlos@0
   238
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
carlos@0
   239
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 1, v_Message) ;
carlos@0
   240
    ELSE
gorkaion@239
   241
      DBMS_OUTPUT.PUT_LINE('--<<MA_WorkRequirement_Process finished>> ' || v_Message) ;
carlos@0
   242
    END IF;
carlos@0
   243
    RETURN;
carlos@0
   244
  END; --BODY
carlos@0
   245
EXCEPTION
carlos@0
   246
WHEN OTHERS THEN
carlos@0
   247
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   248
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   249
  IF(p_PInstance_ID IS NOT NULL) THEN
carlos@0
   250
    ROLLBACK;
carlos@0
   251
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   252
  ELSE
carlos@0
   253
    DECLARE
antonio@735
   254
      v_Code NUMBER:=SQLCODE;
carlos@0
   255
    BEGIN
carlos@0
   256
      INSERT
carlos@0
   257
      INTO DBA_ErrorLog
carlos@0
   258
        (
carlos@0
   259
          DBA_ErrorLog_ID, Created,
carlos@0
   260
          Code, Msg,
carlos@0
   261
          Info
carlos@0
   262
        )
carlos@0
   263
        VALUES
carlos@0
   264
        (
carlos@0
   265
          DBA_ErrorLog_Seq.NextVal, now(),
carlos@0
   266
          v_Code, v_ResultStr,
carlos@0
   267
           'MA_WorkRequirement_Post'
carlos@0
   268
        )
carlos@0
   269
        ;
carlos@0
   270
    END;
carlos@0
   271
  END IF;
carlos@0
   272
  --p_Invoice_ID := 0;        --  Error Indicator
carlos@0
   273
  RETURN;
antonio@735
   274
END MA_WORKREQUIREMENT_PROCESS
gorkaion@239
   275
]]></body>
carlos@0
   276
    </function>
adrian@94
   277
  </database>