src-db/database/model/functions/M_RESERVATION_POST.xml
author Gorka Ion Damián <gorkaion.damian@openbravo.com>
Wed, 14 Nov 2012 10:17:11 +0100
changeset 18696 018782102b24
parent 18695 f584300636c8
child 18705 5966a12e0b3c
permissions -rw-r--r--
[Reservations]Fix issue in Reservation Post.
gorkaion@18649
     1
<?xml version="1.0"?>
gorkaion@18649
     2
  <database name="FUNCTION M_RESERVATION_POST">
gorkaion@18649
     3
    <function name="M_RESERVATION_POST" type="NULL">
gorkaion@18649
     4
      <parameter name="p_pinstance_id" type="VARCHAR" mode="in">
gorkaion@18649
     5
        <default/>
gorkaion@18649
     6
      </parameter>
gorkaion@18649
     7
      <parameter name="p_reservation_id" type="VARCHAR" mode="in">
gorkaion@18649
     8
        <default/>
gorkaion@18649
     9
      </parameter>
gorkaion@18649
    10
      <parameter name="p_resaction" type="VARCHAR" mode="in">
gorkaion@18649
    11
        <default/>
gorkaion@18649
    12
      </parameter>
gorkaion@18649
    13
      <parameter name="p_user_id" type="VARCHAR" mode="in">
gorkaion@18649
    14
        <default/>
gorkaion@18649
    15
      </parameter>
