# -*- coding: utf-8 -*- from odoo import api, fields, models, _ class PosDetails(models.TransientModel): _name = 'pos.sale.details.wizard' _description = 'Point of Sale Details User Report' def _default_start_date(self): """ Find the earliest start_date of the latests sessions """ # restrict to configs available to the user config_ids = self.env['pos.config'].search([]).ids # exclude configs has not been opened for 2 days self.env.cr.execute(""" SELECT max(start_at) as start, config_id FROM pos_session WHERE config_id = ANY(%s) AND start_at > (NOW() - INTERVAL '2 DAYS') GROUP BY config_id """, (config_ids,)) latest_start_dates = [res['start'] for res in self.env.cr.dictfetchall()] # earliest of the latest sessions return latest_start_dates and min(latest_start_dates) or fields.Datetime.now() def generate_report(self): data = {'date_start': self.start_date, 'date_stop': self.end_date, 'user_ids': self.user_ids.ids} return self.env.ref('advanced_pos_reports.pos_sale_details_report').report_action([], data=data) start_date = fields.Datetime(required=True, default=_default_start_date) end_date = fields.Datetime(required=True, default=fields.Datetime.now) user_ids = fields.Many2many('res.users', default=lambda s: s.env['pos.session'].search([]).mapped('user_id')) @api.onchange('start_date') def _onchange_start_date(self): if self.start_date and self.end_date and self.end_date < self.start_date: self.end_date = self.start_date @api.onchange('end_date') def _onchange_end_date(self): if self.end_date and self.end_date < self.start_date: self.start_date = self.end_date