def get_dates_ranges(self): today = fields.Date.context_today(self) is_account_present = hasattr(self.env.company, 'compute_fiscalyear_dates') this_year = { 'date_from': date(today.year, 1, 1), 'date_to': date(today.year, 12, 31) } last_year = { 'date_from': date(today.year - 1, 1, 1), 'date_to': date(today.year - 1, 12, 31) } this_year_dates = self.env.company.compute_fiscalyear_dates( today) if is_account_present else this_year last_year_dates = self.env.company.compute_fiscalyear_dates( today - relativedelta(years=1)) if is_account_present else last_year this_quarter_from, this_quarter_to = date_utils.get_quarter(today) last_quarter_from, last_quarter_to = date_utils.get_quarter( today - relativedelta(months=3)) this_month_from, this_month_to = date_utils.get_month(today) last_month_from, last_month_to = date_utils.get_month(today - relativedelta( months=1)) return { 'this_year': { 'date_from': this_year_dates['date_from'], 'date_to': this_year_dates['date_to'] }, 'last_year': { 'date_from': last_year_dates['date_from'], 'date_to': last_year_dates['date_to'] }, 'this_quarter': { 'date_from': this_quarter_from, 'date_to': this_quarter_to }, 'last_quarter': { 'date_from': last_quarter_from, 'date_to': last_quarter_to }, 'this_month': { 'date_from': this_month_from, 'date_to': this_month_to }, 'last_month': { 'date_from': last_month_from, 'date_to': last_month_to }, }
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_json_replenishment_history(self): for replenishment_report in self: replenishment_history = [] today = fields.Datetime.now() first_month = subtract(today, month=2) date_from, dummy = get_month(first_month) dummy, date_to = get_month(today) domain = [('product_id', '=', replenishment_report.product_id.id), ('date', '>=', date_from), ('date', '<=', datetime.combine(date_to, time.max)), ('state', '=', 'done'), ('company_id', '=', replenishment_report.orderpoint_id.company_id.id)] quantity_by_month_out = self.env['stock.move'].read_group( AND([domain, [('location_dest_id.usage', '=', 'customer')]]), ['date', 'product_qty'], ['date:month']) quantity_by_month_returned = self.env['stock.move'].read_group( AND([domain, [('location_id.usage', '=', 'customer')]]), ['date', 'product_qty'], ['date:month']) quantity_by_month_returned = { g['date:month']: g['product_qty'] for g in quantity_by_month_returned } for group in quantity_by_month_out: month = group['date:month'] replenishment_history.append({ 'name': month, 'quantity': group['product_qty'] - quantity_by_month_returned.get(month, 0), 'uom_name': replenishment_report.product_id.uom_id.display_name, }) replenishment_report.json_replenishment_history = dumps({ 'template': 'stock.replenishmentHistory', 'replenishment_history': replenishment_history })
def _apply_date_filter(self, options): def create_vals(period_vals): vals = {'string': period_vals['string']} if self.has_single_date_filter(options): vals['date'] = (period_vals['date_to'] or period_vals['date_from'] ).strftime(DEFAULT_SERVER_DATE_FORMAT) else: vals['date_from'] = period_vals['date_from'].strftime( DEFAULT_SERVER_DATE_FORMAT) vals['date_to'] = period_vals['date_to'].strftime( DEFAULT_SERVER_DATE_FORMAT) return vals # ===== Date Filter ===== if not options.get('date') or not options['date'].get('filter'): return options_filter = options['date']['filter'] date_from = None date_to = datetime.date.today() if options_filter == 'custom': if self.has_single_date_filter(options): date_from = None date_to = fields.Date.from_string(options['date']['date']) else: date_from = fields.Date.from_string( options['date']['date_from']) date_to = fields.Date.from_string(options['date']['date_to']) elif 'today' in options_filter: if not self.has_single_date_filter(options): date_from = request.env.user.company_id.compute_fiscalyear_dates( date_to)['date_from'] elif 'month' in options_filter: date_from, date_to = date_utils.get_month(date_to) elif 'quarter' in options_filter: date_from, date_to = date_utils.get_quarter(date_to) elif 'year' in options_filter: company_fiscalyear_dates = request.env.user.company_id.compute_fiscalyear_dates( date_to) date_from = company_fiscalyear_dates['date_from'] date_to = company_fiscalyear_dates['date_to'] else: raise UserError( 'Programmation Error: Unrecognized parameter %s in date filter!' % str(options_filter)) period_vals = self._get_dates_period(options, date_from, date_to) if 'last' in options_filter: period_vals = self._get_dates_previous_period(options, period_vals) options['date'].update(create_vals(period_vals))
def _get_dates_previous_year(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_to = date_from or date_to date_from = None date_to = date_to - relativedelta(years=1) # Take care about the 29th february. # Moving from 2017-02-28 -> 2016-02-28 is wrong! It must be 2016-02-29. if period_type == 'month': date_from, date_to = date_utils.get_month(date_to) elif date_from: date_from = date_from - relativedelta(years=1) return self._get_dates_period(options, date_from, date_to, period_type=period_type)
def _search_invoice(self, exist_supplier, amount, serie_folio, folio, xml_date): inv_obj = self.env['account.move'] domain = [ '|', ('partner_id', 'child_of', exist_supplier.id), ('partner_id', '=', exist_supplier.id) ] force_folio = self.env['ir.config_parameter'].sudo().get_param( 'l10n_mx_force_only_folio', '') if serie_folio and force_folio: domain.append('|') domain.append(('invoice_payment_ref', '=ilike', folio)) if serie_folio: domain.append(('invoice_payment_ref', '=ilike', serie_folio)) return inv_obj.search(domain, limit=1) domain.append(('amount_total', '>=', amount - 1)) domain.append(('amount_total', '<=', amount + 1)) domain.append(('l10n_mx_edi_cfdi_name', '=', False)) domain.append(('state', '!=', 'cancel')) domain.append(('type', 'in', ('in_invoice', 'in_refund'))) omit_state_in_invoices = self.env['ir.config_parameter'].sudo( ).get_param('omit_state_in_invoices', '') if omit_state_in_invoices: omit_state_in_invoices = omit_state_in_invoices.split(",") domain.append(('state', 'not in', omit_state_in_invoices)) domain.append( ('invoice_payment_state', 'not in', omit_state_in_invoices)) date_type = self.env['ir.config_parameter'].sudo().get_param( 'l10n_mx_edi_vendor_bills_force_use_date') xml_date = fields.datetime.strptime(xml_date, '%Y-%m-%dT%H:%M:%S').date() if date_type == "day": domain.append(('invoice_date', '=', xml_date)) elif date_type == "month": invoice_date = date_utils.get_month(xml_date) domain.append(('invoice_date', '>=', invoice_date[0])) domain.append(('invoice_date', '<=', invoice_date[1])) return inv_obj.search(domain, limit=1)
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, }