src-db/database/model/functions/MA_COPY_VERSION.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Wed, 03 Sep 2008 17:55:37 +0000
changeset 1605 8a0fe0193bef
parent 1132 8b7c81074e6e
child 3545 adb69eb2ec61
permissions -rw-r--r--
Merge r2.5x intro trunk
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION MA_COPY_VERSION">
adrian@94
     3
    <function name="MA_COPY_VERSION" type="NULL">
juanpablo@1605
     4
      <parameter name="p_pinstance_id" type="VARCHAR" 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):='';
juanpablo@1605
    26
  v_Record_ID VARCHAR2(32);
carlos@0
    27
  v_Result NUMBER:=1; --  Success
carlos@0
    28
  --  Parameter
carlos@0
    29
  TYPE RECORD IS REF CURSOR;
carlos@0
    30
    Cur_Parameter RECORD;
carlos@0
    31
    --  Record Info
carlos@0
    32
    v_Version NUMBER;
juanpablo@1605
    33
    v_ProcessPlanVersion_ID VARCHAR2(32);
juanpablo@1605
    34
    v_Client_ID VARCHAR2(32);
juanpablo@1605
    35
    v_Org_ID VARCHAR2(32);
juanpablo@1605
    36
    v_User_ID VARCHAR2(32);
carlos@0
    37
    v_UpdatedBy NUMBER;
carlos@0
    38
    v_DocumentNo NUMBER;
juanpablo@1605
    39
    v_Sequence_ID VARCHAR2(32);
juanpablo@1605
    40
    v_Product_ID VARCHAR2(32);
gorkaion@1132
    41
    v_DateFrom DATE :=now();
gorkaion@1132
    42
    v_DateTo DATE :=TO_DATE('31-12-9999', 'DD-MM-YYYY');
gorkaion@1132
    43
    v_Count NUMBER;
carlos@0
    44
    --  v_Processing        CHAR;
carlos@0
    45
    --  v_Processed         CHAR;
carlos@0
    46
    Cur_Sequence RECORD;
carlos@0
    47
    Cur_Product RECORD;
carlos@0
    48
  BEGIN
carlos@0
    49
    v_Result:=1;
carlos@0
    50
    --  Get Parameters
carlos@0
    51
    v_ResultStr:='ReadingParameters';
carlos@0
    52
    FOR Cur_Parameter IN
carlos@0
    53
      (SELECT i.Record_ID,
carlos@0
    54
        i.AD_User_ID,
carlos@0
    55
        p.ParameterName,
carlos@0
    56
        p.P_String,
carlos@0
    57
        p.P_Number,
carlos@0
    58
        p.P_Date,
carlos@0
    59
        p.AD_Org_ID,
carlos@0
    60
        p.AD_Client_ID
carlos@0
    61
      FROM AD_PInstance i
carlos@0
    62
      LEFT JOIN AD_PInstance_Para p
carlos@0
    63
        ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
    64
      WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
    65
      ORDER BY p.SeqNo
carlos@0
    66
      )
carlos@0
    67
    LOOP
carlos@0
    68
      v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
    69
      v_User_ID:=Cur_Parameter.AD_User_ID;
carlos@0
    70
      v_Org_ID:=Cur_Parameter.AD_Org_ID;
carlos@0
    71
      v_Client_ID:=Cur_Parameter.AD_Client_ID;
carlos@0
    72
      IF(Cur_Parameter.ParameterName='MA_Processplan_Version_ID') THEN
carlos@0
    73
        v_Version:=Cur_Parameter.P_Number;
carlos@0
    74
        DBMS_OUTPUT.PUT_LINE('  Version=' || v_Version) ;
carlos@0
    75
      END IF;
carlos@0
    76
    END LOOP; --  Get Parameter
carlos@0
    77
    DBMS_OUTPUT.PUT_LINE('  v_Record_ID=' || v_Record_ID) ;
