src-db/database/model/triggers/C_PERIOD_TRG2.xml
author Javier Etxarri <javier.echarri@openbravo.com>
Thu, 31 May 2012 11:30:47 +0200
changeset 9867 55c5e981efd3
parent 9325 7f853bd7be35
child 9893 0a548ca8374d
permissions -rw-r--r--
Fixes issue 20654: Should not be possible to create several periods for same date range in different years
adrian@94
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="TRIGGER C_PERIOD_TRG2">
ainhoa@9292
     3
    <trigger name="C_PERIOD_TRG2" table="C_PERIOD" fires="before" insert="true" update="true" delete="true" foreach="row">
gorkaion@239
     4
      <body><![CDATA[
juanpablo@771
     5
    /*************************************************************************
juanpablo@771
     6
    * The contents of this file are subject to the Compiere Public
juanpablo@771
     7
    * License 1.1 ("License"); You may not use this file except in
juanpablo@771
     8
    * compliance with the License. You may obtain a copy of the License in
juanpablo@771
     9
    * the legal folder of your Openbravo installation.
adrian@94
    10
    * Software distributed under the License is distributed on an
adrian@94
    11
    * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
adrian@94
    12
    * implied. See the License for the specific language governing rights
adrian@94
    13
    * and limitations under the License.
juanpablo@778
    14
    * The Original Code is  Compiere  ERP &  Business Solution
juanpablo@771
    15
    * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
adrian@94
    16
    * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
adrian@94
    17
    * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
adrian@94
    18
    * All Rights Reserved.
ggi@6701
    19
    * Contributor(s): Openbravo SLU
ainhoa@9292
    20
    * Contributions are Copyright (C) 2001-2011 Openbravo, S.L.U.
juanpablo@771
    21
    *
juanpablo@771
    22
    * Specifically, this derivative work is based upon the following Compiere
juanpablo@771
    23
    * file and version.
adrian@94
    24
    *************************************************************************
adrian@94
    25
    * Calculate End Date
adrian@94
    26
    */
gorkaion@280
    27
    v_DateNull DATE := TO_DATE('01-01-1900','DD-MM-YYYY');
adrianromero@9325
    28
    V_COUNT NUMBER:= 0;
adrianromero@9325
    29
asier@2078
    30
BEGIN
asier@2084
    31
    
asier@2084
    32
    IF AD_isTriggerEnabled()='N' THEN RETURN;
asier@2078
    33
    END IF;
asier@2078
    34
adrian@94
    35
   IF(UPDATING) THEN
adrianromero@9325
    36
     IF COALESCE(:old.StartDate, v_DateNull) <> COALESCE(:NEW.StartDate, v_DateNull) THEN
adrianromero@9325
    37
	:new.EndDate:= ADD_MONTHS(TRUNC(:new.StartDate, 'MM'), 1) -1;
ainhoa@9292
    38
     END IF;
ainhoa@9292
    39
   END IF;
ainhoa@9292
    40
ainhoa@9292
    41
   IF(INSERTING) THEN
ainhoa@9292
    42
        :new.EndDate:= ADD_MONTHS(TRUNC(:new.StartDate, 'MM'), 1) -1;
ainhoa@9292
    43
   END IF;
ainhoa@9292
    44
adrianromero@9325
    45
   -- Validating dates period
adrianromero@9325
    46
   IF(UPDATING OR INSERTING) THEN
adrianromero@9325
    47
adrianromero@9325
    48
	IF (INSERTING AND :NEW.PERIODTYPE = 'C_YEARPERIOD') THEN
adrianromero@9325
    49
		-- Creating periods from procedure, no need to check dates
adrianromero@9325
    50
		:NEW.PERIODTYPE := 'S';
adrianromero@9325
    51
	ELSE
adrianromero@9325
    52
		IF (:NEW.STARTDATE > :NEW.ENDDATE) THEN
adrianromero@9325
    53
			RAISE_APPLICATION_ERROR(-20000, '@DatesWrong@');
adrianromero@9325
    54
		END IF;
javier@9867
    55
		SELECT COUNT(*) INTO V_COUNT 
javier@9867
    56
		FROM C_PERIOD 
javier@9867
    57
		WHERE C_PERIOD_ID <> :NEW.C_PERIOD_ID 
javier@9867
    58
			AND AD_ORG_ID = :NEW.AD_ORG_ID
javier@9867
    59
			AND :NEW.STARTDATE <= ENDDATE 
javier@9867
    60
			AND :NEW.ENDDATE >= STARTDATE;
adrianromero@9325
    61
		IF (V_COUNT > 0) THEN
adrianromero@9325
    62
			RAISE_APPLICATION_ERROR(-20000, '@DatesOverlapped@');
adrianromero@9325
    63
		END IF;
adrianromero@9325
    64
	END IF;
adrianromero@9325
    65
   END IF;
adrianromero@9325
    66
ainhoa@9292
    67
   IF DELETING THEN
ainhoa@9292
    68
        DELETE FROM C_PeriodControl_log
ainhoa@9292
    69
	WHERE periodno=:old.C_Period_ID;
ainhoa@9292
    70
ainhoa@9292
    71
	DELETE FROM C_PeriodControl
ainhoa@9292
    72
	WHERE C_Period_ID=:old.C_Period_ID;
ainhoa@9292
    73
   END IF;
ainhoa@9292
    74
antonio@735
    75
END C_PERIOD_TRG2
gorkaion@239
    76
]]></body>
adrian@94
    77
    </trigger>
adrian@94
    78
  </database>