Implement packcode trace.

This commit refs #3285
This commit is contained in:
Sergio Morillo 2017-10-31 08:55:28 +01:00
parent f20200f321
commit 1aab1caf45
7 changed files with 196 additions and 6 deletions

View file

@ -3,10 +3,15 @@
from trytond.pool import Pool
from .printer import Printer
from .stock import Location
from .unit_load import UnitLoadTrace, GetPackCode
def register():
Pool.register(
Printer,
Location,
GetPackCode,
module='stock_label_printer', type_='model')
Pool.register(
UnitLoadTrace,
module='stock_label_printer', type_='wizard')

View file

@ -12,4 +12,44 @@ msgstr "Ubicación"
msgctxt "error:stock.location:"
msgid "Invalid trace code \"%s\"."
msgstr "Código de trazabilidad \"%s\" inválido."
msgstr "Código de trazabilidad \"%s\" inválido."
msgctxt "field:stock.unit_load.trace.get_pack_code,code:"
msgid "Pack code"
msgstr "Código unidad venta"
msgctxt "field:stock.unit_load.trace.get_pack_code,id:"
msgid "ID"
msgstr "Identificador"
msgctxt "field:stock.unit_load.trace.get_pack_code,year:"
msgid "Year"
msgstr "Año"
msgctxt "model:ir.ui.menu,name:menu_trace"
msgid "Unit load trace"
msgstr "Trazabilidad de UdC"
msgctxt "model:stock.unit_load.trace.get_pack_code,name:"
msgid "Unit load trace get pack code"
msgstr "Trazabilidad de UdC obtener código de unidad de venta"
msgctxt "wizard_button:stock.unit_load.trace,get_pack_code,end:"
msgid "Cancel"
msgstr "Cancelar"
msgctxt "wizard_button:stock.unit_load.trace,get_pack_code,backward_:"
msgid "Trace"
msgstr "Trazar"
msgctxt "wizard_button:stock.unit_load.trace,get_pack_code,print_:"
msgid "Print"
msgstr "Imprimir"
msgctxt "view:stock.unit_load.trace.get_pack_code:"
msgid "Unit Load Trace"
msgstr "Trazabilidad"
msgctxt "report:stock.unit_load.trace_report:"
msgid "Pack code:"
msgstr "Código Unidad de venta:"

View file

@ -15,12 +15,14 @@ class Location:
__metaclass__ = PoolMeta
printers = fields.One2Many('label.printer', 'location', 'Printers',
states={'invisible': ~Eval('type').in_(['storage', 'production'])},
depends=['type'])
states={'invisible': ~Eval('type').in_(['storage', 'production'])},
depends=['type'])
trace_code = fields.Char('Trace code', size=2,
states={'readonly': Not(Equal(Eval('type'), 'production')),
'invisible': Not(Equal(Eval('type'), 'production'))},
depends=['type'])
states={
'readonly': Not(Equal(Eval('type'), 'production')),
'invisible': Not(Equal(Eval('type'), 'production'))
},
depends=['type'])
@classmethod
def __setup__(cls):

View file

@ -6,6 +6,10 @@ depends:
res
stock
extras_depend:
stock_unit_load_trace
xml:
printer.xml
stock.xml
unit_load.xml

114
unit_load.py Normal file
View file

