trytond-product_qty/product.py

68 lines
2.6 KiB
Python
Raw Permalink 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.
import datetime
2015-03-25 13:57:34 +01:00
from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction
__all__ = ['Template', 'Product']
2018-09-07 07:20:08 +02:00
class Template(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,
with_childs=True):
2015-03-25 13:57:34 +01:00
return super(Template, self).sum_product(name)
return super(Template, self).sum_product(name)
2018-09-07 07:20:08 +02:00
class Product(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
stock_date = Date.today() if name == 'product' else datetime.date.max
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]
with Transaction().set_context(locations=location_ids,
stock_date_end=stock_date, with_childs=True):
2018-09-07 07:25:22 +02:00
products_ids = list(map(int, products))
return cls._get_quantity(products, name, location_ids,
grouping_filter=(products_ids,))
2015-03-25 13:57:34 +01:00
# 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]
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)