| 1 |
|
| 2 |
|
| 3 |
from odoo import _, api, models |
| 4 |
from odoo.exceptions import UserError |
| 5 |
from odoo.tools import split_every |
| 6 |
|
| 7 |
|
| 8 |
class AccountTax(models.Model): |
| 9 |
_name = 'account.tax' |
| 10 |
_inherit = ['account.tax', 'pos.load.mixin'] |
| 11 |
|
| 12 |
def write(self, vals): |
| 13 |
forbidden_fields = { |
| 14 |
'amount_type', 'amount', 'type_tax_use', 'tax_group_id', 'price_include', |
| 15 |
'include_base_amount', 'is_base_affected', |
| 16 |
} |
| 17 |
if forbidden_fields & set(vals.keys()): |
| 18 |
lines = self.env['pos.order.line'].sudo().search([ |
| 19 |
('order_id.session_id.state', '!=', 'closed') |
| 20 |
]) |
| 21 |
self_ids = set(self.ids) |
| 22 |
for lines_chunk in map(self.env['pos.order.line'].sudo().browse, split_every(100000, lines.ids)): |
| 23 |
if any(tid in self_ids for ts in lines_chunk.read(['tax_ids']) for tid in ts['tax_ids']): |
| 24 |
raise UserError(_( |
| 25 |
'It is forbidden to modify a tax used in a POS order not posted. ' |
| 26 |
'You must close the POS sessions before modifying the tax.' |
| 27 |
)) |
| 28 |
lines_chunk.invalidate_recordset(['tax_ids']) |
| 29 |
return super(AccountTax, self).write(vals) |
| 30 |
|
| 31 |
def _hook_compute_is_used(self, taxes_to_compute): |
| 32 |
|
| 33 |
|
| 34 |
used_taxes = super()._hook_compute_is_used(taxes_to_compute) |
| 35 |
taxes_to_compute -= used_taxes |
| 36 |
|
| 37 |
if taxes_to_compute: |
| 38 |
self.env['pos.order.line'].flush_model(['tax_ids']) |
| 39 |
self.env.cr.execute(""" |
| 40 |
SELECT id |
| 41 |
FROM account_tax |
| 42 |
WHERE EXISTS( |
| 43 |
SELECT 1 |
| 44 |
FROM account_tax_pos_order_line_rel AS pos |
| 45 |
WHERE account_tax_id IN %s |
| 46 |
AND account_tax.id = pos.account_tax_id |
| 47 |
) |
| 48 |
""", [tuple(taxes_to_compute)]) |
| 49 |
|
| 50 |
used_taxes.update([tax[0] for tax in self.env.cr.fetchall()]) |
| 51 |
|
| 52 |
return used_taxes |
| 53 |
|
| 54 |
@api.model |
| 55 |
def _load_pos_data_domain(self, data, config): |
| 56 |
return self.env['account.tax']._check_company_domain(config.company_id.id) |
| 57 |
|
| 58 |
@api.model |
| 59 |
def _load_pos_data_fields(self, config): |
| 60 |
return [ |
| 61 |
'id', 'name', 'price_include', 'include_base_amount', 'is_base_affected', 'has_negative_factor', |
| 62 |
'amount_type', 'children_tax_ids', 'amount', 'company_id', 'id', 'sequence', 'tax_group_id', |
| 63 |
] |