def _get_dates_previous_period(self, options, period_vals): period_type = period_vals['period_type'] date_from = period_vals['date_from'] date_to = period_vals['date_to'] if not date_from or not date_to: date = (date_from or date_to).replace(day=1) - datetime.timedelta(days=1) return self._get_dates_period(options, None, date, period_type=period_type) date_to = date_from - datetime.timedelta(days=1) if period_type == 'fiscalyear': company_fiscalyear_dates = request.env.user.company_id.compute_fiscalyear_dates( date_to) return self._get_dates_period( options, company_fiscalyear_dates['date_from'], company_fiscalyear_dates['date_to']) if period_type == 'month': return self._get_dates_period(options, *date_utils.get_month(date_to), period_type='month') if period_type == 'quarter': return self._get_dates_period(options, *date_utils.get_quarter(date_to), period_type='quarter') if period_type == 'year': return self._get_dates_period(options, *date_utils.get_fiscal_year(date_to), period_type='year') date_from = date_to - datetime.timedelta(days=(date_to - date_from).days) return self._get_dates_period(options, date_from, date_to)
def compute_fiscalyear_dates(self, current_date): '''Computes the start and end dates of the fiscal year where the given 'date' belongs to. :param current_date: A datetime.date/datetime.datetime object. :return: A dictionary containing: * date_from * date_to * [Optionally] record: The fiscal year record. ''' self.ensure_one() date_str = current_date.strftime(DEFAULT_SERVER_DATE_FORMAT) # Search a fiscal year record containing the date. # If a record is found, then no need further computation, we get the dates range directly. fiscalyear = self.env['account.fiscal.year'].search([ ('company_id', '=', self.id), ('date_from', '<=', date_str), ('date_to', '>=', date_str), ], limit=1) if fiscalyear: return { 'date_from': fiscalyear.date_from, 'date_to': fiscalyear.date_to, 'record': fiscalyear, } date_from, date_to = date_utils.get_fiscal_year( current_date, day=self.fiscalyear_last_day, month=self.fiscalyear_last_month) date_from_str = date_from.strftime(DEFAULT_SERVER_DATE_FORMAT) date_to_str = date_to.strftime(DEFAULT_SERVER_DATE_FORMAT) # Search for fiscal year records reducing the delta between the date_from/date_to. # This case could happen if there is a gap between two fiscal year records. # E.g. two fiscal year records: 2017-01-01 -> 2017-02-01 and 2017-03-01 -> 2017-12-31. # => The period 2017-02-02 - 2017-02-30 is not covered by a fiscal year record. fiscalyear_from = self.env['account.fiscal.year'].search([ ('company_id', '=', self.id), ('date_from', '<=', date_from_str), ('date_to', '>=', date_from_str), ], limit=1) if fiscalyear_from: date_from = fiscalyear_from.date_to + timedelta(days=1) fiscalyear_to = self.env['account.fiscal.year'].search([ ('company_id', '=', self.id), ('date_from', '<=', date_to_str), ('date_to', '>=', date_to_str), ], limit=1) if fiscalyear_to: date_to = fiscalyear_to.date_from - timedelta(days=1) return {'date_from': date_from, 'date_to': date_to}
def _compute_fy_start_date(self): for wiz in self: if wiz.date_from: date_from, date_to = date_utils.get_fiscal_year( wiz.date_from, day=self.company_id.fiscalyear_last_day, month=int(self.company_id.fiscalyear_last_month), ) wiz.fy_start_date = date_from else: wiz.fy_start_date = False
def compute_fiscalyear_dates(self, current_date): '''Computes the start and end dates of the fiscal year where the given 'date' belongs to. :param current_date: A datetime.date/datetime.datetime object. :return: A dictionary containing: * date_from * date_to * [Optionally] record: The fiscal year record. ''' self.ensure_one() date_str = current_date.strftime(DEFAULT_SERVER_DATE_FORMAT) # Search a fiscal year record containing the date. # If a record is found, then no need further computation, we get the dates range directly. fiscalyear = self.env['account.fiscal.year'].search([ ('company_id', '=', self.id), ('date_from', '<=', date_str), ('date_to', '>=', date_str), ], limit=1) if fiscalyear: return { 'date_from': fiscalyear.date_from, 'date_to': fiscalyear.date_to, 'record': fiscalyear, } date_from, date_to = date_utils.get_fiscal_year( current_date, day=self.fiscalyear_last_day, month=int(self.fiscalyear_last_month)) date_from_str = date_from.strftime(DEFAULT_SERVER_DATE_FORMAT) date_to_str = date_to.strftime(DEFAULT_SERVER_DATE_FORMAT) # Search for fiscal year records reducing the delta between the date_from/date_to. # This case could happen if there is a gap between two fiscal year records. # E.g. two fiscal year records: 2017-01-01 -> 2017-02-01 and 2017-03-01 -> 2017-12-31. # => The period 2017-02-02 - 2017-02-30 is not covered by a fiscal year record. fiscalyear_from = self.env['account.fiscal.year'].search([ ('company_id', '=', self.id), ('date_from', '<=', date_from_str), ('date_to', '>=', date_from_str), ], limit=1) if fiscalyear_from: date_from = fiscalyear_from.date_to + timedelta(days=1) fiscalyear_to = self.env['account.fiscal.year'].search([ ('company_id', '=', self.id), ('date_from', '<=', date_to_str), ('date_to', '>=', date_to_str), ], limit=1) if fiscalyear_to: date_to = fiscalyear_to.date_from - timedelta(days=1) return {'date_from': date_from, 'date_to': date_to}
def _default_cutoff_date(self): today = fields.Date.context_today(self) company = self.env.company date_from, date_to = date_utils.get_fiscal_year( today, day=company.fiscalyear_last_day, month=int(company.fiscalyear_last_month), ) if date_from: return date_from - relativedelta(days=1) else: return False
def compute_fiscalyear_dates(self, current_date): """Computes the start and end dates of the fiscal year where the given 'date' belongs to. :param current_date: A datetime.date/datetime.datetime object. :return: A dictionary containing: * date_from * date_to * [Optionally] record: The fiscal year record. """ self.ensure_one() date_str = current_date.strftime(DEFAULT_SERVER_DATE_FORMAT) # Search a fiscal year record containing the date. fiscalyear = self.env["account.fiscal.year"].search( [ ("company_id", "=", self.id), ("date_from", "<=", date_str), ("date_to", ">=", date_str), ], limit=1, ) if fiscalyear: return { "date_from": fiscalyear.date_from, "date_to": fiscalyear.date_to, "record": fiscalyear, } date_from, date_to = date_utils.get_fiscal_year( current_date, day=self.fiscalyear_last_day, month=int(self.fiscalyear_last_month), ) # Search for fiscal year records reducing # the delta between the date_from/date_to. # This case could happen if there is a gap # between two fiscal year records. # E.g. two fiscal year records: # 2017-01-01 -> 2017-02-01 and 2017-03-01 -> 2017-12-31. # => # The period 2017-02-02 - 2017-02-30 is not covered by a fiscal year record. date_from_str = date_from.strftime(DEFAULT_SERVER_DATE_FORMAT) fiscalyear_from = self.env["account.fiscal.year"].search( [ ("company_id", "=", self.id), ("date_from", "<=", date_from_str), ("date_to", ">=", date_from_str), ], limit=1, ) if fiscalyear_from: date_from = fiscalyear_from.date_to + timedelta(days=1) date_to_str = date_to.strftime(DEFAULT_SERVER_DATE_FORMAT) fiscalyear_to = self.env["account.fiscal.year"].search( [ ("company_id", "=", self.id), ("date_from", "<=", date_to_str), ("date_to", ">=", date_to_str), ], limit=1, ) if fiscalyear_to: date_to = fiscalyear_to.date_from - timedelta(days=1) return { "date_from": date_from, "date_to": date_to, }
from dateutil.relativedelta import relativedelta from odoo.tools import date_utils from odoo.fields import Datetime today = Datetime.today() # today = datetime.strptime('2019-03-29 01:53:48', misc.DEFAULT_SERVER_DATETIME_FORMAT) # Представим, что сейчас 2019-03-29 01:53:48 date_utils.get_month(today) # (datetime.datetime(2019, 3, 1, 0, 0), datetime.datetime(2019, 3, 31, 0, 0)) date_utils.get_quarter(today) # (datetime.datetime(2019, 1, 1, 0, 0), datetime.datetime(2019, 3, 31, 0, 0)) date_utils.get_quarter_number(today) # 1 date_utils.get_fiscal_year(today) # (datetime.datetime(2019, 1, 1, 0, 0), datetime.datetime(2019, 12, 31, 0, 0)) date_utils.start_of(today, 'hour') # 2019-03-29 01:00:00 date_utils.start_of(today, 'day') # 2019-03-29 00:00:00 date_utils.start_of(today, 'week') # 2019-03-25 00:00:00 date_utils.start_of(today, 'month') # 2019-03-01 00:00:00 date_utils.start_of(today, 'quarter') # 2019-01-01 00:00:00 date_utils.start_of(today, 'year') # 2019-01-01 00:00:00 date_utils.end_of(today, 'hour')
def _get_dates_period(self, options, date_from, date_to, period_type=None): def match(dt_from, dt_to): if self.has_single_date_filter(options): return (date_to or date_from) == dt_to else: return (dt_from, dt_to) == (date_from, date_to) string = None # If no date_from or not date_to, we are unable to determine a period if not period_type: date = date_to or date_from company_fiscalyear_dates = request.env.user.company_id.compute_fiscalyear_dates( date) if match(company_fiscalyear_dates['date_from'], company_fiscalyear_dates['date_to']): period_type = 'fiscalyear' if company_fiscalyear_dates.get('record'): string = company_fiscalyear_dates['record'].name elif match(*date_utils.get_month(date)): period_type = 'month' elif match(*date_utils.get_quarter(date)): period_type = 'quarter' elif match(*date_utils.get_fiscal_year(date)): period_type = 'year' else: period_type = 'custom' if not string: fy_day = request.env.user.company_id.fiscalyear_last_day fy_month = request.env.user.company_id.fiscalyear_last_month if self.has_single_date_filter(options): string = _('As of %s') % (format_date( request.env, date_to.strftime(DEFAULT_SERVER_DATE_FORMAT))) elif period_type == 'year' or ( period_type == 'fiscalyear' and (date_from, date_to) == date_utils.get_fiscal_year(date_to)): string = date_to.strftime('%Y') elif period_type == 'fiscalyear' and ( date_from, date_to) == date_utils.get_fiscal_year( date_to, day=fy_day, month=fy_month): string = '%s - %s' % (date_to.year - 1, date_to.year) elif period_type == 'month': string = format_date( request.env, date_to.strftime(DEFAULT_SERVER_DATE_FORMAT), date_format='MMM YYYY') elif period_type == 'quarter': quarter_names = get_quarter_names( 'abbreviated', locale=request.env.context.get('lang') or 'en_US') string = u'%s\N{NO-BREAK SPACE}%s' % (quarter_names[ date_utils.get_quarter_number(date_to)], date_to.year) else: dt_from_str = format_date( request.env, date_from.strftime(DEFAULT_SERVER_DATE_FORMAT)) dt_to_str = format_date( request.env, date_to.strftime(DEFAULT_SERVER_DATE_FORMAT)) string = _('From %s \n to %s') % (dt_from_str, dt_to_str) return { 'string': string, 'period_type': period_type, 'date_from': date_from, 'date_to': date_to, }