carlos@0
    78
    v_ResultStr:='UpdateCurrentVersion';
gorkaion@1132
    79
    SELECT count(*) INTO v_Count
gorkaion@1132
    80
    FROM MA_ProcessPlan_Version
gorkaion@1132
    81
    WHERE MA_Processplan_ID = v_Record_ID
gorkaion@1132
    82
      AND dateto>=v_DateFrom
gorkaion@1132
    83
      AND datefrom<=v_DateFrom;
gorkaion@1132
    84
    IF (v_count >= 0) THEN
gorkaion@1132
    85
      UPDATE MA_Processplan_Version
gorkaion@1132
    86
        SET dateto=v_DateFrom
gorkaion@1132
    87
      WHERE MA_Processplan_ID=v_Record_ID
gorkaion@1132
    88
        AND dateto>=v_DateFrom
gorkaion@1132
    89
        AND datefrom<=v_DateFrom;
gorkaion@1132
    90
    END IF;
gorkaion@1132
    91
    SELECT count(*) INTO v_Count
gorkaion@1132
    92
    FROM MA_Processplan_Version
gorkaion@1132
    93
    WHERE MA_Processplan_ID = v_Record_ID
gorkaion@1132
    94
      AND datefrom > v_dateFrom;
gorkaion@1132
    95
    IF (v_Count > 0) THEN
gorkaion@1132
    96
      SELECT min(datefrom) INTO v_DateTo
gorkaion@1132
    97
      FROM MA_Processplan_version
gorkaion@1132
    98
      WHERE MA_Processplan_ID = v_Record_ID
gorkaion@1132
    99
        AND datefrom > v_DateFrom;
gorkaion@1132
   100
    END IF;
carlos@0
   101
    v_ResultStr:='InsertVersion';
carlos@0
   102
    Ad_Sequence_Next('MA_ProcessPlan_Version', v_Client_ID, v_ProcessPlanVersion_ID) ;
carlos@0
   103
    Ad_Sequence_Doc('DocumentNo_MA_ProcessPlan_Version', v_Client_ID, 'Y', v_DocumentNo) ;
carlos@0
   104
    INSERT
carlos@0
   105
    INTO MA_Processplan_Version
carlos@0
   106
      (
carlos@0
   107
        MA_Processplan_Version_ID, AD_Client_ID, AD_Org_ID, Isactive,
carlos@0
   108
        Created, Createdby, Updated, Updatedby,
carlos@0
   109
        MA_Processplan_ID, DocumentNo, Datefrom, Dateto
carlos@0
   110
      )
carlos@0
   111
      VALUES
carlos@0
   112
      (
carlos@0
   113
        v_ProcessPlanVersion_ID, v_Client_ID, v_Org_ID, 'Y',
juanpablo@1605
   114
        now(), COALESCE(v_User_ID, '0'), now(), COALESCE(v_User_ID, '0'),
gorkaion@1132
   115
        v_Record_ID, v_DocumentNo, v_DateFrom, v_DateTo
carlos@0
   116
      )
carlos@0
   117
      ;
carlos@0
   118
    FOR Cur_Sequence IN
carlos@0
   119
      (SELECT *  FROM MA_Sequence  WHERE MA_Processplan_Version_ID=v_Version)
carlos@0
   120
    LOOP
carlos@0
   121
      v_ResultStr:='InsertSequence';
carlos@0
   122
      Ad_Sequence_Next('MA_Sequence', v_Client_ID, v_Sequence_ID) ;
carlos@0
   123
      INSERT
carlos@0
   124
      INTO MA_Sequence
carlos@0
   125
        (
carlos@0
   126
          MA_Sequence_ID, AD_Client_ID, AD_Org_ID, Isactive,
carlos@0
   127
          Created, Createdby, Updated, Updatedby,
carlos@0
   128
          MA_Processplan_Version_ID, MA_Process_ID, Value, Name,
carlos@0
   129
          Description, CostCenterUse, Preptime, Multiplier,
carlos@0
   130
          Seqno, Noqty, Groupuse, Calculated
carlos@0
   131
        )
