trytond-sale_product_shop/sale.py

53 lines
1.7 KiB
Python
Raw Permalink Normal View History

2015-04-21 11:41:18 +02:00
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
from trytond.pool import Pool, PoolMeta
2015-04-23 15:41:56 +02:00
from trytond.transaction import Transaction
2015-04-21 11:41:18 +02:00
__all__ = ['Sale']
class Sale:
2016-03-29 12:00:40 +02:00
__metaclass__ = PoolMeta
2015-04-21 11:41:18 +02:00
__name__ = 'sale.sale'
@classmethod
def __setup__(cls):
super(Sale, cls).__setup__()
cls._error_messages.update({
'not_available_in_shop': 'Product %s is not available for '
'its sale in shop %s.',
})
@classmethod
def quote(cls, sales):
cls.available_in_shop(sales)
super(Sale, cls).quote(sales)
2015-04-23 13:23:53 +02:00
@classmethod
def check_product_shop(cls):
if Transaction().context.get('without_warning'):
return False
return True
2015-04-21 11:41:18 +02:00
@classmethod
def available_in_shop(cls, sales):
ProductShop = Pool().get('product.template-sale.shop')
2015-04-23 13:23:53 +02:00
if not cls.check_product_shop():
return
2015-04-21 11:41:18 +02:00
for sale in sales:
2015-04-23 15:41:11 +02:00
templates = list({l.product.template for s in sales
for l in s.lines if l.product})
2015-04-23 15:34:32 +02:00
shop_products = ProductShop.search([('template', 'in', templates)])
2015-04-21 11:41:18 +02:00
products = [p for sp in shop_products
for p in sp.template.products]
for line in sale.lines:
2016-03-16 14:14:28 +01:00
if (line.type == 'line' and line.product
and line.product not in products):
2015-04-21 11:41:18 +02:00
cls.raise_user_warning(
'not_available_in_shop_%s' % line.id,
'not_available_in_shop',
(line.product.name, sale.shop.name)
)