Add default location for inventory moves in shipment out and shipment in.

This commit refs #1564
This commit is contained in:
Sergio Morillo 2016-10-28 10:34:48 +02:00
parent 14f52a3c7c
commit 7d81b90194
3 changed files with 175 additions and 16 deletions

View file

@ -3,6 +3,7 @@
from trytond.pool import Pool
from .location import ProductCategoryLocation, Location
from .product import Category
from .stock import ShipmentOut, ShipmentIn
def register():
@ -10,4 +11,6 @@ def register():
Category,
Location,
ProductCategoryLocation,
ShipmentOut,
ShipmentIn,
module='stock_product_category_location', type_='model')

43
stock.py Normal file
View file

@ -0,0 +1,43 @@
# The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from trytond.pool import PoolMeta
__all__ = ['ShipmentOut', 'ShipmentIn']
class ShipmentOut:
__metaclass__ = PoolMeta
__name__ = 'stock.shipment.out'
def _get_inventory_move(self, move):
res = super(ShipmentOut, self)._get_inventory_move(move)
for c in res.product.categories:
_from_location = c.get_default_location(
**self._get_default_location_params())
if _from_location:
res.from_location = _from_location
break
return res
def _get_default_location_params(self):
return {'warehouse': self.warehouse}
class ShipmentIn:
__metaclass__ = PoolMeta
__name__ = 'stock.shipment.in'
@classmethod
def _get_inventory_moves(cls, incoming_move):
res = super(ShipmentIn, cls)._get_inventory_moves(incoming_move)
for c in res.product.categories:
_to_location = c.get_default_location(
**cls._get_default_location_params(incoming_move))
if _to_location:
res.to_location = _to_location
break
return res
@classmethod
def _get_default_location_params(cls, incoming_move):
return {'warehouse': incoming_move.shipment.warehouse}

View file

