Compute name of the product for sale, purchase and invoice lines

Instead of having the code in the report templates, we use a property that can
be extended without updating the template.

Closes #12892
This commit is contained in:
Cédric Krier committed 2024-01-04 23:55:09 +01:00
1 parent bd54284d6f
commit b4608428fb
12 files changed
+82 -23

No files matched your search

+2 -2
View File
@@ -818,8 +818,8 @@
</table:table-row>
<table:table-row>
<table:table-cell table:style-name="Table1.A5" office:value-type="string">
<text:p text:style-name="Table_20_Contents"><text:placeholder text:placeholder-type="text">&lt;if test=&quot;line.product&quot;&gt;</text:placeholder></text:p>
<text:p text:style-name="Table_20_Contents"><text:placeholder text:placeholder-type="text">&lt;line.product.rec_name&gt;</text:placeholder></text:p>
<text:p text:style-name="Table_20_Contents"><text:placeholder text:placeholder-type="text">&lt;if test=&quot;line.product_name&quot;&gt;</text:placeholder></text:p>
<text:p text:style-name="Table_20_Contents"><text:placeholder text:placeholder-type="text">&lt;line.product_name&gt;</text:placeholder></text:p>
<text:p text:style-name="Table_20_Contents"><text:placeholder text:placeholder-type="text">&lt;/if&gt;</text:placeholder></text:p>
<text:p text:style-name="Table_20_Contents"><text:placeholder text:placeholder-type="text">&lt;if test=&quot;line.description&quot;&gt;</text:placeholder></text:p>
<text:p text:style-name="Table_20_Contents"><text:placeholder text:placeholder-type="text">&lt;for each=&quot;description in line.description.split(&apos;\n&apos;)&quot;&gt;</text:placeholder></text:p>
+6 -1
View File
@@ -29,7 +29,8 @@ from trytond.pool import Pool
from trytond.pyson import Bool, Eval, Id, If
from trytond.report import Report
from trytond.rpc import RPC
from trytond.tools import firstline, grouped_slice, reduce_ids, slugify
from trytond.tools import (
cached_property, firstline, grouped_slice, reduce_ids, slugify)
from trytond.transaction import Transaction
from trytond.wizard import (
Button, StateAction, StateReport, StateTransition, StateView, Wizard)
@@ -2636,6 +2637,10 @@ class InvoiceLine(sequence_ordered(), ModelSQL, ModelView, TaxableMixin):
if not self.unit or self.unit.category != category:
self.unit = self.product.default_uom.id
@cached_property
def product_name(self):
return self.product.rec_name if self.product else ''
@fields.depends('product')
def on_change_with_product_uom_category(self, name=None):
return self.product.default_uom_category if self.product else None
+11
View File
@@ -7,6 +7,7 @@ from trytond.model import ModelView, Workflow, fields
from trytond.model.exceptions import AccessError
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval
from trytond.tools import cached_property
from trytond.transaction import Transaction, without_check_access
@@ -110,6 +111,16 @@ class InvoiceLine(metaclass=PoolMeta):
('type', '=', Eval('type')),
]
@cached_property
def product_name(self):
pool = Pool()
PurchaseLine = pool.get('purchase.line')
name = super().product_name
if (isinstance(self.origin, PurchaseLine)
and self.origin.product_supplier):
name = self.origin.product_supplier.rec_name
return name
@fields.depends('origin')
def on_change_with_product_uom_category(self, name=None):
pool = Pool()
+3 -8
View File
@@ -707,14 +707,9 @@
</table:table-row>
<table:table-row>
<table:table-cell table:style-name="Table1.A5" office:value-type="string">
<text:p text:style-name="P26"><text:placeholder text:placeholder-type="text">&lt;choose&gt;</text:placeholder></text:p>
<text:p text:style-name="P26"><text:placeholder text:placeholder-type="text">&lt;when test=&quot;line.product_supplier&quot;&gt;</text:placeholder></text:p>
<text:p text:style-name="P25"><text:placeholder text:placeholder-type="text">&lt;line.product_supplier.rec_name&gt;</text:placeholder></text:p>
<text:p text:style-name="P25"><text:placeholder text:placeholder-type="text">&lt;/when&gt;</text:placeholder></text:p>
<text:p text:style-name="P25"><text:placeholder text:placeholder-type="text">&lt;when test=&quot;line.product&quot;&gt;</text:placeholder></text:p>
<text:p text:style-name="P25"><text:placeholder text:placeholder-type="text">&lt;line.product.rec_name&gt;</text:placeholder></text:p>
<text:p text:style-name="P25"><text:placeholder text:placeholder-type="text">&lt;/when&gt;</text:placeholder></text:p>
<text:p text:style-name="P25"><text:placeholder text:placeholder-type="text">&lt;/choose&gt;</text:placeholder></text:p>
<text:p text:style-name="P25"><text:placeholder text:placeholder-type="text">&lt;if test=&quot;line.product_name&quot;&gt;</text:placeholder></text:p>
<text:p text:style-name="P25"><text:placeholder text:placeholder-type="text">&lt;line.product_name&gt;</text:placeholder></text:p>
<text:p text:style-name="P25"><text:placeholder text:placeholder-type="text">&lt;/if&gt;</text:placeholder></text:p>
<text:p text:style-name="P25"><text:placeholder text:placeholder-type="text">&lt;if test=&quot;line.description&quot;&gt;</text:placeholder></text:p>
<text:p text:style-name="P25"><text:placeholder text:placeholder-type="text">&lt;for each=&quot;description in line.description.split(&apos;\n&apos;)&quot;&gt;</text:placeholder></text:p>
<text:p text:style-name="P25"><text:placeholder text:placeholder-type="text">&lt;description&gt;</text:placeholder></text:p>
+10 -1
View File
@@ -25,7 +25,7 @@ from trytond.modules.currency.fields import Monetary
from trytond.modules.product import price_digits
from trytond.pool import Pool
from trytond.pyson import Bool, Eval, If, PYSONEncoder
from trytond.tools import firstline
from trytond.tools import cached_property, firstline
from trytond.transaction import Transaction
from trytond.wizard import (
Button, StateAction, StateTransition, StateView, Wizard)
@@ -1467,6 +1467,15 @@ class Line(sequence_ordered(), ModelSQL, ModelView):
self.amount = self.on_change_with_amount()
@cached_property
def product_name(self):
if self.product_supplier:
return self.product_supplier.rec_name
elif self.product:
return self.product.rec_name
else:
return ''
@fields.depends(
'type', 'product',
methods=['on_change_with_company', '_get_tax_rule_pattern'])
+2 -2
View File
@@ -698,8 +698,8 @@
</table:table-row>
<table:table-row>
<table:table-cell table:style-name="Table1.A5" office:value-type="string">
<text:p text:style-name="P20"><text:placeholder text:placeholder-type="text">&lt;if test=&quot;line.product&quot;&gt;</text:placeholder></text:p>
<text:p text:style-name="P20"><text:placeholder text:placeholder-type="text">&lt;line.product.rec_name&gt;</text:placeholder></text:p>
<text:p text:style-name="P20"><text:placeholder text:placeholder-type="text">&lt;if test=&quot;line.product_name&quot;&gt;</text:placeholder></text:p>
<text:p text:style-name="P20"><text:placeholder text:placeholder-type="text">&lt;line.product_name&gt;</text:placeholder></text:p>
<text:p text:style-name="P20"><text:placeholder text:placeholder-type="text">&lt;/if&gt;</text:placeholder></text:p>
<text:p text:style-name="P20"><text:placeholder text:placeholder-type="text">&lt;if test=&quot;line.description&quot;&gt;</text:placeholder></text:p>
<text:p text:style-name="P20"><text:placeholder text:placeholder-type="text">&lt;for each=&quot;description in line.description.split(&apos;\n&apos;)&quot;&gt;</text:placeholder></text:p>
+5 -1
View File
@@ -25,7 +25,7 @@ from trytond.modules.currency.fields import Monetary
from trytond.modules.product import price_digits
from trytond.pool import Pool
from trytond.pyson import Bool, Eval, If, PYSONEncoder
from trytond.tools import firstline, sortable_values
from trytond.tools import cached_property, firstline, sortable_values
from trytond.transaction import Transaction
from trytond.wizard import (
Button, StateAction, StateTransition, StateView, Wizard)
@@ -1550,6 +1550,10 @@ class SaleLine(TaxableMixin, sequence_ordered(), ModelSQL, ModelView):
self.amount = self.on_change_with_amount()
@cached_property
def product_name(self):
return self.product.rec_name if self.product else ''
@fields.depends(
'type', 'product',
methods=['on_change_with_company', '_get_tax_rule_pattern'])
+2 -1
View File
@@ -3,7 +3,7 @@
from trytond.pool import Pool
from . import product, sale
from . import account, product, sale
__all__ = ['register']
@@ -14,6 +14,7 @@ def register():
product.Product,
product.ProductCustomer,
sale.Line,
account.InvoiceLine,
module='sale_product_customer', type_='model')
Pool.register(
sale.AmendmentLine,
+19
View File
@@ -0,0 +1,19 @@
# 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 trytond.pool import Pool, PoolMeta
from trytond.tools import cached_property
class InvoiceLine(metaclass=PoolMeta):
__name__ = 'account.invoice.line'
@cached_property
def product_name(self):
pool = Pool()
SaleLine = pool.get('sale.line')
name = super().product_name
if (isinstance(self.origin, SaleLine)
and self.origin.product_customer):
name = self.origin.product_customer.rec_name
return name
+13 -7
View File
@@ -52,14 +52,20 @@ class ProductCustomer(
self.template = self.product.template
def get_rec_name(self, name):
if self.name:
name = self.name
elif self.product:
name = self.product.name
if not self.name and not self.code:
if self.product:
name = self.product.rec_name
else:
name = self.template.rec_name
else:
name = self.template.name
if self.code:
name = '[' + self.code + '] ' + name
if self.name:
name = self.name
elif self.product:
name = self.product.name
else:
name = self.template.name
if self.code:
name = '[' + self.code + '] ' + name
return name
@classmethod
+8
View File
@@ -4,6 +4,7 @@
from trytond.model import fields
from trytond.pool import PoolMeta
from trytond.pyson import Bool, Eval, If
from trytond.tools import cached_property
class Line(metaclass=PoolMeta):
@@ -62,6 +63,13 @@ class Line(metaclass=PoolMeta):
self.product, = self.product_customer.template.products
self.on_change_product()
@cached_property
def product_name(self):
name = super().product_name
if self.product_customer:
name = self.product_customer.rec_name
return name
class AmendmentLine(metaclass=PoolMeta):
__name__ = 'sale.amendment.line'
+1
View File
@@ -1,6 +1,7 @@
[tryton]
version=7.1.0
depends:
account_invoice
ir
product
sale