Fixed bug 29808: Impossible to create several calendars for the same organization
authorVíctor Martínez Romanos <victor.martinez@openbravo.com>
Thu, 07 May 2015 12:11:39 +0200
changeset 26701 19fce9568e90
parent 26700 ba8075e92bef
child 26702 e861122b0b1d
Fixed bug 29808: Impossible to create several calendars for the same organization

Two pieces of code were affected by this bug:
PeriodEventHandler.java: EntityPersistenceEventObserver in charge of checking overlap in manual inserts/updates (or any java process) in c_period table
C_YEARPERIODS: db function associated to the create periods process inside the Fiscal Calendar | Year tab. It also verifies the periods don't overlap other periods.

The fix consists in checking that there is no date overlap per calendar. Before this fix the calendar wasn't taken into account, so it was not possible to define several calendars for the same organization with the same periods.
src-db/database/model/functions/C_YEARPERIODS.xml
src/org/openbravo/event/PeriodEventHandler.java
--- a/src-db/database/model/functions/C_YEARPERIODS.xml	Tue May 19 14:23:05 2015 +0200
+++ b/src-db/database/model/functions/C_YEARPERIODS.xml	Thu May 07 12:11:39 2015 +0200
@@ -19,7 +19,7 @@
   * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
   * All Rights Reserved.
   * Contributor(s): Openbravo SLU
-  * Contributions are Copyright (C) 2001-2012 Openbravo, S.L.U.
+  * Contributions are Copyright (C) 2001-2015 Openbravo, S.L.U.
   *
   * Specifically, this derivative work is based upon the following Compiere
   * file and version.
@@ -130,12 +130,14 @@
         --          DBMS_OUTPUT.PUT_LINE('Adding Period ID: '||v_NextNo);
 
         SELECT COUNT(*) INTO V_COUNT 
-	FROM C_PERIOD 
-	WHERE C_PERIOD_ID <> v_NextNo
-	AND AD_ORG_ID = v_Org_ID
-	AND AD_CLIENT_ID = v_Client_ID
-	AND TO_DATE(ADD_MONTHS(v_StartDate, v_MonthNo-1)) <= ENDDATE 
-	AND ADD_MONTHS(TRUNC(TO_DATE(ADD_MONTHS(v_StartDate, v_MonthNo-1)), 'MM'), 1) -1 >= STARTDATE;
+	FROM C_PERIOD P
+	INNER JOIN C_YEAR Y ON (P.C_YEAR_ID = Y.C_YEAR_ID)
+	WHERE P.C_PERIOD_ID <> v_NextNo
+	AND P.AD_ORG_ID = v_Org_ID
+	AND P.AD_CLIENT_ID = v_Client_ID
+	AND Y.C_CALENDAR_ID = v_Calendar_ID
+	AND TO_DATE(ADD_MONTHS(v_StartDate, v_MonthNo-1)) <= P.ENDDATE 
+	AND ADD_MONTHS(TRUNC(TO_DATE(ADD_MONTHS(v_StartDate, v_MonthNo-1)), 'MM'), 1) -1 >= P.STARTDATE;
 	
 	IF (V_COUNT > 0) THEN
 		RAISE_APPLICATION_ERROR(-20000, '@DatesOverlapped@');
--- a/src/org/openbravo/event/PeriodEventHandler.java	Tue May 19 14:23:05 2015 +0200
+++ b/src/org/openbravo/event/PeriodEventHandler.java	Thu May 07 12:11:39 2015 +0200
@@ -11,7 +11,7 @@
  * under the License.
  * The Original Code is Openbravo ERP.
  * The Initial Developer of the Original Code is Openbravo SLU
- * All portions are Copyright (C) 2013 Openbravo SLU
+ * All portions are Copyright (C) 2013-2015 Openbravo SLU
  * All Rights Reserved.
  * Contributor(s):  ______________________________________.
  *************************************************************************
@@ -34,6 +34,7 @@
 import org.openbravo.database.ConnectionProvider;
 import org.openbravo.erpCommon.utility.Utility;
 import org.openbravo.model.financialmgmt.calendar.Period;
+import org.openbravo.model.financialmgmt.calendar.Year;
 import org.openbravo.service.db.DalConnectionProvider;
 
 public class PeriodEventHandler extends EntityPersistenceEventObserver {
@@ -45,7 +46,8 @@
     return entities;
   }
 
-  public void onUpdate(@Observes EntityUpdateEvent event) {
+  public void onUpdate(@Observes
+  EntityUpdateEvent event) {
     if (!isValidEvent(event)) {
       return;
     }
@@ -54,7 +56,8 @@
     }
   }
 
-  public void onSave(@Observes EntityNewEvent event) {
+  public void onSave(@Observes
+  EntityNewEvent event) {
     if (!isValidEvent(event)) {
       return;
     }
@@ -81,6 +84,8 @@
     criteria.add(Restrictions.ge(Period.PROPERTY_ENDINGDATE, period.getStartingDate()));
     criteria.add(Restrictions.le(Period.PROPERTY_STARTINGDATE, period.getEndingDate()));
     criteria.add(Restrictions.eq(Period.PROPERTY_PERIODTYPE, "S"));
+    criteria.createAlias(Period.PROPERTY_YEAR, "y");
+    criteria.add(Restrictions.eq("y." + Year.PROPERTY_CALENDAR, period.getYear().getCalendar()));
     criteria.setMaxResults(1);
 
     if (criteria.uniqueResult() != null) {