forked from ingadhoc/account-invoicing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccount_invoice_tax.py
More file actions
133 lines (117 loc) · 5.49 KB
/
Copy pathaccount_invoice_tax.py
File metadata and controls
133 lines (117 loc) · 5.49 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import Command, _, api, fields, models
from odoo.exceptions import UserError
class AccountInvoiceTax(models.TransientModel):
_name = "account.invoice.tax"
_description = "Account Invoice Tax"
move_id = fields.Many2one("account.move", required=True)
tax_line_ids = fields.One2many("account.invoice.tax_line", "invoice_tax_id")
@api.model
def default_get(self, fields):
res = super().default_get(fields)
move_ids = (
self.env["account.move"].browse(self.env.context["active_ids"])
if self.env.context.get("active_model") == "account.move"
else self.env["account.move"]
)
res["move_id"] = move_ids[0].id if move_ids else False
if move_ids[0].move_type == "in_invoice":
sign = 1
else: # For refund
sign = -1
lines = []
for line in move_ids[0].line_ids.filtered(lambda x: x.tax_line_id):
lines.append(
Command.create({"tax_id": line.tax_line_id.id, "amount": line.amount_currency * sign, "new_tax": False})
)
res["tax_line_ids"] = lines
return res
def action_update_tax(self):
move = self.move_id
fixed_taxes_bu = {
line.tax_line_id: {
"amount_currency": line.amount_currency,
"debit": line.debit,
"credit": line.credit,
}
for line in self.move_id.line_ids.filtered(
lambda x: x.tax_repartition_line_id.tax_id.amount_type == "fixed"
)
}
active_tax = self.tax_line_ids.mapped("tax_id")
origin_tax = self.move_id.line_ids.filtered(lambda x: x.tax_line_id).mapped("tax_repartition_line_id.tax_id")
to_remove_tax = origin_tax - active_tax
to_add_tax = active_tax - origin_tax
container = {"records": move, "self": move}
# change tax list
with move.with_context(check_move_validity=False)._check_balanced(container):
with move._sync_dynamic_lines(container):
if to_remove_tax:
move.invoice_line_ids.filtered(lambda x: x.display_type == "product").write(
{"tax_ids": [Command.unlink(tax_id.id) for tax_id in to_remove_tax]}
)
if to_add_tax:
move.invoice_line_ids.filtered(lambda x: x.display_type == "product").write(
{"tax_ids": [Command.link(tax_id.id) for tax_id in to_add_tax]}
)
# set amount in the new created tax line. En este momento si queda balanceado y se ajusta la linea AP/AR
container = {"records": move}
if move.move_type == "in_invoice":
sign = 1
else: # For refund
sign = -1
with move._check_balanced(container):
with move._sync_dynamic_lines(container):
# restauramos todos los valores de impuestos fixed que se habrian recomputado
# restaured = []
for tax_line in move.line_ids.filtered(
lambda x: x.tax_repartition_line_id.tax_id in fixed_taxes_bu
and x.tax_repartition_line_id.tax_id.amount_type == "fixed"
):
tax_line.write(fixed_taxes_bu.get(tax_line.tax_line_id))
for tax_line_id in self.tax_line_ids:
# seteamos valor al impuesto segun lo que puso en el wizard
line_with_tax = move.line_ids.filtered(lambda x: x.tax_line_id == tax_line_id.tax_id)
line_with_tax.write({"amount_currency": tax_line_id.amount * sign})
def add_tax_and_new(self):
self.add_tax()
return {
"type": "ir.actions.act_window",
"name": _("Edit tax lines"),
"res_model": self._name,
"target": "new",
"view_mode": "form",
"context": self._context,
}
@api.constrains("tax_line_ids")
@api.onchange("tax_line_ids")
def check_analytic(self):
taxes = self.tax_line_ids.filtered("tax_id.analytic").mapped("tax_id")
if taxes:
raise UserError(
'No puede usar este asistente ya que algún impuesto tiene establecido "Incluir en el costo analítico?".\nImpuestos: %s'
% (", ".join(taxes.mapped(lambda x: "%s (%s)" % (x.name, x.id))))
)
class AccountInvoiceTaxLine(models.TransientModel):
_name = "account.invoice.tax_line"
_description = "Account Invoice Tax line"
_check_company_auto = True
_check_company_domain = models.check_companies_domain_parent_of
invoice_tax_id = fields.Many2one("account.invoice.tax")
tax_id = fields.Many2one(
"account.tax",
required=True,
check_company=True,
domain="[('type_tax_use', '=', 'purchase'), ('id', 'not in', existing_tax_ids)]",
)
company_id = fields.Many2one(related="invoice_tax_id.move_id.company_id")
currency_id = fields.Many2one(related="invoice_tax_id.move_id.currency_id")
existing_tax_ids = fields.Many2many("account.tax", compute="_compute_existing_taxes")
amount = fields.Monetary(
currency_field="currency_id",
)
new_tax = fields.Boolean(default=True)
@api.depends("invoice_tax_id.tax_line_ids.tax_id")
def _compute_existing_taxes(self):
for record in self:
record.existing_tax_ids = record.invoice_tax_id.tax_line_ids.mapped("tax_id")