| 1 |
from odoo import fields, models, api, _ |
| 2 |
from odoo.exceptions import ValidationError, UserError |
| 3 |
from datetime import datetime, timedelta |
| 4 |
from collections import defaultdict |
| 5 |
|
| 6 |
|
| 7 |
class PosPreset(models.Model): |
| 8 |
_name = 'pos.preset' |
| 9 |
_inherit = ['pos.load.mixin'] |
| 10 |
_description = 'Easily load a set of configuration options' |
| 11 |
|
| 12 |
name = fields.Char(string='Label', required=True, translate=True) |
| 13 |
pricelist_id = fields.Many2one('product.pricelist', string='Pricelist') |
| 14 |
fiscal_position_id = fields.Many2one('account.fiscal.position', string='Fiscal Position') |
| 15 |
identification = fields.Selection([('none', 'Not required'), ('address', 'Address'), ('name', 'Name')], default="none", string='Identification', required=True) |
| 16 |
is_return = fields.Boolean(string='Return mode', default=False, help="All quantity in the cart will be in negative. Ideal for return managment.") |
| 17 |
color = fields.Integer(string='Color', default=0) |
| 18 |
image_512 = fields.Image(string='Image', max_width=512, max_height=512) |
| 19 |
image_128 = fields.Image(string='Image 128', related="image_512", max_width=128, max_height=128, store=True) |
| 20 |
has_image = fields.Boolean(compute='_compute_has_image') |
| 21 |
count_linked_orders = fields.Integer(compute='_compute_count_linked_orders') |
| 22 |
count_linked_config = fields.Integer(compute='_compute_count_linked_config') |
| 23 |
|
| 24 |
# Timing options |
| 25 |
use_timing = fields.Boolean(string='Manage orders by time', default=False) |
| 26 |
resource_calendar_id = fields.Many2one('resource.calendar', 'Resource') |
| 27 |
attendance_ids = fields.One2many(related="resource_calendar_id.attendance_ids", string="Attendances", readonly=False) |
| 28 |
slots_per_interval = fields.Integer(string='Capacity', default=5) |
| 29 |
interval_time = fields.Integer(string='Interval time (in min)', default=20) |
| 30 |
|
| 31 |
@api.constrains('attendance_ids') |
| 32 |
def _check_slots(self): |
| 33 |
for preset in self: |
| 34 |
for attendance in preset.attendance_ids: |
| 35 |
if attendance.hour_from % 24 >= attendance.hour_to % 24: |
| 36 |
raise ValidationError(_('The start time must be before the end time.')) |
| 37 |
|
| 38 |
@api.model |
| 39 |
def _load_pos_data_domain(self, data, config): |
| 40 |
preset_ids = config.available_preset_ids.ids + [config.default_preset_id.id] |
| 41 |
return [('id', 'in', preset_ids)] |
| 42 |
|
| 43 |
@api.model |
| 44 |
def _load_pos_data_fields(self, config): |
| 45 |
return ['id', 'name', 'pricelist_id', 'fiscal_position_id', 'is_return', 'color', 'has_image', 'write_date', 'identification', |
| 46 |
'use_timing', 'slots_per_interval', 'interval_time', 'attendance_ids'] |
| 47 |
|
| 48 |
def _compute_count_linked_orders(self): |
| 49 |
for record in self: |
| 50 |
record.count_linked_orders = self.env['pos.order'].search_count([('preset_id', 'in', record.ids)]) |
| 51 |
|
| 52 |
def _compute_count_linked_config(self): |
| 53 |
for record in self: |
| 54 |
record.count_linked_config = self.env['pos.config'].search_count([ |
| 55 |
'|', ('default_preset_id', 'in', record.ids), |
| 56 |
('available_preset_ids', 'in', record.ids) |
| 57 |
]) |
| 58 |
|
| 59 |
@api.depends('has_image') |
| 60 |
def _compute_has_image(self): |
| 61 |
for record in self: |
| 62 |
record.has_image = bool(record.image_512) |
| 63 |
|
| 64 |
# Slots are created directly here in the form of dates, to avoid polluting |
| 65 |
# the database with a “slots” model. All we need is the slot time, and with the preset |
| 66 |
# information we can deduce the maximum occupancy per slot. |
| 67 |
def get_available_slots(self): |
| 68 |
self.ensure_one() |
| 69 |
usage = self._compute_slots_usage() |
| 70 |
return { |
| 71 |
'usage_utc': usage, |
| 72 |
} |
| 73 |
|
| 74 |
def _compute_slots_usage(self): |
| 75 |
usage = defaultdict(int) |
| 76 |
orders = self.env['pos.order'].search([ |
| 77 |
('preset_id', '=', self.id), |
| 78 |
('session_id.state', '=', 'opened'), |
| 79 |
('preset_time', '!=', False), |
| 80 |
('state', 'in', ['draft', 'paid']), |
| 81 |
('create_date', '>=', fields.Datetime.now() - timedelta(days=1)) |
| 82 |
]) |
| 83 |
for order in orders: |
| 84 |
sql_datetime_str = order.preset_time.strftime("%Y-%m-%d %H:%M:%S") |
| 85 |
|
| 86 |
if not usage[sql_datetime_str]: |
| 87 |
usage[sql_datetime_str] = [] |
| 88 |
|
| 89 |
usage[sql_datetime_str].append(order.id) |
| 90 |
|
| 91 |
return usage |
| 92 |
|
| 93 |
def action_open_linked_orders(self): |
| 94 |
self.ensure_one() |
| 95 |
return { |
| 96 |
'name': _('Linked Orders'), |
| 97 |
'view_mode': 'list', |
| 98 |
'res_model': 'pos.order', |
| 99 |
'type': 'ir.actions.act_window', |
| 100 |
'domain': [('preset_id', '=', self.id)], |
| 101 |
} |
| 102 |
|
| 103 |
def action_open_linked_config(self): |
| 104 |
self.ensure_one() |
| 105 |
return { |
| 106 |
'name': _('Linked POS Configurations'), |
| 107 |
'view_mode': 'list', |
| 108 |
'res_model': 'pos.config', |
| 109 |
'type': 'ir.actions.act_window', |
| 110 |
'domain': ['|', ('default_preset_id', '=', self.id), ('available_preset_ids', 'in', self.id)] |
| 111 |
} |
| 112 |
|
| 113 |
@api.ondelete(at_uninstall=False) |
| 114 |
def _unlink_except_used_preset(self): |
| 115 |
for preset in self: |
| 116 |
if preset.count_linked_config: |
| 117 |
raise UserError(_('You cannot delete a preset that is linked to a POS configuration.')) |