src-db/database/model/functions/M_RESERVATION_POST.xml
author Gorka Ion Damián <gorkaion.damian@openbravo.com>
Mon, 12 Nov 2012 09:56:06 +0100
changeset 18689 a17b382229ce
parent 18663 157c92ecd0e2
child 18693 7b17189c848b
permissions -rw-r--r--
[Reservations]Implement reservations in MRP Purchasing.
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@18649
    43
gorkaion@18649
    44
TYPE RECORD IS REF CURSOR;
gorkaion@18649
    45
  Cur_Parameter RECORD;
gorkaion@18649
    46
gorkaion@18649
    47
BEGIN
gorkaion@18649
    48
  IF (p_PInstance_ID IS NOT NULL) THEN
gorkaion@18649
    49
    --  Update AD_PInstance
gorkaion@18649
    50
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_pinstance_id) ;
gorkaion@18649
    51
    v_ResultStr:='PInstanceNotFound';
gorkaion@18649
    52
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
gorkaion@18649
    53
    --  Get Parameters
gorkaion@18649
    54
    v_ResultStr:='ReadingParameters';
gorkaion@18649
    55
    FOR Cur_Parameter IN (
gorkaion@18649
    56
        SELECT i.record_id, i.ad_user_id, p.parametername, p.p_string, p.p_number, p.p_date
gorkaion@18649
    57
        FROM ad_pinstance i
gorkaion@18649
    58
            LEFT JOIN ad_pinstance_para p ON i.ad_pinstance_id=p.ad_pinstance_id
gorkaion@18649
    59
        WHERE i.ad_pinstance_id=p_pinstance_id
gorkaion@18649
    60
        ORDER BY p.seqno
gorkaion@18649
    61
    ) LOOP
gorkaion@18649
    62
      IF (cur_parameter.parametername = 'RES_Action') THEN
gorkaion@18649
    63
        v_resaction := cur_parameter.p_string;
gorkaion@18649
    64
      END IF;
gorkaion@18649
    65
      v_reservation_id := cur_parameter.record_id;
gorkaion@18649
    66
      v_user_id := cur_parameter.ad_user_id;
gorkaion@18649
    67
    END LOOP; --  Get Parameter
gorkaion@18649
    68
    DBMS_OUTPUT.PUT_LINE('  v_Record_ID=' || v_reservation_id) ;
gorkaion@18649
    69
  ELSE
gorkaion@18649
    70
    v_reservation_id := p_reservation_id;
gorkaion@18649
    71
    v_resaction := p_resaction;
gorkaion@18649
    72
    v_user_id := p_user_id;
gorkaion@18649
    73
  END IF;
gorkaion@18649
    74
gorkaion@18649
    75
BEGIN
gorkaion@18689
    76
  SELECT res_status, c_orderline_id
gorkaion@18689
    77
    INTO v_resstatus, v_orderline_id
gorkaion@18649
    78
  FROM m_reservation
gorkaion@18649
    79
  WHERE m_reservation_id = v_reservation_id;
gorkaion@18649
    80
  
gorkaion@18649
    81
  IF (v_resaction = 'PR') THEN
gorkaion@18649
    82
  /*
gorkaion@18649
    83
    Reservation Complete Process
gorkaion@18649
    84
  */
gorkaion@18649
    85
    IF (v_resstatus != 'DR') THEN
gorkaion@18649
    86
      RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@18649
    87
    END IF;
gorkaion@18689
    88
    IF (v_orderline_id IS NOT NULL) THEN
gorkaion@18689
    89
      SELECT count(*) INTO v_count
gorkaion@18689
    90
      FROM m_reservation
gorkaion@18689
    91
      WHERE c_orderline_id = v_orderline_id
gorkaion@18689
    92
        AND res_status != 'CL'
gorkaion@18689
    93
        AND m_reservation_id != v_reservation_id;
gorkaion@18689
    94
      IF (v_count > 0) THEN
gorkaion@18689
    95
        RAISE_APPLICATION_ERROR(-20000, '@SOLineWithMoreThanOneOpenReservation@');
gorkaion@18689
    96
      END IF;
gorkaion@18689
    97
    END IF;
gorkaion@18689
    98
gorkaion@18649
    99
    v_newstatus := 'CO';
gorkaion@18649
   100
    v_newaction := 'HO';
gorkaion@18649
   101
    -- ADD RESERVED STOCK