gorkaion@18649
    16
      <body><![CDATA[/*************************************************************************
gorkaion@18649
    17
* The contents of this file are subject to the Openbravo  Public  License
gorkaion@18649
    18
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
gorkaion@18649
    19
* Version 1.1  with a permitted attribution clause; you may not  use this
gorkaion@18649
    20
* file except in compliance with the License. You  may  obtain  a copy of
gorkaion@18649
    21
* the License at http://www.openbravo.com/legal/license.html
gorkaion@18649
    22
* Software distributed under the License  is  distributed  on  an "AS IS"
gorkaion@18649
    23
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
gorkaion@18649
    24
* License for the specific  language  governing  rights  and  limitations
gorkaion@18649
    25
* under the License.
gorkaion@18649
    26
* The Original Code is Openbravo ERP.
gorkaion@18649
    27
* The Initial Developer of the Original Code is Openbravo SLU
gorkaion@18649
    28
* All portions are Copyright (C) 2012 Openbravo SLU
gorkaion@18649
    29
* All Rights Reserved.
gorkaion@18649
    30
* Contributor(s):  ______________________________________.
gorkaion@18649
    31
************************************************************************/
gorkaion@18649
    32
  v_ResultStr       VARCHAR2(2000):='';
gorkaion@18649
    33
  v_Message         VARCHAR2(2000):='';
gorkaion@18649
    34
  v_reservation_id  VARCHAR2(32);
gorkaion@18649
    35
  v_user_id         VARCHAR2(32);
gorkaion@18663
    36
  v_Result NUMBER:=1; --  Success
gorkaion@18649
    37
  v_resaction       VARCHAR2(60);
gorkaion@18649
    38
  v_resstatus       VARCHAR2(60);
gorkaion@18649
    39
  v_newstatus       VARCHAR2(60);
gorkaion@18649
    40
  v_newaction       VARCHAR2(60);
gorkaion@18689
    41
  v_orderline_id    VARCHAR2(32);
gorkaion@18695
    42
  v_product_id      VARCHAR2(32);
gorkaion@18695
    43
  v_uom_id          VARCHAR2(32);
gorkaion@18689
    44
  v_count           NUMBER;
gorkaion@18693
    45
  v_quantity        NUMBER;
gorkaion@18693
    46
  v_reservedqty     NUMBER;
gorkaion@18649
    47
gorkaion@18649
    48
TYPE RECORD IS REF CURSOR;
gorkaion@18649
    49
  Cur_Parameter RECORD;
gorkaion@18649
    50
gorkaion@18649
    51
BEGIN
gorkaion@18649
    52
  IF (p_PInstance_ID IS NOT NULL) THEN
gorkaion@18649
    53
    --  Update AD_PInstance
gorkaion@18649
    54
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_pinstance_id) ;
gorkaion@18649
    55
    v_ResultStr:='PInstanceNotFound';
gorkaion@18649
    56
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
gorkaion@18649
    57
    --  Get Parameters
gorkaion@18649
    58
    v_ResultStr:='ReadingParameters';
gorkaion@18649
    59
    FOR Cur_Parameter IN (
gorkaion@18649
    60
        SELECT i.record_id, i.ad_user_id, p.parametername, p.p_string, p.p_number, p.p_date
gorkaion@18649
    61
        FROM ad_pinstance i
gorkaion@18649
    62
            LEFT JOIN ad_pinstance_para p ON i.ad_pinstance_id=p.ad_pinstance_id
gorkaion@18649
    63
        WHERE i.ad_pinstance_id=p_pinstance_id
gorkaion@18649
    64
        ORDER BY p.seqno
gorkaion@18649
    65
    ) LOOP
gorkaion@18649
    66
      IF (cur_parameter.parametername = 'RES_Action') THEN
gorkaion@18649
    67
        v_resaction := cur_parameter.p_string;
gorkaion@18649
    68
      END IF;
gorkaion@18649
    69
      v_reservation_id := cur_parameter.record_id;
gorkaion@18649
    70
      v_user_id := cur_parameter.ad_user_id;
gorkaion@18649
    71
    END LOOP; --  Get Parameter
gorkaion@18649
    72
    DBMS_OUTPUT.PUT_LINE('  v_Record_ID=' || v_reservation_id) ;
gorkaion@18649
    73
  ELSE
gorkaion@18649
    74
    v_reservation_id := p_reservation_id;
gorkaion@18649
    75
    v_resaction := p_resaction;
gorkaion@18649
    76
    v_user_id := p_user_id;
gorkaion@18649
    77
  END IF;
gorkaion@18649
    78
gorkaion@18649
    79
BEGIN
gorkaion@18696
    80
  SELECT res_status, c_orderline_id, quantity, reservedqty,
gorkaion@18696
    81
        m_product_id, c_uom_id
gorkaion@18696
    82
    INTO v_resstatus, v_orderline_id, v_quantity, v_reservedqty,
gorkaion@18696
    83
        v_product_id, v_uom_id
gorkaion@18649
    84
  FROM m_reservation
gorkaion@18649
    85
  WHERE m_reservation_id = v_reservation_id;
gorkaion@18649
    86
  
gorkaion@18649
    87
  IF (v_resaction = 'PR') THEN
gorkaion@18649
    88
  /*
gorkaion@18649
    89
    Reservation Complete Process
gorkaion@18649
    90
  */
gorkaion@18693
    91
    DECLARE
gorkaion@18693
    92
      v_sales_order_id        VARCHAR2(32);
gorkaion@18695
    93
      cur_not_available       RECORD;
gorkaion@18693
    94
      v_linecount             NUMBER;
gorkaion@18693
    95
      v_creservedcount        NUMBER;
gorkaion@18693
    96
      v_preservedcount        NUMBER;
gorkaion@18693
    97
    BEGIN
gorkaion@18693
    98
      IF (v_resstatus != 'DR') THEN
gorkaion@18693
    99
        RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@18689
   100
      END IF;
gorkaion@18695
   101
      -- Check that stock is still available
gorkaion@18695
   102
      FOR cur_not_available IN (
gorkaion@18695
   103
        SELECT AD_COLUMN_IDENTIFIER('M_Locator', rs.m_locator_id, 'en_US') as locatorName,
gorkaion@18695
   104
               asi.description AS attributeName
gorkaion@18695
   105
        FROM m_reservation_stock rs
gorkaion@18695
   106
            JOIN m_attributesetinstance asi ON COALESCE(rs.m_attributesetinstance_id, '0') = asi.m_attributesetinstance_id
gorkaion@18695
   107
            LEFT JOIN m_storage_detail sd ON sd.m_product_id = v_product_id
gorkaion@18695
   108
                                             AND sd.c_uom_id = v_uom_id
gorkaion@18695
   109
                                             AND sd.m_product_uom_id IS NULL
gorkaion@18695
   110
                                             AND COALESCE(sd.m_attributesetinstance_id, '0') = COALESCE(rs.m_attributesetinstance_id, '0')
gorkaion@18695
   111
                                             AND sd.m_locator_id = rs.m_locator_id
gorkaion@18695
   112
            LEFT JOIN (
gorkaion@18695
   113
                    SELECT SUM(rs2.quantity - COALESCE(rs2.releasedqty,0)) AS reservedstock, rs2.m_locator_id, COALESCE(rs2.m_attributesetinstance_id,'0') AS m_attributesetinstance_id
gorkaion@18695
   114
                    FROM m_reservation_stock rs2
gorkaion@18695
   115
                      JOIN m_reservation r ON rs2.m_reservation_id = r.m_reservation_id
gorkaion@18695
   116
                    WHERE r.m_product_id = v_product_id
gorkaion@18695
   117
                      AND r.res_status NOT IN ('CL', 'DR')
gorkaion@18695
   118
                      AND r.c_uom_id = v_uom_id
gorkaion@18695
   119
                    GROUP BY rs2.m_locator_id, COALESCE(rs2.m_attributesetinstance_id,'0')
gorkaion@18695
   120
                  ) other ON rs.m_locator_id = other.m_locator_id
gorkaion@18695
   121
                             AND COALESCE(rs.m_attributesetinstance_id, '0') = COALESCE(other.m_attributesetinstance_id, '0')
gorkaion@18695
   122
        WHERE rs.quantity - COALESCE(rs.releasedqty, 0) > COALESCE(sd.qtyonhand, 0) - COALESCE(other.reservedstock, 0)
gorkaion@18695
   123
          AND rs.m_reservation_id = v_reservation_id
gorkaion@18695
   124
      ) LOOP
gorkaion@18695
   125
        RAISE_APPLICATION_ERROR(-20000, '@NotEnoughAvailableStock@ : ' || cur_not_available.locatorName || ' - ' || cur_not_available.attributeName);
gorkaion@18695
   126
      END LOOP;
gorkaion@18695
   127
      
gorkaion@18693
   128
      IF (v_orderline_id IS NOT NULL) THEN
gorkaion@18693
   129
        SELECT count(*) INTO v_count
gorkaion@18693
   130
        FROM m_reservation
gorkaion@18693
   131
        WHERE c_orderline_id = v_orderline_id
gorkaion@18693
   132
          AND res_status != 'CL'
gorkaion@18693
   133
          AND m_reservation_id != v_reservation_id;
gorkaion@18693
   134
        IF (v_count > 0) THEN
gorkaion@18693
   135
          RAISE_APPLICATION_ERROR(-20000, '@SOLineWithMoreThanOneOpenReservation@');
gorkaion@18693
   136
        END IF;
gorkaion@18693
   137
  
gorkaion@18693
   138
        UPDATE c_orderline
gorkaion@18693
   139
        SET so_res_status = CASE WHEN v_quantity = v_reservedqty THEN 'CR'
gorkaion@18693
   140
                                 ELSE 'PR'
gorkaion@18693
   141
                            END
gorkaion@18693
   142
        WHERE c_orderline_id = v_orderline_id;
gorkaion@18693
   143
  
gorkaion@18693
   144
        SELECT c_order_id INTO v_sales_order_id
gorkaion@18693
   145
        FROM c_orderline
gorkaion@18693
   146
        WHERE c_orderline_id = v_orderline_id;
gorkaion@18693
   147
        SELECT COUNT(*), SUM(CASE so_res_status WHEN 'CR' THEN 1 ELSE 0 END), SUM(CASE so_res_status WHEN 'PR' THEN 1 ELSE 0 END)
gorkaion@18693
   148
          INTO v_linecount, v_creservedcount, v_preservedcount
gorkaion@18693
   149
        FROM c_orderline
gorkaion@18693
   150
        WHERE c_order_id = v_sales_order_id;
gorkaion@18693
   151
  
gorkaion@18693
   152
        UPDATE c_order
gorkaion@18693
   153
        SET so_res_status = CASE WHEN v_linecount = v_creservedcount THEN 'CR'
gorkaion@18693
   154
                                 WHEN v_creservedcount + v_preservedcount > 0 THEN 'PR'
gorkaion@18693
   155
                                 ELSE 'NR'
gorkaion@18693
   156
                            END
gorkaion@18693
   157
        WHERE c_order_id = v_sales_order_id;
gorkaion@18693
   158
      END IF;
gorkaion@18693
   159
  
gorkaion@18693
   160
      v_newstatus := 'CO';
gorkaion@18693
   161
      v_newaction := 'HO';
gorkaion@18693
   162
      -- ADD RESERVED STOCK
gorkaion@18693
   163
      M_RESERVE_STOCK_AUTO(v_reservation_id, v_user_id, v_message);
gorkaion@18693
   164
    END;
gorkaion@18649
   165
gorkaion@18649
   166
  ELSIF (v_resaction = 'HO') THEN
gorkaion@18649
   167
  /*
gorkaion@18649
   168
    Reservation Hold Process
gorkaion@18649
   169
  */
gorkaion@18649
   170
    IF (v_resstatus != 'CO') THEN
gorkaion@18649
   171
      RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@18649
   172
    END IF;
gorkaion@18649
   173
    v_newstatus := 'HO';
gorkaion@18649
   174
    v_newaction := 'UNHO';
gorkaion@18649
   175
  
gorkaion@18649
   176
  
gorkaion@18649
   177
  ELSIF (v_resaction = 'UNHO') THEN
gorkaion@18649
   178
  /*
gorkaion@18649
   179
    Reservation Un-Hold Process
gorkaion@18649
   180
  */
gorkaion@18649
   181
    IF (v_resstatus != 'HO') THEN
gorkaion@18649
   182
      RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@18649
   183
    END IF;
gorkaion@18649
   184
    v_newstatus := 'CO';
gorkaion@18649
   185
    v_newaction := 'HO';
gorkaion@18649
   186
gorkaion@18649
   187
  ELSIF (v_resaction = 'RE') THEN
gorkaion@18649
   188
  /*
gorkaion@18649
   189
    Reservation Reactivate Process
gorkaion@18649
   190
  */
gorkaion@18693
   191
    DECLARE
gorkaion@18693
   192
      v_sales_order_id        VARCHAR2(32);
gorkaion@18693
   193
      v_reservedcount         NUMBER;
gorkaion@18693
   194
    BEGIN
gorkaion@18693
   195
gorkaion@18693
   196
      IF (v_resstatus != 'CO') THEN
gorkaion@18693
   197
        RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@18693
   198
      END IF;
gorkaion@18693
   199
gorkaion@18693
   200
      UPDATE c_orderline
gorkaion@18693
   201
      SET so_res_status = 'NR'
gorkaion@18693
   202
      WHERE c_orderline_id = v_orderline_id;
gorkaion@18693
   203
gorkaion@18693
   204
      SELECT c_order_id INTO v_sales_order_id
gorkaion@18693
   205
      FROM c_orderline
gorkaion@18693
   206
      WHERE c_orderline_id = v_orderline_id;
gorkaion@18693
   207
      SELECT COUNT(*) INTO v_reservedcount
gorkaion@18693
   208
      FROM c_orderline
gorkaion@18693
   209
      WHERE c_order_id = v_sales_order_id
gorkaion@18693
   210
        AND so_res_status <> 'NR';
gorkaion@18693
   211
gorkaion@18693
   212
      UPDATE c_order
gorkaion@18693
   213
      SET so_res_status = CASE WHEN v_reservedcount > 0 THEN 'PR'
gorkaion@18693
   214
                               ELSE 'NR'
gorkaion@18693
   215
                          END
gorkaion@18693
   216
      WHERE c_order_id = v_sales_order_id;
gorkaion@18693
   217
gorkaion@18693
   218
      v_newstatus := 'DR';
gorkaion@18693
   219
      v_newaction := 'PR';
gorkaion@18693
   220
    END;
gorkaion@18649
   221
  
gorkaion@18649
   222
  ELSIF (v_resaction = 'CL') THEN
gorkaion@18649
   223
  /*
gorkaion@18649
   224
    Reservation Close Process
gorkaion@18649
   225
  */
gorkaion@18649
   226
    IF (v_resstatus != 'CO' AND v_resstatus != 'HO') THEN
gorkaion@18649
   227
      RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@18649
   228
    END IF;
gorkaion@18649
   229
    v_newstatus := 'CL';
gorkaion@18649
   230
    v_newaction := 'CL';
gorkaion@18649
   231
gorkaion@18649
   232
  ELSE
gorkaion@18649
   233
    RAISE_APPLICATION_ERROR(-20000, '@UnsupportedResAction@');
gorkaion@18649
   234
  END IF;
gorkaion@18649
   235
gorkaion@18649
   236
  UPDATE m_reservation
gorkaion@18649
   237
  SET res_status = v_newstatus,
gorkaion@18649
   238
      res_process = v_newaction,
gorkaion@18649
   239
      updated = now(),
gorkaion@18649
   240
      updatedby = v_user_id
gorkaion@18649
   241
  WHERE m_reservation_id = v_reservation_id;
gorkaion@18649
   242
gorkaion@18649
   243
  IF (p_pinstance_id IS NOT NULL) THEN
gorkaion@18649
   244
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
gorkaion@18649
   245
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
gorkaion@18649
   246
    DBMS_OUTPUT.PUT_LINE('--<<M_Reservation_Post finished>> ' || v_Message) ;
gorkaion@18649
   247
  END IF;
gorkaion@18649
   248
  RETURN;
gorkaion@18649
   249
END; --BODY
gorkaion@18649
   250
EXCEPTION
gorkaion@18649
   251
WHEN OTHERS THEN
gorkaion@18649
   252
  v_ResultStr:= '@ERROR=' || SQLERRM;
gorkaion@18649
   253
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
gorkaion@18649
   254
  IF (p_pinstance_id IS NOT NULL) THEN
gorkaion@18649
   255
    ROLLBACK;
gorkaion@18649
   256
    AD_UPDATE_PINSTANCE(p_pinstance_id, NULL, 'N', 0, v_ResultStr) ;
gorkaion@18649
   257
  ELSE
gorkaion@18649
   258
    RAISE;
gorkaion@18649
   259
  END IF;
gorkaion@18649
   260
  RETURN;
gorkaion@18649
   261
END M_RESERVATION_POST
gorkaion@18649
   262
]]></body>
gorkaion@18649
   263
    </function>
gorkaion@18649
   264
  </database>