@@ -10,7 +10,7 @@ class BudgetLine(models.Model):
1010 budget_id = fields .Many2one (
1111 comodel_name = "budget.budget" , string = "Budget" , required = True
1212 )
13- state = fields .Selection (related = "budget_id.state" )
13+ state = fields .Selection (related = "budget_id.state" , readonly = True )
1414 budget_amount = fields .Monetary (
1515 string = "Budget Amount" ,
1616 default = 0.0 ,
@@ -19,30 +19,21 @@ class BudgetLine(models.Model):
1919 )
2020 achieved_amount = fields .Monetary (
2121 string = "Achieved Amount" ,
22- default = 0.0 ,
2322 compute = "_compute_achieved_amount" ,
24- store = True ,
2523 currency_field = "currency_id" ,
2624 )
2725 achieved_percentage = fields .Float (
2826 string = "Achieved (%)" ,
2927 compute = "_compute_achieved_amount" ,
30- store = True ,
3128 readonly = True ,
3229 help = "Percentage of the budget achieved based on analytic lines." ,
3330 )
3431 analytic_account_id = fields .Many2one (
3532 "account.analytic.account" , string = "Analytic Account" , required = True
3633 )
37- analytic_line_ids = fields .One2many (
38- comodel_name = "account.analytic.line" ,
39- inverse_name = "budget_line_id" ,
40- string = "Analytic Lines" ,
41- )
4234 over_budget = fields .Monetary (
4335 string = "Over Budget" ,
4436 compute = "_compute_achieved_amount" ,
45- store = True ,
4637 help = "The amount by which the budget line exceeds its allocated budget." ,
4738 currency_field = "currency_id" ,
4839 )
@@ -53,50 +44,90 @@ class BudgetLine(models.Model):
5344 readonly = True ,
5445 )
5546
56- @api .depends ("analytic_line_ids.amount " )
47+ @api .depends ("budget_amount " )
5748 def _compute_achieved_amount (self ):
5849 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 )
6068 record .achieved_percentage = (
6169 (record .achieved_amount / record .budget_amount ) * 100
6270 if record .budget_amount > 0
6371 else 0.0
6472 )
6573 record .over_budget = max (0.0 , record .achieved_amount - record .budget_amount )
6674
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
7482
7583 @api .constrains ("budget_amount" )
7684 def _check_budget_amount (self ):
7785 for record in self :
7886 if record .budget_amount < 0 :
7987 raise ValidationError ("Budget amount cannot be negative." )
8088
81-
8289 @api .model_create_multi
8390 def create (self , vals_list ):
8491 active_budget = None
8592 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+ )
8996 else :
9097 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" ])
94100 break
95-
96- if not active_budget :
97- raise UserError ("No budget found in context or record." )
98101
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
101115
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