database/model/functions/M_INOUT_UPDATEQUANTITYES.xml
author Adrián Romero <adrian.romero@openbravo.com>
Tue, 20 Nov 2007 16:27:14 +0000
changeset 94 4e7ba7b8a4ad
parent 0 0247c26f10c6
child 132 33c5ca45fcff
permissions -rw-r--r--
fix: Changed check constraint names and foreign key names to fit in to
30 characteres.
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION M_INOUT_UPDATEQUANTITYES">
adrian@94
     3
    <function name="M_INOUT_UPDATEQUANTITYES" type="NULL">
adrian@94
     4
      <parameter name="p_PInstance_ID" type="NUMERIC" mode="in"/>
adrian@94
     5
      <body>
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
  v_ResultStr VARCHAR2(2000):='';
carlos@0
    23
  v_Message VARCHAR2(2000):='';
carlos@0
    24
  v_Record_ID NUMBER;
carlos@0
    25
  v_Result NUMBER:=0; -- failure
carlos@0
    26
  v_AD_User_ID NUMBER;
carlos@0
    27
  v_AD_Org_ID NUMBER;
carlos@0
    28
  v_AD_Client_ID NUMBER;
carlos@0
    29
  -- Parameter
carlos@0
    30
  TYPE RECORD IS REF CURSOR;
carlos@0
    31
    Cur_Parameter RECORD;
carlos@0
    32
    -- Parameter Variables
carlos@0
    33
    v_M_InOut_ID NUMBER;
carlos@0
    34
    v_Total NUMBER;
carlos@0
    35
    FINISH_PROCESS BOOLEAN:=false;
carlos@0
    36
  BEGIN
carlos@0
    37
    --  Update AD_PInstance
carlos@0
    38
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
    39
    v_ResultStr:='PInstanceNotFound';
carlos@0
    40
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
    41
  BEGIN --BODY
carlos@0
    42
    -- Get Parameters
carlos@0
    43
    v_ResultStr:='ReadingParameters';
carlos@0
    44
    FOR Cur_Parameter IN
carlos@0
    45
      (SELECT i.Record_ID,
carlos@0
    46
        i.AD_USER_ID,
carlos@0
    47
        i.AD_ORG_ID,
carlos@0
    48
        i.AD_CLIENT_ID,
carlos@0
    49
        p.ParameterName,
carlos@0
    50
        p.P_String,
carlos@0
    51
        p.P_Number,
carlos@0
    52
        p.P_Date
carlos@0
    53
      FROM AD_PInstance i
carlos@0
    54
      LEFT JOIN AD_PInstance_Para p
carlos@0
    55
        ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
    56
      WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
    57
      ORDER BY p.SeqNo
carlos@0
    58
      )
carlos@0
    59
    LOOP
carlos@0
    60
      v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
    61
      v_AD_User_ID:=Cur_Parameter.AD_User_ID;
carlos@0
    62
      v_AD_Org_ID:=Cur_Parameter.AD_Org_ID;
carlos@0
    63
      v_AD_Client_ID:=Cur_Parameter.AD_Client_ID;
carlos@0
    64
      IF(Cur_Parameter.ParameterName='M_InOut_ID') THEN
carlos@0
    65
        v_M_InOut_ID:=Cur_Parameter.P_Number;
carlos@0
    66
        DBMS_OUTPUT.PUT_LINE('  M_InOut_ID=' || v_M_InOut_ID) ;
carlos@0
    67
      ELSE
carlos@0
    68
        DBMS_OUTPUT.PUT_LINE('*** Unknown Parameter=' || Cur_Parameter.ParameterName) ;
carlos@0
    69
      END IF;
carlos@0
    70
    END LOOP; -- Get Parameter
carlos@0
    71
    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
carlos@0
    72
    v_ResultStr:='CheckRestriction';
carlos@0
    73
    SELECT COUNT(*)
carlos@0
    74
    INTO v_Total
carlos@0
    75
    FROM
carlos@0
    76
      (SELECT M_PRODUCT_ID,
carlos@0
    77
        C_UOM_ID,
carlos@0
    78
        ROUND(MOVEMENTQTY, 2),
carlos@0
    79
        COALESCE(M_PRODUCT_UOM_ID, 0) AS M_PRODUCT_UOM_ID,
carlos@0
    80
        COALESCE(QUANTITYORDER, 0) AS QUANTITYORDER,
carlos@0
    81
        COUNT(*)
carlos@0
    82
      FROM M_INOUTLINE
carlos@0
    83
      WHERE M_INOUT_ID=v_M_InOut_ID
carlos@0
    84
        AND ISACTIVE='Y'
carlos@0
    85
      GROUP BY M_PRODUCT_ID,
carlos@0
    86
        C_UOM_ID,
carlos@0
    87
        MOVEMENTQTY,
carlos@0
    88
        COALESCE(M_PRODUCT_UOM_ID, 0),
carlos@0
    89
        COALESCE(QUANTITYORDER, 0) MINUS
carlos@0
    90
      SELECT M_PRODUCT_ID,
carlos@0
    91
        C_UOM_ID,
carlos@0
    92
        ROUND(MOVEMENTQTY, 2),
carlos@0
    93
        COALESCE(M_PRODUCT_UOM_ID, 0) AS M_PRODUCT_UOM_ID,
carlos@0
    94
        COALESCE(QUANTITYORDER, 0) AS QUANTITYORDER,
carlos@0
    95
        COUNT(*)
carlos@0
    96
      FROM M_INOUTLINE
carlos@0
    97
      WHERE M_INOUT_ID=v_Record_ID
carlos@0
    98
        AND ISACTIVE='Y'
carlos@0
    99
      GROUP BY M_PRODUCT_ID,
carlos@0
   100
        C_UOM_ID,
carlos@0
   101
        MOVEMENTQTY,
carlos@0
   102
        COALESCE(M_PRODUCT_UOM_ID, 0),
carlos@0
   103
        COALESCE(QUANTITYORDER, 0)
carlos@0
   104
      )
