src-db/database/model/functions/M_RESERVATION_POST.xml
author Gorka Ion Damián <gorkaion.damian@openbravo.com>
Mon, 12 Nov 2012 18:05:28 +0100
changeset 18693 7b17189c848b
parent 18689 a17b382229ce
child 18695 f584300636c8
permissions -rw-r--r--
[Reservations]Add res status in SO and SOL.
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@18689
    42
  v_count           NUMBER;
gorkaion@18693
    43
  v_quantity        NUMBER;
gorkaion@18693
    44
  v_reservedqty     NUMBER;
gorkaion@18649
    45
gorkaion@18649
    46
TYPE RECORD IS REF CURSOR;
gorkaion@18649
    47
  Cur_Parameter RECORD;
gorkaion@18649
    48
gorkaion@18649
    49
BEGIN
gorkaion@18649
    50
  IF (p_PInstance_ID IS NOT NULL) THEN
gorkaion@18649
    51
    --  Update AD_PInstance
gorkaion@18649
    52
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_pinstance_id) ;
gorkaion@18649
    53
    v_ResultStr:='PInstanceNotFound';
gorkaion@18649
    54
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
gorkaion@18649
    55
    --  Get Parameters
gorkaion@18649
    56
    v_ResultStr:='ReadingParameters';
gorkaion@18649
    57
    FOR Cur_Parameter IN (
gorkaion@18649
    58
        SELECT i.record_id, i.ad_user_id, p.parametername, p.p_string, p.p_number, p.p_date
gorkaion@18649
    59
        FROM ad_pinstance i
gorkaion@18649
    60
            LEFT JOIN ad_pinstance_para p ON i.ad_pinstance_id=p.ad_pinstance_id
gorkaion@18649
    61
        WHERE i.ad_pinstance_id=p_pinstance_id
gorkaion@18649
    62
        ORDER BY p.seqno
gorkaion@18649
    63
    ) LOOP
gorkaion@18649
    64
      IF (cur_parameter.parametername = 'RES_Action') THEN
gorkaion@18649
    65
        v_resaction := cur_parameter.p_string;
gorkaion@18649
    66
      END IF;
gorkaion@18649
    67
      v_reservation_id := cur_parameter.record_id;
gorkaion@18649
    68
      v_user_id := cur_parameter.ad_user_id;
gorkaion@18649
    69
    END LOOP; --  Get Parameter
gorkaion@18649
    70
    DBMS_OUTPUT.PUT_LINE('  v_Record_ID=' || v_reservation_id) ;
gorkaion@18649
    71
  ELSE
gorkaion@18649
    72
    v_reservation_id := p_reservation_id;
gorkaion@18649
    73
    v_resaction := p_resaction;
gorkaion@18649
    74
    v_user_id := p_user_id;
gorkaion@18649
    75
  END IF;
gorkaion@18649
    76
gorkaion@18649
    77
BEGIN
gorkaion@18693
    78
  SELECT res_status, c_orderline_id, quantity, reservedqty
gorkaion@18693
    79
    INTO v_resstatus, v_orderline_id, v_quantity, v_reservedqty
gorkaion@18649
    80
  FROM m_reservation
gorkaion@18649
    81
  WHERE m_reservation_id = v_reservation_id;
gorkaion@18649
    82
  
gorkaion@18649
    83
  IF (v_resaction = 'PR') THEN
gorkaion@18649
    84
  /*
gorkaion@18649
    85
    Reservation Complete Process
gorkaion@18649
    86
  */
gorkaion@18693
    87
    DECLARE
gorkaion@18693
    88
      v_sales_order_id        VARCHAR2(32);
gorkaion@18693
    89
      v_linecount             NUMBER;
gorkaion@18693
    90
      v_creservedcount        NUMBER;
gorkaion@18693
    91
      v_preservedcount        NUMBER;
gorkaion@18693
    92
    BEGIN
gorkaion@18693
    93
      IF (v_resstatus != 'DR') THEN
gorkaion@18693
    94
        RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@18689
    95
      END IF;
gorkaion@18693
    96
      IF (v_orderline_id IS NOT NULL) THEN
