@@ -10,7 +10,7 @@ class BudgetLine(models.Model):
10
10
budget_id = fields .Many2one (
11
11
comodel_name = "budget.budget" , string = "Budget" , required = True
12
12
)
13
- state = fields .Selection (related = "budget_id.state" )
13
+ state = fields .Selection (related = "budget_id.state" , readonly = True )
14
14
budget_amount = fields .Monetary (
15
15
string = "Budget Amount" ,
16
16
default = 0.0 ,
@@ -19,30 +19,21 @@ class BudgetLine(models.Model):
19
19
)
20
20
achieved_amount = fields .Monetary (
21
21
string = "Achieved Amount" ,
22
- default = 0.0 ,
23
22
compute = "_compute_achieved_amount" ,
24
- store = True ,
25
23
currency_field = "currency_id" ,
26
24
)
27
25
achieved_percentage = fields .Float (
28
26
string = "Achieved (%)" ,
29
27
compute = "_compute_achieved_amount" ,
30
- store = True ,
31
28
readonly = True ,
32
29
help = "Percentage of the budget achieved based on analytic lines." ,
33
30
)
34
31
analytic_account_id = fields .Many2one (
35
32
"account.analytic.account" , string = "Analytic Account" , required = True
36
33
)
37
- analytic_line_ids = fields .One2many (
38
- comodel_name = "account.analytic.line" ,
39
- inverse_name = "budget_line_id" ,
40
- string = "Analytic Lines" ,
41
- )
42
34
over_budget = fields .Monetary (
43
35
string = "Over Budget" ,
44
36
compute = "_compute_achieved_amount" ,
45
- store = True ,
46
37
help = "The amount by which the budget line exceeds its allocated budget." ,
47
38
currency_field = "currency_id" ,
48
39
)
@@ -53,50 +44,90 @@ class BudgetLine(models.Model):
53
44
readonly = True ,
54
45
)
55
46
56
- @api .depends ("analytic_line_ids.amount " )
47
+ @api .depends ("budget_amount " )
57
48
def _compute_achieved_amount (self ):
58
49
for record in self :
59
- record .achieved_amount = sum (record .analytic_line_ids .mapped ("amount" ))
50
+ if not record .analytic_account_id :
51
+ record .achieved_amount = 0.0
52
+ record .achieved_percentage = 0.0
53
+ record .over_budget = 0.0
54
+ continue
55
+ analytic_account_lines = self .env ["account.analytic.line" ].search_read (
56
+ [
57
+ ("auto_account_id" , "=" , record .analytic_account_id .id ),
58
+ ("date" , ">=" , record .budget_id .date_start ),
59
+ ("date" , "<=" , record .budget_id .date_end ),
60
+ ("amount" , "<" , 0 ),
61
+ ],
62
+ fields = ["amount" ],
63
+ )
64
+
65
+ achieved = sum (line .get ("amount" ) for line in analytic_account_lines )
66
+
67
+ record .achieved_amount = abs (achieved )
60
68
record .achieved_percentage = (
61
69
(record .achieved_amount / record .budget_amount ) * 100
62
70
if record .budget_amount > 0
63
71
else 0.0
64
72
)
65
73
record .over_budget = max (0.0 , record .achieved_amount - record .budget_amount )
66
74
67
- if (
68
- record .budget_id .on_over_budget == "warning"
69
- and record .achieved_amount > record . budget_amount
70
- ):
71
- record .budget_id .warnings = "Achived amount is more than your budget!"
72
- else :
73
- record .budget_id .warnings = False
75
+ # if (
76
+ # record.budget_id.on_over_budget == "warning"
77
+ # and any( record.over_budget for record in self) > 0
78
+ # ):
79
+ # record.budget_id.warnings = "Achieved amount exceeds the budget!"
80
+ # else:
81
+ # record.budget_id.warnings = False
74
82
75
83
@api .constrains ("budget_amount" )
76
84
def _check_budget_amount (self ):
77
85
for record in self :
78
86
if record .budget_amount < 0 :
79
87
raise ValidationError ("Budget amount cannot be negative." )
80
88
81
-
82
89
@api .model_create_multi
83
90
def create (self , vals_list ):
84
91
active_budget = None
85
92
if self .env .context .get ("active_id" ):
86
- active_budget = self .env ["budget.budget" ].browse (self . env . context . get ( "active_id" ))
87
- if active_budget . state != "draft" :
88
- raise UserError ( "Budget lines can only be created when the state is 'draft'." )
93
+ active_budget = self .env ["budget.budget" ].browse (
94
+ self . env . context [ "active_id" ]
95
+ )
89
96
else :
90
97
for vals in vals_list :
91
- budget_id = vals .get ("budget_id" )
92
- if budget_id :
93
- active_budget = self .env ["budget.budget" ].browse (budget_id )
98
+ if vals .get ("budget_id" ):
99
+ active_budget = self .env ["budget.budget" ].browse (vals ["budget_id" ])
94
100
break
95
-
96
- if not active_budget :
97
- raise UserError ("No budget found in context or record." )
98
101
99
- if active_budget .state != "draft" :
100
- raise UserError ("Budget lines can only be created when the state is 'draft'." )
102
+ if not active_budget or active_budget .state != "draft" :
103
+ raise UserError (
104
+ "Budget lines can only be created when the budget is in 'draft' state."
105
+ )
106
+
107
+ return super (BudgetLine , self ).create (vals_list )
108
+
109
+ def open_analytic_lines_action (self ):
110
+ if not self .budget_id :
111
+ raise UserError ("No budget linked to this budget line." )
112
+
113
+ budget_start_date = self .budget_id .date_start
114
+ budget_end_date = self .budget_id .date_end
101
115
102
- return super (BudgetLine , self ).create (vals_list )
116
+ return {
117
+ "type" : "ir.actions.act_window" ,
118
+ "name" : "Analytic Lines" ,
119
+ "res_model" : "account.analytic.line" ,
120
+ "view_mode" : "list" ,
121
+ "target" : "current" ,
122
+ "context" : {
123
+ "default_account_id" : self .analytic_account_id .id ,
124
+ "budget_start_date" : budget_start_date ,
125
+ "budget_end_date" : budget_end_date ,
126
+ },
127
+ "domain" : [
128
+ ("account_id" , "=" , self .analytic_account_id .id ),
129
+ ("date" , ">=" , budget_start_date ),
130
+ ("date" , "<=" , budget_end_date ),
131
+ ("amount" , "<" , 0 ),
132
+ ],
133
+ }
0 commit comments