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
|
# 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 ModelSQL, fields
from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction
class ProductSupplier(metaclass=PoolMeta):
__name__ = 'purchase.product_supplier'
weekdays = fields.Many2Many(
'purchase.product_supplier.day', 'product_supplier', 'day',
"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:
diff = weekday.index - 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:
diff = (date.weekday() - weekday.index) % 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):
'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')
|