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