From dae737eca119227cdc4f341c0766cab9caec48bb Mon Sep 17 00:00:00 2001 From: Martin Trigaux Date: Thu, 22 Dec 2016 10:16:54 +0100 Subject: [PATCH] [IMP] pos: better default start date 1;2802;0c now() was bad as it excluded the orders of the latest session midnight is not good either because - midnight server != midnight user (timezone) - some restaurant, bars start in the evening and finish after midgnight With this default, with 4 sessions: - POS A, start 2016-12-21 08:05:36 - POS B, start 2016-12-21 08:08:11 - POS A, start 2016-12-22 08:17:51 - POS B, start 2016-12-21 08:19:23 The default start date will be 2016-12-22 08:17:51 to include all orders of both sessions. --- addons/point_of_sale/wizard/pos_details.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/addons/point_of_sale/wizard/pos_details.py b/addons/point_of_sale/wizard/pos_details.py index 3b8c236fccc..4153ae61c9b 100644 --- a/addons/point_of_sale/wizard/pos_details.py +++ b/addons/point_of_sale/wizard/pos_details.py @@ -9,7 +9,25 @@ class PosDetails(models.TransientModel): _name = 'pos.details.wizard' _description = 'Open Sale Details Report' - start_date = fields.Datetime(required=True, default=fields.Datetime.now) + 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() + + start_date = fields.Datetime(required=True, default=_default_start_date) end_date = fields.Datetime(required=True, default=fields.Datetime.now) pos_config_ids = fields.Many2many('pos.config', 'pos_detail_configs', default=lambda s: s.env['pos.config'].search([]))