Add compatibility with stock_lot
From issue9800
This commit is contained in:
parent
4958f3cb42
commit
2b7f6ff15e
|
@ -7,11 +7,11 @@ from . import stock
|
|||
|
||||
def register():
|
||||
Pool.register(
|
||||
product.Configuration,
|
||||
product.ConfigurationDefaultLotSequence,
|
||||
product.CategoryCompany,
|
||||
product.Category,
|
||||
product.TemplateCompany,
|
||||
product.Template,
|
||||
stock.CompanyConfiguration,
|
||||
stock.Configuration,
|
||||
stock.Lot,
|
||||
module='stock_lot_sequence', type_='model')
|
||||
|
|
47
product.py
47
product.py
|
@ -1,24 +1,49 @@
|
|||
# The COPYRIGHT file at the top level of this repository contains the full
|
||||
# copyright notices and license terms.
|
||||
from trytond.model import ModelSQL, fields
|
||||
from trytond.pool import PoolMeta
|
||||
from trytond.pool import PoolMeta, Pool
|
||||
from trytond.pyson import Eval, Id
|
||||
from trytond import backend
|
||||
from trytond.modules.company.model import (
|
||||
CompanyMultiValueMixin, CompanyValueMixin)
|
||||
|
||||
def default_func(field_name):
|
||||
@classmethod
|
||||
def default(cls, **pattern):
|
||||
return getattr(
|
||||
cls.multivalue_model(field_name),
|
||||
'default_%s' % field_name, lambda: None)()
|
||||
return default
|
||||
|
||||
|
||||
class Configuration(metaclass=PoolMeta):
|
||||
__name__ = 'product.configuration'
|
||||
default_default_lot_sequence = default_func('default_lot_sequence')
|
||||
|
||||
|
||||
class ConfigurationDefaultLotSequence(metaclass=PoolMeta):
|
||||
__name__ = 'product.configuration.default_lot_sequence'
|
||||
|
||||
@classmethod
|
||||
def default_default_lot_sequence(cls):
|
||||
pool = Pool()
|
||||
ModelData = pool.get('ir.model.data')
|
||||
try:
|
||||
return ModelData.get_id('stock_lot_sequence', 'sequence_lot')
|
||||
except KeyError:
|
||||
return None
|
||||
|
||||
|
||||
class CategoryCompany(ModelSQL, CompanyValueMixin):
|
||||
'Category per Company'
|
||||
__name__ = 'product.category.lot_sequence'
|
||||
|
||||
category = fields.Many2One('product.category', 'Category', required=True,
|
||||
ondelete='CASCADE', select=True)
|
||||
lot_sequence = fields.Many2One(
|
||||
'ir.sequence', 'Lot Sequence',
|
||||
domain=[
|
||||
('sequence_type', '=', Id('stock_lot_sequence',
|
||||
'sequence_type_lot')),
|
||||
('sequence_type', '=', Id('stock_lot',
|
||||
'sequence_type_stock_lot')),
|
||||
('company', 'in', [Eval('company', -1), None]),
|
||||
],
|
||||
depends=['company'])
|
||||
|
@ -34,11 +59,10 @@ class CategoryCompany(ModelSQL, CompanyValueMixin):
|
|||
|
||||
class Category(CompanyMultiValueMixin, metaclass=PoolMeta):
|
||||
__name__ = 'product.category'
|
||||
|
||||
lot_sequence = fields.MultiValue(fields.Many2One('ir.sequence',
|
||||
'Lot Sequence', domain=[
|
||||
('sequence_type', '=', Id('stock_lot_sequence',
|
||||
'sequence_type_lot')),
|
||||
('sequence_type', '=', Id('stock_lot',
|
||||
'sequence_type_stock_lot')),
|
||||
('company', 'in',
|
||||
[Eval('context', {}).get('company', -1), None]),
|
||||
],
|
||||
|
@ -53,14 +77,13 @@ class Category(CompanyMultiValueMixin, metaclass=PoolMeta):
|
|||
class TemplateCompany(ModelSQL, CompanyValueMixin):
|
||||
'Template per Company'
|
||||
__name__ = 'product.template.lot_sequence'
|
||||
|
||||
template = fields.Many2One('product.template', 'Template', required=True,
|
||||
ondelete='CASCADE', select=True)
|
||||
lot_sequence = fields.Many2One(
|
||||
'ir.sequence', 'Lot Sequence',
|
||||
domain=[
|
||||
('sequence_type', '=', Id('stock_lot_sequence',
|
||||
'sequence_type_lot')),
|
||||
('sequence_type', '=', Id('stock_lot',
|
||||
'sequence_type_stock_lot')),
|
||||
('company', 'in', [Eval('company', -1), None]),
|
||||
],
|
||||
select=True,
|
||||
|
@ -81,8 +104,8 @@ class Template(metaclass=PoolMeta):
|
|||
|
||||
lot_sequence = fields.MultiValue(fields.Many2One('ir.sequence',
|
||||
'Lot Sequence', domain=[
|
||||
('sequence_type', '=', Id('stock_lot_sequence',
|
||||
'sequence_type_lot')),
|
||||
('sequence_type', '=', Id('stock_lot',
|
||||
'sequence_type_stock_lot')),
|
||||
('company', 'in',
|
||||
[Eval('context', {}).get('company', -1), None]),
|
||||
],
|
||||
|
|
58
stock.py
58
stock.py
|
@ -1,62 +1,10 @@
|
|||
# The COPYRIGHT file at the top level of this repository contains the full
|
||||
# copyright notices and license terms.
|
||||
from trytond.model import ModelSQL, fields
|
||||
from trytond.pyson import Eval, Id
|
||||
from trytond.pool import Pool, PoolMeta
|
||||
from trytond.modules.company.model import CompanyValueMixin
|
||||
from trytond.i18n import gettext
|
||||
from trytond.exceptions import UserError
|
||||
|
||||
|
||||
def default_func(field_name):
|
||||
@classmethod
|
||||
def default(cls, **pattern):
|
||||
return getattr(
|
||||
cls.multivalue_model(field_name),
|
||||
'default_%s' % field_name, lambda: None)()
|
||||
return default
|
||||
|
||||
|
||||
class Configuration(metaclass=PoolMeta):
|
||||
__name__ = 'stock.configuration'
|
||||
lot_sequence = fields.MultiValue(fields.Many2One('ir.sequence',
|
||||
'Lot Sequence', domain=[
|
||||
('sequence_type', '=', Id('stock_lot_sequence',
|
||||
'sequence_type_lot')),
|
||||
('company', 'in',
|
||||
[Eval('context', {}).get('company', -1), None]),
|
||||
], required=True))
|
||||
|
||||
@classmethod
|
||||
def multivalue_model(cls, field):
|
||||
pool = Pool()
|
||||
if field == 'lot_sequence':
|
||||
return pool.get('stock.configuration.company')
|
||||
return super(Configuration, cls).multivalue_model(field)
|
||||
|
||||
default_lot_sequence = default_func('lot_sequence')
|
||||
|
||||
|
||||
class CompanyConfiguration(ModelSQL, CompanyValueMixin):
|
||||
'Stock Company Configuration'
|
||||
__name__ = 'stock.configuration.company'
|
||||
lot_sequence = fields.Many2One('ir.sequence', 'Lot Sequence', domain=[
|
||||
('sequence_type', '=', Id('stock_lot_sequence',
|
||||
'sequence_type_lot')),
|
||||
('company', 'in',
|
||||
[Eval('context', {}).get('company', -1), None]),
|
||||
])
|
||||
|
||||
@classmethod
|
||||
def default_lot_sequence(cls):
|
||||
pool = Pool()
|
||||
ModelData = pool.get('ir.model.data')
|
||||
try:
|
||||
return ModelData.get_id('stock_lot_sequence', 'sequence_lot')
|
||||
except KeyError:
|
||||
return None
|
||||
|
||||
|
||||
class Lot(metaclass=PoolMeta):
|
||||
__name__ = 'stock.lot'
|
||||
|
||||
|
@ -89,7 +37,7 @@ class Lot(metaclass=PoolMeta):
|
|||
@classmethod
|
||||
def calc_number(cls, product, lot_values=None):
|
||||
pool = Pool()
|
||||
Config = pool.get('stock.configuration')
|
||||
Config = pool.get('product.configuration')
|
||||
|
||||
config = Config(1)
|
||||
if product.template.lot_sequence:
|
||||
|
@ -100,7 +48,7 @@ class Lot(metaclass=PoolMeta):
|
|||
sequence = category.lot_sequence
|
||||
break
|
||||
else:
|
||||
if not config.lot_sequence:
|
||||
if not config.default_lot_sequence:
|
||||
raise UserError(gettext('stock_lot_sequence.no_sequence'))
|
||||
sequence = config.lot_sequence
|
||||
sequence = config.default_lot_sequence
|
||||
return sequence.get()
|
||||
|
|
22
stock.xml
22
stock.xml
|
@ -3,29 +3,9 @@
|
|||
copyright notices and license terms. -->
|
||||
<tryton>
|
||||
<data>
|
||||
<record id="view_stock_configuration_view_form" model="ir.ui.view">
|
||||
<field name="name">configuration_form</field>
|
||||
<field name="model">stock.configuration</field>
|
||||
<field name="inherit" ref="stock.stock_configuration_view_form"/>
|
||||
</record>
|
||||
|
||||
<record model="ir.sequence.type" id="sequence_type_lot">
|
||||
<field name="name">Lot</field>
|
||||
</record>
|
||||
<record model="ir.sequence.type-res.group"
|
||||
id="sequence_type_lot_group_admin">
|
||||
<field name="sequence_type" ref="sequence_type_lot"/>
|
||||
<field name="group" ref="res.group_admin"/>
|
||||
</record>
|
||||
<record model="ir.sequence.type-res.group"
|
||||
id="sequence_type_lot_group_stock_admin">
|
||||
<field name="sequence_type" ref="sequence_type_lot"/>
|
||||
<field name="group" ref="stock.group_stock_admin"/>
|
||||
</record>
|
||||
|
||||
<record model="ir.sequence" id="sequence_lot">
|
||||
<field name="name">Lot</field>
|
||||
<field name="sequence_type" ref="sequence_type_lot"/>
|
||||
<field name="sequence_type" ref="stock_lot.sequence_type_stock_lot"/>
|
||||
</record>
|
||||
</data>
|
||||
</tryton>
|
||||
|
|
|
@ -31,6 +31,7 @@ class TestCase(ModuleTestCase):
|
|||
with set_company(company):
|
||||
category = Category(name='Category')
|
||||
category.save()
|
||||
self.assertEqual(category.lot_sequence, None)
|
||||
|
||||
unit, = Uom.search([
|
||||
('name', '=', 'Unit'),
|
||||
|
@ -42,20 +43,22 @@ class TestCase(ModuleTestCase):
|
|||
cost_price=Decimal(3),
|
||||
default_uom=unit,
|
||||
categories=[category],
|
||||
lot_sequence=None,
|
||||
)
|
||||
template.save()
|
||||
product = Product(template=template)
|
||||
product.save()
|
||||
self.assertEqual(template.lot_sequence, None)
|
||||
|
||||
lot, = Lot.create([{'product': product.id}])
|
||||
|
||||
sequence_type = SequenceType(ModelData.get_id('stock_lot_sequence',
|
||||
'sequence_type_lot'))
|
||||
sequence_type = SequenceType(ModelData.get_id('stock_lot',
|
||||
'sequence_type_stock_lot'))
|
||||
cat_sequence = Sequence(sequence_type=sequence_type,
|
||||
name='Category Sequence')
|
||||
cat_sequence.save()
|
||||
sequence_type = SequenceType(ModelData.get_id('stock_lot_sequence',
|
||||
'sequence_type_lot'))
|
||||
sequence_type = SequenceType(ModelData.get_id('stock_lot',
|
||||
'sequence_type_stock_lot'))
|
||||
tem_sequence = Sequence(sequence_type=sequence_type,
|
||||
name='Template Sequence')
|
||||
tem_sequence.save()
|
||||
|
@ -75,6 +78,7 @@ class TestCase(ModuleTestCase):
|
|||
lot.save()
|
||||
self.assertEqual(lot.number, 'M1')
|
||||
|
||||
# category + not category sequence
|
||||
lots = Lot.create([
|
||||
{'product': product.id},
|
||||
{'product': product.id},
|
||||
|
@ -85,6 +89,7 @@ class TestCase(ModuleTestCase):
|
|||
self.assertEqual([l.number for l in lots], [str(x) for x
|
||||
in range(7, 12)])
|
||||
|
||||
# category + category sequence
|
||||
category.lot_sequence = cat_sequence
|
||||
category.save()
|
||||
# It should use the category sequence
|
||||
|
@ -98,6 +103,7 @@ class TestCase(ModuleTestCase):
|
|||
self.assertEqual([l.number for l in lots], [str(x) for x
|
||||
in range(1, 6)])
|
||||
|
||||
# not category sequence + template sequence
|
||||
template.lot_sequence = tem_sequence
|
||||
template.save()
|
||||
# It should use the template sequence
|
||||
|
|
Loading…
Reference in a new issue