trytond-product_qty/product.py

65 lines
2.4 KiB
Python
Raw Normal View History

2015-03-25 13:57:34 +01:00
# This file is part product_qty module for Tryton.
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction
from trytond.pyson import If, Eval, Less
2015-03-25 13:57:34 +01:00
__all__ = ['Template', 'Product']
class Template:
2016-03-29 12:00:32 +02:00
__metaclass__ = PoolMeta
2015-03-25 13:57:34 +01:00
__name__ = 'product.template'
def sum_product(self, name):
Location = Pool().get('stock.location')
if (name in ('quantity', 'forecast_quantity') and
'locations' not in Transaction().context):
warehouses = Location.search([('type', '=', 'warehouse')])
location_ids = [w.storage_location.id for w in warehouses]
with Transaction().set_context(locations=location_ids):
return super(Template, self).sum_product(name)
return super(Template, self).sum_product(name)
class Product:
2016-03-29 12:00:32 +02:00
__metaclass__ = PoolMeta
2015-03-25 13:57:34 +01:00
__name__ = 'product.product'
@classmethod
def get_quantity(cls, products, name):
2016-02-23 09:23:14 +01:00
pool = Pool()
Location = pool.get('stock.location')
Date = pool.get('ir.date')
2015-03-25 13:57:34 +01:00
2016-02-23 09:23:14 +01:00
today = Date.today()
2015-09-16 16:00:03 +02:00
context = Transaction().context
2016-02-23 09:23:14 +01:00
2015-03-25 13:57:34 +01:00
# not locations in context
2015-09-16 16:00:03 +02:00
if not context.get('locations'):
2015-03-25 13:57:34 +01:00
warehouses = Location.search([('type', '=', 'warehouse')])
location_ids = [w.storage_location.id for w in warehouses]
2016-02-23 09:23:14 +01:00
with Transaction().set_context(locations=location_ids, stock_date_end=today):
2015-03-25 13:57:34 +01:00
return cls._get_quantity(products, name, location_ids, products)
# return super (with locations in context)
return super(Product, cls).get_quantity(products, name)
@classmethod
def search_quantity(cls, name, domain=None):
2016-02-23 09:23:14 +01:00
pool = Pool()
Location = pool.get('stock.location')
Date = pool.get('ir.date')
2015-03-25 13:57:34 +01:00
2016-02-23 09:23:14 +01:00
today = Date.today()
2015-09-16 16:00:03 +02:00
context = Transaction().context
2015-03-25 13:57:34 +01:00
# not locations in context
2015-09-16 16:00:03 +02:00
if not context.get('locations'):
2015-03-25 13:57:34 +01:00
warehouses = Location.search([('type', '=', 'warehouse')])
location_ids = [w.storage_location.id for w in warehouses]
2016-02-23 09:23:14 +01:00
with Transaction().set_context(locations=location_ids, stock_date_end=today):
2015-03-25 13:57:34 +01:00
return cls._search_quantity(name, location_ids, domain)
# return super (with locations in context)
return super(Product, cls).search_quantity(name, domain)