forked from OCA/account-financial-tools
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaccount_asset.py
1311 lines (1210 loc) · 48.1 KB
/
account_asset.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2009-2018 Noviat
# Copyright 2019 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import calendar
import logging
from datetime import date
from functools import reduce
from sys import exc_info
from traceback import format_exception
from dateutil.relativedelta import relativedelta
from odoo import _, api, fields, models
from odoo.exceptions import UserError
_logger = logging.getLogger(__name__)
class DummyFy:
def __init__(self, *args, **argv):
for key, arg in argv.items():
setattr(self, key, arg)
class AccountAsset(models.Model):
_name = "account.asset"
_inherit = ["mail.thread", "mail.activity.mixin", "analytic.mixin"]
_description = "Asset"
_order = "date_start desc, code, name"
_check_company_auto = True
_rec_names_search = ["code", "name"]
account_move_line_ids = fields.One2many(
comodel_name="account.move.line",
inverse_name="asset_id",
string="Entries",
readonly=True,
copy=False,
check_company=True,
)
move_line_check = fields.Boolean(
compute="_compute_move_line_check", string="Has accounting entries"
)
name = fields.Char(
string="Asset Name",
required=True,
)
code = fields.Char(
string="Reference",
size=32,
)
purchase_value = fields.Monetary(
required=True,
help="This amount represent the initial value of the asset."
"\nThe Depreciation Base is calculated as follows:"
"\nPurchase Value - Salvage Value.",
)
salvage_value = fields.Monetary(
compute="_compute_salvage_value",
store=True,
readonly=False,
help="The estimated value that an asset will realize upon "
"its sale at the end of its useful life.\n"
"This value is used to determine the depreciation amounts.",
)
depreciation_base = fields.Monetary(
compute="_compute_depreciation_base",
store=True,
help="This amount represent the depreciation base "
"of the asset (Purchase Value - Salvage Value).",
)
value_residual = fields.Monetary(
compute="_compute_depreciation",
string="Residual Value",
store=True,
)
value_depreciated = fields.Monetary(
compute="_compute_depreciation",
string="Depreciated Value",
store=True,
)
note = fields.Text()
profile_id = fields.Many2one(
comodel_name="account.asset.profile",
string="Asset Profile",
change_default=True,
required=True,
check_company=True,
)
group_ids = fields.Many2many(
comodel_name="account.asset.group",
compute="_compute_group_ids",
readonly=False,
store=True,
relation="account_asset_group_rel",
column1="asset_id",
column2="group_id",
string="Asset Groups",
)
date_start = fields.Date(
string="Asset Start Date",
required=True,
help="You should manually add depreciation lines "
"with the depreciations of previous fiscal years "
"if the Depreciation Start Date is different from the date "
"for which accounting entries need to be generated.",
)
date_remove = fields.Date(string="Asset Removal Date", readonly=True)
state = fields.Selection(
selection=[
("draft", "Draft"),
("open", "Running"),
("close", "Close"),
("removed", "Removed"),
],
string="Status",
required=True,
default="draft",
copy=False,
help="When an asset is created, the status is 'Draft'.\n"
"If the asset is confirmed, the status goes in 'Running' "
"and the depreciation lines can be posted "
"to the accounting.\n"
"If the last depreciation line is posted, "
"the asset goes into the 'Close' status.\n"
"When the removal entries are generated, "
"the asset goes into the 'Removed' status.",
)
active = fields.Boolean(default=True)
partner_id = fields.Many2one(
comodel_name="res.partner",
string="Partner",
)
method = fields.Selection(
selection=lambda self: self.env["account.asset.profile"]._selection_method(),
string="Computation Method",
compute="_compute_method",
readonly=False,
store=True,
help="Choose the method to use to compute the depreciation lines.\n"
" * Linear: Calculated on basis of: "
"Depreciation Base / Number of Depreciations. "
"Depreciation Base = Purchase Value - Salvage Value.\n"
" * Linear-Limit: Linear up to Salvage Value. "
"Depreciation Base = Purchase Value.\n"
" * Degressive: Calculated on basis of: "
"Residual Value * Degressive Factor.\n"
" * Degressive-Linear (only for Time Method = Year): "
"Degressive becomes linear when the annual linear "
"depreciation exceeds the annual degressive depreciation.\n"
" * Degressive-Limit: Degressive up to Salvage Value. "
"The Depreciation Base is equal to the asset value.",
)
method_number = fields.Integer(
string="Number of Years",
compute="_compute_method_number",
readonly=False,
store=True,
help="The number of years needed to depreciate your asset",
)
method_period = fields.Selection(
selection=lambda self: self.env[
"account.asset.profile"
]._selection_method_period(),
string="Period Length",
compute="_compute_method_period",
readonly=False,
store=True,
help="Period length for the depreciation accounting entries",
)
method_end = fields.Date(
string="Ending Date",
compute="_compute_method_end",
readonly=False,
store=True,
)
method_progress_factor = fields.Float(
string="Degressive Factor",
compute="_compute_method_progress_factor",
readonly=False,
store=True,
)
method_time = fields.Selection(
selection=lambda self: self.env[
"account.asset.profile"
]._selection_method_time(),
string="Time Method",
compute="_compute_method_time",
readonly=False,
store=True,
help="Choose the method to use to compute the dates and "
"number of depreciation lines.\n"
" * Number of Years: Specify the number of years "
"for the depreciation.\n"
" * Number of Depreciations: Fix the number of "
"depreciation lines and the time between 2 depreciations.\n",
)
days_calc = fields.Boolean(
string="Calculate by days",
compute="_compute_days_calc",
readonly=False,
store=True,
help="Use number of days to calculate depreciation amount",
)
use_leap_years = fields.Boolean(
compute="_compute_use_leap_years",
readonly=False,
store=True,
help="If not set, the system will distribute evenly the amount to "
"amortize across the years, based on the number of years. "
"So the amount per year will be the "
"depreciation base / number of years.\n "
"If set, the system will consider if the current year "
"is a leap year. The amount to depreciate per year will be "
"calculated as depreciation base / (depreciation end date - "
"start date + 1) * days in the current year.",
)
prorata = fields.Boolean(
string="Prorata Temporis",
compute="_compute_prorrata",
readonly=False,
store=True,
help="Indicates that the first depreciation entry for this asset "
"has to be done from the depreciation start date instead of "
"the first day of the fiscal year.",
)
depreciation_line_ids = fields.One2many(
comodel_name="account.asset.line",
inverse_name="asset_id",
string="Depreciation Lines",
copy=False,
check_company=True,
)
company_id = fields.Many2one(
comodel_name="res.company",
string="Company",
required=True,
readonly=True,
default=lambda self: self._default_company_id(),
)
currency_id = fields.Many2one(
comodel_name="res.currency",
related="company_id.currency_id",
string="Company Currency",
store=True,
)
carry_forward_missed_depreciations = fields.Boolean(
string="Accumulate missed depreciations",
help="""If create an asset in a fiscal period that is now closed
the accumulated amount of depreciations that cannot be posted will be
carried forward to the first depreciation line of the current open
period.""",
)
@api.model
def _default_company_id(self):
return self.env.company
@api.depends("depreciation_line_ids.move_id")
def _compute_move_line_check(self):
for asset in self:
asset.move_line_check = bool(
asset.depreciation_line_ids.filtered("move_id")
)
def _get_salvage_value_profile(self):
self.ensure_one()
salvage_value = self.profile_id.salvage_value
if self.profile_id.salvage_type == "percent":
salvage_value = (salvage_value / 100) * self.purchase_value
return salvage_value
@api.depends("profile_id")
def _compute_salvage_value(self):
for asset in self:
asset.salvage_value = asset._get_salvage_value_profile()
@api.depends("purchase_value", "salvage_value", "method")
def _compute_depreciation_base(self):
for asset in self:
if asset.method in ["linear-limit", "degr-limit"]:
asset.depreciation_base = asset.purchase_value
else:
asset.depreciation_base = asset.purchase_value - asset.salvage_value
@api.depends(
"depreciation_base",
"depreciation_line_ids.type",
"depreciation_line_ids.amount",
"depreciation_line_ids.previous_id",
"depreciation_line_ids.init_entry",
"depreciation_line_ids.move_check",
)
def _compute_depreciation(self):
for asset in self:
lines = asset.depreciation_line_ids.filtered(
lambda line: line.type in ("depreciate", "remove")
and (line.init_entry or line.move_check)
)
value_depreciated = sum(line.amount for line in lines)
residual = asset.depreciation_base - value_depreciated
depreciated = value_depreciated
asset.update({"value_residual": residual, "value_depreciated": depreciated})
@api.depends("profile_id")
def _compute_group_ids(self):
for asset in self:
if asset.profile_id:
asset.group_ids = asset.profile_id.group_ids
@api.depends("profile_id")
def _compute_method(self):
for asset in self:
asset.method = asset.profile_id.method
@api.depends("profile_id", "method_end")
def _compute_method_number(self):
for asset in self:
if asset.method_end:
asset.method_number = 0
else:
asset.method_number = asset.profile_id.method_number
@api.depends("profile_id")
def _compute_method_period(self):
for asset in self:
asset.method_period = asset.profile_id.method_period
@api.depends("method_number")
def _compute_method_end(self):
for asset in self:
if asset.method_number:
asset.method_end = False
@api.depends("profile_id")
def _compute_method_progress_factor(self):
for asset in self:
asset.method_progress_factor = asset.profile_id.method_progress_factor
@api.depends("profile_id")
def _compute_method_time(self):
for asset in self:
asset.method_time = asset.profile_id.method_time
@api.depends("profile_id")
def _compute_days_calc(self):
for asset in self:
asset.days_calc = asset.profile_id.days_calc
@api.depends("profile_id")
def _compute_use_leap_years(self):
for asset in self:
asset.use_leap_years = asset.profile_id.use_leap_years
@api.depends("profile_id", "method_time")
def _compute_prorrata(self):
for asset in self:
if asset.method_time != "year":
asset.prorata = True
else:
asset.prorata = asset.profile_id.prorata
@api.depends("profile_id")
def _compute_account_analytic_id(self):
for asset in self:
asset.account_analytic_id = asset.profile_id.account_analytic_id
@api.depends("profile_id")
def _compute_analytic_distribution(self):
for asset in self:
asset.analytic_distribution = asset.profile_id.analytic_distribution
@api.constrains("method", "method_time")
def _check_method(self):
if self.filtered(
lambda a: a.method == "degr-linear" and a.method_time != "year"
):
raise UserError(
_("Degressive-Linear is only supported for Time Method = Year.")
)
@api.constrains("date_start", "method_end", "method_number", "method_time")
def _check_dates(self):
if self.filtered(
lambda a: a.method_time == "year"
and not a.method_number
and a.method_end
and a.method_end <= a.date_start
):
raise UserError(_("The Start Date must precede the Ending Date."))
@api.constrains("profile_id")
def _check_profile_change(self):
if self.depreciation_line_ids.filtered("move_id"):
raise UserError(
_(
"You cannot change the profile of an asset "
"with accounting entries."
)
)
@api.onchange("purchase_value", "salvage_value", "date_start", "method")
def _onchange_purchase_salvage_value(self):
if self.method in ["linear-limit", "degr-limit"]:
self.depreciation_base = self.purchase_value or 0.0
else:
purchase_value = self.purchase_value or 0.0
salvage_value = self.salvage_value or 0.0
self.depreciation_base = purchase_value - salvage_value
dl_create_line = self.depreciation_line_ids.filtered(
lambda r: r.type == "create"
)
if dl_create_line:
dl_create_line.update(
{"amount": self.depreciation_base, "line_date": self.date_start}
)
@api.model_create_multi
def create(self, vals_list):
asset_ids = super().create(vals_list)
for asset_id in asset_ids:
asset_id._create_first_asset_line()
return asset_ids
def write(self, vals):
res = super().write(vals)
for asset in self:
if self.env.context.get("asset_validate_from_write"):
continue
asset._create_first_asset_line()
if asset.profile_id.open_asset and self.env.context.get(
"create_asset_from_move_line"
):
asset.compute_depreciation_board()
# extra context to avoid recursion
asset.with_context(asset_validate_from_write=True).validate()
return res
def _create_first_asset_line(self):
self.ensure_one()
if self.depreciation_base and not self.depreciation_line_ids:
asset_line_obj = self.env["account.asset.line"]
line_name = self._get_depreciation_entry_name(0)
asset_line_vals = {
"amount": self.depreciation_base,
"asset_id": self.id,
"name": line_name,
"line_date": self.date_start,
"init_entry": True,
"type": "create",
}
asset_line = asset_line_obj.create(asset_line_vals)
if self.env.context.get("create_asset_from_move_line"):
asset_line.move_id = self.env.context["move_id"]
def unlink(self):
for asset in self:
if asset.state != "draft":
raise UserError(_("You can only delete assets in draft state."))
if asset.depreciation_line_ids.filtered(
lambda r: r.type == "depreciate" and r.move_check
):
raise UserError(
_(
"You cannot delete an asset that contains "
"posted depreciation lines."
)
)
# update accounting entries linked to lines of type 'create'
amls = self.with_context(allow_asset_removal=True).mapped(
"account_move_line_ids"
)
amls.write({"asset_id": False})
return super().unlink()
@api.depends("name", "code")
def _compute_display_name(self):
for asset in self:
name = asset.name
if asset.code:
name = " - ".join([asset.code, name])
asset.display_name = name
def validate(self):
for asset in self:
if asset.currency_id.is_zero(asset.value_residual):
asset.state = "close"
else:
asset.state = "open"
if not asset.depreciation_line_ids.filtered(
lambda line: line.type != "create"
):
asset.compute_depreciation_board()
return True
def remove(self):
self.ensure_one()
ctx = dict(self.env.context, active_ids=self.ids, active_id=self.id)
early_removal = False
if self.method in ["linear-limit", "degr-limit"]:
if self.value_residual != self.salvage_value:
early_removal = True
elif self.value_residual:
early_removal = True
if early_removal:
ctx.update({"early_removal": True})
return {
"name": _("Generate Asset Removal entries"),
"view_mode": "form",
"res_model": "account.asset.remove",
"target": "new",
"type": "ir.actions.act_window",
"context": ctx,
}
def set_to_draft(self):
return self.write({"state": "draft"})
def open_entries(self):
self.ensure_one()
# needed for avoiding errors after grouping in assets
context = dict(self.env.context)
context.pop("group_by", None)
return {
"name": _("Journal Entries"),
"view_mode": "tree,form",
"res_model": "account.move",
"view_id": False,
"type": "ir.actions.act_window",
"context": context,
"domain": [("id", "in", self.account_move_line_ids.mapped("move_id").ids)],
}
def _group_lines(self, table):
"""group lines prior to depreciation start period."""
def group_lines(x, y):
y.update({"amount": x["amount"] + y["amount"]})
return y
depreciation_start_date = self.date_start
lines = table[0]["lines"]
lines1 = []
lines2 = []
flag = lines[0]["date"] < depreciation_start_date
for line in lines:
if flag:
lines1.append(line)
if line["date"] >= depreciation_start_date:
flag = False
else:
lines2.append(line)
if lines1:
lines1 = [reduce(group_lines, lines1)]
lines1[0]["depreciated_value"] = 0.0
table[0]["lines"] = lines1 + lines2
def _compute_depreciation_line(
self,
depreciated_value_posted,
table_i_start,
line_i_start,
table,
last_line,
posted_lines,
):
company = self.company_id
currency = company.currency_id
fiscalyear_lock_date = company.fiscalyear_lock_date or fields.Date.to_date(
"1901-01-01"
)
seq = len(posted_lines)
depr_line = last_line
last_date = table[-1]["lines"][-1]["date"]
depreciated_value = depreciated_value_posted
amount_to_allocate = 0.0
for entry in table[table_i_start:]:
for line in entry["lines"][line_i_start:]:
seq += 1
name = self._get_depreciation_entry_name(seq)
amount = line["amount"]
if self.carry_forward_missed_depreciations:
if line["init"]:
amount_to_allocate += amount
amount = 0
else:
amount += amount_to_allocate
amount_to_allocate = 0.0
if line["date"] == last_date:
# ensure that the last entry of the table always
# depreciates the remaining value
amount = self.depreciation_base - depreciated_value
if self.method in ["linear-limit", "degr-limit"]:
amount -= self.salvage_value
if amount or self.carry_forward_missed_depreciations:
vals = {
"previous_id": depr_line.id,
"amount": currency.round(amount),
"asset_id": self.id,
"name": name,
"line_date": line["date"],
"line_days": line["days"],
"init_entry": fiscalyear_lock_date >= line["date"],
}
depreciated_value += currency.round(amount)
depr_line = self.env["account.asset.line"].create(vals)
else:
seq -= 1
line_i_start = 0
def compute_depreciation_board(self):
line_obj = self.env["account.asset.line"]
for asset in self:
currency = asset.company_id.currency_id
if currency.is_zero(asset.value_residual):
continue
domain = [
("asset_id", "=", asset.id),
("type", "=", "depreciate"),
"|",
("move_check", "=", True),
("init_entry", "=", True),
]
posted_lines = line_obj.search(domain, order="line_date desc")
if posted_lines:
last_line = posted_lines[0]
else:
last_line = line_obj
domain = [
("asset_id", "=", asset.id),
("type", "=", "depreciate"),
("move_id", "=", False),
("init_entry", "=", False),
]
old_lines = line_obj.search(domain)
if old_lines:
old_lines.unlink()
table = asset._compute_depreciation_table()
if not table:
continue
asset._group_lines(table)
# check table with posted entries and
# recompute in case of deviation
depreciated_value_posted = depreciated_value = 0.0
if posted_lines:
total_table_lines = sum(len(entry["lines"]) for entry in table)
move_check_lines = asset.depreciation_line_ids.filtered("move_check")
last_depreciation_date = last_line.line_date
last_date_in_table = table[-1]["lines"][-1]["date"]
# If the number of lines in the table is the same as the depreciation
# lines, we will not show an error even if the dates are the same.
if (last_date_in_table < last_depreciation_date) or (
last_date_in_table == last_depreciation_date
and total_table_lines != len(move_check_lines)
):
raise UserError(
_(
"The duration of the asset conflicts with the "
"posted depreciation table entry dates."
)
)
for _table_i, entry in enumerate(table):
residual_amount_table = entry["lines"][-1]["remaining_value"]
if (
entry["date_start"]
<= last_depreciation_date
<= entry["date_stop"]
):
break
if entry["date_stop"] == last_depreciation_date:
_table_i += 1
_line_i = 0
else:
entry = table[_table_i]
date_min = entry["date_start"]
for _line_i, line in enumerate(entry["lines"]):
residual_amount_table = line["remaining_value"]
if date_min <= last_depreciation_date <= line["date"]:
break
date_min = line["date"]
if line["date"] == last_depreciation_date:
_line_i += 1
table_i_start = _table_i
line_i_start = _line_i
# check if residual value corresponds with table
# and adjust table when needed
depreciated_value_posted = depreciated_value = sum(
posted_line.amount for posted_line in posted_lines
)
residual_amount = asset.depreciation_base - depreciated_value
amount_diff = currency.round(residual_amount_table - residual_amount)
if amount_diff:
# We will auto-create a new line because the number of lines in
# the tables are the same as the posted depreciations and there
# is still a residual value. Only in this case we will need to
# add a new line to the table with the amount of the difference.
if len(move_check_lines) == total_table_lines:
table[table_i_start]["lines"].append(
table[table_i_start]["lines"][line_i_start - 1]
)
line = table[table_i_start]["lines"][line_i_start]
line["days"] = 0
line["amount"] = amount_diff
# compensate in first depreciation entry
# after last posting
line = table[table_i_start]["lines"][line_i_start]
line["amount"] -= amount_diff
else: # no posted lines
table_i_start = 0
line_i_start = 0
asset._compute_depreciation_line(
depreciated_value_posted,
table_i_start,
line_i_start,
table,
last_line,
posted_lines,
)
return True
def _get_fy_duration(self, fy, option="days"):
"""Returns fiscal year duration.
@param option:
- days: duration in days
- months: duration in months,
a started month is counted as a full month
- years: duration in calendar years, considering also leap years
"""
fy_date_start = fy.date_from
fy_date_stop = fy.date_to
days = (fy_date_stop - fy_date_start).days + 1
months = (
(fy_date_stop.year - fy_date_start.year) * 12
+ (fy_date_stop.month - fy_date_start.month)
+ 1
)
if option == "days":
return days
elif option == "months":
return months
elif option == "years":
year = fy_date_start.year
cnt = fy_date_stop.year - fy_date_start.year + 1
for i in range(cnt):
cy_days = calendar.isleap(year) and 366 or 365
if i == 0: # first year
if fy_date_stop.year == year:
duration = (fy_date_stop - fy_date_start).days + 1
else:
duration = (date(year, 12, 31) - fy_date_start).days + 1
factor = float(duration) / cy_days
elif i == cnt - 1: # last year
duration = (fy_date_stop - date(year, 1, 1)).days + 1
factor += float(duration) / cy_days
else:
factor += 1.0
year += 1
return factor
def _get_fy_duration_factor(self, entry, firstyear):
"""
localization: override this method to change the logic used to
calculate the impact of extended/shortened fiscal years
"""
duration_factor = 1.0
fy = entry["fy"]
if self.prorata:
if firstyear:
depreciation_date_start = self.date_start
fy_date_stop = entry["date_stop"]
first_fy_asset_days = (fy_date_stop - depreciation_date_start).days + 1
first_fy_duration = self._get_fy_duration(fy, option="days")
first_fy_year_factor = self._get_fy_duration(fy, option="years")
duration_factor = (
float(first_fy_asset_days)
/ first_fy_duration
* first_fy_year_factor
)
else:
duration_factor = self._get_fy_duration(fy, option="years")
else:
fy_months = self._get_fy_duration(fy, option="months")
duration_factor = float(fy_months) / 12
return duration_factor
def _get_depreciation_start_date(self, fy):
"""
In case of 'Linear': the first month is counted as a full month
if the fiscal year starts in the middle of a month.
"""
if self.prorata:
depreciation_start_date = self.date_start
else:
depreciation_start_date = fy.date_from
return depreciation_start_date
def _get_depreciation_stop_date(self, depreciation_start_date):
if self.method_time == "year" and not self.method_end:
depreciation_stop_date = depreciation_start_date + relativedelta(
years=self.method_number, days=-1
)
elif self.method_time == "number":
if self.method_period == "month":
depreciation_stop_date = depreciation_start_date + relativedelta(
months=self.method_number, days=-1
)
elif self.method_period == "quarter":
m = [x for x in [3, 6, 9, 12] if x >= depreciation_start_date.month][0]
first_line_date = depreciation_start_date + relativedelta(
month=m, day=31
)
months = self.method_number * 3
depreciation_stop_date = first_line_date + relativedelta(
months=months - 1, days=-1
)
elif self.method_period == "year":
depreciation_stop_date = depreciation_start_date + relativedelta(
years=self.method_number, days=-1
)
elif self.method_time == "year" and self.method_end:
depreciation_stop_date = self.method_end
return depreciation_stop_date
def _get_first_period_amount(
self, table, entry, depreciation_start_date, line_dates
):
"""
Return prorata amount for Time Method 'Year' in case of
'Prorata Temporis'
"""
amount = entry.get("period_amount")
if self.prorata and self.method_time == "year":
dates = [x for x in line_dates if x <= entry["date_stop"]]
full_periods = len(dates) - 1
amount = entry["fy_amount"] - amount * full_periods
return amount
def _get_amount_linear(
self, depreciation_start_date, depreciation_stop_date, entry
):
"""
Override this method if you want to compute differently the
yearly amount.
"""
if not self.use_leap_years and self.method_number:
return self.depreciation_base / self.method_number
year = entry["date_stop"].year
cy_days = calendar.isleap(year) and 366 or 365
days = (depreciation_stop_date - depreciation_start_date).days + 1
return (self.depreciation_base / days) * cy_days
def _compute_year_amount(
self, residual_amount, depreciation_start_date, depreciation_stop_date, entry
):
"""
Localization: override this method to change the degressive-linear
calculation logic according to local legislation.
"""
if self.method_time != "year":
raise UserError(
_(
"The '_compute_year_amount' method is only intended for "
"Time Method 'Number of Years'."
)
)
year_amount_linear = self._get_amount_linear(
depreciation_start_date, depreciation_stop_date, entry
)
if self.method == "linear":
return year_amount_linear
if self.method == "linear-limit":
if (residual_amount - year_amount_linear) < self.salvage_value:
return residual_amount - self.salvage_value
else:
return year_amount_linear
year_amount_degressive = residual_amount * self.method_progress_factor
if self.method == "degressive":
return year_amount_degressive
if self.method == "degr-linear":
if year_amount_linear > year_amount_degressive:
return min(year_amount_linear, residual_amount)
else:
return min(year_amount_degressive, residual_amount)
if self.method == "degr-limit":
if (residual_amount - year_amount_degressive) < self.salvage_value:
return residual_amount - self.salvage_value
else:
return year_amount_degressive
else:
raise UserError(_("Illegal value %s in asset.method.") % self.method)
def _compute_line_dates(self, table, start_date, stop_date):
"""
The posting dates of the accounting entries depend on the
chosen 'Period Length' as follows:
- month: last day of the month
- quarter: last of the quarter
- year: last day of the fiscal year
Override this method if another posting date logic is required.
"""
line_dates = []
if self.method_period == "month":
line_date = start_date + relativedelta(day=31)
if self.method_period == "quarter":
m = [x for x in [3, 6, 9, 12] if x >= start_date.month][0]
line_date = start_date + relativedelta(month=m, day=31)
elif self.method_period == "year":
line_date = table[0]["date_stop"]
i = 1
while line_date < stop_date:
line_dates.append(line_date)
if self.method_period == "month":
line_date = line_date + relativedelta(months=1, day=31)
elif self.method_period == "quarter":
line_date = line_date + relativedelta(months=3, day=31)
elif self.method_period == "year":
line_date = table[i]["date_stop"]
i += 1
# last entry
if not (self.method_time == "number" and len(line_dates) == self.method_number):
if self.days_calc:
line_dates.append(stop_date)
else:
line_dates.append(line_date)
return line_dates
def _compute_depreciation_amount_per_fiscal_year(
self, table, line_dates, depreciation_start_date, depreciation_stop_date
):
self.ensure_one()
currency = self.company_id.currency_id
fy_residual_amount = self.depreciation_base
i_max = len(table) - 1
asset_sign = self.depreciation_base >= 0 and 1 or -1
day_amount = 0.0
if self.days_calc:
days = (depreciation_stop_date - depreciation_start_date).days + 1
day_amount = self.depreciation_base / days
for i, entry in enumerate(table):
if self.method_time == "year":
year_amount = self._compute_year_amount(
fy_residual_amount,
depreciation_start_date,
depreciation_stop_date,
entry,
)
if self.method_period == "year":
period_amount = year_amount
elif self.method_period == "quarter":
period_amount = year_amount / 4
elif self.method_period == "month":
period_amount = year_amount / 12
if i == i_max:
if self.method in ["linear-limit", "degr-limit"]:
fy_amount = fy_residual_amount - self.salvage_value
else:
fy_amount = fy_residual_amount
else:
firstyear = i == 0 and True or False
fy_factor = self._get_fy_duration_factor(entry, firstyear)
fy_amount = year_amount * fy_factor
if (
currency.compare_amounts(
asset_sign * (fy_amount - fy_residual_amount), 0
)
> 0
):
fy_amount = fy_residual_amount
period_amount = currency.round(period_amount)
fy_amount = currency.round(fy_amount)
else:
fy_amount = False
if self.method_time == "number":
number = self.method_number
else:
number = len(line_dates)
period_amount = currency.round(self.depreciation_base / number)
entry.update(
{
"period_amount": period_amount,