from odoo import models from odoo.exceptions import UserError import pytz from datetime import datetime class PosSession(models.Model): _inherit = 'pos.session' def _is_morning_shift_bypass(self): """Check if we should bypass draft order validation based on current time.""" self.ensure_one() if self.config_id.morning_shift_end_time: user_tz = pytz.timezone('Asia/Jakarta') 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: return True return False def get_session_orders(self): """Hide draft orders during the morning shift bypass period.""" res = super().get_session_orders() if self._is_morning_shift_bypass(): return res.filtered(lambda o: o.state != 'draft') return res def _check_if_no_draft_orders(self): """Bypass check if it's morning shift.""" if self._is_morning_shift_bypass(): 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