from odoo import models from odoo.exceptions import UserError import pytz from datetime import datetime class PosSession(models.Model): _inherit = 'pos.session' def _check_if_no_draft_orders(self): """ Bypass standard Odoo draft order check if the current time in the user's timezone is before the configured morning_shift_end_time on the pos.config. """ draft_orders = self.get_session_orders().filtered(lambda order: order.state == 'draft') if draft_orders and self.config_id.morning_shift_end_time: user_tz = pytz.timezone(self.env.user.tz or 'UTC') local_dt = datetime.now(pytz.utc).astimezone(user_tz) local_hour = local_dt.hour + (local_dt.minute / 60.0) if local_hour < self.config_id.morning_shift_end_time: # It's the morning shift, we bypass the check return True return super()._check_if_no_draft_orders() def _set_opening_control_data(self, cashbox_value: int, notes: str): """ When a new session is opened, we pull the draft orders from the previous closed sessions of this same config, so the afternoon shift can handle them. """ res = super()._set_opening_control_data(cashbox_value, notes) # Find draft orders belonging to closed sessions of the same POS pending_orders = self.env['pos.order'].search([ ('session_id.config_id', '=', self.config_id.id), ('session_id.state', '=', 'closed'), ('state', '=', 'draft') ]) if pending_orders: pending_orders.write({'session_id': self.id}) return res