@ -0,0 +1,114 @@
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
import dateutil
from trytond.model import ModelView, fields
from trytond.pool import Pool, PoolMeta
from trytond.pyson import PYSONEncoder
from trytond.wizard import StateView, Button, StateReport
from trytond.transaction import Transaction
import datetime
__all__ = ['UnitLoadTrace', 'GetPackCode']
class GetPackCode(ModelView):
"""Unit load trace get pack code"""
__name__ = 'stock.unit_load.trace.get_pack_code'
code = fields.Char('Pack code', size=6, required=True)
year = fields.Char('Year', size=4, required=True)
@staticmethod
def default_year(name=None):
return str(datetime.datetime.now().year)
class UnitLoadTrace:
__name__ = 'stock.unit_load.trace'
__metaclass__ = PoolMeta
get_pack_code = StateView('stock.unit_load.trace.get_pack_code',
'stock_label_printer.unit_load_trace_get_pack_code_view_form',
[Button('Cancel', 'end', 'tryton-cancel'),
Button('Print', 'print_', 'tryton-print'),
Button('Trace', 'backward_', 'tryton-find', default=True)],)
print_ = StateReport('stock.unit_load.trace_report')
def transition_start(self):
active_model = Transaction().context['active_model']
if active_model == 'ir.ui.menu':
return 'get_pack_code'
return super(UnitLoadTrace, self).transition_start()
def decode_packcode(self):
return self._decode_packcode(self.get_pack_code.code.upper(),
self.get_pack_code.year)
@classmethod
def _decode_packcode(cls, packcode, year):
pool = Pool()
Location = pool.get('stock.location')
return Location._decode_trace(packcode.upper(), year)
def do_backward_(self, action):
pool = Pool()
Location = pool.get('stock.location')
Company = pool.get('company.company')
if Transaction().context['active_model'] != 'stock.unit_load':
production_locations, end_date = self.decode_packcode()
if not production_locations or not end_date:
return action, {}
company = Company(Transaction().context['company'])
lzone = (dateutil.tz.gettz(company.timezone) if company.timezone
else dateutil.tz.tzutc())
szone = dateutil.tz.tzutc()
_date = end_date.replace(tzinfo=szone).astimezone(lzone).replace(
tzinfo=None)
action['name'] += ' - %s (%s) %s @ %s' % (
self.get_pack_code.code.upper(),
self.get_pack_code.year,
Location(production_locations[0]).rec_name
if production_locations else None,
_date)
traced_unit_loads_ids = self._get_ul_backward_trace(None,
packcode=self.get_pack_code.code, year=self.get_pack_code.year)
action['pyson_domain'] = PYSONEncoder().encode(
[('id', 'in', list(set(traced_unit_loads_ids)) or [0])])
return action, {}
return super(UnitLoadTrace, self).do_backward_(action)
@classmethod
def _get_ul_backward_trace_domain(cls, unit_load, **kwargs):
if not unit_load:
assert kwargs.get('packcode') and kwargs.get('year')
packcode = kwargs.get('packcode')
year = kwargs.get('year')
production_locations, end_date = cls._decode_packcode(
packcode, year)
if not production_locations or not end_date:
return []
start_date = end_date
return [
('start_date', '<=', end_date),
('end_date', '>=', start_date),
('to_location', 'in', production_locations),
('state', '=', 'done')]
return super(UnitLoadTrace, cls)._get_ul_backward_trace_domain(
unit_load, **kwargs)
def do_print_(self, action):
traced_unit_loads_ids = set(self._get_ul_backward_trace(None,
packcode=self.get_pack_code.code, year=self.get_pack_code.year))
action['pyson_domain'] = PYSONEncoder().encode([
('id', 'in', list(traced_unit_loads_ids))])
return action, {
'ids': list(traced_unit_loads_ids),
'packcode': self.get_pack_code.code,
'packcode_year': self.get_pack_code.year
}

16
unit_load.xml Normal file
View file

@ -0,0 +1,16 @@
<?xml version="1.0"?>
<!-- The COPYRIGHT file at the top level of this repository contains the full
copyright notices and license terms. -->
<tryton>
<data depends="stock_unit_load_trace">
<!-- UL trace menu -->
<record model="ir.ui.view" id="unit_load_trace_get_pack_code_view_form">
<field name="model">stock.unit_load.trace.get_pack_code</field>
<field name="type">form</field>
<field name="name">unit_load_trace_get_pack_code_form</field>
</record>
<menuitem action="stock_unit_load_trace.wizard_trace" id="menu_trace"
parent="stock_unit_load.menu_unit_load" sequence="40"
name="Unit load trace"/>
</data>
</tryton>

View file

@ -0,0 +1,9 @@
<?xml version="1.0"?>
<!-- The COPYRIGHT file at the top level of this repository contains the full
copyright notices and license terms. -->
<form>
<label name="code"/>
<field name="code"/>
<label name="year"/>
<field name="year"/>
</form>