src-db/database/model/triggers/M_RESERVATION_TRG.xml
changeset 18693 7b17189c848b
child 18702 f326d9aadc41
equal deleted inserted replaced
18692:f9f27a33fbda 18693:7b17189c848b
       
     1 <?xml version="1.0"?>
       
     2   <database name="TRIGGER M_RESERVATION_TRG">
       
     3     <trigger name="M_RESERVATION_TRG" table="M_RESERVATION" fires="before" insert="true" update="true" delete="true" foreach="row">
       
     4       <body><![CDATA[
       
     5 
       
     6 /*************************************************************************
       
     7 * The contents of this file are subject to the Openbravo  Public  License
       
     8 * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
       
     9 * Version 1.1  with a permitted attribution clause; you may not  use this
       
    10 * file except in compliance with the License. You  may  obtain  a copy of
       
    11 * the License at http://www.openbravo.com/legal/license.html
       
    12 * Software distributed under the License  is  distributed  on  an "AS IS"
       
    13 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
       
    14 * License for the specific  language  governing  rights  and  limitations
       
    15 * under the License.
       
    16 * The Original Code is Openbravo ERP.
       
    17 * The Initial Developer of the Original Code is Openbravo SLU
       
    18 * All portions are Copyright (C) 2012 Openbravo SLU
       
    19 * All Rights Reserved.
       
    20 * Contributor(s):  ______________________________________.
       
    21 ************************************************************************/
       
    22   v_sales_order_id      VARCHAR2(32);
       
    23   v_linecount           NUMBER;
       
    24   v_creservedcount        NUMBER;
       
    25   v_preservedcount        NUMBER;
       
    26   v_reservedcount       NUMBER;
       
    27 BEGIN
       
    28   IF (AD_isTriggerEnabled()='N') THEN
       
    29     RETURN;
       
    30   END IF;
       
    31 
       
    32   IF (INSERTING OR UPDATING) THEN
       
    33     IF (:NEW.res_status NOT IN ('DR', 'CL') AND :NEW.c_orderline_id IS NOT NULL) THEN
       
    34       UPDATE c_orderline
       
    35       SET so_res_status = CASE WHEN :NEW.quantity = :NEW.reservedqty THEN 'CR'
       
    36                                ELSE 'PR'
       
    37                           END
       
    38       WHERE c_orderline_id = :NEW.c_orderline_id;
       
    39 
       
    40       SELECT c_order_id INTO v_sales_order_id
       
    41       FROM c_orderline
       
    42       WHERE c_orderline_id = :NEW.c_orderline_id;
       
    43       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)
       
    44         INTO v_linecount, v_creservedcount, v_preservedcount
       
    45       FROM c_orderline
       
    46       WHERE c_order_id = v_sales_order_id;
       
    47 
       
    48       UPDATE c_order
       
    49       SET so_res_status = CASE WHEN v_linecount = v_creservedcount THEN 'CR'
       
    50                                WHEN v_creservedcount + v_preservedcount > 0 THEN 'PR'
       
    51                                ELSE 'NR'
       
    52                           END
       
    53       WHERE c_order_id = v_sales_order_id;
       
    54     END IF;
       
    55   END IF;
       
    56 
       
    57   IF (DELETING) THEN
       
    58     IF (:OLD.res_status NOT IN ('DR', 'CL') AND :OLD.c_orderline_id IS NOT NULL) THEN
       
    59       UPDATE c_orderline
       
    60       SET so_res_status = 'NR'
       
    61       WHERE c_orderline_id = :OLD.c_orderline_id;
       
    62 
       
    63       SELECT c_order_id INTO v_sales_order_id
       
    64       FROM c_orderline
       
    65       WHERE c_orderline_id = :OLD.c_orderline_id;
       
    66       SELECT COUNT(*) INTO v_reservedcount
       
    67       FROM c_orderline
       
    68       WHERE c_order_id = v_sales_order_id
       
    69         AND so_res_status <> 'NR';
       
    70 
       
    71       UPDATE c_order
       
    72       SET so_res_status = CASE WHEN v_reservedcount > 0 THEN 'PR'
       
    73                                ELSE 'NR'
       
    74                           END
       
    75       WHERE c_order_id = v_sales_order_id;
       
    76     END IF;
       
    77   END IF;
       
    78 END M_RESERVATION_TRG
       
    79 ]]></body>
       
    80     </trigger>
       
    81   </database>