carlos@0
   132
        VALUES
carlos@0
   133
        (
carlos@0
   134
          v_Sequence_ID, v_Client_ID, v_Org_ID, 'Y',
juanpablo@1605
   135
          now(), COALESCE(v_User_ID, '0'), now(), COALESCE(v_User_ID, '0'),
carlos@0
   136
          v_ProcessPlanVersion_ID, Cur_Sequence.MA_Process_ID, Cur_Sequence.Value, Cur_Sequence.Name,
carlos@0
   137
          Cur_Sequence.Description, Cur_Sequence.CostCenterUse, Cur_Sequence.Preptime, Cur_Sequence.Multiplier,
carlos@0
   138
          Cur_Sequence.Seqno, Cur_Sequence.Noqty, Cur_Sequence.Groupuse, Cur_Sequence.Calculated
carlos@0
   139
        )
carlos@0
   140
        ;
carlos@0
   141
      FOR Cur_Product IN
carlos@0
   142
        (SELECT *
carlos@0
   143
        FROM MA_SequenceProduct
carlos@0
   144
        WHERE MA_Sequence_ID=Cur_Sequence.MA_Sequence_ID
carlos@0
   145
        )
carlos@0
   146
      LOOP
carlos@0
   147
        v_ResultStr:='InsertProducts';
carlos@0
   148
        Ad_Sequence_Next('MA_SequenceProduct', v_Client_ID, v_Product_ID) ;
carlos@0
   149
        INSERT
carlos@0
   150
        INTO MA_SequenceProduct
carlos@0
   151
          (
carlos@0
   152
            MA_Sequenceproduct_ID, AD_Client_ID, AD_Org_ID, Isactive,
carlos@0
   153
            Created, Createdby, Updated, Updatedby,
carlos@0
   154
            MA_Sequence_ID, M_Product_ID, Quantity, Productiontype,
carlos@0
   155
            C_Uom_ID, Quantityorder, M_Product_Uom_ID, Componentcost,
asier@799
   156
            Decrease, Rejected
carlos@0
   157
          )
carlos@0
   158
          VALUES
carlos@0
   159
          (
carlos@0
   160
            v_Product_ID, v_Client_ID, v_Org_ID, 'Y',
juanpablo@1605
   161
            now(), COALESCE(v_User_ID, '0'), now(), COALESCE(v_User_ID, '0'),
carlos@0
   162
            v_Sequence_ID, Cur_Product.M_Product_ID, Cur_Product.Quantity, Cur_Product.Productiontype,
carlos@0
   163
            Cur_Product.C_Uom_ID, Cur_Product.Quantityorder, Cur_Product.M_Product_Uom_ID, Cur_Product.Componentcost,
asier@799
   164
            Cur_Product.Decrease, Cur_Product.Rejected
carlos@0
   165
          )
carlos@0
   166
          ;
carlos@0
   167
      END LOOP;
carlos@0
   168
    END LOOP;
gorkaion@239
   169
    --<<FINISH_PROCESS>>
carlos@0
   170
    --  Update AD_PInstance
carlos@0
   171
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished - ' || v_Message) ;
carlos@0
   172
    AD_UPDATE_PINSTANCE(p_PInstance_ID, v_User_ID, 'N', v_Result, v_Message) ;
carlos@0
   173
    RETURN;
carlos@0
   174
EXCEPTION
carlos@0
   175
  WHEN OTHERS THEN
carlos@0
   176
    v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   177
    DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   178
    ROLLBACK;
carlos@0
   179
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   180
    RETURN;
antonio@735
   181
END MA_COPY_VERSION
gorkaion@239
   182
]]></body>
adrian@94
   183
    </function>
adrian@94
   184
  </database>