trytonpsk-stock_co/position.py

73 lines
2.6 KiB
Python
Raw Normal View History

2022-03-03 14:41:01 +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.
2023-08-05 17:04:05 +02:00
from trytond.model import ModelView, ModelSQL, fields, Unique
2023-08-05 19:30:54 +02:00
from .exceptions import DeleteWarning
from trytond.pool import Pool
from trytond.i18n import gettext
2022-03-03 14:41:01 +01:00
class ProductPosition(ModelSQL, ModelView):
"Product Position"
__name__ = "product.position"
name = fields.Char('Name', required=True)
2023-08-05 17:04:05 +02:00
code = fields.Char('Code')
2022-03-03 14:41:01 +01:00
warehouse = fields.Many2One('stock.location', 'Warehouse', states={
'required': True
}, domain=[('type', '=', 'warehouse')]
)
@classmethod
def __setup__(cls):
super(ProductPosition, cls).__setup__()
cls._order.insert(0, ('name', 'ASC'))
2023-08-05 17:04:05 +02:00
t = cls.__table__()
cls._sql_constraints = [
2023-08-05 19:30:54 +02:00
('unique', Unique(t, t.code), "Code must be unique."),
2023-08-05 17:04:05 +02:00
]
2022-03-03 14:41:01 +01:00
@classmethod
def search_rec_name(cls, name, clause):
2023-08-01 16:12:26 +02:00
clause = tuple(clause)
if clause[1].startswith('!') or clause[1].startswith('not '):
bool_op = 'AND'
else:
bool_op = 'OR'
return [bool_op,
('name',) + clause[1:],
2023-08-05 17:04:05 +02:00
('code',) + clause[1:],
2023-08-01 16:12:26 +02:00
(cls._rec_name,) + clause[1:],
]
2022-03-03 14:41:01 +01:00
2023-08-05 19:30:54 +02:00
@classmethod
def delete(cls, records):
Warning = Pool().get('res.user.warning')
ProductTemplate = Pool().get('product_template.position')
for record in records:
key = '%s.delete' % record
pds_position = ProductTemplate.search([('position', '=', record.id)])
if Warning.check(key) and len(pds_position) > 0:
raise DeleteWarning(key,
gettext('stock_co.msg_delete_position',
position=record.rec_name, count=len(pds_position)))
super(cls, ProductPosition).delete(records)
2022-03-03 14:41:01 +01:00
class ProductTemplatePosition(ModelSQL, ModelView):
"Product Template Position"
__name__ = 'product_template.position'
# _rec_name = 'position'
position = fields.Many2One('product.position', 'Position', required=True,
ondelete='CASCADE')
template = fields.Many2One('product.template', 'Template', required=True,
ondelete='RESTRICT')
warehouse = fields.Function(fields.Many2One('stock.location', 'Warehouse'),
'on_change_with_warehouse')
2022-03-03 14:41:01 +01:00
@fields.depends('position')
2022-03-03 16:03:58 +01:00
def on_change_with_warehouse(self, name=None):
2022-03-03 14:41:01 +01:00
warehouse = None
if self.position:
warehouse = self.position.warehouse.id
return warehouse