| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from odoo import api, fields, models, _, Command |
| 5 |
from odoo.exceptions import UserError |
| 6 |
|
| 7 |
|
| 8 |
class RestaurantFloor(models.Model): |
| 9 |
_name = 'restaurant.floor' |
| 10 |
|
| 11 |
_description = 'Restaurant Floor' |
| 12 |
_order = "sequence, name" |
| 13 |
_inherit = ['pos.load.mixin'] |
| 14 |
|
| 15 |
name = fields.Char('Floor Name', required=True) |
| 16 |
pos_config_ids = fields.Many2many('pos.config', string='Point of Sales', domain="[('module_pos_restaurant', '=', True)]") |
| 17 |
background_image = fields.Binary('Background Image') |
| 18 |
background_color = fields.Char('Background Color', help='The background color of the floor in a html-compatible format') |
| 19 |
table_ids = fields.One2many('restaurant.table', 'floor_id', string='Tables') |
| 20 |
sequence = fields.Integer('Sequence', default=1) |
| 21 |
active = fields.Boolean(default=True) |
| 22 |
floor_background_image = fields.Image(string='Floor Background Image') |
| 23 |
|
| 24 |
@api.model |
| 25 |
def _load_pos_data_domain(self, data, config): |
| 26 |
return [('pos_config_ids', '=', config.id)] |
| 27 |
|
| 28 |
@api.model |
| 29 |
def _load_pos_data_fields(self, config): |
| 30 |
return ['name', 'background_color', 'table_ids', 'sequence', 'pos_config_ids', 'floor_background_image', 'active'] |
| 31 |
|
| 32 |
@api.ondelete(at_uninstall=False) |
| 33 |
def _unlink_except_active_pos_session(self): |
| 34 |
confs = self.mapped('pos_config_ids').filtered(lambda c: c.module_pos_restaurant) |
| 35 |
opened_session = self.env['pos.session'].search([('config_id', 'in', confs.ids), ('state', '!=', 'closed')]) |
| 36 |
if opened_session and confs: |
| 37 |
error_msg = _("You cannot remove a floor that is used in a PoS session, close the session(s) first: \n") |
| 38 |
for floor in self: |
| 39 |
for session in opened_session: |
| 40 |
if floor in session.config_id.floor_ids: |
| 41 |
error_msg += _("Floor: %(floor)s - PoS Config: %(config)s \n", floor=floor.name, config=session.config_id.name) |
| 42 |
raise UserError(error_msg) |
| 43 |
|
| 44 |
def write(self, vals): |
| 45 |
for floor in self: |
| 46 |
for config in floor.pos_config_ids: |
| 47 |
if config.has_active_session and (vals.get('pos_config_ids') or vals.get('active')): |
| 48 |
raise UserError( |
| 49 |
self.env._( |
| 50 |
"Please close and validate the following open PoS Session before modifying this floor.\n" |
| 51 |
"Open session: %(session_names)s", |
| 52 |
session_names=" ".join(config.mapped("name")), |
| 53 |
) |
| 54 |
) |
| 55 |
|
| 56 |
return super().write(vals) |
| 57 |
|
| 58 |
def rename_floor(self, new_name): |
| 59 |
for floor in self: |
| 60 |
floor.name = new_name |
| 61 |
|
| 62 |
@api.model |
| 63 |
def sync_from_ui(self, name, background_color, config_id): |
| 64 |
floor_fields = { |
| 65 |
"name": name, |
| 66 |
"background_color": background_color, |
| 67 |
} |
| 68 |
pos_floor = self.create(floor_fields) |
| 69 |
pos_floor.pos_config_ids = [Command.link(config_id)] |
| 70 |
return { |
| 71 |
'id': pos_floor.id, |
| 72 |
'name': pos_floor.name, |
| 73 |
'background_color': pos_floor.background_color, |
| 74 |
'table_ids': [], |
| 75 |
'sequence': pos_floor.sequence, |
| 76 |
'tables': [], |
| 77 |
} |
| 78 |
|
| 79 |
def deactivate_floor(self, session_id): |
| 80 |
draft_orders = self.env['pos.order'].search([('session_id', '=', session_id), ('state', '=', 'draft'), ('table_id.floor_id', '=', self.id)]) |
| 81 |
if draft_orders: |
| 82 |
raise UserError(_("You cannot delete a floor when orders are still in draft for this floor.")) |
| 83 |
for table in self.table_ids: |
| 84 |
table.active = False |
| 85 |
self.active = False |
| 86 |
|
| 87 |
return True |
| 88 |
|
| 89 |
|
| 90 |
class RestaurantTable(models.Model): |
| 91 |
_name = 'restaurant.table' |
| 92 |
|
| 93 |
_description = 'Restaurant Table' |
| 94 |
_inherit = ['pos.load.mixin'] |
| 95 |
|
| 96 |
floor_id = fields.Many2one('restaurant.floor', string='Floor', index='btree_not_null') |
| 97 |
table_number = fields.Integer('Table Number', required=True, help='The number of the table as displayed on the floor plan', default=0) |
| 98 |
shape = fields.Selection([('square', 'Square'), ('round', 'Round')], string='Shape', required=True, default='square') |
| 99 |
position_h = fields.Float('Horizontal Position', default=10, |
| 100 |
help="The table's horizontal position from the left side to the table's center, in pixels") |
| 101 |
position_v = fields.Float('Vertical Position', default=10, |
| 102 |
help="The table's vertical position from the top to the table's center, in pixels") |
| 103 |
width = fields.Float('Width', default=50, help="The table's width in pixels") |
| 104 |
height = fields.Float('Height', default=50, help="The table's height in pixels") |
| 105 |
seats = fields.Integer('Seats', default=1, help="The default number of customer served at this table.") |
| 106 |
color = fields.Char('Color', help="The table's color, expressed as a valid 'background' CSS property value") |
| 107 |
parent_id = fields.Many2one('restaurant.table', string='Parent Table', help="The parent table if this table is part of a group of tables") |
| 108 |
active = fields.Boolean('Active', default=True, help='If false, the table is deactivated and will not be available in the point of sale') |
| 109 |
|
| 110 |
@api.depends('table_number', 'floor_id') |
| 111 |
def _compute_display_name(self): |
| 112 |
for table in self: |
| 113 |
table.display_name = f"{table.floor_id.name}, {table.table_number}" |
| 114 |
|
| 115 |
@api.model |
| 116 |
def _load_pos_data_domain(self, data, config): |
| 117 |
return [('active', '=', True), ('floor_id', 'in', config.floor_ids.ids)] |
| 118 |
|
| 119 |
@api.model |
| 120 |
def _load_pos_data_fields(self, config): |
| 121 |
return ['table_number', 'width', 'height', 'position_h', 'position_v', 'parent_id', 'shape', 'floor_id', 'color', 'seats', 'active'] |
| 122 |
|
| 123 |
def are_orders_still_in_draft(self): |
| 124 |
draft_orders_count = self.env['pos.order'].search_count([('table_id', 'in', self.ids), ('state', '=', 'draft')]) |
| 125 |
|
| 126 |
if draft_orders_count > 0: |
| 127 |
raise UserError(_("You cannot delete a table when orders are still in draft for this table.")) |
| 128 |
|
| 129 |
return True |
| 130 |
|
| 131 |
@api.ondelete(at_uninstall=False) |
| 132 |
def _unlink_except_active_pos_session(self): |
| 133 |
confs = self.mapped('floor_id.pos_config_ids').filtered(lambda c: c.module_pos_restaurant) |
| 134 |
opened_session = self.env['pos.session'].search([('config_id', 'in', confs.ids), ('state', '!=', 'closed')]) |
| 135 |
if opened_session: |
| 136 |
error_msg = _("You cannot remove a table that is used in a PoS session, close the session(s) first.") |
| 137 |
if confs: |
| 138 |
raise UserError(error_msg) |