Use ir.calendar.month as selection for tariff_code's month fields

issue11892
review418721003
This commit is contained in:
Maxime Richez committed 2022-11-21 10:12:40 +01:00
1 parent 5c9f96d0c2
commit e7ead35ae5
4 files changed
+61 -33

No files matched your search

+2
View File
@@ -1,3 +1,5 @@
* Use ir.calendar.month for tariff code months
Version 6.6.0 - 2022-10-31
--------------------------
* Bug fixes (see mercurial logs for details)
+49 -27
View File
@@ -5,28 +5,13 @@ 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
# Use 2 chars numbering to allow string comparison
MONTHS = [
(None, ''),
('01', 'January'),
('02', 'February'),
('03', 'March'),
('04', 'April'),
('05', 'May'),
('06', 'June'),
('07', 'July'),
('08', 'August'),
('09', 'September'),
('10', 'October'),
('11', 'November'),
('12', 'December'),
]
from trytond.transaction import Transaction
class TariffCode(DeactivableMixin, ModelSQL, ModelView, MatchMixin):
@@ -38,30 +23,26 @@ class TariffCode(DeactivableMixin, ModelSQL, ModelView, MatchMixin):
description = fields.Char('Description', translate=True)
country = fields.Many2One('country.country', 'Country')
# TODO country group
start_month = fields.Selection(MONTHS, 'Start Month', sort=False,
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', '<=', If(Eval('start_month').in_(
['01', '03', '05', '07', '08', '10', '12']), 31,
If(Eval('start_month') == '02', 29, 30))),
('start_day', '=', None),
[('start_day', '>=', 1), ('start_day', '<=', 31)],
],
states={
'required': Bool(Eval('start_month')),
})
end_month = fields.Selection(MONTHS, 'End Month', sort=False,
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', '<=', If(Eval('end_month').in_(
['01', '03', '05', '07', '08', '10', '12']), 31,
If(Eval('end_month') == '02', 29, 30))),
('end_day', '=', None),
[('end_day', '>=', 1), ('end_day', '<=', 31)],
],
states={
'required': Bool(Eval('end_month')),
@@ -74,13 +55,54 @@ class TariffCode(DeactivableMixin, ModelSQL, ModelView, MatchMixin):
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 = (int(self.start_month), self.start_day)
end = (int(self.end_month), self.end_day)
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):
+8 -4
View File
@@ -20,12 +20,16 @@ class CustomsTestCase(ModuleTestCase):
Template = pool.get('product.template')
Product_TariffCode = pool.get(
'product-customs.tariff.code')
Month = pool.get('ir.calendar.month')
june, = Month.search([('index', '=', 6)])
august, = Month.search([('index', '=', 8)])
# Test start <= end
tariff1 = Tariff(code='170390')
tariff2 = Tariff(code='17039099',
start_month='06', start_day=20,
end_month='08', end_day=20)
start_month=june, start_day=20,
end_month=august, end_day=20)
Tariff.save([tariff1, tariff2])
template = Template(tariff_codes=[
Product_TariffCode(tariff_code=tariff2),
@@ -40,8 +44,8 @@ class CustomsTestCase(ModuleTestCase):
self.assertEqual(template.get_tariff_code(pattern), result)
# Test start > end
tariff2.start_month = '08'
tariff2.end_month = '06'
tariff2.start_month = august
tariff2.end_month = june
tariff2.save()
for pattern, result in [
+2 -2
View File
@@ -15,11 +15,11 @@ this repository contains the full copyright notices and license terms. -->
<label id="from" string="From"/>
<group id="start" col="2">
<field name="start_day"/>
<field name="start_month"/>
<field name="start_month" widget="selection"/>
</group>
<label id="to" string="To"/>
<group id="end" col="2">
<field name="end_day"/>
<field name="end_month"/>
<field name="end_month" widget="selection"/>
</group>
</form>