src-db/database/model/functions/M_RESERVATION_POST.xml
author Gorka Ion Damián <gorkaion.damian@openbravo.com>
Tue, 13 Nov 2012 13:47:07 +0100
changeset 18695 f584300636c8
parent 18693 7b17189c848b
child 18696 018782102b24
permissions -rw-r--r--
[Reservations]Do not allow to reserve unavailable stock.
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@18693
    80
  SELECT res_status, c_orderline_id, quantity, reservedqty
gorkaion@18693
    81
    INTO v_resstatus, v_orderline_id, v_quantity, v_reservedqty
gorkaion@18649
    82
  FROM m_reservation
gorkaion@18649
    83
  WHERE m_reservation_id = v_reservation_id;
gorkaion@18649
    84
  
gorkaion@18649
    85
  IF (v_resaction = 'PR') THEN
gorkaion@18649
    86
  /*
gorkaion@18649
    87
    Reservation Complete Process
gorkaion@18649
    88
  */
gorkaion@18693
    89
    DECLARE
gorkaion@18693
    90
      v_sales_order_id        VARCHAR2(32);
gorkaion@18695
    91
      cur_not_available       RECORD;
gorkaion@18693
    92
      v_linecount             NUMBER;
gorkaion@18693
    93
      v_creservedcount        NUMBER;
gorkaion@18693
    94
      v_preservedcount        NUMBER;
gorkaion@18693
    95
    BEGIN
gorkaion@18693
    96
      IF (v_resstatus != 'DR') THEN
gorkaion@18693
    97
        RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@18689
    98
      END IF;
gorkaion@18695
    99
      -- Check that stock is still available
gorkaion@18695
   100
      FOR cur_not_available IN (
gorkaion@18695
   101
        SELECT AD_COLUMN_IDENTIFIER('M_Locator', rs.m_locator_id, 'en_US') as locatorName,
gorkaion@18695
   102
               asi.description AS attributeName
gorkaion@18695
   103
        FROM m_reservation_stock rs
gorkaion@18695
   104
            JOIN m_attributesetinstance asi ON COALESCE(rs.m_attributesetinstance_id, '0') = asi.m_attributesetinstance_id
gorkaion@18695
   105
            LEFT JOIN m_storage_detail sd ON sd.m_product_id = v_product_id
gorkaion@18695
   106
                                             AND sd.c_uom_id = v_uom_id
gorkaion@18695
   107
                                             AND sd.m_product_uom_id IS NULL
gorkaion@18695
   108
                                             AND COALESCE(sd.m_attributesetinstance_id, '0') = COALESCE(rs.m_attributesetinstance_id, '0')
gorkaion@18695
   109
                                             AND sd.m_locator_id = rs.m_locator_id
gorkaion@18695
   110
            LEFT JOIN (
gorkaion@18695
   111
                    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
   112
                    FROM m_reservation_stock rs2
gorkaion@18695
   113
                      JOIN m_reservation r ON rs2.m_reservation_id = r.m_reservation_id
gorkaion@18695
   114
                    WHERE r.m_product_id = v_product_id
gorkaion@18695
   115
                      AND r.res_status NOT IN ('CL', 'DR')
gorkaion@18695
   116
                      AND r.c_uom_id = v_uom_id
gorkaion@18695
   117
                    GROUP BY rs2.m_locator_id, COALESCE(rs2.m_attributesetinstance_id,'0')
gorkaion@18695
   118
                  ) other ON rs.m_locator_id = other.m_locator_id
gorkaion@18695
   119
                             AND COALESCE(rs.m_attributesetinstance_id, '0') = COALESCE(other.m_attributesetinstance_id, '0')
gorkaion@18695
   120
        WHERE rs.quantity - COALESCE(rs.releasedqty, 0) > COALESCE(sd.qtyonhand, 0) - COALESCE(other.reservedstock, 0)
gorkaion@18695
   121
          AND rs.m_reservation_id = v_reservation_id
gorkaion@18695
   122
      ) LOOP
gorkaion@18695
   123
        RAISE_APPLICATION_ERROR(-20000, '@NotEnoughAvailableStock@ : ' || cur_not_available.locatorName || ' - ' || cur_not_available.attributeName);
gorkaion@18695
   124
      END LOOP;