gorkaion@18663
   102
    M_RESERVE_STOCK_AUTO(v_reservation_id, v_user_id, v_message);
gorkaion@18649
   103
gorkaion@18649
   104
  ELSIF (v_resaction = 'HO') THEN
gorkaion@18649
   105
  /*
gorkaion@18649
   106
    Reservation Hold Process
gorkaion@18649
   107
  */
gorkaion@18649
   108
    IF (v_resstatus != 'CO') THEN
gorkaion@18649
   109
      RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@18649
   110
    END IF;
gorkaion@18649
   111
    v_newstatus := 'HO';
gorkaion@18649
   112
    v_newaction := 'UNHO';
gorkaion@18649
   113
  
gorkaion@18649
   114
  
gorkaion@18649
   115
  ELSIF (v_resaction = 'UNHO') THEN
gorkaion@18649
   116
  /*
gorkaion@18649
   117
    Reservation Un-Hold Process
gorkaion@18649
   118
  */
gorkaion@18649
   119
    IF (v_resstatus != 'HO') THEN
gorkaion@18649
   120
      RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@18649
   121
    END IF;
gorkaion@18649
   122
    v_newstatus := 'CO';
gorkaion@18649
   123
    v_newaction := 'HO';
gorkaion@18649
   124
gorkaion@18649
   125
  ELSIF (v_resaction = 'RE') THEN
gorkaion@18649
   126
  /*
gorkaion@18649
   127
    Reservation Reactivate Process
gorkaion@18649
   128
  */
gorkaion@18649
   129
    IF (v_resstatus != 'CO') THEN
gorkaion@18649
   130
      RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@18649
   131
    END IF;
gorkaion@18649
   132
    v_newstatus := 'DR';
gorkaion@18649
   133
    v_newaction := 'PR';
gorkaion@18649
   134
  
gorkaion@18649
   135
  ELSIF (v_resaction = 'CL') THEN
gorkaion@18649
   136
  /*
gorkaion@18649
   137
    Reservation Close Process
gorkaion@18649
   138
  */
gorkaion@18649
   139
    IF (v_resstatus != 'CO' AND v_resstatus != 'HO') THEN
gorkaion@18649
   140
      RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
gorkaion@18649
   141
    END IF;
gorkaion@18649
   142
    v_newstatus := 'CL';
gorkaion@18649
   143
    v_newaction := 'CL';
gorkaion@18649
   144
gorkaion@18649
   145
  ELSE
gorkaion@18649
   146
    RAISE_APPLICATION_ERROR(-20000, '@UnsupportedResAction@');
gorkaion@18649
   147
  END IF;
gorkaion@18649
   148
gorkaion@18649
   149
  UPDATE m_reservation
gorkaion@18649
   150
  SET res_status = v_newstatus,
gorkaion@18649
   151
      res_process = v_newaction,
gorkaion@18649
   152
      updated = now(),
gorkaion@18649
   153
      updatedby = v_user_id
gorkaion@18649
   154
  WHERE m_reservation_id = v_reservation_id;
gorkaion@18649
   155
gorkaion@18649
   156
  IF (p_pinstance_id IS NOT NULL) THEN
gorkaion@18649
   157
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
gorkaion@18649
   158
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
gorkaion@18649
   159
    DBMS_OUTPUT.PUT_LINE('--<<M_Reservation_Post finished>> ' || v_Message) ;
gorkaion@18649
   160
  END IF;
gorkaion@18649
   161
  RETURN;
gorkaion@18649
   162
END; --BODY
gorkaion@18649
   163
EXCEPTION
gorkaion@18649
   164
WHEN OTHERS THEN
gorkaion@18649
   165
  v_ResultStr:= '@ERROR=' || SQLERRM;
gorkaion@18649
   166
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
gorkaion@18649
   167
  IF (p_pinstance_id IS NOT NULL) THEN
gorkaion@18649
   168
    ROLLBACK;
gorkaion@18649
   169
    AD_UPDATE_PINSTANCE(p_pinstance_id, NULL, 'N', 0, v_ResultStr) ;
gorkaion@18649
   170
  ELSE
gorkaion@18649
   171
    RAISE;
gorkaion@18649
   172
  END IF;
gorkaion@18649
   173
  RETURN;
gorkaion@18649
   174
END M_RESERVATION_POST
gorkaion@18649
   175
]]></body>
gorkaion@18649
   176
    </function>
gorkaion@18649
   177
  </database>