src-db/database/model/functions/MA_STANDARD_COST_SEQUENCE.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Wed, 03 Sep 2008 17:55:37 +0000
changeset 1605 8a0fe0193bef
parent 756 ae11e4610537
child 1787 84217f37d788
permissions -rw-r--r--
Merge r2.5x intro trunk
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION MA_STANDARD_COST_SEQUENCE">
adrian@94
     3
    <function name="MA_STANDARD_COST_SEQUENCE" type="NULL">
juanpablo@1605
     4
      <parameter name="p_sequence_id" type="VARCHAR" mode="in">
antonio@735
     5
        <default/>
antonio@735
     6
      </parameter>
juanpablo@1605
     7
      <parameter name="p_budget_id" type="VARCHAR" mode="in">
antonio@735
     8
        <default/>
antonio@735
     9
      </parameter>
antonio@735
    10
      <parameter name="p_calcdate" type="TIMESTAMP" mode="in">
antonio@735
    11
        <default/>
antonio@735
    12
      </parameter>
juanpablo@1605
    13
      <parameter name="p_user" type="VARCHAR" mode="in">
antonio@735
    14
        <default/>
antonio@735
    15
      </parameter>
antonio@735
    16
      <parameter name="p_message" type="VARCHAR" mode="out">
antonio@735
    17
        <default/>
antonio@735
    18
      </parameter>
