Files
tryton/purchase.py
T

71 lines
2.6 KiB
Python
Raw Normal View History

2014-12-02 12:56:16 +01:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
2008-10-01 13:13:56 +02:00
import datetime
2019-12-18 21:41:21 +01:00
from trytond.model import ModelSQL, fields
2019-01-01 15:47:23 +01:00
from trytond.pool import PoolMeta, Pool
from trytond.transaction import Transaction
2012-09-24 22:18:25 +02:00
2008-10-01 13:13:56 +02:00
2018-06-26 19:11:58 +02:00
class ProductSupplier(metaclass=PoolMeta):
2012-09-24 22:18:25 +02:00
__name__ = 'purchase.product_supplier'
2019-12-18 21:41:21 +01:00
weekdays = fields.Many2Many(
'purchase.product_supplier.day', 'product_supplier', 'day',
"Week Days")
2012-09-24 22:18:25 +02:00
def compute_supply_date(self, date=None):
date = super(ProductSupplier, self).compute_supply_date(date=date)
earlier_date = None
2016-01-21 10:33:12 +01:00
if date != datetime.date.max:
2019-01-01 15:47:23 +01:00
for weekday in self.weekdays:
2019-12-18 21:41:21 +01:00
diff = weekday.index - date.weekday()
2016-01-21 10:33:12 +01:00
if diff < 0:
diff += 7
new_date = date + datetime.timedelta(diff)
2016-01-21 10:33:12 +01:00
if earlier_date and earlier_date <= new_date:
continue
earlier_date = new_date
return earlier_date or date
2012-09-24 22:18:25 +02:00
def compute_purchase_date(self, date):
later_date = None
2019-01-01 15:47:23 +01:00
for weekday in self.weekdays:
2019-12-18 21:41:21 +01:00
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
2012-09-24 22:18:25 +02:00
return super(ProductSupplier, self).compute_purchase_date(date)
2019-12-18 21:41:21 +01:00
class ProductSupplierDay(ModelSQL):
2009-03-17 14:06:46 +01:00
'Product Supplier Day'
2012-09-24 22:18:25 +02:00
__name__ = 'purchase.product_supplier.day'
2019-12-18 21:41:21 +01:00
product_supplier = fields.Many2One(
'purchase.product_supplier', 'Supplier',
required=True, ondelete='CASCADE', select=True)
2019-01-01 15:47:23 +01:00
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))
2019-01-01 15:47:23 +01:00
for day_id, index in cursor:
update.execute(*table.update(
[table.day], [day_id],
where=table.weekday == str(index)))
table_h.drop_column('weekday')