summaryrefslogtreecommitdiff
path: root/addons/point_of_sale/models/account_tax.py
blob: a8ad419a0e54b0ee8c11b610b8af6d9d10a10506 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- coding: utf-8 -*-

from odoo import _, api, models
from odoo.exceptions import UserError


class AccountTax(models.Model):
    _inherit = 'account.tax'

    def write(self, vals):
        forbidden_fields = set([
            'amount_type', 'amount', 'type_tax_use', 'tax_group_id', 'price_include',
            'include_base_amount'
        ])
        if forbidden_fields & set(vals.keys()):
            tax_ids = self.env['pos.order.line'].sudo().search([
                ('order_id.session_id.state', '!=', 'closed')
            ]).read(['tax_ids'])
            # Flatten the list of taxes, see https://stackoverflow.com/questions/952914
            tax_ids = set([i for sl in [t['tax_ids'] for t in tax_ids] for i in sl])
            if tax_ids & set(self.ids):
                raise UserError(_(
                    'It is forbidden to modify a tax used in a POS order not posted. '
                    'You must close the POS sessions before modifying the tax.'
                ))
        return super(AccountTax, self).write(vals)

    def get_real_tax_amount(self):
        tax_list = []
        for tax in self:
            tax_repartition_lines = tax.invoice_repartition_line_ids.filtered(lambda x: x.repartition_type == 'tax')
            total_factor = sum(tax_repartition_lines.mapped('factor'))
            real_amount = tax.amount * total_factor
            tax_list.append({'id': tax.id, 'amount': real_amount})
        return tax_list