From 47392d8a044cf6765ca4c9daa9cafe72186f4f77 Mon Sep 17 00:00:00 2001 From: Sergi Almacellas Abellana Date: Mon, 22 Jul 2024 12:24:34 +0200 Subject: [PATCH] Compute lot trace document using sql expression --- modules/production/stock.py | 29 ++++++++----- modules/stock_lot/stock.py | 84 +++++++++++++++++++++---------------- 2 files changed, 66 insertions(+), 47 deletions(-) diff --git a/modules/production/stock.py b/modules/production/stock.py index 424b25b0b4..4dffb39887 100644 --- a/modules/production/stock.py +++ b/modules/production/stock.py @@ -1,6 +1,8 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. -from sql import Null +from sql import Cast, Null +from sql.conditionals import Case +from sql.operators import Concat from trytond.model import Check, fields from trytond.pool import Pool, PoolMeta @@ -151,8 +153,9 @@ class LotTrace(metaclass=PoolMeta): production_output = fields.Many2One('production', "Production Output") @classmethod - def _columns(cls, move): - return super()._columns(move) + [ + def _columns(cls, tables): + move = tables['move'] + return super()._columns(tables) + [ move.production_input.as_('production_input'), move.production_output.as_('production_output'), ] @@ -164,13 +167,19 @@ class LotTrace(metaclass=PoolMeta): return super().get_documents() + [ ('production', Model.get_name('production'))] - def get_document(self, name): - document = super().get_document(name) - if self.production_input: - document = str(self.production_input) - elif self.production_output: - document = str(self.production_output) - return document + @classmethod + def get_document(cls, tables): + document = super().get_document(tables) + sql_type = cls.document.sql_type().base + move = tables['move'] + return Case( + ((move.production_input != Null), + Concat('production,', + Cast(move.production_input, sql_type))), + ((move.production_output != Null), + Concat('production,', + Cast(move.production_output, sql_type))), + else_=document) def _get_upward_traces(self): traces = super()._get_upward_traces() diff --git a/modules/stock_lot/stock.py b/modules/stock_lot/stock.py index 635be80c99..559c9b26e3 100644 --- a/modules/stock_lot/stock.py +++ b/modules/stock_lot/stock.py @@ -5,8 +5,10 @@ from collections import defaultdict from copy import copy from functools import wraps -from sql import Column, Null +from sql import Cast, Column, Null +from sql.conditionals import Case from sql.functions import CharLength +from sql.operators import Concat from trytond.i18n import gettext from trytond.model import ( @@ -195,10 +197,7 @@ class LotTrace(ModelSQL, ModelView): company = fields.Many2One('company.company', "Company") date = fields.Date("Date") - shipment = fields.Reference("Shipment", 'get_shipments') - origin = fields.Reference("Origin", 'get_origins') - document = fields.Function( - fields.Reference("Document", 'get_documents'), 'get_document') + document = fields.Reference("Document", 'get_documents') upward_traces = fields.Function( fields.Many2Many( @@ -216,16 +215,38 @@ class LotTrace(ModelSQL, ModelView): @classmethod def table_query(cls): - pool = Pool() - Move = pool.get('stock.move') - move = Move.__table__() - return ( - move.select( - *cls._columns(move), - where=(move.lot != Null) & (move.state == 'done'))) + from_item, tables = cls._joins() + query = from_item.select( + *cls._columns(tables), + where=cls._where(tables)) + return query @classmethod - def _columns(cls, move): + def _joins(cls): + pool = Pool() + Move = pool.get('stock.move') + InventoryLine = pool.get('stock.inventory.line') + + move = Move.__table__() + inventory_line = InventoryLine.__table__() + tables = {} + tables['move'] = move + tables['inventory_line'] = inventory_line + + inventory_line_id = Move.origin.sql_id(move.origin, Move) + from_item = (move.join(inventory_line, type_='LEFT', + condition=(move.origin.like(InventoryLine.__name__ + '%') + & (inventory_line.id == inventory_line_id)))) + return from_item, tables + + @classmethod + def _where(cls, tables): + move = tables['move'] + return (move.lot != Null) & (move.state == 'done') + + @classmethod + def _columns(cls, tables): + move = tables['move'] return [ move.id.as_('id'), move.create_uid.as_('create_uid'), @@ -240,41 +261,30 @@ class LotTrace(ModelSQL, ModelView): move.unit.as_('unit'), move.company.as_('company'), move.effective_date.as_('date'), - move.shipment.as_('shipment'), - move.origin.as_('origin'), + cls.get_document(tables).as_('document'), ] def get_rec_name(self, name): return self.document.rec_name if self.document else str(self.id) - @classmethod - def get_shipments(cls): - pool = Pool() - Move = pool.get('stock.move') - return Move.get_shipment() - - @classmethod - def get_origins(cls): - pool = Pool() - Move = pool.get('stock.move') - return Move.get_origin() - @classmethod def get_documents(cls): pool = Pool() Model = pool.get('ir.model') - return cls.get_shipments() + [ + Move = pool.get('stock.move') + return Move.get_origin() + [ ('stock.inventory', Model.get_name('stock.inventory'))] - def get_document(self, name): - pool = Pool() - InventoryLine = pool.get('stock.inventory.line') - document = None - if self.shipment: - document = str(self.shipment) - elif isinstance(self.origin, InventoryLine): - document = str(self.origin.inventory) - return str(document) if document else None + @classmethod + def get_document(cls, tables): + move = tables['move'] + inventory_line = tables['inventory_line'] + sql_type = cls.document.sql_type().base + return Case( + ((inventory_line.id != Null), + Concat('stock.inventory,', + Cast(inventory_line.inventory, sql_type))), + else_=move.shipment) @classmethod def _is_trace_move(cls, move):