Files
tryton/modules/stock_lot/tests/test_module.py
T

313 lines
12 KiB
Python
Raw Normal View History

2014-12-02 12:56:10 +01:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import datetime as dt
2013-06-21 23:25:43 +02:00
from decimal import Decimal
2012-05-13 23:12:11 +02:00
from trytond.modules.company.tests import (
2021-12-11 17:58:44 +01:00
CompanyTestMixin, create_company, set_company)
from trytond.pool import Pool
from trytond.tests.test_tryton import ModuleTestCase, with_transaction
2021-12-11 17:58:44 +01:00
from trytond.transaction import Transaction
2012-05-13 23:12:11 +02:00
class StockLotTestCase(CompanyTestMixin, ModuleTestCase):
2014-01-12 20:00:16 +01:00
'Test Stock Lot module'
2015-01-27 18:14:13 +01:00
module = 'stock_lot'
2012-05-13 23:12:11 +02:00
2016-02-06 19:13:21 +01:00
@with_transaction()
def test_products_by_location(self):
2014-01-12 20:00:16 +01:00
'Test products_by_location'
2016-02-06 19:13:21 +01:00
pool = Pool()
Uom = pool.get('product.uom')
Template = pool.get('product.template')
Product = pool.get('product.product')
Location = pool.get('stock.location')
Move = pool.get('stock.move')
Lot = pool.get('stock.lot')
2013-06-21 23:25:43 +02:00
2016-02-06 19:13:21 +01:00
kg, = Uom.search([('name', '=', 'Kilogram')])
g, = Uom.search([('name', '=', 'Gram')])
template, = Template.create([{
'name': 'Test products_by_location',
'type': 'goods',
'default_uom': kg.id,
}])
product, = Product.create([{
'template': template.id,
}])
supplier, = Location.search([('code', '=', 'SUP')])
customer, = Location.search([('code', '=', 'CUS')])
storage, = Location.search([('code', '=', 'STO')])
company = create_company()
currency = company.currency
with set_company(company):
lot1, lot2 = Lot.create([{
2013-06-21 23:25:43 +02:00
'number': '1',
'product': product.id,
}, {
'number': '2',
'product': product.id,
}])
2016-02-06 19:13:21 +01:00
moves = Move.create([{
2013-06-21 23:25:43 +02:00
'product': product.id,
'lot': lot1.id,
2023-08-22 13:18:33 +02:00
'unit': kg.id,
2013-06-21 23:25:43 +02:00
'quantity': 5,
'from_location': supplier.id,
'to_location': storage.id,
'company': company.id,
'unit_price': Decimal('1'),
'currency': currency.id,
}, {
'product': product.id,
'lot': lot2.id,
2023-08-22 13:18:33 +02:00
'unit': kg.id,
2013-06-21 23:25:43 +02:00
'quantity': 10,
'from_location': supplier.id,
'to_location': storage.id,
'company': company.id,
'unit_price': Decimal('1'),
'currency': currency.id,
}, {
'product': product.id,
'lot': lot2.id,
2023-08-22 13:18:33 +02:00
'unit': kg.id,
2013-06-21 23:25:43 +02:00
'quantity': 2,
'from_location': storage.id,
'to_location': customer.id,
'company': company.id,
'unit_price': Decimal('1'),
'currency': currency.id,
}, {
'product': product.id,
'lot': None,
2023-08-22 13:18:33 +02:00
'unit': kg.id,
2013-06-21 23:25:43 +02:00
'quantity': 3,
'from_location': supplier.id,
'to_location': storage.id,
'company': company.id,
'unit_price': Decimal('1'),
'currency': currency.id,
}])
2016-02-06 19:13:21 +01:00
Move.do(moves)
2013-06-21 23:25:43 +02:00
2016-02-06 19:13:21 +01:00
self.assertEqual(Product.products_by_location([storage.id],
2018-02-28 18:26:17 +01:00
grouping_filter=([product.id],)), {
2013-06-21 23:25:43 +02:00
(storage.id, product.id): 16,
})
2016-02-06 19:13:21 +01:00
self.assertEqual(Product.products_by_location([storage.id],
2018-02-28 18:26:17 +01:00
grouping=('product', 'lot',),
grouping_filter=([product.id],)), {
2013-06-21 23:25:43 +02:00
(storage.id, product.id, lot1.id): 5,
(storage.id, product.id, lot2.id): 8,
(storage.id, product.id, None): 3,
})
with Transaction().set_context(locations=[storage.id]):
lot1, lot2 = Lot.browse([lot1, lot2])
self.assertEqual(lot1.quantity, 5)
self.assertEqual(lot2.quantity, 8)
2013-06-21 23:25:43 +02:00
2016-02-06 19:13:21 +01:00
@with_transaction()
def test_period(self):
2014-01-12 20:00:16 +01:00
'Test period'
2016-02-06 19:13:21 +01:00
pool = Pool()
Uom = pool.get('product.uom')
Template = pool.get('product.template')
Product = pool.get('product.product')
Location = pool.get('stock.location')
Move = pool.get('stock.move')
Lot = pool.get('stock.lot')
Period = pool.get('stock.period')
2013-06-21 23:25:43 +02:00
2016-02-06 19:13:21 +01:00
unit, = Uom.search([('name', '=', 'Unit')])
template, = Template.create([{
'name': 'Test period',
'type': 'goods',
'default_uom': unit.id,
}])
product, = Product.create([{
'template': template.id,
}])
supplier, = Location.search([('code', '=', 'SUP')])
storage, = Location.search([('code', '=', 'STO')])
company = create_company()
currency = company.currency
with set_company(company):
lot1, lot2 = Lot.create([{
2013-06-21 23:25:43 +02:00
'number': '1',
'product': product.id,
}, {
'number': '2',
'product': product.id,
}])
today = dt.date.today()
2013-06-21 23:25:43 +02:00
2016-02-06 19:13:21 +01:00
moves = Move.create([{
2013-06-21 23:25:43 +02:00
'product': product.id,
'lot': lot1.id,
2023-08-22 13:18:33 +02:00
'unit': unit.id,
2013-06-21 23:25:43 +02:00
'quantity': 5,
'from_location': supplier.id,
'to_location': storage.id,
'planned_date': today - dt.timedelta(days=1),
'effective_date': today - dt.timedelta(days=1),
2013-06-21 23:25:43 +02:00
'company': company.id,
'unit_price': Decimal('1'),
'currency': currency.id,
}, {
'product': product.id,
'lot': lot2.id,
2023-08-22 13:18:33 +02:00
'unit': unit.id,
2013-06-21 23:25:43 +02:00
'quantity': 10,
'from_location': supplier.id,
'to_location': storage.id,
'planned_date': today - dt.timedelta(days=1),
'effective_date': today - dt.timedelta(days=1),
2013-06-21 23:25:43 +02:00
'company': company.id,
'unit_price': Decimal('1'),
'currency': currency.id,
}, {
'product': product.id,
'lot': None,
2023-08-22 13:18:33 +02:00
'unit': unit.id,
2013-06-21 23:25:43 +02:00
'quantity': 3,
'from_location': supplier.id,
'to_location': storage.id,
'planned_date': today - dt.timedelta(days=1),
'effective_date': today - dt.timedelta(days=1),
2013-06-21 23:25:43 +02:00
'company': company.id,
'unit_price': Decimal('1'),
'currency': currency.id,
}])
2016-02-06 19:13:21 +01:00
Move.do(moves)
2013-06-21 23:25:43 +02:00
2016-02-06 19:13:21 +01:00
period, = Period.create([{
'date': today - dt.timedelta(days=1),
2013-06-21 23:25:43 +02:00
'company': company.id,
}])
2016-02-06 19:13:21 +01:00
Period.close([period])
2013-06-21 23:25:43 +02:00
self.assertEqual(period.state, 'closed')
quantities = {
supplier: -18,
storage: 18,
}
for cache in period.caches:
self.assertEqual(cache.product, product)
self.assertEqual(cache.internal_quantity,
quantities[cache.location])
quantities = {
(supplier, lot1): -5,
(storage, lot1): 5,
(supplier, lot2): -10,
(storage, lot2): 10,
(supplier, None): -3,
(storage, None): 3,
}
for lot_cache in period.lot_caches:
self.assertEqual(lot_cache.product, product)
self.assertEqual(lot_cache.internal_quantity,
quantities[(lot_cache.location, lot_cache.lot)])
2024-06-09 17:29:22 +02:00
@with_transaction()
2020-08-10 23:30:14 +02:00
def test_assign_try_with_lot(self):
"Test Move assign_try with lot"
pool = Pool()
Template = pool.get('product.template')
Product = pool.get('product.product')
Uom = pool.get('product.uom')
Location = pool.get('stock.location')
Move = pool.get('stock.move')
Lot = pool.get('stock.lot')
2023-08-22 13:18:33 +02:00
unit, = Uom.search([('name', '=', 'Meter')])
2020-08-10 23:30:14 +02:00
template = Template(
name="Product",
type='goods',
2023-08-22 13:18:33 +02:00
default_uom=unit,
2020-08-10 23:30:14 +02:00
)
template.save()
product = Product(template=template.id)
product.save()
supplier, = Location.search([('code', '=', 'SUP')])
storage, = Location.search([('code', '=', 'STO')])
customer, = Location.search([('code', '=', 'CUS')])
company = create_company()
with set_company(company):
lot1, lot2 = Lot.create([{
'number': "1",
'product': product.id,
}, {
'number': "2",
'product': product.id,
}])
moves = Move.create([{
'product': product.id,
'lot': lot1.id,
2023-08-22 13:18:33 +02:00
'unit': unit.id,
2020-08-10 23:30:14 +02:00
'quantity': 2,
'from_location': supplier.id,
'to_location': storage.id,
'company': company.id,
'unit_price': Decimal(1),
'currency': company.currency.id,
}, {
'product': product.id,
'lot': lot2.id,
2023-08-22 13:18:33 +02:00
'unit': unit.id,
2020-08-10 23:30:14 +02:00
'quantity': 3,
'from_location': supplier.id,
'to_location': storage.id,
'company': company.id,
'unit_price': Decimal(1),
'currency': company.currency.id,
}, {
'product': product.id,
'lot': None,
2023-08-22 13:18:33 +02:00
'unit': unit.id,
2020-08-10 23:30:14 +02:00
'quantity': 3,
'from_location': supplier.id,
'to_location': storage.id,
'company': company.id,
'unit_price': Decimal(1),
'currency': company.currency.id,
}])
Move.do(moves)
move, = Move.create([{
'product': product.id,
2023-08-22 13:18:33 +02:00
'unit': unit.id,
2020-08-10 23:30:14 +02:00
'quantity': 10,
'from_location': storage.id,
'to_location': customer.id,
'company': company.id,
'unit_price': Decimal(1),
'currency': company.currency.id,
}])
self.assertFalse(
Move.assign_try([move], grouping=('product', 'lot')))
moves = Move.search([
('product', '=', product.id),
('from_location', '=', storage.id),
('to_location', '=', customer.id),
('company', '=', company.id),
])
self.assertEqual(len(moves), 4)
self.assertEqual({
(m.lot, m.quantity, m.state) for m in moves}, {
(lot1, 2, 'assigned'),
(lot2, 3, 'assigned'),
2024-06-09 17:29:22 +02:00
(None, 3, 'assigned'),
(None, 2, 'draft'),
2020-08-10 23:30:14 +02:00
})
2012-05-13 23:12:11 +02:00
del ModuleTestCase