-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathaccount_analytic_line.py
144 lines (124 loc) · 6.11 KB
/
account_analytic_line.py
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
134
135
136
137
138
139
140
141
142
143
144
from odoo import models, api
from odoo.exceptions import ValidationError
class AccountAnalyticLine(models.Model):
_inherit = "account.analytic.line"
@api.model_create_multi
def create(self, vals_list):
# print("* " * 100)
# print(vals_list)
# print("* " * 100)
for vals in vals_list:
entry_date = vals.get("date")
if not entry_date:
raise ValidationError(
"The date field is required to determine the appropriate budget."
)
budget_line = self.env["budget.management.budget.lines"].search(
[
("budget_id.date_start", "<=", entry_date),
("budget_id.date_end", ">=", entry_date),
("budget_id.active", "=", True),
("analytic_account_id", "=", vals.get("account_id")),
],
limit=1,
)
if budget_line:
budget = budget_line.budget_id
analytic_account_lines = self.env["account.analytic.line"].search_read(
[
("account_id", "=", vals.get("account_id")),
("date", ">=", budget.date_start),
("date", "<=", budget.date_end),
("amount", "<", 0),
],
fields=["amount"],
)
# print(list(line.get("amount") for line in analytic_account_lines))
achieved = sum(line.get("amount") for line in analytic_account_lines)
# print(budget.on_over_budget, abs(achieved + vals.get("amount")), budget_line.budget_amount)
if budget.on_over_budget == "restriction":
if abs(achieved + vals.get("amount")) > budget_line.budget_amount:
raise ValidationError(
"You cannot create a budget line because it exceeds the allowed budget!"
)
budget_line.achieved_amount = abs(achieved + vals.get("amount"))
budget_line.count_analytic_lines = len(analytic_account_lines) + 1
return super(AccountAnalyticLine, self).create(vals_list)
def write(self, vals):
if "date" in vals or "amount" in vals or "account_id" in vals:
for record in self:
entry_date = vals.get("date", record.date)
budget_line = self.env["budget.management.budget.lines"].search(
[
("budget_id.date_start", "<=", entry_date),
("budget_id.date_end", ">=", entry_date),
("budget_id.active", "=", True),
(
"analytic_account_id",
"=",
vals.get("account_id", record.account_id.id),
),
],
limit=1,
)
if budget_line:
budget = budget_line.budget_id
analytic_account_lines = self.env[
"account.analytic.line"
].search_read(
[
(
"account_id",
"=",
vals.get("account_id", record.account_id.id),
),
("date", ">=", budget.date_start),
("date", "<=", budget.date_end),
("amount", "<", 0),
("id", "!=", record.id),
],
fields=["amount"],
)
achieved = sum(
line.get("amount") for line in analytic_account_lines
)
new_amount = vals.get("amount", record.amount)
# print(budget.on_over_budget, abs(achieved - record.amount + new_amount), record.amount , new_amount, budget_line.budget_amount)
total_achieved_amount_will_be = abs(achieved + new_amount)
if budget.on_over_budget == "restriction":
if total_achieved_amount_will_be > budget_line.budget_amount:
raise ValidationError(
"You cannot modify the budget line because it exceeds the allowed budget!"
)
budget_line.achieved_amount = total_achieved_amount_will_be
budget_line.count_analytic_lines = len(analytic_account_lines) + 1
return super(AccountAnalyticLine, self).write(vals)
def unlink(self):
for record in self:
entry_date = record.date
budget_line = self.env["budget.management.budget.lines"].search(
[
("budget_id.date_start", "<=", entry_date),
("budget_id.date_end", ">=", entry_date),
("budget_id.active", "=", True),
("analytic_account_id", "=", record.account_id.id),
],
limit=1,
)
if budget_line:
budget = budget_line.budget_id
# Recalculate achieved amount excluding the current record
analytic_account_lines = self.env["account.analytic.line"].search_read(
[
("account_id", "=", record.account_id.id),
("date", ">=", budget.date_start),
("date", "<=", budget.date_end),
("amount", "<", 0),
("id", "!=", record.id), # Exclude the current record
],
fields=["amount"],
)
achieved = sum(line.get("amount") for line in analytic_account_lines)
budget_line.achieved_amount = abs(achieved)
budget_line.count_analytic_lines = len(analytic_account_lines)
return super(AccountAnalyticLine, self).unlink()