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 2
/
customs.py
209 lines (186 loc) · 7.72 KB
/
customs.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
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import datetime
from decimal import Decimal
from sql import Null
from trytond import backend
from trytond.model import (
DeactivableMixin, MatchMixin, ModelSQL, ModelView, fields)
from trytond.modules.product import price_digits
from trytond.pool import Pool
from trytond.pyson import Bool, Eval, If
from trytond.transaction import Transaction
class TariffCode(DeactivableMixin, ModelSQL, ModelView, MatchMixin):
'Tariff Code'
__name__ = 'customs.tariff.code'
_rec_name = 'code'
code = fields.Char('Code', required=True,
help='The code from Harmonized System of Nomenclature.')
description = fields.Char('Description', translate=True)
country = fields.Many2One('country.country', 'Country')
# TODO country group
start_month = fields.Many2One('ir.calendar.month', "Start Month",
states={
'required': Eval('end_month') | Eval('start_day'),
})
start_day = fields.Integer('Start Day',
domain=['OR',
('start_day', '=', None),
[('start_day', '>=', 1), ('start_day', '<=', 31)],
],
states={
'required': Bool(Eval('start_month')),
})
end_month = fields.Many2One('ir.calendar.month', "End Month",
states={
'required': Eval('start_month') | Eval('end_day'),
})
end_day = fields.Integer('End Day',
domain=['OR',
('end_day', '=', None),
[('end_day', '>=', 1), ('end_day', '<=', 31)],
],
states={
'required': Bool(Eval('end_month')),
})
duty_rates = fields.One2Many('customs.duty.rate', 'tariff_code',
'Duty Rates')
@classmethod
def __setup__(cls):
super(TariffCode, cls).__setup__()
cls._order.insert(0, ('code', 'ASC'))
@classmethod
def __register__(cls, module_name):
transaction = Transaction()
cursor = transaction.connection.cursor()
pool = Pool()
Month = pool.get('ir.calendar.month')
sql_table = cls.__table__()
month = Month.__table__()
table_h = cls.__table_handler__(module_name)
# Migration from 6.6: use ir.calendar
migrate_calendar = False
if (backend.TableHandler.table_exist(cls._table)
and table_h.column_exist('start_month')
and table_h.column_exist('end_month')):
migrate_calendar = (
table_h.column_is_type('start_month', 'VARCHAR')
or table_h.column_is_type('end_month', 'VARCHAR'))
if migrate_calendar:
table_h.column_rename('start_month', '_temp_start_month')
table_h.column_rename('end_month', '_temp_end_month')
super().__register__(module_name)
table_h = cls.__table_handler__(module_name)
# Migration from 6.6: use ir.calendar
if migrate_calendar:
update = transaction.connection.cursor()
cursor.execute(*month.select(month.id, month.index))
for month_id, index in cursor:
str_index = f'{index:02d}'
update.execute(*sql_table.update(
[sql_table.start_month], [month_id],
where=sql_table._temp_start_month == str_index))
update.execute(*sql_table.update(
[sql_table.end_month], [month_id],
where=sql_table._temp_end_month == str_index))
table_h.drop_column('_temp_start_month')
table_h.drop_column('_temp_end_month')
def match(self, pattern):
if 'date' in pattern:
pattern = pattern.copy()
date = pattern.pop('date')
if self.start_month and self.end_month:
start = (self.start_month.index, self.start_day)
end = (self.end_month.index, self.end_day)
date = (date.month, date.day)
if start <= end:
if not (start <= date <= end):
return False
else:
if end <= date <= start:
return False
return super(TariffCode, self).match(pattern)
def get_duty_rate(self, pattern):
for rate in self.duty_rates:
if rate.match(pattern):
return rate
class DutyRate(ModelSQL, ModelView, MatchMixin):
'Duty Rate'
__name__ = 'customs.duty.rate'
tariff_code = fields.Many2One(
'customs.tariff.code', "Tariff Code", required=True)
country = fields.Many2One('country.country', 'Country')
# TODO country group
type = fields.Selection([
('import', 'Import'),
('export', 'Export'),
], 'Type')
start_date = fields.Date('Start Date',
domain=['OR',
('start_date', '<=', If(Bool(Eval('end_date')),
Eval('end_date', datetime.date.max), datetime.date.max)),
('start_date', '=', None),
])
end_date = fields.Date('End Date',
domain=['OR',
('end_date', '>=', If(Bool(Eval('start_date')),
Eval('start_date', datetime.date.min), datetime.date.min)),
('end_date', '=', None),
])
computation_type = fields.Selection([
('amount', 'Amount'),
('quantity', 'Quantity'),
], 'Computation Type')
amount = fields.Numeric('Amount', digits=price_digits,
states={
'required': Eval('computation_type').in_(['amount', 'quantity']),
'invisible': ~Eval('computation_type').in_(['amount', 'quantity']),
})
currency = fields.Many2One('currency.currency', 'Currency',
states={
'required': Eval('computation_type').in_(['amount', 'quantity']),
'invisible': ~Eval('computation_type').in_(['amount', 'quantity']),
})
uom = fields.Many2One('product.uom', 'Uom',
states={
'required': Eval('computation_type') == 'quantity',
'invisible': Eval('computation_type') != 'quantity',
})
@classmethod
def __setup__(cls):
super(DutyRate, cls).__setup__()
cls._order.insert(0, ('start_date', 'ASC'))
cls._order.insert(0, ('end_date', 'ASC'))
@classmethod
def default_type(cls):
return 'import'
@staticmethod
def order_start_date(tables):
table, _ = tables[None]
return [table.start_date == Null, table.start_date]
@staticmethod
def order_end_date(tables):
table, _ = tables[None]
return [table.end_date == Null, table.end_date]
def match(self, pattern):
if 'date' in pattern:
pattern = pattern.copy()
start = self.start_date or datetime.date.min
end = self.end_date or datetime.date.max
if not (start <= pattern.pop('date') <= end):
return False
return super(DutyRate, self).match(pattern)
def compute(self, currency, quantity, uom, **kwargs):
return getattr(self, 'compute_%s' % self.computation_type)(
currency, quantity, uom, **kwargs)
def compute_amount(self, currency, quantity, uom, **kwargs):
pool = Pool()
Currency = pool.get('currency.currency')
return Currency.compute(self.currency, self.amount, currency)
def compute_quantity(self, currency, quantity, uom, **kwargs):
pool = Pool()
Currency = pool.get('currency.currency')
Uom = pool.get('product.uom')
amount = Uom.compute_price(self.uom, self.amount, uom)
amount *= Decimal(str(quantity))
return Currency.compute(self.currency, amount, currency)