76 lines
3.2 KiB
Python
76 lines
3.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
from odoo import fields, models, api
|
|
from odoo.osv import expression
|
|
from datetime import timedelta, datetime
|
|
import pytz
|
|
|
|
|
|
class PosOrder(models.Model):
|
|
_inherit = 'pos.order'
|
|
|
|
@api.model
|
|
def search_paid_order_ids(self, config_id, domain, limit, offset):
|
|
"""Limit paid orders list loaded on POS frontend to today and the day before."""
|
|
user_tz_name = self.env.context.get('tz') or self.env.user.tz or 'UTC'
|
|
try:
|
|
user_tz = pytz.timezone(user_tz_name)
|
|
except pytz.UnknownTimeZoneError:
|
|
user_tz = pytz.UTC
|
|
|
|
today_local = fields.Date.to_date(fields.Date.context_today(self))
|
|
yesterday_local = today_local - timedelta(days=1)
|
|
yesterday_start_local = datetime.combine(yesterday_local, datetime.min.time())
|
|
yesterday_start_tz = user_tz.localize(yesterday_start_local)
|
|
yesterday_start_utc = yesterday_start_tz.astimezone(pytz.UTC).replace(tzinfo=None)
|
|
|
|
# Restrict domain so only orders from today and yesterday (local timezone start) are returned
|
|
domain = expression.AND([domain, [('date_order', '>=', yesterday_start_utc)]])
|
|
|
|
return super(PosOrder, self).search_paid_order_ids(config_id, domain, limit, offset)
|
|
|
|
@api.model
|
|
def check_table_has_real_orders(self, table_id, local_order_ids=None):
|
|
"""
|
|
Check whether a restaurant table has real (non-empty, non-cancelled) orders
|
|
on the server, excluding the empty local order IDs known to the calling device.
|
|
|
|
This is a safety guard for the multi-device "Release table" scenario:
|
|
- Device X creates an order on table T and syncs it to the server.
|
|
- Device Y opens table T and sees an empty local order (sync lag).
|
|
- Before allowing Device Y to release (cancel) the empty order, we verify
|
|
with the server that no real orderlines exist for this table.
|
|
|
|
Args:
|
|
table_id (int): The restaurant.table ID to check.
|
|
local_order_ids (list[int]): Server IDs of orders already known to the
|
|
calling device (typically the empty local order).
|
|
|
|
Returns:
|
|
dict:
|
|
- has_real_orders (bool): True if real draft orders with non-zero lines exist.
|
|
- order_count (int): Number of matching server orders found.
|
|
"""
|
|
if local_order_ids is None:
|
|
local_order_ids = []
|
|
|
|
# Find draft (non-finalized) orders for this table that have lines
|
|
domain = [
|
|
('table_id', '=', table_id),
|
|
('state', '=', 'draft'),
|
|
('lines', '!=', False),
|
|
]
|
|
if local_order_ids:
|
|
# Exclude the empty order(s) already known to the calling device so we
|
|
# don't false-positive on an order that this device itself created.
|
|
domain = expression.AND([domain, [('id', 'not in', local_order_ids)]])
|
|
|
|
orders = self.env['pos.order'].search(domain)
|
|
|
|
# Double-check: at least one line with non-zero qty must exist
|
|
real_orders = orders.filtered(lambda o: any(line.qty != 0 for line in o.lines))
|
|
|
|
return {
|
|
'has_real_orders': bool(real_orders),
|
|
'order_count': len(real_orders),
|
|
}
|