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