From da3c68d0c04aa0fcf1ce819d550422609dfc7c1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Wed, 1 Nov 2023 16:34:42 +0100 Subject: [PATCH] Add invoice progress on sale and purchase line --- modules/purchase/purchase.py | 30 ++++++++++++++++++++++++++++++ modules/sale/sale.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/modules/purchase/purchase.py b/modules/purchase/purchase.py index 62eecf18df..50f0424410 100644 --- a/modules/purchase/purchase.py +++ b/modules/purchase/purchase.py @@ -1210,6 +1210,9 @@ class Line(sequence_ordered(), ModelSQL, ModelView): states={ 'invisible': ~Eval('invoice_lines'), }) + invoice_progress = fields.Function( + fields.Float("Invoice Progress", digits=(1, 4)), + 'get_invoice_progress') moves = fields.One2Many( 'stock.move', 'origin', "Moves", readonly=True, states={ @@ -1314,6 +1317,33 @@ class Line(sequence_ordered(), ModelSQL, ModelView): def default_delivery_date_edit(cls): return False + @property + def _invoice_remaining_quantity(self): + "Compute the remaining quantity to be paid" + pool = Pool() + UoM = pool.get('product.uom') + if self.type != 'line': + return + skips = set(self.purchase.invoices_ignored) + quantity = self.quantity + for invoice_line in self.invoice_lines: + if invoice_line.type != 'line': + continue + if (invoice_line.invoice.state == 'paid' + or invoice_line.invoice in skips): + quantity -= UoM.compute_qty( + invoice_line.unit or self.unit, invoice_line.quantity, + self.unit) + return quantity + + def get_invoice_progress(self, name): + progress = None + quantity = self._invoice_remaining_quantity + if quantity is not None and self.quantity: + progress = round((self.quantity - quantity) / self.quantity, 4) + progress = max(0, min(1, progress)) + return progress + @property def _move_remaining_quantity(self): "Compute the remaining quantity to receive" diff --git a/modules/sale/sale.py b/modules/sale/sale.py index 670240c2f9..7feb939581 100644 --- a/modules/sale/sale.py +++ b/modules/sale/sale.py @@ -1322,6 +1322,9 @@ class SaleLine(TaxableMixin, sequence_ordered(), ModelSQL, ModelView): states={ 'invisible': ~Eval('invoice_lines'), }) + invoice_progress = fields.Function( + fields.Float("Invoice Progress", digits=(1, 4)), + 'get_invoice_progress') moves = fields.One2Many( 'stock.move', 'origin', "Moves", readonly=True, states={ @@ -1389,6 +1392,33 @@ class SaleLine(TaxableMixin, sequence_ordered(), ModelSQL, ModelView): self.unit = None self.taxes = None + @property + def _invoice_remaining_quantity(self): + "Compute the remaining quantity to be paid" + pool = Pool() + UoM = pool.get('product.uom') + if self.type != 'line': + return + skips = set(self.sale.invoices_ignored) + quantity = self.quantity + for invoice_line in self.invoice_lines: + if invoice_line.type != 'line': + continue + if (invoice_line.invoice.state == 'paid' + or invoice_line.invoice in skips): + quantity -= UoM.compute_qty( + invoice_line.unit or self.unit, invoice_line.quantity, + self.unit) + return quantity + + def get_invoice_progress(self, name): + progress = None + quantity = self._invoice_remaining_quantity + if quantity is not None and self.quantity: + progress = round((self.quantity - quantity) / self.quantity, 4) + progress = max(0, min(1, progress)) + return progress + @property def _move_remaining_quantity(self): "Compute the remaining quantity to ship"