From 6c792b91cfa4819a784f4c565ce4c4e45796a81e Mon Sep 17 00:00:00 2001 From: Raimon Esteve Date: Tue, 20 Jun 2017 10:24:31 +0200 Subject: [PATCH] Add shipment origin fields in invoice line --- __init__.py | 1 + invoice.py | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/__init__.py b/__init__.py index c4843f9..9bf9c3f 100644 --- a/__init__.py +++ b/__init__.py @@ -8,4 +8,5 @@ from . import invoice def register(): Pool.register( invoice.Invoice, + invoice.InvoiceLine, module='account_invoice_shipment', type_='model') diff --git a/invoice.py b/invoice.py index 812f3bb..aeff523 100644 --- a/invoice.py +++ b/invoice.py @@ -3,7 +3,7 @@ from trytond.model import fields from trytond.pool import Pool, PoolMeta -__all__ = ['Invoice'] +__all__ = ['Invoice', 'InvoiceLine'] class Invoice: @@ -84,3 +84,39 @@ class Invoice: get_sales_origin_number = get_sales_origin_reference('number') get_sales_origin_reference = get_sales_origin_reference('reference') + + +class InvoiceLine: + __metaclass__ = PoolMeta + __name__ = 'account.invoice.line' + + shipments_origin = fields.Function(fields.One2Many('stock.shipment.out', None, + 'Shipments'), 'get_shipments_origin') + shipments_origin_return = fields.Function( + fields.One2Many('stock.shipment.out.return', None, 'Shipment Returns'), + 'get_shipments_origin_returns') + shipments_origin_number = fields.Function(fields.Char('Origin Shipment Number'), + 'get_shipments_origin_number') + + def get_shipments_origin_returns(model_name): + "Computes the origin returns or shipments" + def method(self, name): + Model = Pool().get(model_name) + shipments = set() + if self.origin and self.origin.__name__ == 'sale.line': + for move in self.origin.moves: + if move.shipment and isinstance(move.shipment, Model): + shipments.add(move.shipment.id) + return list(shipments) + return method + + get_shipments_origin = get_shipments_origin_returns('stock.shipment.out') + get_shipments_origin_returns = get_shipments_origin_returns('stock.shipment.out.return') + + def get_shipments_origin_number(self, name=None): + numbers = [] + for shipment_origin in ['shipments_origin', 'shipments_origin_return']: + for shipment in getattr(self, shipment_origin): + if shipment.number: + numbers.append(shipment.number) + return ', '.join(numbers)