diff --git a/fiscalyear.py b/fiscalyear.py index 3213987d44..fb01db7e27 100644 --- a/fiscalyear.py +++ b/fiscalyear.py @@ -34,10 +34,31 @@ class FiscalYear(OSV): 'create_period', 'create_period_3', ] + self._constraints += [ + ('check_dates', + 'Error! You can not have 2 fiscal years that overlaps!', + ['start_date', 'end_date']), + ] def default_state(self, cursor, user, context=None): return 'open' + def check_dates(self, cursor, user, ids): + for fiscalyear in self.browse(cursor, user, ids): + cursor.execute('SELECT id ' \ + 'FROM ' + self._table + ' ' \ + 'WHERE ((start_date <= %s AND end_date >= %s) ' \ + 'OR (start_date <= %s AND end_date >= %s) ' \ + 'OR (start_date >= %s AND end_date <= %s)) ' \ + 'AND id != %s', + (fiscalyear.start_date, fiscalyear.start_date, + fiscalyear.end_date, fiscalyear.end_date, + fiscalyear.start_date, fiscalyear.end_date, + fiscalyear.id)) + if cursor.rowcount: + return False + return True + def create_period(self, cursor, user, ids, context=None, interval=1): ''' Create periods for the fiscal years with month interval @@ -51,6 +72,9 @@ class FiscalYear(OSV): while period_start_date < end_date: period_end_date = period_start_date + \ mx.DateTime.RelativeDateTime(months=interval) + period_end_date = mx.DateTime.DateTime(period_end_date.year, + period_end_date.month, 1) - \ + mx.DateTime.RelativeDateTime(days=1) if period_end_date > end_date: period_end_date = end_date period_obj.create(cursor, user, { @@ -60,8 +84,8 @@ class FiscalYear(OSV): 'end_date': period_end_date.strftime('%Y-%m-%d'), 'fiscalyear': fiscalyear.id, }, context=context) - period_start_date = period_start_date + \ - mx.DateTime.RelativeDateTime(months=interval) + period_start_date = period_end_date + \ + mx.DateTime.RelativeDateTime(days=1) return True def create_period_3(self, cursor, user, ids, context=None): diff --git a/period.py b/period.py index 63eb2e8855..0b491dc30d 100644 --- a/period.py +++ b/period.py @@ -26,9 +26,33 @@ class Period(OSV): ('close', 'Close'), ], 'State', readonly=True, required=True) + def __init__(self): + super(Period, self).__init__() + self._constraints += [ + ('check_dates', + 'Error! You can not have 2 periods that overlaps!', + ['start_date', 'end_date']), + ] + def default_state(self, cursor, user, context=None): return 'open' + def check_dates(self, cursor, user, ids): + for period in self.browse(cursor, user, ids): + cursor.execute('SELECT id ' \ + 'FROM ' + self._table + ' ' \ + 'WHERE ((start_date <= %s AND end_date >= %s) ' \ + 'OR (start_date <= %s AND end_date >= %s) ' \ + 'OR (start_date >= %s AND end_date <= %s)) ' \ + 'AND id != %s', + (period.start_date, period.start_date, + period.end_date, period.end_date, + period.start_date, period.end_date, + period.id)) + if cursor.rowcount: + return False + return True + def find(self, cursor, user, date=None, exception=True, context=None): ''' Return the period for the date or the current date. @@ -101,7 +125,7 @@ class Period(OSV): def unlink(self, cursor, user, ids, context=None): self._check(cursor, user, ids, context=context) - return super(Period, self).unlink(cursor, user, ids, vals, + return super(Period, self).unlink(cursor, user, ids, context=context) def close(self, cursor, user, ids, context=None):