gorkaion@18695
   125
      
gorkaion@18693
   126
      IF (v_orderline_id IS NOT NULL) THEN
gorkaion@18693
   127
        SELECT count(*) INTO v_count
gorkaion@18693
   128
        FROM m_reservation
gorkaion@18693
   129
        WHERE c_orderline_id = v_orderline_id
gorkaion@18693
   130
          AND res_status != 'CL'
gorkaion@18693
   131
          AND m_reservation_id != v_reservation_id;
gorkaion@18693
   132
        IF (v_count > 0) THEN
gorkaion@18693
   133
          RAISE_APPLICATION_ERROR(-20000, '@SOLineWithMoreThanOneOpenReservation@');
gorkaion@18693
   134
        END IF;
gorkaion@18693
   135
  
gorkaion@18693
   136
        UPDATE c_orderline
gorkaion@18693
   137
        SET so_res_status = CASE WHEN v_quantity = v_reservedqty THEN 'CR'
gorkaion@18693
   138
                                 ELSE 'PR'
gorkaion@18693
   139
                            END
gorkaion@18693
   140
        WHERE c_orderline_id = v_orderline_id;
gorkaion@18693
   141
  
gorkaion@18693
   142
        SELECT c_order_id INTO v_sales_order_id
gorkaion@18693
   143
        FROM c_orderline
gorkaion@18693
   144
        WHERE c_orderline_id = v_orderline_id;
gorkaion@18693
   145
        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
   146
          INTO v_linecount, v_creservedcount, v_preservedcount
gorkaion@18693
   147
        FROM c_orderline
gorkaion@18693
   148
        WHERE c_order_id = v_sales_order_id;
gorkaion@18693
   149
  
gorkaion@18693
   150
        UPDATE c_order
gorkaion@18693
   151
        SET so_res_status = CASE WHEN v_linecount = v_creservedcount THEN 'CR'
gorkaion@18693
   152
                                 WHEN v_creservedcount + v_preservedcount > 0 THEN 'PR'
gorkaion@18693
   153
                                 ELSE 'NR'
gorkaion@18693
   154
                            END
gorkaion@18693
   155
        WHERE c_order_id = v_sales_order_id;
gorkaion@18693
   156
      END IF;
gorkaion@18693
   157
  
gorkaion@18693
   158
      v_newstatus := 'CO';
gorkaion@18693
   159
      v_newaction := 'HO';
gorkaion@18693
   160
      -- ADD RESERVED STOCK
gorkaion@18693
   161
      M_RESERVE_STOCK_AUTO(v_reservation_id, v_user_id, v_message);
gorkaion@18693
   162
    END;
gorkaion@18649
   163
gorkaion@18649
   164
  ELSIF (v_resaction = 'HO') THEN
gorkaion@18649
   165
  /*
gorkaion@18649
   166
    Reservation Hold Process
gorkaion@18649
   167
  */
gorkaion@18649
   168
    IF (v_resstatus != 'CO') THEN
gorkaion@18649
   169
      RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@18649
   170
    END IF;
gorkaion@18649
   171
    v_newstatus := 'HO';
gorkaion@18649
   172
    v_newaction := 'UNHO';
gorkaion@18649
   173
  
gorkaion@18649
   174
  
gorkaion@18649
   175
  ELSIF (v_resaction = 'UNHO') THEN
gorkaion@18649
   176
  /*
gorkaion@18649
   177
    Reservation Un-Hold Process
gorkaion@18649
   178
  */
gorkaion@18649
   179
    IF (v_resstatus != 'HO') THEN
gorkaion@18649
   180
      RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@18649
   181
    END IF;
gorkaion@18649
   182
    v_newstatus := 'CO';
gorkaion@18649
   183
    v_newaction := 'HO';
gorkaion@18649
   184
gorkaion@18649
   185
  ELSIF (v_resaction = 'RE') THEN
gorkaion@18649
   186
  /*
gorkaion@18649
   187
    Reservation Reactivate Process
gorkaion@18649
   188
  */
gorkaion@18693
   189
    DECLARE
gorkaion@18693
   190
      v_sales_order_id        VARCHAR2(32);
gorkaion@18693
   191
      v_reservedcount         NUMBER;
