Don't use Period().get_cache_vals but extend PeriodCache.create()

This commit is contained in:
Guillem Barba 2015-06-11 17:38:59 +02:00
parent 850c697ab6
commit 19f177b653
2 changed files with 66 additions and 37 deletions

View file

@ -4,6 +4,7 @@ from trytond.pool import Pool
from .product import *
from .stock import *
def register():
Pool.register(
Template,
@ -13,7 +14,6 @@ def register():
ShipmentIn,
ShipmentOut,
ShipmentOutReturn,
Period,
PeriodCache,
PeriodCacheLot,
Inventory,

101
stock.py
View file

@ -11,7 +11,7 @@ from trytond.pyson import Eval, In
from trytond.transaction import Transaction
__all__ = ['Lot', 'Move', 'ShipmentIn', 'ShipmentOut', 'ShipmentOutReturn',
'Period', 'PeriodCache', 'PeriodCacheLot', 'Inventory', 'InventoryLine']
'PeriodCache', 'PeriodCacheLot', 'Inventory', 'InventoryLine']
__metaclass__ = PoolMeta
STATES = {
@ -324,52 +324,81 @@ class ShipmentOutReturn:
return move
class Period:
__name__ = 'stock.period'
def get_cache_vals(self, Cache, grouping, locations):
pool = Pool()
Product = pool.get('product.product')
cache_vlist = super(Period, self).get_cache_vals(Cache, grouping,
locations)
if grouping not in [('product',), ('product', 'lot')]:
return cache_vlist
cache_vals_by_key = {}
for values in cache_vlist:
key = (values['location'], ) + tuple([values[f] for f in grouping])
cache_vals_by_key[key] = values
cache_vlist = []
with Transaction().set_context(
stock_date_end=self.date,
stock_date_start=None,
stock_assign=False,
forecast=False,
stock_destinations=None,
second_uom=True,
):
pbl = Product.products_by_location(
[l.id for l in locations], grouping=grouping)
for key, quantity in pbl.iteritems():
values = cache_vals_by_key[key]
values['second_internal_quantity'] = quantity
cache_vlist.append(values)
return cache_vlist
class PeriodCache:
__name__ = 'stock.period.cache'
second_internal_quantity = fields.Float('Second Internal Quantity',
readonly=True)
@classmethod
def create(cls, vlist):
pool = Pool()
Period = pool.get('stock.period')
Product = pool.get('product.product')
vlist_by_period_location = {}
for values in vlist:
vlist_by_period_location.setdefault(values['period'], {})\
.setdefault(values['location'], []).append(values)
vlist = []
for period_id, vlist_by_location in \
vlist_by_period_location.iteritems():
period = Period(period_id)
with Transaction().set_context(
stock_date_end=period.date,
stock_date_start=None,
stock_assign=False,
forecast=False,
stock_destinations=None,
second_uom=True,
):
pbl = Product.products_by_location(
vlist_by_location.keys(), grouping=('product',))
for location_id, location_vlist in vlist_by_location.iteritems():
for values in location_vlist:
key = (location_id, values['product'])
values['second_internal_quantity'] = pbl.get(key, 0.0)
vlist.append(values)
return super(PeriodCache, cls).create(vlist)
class PeriodCacheLot:
__name__ = 'stock.period.cache.lot'
second_internal_quantity = fields.Float('Second Internal Quantity',
readonly=True)
@classmethod
def create(cls, vlist):
pool = Pool()
Period = pool.get('stock.period')
Product = pool.get('product.product')
vlist_by_period_location = {}
for values in vlist:
vlist_by_period_location.setdefault(values['period'], {})\
.setdefault(values['location'], []).append(values)
vlist = []
for period_id, vlist_by_location in \
vlist_by_period_location.iteritems():
period = Period(period_id)
with Transaction().set_context(
stock_date_end=period.date,
stock_date_start=None,
stock_assign=False,
forecast=False,
stock_destinations=None,
second_uom=True,
):
pbl = Product.products_by_location(
vlist_by_location.keys(), grouping=('product', 'lot'))
for location_id, location_vlist in vlist_by_location.iteritems():
for values in location_vlist:
key = (location_id, values['product'], values['lot'])
values['second_internal_quantity'] = pbl.get(key, 0.0)
vlist.append(values)
return super(PeriodCacheLot, cls).create(vlist)
class Inventory:
__name__ = 'stock.inventory'