Allow to extend order methods for another origins.
This commit refs #14056
This commit is contained in:
parent
32f7d3ce48
commit
b1efff52ac
106
invoice.py
106
invoice.py
|
@ -3,9 +3,10 @@
|
|||
from trytond.model import fields
|
||||
from trytond.pool import PoolMeta, Pool
|
||||
from trytond.pyson import Equal, Eval, Not, And
|
||||
from trytond.transaction import Transaction
|
||||
from sql import Null, Literal
|
||||
from sql.operators import Concat
|
||||
from sql import Cast, Literal
|
||||
from sql.functions import Substring, Position
|
||||
from sql.conditionals import Case
|
||||
|
||||
__all__ = ['InvoiceLine']
|
||||
|
||||
|
@ -21,46 +22,91 @@ class InvoiceLine(metaclass=PoolMeta):
|
|||
|
||||
@classmethod
|
||||
def order_sale_date(cls, tables):
|
||||
line, _ = tables[None]
|
||||
|
||||
# it allows to get different origins which are related in some way
|
||||
# with a sale
|
||||
fromitem, conditions = cls._get_order_sale_date_query_items(line)
|
||||
query = fromitem.select(
|
||||
line.id,
|
||||
Case(*conditions, _else=Null),
|
||||
where=(line.invoice_type == 'out'),
|
||||
order_by=Case(*conditions, _else=Null)
|
||||
)
|
||||
|
||||
cursor = Transaction().connection.cursor()
|
||||
cursor.execute(*query)
|
||||
dates = cursor.fetchall()
|
||||
|
||||
conditions = []
|
||||
for x, values in enumerate(dates):
|
||||
line_id, date = values
|
||||
conditions.append((line.id == line_id, date))
|
||||
return [Case(*conditions, _else=Null)]
|
||||
|
||||
@classmethod
|
||||
def _get_order_sale_date_query_items(cls, table):
|
||||
pool = Pool()
|
||||
Sale = pool.get('sale.sale')
|
||||
SaleLine = pool.get('sale.line')
|
||||
invoice, _ = tables[None]
|
||||
field = Sale._fields['sale_date']
|
||||
sale = Sale.__table__()
|
||||
sale_line = SaleLine.__table__()
|
||||
|
||||
if 'sale' not in tables:
|
||||
sale = Sale.__table__()
|
||||
sale_line = SaleLine.__table__()
|
||||
tables.update({
|
||||
'sale_line': {None: (sale_line, invoice.origin == Concat(
|
||||
'sale.line,', sale_line.id))},
|
||||
'sale': {None: (sale, sale_line.sale == sale.id)}
|
||||
})
|
||||
return field.convert_order('sale_date', tables['sale'], Sale)
|
||||
fromitem = table.join(sale_line, 'LEFT', condition=(
|
||||
Concat('sale.line,', sale_line.id) == table.origin)
|
||||
).join(sale, 'LEFT', condition=(sale_line.sale == sale.id))
|
||||
conditionals = [(sale.sale_date != Null, sale.sale_date)]
|
||||
return fromitem, conditionals
|
||||
|
||||
@classmethod
|
||||
def order_shipment_out_numbers(cls, tables):
|
||||
line, _ = tables[None]
|
||||
fromitem, conditions = cls._get_order_shipment_out_numbers_query_items(
|
||||
line)
|
||||
query = fromitem.select(
|
||||
line.id,
|
||||
Case(*conditions, _else=Literal('')),
|
||||
where=(line.invoice_type == 'out'),
|
||||
order_by=Case(*conditions, _else=Literal('')),
|
||||
group_by=(line.id, Case(*conditions, _else=Literal('')))
|
||||
)
|
||||
|
||||
cursor = Transaction().connection.cursor()
|
||||
cursor.execute(*query)
|
||||
numbers = cursor.fetchall()
|
||||
|
||||
conditions = []
|
||||
lines = {}
|
||||
for x, values in enumerate(numbers):
|
||||
# group by line
|
||||
line_id, number = values
|
||||
if number:
|
||||
lines.setdefault(line_id, []).append(number)
|
||||
|
||||
for line_id, numbers in lines.items():
|
||||
conditions.append((line.id == line_id, ','.join(sorted(numbers))))
|
||||
return [Case(*conditions, _else=Null)]
|
||||
|
||||
@classmethod
|
||||
def _get_order_shipment_out_numbers_query_items(cls, table):
|
||||
pool = Pool()
|
||||
StockMove = pool.get('stock.move')
|
||||
InvoiceMove = pool.get('account.invoice.line-stock.move')
|
||||
Shipmentout = pool.get('stock.shipment.out')
|
||||
line, _ = tables[None]
|
||||
field = getattr(Shipmentout, 'number')
|
||||
invoice_move = InvoiceMove.__table__()
|
||||
stock_move = StockMove.__table__()
|
||||
shipment_out = Shipmentout.__table__()
|
||||
|
||||
if 'shipment_out' not in tables:
|
||||
invoice_move = InvoiceMove.__table__()
|
||||
stock_move = StockMove.__table__()
|
||||
shipment_out = Shipmentout.__table__()
|
||||
tables.update({
|
||||
'invoice_move': {None: (invoice_move,
|
||||
invoice_move.invoice_line == line.id)},
|
||||
'stock_move': {None: (stock_move,
|
||||
invoice_move.stock_move == stock_move.id)},
|
||||
'shipment_out': {None: (shipment_out,
|
||||
Concat('stock.shipment.out,', shipment_out.id
|
||||
) == stock_move.shipment)},
|
||||
})
|
||||
return field.convert_order('number', tables['shipment_out'],
|
||||
Shipmentout)
|
||||
fromitem = table.join(invoice_move, 'LEFT', condition=(
|
||||
invoice_move.invoice_line == table.id)
|
||||
).join(stock_move, 'LEFT', condition=(
|
||||
invoice_move.stock_move == stock_move.id)
|
||||
).join(shipment_out, 'LEFT', condition=(
|
||||
Concat('stock.shipment.out,', shipment_out.id
|
||||
) == stock_move.shipment)
|
||||
)
|
||||
conditionals = [(shipment_out.number != Null, shipment_out.number)]
|
||||
return fromitem, conditionals
|
||||
|
||||
def get_shipment_out_numbers(self, name=None):
|
||||
if self.stock_moves:
|
||||
|
|
|
@ -148,4 +148,6 @@ Invoice line sale info::
|
|||
True
|
||||
>>> lines = InvoiceLine.find([('shipment_out_numbers', '=', '2')])
|
||||
>>> not lines
|
||||
True
|
||||
True
|
||||
>>> _ = InvoiceLine.find([], order=[('sale_date', 'ASC')])
|
||||
>>> _ = InvoiceLine.find([], order=[('shipment_out_numbers', 'ASC')])
|
Loading…
Reference in New Issue