gorkaion@18693
   192
    BEGIN
gorkaion@18693
   193
gorkaion@18693
   194
      IF (v_resstatus != 'CO') THEN
gorkaion@18693
   195
        RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@18693
   196
      END IF;
gorkaion@18693
   197
gorkaion@18693
   198
      UPDATE c_orderline
gorkaion@18693
   199
      SET so_res_status = 'NR'
gorkaion@18693
   200
      WHERE c_orderline_id = v_orderline_id;
gorkaion@18693
   201
gorkaion@18693
   202
      SELECT c_order_id INTO v_sales_order_id
gorkaion@18693
   203
      FROM c_orderline
gorkaion@18693
   204
      WHERE c_orderline_id = v_orderline_id;
gorkaion@18693
   205
      SELECT COUNT(*) INTO v_reservedcount
gorkaion@18693
   206
      FROM c_orderline
gorkaion@18693
   207
      WHERE c_order_id = v_sales_order_id
gorkaion@18693
   208
        AND so_res_status <> 'NR';
gorkaion@18693
   209
gorkaion@18693
   210
      UPDATE c_order
gorkaion@18693
   211
      SET so_res_status = CASE WHEN v_reservedcount > 0 THEN 'PR'
gorkaion@18693
   212
                               ELSE 'NR'
gorkaion@18693
   213
                          END
gorkaion@18693
   214
      WHERE c_order_id = v_sales_order_id;
gorkaion@18693
   215
gorkaion@18693
   216
      v_newstatus := 'DR';
gorkaion@18693
   217
      v_newaction := 'PR';
gorkaion@18693
   218
    END;
gorkaion@18649
   219
  
gorkaion@18649
   220
  ELSIF (v_resaction = 'CL') THEN
gorkaion@18649
   221
  /*
gorkaion@18649
   222
    Reservation Close Process
gorkaion@18649
   223
  */
gorkaion@18649
   224
    IF (v_resstatus != 'CO' AND v_resstatus != 'HO') THEN
gorkaion@18649
   225
      RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@18649
   226
    END IF;
gorkaion@18649
   227
    v_newstatus := 'CL';
gorkaion@18649
   228
    v_newaction := 'CL';
gorkaion@18649
   229
gorkaion@18649
   230
  ELSE
gorkaion@18649
   231
    RAISE_APPLICATION_ERROR(-20000, '@UnsupportedResAction@');
gorkaion@18649
   232
  END IF;
gorkaion@18649
   233
gorkaion@18649
   234
  UPDATE m_reservation
gorkaion@18649
   235
  SET res_status = v_newstatus,
gorkaion@18649
   236
      res_process = v_newaction,
gorkaion@18649
   237
      updated = now(),
gorkaion@18649
   238
      updatedby = v_user_id
gorkaion@18649
   239
  WHERE m_reservation_id = v_reservation_id;
gorkaion@18649
   240
gorkaion@18649
   241
  IF (p_pinstance_id IS NOT NULL) THEN
gorkaion@18649
   242
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
gorkaion@18649
   243
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
gorkaion@18649
   244
    DBMS_OUTPUT.PUT_LINE('--<<M_Reservation_Post finished>> ' || v_Message) ;
gorkaion@18649
   245
  END IF;
gorkaion@18649
   246
  RETURN;
gorkaion@18649
   247
END; --BODY
gorkaion@18649
   248
EXCEPTION
gorkaion@18649
   249
WHEN OTHERS THEN
gorkaion@18649
   250
  v_ResultStr:= '@ERROR=' || SQLERRM;
gorkaion@18649
   251
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
gorkaion@18649
   252
  IF (p_pinstance_id IS NOT NULL) THEN
gorkaion@18649
   253
    ROLLBACK;
gorkaion@18649
   254
    AD_UPDATE_PINSTANCE(p_pinstance_id, NULL, 'N', 0, v_ResultStr) ;
gorkaion@18649
   255
  ELSE
gorkaion@18649
   256
    RAISE;
gorkaion@18649
   257
  END IF;
gorkaion@18649
   258
  RETURN;
gorkaion@18649
   259
END M_RESERVATION_POST
gorkaion@18649
   260
]]></body>
gorkaion@18649
   261
    </function>
gorkaion@18649
   262
  </database>