This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
payment_term.py
398 lines (348 loc) · 14.8 KB
/
payment_term.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
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from decimal import Decimal
from dateutil.relativedelta import relativedelta
from sql import Column
from trytond import backend
from trytond.config import config
from trytond.i18n import gettext
from trytond.model import (
DeactivableMixin, ModelSQL, ModelView, fields, sequence_ordered)
from trytond.modules.currency.fields import Monetary
from trytond.pool import Pool
from trytond.pyson import Eval
from trytond.transaction import Transaction
from trytond.wizard import Button, StateView, Wizard
from .exceptions import PaymentTermComputeError, PaymentTermValidationError
class PaymentTerm(DeactivableMixin, ModelSQL, ModelView):
'Payment Term'
__name__ = 'account.invoice.payment_term'
name = fields.Char('Name', size=None, required=True, translate=True)
description = fields.Text('Description', translate=True)
lines = fields.One2Many('account.invoice.payment_term.line', 'payment',
'Lines')
@classmethod
def __setup__(cls):
super(PaymentTerm, cls).__setup__()
cls._order.insert(0, ('name', 'ASC'))
@classmethod
def validate_fields(cls, terms, field_names):
super().validate_fields(terms, field_names)
cls.check_remainder(terms, field_names)
@classmethod
def check_remainder(cls, terms, field_names=None):
if field_names and 'lines' not in field_names:
return
for term in terms:
if not term.lines or not term.lines[-1].type == 'remainder':
raise PaymentTermValidationError(gettext(
'account_invoice'
'.msg_payment_term_missing_last_remainder',
payment_term=term.rec_name))
def compute(self, amount, currency, date):
"""Calculate payment terms and return a list of tuples
with (date, amount) for each payment term line.
amount must be a Decimal used for the calculation.
"""
# TODO implement business_days
# http://pypi.python.org/pypi/BusinessHours/
sign = 1 if amount >= Decimal('0.0') else -1
res = []
remainder = amount
for line in self.lines:
value = line.get_value(remainder, amount, currency)
value_date = line.get_date(date)
if value is None or not value_date:
continue
if ((remainder - value) * sign) < Decimal('0.0'):
res.append((value_date, remainder))
break
if value:
res.append((value_date, value))
remainder -= value
else:
# Enforce to have at least one term
if not res:
res.append((date, Decimal(0)))
if not currency.is_zero(remainder):
raise PaymentTermComputeError(
gettext('account_invoice.msg_payment_term_missing_remainder',
payment_term=self.rec_name))
return res
class PaymentTermLine(sequence_ordered(), ModelSQL, ModelView):
'Payment Term Line'
__name__ = 'account.invoice.payment_term.line'
payment = fields.Many2One('account.invoice.payment_term', 'Payment Term',
required=True, ondelete="CASCADE")
type = fields.Selection([
('fixed', 'Fixed'),
('percent', 'Percentage on Remainder'),
('percent_on_total', 'Percentage on Total'),
('remainder', 'Remainder'),
], 'Type', required=True)
ratio = fields.Numeric('Ratio', digits=(14, 10),
states={
'invisible': ~Eval('type').in_(['percent', 'percent_on_total']),
'required': Eval('type').in_(['percent', 'percent_on_total']),
})
divisor = fields.Numeric('Divisor', digits=(10, 14),
states={
'invisible': ~Eval('type').in_(['percent', 'percent_on_total']),
'required': Eval('type').in_(['percent', 'percent_on_total']),
})
amount = Monetary(
"Amount", currency='currency', digits='currency',
states={
'invisible': Eval('type') != 'fixed',
'required': Eval('type') == 'fixed',
})
currency = fields.Many2One('currency.currency', 'Currency',
states={
'invisible': Eval('type') != 'fixed',
'required': Eval('type') == 'fixed',
})
relativedeltas = fields.One2Many(
'account.invoice.payment_term.line.delta', 'line', 'Deltas')
@classmethod
def __setup__(cls):
super().__setup__()
cls.__access__.add('payment')
@classmethod
def __register__(cls, module_name):
sql_table = cls.__table__()
super(PaymentTermLine, cls).__register__(module_name)
cursor = Transaction().connection.cursor()
table = cls.__table_handler__(module_name)
# Migration from 3.8: rename percentage into ratio
if table.column_exist('percentage'):
cursor.execute(*sql_table.update(
columns=[sql_table.ratio],
values=[sql_table.percentage / 100]))
table.drop_column('percentage')
@staticmethod
def default_type():
return 'remainder'
@classmethod
def default_relativedeltas(cls):
if Transaction().user == 0:
return []
return [{}]
@fields.depends('type')
def on_change_type(self):
if self.type != 'fixed':
self.amount = Decimal('0.0')
self.currency = None
if self.type not in ('percent', 'percent_on_total'):
self.ratio = Decimal('0.0')
self.divisor = Decimal('0.0')
@fields.depends('ratio')
def on_change_ratio(self):
if not self.ratio:
self.divisor = Decimal('0.0')
else:
self.divisor = self.round(1 / self.ratio,
self.__class__.divisor.digits[1])
@fields.depends('divisor')
def on_change_divisor(self):
if not self.divisor:
self.ratio = Decimal('0.0')
else:
self.ratio = self.round(1 / self.divisor,
self.__class__.ratio.digits[1])
def get_date(self, date):
for relativedelta_ in self.relativedeltas:
date += relativedelta_.get()
return date
def get_value(self, remainder, amount, currency):
Currency = Pool().get('currency.currency')
if self.type == 'fixed':
fixed = Currency.compute(self.currency, self.amount, currency)
return fixed.copy_sign(amount)
elif self.type == 'percent':
return currency.round(remainder * self.ratio)
elif self.type == 'percent_on_total':
return currency.round(amount * self.ratio)
elif self.type == 'remainder':
return currency.round(remainder)
return None
@staticmethod
def round(number, digits):
quantize = Decimal(10) ** -Decimal(digits)
return Decimal(number).quantize(quantize)
@classmethod
def validate_fields(cls, lines, field_names):
super().validate_fields(lines, field_names)
cls.check_ratio_and_divisor(lines, field_names)
@classmethod
def check_ratio_and_divisor(cls, lines, field_names=None):
"Check consistency between ratio and divisor"
if field_names and not (field_names & {'type', 'ratio', 'divisor'}):
return
for line in lines:
if line.type not in ('percent', 'percent_on_total'):
continue
if line.ratio is None or line.divisor is None:
raise PaymentTermValidationError(
gettext('account_invoice'
'.msg_payment_term_invalid_ratio_divisor',
line=line.rec_name))
if (line.ratio != round(
1 / line.divisor, cls.ratio.digits[1])
and line.divisor != round(
1 / line.ratio, cls.divisor.digits[1])):
raise PaymentTermValidationError(
gettext('account_invoice'
'.msg_payment_term_invalid_ratio_divisor',
line=line.rec_name))
class PaymentTermLineRelativeDelta(sequence_ordered(), ModelSQL, ModelView):
'Payment Term Line Relative Delta'
__name__ = 'account.invoice.payment_term.line.delta'
line = fields.Many2One('account.invoice.payment_term.line',
'Payment Term Line', required=True, ondelete='CASCADE')
day = fields.Integer('Day of Month',
domain=['OR',
('day', '=', None),
[('day', '>=', 1), ('day', '<=', 31)],
])
month = fields.Many2One('ir.calendar.month', "Month")
weekday = fields.Many2One('ir.calendar.day', "Day of Week")
months = fields.Integer('Number of Months', required=True)
weeks = fields.Integer('Number of Weeks', required=True)
days = fields.Integer('Number of Days', required=True)
@classmethod
def __setup__(cls):
super().__setup__()
cls.__access__.add('line')
@classmethod
def __register__(cls, module_name):
transaction = Transaction()
cursor = transaction.connection.cursor()
pool = Pool()
Line = pool.get('account.invoice.payment_term.line')
Month = pool.get('ir.calendar.month')
Day = pool.get('ir.calendar.day')
sql_table = cls.__table__()
line = Line.__table__()
month = Month.__table__()
day = Day.__table__()
table_h = cls.__table_handler__(module_name)
# Migration from 4.0: rename long table
old_model_name = 'account.invoice.payment_term.line.relativedelta'
old_table = config.get(
'table', old_model_name, default=old_model_name.replace('.', '_'))
if backend.TableHandler.table_exist(old_table):
backend.TableHandler.table_rename(old_table, cls._table)
# Migration from 5.0: use ir.calendar
migrate_calendar = False
if (backend.TableHandler.table_exist(cls._table)
and table_h.column_exist('month')
and table_h.column_exist('weekday')):
migrate_calendar = (
table_h.column_is_type('month', 'VARCHAR')
or table_h.column_is_type('weekday', 'VARCHAR'))
if migrate_calendar:
table_h.column_rename('month', '_temp_month')
table_h.column_rename('weekday', '_temp_weekday')
super(PaymentTermLineRelativeDelta, cls).__register__(module_name)
table_h = cls.__table_handler__(module_name)
line_table = Line.__table_handler__(module_name)
# Migration from 3.4
fields = ['day', 'month', 'weekday', 'months', 'weeks', 'days']
if any(line_table.column_exist(f) for f in fields):
columns = ([line.id.as_('line')]
+ [Column(line, f) for f in fields])
cursor.execute(*sql_table.insert(
columns=[sql_table.line]
+ [Column(sql_table, f) for f in fields],
values=line.select(*columns)))
for field in fields:
line_table.drop_column(field)
# Migration from 5.0: use ir.calendar
if migrate_calendar:
update = transaction.connection.cursor()
cursor.execute(*month.select(month.id, month.index))
for month_id, index in cursor:
update.execute(*sql_table.update(
[sql_table.month], [month_id],
where=sql_table._temp_month == str(index)))
table_h.drop_column('_temp_month')
cursor.execute(*day.select(day.id, day.index))
for day_id, index in cursor:
update.execute(*sql_table.update(
[sql_table.weekday], [day_id],
where=sql_table._temp_weekday == str(index)))
table_h.drop_column('_temp_weekday')
@staticmethod
def default_months():
return 0
@staticmethod
def default_weeks():
return 0
@staticmethod
def default_days():
return 0
def get(self):
"Return the relativedelta"
return relativedelta(
day=self.day,
month=int(self.month.index) if self.month else None,
days=self.days,
weeks=self.weeks,
months=self.months,
weekday=int(self.weekday.index) if self.weekday else None,
)
class TestPaymentTerm(Wizard):
'Test Payment Term'
__name__ = 'account.invoice.payment_term.test'
start_state = 'test'
test = StateView('account.invoice.payment_term.test',
'account_invoice.payment_term_test_view_form',
[Button('Close', 'end', 'tryton-close', default=True)])
def default_test(self, fields):
default = {}
if (self.model
and self.model.__name__ == 'account.invoice.payment_term'):
default['payment_term'] = self.record.id if self.record else None
return default
class TestPaymentTermView(ModelView):
'Test Payment Term'
__name__ = 'account.invoice.payment_term.test'
payment_term = fields.Many2One('account.invoice.payment_term',
'Payment Term', required=True)
date = fields.Date("Date", required=True)
amount = Monetary(
"Amount", currency='currency', digits='currency', required=True)
currency = fields.Many2One('currency.currency', 'Currency', required=True)
result = fields.One2Many('account.invoice.payment_term.test.result',
None, 'Result', readonly=True)
@classmethod
def default_date(cls):
return Pool().get('ir.date').today()
@staticmethod
def default_currency():
pool = Pool()
Company = pool.get('company.company')
company = Transaction().context.get('company')
if company:
return Company(company).currency.id
@fields.depends('payment_term', 'date', 'amount', 'currency', 'result')
def on_change_with_result(self):
pool = Pool()
Result = pool.get('account.invoice.payment_term.test.result')
result = []
if (self.payment_term and self.amount and self.currency and self.date):
for date, amount in self.payment_term.compute(
self.amount, self.currency, self.date):
result.append(Result(
date=date,
amount=amount,
currency=self.currency))
self.result = result
return self._changed_values.get('result', [])
class TestPaymentTermViewResult(ModelView):
'Test Payment Term'
__name__ = 'account.invoice.payment_term.test.result'
date = fields.Date('Date', readonly=True)
amount = Monetary(
"Amount", currency='currency', digits='currency', readonly=True)
currency = fields.Many2One('currency.currency', "Currency")