gorkaion@18693
    97
        SELECT count(*) INTO v_count
gorkaion@18693
    98
        FROM m_reservation
gorkaion@18693
    99
        WHERE c_orderline_id = v_orderline_id
gorkaion@18693
   100
          AND res_status != 'CL'
gorkaion@18693
   101
          AND m_reservation_id != v_reservation_id;
gorkaion@18693
   102
        IF (v_count > 0) THEN
gorkaion@18693
   103
          RAISE_APPLICATION_ERROR(-20000, '@SOLineWithMoreThanOneOpenReservation@');
gorkaion@18693
   104
        END IF;
gorkaion@18693
   105
  
gorkaion@18693
   106
        UPDATE c_orderline
gorkaion@18693
   107
        SET so_res_status = CASE WHEN v_quantity = v_reservedqty THEN 'CR'
gorkaion@18693
   108
                                 ELSE 'PR'
gorkaion@18693
   109
                            END
gorkaion@18693
   110
        WHERE c_orderline_id = v_orderline_id;
gorkaion@18693
   111
  
gorkaion@18693
   112
        SELECT c_order_id INTO v_sales_order_id
gorkaion@18693
   113
        FROM c_orderline
gorkaion@18693
   114
        WHERE c_orderline_id = v_orderline_id;
gorkaion@18693
   115
        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
   116
          INTO v_linecount, v_creservedcount, v_preservedcount
gorkaion@18693
   117
        FROM c_orderline
gorkaion@18693
   118
        WHERE c_order_id = v_sales_order_id;
gorkaion@18693
   119
  
gorkaion@18693
   120
        UPDATE c_order
gorkaion@18693
   121
        SET so_res_status = CASE WHEN v_linecount = v_creservedcount THEN 'CR'
gorkaion@18693
   122
                                 WHEN v_creservedcount + v_preservedcount > 0 THEN 'PR'
gorkaion@18693
   123
                                 ELSE 'NR'
gorkaion@18693
   124
                            END
gorkaion@18693
   125
        WHERE c_order_id = v_sales_order_id;
gorkaion@18693
   126
      END IF;
gorkaion@18693
   127
  
gorkaion@18693
   128
      v_newstatus := 'CO';
gorkaion@18693
   129
      v_newaction := 'HO';
gorkaion@18693
   130
      -- ADD RESERVED STOCK
gorkaion@18693
   131
      M_RESERVE_STOCK_AUTO(v_reservation_id, v_user_id, v_message);
gorkaion@18693
   132
    END;
gorkaion@18649
   133
gorkaion@18649
   134
  ELSIF (v_resaction = 'HO') THEN
gorkaion@18649
   135
  /*
gorkaion@18649
   136
    Reservation Hold Process
gorkaion@18649
   137
  */
gorkaion@18649
   138
    IF (v_resstatus != 'CO') THEN
gorkaion@18649
   139
      RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@18649
   140
    END IF;
gorkaion@18649
   141
    v_newstatus := 'HO';
gorkaion@18649
   142
    v_newaction := 'UNHO';
gorkaion@18649
   143
  
gorkaion@18649
   144
  
gorkaion@18649
   145
  ELSIF (v_resaction = 'UNHO') THEN
gorkaion@18649
   146
  /*
gorkaion@18649
   147
    Reservation Un-Hold Process
gorkaion@18649
   148
  */
gorkaion@18649
   149
    IF (v_resstatus != 'HO') THEN
gorkaion@18649
   150
      RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@18649
   151
    END IF;
gorkaion@18649
   152
    v_newstatus := 'CO';
gorkaion@18649
   153
    v_newaction := 'HO';
gorkaion@18649
   154
gorkaion@18649
   155
  ELSIF (v_resaction = 'RE') THEN
gorkaion@18649
   156
  /*
gorkaion@18649
   157
    Reservation Reactivate Process
gorkaion@18649
   158
  */
gorkaion@18693
   159
    DECLARE
gorkaion@18693
   160
      v_sales_order_id        VARCHAR2(32);
gorkaion@18693
   161
      v_reservedcount         NUMBER;
gorkaion@18693
   162
    BEGIN
gorkaion@18693
   163
