src-db/database/model/functions/M_INTERNAL_CONSUMPTION_POST1.xml
author Miguel A. Alsasua <miguel.alsasua@openbravo.com>
Fri, 19 Sep 2014 11:53:32 +0200
changeset 24913 e10ca5e6571f
parent 20976 3f5e8c7117d6
child 29706 a8a6d8bd52e5
permissions -rw-r--r--
[costAdjs]void shipment or internal consump: the trxs are checked as permanent costs
gorkaion@17023
     1
<?xml version="1.0"?>
gorkaion@17023
     2
  <database name="FUNCTION M_INTERNAL_CONSUMPTION_POST1">
gorkaion@17023
     3
    <function name="M_INTERNAL_CONSUMPTION_POST1" type="NULL">
gorkaion@17023
     4
      <parameter name="pinstance_id" type="VARCHAR" mode="in">
gorkaion@17023
     5
        <default/>
gorkaion@17023
     6
      </parameter>
gorkaion@17023
     7
      <parameter name="p_internal_cosumption_id" type="VARCHAR" mode="in">
gorkaion@17023
     8
        <default/>
gorkaion@17023
     9
      </parameter>
gorkaion@17023
    10
      <parameter name="p_action" type="VARCHAR" mode="in">
gorkaion@17023
    11
        <default/>
gorkaion@17023
    12
      </parameter>
