This commit is contained in:
2026-04-09 21:23:27 +02:00
parent b39607d987
commit 9c8d7f11ae
3 changed files with 276 additions and 25 deletions

View File

@@ -473,32 +473,108 @@ class Sale(metaclass=PoolMeta):
if lots and getattr(lots[0], 'lot_unit_line', None):
return lots[0].lot_unit_line
return getattr(line, 'unit', None)
def _get_report_total_unit(self):
virtual_units = []
for line in self._get_report_lines():
for lot in self._get_report_line_lots(line):
if (
getattr(lot, 'lot_type', None) == 'virtual'
and getattr(lot, 'lot_unit_line', None)):
virtual_units.append(lot.lot_unit_line)
if len(virtual_units) == 1:
return virtual_units[0]
line = self._get_report_first_line()
if line:
return self._get_report_line_unit(line)
return None
@staticmethod
def _get_report_unit_wording(unit):
label = ''
for attr in ('symbol', 'rec_name', 'name'):
value = getattr(unit, attr, None)
if value:
label = str(value).strip().upper()
break
mapping = {
'MT': ('METRIC TON', 'METRIC TONS'),
'METRIC TON': ('METRIC TON', 'METRIC TONS'),
'METRIC TONS': ('METRIC TON', 'METRIC TONS'),
'KG': ('KILOGRAM', 'KILOGRAMS'),
'KGS': ('KILOGRAM', 'KILOGRAMS'),
'KILOGRAM': ('KILOGRAM', 'KILOGRAMS'),
'KILOGRAMS': ('KILOGRAM', 'KILOGRAMS'),
'LB': ('POUND', 'POUNDS'),
'LBS': ('POUND', 'POUNDS'),
'POUND': ('POUND', 'POUNDS'),
'POUNDS': ('POUND', 'POUNDS'),
'BALE': ('BALE', 'BALES'),
'BALES': ('BALE', 'BALES'),
}
if label in mapping:
return mapping[label]
if label.endswith('S') and len(label) > 1:
return label[:-1], label
return label, label
@classmethod
def _report_quantity_to_words(cls, quantity, unit):
singular, plural = cls._get_report_unit_wording(unit)
return quantity_to_words(
quantity,
unit_singular=singular,
unit_plural=plural,
)
@staticmethod
def _convert_report_quantity(quantity, from_unit, to_unit):
value = Decimal(str(quantity or 0))
if not from_unit or not to_unit:
return value
if getattr(from_unit, 'id', None) == getattr(to_unit, 'id', None):
return value
from_name = getattr(from_unit, 'rec_name', None)
to_name = getattr(to_unit, 'rec_name', None)
if from_name and to_name and from_name == to_name:
return value
Uom = Pool().get('product.uom')
converted = Uom.compute_qty(from_unit, float(value), to_unit) or 0
return Decimal(str(converted))
def _get_report_total_weight(self, index):
lines = self._get_report_lines()
if not lines:
return None
total_unit = self._get_report_total_unit()
total = Decimal(0)
for line in lines:
quantity = self._get_report_line_weights(line)[index]
total += self._convert_report_quantity(
quantity,
self._get_report_line_unit(line),
total_unit,
)
return total
@property
def report_gross(self):
lines = self._get_report_lines()
if lines:
total = Decimal(0)
for line in lines:
total += self._get_report_line_weights(line)[1]
total = self._get_report_total_weight(1)
if total is not None:
return total
return ''
@property
def report_net(self):
lines = self._get_report_lines()
if lines:
total = Decimal(0)
for line in lines:
total += self._get_report_line_weights(line)[0]
total = self._get_report_total_weight(0)
if total is not None:
return total
return ''
@property
def report_total_quantity(self):
lines = self._get_report_lines()
if lines:
total = sum(self._get_report_line_weights(line)[0] for line in lines)
total = self._get_report_total_weight(0)
if total is not None:
return self._format_report_number(total, keep_trailing_decimal=True)
return '0.0'
@@ -515,10 +591,10 @@ class Sale(metaclass=PoolMeta):
@property
def report_qt(self):
lines = self._get_report_lines()
if lines:
total = sum(self._get_report_line_quantity(line) for line in lines)
return quantity_to_words(total)
total = self._get_report_total_weight(0)
if total is not None:
return self._report_quantity_to_words(
total, self._get_report_total_unit())
return ''
@property
@@ -536,7 +612,7 @@ class Sale(metaclass=PoolMeta):
line_unit.rec_name.upper()
if line_unit and line_unit.rec_name else ''
)
words = quantity_to_words(current_quantity)
words = self._report_quantity_to_words(current_quantity, line_unit)
period = line.del_period.description if getattr(line, 'del_period', None) else ''
detail = ' '.join(
part for part in [
@@ -623,8 +699,12 @@ class Sale(metaclass=PoolMeta):
current_quantity = self._get_report_line_quantity(line)
quantity = self._format_report_number(
current_quantity, keep_trailing_decimal=True)
unit = line.unit.rec_name.upper() if line.unit and line.unit.rec_name else ''
words = quantity_to_words(current_quantity)
line_unit = self._get_report_line_unit(line)
unit = (
line_unit.rec_name.upper()
if line_unit and line_unit.rec_name else ''
)
words = self._report_quantity_to_words(current_quantity, line_unit)
period = line.del_period.description if getattr(line, 'del_period', None) else ''
quantity_line = ' '.join(
part for part in [