gorkaion@18693
   164
      IF (v_resstatus != 'CO') THEN
gorkaion@18693
   165
        RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@18693
   166
      END IF;
gorkaion@18693
   167
gorkaion@18693
   168
      UPDATE c_orderline
gorkaion@18693
   169
      SET so_res_status = 'NR'
gorkaion@18693
   170
      WHERE c_orderline_id = v_orderline_id;
gorkaion@18693
   171
gorkaion@18693
   172
      SELECT c_order_id INTO v_sales_order_id
gorkaion@18693
   173
      FROM c_orderline
gorkaion@18693
   174
      WHERE c_orderline_id = v_orderline_id;
gorkaion@18693
   175
      SELECT COUNT(*) INTO v_reservedcount
gorkaion@18693
   176
      FROM c_orderline
gorkaion@18693
   177
      WHERE c_order_id = v_sales_order_id
gorkaion@18693
   178
        AND so_res_status <> 'NR';
gorkaion@18693
   179
gorkaion@18693
   180
      UPDATE c_order
gorkaion@18693
   181
      SET so_res_status = CASE WHEN v_reservedcount > 0 THEN 'PR'
gorkaion@18693
   182
                               ELSE 'NR'
gorkaion@18693
   183
                          END
gorkaion@18693
   184
      WHERE c_order_id = v_sales_order_id;
gorkaion@18693
   185
gorkaion@18693
   186
      v_newstatus := 'DR';
gorkaion@18693
   187
      v_newaction := 'PR';
gorkaion@18693
   188
    END;
gorkaion@18649
   189
  
gorkaion@18649
   190
  ELSIF (v_resaction = 'CL') THEN
gorkaion@18649
   191
  /*
gorkaion@18649
   192
    Reservation Close Process
gorkaion@18649
   193
  */
gorkaion@18649
   194
    IF (v_resstatus != 'CO' AND v_resstatus != 'HO') THEN
gorkaion@18649
   195
      RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@18649
   196
    END IF;
gorkaion@18649
   197
    v_newstatus := 'CL';
gorkaion@18649
   198
    v_newaction := 'CL';
gorkaion@18649
   199
gorkaion@18649
   200
  ELSE
gorkaion@18649
   201
    RAISE_APPLICATION_ERROR(-20000, '@UnsupportedResAction@');
gorkaion@18649
   202
  END IF;
gorkaion@18649
   203
gorkaion@18649
   204
  UPDATE m_reservation
gorkaion@18649
   205
  SET res_status = v_newstatus,
gorkaion@18649
   206
      res_process = v_newaction,
gorkaion@18649
   207
      updated = now(),
gorkaion@18649
   208
      updatedby = v_user_id
gorkaion@18649
   209
  WHERE m_reservation_id = v_reservation_id;
gorkaion@18649
   210
gorkaion@18649
   211
  IF (p_pinstance_id IS NOT NULL) THEN
gorkaion@18649
   212
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
gorkaion@18649
   213
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
gorkaion@18649
   214
    DBMS_OUTPUT.PUT_LINE('--<<M_Reservation_Post finished>> ' || v_Message) ;
gorkaion@18649
   215
  END IF;
gorkaion@18649
   216
  RETURN;
gorkaion@18649
   217
END; --BODY
gorkaion@18649
   218
EXCEPTION
gorkaion@18649
   219
WHEN OTHERS THEN
gorkaion@18649
   220
  v_ResultStr:= '@ERROR=' || SQLERRM;
gorkaion@18649
   221
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
gorkaion@18649
   222
  IF (p_pinstance_id IS NOT NULL) THEN
gorkaion@18649
   223
    ROLLBACK;
gorkaion@18649
   224
    AD_UPDATE_PINSTANCE(p_pinstance_id, NULL, 'N', 0, v_ResultStr) ;
gorkaion@18649
   225
  ELSE
gorkaion@18649
   226
    RAISE;
gorkaion@18649
   227
  END IF;
gorkaion@18649
   228
  RETURN;
gorkaion@18649
   229
END M_RESERVATION_POST
gorkaion@18649
   230
]]></body>
gorkaion@18649
   231
    </function>
gorkaion@18649
   232
  </database>