gorkaion@17023
    13
      <body><![CDATA[/*************************************************************************
gorkaion@17023
    14
* The contents of this file are subject to the Openbravo  Public  License
gorkaion@17023
    15
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
gorkaion@17023
    16
* Version 1.1  with a permitted attribution clause; you may not  use this
gorkaion@17023
    17
* file except in compliance with the License. You  may  obtain  a copy of
gorkaion@17023
    18
* the License at http://www.openbravo.com/legal/license.html
gorkaion@17023
    19
* Software distributed under the License  is  distributed  on  an "AS IS"
gorkaion@17023
    20
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
gorkaion@17023
    21
* License for the specific  language  governing  rights  and  limitations
gorkaion@17023
    22
* under the License.
gorkaion@17023
    23
* The Original Code is Openbravo ERP.
gorkaion@17023
    24
* The Initial Developer of the Original Code is Openbravo SLU
gorkaion@20946
    25
* All portions are Copyright (C) 2012-2013 Openbravo SLU
gorkaion@17023
    26
* All Rights Reserved.
gorkaion@17023
    27
* Contributor(s):  ______________________________________.
gorkaion@17023
    28
************************************************************************/
gorkaion@17023
    29
  -- Logistice
gorkaion@17023
    30
  v_ResultStr VARCHAR2(2000):='';
gorkaion@17023
    31
  v_Message VARCHAR2(2000):='';
gorkaion@17023
    32
  Record_ID VARCHAR2(32);
gorkaion@17023
    33
  v_Result NUMBER:=1;
gorkaion@17023
    34
  -- Parameter
gorkaion@17023
    35
  TYPE RECORD IS REF CURSOR;
gorkaion@17023
    36
  Cur_Parameter RECORD;
gorkaion@17023
    37
gorkaion@17023
    38
  -- Parameter Variables
gorkaion@17023
    39
  v_IsProcessing CHAR(1) ;
gorkaion@17023
    40
  v_IsProcessed VARCHAR2(60) ;
gorkaion@17023
    41
  v_MoveDate DATE;
gorkaion@17023
    42
  v_Client_ID VARCHAR2(32) ;
gorkaion@17023
    43
  v_Org_ID VARCHAR2(32);
gorkaion@17023
    44
  v_User VARCHAR2(32);
gorkaion@17023
    45
  v_line NUMBER;
gorkaion@17023
    46
  v_Count NUMBER:=0;
gorkaion@17023
    47
  v_action VARCHAR2(60):='CO';
gorkaion@17023
    48
  v_status M_Internal_Consumption.status%TYPE;
gorkaion@17023
    49
  v_name M_Internal_Consumption.name%TYPE;
gorkaion@20944
    50
  v_productname m_product.name%TYPE;
gorkaion@17023
    51
BEGIN
gorkaion@17023
    52
  --  Update AD_PInstance
gorkaion@17023
    53
  IF (pinstance_id IS NOT NULL) THEN
gorkaion@17023
    54
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || PInstance_ID) ;
gorkaion@17023
    55
    v_ResultStr:='PInstanceNotFound';
gorkaion@17023
    56
    AD_UPDATE_PINSTANCE(PInstance_ID, NULL, 'Y', NULL, NULL) ;
gorkaion@17023
    57
  END IF;
gorkaion@17023
    58
BEGIN --BODY
gorkaion@17023
    59
  -- Get Parameters
gorkaion@17023
    60
  IF (pinstance_id IS NOT NULL) THEN
gorkaion@17023
    61
    v_ResultStr:='ReadingParameters';
gorkaion@17023
    62
    FOR Cur_Parameter IN (
gorkaion@17023
    63
      SELECT i.Record_ID, i.AD_User_ID, p.ParameterName,
gorkaion@17023
    64
          p.P_String, p.P_Number, p.P_Date
gorkaion@17023
    65
      FROM AD_PInstance i
gorkaion@17023
    66
             LEFT JOIN AD_PInstance_Para p ON i.AD_PInstance_ID=p.AD_PInstance_ID
gorkaion@17023
    67
      WHERE i.AD_PInstance_ID=PInstance_ID
gorkaion@17023
    68
      ORDER BY p.SeqNo
gorkaion@17023
    69
    ) LOOP
gorkaion@17023
    70
      IF (upper(cur_parameter.parametername) LIKE 'ACTION') THEN
gorkaion@17023
    71
        v_action := cur_parameter.p_string;
gorkaion@17023
    72
      END IF;
gorkaion@17023
    73
      Record_ID := Cur_Parameter.Record_ID;
gorkaion@17023
    74
      v_User := Cur_Parameter.AD_User_ID;
gorkaion@17023
    75
    END LOOP; -- Get Parameter
gorkaion@17023
    76
  ELSE
gorkaion@17023
    77
    record_id := p_internal_cosumption_id;
gorkaion@17023
    78
    v_action := p_action;
gorkaion@17023
    79
  END IF;
gorkaion@17023
    80
  DBMS_OUTPUT.PUT_LINE('  Record_ID=' || Record_ID) ;
gorkaion@17023
    81
  -- Reading Internal_Consumption
gorkaion@17023
    82
  SELECT MovementDate, Processing, Processed, AD_Client_ID,
gorkaion@17023
    83
         AD_Org_ID, COALESCE(v_user, updatedby), status, name
gorkaion@17023
    84
    INTO v_MoveDate, v_IsProcessing, v_IsProcessed, v_Client_ID,
gorkaion@17023
    85
         v_Org_ID, v_user, v_status, v_name
gorkaion@17023
    86
  FROM M_Internal_Consumption
gorkaion@17023
    87
  WHERE M_Internal_Consumption_ID=Record_ID  FOR UPDATE;
gorkaion@17023
    88
  IF (v_IsProcessing = 'Y') THEN
gorkaion@17023
    89
    RAISE_APPLICATION_ERROR(-20000, '@OtherProcessActive@') ;
gorkaion@17023
    90
  END IF;
gorkaion@17023
    91
  IF (v_IsProcessed = 'Y' AND v_action <> 'VO') THEN
gorkaion@17023
    92
    RAISE_APPLICATION_ERROR(-20000, '@AlreadyPosted@') ;
gorkaion@17023
    93
  END IF;
gorkaion@17023
    94
gorkaion@17023
    95
  v_ResultStr:='CheckingRestrictions';
gorkaion@17023
    96
  SELECT COUNT(*), MAX(line)
gorkaion@17023
    97
    INTO v_Count, v_line
gorkaion@17023
    98
  FROM M_Internal_ConsumptionLine M, M_Product P
gorkaion@17023
    99
  WHERE M.M_PRODUCT_ID=P.M_PRODUCT_ID
gorkaion@17023
   100
    AND P.M_ATTRIBUTESET_ID IS NOT NULL
gorkaion@17023
   101
    AND (P.ATTRSETVALUETYPE IS NULL OR P.ATTRSETVALUETYPE <> 'F')
gorkaion@17023
   102
    AND (SELECT ISONEATTRSETVALREQUIRED FROM M_ATTRIBUTESET WHERE M_ATTRIBUTESET_ID = P.M_ATTRIBUTESET_ID) = 'Y'
gorkaion@17023
   103
    AND COALESCE(M.M_ATTRIBUTESETINSTANCE_ID, '0') = '0'
gorkaion@17023
   104
    AND M.M_Internal_Consumption_ID=Record_ID;
gorkaion@17023
   105
  IF (v_Count <> 0) THEN
gorkaion@17023
   106
    RAISE_APPLICATION_ERROR(-20000, '@Inline@'||' '||v_line||' '||'@productWithoutAttributeSet@') ;
gorkaion@17023
   107
  END IF;
javier@19408
   108
javier@19408
   109
  --Checking if the Internal consumption has lines
javier@19408
   110
  SELECT count(*)
javier@19408
   111
  INTO v_Count
javier@19408
   112
  FROM M_Internal_ConsumptionLine
javier@19408
   113
  WHERE M_Internal_Consumption_ID=Record_ID;
javier@19408
   114
  IF (v_Count = 0) THEN
javier@19408
   115
    RAISE_APPLICATION_ERROR(-20000, '@InternalConsuptionNoLines@') ;
javier@19408
   116
  END IF;
gorkaion@20944
   117
gorkaion@20976
   118
  SELECT count(*) INTO v_count
gorkaion@20976
   119
  FROM dual
gorkaion@20976
   120
  WHERE EXISTS (
gorkaion@20976
   121
      SELECT 1
gorkaion@20976
   122
      FROM m_internal_consumptionline icl JOIN m_product p ON icl.m_product_id = p.m_product_id
gorkaion@20976
   123
      WHERE icl.m_internal_consumption_id = record_id
gorkaion@20976
   124
        AND p.isgeneric = 'Y');
gorkaion@20944
   125
  IF (v_count > 0) THEN
gorkaion@20976
   126
    SELECT max(p.name) INTO v_productname
gorkaion@20976
   127
    FROM m_internal_consumptionline icl JOIN m_product p ON icl.m_product_id = p.m_product_id
gorkaion@20976
   128
    WHERE icl.m_internal_consumption_id = record_id
gorkaion@20976
   129
      AND p.isgeneric = 'Y';
gorkaion@20944
   130
    RAISE_APPLICATION_ERROR(-20000, '@CannotUseGenericProduct@ ' || v_productName);
gorkaion@20944
   131
  END IF;
javier@19408
   132
  
gorkaion@17023
   133
  IF (v_action IN ('CO')) THEN
gorkaion@17023
   134
    /**
gorkaion@17023
   135
    * Complete action: Create records on m_transaction and update status
gorkaion@17023
   136
    */
gorkaion@17023
   137
    DECLARE
gorkaion@17023
   138
      Cur_MoveLine RECORD;
gorkaion@17023
   139
    BEGIN
gorkaion@17023
   140
      FOR Cur_MoveLine IN (
gorkaion@17023
   141
          SELECT *
gorkaion@17023
   142
          FROM M_Internal_ConsumptionLine
gorkaion@17023
   143
          WHERE M_Internal_Consumption_ID=Record_ID
gorkaion@17023
   144
          ORDER BY Line
gorkaion@17023
   145
      ) LOOP
gorkaion@17023
   146
        v_ResultStr:='Transaction for line' || Cur_MoveLine.Line;
gorkaion@17023
   147
gorkaion@17023
   148
        INSERT INTO M_Transaction (
gorkaion@17023
   149
            M_Transaction_ID, AD_Client_ID, AD_Org_ID, IsActive,
gorkaion@17023
   150
            Created, CreatedBy, Updated, UpdatedBy,
gorkaion@17023
   151
            MovementType, M_Locator_ID, M_Product_ID, M_AttributeSetInstance_ID,
gorkaion@17023
   152
            MovementDate, MovementQty, M_Internal_ConsumptionLine_ID, M_Product_UOM_ID,
gorkaion@17023
   153
            QuantityOrder, C_UOM_ID
gorkaion@17023
   154
        ) VALUES (
gorkaion@17023
   155
            get_uuid(), Cur_MoveLine.AD_Client_ID, Cur_MoveLine.AD_Org_ID, 'Y',
gorkaion@17023
   156
            now(), v_User, now(), v_User,
gorkaion@17023
   157
            'D-', Cur_MoveLine.M_Locator_ID, Cur_MoveLine.M_Product_ID, COALESCE(Cur_MoveLine.M_AttributeSetInstance_ID, '0'),
gorkaion@17023
   158
            v_MoveDate, (Cur_MoveLine.MovementQty * -1), Cur_MoveLine.M_Internal_ConsumptionLine_ID, Cur_MoveLine.M_Product_UOM_ID,
gorkaion@17023
   159
            (Cur_MoveLine.QuantityOrder * -1), Cur_MoveLine.C_UOM_ID
gorkaion@17023
   160
        );
gorkaion@17023
   161
        M_Check_Stock(Cur_MoveLine.M_Product_ID, v_Client_ID, v_Org_ID, v_Result, v_Message) ;
gorkaion@17023
   162
        IF (v_Result = 0) THEN
gorkaion@17023
   163
          RAISE_APPLICATION_ERROR(-20000, v_Message||' '||'@line@'||' '||Cur_MoveLine.line) ;
gorkaion@17023
   164
        END IF;
gorkaion@17023
   165
      END LOOP;
gorkaion@17023
   166
      v_ResultStr:='ProcessingMovement';
gorkaion@17023
   167
      UPDATE M_Internal_Consumption
gorkaion@17023
   168
      SET Processed = 'Y',
gorkaion@17023
   169
          Status = 'CO',
gorkaion@17023
   170
          Updated = now(),
gorkaion@17023
   171
          UpdatedBy = v_User
gorkaion@17023
   172
      WHERE M_Internal_Consumption_ID = Record_ID;
gorkaion@17023
   173
gorkaion@17023
   174
    END;
gorkaion@17023
   175
  ELSIF (v_action IN ('VO')) THEN
gorkaion@17023
   176
    /*
gorkaion@17023
   177
    * Void Internal consumption.
gorkaion@17023
   178
    *
gorkaion@17023
   179
    * Create a new internal consumption negating the quantities and post it.
gorkaion@17023
   180
    */
gorkaion@17023
   181
    DECLARE
gorkaion@17023
   182
      v_voidintconsumption_id VARCHAR2(32);
gorkaion@17023
   183
      cur_intcons_line RECORD;
gorkaion@17023
   184
    BEGIN
gorkaion@17023
   185
      -- Checks
gorkaion@17023
   186
      IF (v_status <> 'CO') THEN
gorkaion@17023
   187
        RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@17023
   188
      END IF;
gorkaion@17023
   189
      v_voidintconsumption_id := get_uuid();
gorkaion@17023
   190
      -- Create void internal consumption.
gorkaion@17023
   191
      INSERT INTO m_internal_consumption (
gorkaion@17023
   192
          m_internal_consumption_id, ad_client_id, ad_org_id, isactive,
gorkaion@17023
   193
          created, createdby, updated, updatedby,
gorkaion@17023
   194
          name, movementdate
gorkaion@17023
   195
      ) VALUES (
gorkaion@17023
   196
          v_voidintconsumption_id, v_client_id, v_org_id, 'Y',
gorkaion@17023
   197
          now(), v_user, now(), v_user,
gorkaion@17097
   198
          'VO: '|| v_name, v_MoveDate
gorkaion@17023
   199
      );
gorkaion@17023
   200
      
gorkaion@17023
   201
      INSERT INTO m_internal_consumptionline (
gorkaion@17023
   202
        m_internal_consumptionline_id, ad_client_id, ad_org_id, isactive,
gorkaion@17023
   203
        created, createdby, updated, updatedby,
gorkaion@17023
   204
        m_internal_consumption_id, line, m_locator_id,
gorkaion@17023
   205
        m_product_id, m_attributesetinstance_id,
gorkaion@17023
   206
        movementqty, c_uom_id, m_product_uom_id, quantityorder,
gorkaion@17023
   207
        description, void_intconsumption_line_id
gorkaion@17023
   208
      ) 
gorkaion@17023
   209
      SELECT get_uuid(), ad_client_id, ad_org_id, isactive,
gorkaion@17023
   210
          now(), v_user, now(), v_user,
gorkaion@17023
   211
          v_voidintconsumption_id, line, m_locator_id,
gorkaion@17023
   212
          m_product_id, m_attributesetinstance_id,
gorkaion@17023
   213
          movementqty * -1, c_uom_id, m_product_uom_id, quantityorder * -1,
gorkaion@17023
   214
          description, m_internal_consumptionline_id
gorkaion@17023
   215
      FROM m_internal_consumptionline
gorkaion@17023
   216
      WHERE m_internal_consumption_id = record_id;
gorkaion@17023
   217
      
gorkaion@17023
   218
      --Update void
gorkaion@17023
   219
      UPDATE m_internal_consumption
gorkaion@17023
   220
      SET status = 'VO',
gorkaion@17023
   221
          updated = now(),
gorkaion@17023
   222
          updatedby = v_user
gorkaion@17023
   223
      WHERE m_internal_consumption_id = record_id;
gorkaion@17023
   224
      -- post reveral internal consumption
gorkaion@17023
   225
      M_INTERNAL_CONSUMPTION_POST1(NULL, v_voidintconsumption_id, 'CO');
gorkaion@17023
   226
      -- update reversal internal consumption
gorkaion@17023
   227
      UPDATE m_internal_consumption
gorkaion@17023
   228
      SET status = 'VO',
gorkaion@17023
   229
          updated = now(),
gorkaion@17023
   230
          updatedby = v_user
gorkaion@17023
   231
      WHERE m_internal_consumption_id = v_voidintconsumption_id;
gorkaion@17023
   232
miguel@24913
   233
      -- transactions related with original inout and with voided inout will be mark as is cost permanent
miguel@24913
   234
      UPDATE M_TRANSACTION TRX
miguel@24913
   235
      SET ISCOSTPERMANENT='Y'
miguel@24913
   236
      WHERE TRX.M_INTERNAL_CONSUMPTIONLINE_ID IN (SELECT M_INTERNAL_CONSUMPTIONLINE_ID 
miguel@24913
   237
                                   FROM M_INTERNAL_CONSUMPTIONLINE
miguel@24913
   238
                                   WHERE (M_INTERNAL_CONSUMPTION_ID = record_id
miguel@24913
   239
                                          OR M_INTERNAL_CONSUMPTION_ID =  v_voidintconsumption_id));
gorkaion@17023
   240
    END;
gorkaion@17023
   241
  END IF;
gorkaion@17023
   242
   v_ResultStr := 'UnLockingMovement';
gorkaion@17023
   243
  --<<END_PROCESSING>>
gorkaion@17023
   244
  --  Update AD_PInstance
gorkaion@17023
   245
  DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
gorkaion@17023
   246
  IF (pinstance_id IS NOT NULL) THEN
gorkaion@17023
   247
    AD_UPDATE_PINSTANCE(PInstance_ID, v_User, 'N', v_Result, v_Message) ;
gorkaion@17023
   248
  END IF;
gorkaion@17023
   249
  RETURN;
gorkaion@17023
   250
END; --BODY
gorkaion@17023
   251
EXCEPTION
gorkaion@17023
   252
WHEN OTHERS THEN
gorkaion@17023
   253
  v_ResultStr:= '@ERROR=' || SQLERRM;
gorkaion@17023
   254
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
gorkaion@17023
   255
  IF (pinstance_id IS NOT NULL) THEN
gorkaion@17023
   256
    ROLLBACK;
gorkaion@17023
   257
    AD_UPDATE_PINSTANCE(PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
gorkaion@17023
   258
  ELSE
gorkaion@17023
   259
    RAISE;
gorkaion@17023
   260
  END IF;
gorkaion@17023
   261
  RETURN;
gorkaion@17023
   262
END M_INTERNAL_CONSUMPTION_POST1
gorkaion@17023
   263
]]></body>
gorkaion@17023
   264
    </function>
gorkaion@17023
   265
  </database>