|
8 | 8 |
|
9 | 9 | from odoo import api, fields, models
|
10 | 10 | from odoo.exceptions import UserError
|
11 |
| -from odoo.fields import first |
| 11 | +from odoo.fields import Command, first |
12 | 12 | from odoo.osv import expression
|
13 | 13 | from odoo.tools import float_is_zero, frozendict
|
14 | 14 | from odoo.tools.translate import _
|
@@ -614,39 +614,51 @@ def get_line_product(self, line, partner):
|
614 | 614 | return product
|
615 | 615 |
|
616 | 616 | def adjust_accounting_data(self, product, line_vals):
|
617 |
| - account = self.get_credit_account(product) |
618 |
| - line_vals["account_id"] = account.id |
619 |
| - |
620 |
| - new_tax = None |
621 |
| - if len(product.product_tmpl_id.supplier_taxes_id) == 1: |
622 |
| - new_tax = product.product_tmpl_id.supplier_taxes_id[0] |
623 |
| - elif len(account.tax_ids) == 1: |
624 |
| - new_tax = account.tax_ids[0] |
625 |
| - line_tax = self.env["account.tax"] |
626 |
| - if ( |
627 |
| - line_vals.get("tax_ids") |
628 |
| - and line_vals["tax_ids"][0][0] == fields.Command.SET |
629 |
| - ): |
630 |
| - line_tax_id = line_vals["tax_ids"][0][2][0] |
631 |
| - line_tax = self.env["account.tax"].browse(line_tax_id) |
632 |
| - if new_tax and line_tax and new_tax != line_tax: |
633 |
| - if new_tax._get_tax_amount() != line_tax._get_tax_amount(): |
634 |
| - self.log_inconsistency( |
635 |
| - _( |
636 |
| - "XML contains tax %(line_tax)s. " |
637 |
| - "Product %(product)s has tax %(new_tax)s. Using " |
638 |
| - "the XML one" |
| 617 | + line_tax_commands = line_vals.get("tax_ids") |
| 618 | + if line_tax_commands and line_tax_commands[0][0] == fields.Command.SET: |
| 619 | + line_tax_id = line_tax_commands[0][2][0] |
| 620 | + else: |
| 621 | + line_tax_id = False |
| 622 | + line_tax = self.env["account.tax"].browse(line_tax_id) |
| 623 | + |
| 624 | + if product: |
| 625 | + account = self.get_credit_account(product) |
| 626 | + line_vals["account_id"] = account.id |
| 627 | + |
| 628 | + new_tax = None |
| 629 | + if len(product.product_tmpl_id.supplier_taxes_id) == 1: |
| 630 | + new_tax = product.product_tmpl_id.supplier_taxes_id[0] |
| 631 | + elif len(account.tax_ids) == 1: |
| 632 | + new_tax = account.tax_ids[0] |
| 633 | + |
| 634 | + if new_tax and line_tax and new_tax != line_tax: |
| 635 | + if new_tax._get_tax_amount() != line_tax._get_tax_amount(): |
| 636 | + self.log_inconsistency( |
| 637 | + _( |
| 638 | + "XML contains tax %(line_tax)s. " |
| 639 | + "Product %(product)s has tax %(new_tax)s. Using " |
| 640 | + "the XML one" |
| 641 | + ) |
| 642 | + % { |
| 643 | + "line_tax": line_tax.name, |
| 644 | + "product": product.name, |
| 645 | + "new_tax": new_tax.name, |
| 646 | + } |
639 | 647 | )
|
640 |
| - % { |
641 |
| - "line_tax": line_tax.name, |
642 |
| - "product": product.name, |
643 |
| - "new_tax": new_tax.name, |
644 |
| - } |
645 |
| - ) |
646 |
| - else: |
647 |
| - # If product has the same amount of the one in XML, |
648 |
| - # I use it. Typical case: 22% det 50% |
649 |
| - line_vals["tax_ids"] = [(6, 0, [new_tax.id])] |
| 648 | + else: |
| 649 | + # If product has the same amount of the one in XML, |
| 650 | + # I use it. Typical case: 22% det 50% |
| 651 | + line_vals["tax_ids"] = [(6, 0, [new_tax.id])] |
| 652 | + |
| 653 | + # Apply fiscal position |
| 654 | + if line_tax: |
| 655 | + bill = self.env["account.move"].browse(line_vals["move_id"]) |
| 656 | + fiscal_position = bill.fiscal_position_id |
| 657 | + if fiscal_position: |
| 658 | + line_tax = fiscal_position.map_tax(line_tax) |
| 659 | + line_vals["tax_ids"] = [ |
| 660 | + Command.set(line_tax.ids), |
| 661 | + ] + line_tax_commands[1:] |
650 | 662 |
|
651 | 663 | # move_line.tax_ids
|
652 | 664 | # move_line.name
|
@@ -1725,7 +1737,7 @@ def _set_invoice_lines(
|
1725 | 1737 | ):
|
1726 | 1738 | if product:
|
1727 | 1739 | invoice_line_data["product_id"] = product.id
|
1728 |
| - self.adjust_accounting_data(product, invoice_line_data) |
| 1740 | + self.adjust_accounting_data(product, invoice_line_data) |
1729 | 1741 | self.create_and_get_line_id(
|
1730 | 1742 | invoice_lines, invoice_line_model, invoice_line_data
|
1731 | 1743 | )
|
|
0 commit comments