database/model/functions/M_INOUT_UPDATEQUANTITYES.xml
author Gorka Ion Damián <gorkaion.damian@openbravo.com>
Fri, 14 Dec 2007 16:21:33 +0000
changeset 239 a30acae847e1
parent 170 4b5493776a7f
child 735 daced7e311c9
permissions -rw-r--r--
Added CDATA to views, functions and triggers.
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"/>
gorkaion@239
     5
      <body><![CDATA[/*************************************************************************
carlos@0
     6
* The contents of this file are subject to the Openbravo  Public  License
carlos@0
     7
* Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
carlos@0
     8
* Version 1.1  with a permitted attribution clause; you may not  use this
carlos@0
     9
* file except in compliance with the License. You  may  obtain  a copy of
carlos@0
    10
* the License at http://www.openbravo.com/legal/license.html
carlos@0
    11
* Software distributed under the License  is  distributed  on  an "AS IS"
carlos@0
    12
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
carlos@0
    13
* License for the specific  language  governing  rights  and  limitations
carlos@0
    14
* under the License.
carlos@0
    15
* The Original Code is Openbravo ERP.
carlos@0
    16
* The Initial Developer of the Original Code is Openbravo SL
carlos@0
    17
* All portions are Copyright (C) 2001-2006 Openbravo SL
carlos@0
    18
* All Rights Reserved.
carlos@0
    19
* Contributor(s):  ______________________________________.
carlos@0
    20
************************************************************************/
carlos@0
    21
  v_ResultStr VARCHAR2(2000):='';
carlos@0
    22
  v_Message VARCHAR2(2000):='';
carlos@0
    23
  v_Record_ID NUMBER;
carlos@0
    24
  v_Result NUMBER:=0; -- failure
carlos@0
    25
  v_AD_User_ID NUMBER;
carlos@0
    26
  v_AD_Org_ID NUMBER;
carlos@0
    27
  v_AD_Client_ID NUMBER;
carlos@0
    28
  -- Parameter
carlos@0
    29
  TYPE RECORD IS REF CURSOR;
carlos@0
    30
    Cur_Parameter RECORD;
carlos@0
    31
    -- Parameter Variables
carlos@0
    32
    v_M_InOut_ID NUMBER;
carlos@0
    33
    v_Total NUMBER;
carlos@0
    34
    FINISH_PROCESS BOOLEAN:=false;
carlos@0
    35
  BEGIN
carlos@0
    36
    --  Update AD_PInstance
carlos@0
    37
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_PInstance_ID) ;
carlos@0
    38
    v_ResultStr:='PInstanceNotFound';
carlos@0
    39
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
carlos@0
    40
  BEGIN --BODY
carlos@0
    41
    -- Get Parameters
carlos@0
    42
    v_ResultStr:='ReadingParameters';
carlos@0
    43
    FOR Cur_Parameter IN
carlos@0
    44
      (SELECT i.Record_ID,
carlos@0
    45
        i.AD_USER_ID,
carlos@0
    46
        i.AD_ORG_ID,
carlos@0
    47
        i.AD_CLIENT_ID,
carlos@0
    48
        p.ParameterName,
carlos@0
    49
        p.P_String,
carlos@0
    50
        p.P_Number,
carlos@0
    51
        p.P_Date
carlos@0
    52
      FROM AD_PInstance i
carlos@0
    53
      LEFT JOIN AD_PInstance_Para p
carlos@0
    54
        ON i.AD_PInstance_ID=p.AD_PInstance_ID
carlos@0
    55
      WHERE i.AD_PInstance_ID=p_PInstance_ID
carlos@0
    56
      ORDER BY p.SeqNo
carlos@0
    57
      )
carlos@0
    58
    LOOP
carlos@0
    59
      v_Record_ID:=Cur_Parameter.Record_ID;
carlos@0
    60
      v_AD_User_ID:=Cur_Parameter.AD_User_ID;
carlos@0
    61
      v_AD_Org_ID:=Cur_Parameter.AD_Org_ID;
carlos@0
    62
      v_AD_Client_ID:=Cur_Parameter.AD_Client_ID;
carlos@0
    63
      IF(Cur_Parameter.ParameterName='M_InOut_ID') THEN
carlos@0
    64
        v_M_InOut_ID:=Cur_Parameter.P_Number;
carlos@0
    65
        DBMS_OUTPUT.PUT_LINE('  M_InOut_ID=' || v_M_InOut_ID) ;
carlos@0
    66
      ELSE
carlos@0
    67
        DBMS_OUTPUT.PUT_LINE('*** Unknown Parameter=' || Cur_Parameter.ParameterName) ;
carlos@0
    68
      END IF;
carlos@0
    69
    END LOOP; -- Get Parameter
carlos@0
    70
    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
carlos@0
    71
    v_ResultStr:='CheckRestriction';
carlos@0
    72
    SELECT COUNT(*)
carlos@0
    73
    INTO v_Total
carlos@0
    74
    FROM
carlos@0
    75
      (SELECT M_PRODUCT_ID,
carlos@0
    76
        C_UOM_ID,
carlos@0
    77
        ROUND(MOVEMENTQTY, 2),
carlos@0
    78
        COALESCE(M_PRODUCT_UOM_ID, 0) AS M_PRODUCT_UOM_ID,
carlos@0
    79
        COALESCE(QUANTITYORDER, 0) AS QUANTITYORDER,
carlos@0
    80
        COUNT(*)
carlos@0
    81
      FROM M_INOUTLINE
carlos@0
    82
      WHERE M_INOUT_ID=v_M_InOut_ID
carlos@0
    83
        AND ISACTIVE='Y'
carlos@0
    84
      GROUP BY M_PRODUCT_ID,
carlos@0
    85
        C_UOM_ID,
carlos@0
    86
        MOVEMENTQTY,
carlos@0
    87
        COALESCE(M_PRODUCT_UOM_ID, 0),
carlos@0
    88
        COALESCE(QUANTITYORDER, 0) MINUS
carlos@0
    89
      SELECT M_PRODUCT_ID,
carlos@0
    90
        C_UOM_ID,
carlos@0
    91
        ROUND(MOVEMENTQTY, 2),
carlos@0
    92
        COALESCE(M_PRODUCT_UOM_ID, 0) AS M_PRODUCT_UOM_ID,
carlos@0
    93
        COALESCE(QUANTITYORDER, 0) AS QUANTITYORDER,
carlos@0
    94
        COUNT(*)
carlos@0
    95
      FROM M_INOUTLINE
carlos@0
    96
      WHERE M_INOUT_ID=v_Record_ID
carlos@0
    97
        AND ISACTIVE='Y'
carlos@0
    98
      GROUP BY M_PRODUCT_ID,
carlos@0
    99
        C_UOM_ID,
carlos@0
   100
        MOVEMENTQTY,
carlos@0
   101
        COALESCE(M_PRODUCT_UOM_ID, 0),
carlos@0
   102
        COALESCE(QUANTITYORDER, 0)
carlos@0
   103
      )
carlos@0
   104
      A;
gorkaion@239
   105
    IF(v_Total>0) THEN
carlos@0
   106
      v_Message:='LinesNotMatch';
carlos@0
   107
      v_Result:=0;
carlos@0
   108
      FINISH_PROCESS:=true;
carlos@0
   109
    END IF;
carlos@0
   110
    IF(NOT FINISH_PROCESS) THEN
carlos@0
   111
      v_ResultStr:='InsertingLines';
carlos@0
   112
      DECLARE
carlos@0
   113
        Cur_Lines RECORD;
carlos@0
   114
        v_Lines VARCHAR2(4000):='';
carlos@0
   115
        v_Lines_Aux NUMBER(10) ;
carlos@0
   116
        v_StrSQL VARCHAR2(4000):='';
carlos@0
   117
      BEGIN
carlos@0
   118
        FOR Cur_Lines IN
carlos@0
   119
          (SELECT M_PRODUCT_ID,
carlos@0
   120
            C_UOM_ID,
carlos@0
   121
            ROUND(MOVEMENTQTY, 2) AS MOVEMENTQTY,
carlos@0
   122
            COALESCE(M_PRODUCT_UOM_ID, 0) AS M_PRODUCT_UOM_ID,
carlos@0
   123
            COALESCE(QUANTITYORDER, 0) AS QUANTITYORDER,
carlos@0
   124
            M_ATTRIBUTESETINSTANCE_ID
carlos@0
   125
          FROM M_INOUTLINE
carlos@0
   126
          WHERE M_INOUT_ID=v_M_InOut_ID
carlos@0
   127
            AND ISACTIVE='Y'
carlos@0
   128
          )
carlos@0
   129
        LOOP
carlos@0
   130
          DBMS_OUTPUT.PUT_LINE('SQL: ' || Cur_Lines.M_Product_ID) ;
carlos@0
   131
          v_StrSQL:='SELECT MIN(M_INOUTLINE_ID) FROM M_INOUTLINE ';
carlos@0
   132
          v_StrSQL:=v_StrSQL || 'WHERE M_INOUT_ID = ' || v_Record_ID || ' ';
carlos@0
   133
          v_StrSQL:=v_StrSQL || 'AND ISACTIVE = ''Y'' ';
carlos@0
   134
          v_StrSQL:=v_StrSQL || 'AND M_PRODUCT_ID = ' || Cur_Lines.M_PRODUCT_ID || ' ';
carlos@0
   135
          v_StrSQL:=v_StrSQL || 'AND C_UOM_ID = ' || Cur_Lines.C_UOM_ID || ' ';
carlos@0
   136
          v_StrSQL:=v_StrSQL || 'AND ROUND(MOVEMENTQTY, 2) = TO_NUMBER(''' || Cur_Lines.MOVEMENTQTY || ''') ';
carlos@0
   137
          v_StrSQL:=v_StrSQL || 'AND COALESCE(M_PRODUCT_UOM_ID, 0) = ' || Cur_Lines.M_PRODUCT_UOM_ID || ' ';
carlos@0
   138
          v_StrSQL:=v_StrSQL || 'AND COALESCE(QUANTITYORDER, 0) = ' || Cur_Lines.QUANTITYORDER || ' ';
carlos@0
   139
          IF v_Lines IS NOT NULL THEN
carlos@0
   140
            v_StrSQL:=v_StrSQL || 'AND M_INOUTLINE_ID NOT IN (' || v_Lines || ') ';
carlos@0
   141
          END IF;
carlos@0
   142
          EXECUTE IMMEDIATE v_StrSQL INTO v_Lines_Aux;
carlos@0
   143
          IF v_Lines IS NULL THEN
carlos@0
   144
            v_Lines:=v_Lines_Aux;
carlos@0
   145
          ELSE
carlos@0
   146
            v_Lines:=v_Lines || ', ' || v_Lines_Aux;
carlos@0
   147
          END IF;
carlos@0
   148
          DBMS_OUTPUT.PUT('LINES EXECUTED: ' || v_Lines || ' - ACTUAL LINE: ' || v_Lines_Aux) ;
carlos@0
   149
          UPDATE M_INOUTLINE
carlos@0
   150
            SET M_ATTRIBUTESETINSTANCE_ID=Cur_Lines.M_ATTRIBUTESETINSTANCE_ID
carlos@0
   151
          WHERE M_INOUTLINE.M_INOUTLINE_ID=v_Lines_Aux;
carlos@0
   152
        END LOOP;
carlos@0
   153
      END;
gorkaion@239
   154
    END IF;----<<FINISH_PROCESS>>
carlos@0
   155
    --  Update AD_PInstance
carlos@0
   156
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
carlos@0
   157
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 1, v_Message) ;
carlos@0
   158
    RETURN;
carlos@0
   159
  END; --BODY
carlos@0
   160
EXCEPTION
carlos@0
   161
WHEN OTHERS THEN
carlos@0
   162
  v_ResultStr:= '@ERROR=' || SQLERRM;
carlos@0
   163
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
carlos@0
   164
  ROLLBACK;
carlos@0
   165
  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
carlos@0
   166
  RETURN;
adrian@94
   167
END M_InOut_UpdateQuantityes
adrian@170
   168
gorkaion@239
   169
gorkaion@239
   170
]]></body>
adrian@94
   171
    </function>
adrian@94
   172
  </database>