[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>
This commit is contained in:
Pierre Masereel
2019-08-19 08:04:31 +00:00
parent 1699be05da
commit a248171d96
6 changed files with 55 additions and 8 deletions
+3 -2
View File
@@ -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',
+3 -2
View File
@@ -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)
+2 -1
View File
@@ -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)
+11 -1
View File
@@ -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()
+16 -1
View File
@@ -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()
+20 -1
View File
@@ -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):