Files
tryton/purchase.py
T
Cédric Krier bc218140de Add ir.calendar for month and day
This allows to replace time_locale and use the translation infrastructure
instead. So it can be customized using the translation override or derivative
languages.
This provides also a standard base to modules that needs calendar month and
day.

issue7815
review56451002
2019-01-01 15:47:23 +01:00

79 lines
2.8 KiB
Python

# 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 trytond.model import ModelView, ModelSQL, fields
from trytond.pool import PoolMeta, Pool
from trytond.transaction import Transaction
__all__ = ['ProductSupplier', 'ProductSupplierDay']
class ProductSupplier(metaclass=PoolMeta):
__name__ = 'purchase.product_supplier'
weekdays = fields.One2Many('purchase.product_supplier.day',
'product_supplier', 'Week Days')
def compute_supply_date(self, date=None):
date = super(ProductSupplier, self).compute_supply_date(date=date)
earlier_date = None
if date != datetime.date.max:
for weekday in self.weekdays:
weekday = weekday.day.index
diff = weekday - date.weekday()
if diff < 0:
diff += 7
new_date = date + datetime.timedelta(diff)
if earlier_date and earlier_date <= new_date:
continue
earlier_date = new_date
return earlier_date or date
def compute_purchase_date(self, date):
later_date = None
for weekday in self.weekdays:
weekday = weekday.day.index
diff = (date.weekday() - weekday) % 7
new_date = date - datetime.timedelta(diff)
if later_date and later_date >= new_date:
continue
later_date = new_date
if later_date:
date = later_date
return super(ProductSupplier, self).compute_purchase_date(date)
class ProductSupplierDay(ModelSQL, ModelView):
'Product Supplier Day'
__name__ = 'purchase.product_supplier.day'
product_supplier = fields.Many2One('purchase.product_supplier', 'Supplier',
required=True, ondelete='CASCADE')
day = fields.Many2One('ir.calendar.day', "Day", required=True)
@classmethod
def __register__(cls, module_name):
pool = Pool()
Day = pool.get('ir.calendar.day')
day = Day.__table__()
transaction = Transaction()
table = cls.__table__()
super().__register__(module_name)
table_h = cls.__table_handler__(module_name)
# Migration from 5.0: replace weekday by day
if table_h.column_exist('weekday'):
cursor = transaction.connection.cursor()
update = transaction.connection.cursor()
cursor.execute(*day.select([day.id, day.index]))
for day_id, index in cursor:
update.execute(*table.update(
[table.day], [day_id],
where=table.weekday == str(index)))
table_h.drop_column('weekday')
@classmethod
def __setup__(cls):
super(ProductSupplierDay, cls).__setup__()
cls._order.insert(0, ('day', 'ASC'))