@ -1,52 +1,101 @@
====
==================================================
Check default and allowed location for categories
====
==================================================
Imports::
>>> import datetime
>>> from dateutil.relativedelta import relativedelta
>>> from decimal import Decimal
>>> from proteus import config, Model, Wizard
>>> from trytond.modules.company.tests.tools import create_company, \
... get_company
>>> today = datetime.date.today()
>>> yesterday = today - relativedelta(days=1)
Create database::
>>> config = config.set_trytond()
>>> config.pool.test = True
Install team_timesheet Module::
Install Module::
>>> Module = Model.get('ir.module')
>>> module, = Module.find([('name', '=', 'stock_product_category_location')])
>>> module.click('install')
>>> Wizard('ir.module.install_upgrade').execute('upgrade')
Create company::
>>> _ = create_company()
>>> company = get_company()
Reload the context::
>>> User = Model.get('res.user')
>>> config._context = User.get_preferences(True, config.context)
Create customer::
>>> Party = Model.get('party.party')
>>> customer = Party(name='Customer')
>>> customer.save()
Create product::
>>> ProductUom = Model.get('product.uom')
>>> ProductTemplate = Model.get('product.template')
>>> Product = Model.get('product.product')
>>> unit, = ProductUom.find([('name', '=', 'Unit')])
>>> product = Product()
>>> template = ProductTemplate()
>>> template.name = 'Product'
>>> template.default_uom = unit
>>> template.type = 'goods'
>>> template.list_price = Decimal('20')
>>> template.cost_price = Decimal('8')
>>> template.save()
>>> product.template = template
>>> product.save()
Get stock locations::
>>> Location = Model.get('stock.location')
>>> warehouse_loc, = Location.find([('code', '=', 'WH')])
>>> supplier_loc, = Location.find([('code', '=', 'SUP')])
>>> customer_loc, = Location.find([('code', '=', 'CUS')])
>>> output_loc, = Location.find([('code', '=', 'OUT')])
>>> input_loc, = Location.find([('code', '=', 'IN')])
>>> storage_loc, = Location.find([('code', '=', 'STO')])
Check default for warehouse field while allow new category to a location::
>>> Storage = Model.get('stock.location')
>>> storage = Storage.find([('code','=','STO')])[0]
>>> cat_loc = storage.categories.new()
>>> cat_loc.warehouse == storage.parent
>>> cat_loc = storage_loc.categories.new()
>>> cat_loc.warehouse == storage_loc.parent
True
>>> Category = Model.get('product.category')
>>> cat = Category(name='test cat')
>>> cat.save()
>>> cat_loc.category = cat
>>> storage.save()
>>> storage_loc.save()
Check sequence compute in locations form::
>>> storage.reload()
>>> storage.categories[0].sequence
>>> storage_loc.reload()
>>> storage_loc.categories[0].sequence
1
>>> cat_loc = storage.categories.new()
>>> cat_loc = storage_loc.categories.new()
>>> cat = Category(name='test cat2')
>>> cat.save()
>>> cat_loc.category = cat
>>> storage.save()
>>> storage.reload()
>>> storage.categories[0].sequence
>>> storage_loc.save()
>>> storage_loc.reload()
>>> storage_loc.categories[0].sequence
1
>>> storage.categories[1].sequence
>>> storage_loc.categories[1].sequence
1
>>> test_storage = Storage(name='Test storage', type='storage', parent=storage)
>>> test_storage = Location(name='Test storage',
... type='storage', parent=storage_loc)
>>> test_storage.save()
>>> test_storage.reload()
>>> cat_loc = test_storage.categories.new()
@ -55,3 +104,67 @@ Check sequence compute in locations form::
>>> test_storage.reload()
>>> test_storage.categories[0].sequence
2
>>> test_storage.categories[0].warehouse == warehouse_loc
True
Check default location in Shipment out::
>>> template.categories.append(cat)
>>> template.save()
>>> ShipmentOut = Model.get('stock.shipment.out')
>>> shipment_out = ShipmentOut()
>>> shipment_out.planned_date = today
>>> shipment_out.customer = customer
>>> shipment_out.warehouse = warehouse_loc
>>> shipment_out.company = company
>>> StockMove = Model.get('stock.move')
>>> shipment_out.outgoing_moves.append(StockMove())
>>> move = shipment_out.outgoing_moves[0]
>>> move.product = product
>>> move.uom =unit
>>> move.quantity = 1
>>> move.from_location = output_loc
>>> move.to_location = customer_loc
>>> move.company = company
>>> move.unit_price = Decimal('1')
>>> move.currency = company.currency
>>> shipment_out.save()
>>> shipment_out.click('wait')
>>> len(shipment_out.inventory_moves)
1
>>> inv_move = shipment_out.inventory_moves[0]
>>> inv_move.from_location == storage_loc
True
>>> shipment_out.click('draft')
>>> inv_move.delete()
>>> storage_loc.categories[1].sequence = 3
>>> storage_loc.save()
>>> shipment_out.click('wait')
>>> inv_move = shipment_out.inventory_moves[0]
>>> inv_move.from_location == test_storage
True
Check default location in Shipment in::
>>> ShipmentIn = Model.get('stock.shipment.in')
>>> shipment_in = ShipmentIn()
>>> shipment_in.planned_date = today
>>> shipment_in.supplier = customer
>>> shipment_in.warehouse = warehouse_loc
>>> shipment_in.company = company
>>> StockMove = Model.get('stock.move')
>>> move = shipment_in.incoming_moves.new()
>>> move.product = product
>>> move.uom =unit
>>> move.quantity = 1
>>> move.from_location = supplier_loc
>>> move.to_location = input_loc
>>> move.company = company
>>> move.unit_price = Decimal('1')
>>> move.currency = company.currency
>>> shipment_in.save()
>>> shipment_in.click('receive')
>>> len(shipment_in.inventory_moves)
1
>>> shipment_in.inventory_moves[0].to_location == test_storage
True