Many2Many values are not always unique
Scenario:
- Create taxes: Tax1 and Tax2
- Create a product: Product1 Add Tax1 and Tax2 in account category
- Create a account tax rule: "Rule1". Add a new line: 3.1. origin_tax: None 3.2. tax: Tax1
- Create a party: Party1. Add customer_tax_rule related to "Rule1"
- Create an invoice: party is Party1 and create a new product: product1
When do on_change_product, len(taxes) == 3 but client only show two taxes: tax1 and tax2
Now, open a trytond-console, and do same operation.
>>> transaction.set_user(1)
>>> User = pool.get('res.user')
>>> context = User.get_preferences(context_only=True)
>>> transaction.set_context(context)
>>> Invoice = pool.get('account.invoice')
>>> invoice = Invoice(1)
>>> InvoiceLine = pool.get('account.invoice.line')
>>> Product = pool.get('product.product')
>>> product = Product(1)
>>> invoice_line = InvoiceLine()
>>> invoice_line.invoice = invoice
>>> invoice_line.product = product
>>> invoice_line.on_change_product()
> /home/raimon/projectes/nandev/049296/trytond/trytond/modules/account_invoice/invoice.py(2197)on_change_product()
-> self.taxes = taxes
(Pdb) taxes
[1, 30, 30]
(Pdb) c
Pool().get('account.invoice.line')(**{'taxes': [1, 30, 30], 'unit': 1, 'party': None, 'unit_digits': 0, 'company': 1, 'product': 1, 'invoice_type': None, 'account': 1588, 'invoice': 1})
>>> from decimal import Decimal
>>> invoice_line.quantity = Decimal(1)
>>> invoice_line.unit_price = Decimal(1)
>>> invoice_line.save()
>>> invoice_line.taxes
(Pool().get('account.tax')(1), Pool().get('account.tax')(30), Pool().get('account.tax')(30))
>>> Invoice.update_taxes([invoice])
>>> invoice.taxes
(Pool().get('account.invoice.tax')(1), Pool().get('account.invoice.tax')(2))
>>> invoice.untaxed_amount
Decimal('1.00')
>>> [t.base for t in invoice.taxes]
[Decimal('1.00'), Decimal('2.00')]
>>> [(t.tax, t.base) for t in invoice.taxes]
[(Pool().get('account.tax')(1), Decimal('1.00')), (Pool().get('account.tax')(30), Decimal('2.00'))]
SQL:
# select id, line, tax from account_invoice_line_account_tax;
id | line | tax
----+------+-----
7 | 3 | 1
8 | 3 | 30
9 | 3 | 30
Bug:
- Invoice line has three taxes: tax1, tax2 and tax2
- update_taxes(), the base from tax1 is 2.00 and not 1.00 (from total taxes in invoice line)
Reason: New tax_ids are added in current list (extend). We need to set() unique taxes.