gorkaion@239
    19
      <body><![CDATA[/*************************************************************************
carlos@0
    20
 * The contents of this file are subject to the Openbravo  Public  License
carlos@0
    21
 * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
carlos@0
    22
 * Version 1.1  with a permitted attribution clause; you may not  use this
carlos@0
    23
 * file except in compliance with the License. You  may  obtain  a copy of
carlos@0
    24
 * the License at http://www.openbravo.com/legal/license.html
carlos@0
    25
 * Software distributed under the License  is  distributed  on  an "AS IS"
carlos@0
    26
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
carlos@0
    27
 * License for the specific  language  governing  rights  and  limitations
carlos@0
    28
 * under the License.
carlos@0
    29
 * The Original Code is Openbravo ERP.
carlos@0
    30
 * The Initial Developer of the Original Code is Openbravo SL
carlos@0
    31
 * All portions are Copyright (C) 2001-2006 Openbravo SL
carlos@0
    32
 * All Rights Reserved.
carlos@0
    33
 * Contributor(s):  ______________________________________.
carlos@0
    34
 ************************************************************************/
carlos@0
    35
  v_ResultStr             VARCHAR2(2000) := '';
antonio@735
    36
  v_Result NUMBER:= 1;
carlos@0
    37
carlos@0
    38
  v_CostCenterUse         NUMBER;
carlos@0
    39
  v_Multiplier            NUMBER;
carlos@0
    40
  v_Outsourced            CHAR(1);
carlos@0
    41
  v_OutsourcingCost       NUMBER;
carlos@0
    42
  v_CostCenter_Version    NUMBER;
carlos@0
    43
  v_ProcessPlan_Version   NUMBER;
carlos@0
    44
  v_SequenceCost          NUMBER;
carlos@0
    45
  v_SequenceCost_Tmp      NUMBER;
carlos@0
    46
  v_ProducedKg            NUMBER;
carlos@0
    47
  v_ProducedUd            NUMBER;
carlos@0
    48
  v_CompCostSum           NUMBER;
juanpablo@1605
    49
  v_ID                    varchar2(32);
carlos@0
    50
  v_No_Cost               BOOLEAN := TRUE;
carlos@0
    51
  v_Cost                  NUMBER;
carlos@0
    52
carlos@0
    53
  TYPE RECORD IS REF CURSOR;
carlos@0
    54
  Cur_UsedProducts RECORD;
carlos@0
    55
  Cur_IntermediateProduct_In RECORD;
carlos@0
    56
  Cur_IntermediateProduct_Out RECORD;
carlos@0
    57
  Cur_SequenceEmployee_In RECORD;
carlos@0
    58
  Cur_SequenceMachine_In RECORD;
carlos@0
    59
  Cur_SequenceIC_In RECORD;
carlos@0
    60
  Cur_SequencePIC_In RECORD;
carlos@0
    61
carlos@0
    62
BEGIN
carlos@0
    63
BEGIN --BODY
carlos@0
    64
carlos@0
    65
  v_ResultStr := 'Set sequence variables';
carlos@0
    66
  --DBMS_OUTPUT.PUT_LINE('Start MA_Sequence: ' || p_Sequence_ID);
carlos@0
    67
carlos@0
    68
  DELETE FROM MA_SEQUENCE_IC WHERE MA_SEQUENCE_ID = p_Sequence_ID;
carlos@0
    69
  DELETE FROM MA_SEQUENCE_EMPLOYEE WHERE MA_SEQUENCE_ID = p_Sequence_ID;
carlos@0
    70
  DELETE FROM MA_SEQUENCE_MACHINE WHERE MA_SEQUENCE_ID = p_Sequence_ID;
carlos@0
    71
carlos@0
    72
  SELECT s.CostCenterUse, s.Multiplier, s.outsourced, s.outsourcingcost, ccv.MA_CostCenter_Version_ID, s.MA_ProcessPlan_Version_ID,
carlos@0
    73
      SUM(CASE sp.productiontype WHEN '+' THEN (pd.weight*sp.quantity*COALESCE(sp.decrease,1)*COALESCE(sp.rejected,1))
carlos@0
    74
          ELSE 0 END),
carlos@0
    75
      SUM(CASE sp.productiontype WHEN '+' THEN (sp.quantity*COALESCE(sp.decrease,1)*COALESCE(sp.rejected,1))
carlos@0
    76
          ELSE 0 END),
carlos@0
    77
      SUM(CASE sp.productiontype
gorkaion@239
    78
          WHEN '+' THEN (CASE WHEN sp.quantity <> 0 THEN COALESCE(sp.ComponentCost,1) ELSE 0 END )
carlos@0
    79
          ELSE 0 END)
carlos@0
    80
    INTO v_CostCenterUse, v_Multiplier, v_Outsourced, v_OutsourcingCost, v_CostCenter_Version, v_ProcessPlan_Version,
carlos@0
    81
      v_ProducedKG, v_ProducedUd, v_CompCostSum
carlos@0
    82
  FROM MA_Sequence s, MA_SequenceProduct sp, M_Product pd,
carlos@0
    83
    MA_Process p LEFT JOIN (SELECT MA_CostCenter_ID, MA_CostCenter_version_ID
carlos@0
    84
                            FROM MA_CostCenter_Version
carlos@0
    85
                            WHERE ValidFrom = (SELECT max(ccv1.ValidFrom)
carlos@0
    86
                                               FROM MA_CostCenter_Version ccv1
carlos@0
    87
                                               WHERE MA_CostCenter_Version.MA_CostCenter_ID = ccv1.MA_CostCenter_ID
gorkaion@239
    88
                                                 AND ccv1.ValidFrom <= p_CalcDate)) ccv
carlos@0
    89
                 ON p.MA_COSTCENTER_ID = ccv.MA_CostCenter_ID
carlos@0
    90
  WHERE s.MA_Process_ID = p.MA_Process_ID
carlos@0
    91
    AND s.MA_Sequence_ID = sp.MA_Sequence_ID
carlos@0
    92
    aND pd.M_Product_ID = sp.M_Product_ID
carlos@0
    93
    AND s.MA_Sequence_ID = p_Sequence_ID
carlos@0
    94
  GROUP BY s.CostCenterUse, s.Multiplier, s.Outsourced, s.outsourcingcost, ccv.MA_CostCenter_Version_ID, s.MA_ProcessPlan_Version_ID;
carlos@0
    95
carlos@0
    96
  v_SequenceCost := 0;
carlos@0
    97
  v_SequenceCost_Tmp := 0;
carlos@0
    98
  v_ResultStr := 'Calculate used products cost.';
carlos@0
    99
  --Intermediate Products produced in same process plan
carlos@0
   100
  FOR Cur_UsedProducts IN (
carlos@0
   101
    SELECT MA_SEQUENCEPRODUCT.*, quantity*COALESCE(decrease,1)*COALESCE(rejected,1) as quantityfactor
carlos@0
   102
    FROM MA_SEQUENCEPRODUCT
carlos@0
   103
    WHERE MA_SEQUENCE_ID = p_Sequence_ID
carlos@0
   104
      AND PRODUCTIONTYPE = '-'
carlos@0
   105
  ) LOOP
carlos@0
   106
    -- Cost source order: 1. Same processplan, 2. Other processplan, 3. Purchase cost
carlos@0
   107
    -- Same processplan
carlos@0
   108
    v_No_Cost := TRUE;
carlos@0
   109
  FOR Cur_IntermediateProduct_In IN (
carlos@0
   110
      SELECT s.Calculated, s.MA_Sequence_ID, sp1.MA_SequenceProduct_ID, sp1.cost,
carlos@0
   111
        CASE ISDEFAULT WHEN 'Y' THEN 1 ELSE 2 END AS to_order
carlos@0
   112
      FROM MA_SequenceProduct sp1, MA_Sequence s
carlos@0
   113
    WHERE s.MA_Processplan_Version_ID = v_Processplan_Version
carlos@0
   114
      AND s.MA_Sequence_ID = sp1.MA_Sequence_ID
carlos@0
   115
      AND sp1.ProductionType = '+'
carlos@0
   116
        AND sp1.M_Product_ID = Cur_UsedProducts.M_Product_ID
carlos@0
   117
      ORDER BY to_order
carlos@0
   118
    )
carlos@0
   119
  LOOP
carlos@0
   120
    v_ResultStr := 'Calculate Sequence';
carlos@0
   121
    IF (Cur_IntermediateProduct_In.Calculated = 'N') THEN
gorkaion@239
   122
      DBMS_OUTPUT.PUT_LINE('Same PP -> MA_Sequence: ' || p_Sequence_ID);
carlos@0
   123
        MA_Standard_Cost_Sequence(Cur_IntermediateProduct_In.MA_Sequence_ID, p_Budget_ID, p_CalcDate, p_User, p_Message);
carlos@0
   124
    END IF;
carlos@0
   125
      v_SequenceCost_Tmp := COALESCE(v_SequenceCost_Tmp, 0) + Cur_UsedProducts.quantityfactor*Cur_IntermediateProduct_In.cost;
carlos@0
   126
carlos@0
   127
    UPDATE MA_SequenceProduct
carlos@0
   128
      SET COST = Cur_IntermediateProduct_In.cost
carlos@0
   129
      WHERE MA_SequenceProduct_ID = Cur_UsedProducts.MA_SequenceProduct_ID;
carlos@0
   130
      v_No_Cost := FALSE;
carlos@0
   131
      EXIT;
carlos@0
   132
  END LOOP;
carlos@0
   133
carlos@0
   134
    -- Other Process Plan
carlos@0
   135
    IF (v_No_Cost) THEN
carlos@0
   136
  FOR Cur_IntermediateProduct_Out IN (
carlos@0
   137
        SELECT s.Calculated, s.MA_Sequence_ID, sp2.MA_SequenceProduct_ID, ppv.datefrom, sp2.cost,
carlos@0
   138
          CASE s.ISDEFAULT WHEN 'Y' THEN 1 ELSE 2 END AS to_order
carlos@0
   139
        FROM M_Product p, MA_Processplan_Version ppv, MA_Sequence s, MA_SequenceProduct sp2
carlos@0
   140
        WHERE sp2.M_Product_ID = Cur_UsedProducts.M_Product_ID
carlos@0
   141
          AND p.M_Product_ID = Cur_UsedProducts.M_Product_ID
carlos@0
   142
      AND p.MA_ProcessPlan_ID = ppv. MA_ProcessPlan_ID
carlos@0
   143
      AND ppv.MA_ProcessPlan_Version_ID = s.MA_ProcessPlan_Version_ID
carlos@0
   144
      AND s.MA_Sequence_ID = sp2.MA_Sequence_ID
carlos@0
   145
          AND sp2.productiontype = '+'
gorkaion@239
   146
      AND ppv.DateFrom <= p_CalcDate
gorkaion@239
   147
      AND ppv.DateTo > p_CalcDate
carlos@0
   148
        ORDER BY to_order
carlos@0
   149
      ) LOOP
carlos@0
   150
    v_ResultStr := 'Calculate Sequence';
carlos@0
   151
    IF (Cur_IntermediateProduct_Out.Calculated = 'N') THEN
gorkaion@239
   152
      DBMS_OUTPUT.PUT_LINE('Different PP -> MA_Sequence: ' || p_Sequence_ID);
carlos@0
   153
          MA_Standard_Cost_Sequence(Cur_IntermediateProduct_Out.MA_Sequence_ID, p_Budget_ID, p_CalcDate, p_User, p_Message);
carlos@0
   154
    END IF;
carlos@0
   155
        v_SequenceCost_Tmp := COALESCE(v_SequenceCost_Tmp, 0) + Cur_UsedProducts.quantityfactor*Cur_IntermediateProduct_Out.cost;
carlos@0
   156
carlos@0
   157
    UPDATE MA_SequenceProduct
carlos@0
   158
        SET COST = Cur_IntermediateProduct_Out.cost
carlos@0
   159
        WHERE MA_SequenceProduct_ID = Cur_UsedProducts.MA_SequenceProduct_ID;
carlos@0
   160
        v_No_Cost := FALSE;
carlos@0
   161
        EXIT;
carlos@0
   162
  END LOOP;
carlos@0
   163
    END IF;
carlos@0
   164
carlos@0
   165
    -- Purchased raw material
carlos@0
   166
    IF (v_No_Cost) THEN
carlos@0
   167
      SELECT COALESCE(MAX(bl.Price),GET_PRODUCT_PRICE_DATE(MAX(p.AD_CLIENT_ID), MAX(p.M_PRODUCT_ID), p_CalcDate)) INTO v_Cost
carlos@0
   168
      FROM M_Product p LEFT JOIN C_BudgetLine bl ON p.M_Product_ID = bl.M_Product_ID
carlos@0
   169
                                                    AND bl.C_Budget_ID = p_Budget_ID
carlos@0
   170
      WHERE p.M_Product_ID = Cur_UsedProducts.M_Product_ID
carlos@0
   171
      AND p.ispurchased = 'Y';
carlos@0
   172
      v_SequenceCost_Tmp := COALESCE(v_SequenceCost_Tmp, 0) + Cur_UsedProducts.quantityfactor*v_Cost;
carlos@0
   173
carlos@0
   174
  UPDATE MA_SequenceProduct
carlos@0
   175
      SET COST = v_Cost
carlos@0
   176
      WHERE MA_SequenceProduct_ID = Cur_UsedProducts.MA_SequenceProduct_ID;
carlos@0
   177
      v_No_Cost := FALSE;
carlos@0
   178
    END IF;
carlos@0
   179
  END LOOP;
carlos@0
   180
carlos@0
   181
  v_SequenceCost := v_SequenceCost + COALESCE(v_SequenceCost_Tmp,0);
carlos@0
   182
  v_SequenceCost_Tmp := 0;
carlos@0
   183
carlos@0
   184
  IF (v_Outsourced = 'N') THEN
carlos@0
   185
    --CostCenter Cost
carlos@0
   186
    IF (v_CostCenter_Version IS NOT NULL) THEN
carlos@0
   187
    SELECT (CASE COSTUOM
carlos@0
   188
             WHEN 'H' THEN v_CostCenterUse*Cost
carlos@0
   189
             WHEN 'K' THEN v_ProducedKg*Cost
carlos@0
   190
             WHEN 'U' THEN v_ProducedUd*Cost
carlos@0
   191
           END) INTO v_SequenceCost_Tmp
carlos@0
   192
    FROM MA_CostCenter_Version
carlos@0
   193
    WHERE MA_CostCenter_Version_ID = v_CostCenter_Version;
carlos@0
   194
carlos@0
   195
    UPDATE MA_SEQUENCE
carlos@0
   196
    SET COSTCENTERCOST = v_SequenceCost_Tmp
carlos@0
   197
    WHERE MA_SEQUENCE_ID = p_Sequence_ID;
carlos@0
   198
carlos@0
   199
    v_SequenceCost := v_SequenceCost + COALESCE(v_SequenceCost_Tmp,0);
carlos@0
   200
    v_SequenceCost_Tmp := 0;
carlos@0
   201
carlos@0
   202
    --Employees cost
carlos@0
   203
    FOR Cur_SequenceEmployee_In IN (
carlos@0
   204
      SELECT cce.C_Salary_Category_ID, MIN(cce.AD_Client_ID) AS AD_Client_ID,
carlos@0
   205
                MIN(cce.AD_ORG_ID) AS AD_ORG_ID,
carlos@0
   206
                SUM(COALESCE((CASE scc.costuom
carlos@0
   207
                           WHEN 'H' THEN (v_CostCenterUse*scc.COST*cce.quantity)
carlos@0
   208
                           WHEN 'K' THEN (v_ProducedKg*scc.COST*cce.quantity)
carlos@0
   209
                           WHEN 'U' THEN (v_ProducedUd*scc.COST*cce.quantity)
carlos@0
   210
                         END), 0)) AS CalcCost
carlos@0
   211
      FROM MA_CostCenter_Employee cce, C_Salary_Category_Cost scc
carlos@0
   212
      WHERE cce.C_Salary_Category_ID = scc.C_Salary_Category_ID
carlos@0
   213
        AND MA_CostCenter_Version_ID = v_CostCenter_Version
carlos@0
   214
        AND scc.Datefrom = (SELECT max(scc1.datefrom)
carlos@0
   215
                            FROM C_Salary_Category_Cost scc1
carlos@0
   216
                            WHERE scc1.C_Salary_Category_ID = cce.C_Salary_Category_ID
gorkaion@239
   217
                              AND scc1.datefrom < p_CalcDate)
carlos@0
   218
      GROUP BY cce.C_Salary_Category_ID
carlos@0
   219
      )
carlos@0
   220
    LOOP
carlos@0
   221
      v_ResultStr := 'Calculate Sequence Employee cost';
carlos@0
   222
      AD_Sequence_Next('MA_Sequence_Employee', Cur_SequenceEmployee_In.AD_Client_ID, v_ID);
carlos@0
   223
carlos@0
   224
      v_SequenceCost_Tmp := COALESCE(Cur_SequenceEmployee_In.CalcCost, 0);
carlos@0
   225
carlos@0
   226
      INSERT INTO MA_Sequence_Employee (MA_SEQUENCE_EMPLOYEE_ID, AD_CLIENT_ID, AD_ORG_ID,
carlos@0
   227
        ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY, MA_SEQUENCE_ID,
carlos@0
   228
        C_SALARY_CATEGORY_ID, CALCCOST)
carlos@0
   229
      VALUES (v_ID, Cur_SequenceEmployee_In.AD_Client_ID, Cur_SequenceEmployee_In.AD_Org_ID,
antonio@735
   230
        'Y', now(), p_User, now(), p_User, p_Sequence_ID,
carlos@0
   231
        Cur_SequenceEmployee_In.C_Salary_Category_ID, v_SequenceCost_Tmp);
carlos@0
   232
carlos@0
   233
      v_SequenceCost := v_SequenceCost + COALESCE(v_SequenceCost_Tmp, 0);
carlos@0
   234
    END LOOP;
carlos@0
   235
carlos@0
   236
    v_SequenceCost_Tmp := 0;
carlos@0
   237
carlos@0
   238
    --Machines cost
carlos@0
   239
    FOR Cur_SequenceMachine_In IN (
carlos@0
   240
      SELECT ccm.MA_Machine_ID, MIN(ccm.AD_Client_ID) AS AD_Client_ID,
carlos@0
   241
        MIN(ccm.AD_Org_ID) AS AD_Org_ID,
carlos@0
   242
        SUM(COALESCE((CASE mc.costuom
carlos@0
   243
                         WHEN 'H' THEN (v_CostCenterUse*mc.COST*ccm.use)
carlos@0
   244
                         WHEN 'K' THEN (v_ProducedKg*mc.COST*ccm.use)
carlos@0
   245
                         WHEN 'U' THEN (v_ProducedUd*mc.COST*ccm.use)
carlos@0
   246
                         END), 0)) AS CalcCost
carlos@0
   247
      FROM MA_CostCenter_Machine ccm, MA_Machine_Cost mc
carlos@0
   248
      WHERE ccm.MA_Machine_ID = mc.MA_Machine_ID
carlos@0
   249
        AND MA_CostCenter_Version_ID = v_CostCenter_Version
carlos@0
   250
        AND mc.validfrom = (SELECT max(mc1.validfrom)
carlos@0
   251
                            FROM MA_Machine_Cost mc1
carlos@0
   252
                            WHERE mc1.MA_Machine_ID = ccm.MA_Machine_ID
gorkaion@239
   253
                              AND mc1.validfrom < p_CalcDate)
carlos@0
   254
      GROUP BY ccm.MA_Machine_ID
carlos@0
   255
      )
carlos@0
   256
    LOOP
carlos@0
   257
      v_ResultStr := 'Calculate Sequence Machine cost';
carlos@0
   258
      AD_Sequence_Next('MA_Sequence_Machine', Cur_SequenceMachine_In.AD_Client_ID, v_ID);
carlos@0
   259
carlos@0
   260
      v_SequenceCost_Tmp := COALESCE(Cur_SequenceMachine_In.CalcCost, 0);
carlos@0
   261
carlos@0
   262
      INSERT INTO MA_Sequence_Machine (MA_SEQUENCE_MACHINE_ID, AD_CLIENT_ID, AD_ORG_ID,
carlos@0
   263
        ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY, MA_SEQUENCE_ID,
carlos@0
   264
        MA_MACHINE_ID, CALCCOST)
carlos@0
   265
      VALUES (v_ID, Cur_SequenceMachine_In.AD_Client_ID, Cur_SequenceMachine_In.AD_Org_ID,
antonio@735
   266
        'Y', now(), p_User, now(), p_User, p_Sequence_ID,
carlos@0
   267
        Cur_SequenceMachine_In.MA_Machine_ID, v_SequenceCost_Tmp);
carlos@0
   268
carlos@0
   269
      v_SequenceCost := v_SequenceCost + COALESCE(v_SequenceCost_Tmp, 0);
carlos@0
   270
    END LOOP;
carlos@0
   271
carlos@0
   272
    v_SequenceCost_Tmp := 0;
carlos@0
   273
carlos@0
   274
    --Indirect costs cost
carlos@0
   275
    FOR Cur_SequenceIC_In IN (
carlos@0
   276
      SELECT ccic.MA_Indirect_Cost_ID, MIN(ccic.AD_Client_ID) AS AD_Client_ID,
carlos@0
   277
            MIN(ccic.AD_Org_ID) AS AD_Org_ID,
carlos@0
   278
            SUM(CASE COST_UOM
carlos@0
   279
               WHEN 'H' THEN icv.COST*v_CostCenterUse
carlos@0
   280
               WHEN 'K' THEN icv.COST*v_ProducedKg
carlos@0
   281
               WHEN 'U' THEN icv.COST*v_ProducedUd
carlos@0
   282
               END) AS CalcCost
carlos@0
   283
      FROM MA_CostCenter_IC ccic, MA_Indirect_Cost_Value icv, MA_Indirect_Cost ic
carlos@0
   284
      WHERE ccic.MA_Indirect_Cost_ID = ic.MA_Indirect_Cost_ID
carlos@0
   285
        AND ic.MA_Indirect_Cost_ID = icv.MA_Indirect_Cost_ID
carlos@0
   286
        AND ccic.MA_CostCenter_Version_ID = v_CostCenter_Version
gorkaion@239
   287
        AND icv.datefrom < p_CalcDate
gorkaion@239
   288
        AND icv.dateto > p_CalcDate
carlos@0
   289
        AND ic.Cost_Type = 'P'
carlos@0
   290
      GROUP BY ccic.MA_Indirect_Cost_ID
carlos@0
   291
      )
carlos@0
   292
    LOOP
carlos@0
   293
      v_ResultStr := 'Calculate Sequence Indirect cost';
carlos@0
   294
      AD_Sequence_Next('MA_Sequence_IC', Cur_SequenceIC_In.AD_Client_ID, v_ID);
carlos@0
   295
carlos@0
   296
      v_SequenceCost_Tmp := COALESCE(Cur_SequenceIC_In.CalcCost, 0);
carlos@0
   297
carlos@0
   298
      INSERT INTO MA_Sequence_IC (MA_SEQUENCE_IC_ID, AD_CLIENT_ID, AD_ORG_ID,
carlos@0
   299
        ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY, MA_SEQUENCE_ID,
carlos@0
   300
        MA_INDIRECT_COST_ID, CALCCOST)
carlos@0
   301
      VALUES (v_ID, Cur_SequenceIC_In.AD_Client_ID, Cur_SequenceIC_In.AD_Org_ID,
antonio@735
   302
        'Y', now(), p_User, now(), p_User, p_Sequence_ID,
carlos@0
   303
        Cur_SequenceIC_In.MA_INDIRECT_COST_ID, v_SequenceCost_Tmp);
carlos@0
   304
carlos@0
   305
      v_SequenceCost := v_SequenceCost + COALESCE(v_SequenceCost_Tmp, 0);
carlos@0
   306
    END LOOP;
carlos@0
   307
carlos@0
   308
    v_SequenceCost_Tmp := 0;
carlos@0
   309
carlos@0
   310
    --Calculate porcentual indirect costs
carlos@0
   311
    v_ResultStr := 'Costs: porcentual indirect cost';
carlos@0
   312
    FOR Cur_SequencePIC_In IN (
carlos@0
   313
      SELECT ccic.MA_Indirect_Cost_ID, MIN(ccic.AD_Client_ID) AS AD_Client_ID,
carlos@0
   314
              MIN(ccic.AD_Org_ID) AS AD_Org_ID,
carlos@0
   315
              SUM(v_SequenceCost*icv.cost) AS CalcCost
carlos@0
   316
      FROM MA_CostCenter_IC ccic, MA_Indirect_Cost_Value icv
carlos@0
   317
      WHERE ccic.MA_Costcenter_Version_ID = v_CostCenter_Version
carlos@0
   318
        AND ccic.MA_Indirect_Cost_ID = icv.MA_Indirect_Cost_ID
carlos@0
   319
        AND icv.COST_UOM = 'P'
gorkaion@239
   320
        AND icv.DATEFROM < p_CalcDate
gorkaion@239
   321
        AND icv.DATETO > p_CalcDate
carlos@0
   322
      GROUP BY ccic.MA_Indirect_Cost_ID
carlos@0
   323
      )
carlos@0
   324
    LOOP
carlos@0
   325
      v_ResultStr := 'Calculate Sequence porcentual Indirect cost';
carlos@0
   326
      AD_Sequence_Next('MA_Sequence_IC', Cur_SequencePIC_In.AD_Client_ID, v_ID);
carlos@0
   327
carlos@0
   328
      v_SequenceCost_Tmp := COALESCE(Cur_SequencePIC_In.CalcCost, 0);
carlos@0
   329
carlos@0
   330
      INSERT INTO MA_Sequence_IC (MA_SEQUENCE_IC_ID, AD_CLIENT_ID, AD_ORG_ID,
carlos@0
   331
        ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY, MA_SEQUENCE_ID,
carlos@0
   332
        MA_INDIRECT_COST_ID, CALCCOST)
carlos@0
   333
      VALUES (v_ID, Cur_SequencePIC_In.AD_Client_ID, Cur_SequencePIC_In.AD_Org_ID,
antonio@735
   334
        'Y', now(), p_User, now(), p_User, p_Sequence_ID,
carlos@0
   335
        Cur_SequencePIC_In.MA_INDIRECT_COST_ID, v_SequenceCost_Tmp);
carlos@0
   336
carlos@0
   337
      v_SequenceCost := v_SequenceCost + COALESCE(v_SequenceCost_Tmp, 0);
carlos@0
   338
    END LOOP;
carlos@0
   339
carlos@0
   340
    v_SequenceCost_Tmp := 0;
carlos@0
   341
  ELSE
carlos@0
   342
      SELECT '@theSequence@ ' || MA_PROCESSPLAN.NAME || ' - ' || MA_PROCESSPLAN_VERSION.DOCUMENTNO || ' - ' || MA_SEQUENCE.NAME || ' @notCostCenterforDate@' INTO p_Message
carlos@0
   343
      FROM MA_SEQUENCE, MA_PROCESSPLAN_VERSION, MA_PROCESSPLAN
carlos@0
   344
      WHERE MA_PROCESSPLAN.MA_PROCESSPLAN_ID = MA_PROCESSPLAN_VERSION.MA_PROCESSPLAN_ID
carlos@0
   345
        AND MA_PROCESSPLAN_VERSION.MA_PROCESSPLAN_VERSION_ID = MA_SEQUENCE.MA_PROCESSPLAN_VERSION_ID
carlos@0
   346
        AND MA_SEQUENCE.MA_SEQUENCE_ID = p_Sequence_ID
carlos@0
   347
        AND MA_PROCESSPLAN_VERSION.MA_PROCESSPLAN_VERSION_ID = v_ProcessPlan_Version;
carlos@0
   348
    END IF;
carlos@0
   349
  ELSE
carlos@0
   350
    v_SequenceCost := v_SequenceCost + COALESCE(v_OutsourcingCost,0);
carlos@0
   351
  END IF;
carlos@0
   352
carlos@0
   353
  v_ResultStr := 'Update produced products cost: ' || v_SequenceCost || ' component cost total: ' || v_CompCostSum;
carlos@0
   354
  --Update produced products cost
carlos@0
   355
  UPDATE MA_SequenceProduct
carlos@0
   356
  SET cost = ROUND(((v_SequenceCost*COALESCE(componentcost,1)*COALESCE(rejected,1)*COALESCE(decrease,1))/
carlos@0
   357
              (v_CompCostSum*quantity)),4),
carlos@0
   358
      UPDATED = now()
carlos@0
   359
  WHERE productiontype = '+'
gorkaion@239
   360
    AND quantity <> 0
carlos@0
   361
    AND MA_Sequence_ID = p_Sequence_ID;
carlos@0
   362
carlos@0
   363
  --Set sequence as calculated
carlos@0
   364
  UPDATE MA_Sequence
carlos@0
   365
  SET calculated = 'Y',
carlos@0
   366
      updated = p_CalcDate,
carlos@0
   367
      updatedby = p_User
carlos@0
   368
  WHERE MA_Sequence_ID = p_Sequence_ID;
carlos@0
   369
carlos@0
   370
   --DBMS_OUTPUT.PUT_LINE('Finish MA_Sequence: ' || p_Sequence_ID || ' cost: ' || v_SequenceCost);
carlos@0
   371
carlos@0
   372
END; --BODY
carlos@0
   373
EXCEPTION
carlos@0
   374
  WHEN OTHERS THEN
carlos@0
   375
    DBMS_OUTPUT.PUT_LINE('ERROR MA_Standard_Cost_Sequence, sequence_ID '|| p_Sequence_ID || ', date ' || p_CalcDate || ' at ' ||v_ResultStr);
carlos@0
   376
    RAISE;
antonio@735
   377
END MA_STANDARD_COST_SEQUENCE
gorkaion@239
   378
]]></body>
adrian@94
   379
    </function>
adrian@94
   380
  </database>