forked from OCA/l10n-spain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount_asset.py
243 lines (220 loc) · 11.2 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
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (c) 2012 Serv. Tecnol. Avanzados (http://www.serviciosbaeza.com)
# Pedro Manuel Baeza <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import calendar
from openerp.osv import orm, fields
from openerp import fields as new_fields
from dateutil.relativedelta import relativedelta
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT as DSDF
class account_asset_category(orm.Model):
_inherit = 'account.asset.category'
_columns = {
'ext_method_time': fields.selection(
[('number', 'Number of Depreciations'),
('end', 'Ending Date'),
('percentage', 'Fixed percentage')], 'Time Method', required=True,
help="Choose the method to use to compute the dates and number of "
"depreciation lines.\n"
" * Number of Depreciations: Fix the number of depreciation "
"lines and the time between 2 depreciations.\n"
" * Ending Date: Choose the time between 2 depreciations "
"and the date the depreciations won't go beyond.\n"
" * Fixed percentage: Choose the time between 2 "
"depreciations and the percentage to depreciate."),
'method_percentage': fields.float('Depreciation percentage',
digits=(3, 2)),
}
_defaults = {
'method_percentage': 100.0,
'ext_method_time': 'percentage',
}
_sql_constraints = [
('method_percentage',
' CHECK (method_percentage > 0 and method_percentage <= 100)',
'Wrong percentage!'),
]
def onchange_ext_method_time(self, cr, uid, ids, ext_method_time,
context=None):
res = {'value': {}}
res['value']['method_time'] = ('end' if ext_method_time == 'end'
else 'number')
return res
class account_asset_asset(orm.Model):
_inherit = 'account.asset.asset'
_columns = {
'move_end_period': fields.boolean("At the end of the period",
help='Move the depreciation entry at'
' the end of the period. If the '
'period are 12 months, it is put on '
'31st of December, and in the end of'
' the month in other case.'),
# Hay que definir un nuevo campo y jugar con los valores del antiguo
# (method_time) para pasar el constraint _check_prorata y no tener que
# modificar mucho código base
'ext_method_time': fields.selection(
[('number', 'Number of Depreciations'),
('end', 'Ending Date'),
('percentage', 'Fixed percentage')], 'Time Method', required=True,
help="Choose the method to use to compute the dates and number of "
"depreciation lines.\n"
" * Number of Depreciations: Fix the number of depreciation "
"lines and the time between 2 depreciations.\n"
" * Ending Date: Choose the time between 2 depreciations "
"and the date the depreciations won't go beyond.\n"
" * Fixed percentage: Choose the time between 2 "
"depreciations and the percentage to depreciate."),
'method_percentage': fields.float('Depreciation percentage',
digits=(3, 2)),
}
_defaults = {
'method_percentage': 100.0,
'ext_method_time': 'percentage',
'move_end_period': True,
}
_sql_constraints = [
('method_percentage',
' CHECK (method_percentage > 0 and method_percentage <= 100)',
'Wrong percentage!'),
]
def onchange_ext_method_time(self, cr, uid, ids, ext_method_time,
context=None):
res = {'value': {}}
res['value']['method_time'] = ('end' if ext_method_time == 'end'
else 'number')
return res
def onchange_category_id(self, cr, uid, ids, category_id, context=None):
res = super(account_asset_asset, self).onchange_category_id(
cr, uid, ids,
category_id, context=context)
if category_id:
category_obj = self.pool.get('account.asset.category')
category = category_obj.browse(cr, uid, category_id,
context=context)
res['value']['ext_method_time'] = category.ext_method_time
res['value']['method_percentage'] = category.method_percentage
return res
def _compute_board_undone_dotation_nb(self, cr, uid, asset,
depreciation_date, total_days,
context=None):
if asset.ext_method_time == 'percentage':
number = 0
percentage = 100.0
while percentage > 0:
if number == 0 and asset.prorata:
days = (total_days -
float(depreciation_date.strftime('%j'))) + 1
percentage -= asset.method_percentage * days / total_days
else:
percentage -= asset.method_percentage
number += 1
return number
else:
val = super(account_asset_asset, self).\
_compute_board_undone_dotation_nb(cr, uid, asset,
depreciation_date,
total_days, context=context)
if depreciation_date.day == 1 and depreciation_date.month == 1 \
and asset.method_period == 12:
# Quitar una depreciación del nº total si el activo se compró
# el 1 de enero, ya que ese año sería completo
val -= 1
return val
def _compute_board_amount(self, cr, uid, asset, i, residual_amount,
amount_to_depr, undone_dotation_number,
posted_depreciation_line_ids, total_days,
depreciation_date, context=None):
if asset.ext_method_time == 'percentage':
# Nuevo tipo de cálculo
if i == undone_dotation_number:
return residual_amount
else:
if i == 1 and asset.prorata:
days = (total_days -
float(depreciation_date.strftime('%j'))) + 1
percentage = asset.method_percentage * days / total_days
else:
percentage = asset.method_percentage
return amount_to_depr * percentage / 100
elif (asset.method == 'linear' and asset.prorata and
i != undone_dotation_number):
# Caso especial de cálculo que cambia
amount = amount_to_depr / asset.method_number
if i == 1:
days = (total_days -
float(depreciation_date.strftime('%j'))) + 1
amount *= days / total_days
return amount
else:
return super(account_asset_asset, self)._compute_board_amount(
cr, uid, asset, i, residual_amount, amount_to_depr,
undone_dotation_number, posted_depreciation_line_ids,
total_days, depreciation_date, context=context)
def compute_depreciation_board(self, cr, uid, ids, context=None):
super(account_asset_asset, self).compute_depreciation_board(
cr, uid,
ids, context=context)
for asset in self.browse(cr, uid, ids, context=context):
if asset.move_end_period:
# Reescribir la fecha de la depreciación
depr_lin_obj = self.pool.get('account.asset.depreciation.line')
new_depr_line_ids = depr_lin_obj.search(
cr, uid,
[('asset_id', '=', asset.id), ('move_id', '=', False)])
# En el caso de que la fecha de última amortización no sea
# la de compra se debe generar el cuadro al período siguiente
depreciation_date = new_fields.Date.\
from_string(self.
_get_last_depreciation_date(cr, uid,
[asset.id],
context)[asset.id])
initial_date = asset.purchase_date
fixed_depreciation = depreciation_date != new_fields.Date.\
from_string(initial_date)
nb = 0
for depr_line in depr_lin_obj.browse(cr, uid,
new_depr_line_ids):
depr_date = new_fields.Date.from_string(
depr_line.depreciation_date)
if fixed_depreciation:
if depr_date.day != 1:
depr_date += relativedelta(
months=+asset.method_period)
if asset.method_period == 12:
depr_date = depr_date.replace(depr_date.year, 12, 31)
else:
if not asset.prorata:
if depr_date.day != 1:
depr_date = depreciation_date + relativedelta(
months=+ (asset.method_period * (nb+1)))
else:
depr_date = depreciation_date + relativedelta(
months=+ (asset.method_period * nb))
nb += 1
last_month_day = calendar.monthrange(
depr_date.year, depr_date.month)[1]
depr_date = depr_date.replace(depr_date.year,
depr_date.month,
last_month_day)
depr_lin_obj.write(
cr, uid, depr_line.id,
{'depreciation_date': depr_date.strftime(DSDF)})
return True
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: