18
18
19
19
class AccountBudget (models .Model ):
20
20
_name = "account.budget"
21
+ _inherit = ['mail.thread' ]
21
22
_description = "Budget"
22
23
23
24
BUDGET_LEVEL = {
@@ -48,6 +49,7 @@ class AccountBudget(models.Model):
48
49
string = 'Responsible User' ,
49
50
default = lambda self : self .env .user ,
50
51
readonly = True ,
52
+ states = {'draft' : [('readonly' , False )]},
51
53
)
52
54
# validating_user_id = fields.Many2one(
53
55
# 'res.users',
@@ -72,7 +74,8 @@ class AccountBudget(models.Model):
72
74
index = True ,
73
75
required = True ,
74
76
readonly = True ,
75
- # copy=False,
77
+ copy = False ,
78
+ track_visibility = 'onchange' ,
76
79
)
77
80
budget_line_ids = fields .One2many (
78
81
'account.budget.line' ,
@@ -97,12 +100,6 @@ class AccountBudget(models.Model):
97
100
domain = [('budget_method' , '=' , 'expense' )],
98
101
# copy=True,
99
102
)
100
- company_id = fields .Many2one (
101
- 'res.company' ,
102
- string = 'Company' ,
103
- required = True ,
104
- default = lambda self : self .env .user .company_id ,
105
- )
106
103
# version = fields.Float(
107
104
# string='Revision',
108
105
# readonly=True,
@@ -122,15 +119,12 @@ class AccountBudget(models.Model):
122
119
'account.fiscalyear' ,
123
120
string = 'Fiscal Year' ,
124
121
required = True ,
125
- )
126
- currency_id = fields .Many2one (
127
- 'res.currency' ,
128
- string = "Currency" ,
129
- default = lambda self : self .env .user .company_id .currency_id ,
130
122
readonly = True ,
123
+ states = {'draft' : [('readonly' , False )]},
131
124
)
132
125
to_release_amount = fields .Float (
133
126
string = 'Released Amount' ,
127
+ readonly = True ,
134
128
)
135
129
released_amount = fields .Float (
136
130
string = 'Released Amount' ,
@@ -183,6 +177,11 @@ class AccountBudget(models.Model):
183
177
compute = '_compute_past_future_rolling' ,
184
178
help = "Past Actual + Future Plan" ,
185
179
)
180
+ release_diff_rolling = fields .Float (
181
+ string = 'Release - Rolling' ,
182
+ compute = '_compute_release_diff_rolling' ,
183
+ help = "Release amount - rolling amount" ,
184
+ )
186
185
187
186
@api .multi
188
187
def _get_past_consumed_domain (self ):
@@ -217,16 +216,26 @@ def _compute_past_future_rolling(self):
217
216
# Rolling
218
217
budget .rolling = budget .past_consumed + budget .future_plan
219
218
219
+ @api .multi
220
+ @api .depends ()
221
+ def _compute_release_diff_rolling (self ):
222
+ for budget in self :
223
+ amount_diff = budget .released_amount - budget .rolling
224
+ budget .release_diff_rolling = amount_diff
225
+
220
226
@api .multi
221
227
def write (self , vals ):
222
228
res = super (AccountBudget , self ).write (vals )
223
229
for budget in self :
224
230
if budget .budget_level_id .budget_release == 'manual_header' :
225
- if budget .budget_expense_line_ids :
226
- budget .budget_expense_line_ids .write (
227
- {'released_amount' : 0.0 })
228
- budget .budget_expense_line_ids [0 ].write (
229
- {'released_amount' : budget .to_release_amount })
231
+ if not budget .budget_expense_line_ids :
232
+ raise ValidationError (
233
+ _ ('Budget %s has no expense line!\n '
234
+ 'This operation can not proceed.' ) % (budget .name ,))
235
+ budget .budget_expense_line_ids .write (
236
+ {'released_amount' : 0.0 })
237
+ budget .budget_expense_line_ids [0 ].write (
238
+ {'released_amount' : budget .to_release_amount })
230
239
return res
231
240
232
241
@api .multi
@@ -302,11 +311,12 @@ def _validate_plan_vs_release(self):
302
311
if budget .budget_level_id .check_plan_with_released_amount :
303
312
if budget .rolling > budget .released_amount :
304
313
raise ValidationError (
305
- _ ('Rolling plan (%s) will exceed released amount (%s) '
306
- 'on budget control - %s' ) %
307
- (budget .rolling ,
308
- budget .released_amount ,
309
- budget .name_get ()[0 ][1 ]))
314
+ _ ('%s: rolling plan (%s) will exceed '
315
+ 'released amount (%s) after this operation!' ) %
316
+ (budget .name_get ()[0 ][1 ],
317
+ '{:,.2f}' .format (budget .rolling ),
318
+ '{:,.2f}' .format (budget .released_amount ),
319
+ ))
310
320
return True
311
321
312
322
# @api.multi
@@ -417,7 +427,7 @@ def check_budget(self, fiscal_id, budget_type,
417
427
'[%s] the requested budget is %s,\n '
418
428
'but there is no budget control for it.' ) % \
419
429
(fiscal .name , resource .name_get ()[0 ][1 ],
420
- '{0:, }' .format (amount ))
430
+ '{:,.2f }' .format (amount ))
421
431
return res
422
432
else : # Current Budget Status
423
433
res ['budget_status' ].update ({
@@ -437,14 +447,14 @@ def check_budget(self, fiscal_id, budget_type,
437
447
'%s, not enough budget, this transaction '
438
448
'will result in ฿%s over budget!' ) % \
439
449
(fiscal .name , resource .name_get ()[0 ][1 ],
440
- '{0:, }' .format (- monitors [0 ].amount_balance ))
450
+ '{:,.2f }' .format (- monitors [0 ].amount_balance ))
441
451
else :
442
452
res ['message' ] = _ ('%s\n '
443
453
'%s, remaining budget is %s,\n '
444
454
'but the requested budget is %s' ) % \
445
455
(fiscal .name , resource .name_get ()[0 ][1 ],
446
- '{0:, }' .format (monitors [0 ].amount_balance ),
447
- '{0:, }' .format (amount ))
456
+ '{:,.2f }' .format (monitors [0 ].amount_balance ),
457
+ '{:,.2f }' .format (amount ))
448
458
449
459
if not blevel .is_budget_control :
450
460
res ['budget_ok' ] = True # No control, just return information
@@ -587,14 +597,6 @@ class AccountBudgetLine(ActivityCommon, models.Model):
587
597
index = True ,
588
598
required = True ,
589
599
)
590
- company_id = fields .Many2one (
591
- 'res.company' ,
592
- related = 'budget_id.company_id' ,
593
- string = 'Company' ,
594
- type = 'many2one' ,
595
- store = True ,
596
- readonly = True ,
597
- )
598
600
fiscalyear_id = fields .Many2one (
599
601
'account.fiscalyear' ,
600
602
string = 'Fiscal Year' ,
@@ -681,6 +683,11 @@ def write(self, vals):
681
683
if m in vals :
682
684
raise ValidationError (
683
685
_ ('Adjusting past plan amount is not allowed!' ))
686
+ # Post change in tracking fields, m1, ... , m12
687
+ for rec in self :
688
+ message = self ._change_budget_content (rec , vals )
689
+ if message :
690
+ rec .budget_id .message_post (body = message )
684
691
return super (AccountBudgetLine , self ).write (vals )
685
692
686
693
@api .multi
@@ -768,3 +775,27 @@ def release_budget_line(self, release_result):
768
775
amount_to_release = release_result .get (rec .id , 0.0 )
769
776
rec .write ({'released_amount' : amount_to_release })
770
777
return
778
+
779
+ # Messaging
780
+ @api .model
781
+ def _change_budget_content (self , line , vals ):
782
+ _track_fields = ['m1' , 'm2' , 'm3' , 'm4' , 'm5' , 'm6' ,
783
+ 'm7' , 'm8' , 'm9' , 'm10' , 'm11' , 'm12' , ]
784
+ if set (_track_fields ).isdisjoint (vals .keys ()):
785
+ return False
786
+ title = _ ('Budget amount change(s)' )
787
+ message = '<h3>%s</h3><ul>' % title
788
+ # Get the line label
789
+ line_labels = [line .activity_group_id .name ,
790
+ line .activity_id .name ]
791
+ line_labels = filter (lambda a : a is not False , line_labels )
792
+ line_label = '/' .join (line_labels )
793
+ for field in _track_fields :
794
+ if field in vals :
795
+ message += _ (
796
+ '<li><b>%s</b>: %s → %s</li>'
797
+ ) % (line_label ,
798
+ '{:,.2f}' .format (line [field ]),
799
+ '{:,.2f}' .format (vals .get (field )), )
800
+ message += '</ul>'
801
+ return message
0 commit comments