diff options
| author | Pierre Masereel <pim@odoo.com> | 2019-08-19 07:22:23 +0000 |
|---|---|---|
| committer | Pierre Masereel <pim@odoo.com> | 2019-08-19 08:04:31 +0000 |
| commit | a248171d96de3ad6d8e22c855886dfae61e145e2 (patch) | |
| tree | ed416e643bba74244cee0a8df9b32d9cb300973b /addons | |
| parent | 1699be05dab39a8ef4641df113178e688ce13a29 (diff) | |
[IMP] point_of_sale: improve consistencies between models
The deletion of some records can create inconsistencies in the
point_of_sale.
It can create inconsistencies when a session is open, for example if you
delete a partner that can be set on a pos order. You'll also got
problems if you remove an employee and this employee can be used in an
open session.
We also avoid to get point_of_sale config inconsistencies, by deleting
models required by it. If you delete a picking type used in a pos
config or a sequence.
To avoid all this consistencies, we have restricted the deletion of some
records in specific cases.
TASK-ID: 1879971
closes odoo/odoo#35793
Signed-off-by: pimodoo <pimodoo@users.noreply.github.com>
Diffstat (limited to 'addons')
| -rw-r--r-- | addons/account/models/account.py | 5 | ||||
| -rw-r--r-- | addons/point_of_sale/models/pos_config.py | 5 | ||||
| -rw-r--r-- | addons/point_of_sale/models/pos_session.py | 3 | ||||
| -rw-r--r-- | addons/point_of_sale/models/res_partner.py | 12 | ||||
| -rw-r--r-- | addons/pos_hr/models/hr_employee.py | 17 | ||||
| -rw-r--r-- | addons/pos_restaurant/models/pos_restaurant.py | 21 |
6 files changed, 55 insertions, 8 deletions
diff --git a/addons/account/models/account.py b/addons/account/models/account.py index 05b858688b2..69c5d290e71 100644 --- a/addons/account/models/account.py +++ b/addons/account/models/account.py @@ -555,9 +555,10 @@ class AccountJournal(models.Model): account_control_ids = fields.Many2many('account.account', 'account_account_type_rel', 'journal_id', 'account_id', string='Accounts Allowed', domain="[('deprecated', '=', False), ('company_id', '=', company_id)]") default_credit_account_id = fields.Many2one('account.account', string='Default Credit Account', - domain=[('deprecated', '=', False)], help="It acts as a default account for credit amount") + domain=[('deprecated', '=', False)], help="It acts as a default account for credit amount", + ondelete='restrict') default_debit_account_id = fields.Many2one('account.account', string='Default Debit Account', - domain="[('deprecated', '=', False), ('company_id', '=', company_id)]", help="It acts as a default account for debit amount") + domain="[('deprecated', '=', False), ('company_id', '=', company_id)]", help="It acts as a default account for debit amount", ondelete='restrict') update_posted = fields.Boolean(string='Allow Cancelling Entries', help="Check this box if you want to allow the cancellation the entries related to this journal or of the invoice related to this journal") sequence_id = fields.Many2one('ir.sequence', string='Entry Sequence', diff --git a/addons/point_of_sale/models/pos_config.py b/addons/point_of_sale/models/pos_config.py index 247c4e1d008..0ba6baa7d1e 100644 --- a/addons/point_of_sale/models/pos_config.py +++ b/addons/point_of_sale/models/pos_config.py @@ -104,7 +104,8 @@ class PosConfig(models.Model): 'stock.picking.type', string='Operation Type', default=_default_picking_type_id, - domain="[('code', '=', 'outgoing'), ('warehouse_id.company_id', '=', company_id)]") + domain="[('code', '=', 'outgoing'), ('warehouse_id.company_id', '=', company_id)]", + ondelete='restrict') use_existing_lots = fields.Boolean(related='picking_type_id.use_existing_lots', readonly=False) journal_id = fields.Many2one( 'account.journal', string='Sales Journal', @@ -150,7 +151,7 @@ class PosConfig(models.Model): help='A globally unique identifier for this pos configuration, used to prevent conflicts in client-generated data.') sequence_id = fields.Many2one('ir.sequence', string='Order IDs Sequence', readonly=True, help="This sequence is automatically created by Odoo but you can change it " - "to customize the reference numbers of your orders.", copy=False) + "to customize the reference numbers of your orders.", copy=False, ondelete='restrict') sequence_line_id = fields.Many2one('ir.sequence', string='Order Line IDs Sequence', readonly=True, help="This sequence is automatically created by Odoo but you can change it " "to customize the reference numbers of your orders lines.", copy=False) diff --git a/addons/point_of_sale/models/pos_session.py b/addons/point_of_sale/models/pos_session.py index a6885c3205d..0f200cf971d 100644 --- a/addons/point_of_sale/models/pos_session.py +++ b/addons/point_of_sale/models/pos_session.py @@ -37,7 +37,8 @@ class PosSession(models.Model): index=True, readonly=True, states={'opening_control': [('readonly', False)]}, - default=lambda self: self.env.uid) + default=lambda self: self.env.uid, + ondelete='restrict') currency_id = fields.Many2one('res.currency', related='config_id.currency_id', string="Currency", readonly=False) start_at = fields.Datetime(string='Opening Date', readonly=True) stop_at = fields.Datetime(string='Closing Date', readonly=True, copy=False) diff --git a/addons/point_of_sale/models/res_partner.py b/addons/point_of_sale/models/res_partner.py index 94f0eaa1ff5..62ff126c72a 100644 --- a/addons/point_of_sale/models/res_partner.py +++ b/addons/point_of_sale/models/res_partner.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import api, fields, models +from odoo import api, fields, models, _ +from odoo.exceptions import UserError class ResPartner(models.Model): @@ -38,3 +39,12 @@ class ResPartner(models.Model): partner['lang'] = self.env.user.lang partner_id = self.create(partner).id return partner_id + + def unlink(self): + running_sessions = self.env['pos.session'].search([('state', '!=', 'closed')]) + if running_sessions: + raise UserError( + _("You cannot delete contacts while there are active PoS sessions. Close the session(s) %s first.") + % ", ".join(session.name for session in running_sessions) + ) + return super(ResPartner, self).unlink() diff --git a/addons/pos_hr/models/hr_employee.py b/addons/pos_hr/models/hr_employee.py index fc5a3f9b154..5ad415c2f11 100644 --- a/addons/pos_hr/models/hr_employee.py +++ b/addons/pos_hr/models/hr_employee.py @@ -3,7 +3,8 @@ import hashlib -from odoo import api, models +from odoo import api, models, _ +from odoo.exceptions import UserError class HrEmployee(models.Model): @@ -20,3 +21,17 @@ class HrEmployee(models.Model): e['barcode'] = hashlib.sha1(e['barcode'].encode('utf8')).hexdigest() if e['barcode'] else False e['pin'] = hashlib.sha1(e['pin'].encode('utf8')).hexdigest() if e['pin'] else False return employees_data + + def unlink(self): + configs_with_employees = self.env['pos.config'].search([('module_pos_hr', '=', 'True')]).filtered(lambda c: c.current_session_id) + configs_with_all_employees = configs_with_employees.filtered(lambda c: not c.employee_ids) + configs_with_specific_employees = configs_with_employees.filtered(lambda c: c.employee_ids & self) + if configs_with_all_employees or configs_with_specific_employees: + error_msg = _("You cannot delete an employee that may be used in an active PoS session, close the session(s) first: \n") + for employee in self: + config_ids = configs_with_all_employees | configs_with_specific_employees.filtered(lambda c: employee in c.employee_ids) + if config_ids: + error_msg += _("Employee: %s - PoS Config(s): %s \n") % (employee.name, ', '.join(config.name for config in config_ids)) + + raise UserError(error_msg) + return super(HrEmployee, self).unlink() diff --git a/addons/pos_restaurant/models/pos_restaurant.py b/addons/pos_restaurant/models/pos_restaurant.py index ea5c3a04d5b..e89180f9158 100644 --- a/addons/pos_restaurant/models/pos_restaurant.py +++ b/addons/pos_restaurant/models/pos_restaurant.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import api, fields, models +from odoo import api, fields, models, _ +from odoo.exceptions import UserError class RestaurantFloor(models.Model): @@ -16,6 +17,24 @@ class RestaurantFloor(models.Model): table_ids = fields.One2many('restaurant.table', 'floor_id', string='Tables', help='The list of tables in this floor') sequence = fields.Integer('Sequence', help='Used to sort Floors', default=1) + def unlink(self): + confs = self.env['pos.session'].search([ + ('state', '!=', 'closed'), + ('config_id.is_table_management', '=', True) + ]).mapped('config_id') + if confs: + error_msg = _("You cannot remove a floor that is used in a PoS session, close the session(s) first: \n") + for floor in self: + for config in confs: + if floor in config.floor_ids: + error_msg += _("Floor: %s - PoS Config: %s \n") % (floor.name, config.name) + if confs: + raise UserError(error_msg) + return super(RestaurantFloor, self).unlink() + + def _check_pos_sessions(self, error_msg): + import pudb; pudb.set_trace() + class RestaurantTable(models.Model): |
