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.
<?xml version="1.0"?>
  <database name="TRIGGER M_RESERVATION_TRG">
    <trigger name="M_RESERVATION_TRG" table="M_RESERVATION" fires="before" insert="true" update="true" delete="true" foreach="row">
      <body><![CDATA[

/*************************************************************************
* The contents of this file are subject to the Openbravo  Public  License
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
* Version 1.1  with a permitted attribution clause; you may not  use this
* file except in compliance with the License. You  may  obtain  a copy of
* the License at http://www.openbravo.com/legal/license.html
* Software distributed under the License  is  distributed  on  an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific  language  governing  rights  and  limitations
* under the License.
* The Original Code is Openbravo ERP.
* The Initial Developer of the Original Code is Openbravo SLU
* All portions are Copyright (C) 2012 Openbravo SLU
* All Rights Reserved.
* Contributor(s):  ______________________________________.
************************************************************************/
  v_sales_order_id      VARCHAR2(32);
  v_linecount           NUMBER;
  v_creservedcount        NUMBER;
  v_preservedcount        NUMBER;
  v_reservedcount       NUMBER;
BEGIN
  IF (AD_isTriggerEnabled()='N') THEN
    RETURN;
  END IF;

  IF (INSERTING OR UPDATING) THEN
    IF (:NEW.res_status NOT IN ('DR', 'CL') AND :NEW.c_orderline_id IS NOT NULL) THEN
      UPDATE c_orderline
      SET so_res_status = CASE WHEN :NEW.quantity = :NEW.reservedqty THEN 'CR'
                               ELSE 'PR'
                          END
      WHERE c_orderline_id = :NEW.c_orderline_id;

      SELECT c_order_id INTO v_sales_order_id
      FROM c_orderline
      WHERE c_orderline_id = :NEW.c_orderline_id;
      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)
        INTO v_linecount, v_creservedcount, v_preservedcount
      FROM c_orderline
      WHERE c_order_id = v_sales_order_id;

      UPDATE c_order
      SET so_res_status = CASE WHEN v_linecount = v_creservedcount THEN 'CR'
                               WHEN v_creservedcount + v_preservedcount > 0 THEN 'PR'
                               ELSE 'NR'
                          END
      WHERE c_order_id = v_sales_order_id;
    END IF;
  END IF;

  IF (DELETING) THEN
    IF (:OLD.res_status NOT IN ('DR', 'CL') AND :OLD.c_orderline_id IS NOT NULL) THEN
      UPDATE c_orderline
      SET so_res_status = 'NR'
      WHERE c_orderline_id = :OLD.c_orderline_id;

      SELECT c_order_id INTO v_sales_order_id
      FROM c_orderline
      WHERE c_orderline_id = :OLD.c_orderline_id;
      SELECT COUNT(*) INTO v_reservedcount
      FROM c_orderline
      WHERE c_order_id = v_sales_order_id
        AND so_res_status <> 'NR';

      UPDATE c_order
      SET so_res_status = CASE WHEN v_reservedcount > 0 THEN 'PR'
                               ELSE 'NR'
                          END
      WHERE c_order_id = v_sales_order_id;
    END IF;
  END IF;
END M_RESERVATION_TRG
]]></body>
    </trigger>
  </database>