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