carlos@0
   105
      A;
adrian@94
   106
    IF(v_Total&gt;0) THEN
carlos@0
   107
      v_Message:='LinesNotMatch';
carlos@0
   108
      v_Result:=0;
carlos@0
   109
      FINISH_PROCESS:=true;
carlos@0
   110
    END IF;
carlos@0
   111
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   112
      v_ResultStr:='InsertingLines';
carlos@0
   113
      DECLARE
carlos@0
   114
        Cur_Lines RECORD;
carlos@0
   115
        v_Lines VARCHAR2(4000):='';
carlos@0
   116
        v_Lines_Aux NUMBER(10) ;
carlos@0
   117
        v_StrSQL VARCHAR2(4000):='';
carlos@0
   118
      BEGIN
carlos@0
   119
        FOR Cur_Lines IN
carlos@0
   120
          (SELECT M_PRODUCT_ID,
carlos@0
   121
            C_UOM_ID,
carlos@0
   122
            ROUND(MOVEMENTQTY, 2) AS MOVEMENTQTY,
carlos@0
   123
            COALESCE(M_PRODUCT_UOM_ID, 0) AS M_PRODUCT_UOM_ID,
carlos@0
   124
            COALESCE(QUANTITYORDER, 0) AS QUANTITYORDER,
carlos@0
   125
            M_ATTRIBUTESETINSTANCE_ID
carlos@0
   126
          FROM M_INOUTLINE
carlos@0
   127
          WHERE M_INOUT_ID=v_M_InOut_ID
carlos@0
   128
            AND ISACTIVE='Y'
carlos@0
   129
          )
carlos@0
   130
        LOOP
carlos@0
   131
          DBMS_OUTPUT.PUT_LINE('SQL: ' || Cur_Lines.M_Product_ID) ;
carlos@0
   132
          v_StrSQL:='SELECT MIN(M_INOUTLINE_ID) FROM M_INOUTLINE ';
carlos@0
   133
          v_StrSQL:=v_StrSQL || 'WHERE M_INOUT_ID = ' || v_Record_ID || ' ';
carlos@0
   134
          v_StrSQL:=v_StrSQL || 'AND ISACTIVE = ''Y'' ';
carlos@0
   135
          v_StrSQL:=v_StrSQL || 'AND M_PRODUCT_ID = ' || Cur_Lines.M_PRODUCT_ID || ' ';
carlos@0
   136
          v_StrSQL:=v_StrSQL || 'AND C_UOM_ID = ' || Cur_Lines.C_UOM_ID || ' ';
carlos@0
   137
          v_StrSQL:=v_StrSQL || 'AND ROUND(MOVEMENTQTY, 2) = TO_NUMBER(''' || Cur_Lines.MOVEMENTQTY || ''') ';
carlos@0
   138
          v_StrSQL:=v_StrSQL || 'AND COALESCE(M_PRODUCT_UOM_ID, 0) = ' || Cur_Lines.M_PRODUCT_UOM_ID || ' ';
carlos@0
   139
          v_StrSQL:=v_StrSQL || 'AND COALESCE(QUANTITYORDER, 0) = ' || Cur_Lines.QUANTITYORDER || ' ';
carlos@0
   140
          IF v_Lines IS NOT NULL THEN
carlos@0
   141
            v_StrSQL:=v_StrSQL || 'AND M_INOUTLINE_ID NOT IN (' || v_Lines || ') ';
carlos@0
   142
          END IF;
carlos@0
   143
          EXECUTE IMMEDIATE v_StrSQL INTO v_Lines_Aux;
carlos@0
   144
          IF v_Lines IS NULL THEN
carlos@0
   145
            v_Lines:=v_Lines_Aux;
carlos@0
   146
          ELSE
carlos@0
   147
            v_Lines:=v_Lines || ', ' || v_Lines_Aux;
carlos@0
   148
          END IF;
carlos@0
   149
          DBMS_OUTPUT.PUT('LINES EXECUTED: ' || v_Lines || ' - ACTUAL LINE: ' || v_Lines_Aux) ;
carlos@0
   150
          UPDATE M_INOUTLINE
carlos@0
   151
            SET M_ATTRIBUTESETINSTANCE_ID=Cur_Lines.M_ATTRIBUTESETINSTANCE_ID
carlos@0
   152
          WHERE M_INOUTLINE.M_INOUTLINE_ID=v_Lines_Aux;
carlos@0
   153
        END LOOP;
carlos@0
   154
      END;
adrian@94
   155
    END IF;----&lt;&lt;FINISH_PROCESS&gt;&gt;
carlos@0
   156
    --  Update AD_PInstance
carlos@0
   157
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
carlos@0
   158
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 1, v_Message) ;
carlos@0
   159
    RETURN;
carlos@0
   160
  END; --BODY
carlos@0
   161
EXCEPTION
carlos@0
   162
WHEN OTHERS THEN
carlos@0
   163
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   164
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   165
  ROLLBACK;
carlos@0
   166
  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   167
  RETURN;
adrian@94
   168
END M_InOut_UpdateQuantityes
carlos@0
   169
</body>
adrian@94
   170
    </function>
adrian@94
   171
  </database>