CN/DN commission

This commit is contained in:
2026-06-17 09:19:21 +02:00
parent a8102d513f
commit dd2acc2d09
4 changed files with 182 additions and 9 deletions

View File

@@ -383,6 +383,7 @@ class Fee(ModelSQL,ModelView):
"create a debit note or credit note by reversing the "
"previous fee invoice line and adding a new line with "
"the current fee values. Do you want to continue?")
fee.warn_percent_price_partial_lots()
fee.ensure_ordered_purchase()
if fee.purchase:
fl = FeeLots.search([('fee','=',fee.id)])
@@ -498,6 +499,10 @@ class Fee(ModelSQL,ModelView):
FeeLots = Pool().get('fee.lots')
fee_lots = FeeLots.search([('fee', '=', self.id)])
lots = [fee_lot.lot for fee_lot in fee_lots if fee_lot.lot]
return self._select_effective_lots(lots)
@staticmethod
def _select_effective_lots(lots):
physical_lots = [
lot for lot in lots
if getattr(lot, 'lot_type', None) == 'physic'
@@ -509,6 +514,42 @@ class Fee(ModelSQL,ModelView):
if getattr(lot, 'lot_type', None) == 'virtual'
]
@staticmethod
def _record_key(record):
return getattr(record, 'id', None) or id(record)
def _get_effective_line_lots(self):
line = self.line or self.sale_line
return self._select_effective_lots(
list(getattr(line, 'lots', []) or []))
def percent_price_lots_cover_line(self):
if self.mode != 'pprice':
return True
line_lots = self._get_effective_line_lots()
if not line_lots:
return True
fee_lots = self._get_effective_fee_lots()
fee_lot_keys = {self._record_key(lot) for lot in fee_lots}
return all(
self._record_key(lot) in fee_lot_keys
for lot in line_lots)
def warn_percent_price_partial_lots(self):
if self.percent_price_lots_cover_line():
return
Warning = Pool().get('res.user.warning')
warning_name = Warning.format(
"Partial % price fee lots", [self])
if Warning.check(warning_name):
raise UserWarning(
warning_name,
"This % price fee is linked to only part of the line lots. "
"The commission cannot be safely computed from a partial "
"quantity because lot quantities may change through BL, WR "
"or LR weighing states. Please link all lots to the fee or "
"review the fee amount manually before continuing.")
def is_effective_fee_lot(self, lot):
return bool(lot and lot in self._get_effective_fee_lots())
@@ -713,8 +754,16 @@ class Fee(ModelSQL,ModelView):
return self._unsigned_amount(round((self.quantity if self.quantity else 0) * self.price * sign,2))
elif self.mode == 'pprice':
if self.line:
if self.percent_price_lots_cover_line():
return self._unsigned_amount(round(
self.price / 100
* Decimal(self.line.amount or 0) * sign, 2))
return self._unsigned_amount(round(self.price / 100 * self.line.unit_price * (self.quantity if self.quantity else 0) * sign,2))
if self.sale_line:
if self.percent_price_lots_cover_line():
return self._unsigned_amount(round(
self.price / 100
* Decimal(self.sale_line.amount or 0) * sign, 2))
return self._unsigned_amount(round(self.price / 100 * self.sale_line.unit_price * (self.quantity if self.quantity else 0) * sign,2))
if self.shipment_in:
StockMove = Pool().get('stock.move')
@@ -761,13 +810,18 @@ class Fee(ModelSQL,ModelView):
if self.mode == 'lumpsum':
if self.amount != self.purchase.lines[0].unit_price:
self.purchase.lines[0].unit_price = self.amount
elif self.mode == 'ppack':
if self.amount != self.purchase.lines[0].amount:
self.purchase.lines[0].unit_price = self.price
self.purchase.lines[0].quantity = self.quantity
else:
if self.get_price_per_qt() != self.purchase.lines[0].unit_price:
self.purchase.lines[0].unit_price = self.get_price_per_qt()
elif self.mode == 'ppack':
if self.amount != self.purchase.lines[0].amount:
self.purchase.lines[0].unit_price = self.price
self.purchase.lines[0].quantity = self.quantity
elif self.mode == 'pprice':
if self.amount != self.purchase.lines[0].unit_price:
self.purchase.lines[0].unit_price = self.amount
if self.purchase.lines[0].quantity != 1:
self.purchase.lines[0].quantity = 1
else:
if self.get_price_per_qt() != self.purchase.lines[0].unit_price:
self.purchase.lines[0].unit_price = self.get_price_per_qt()
if self.quantity != self.purchase.lines[0].quantity:
self.purchase.lines[0].quantity = self.quantity
if self.product != self.purchase.lines[0].product:
@@ -912,6 +966,10 @@ class Fee(ModelSQL,ModelView):
line.quantity = round(quantity, 5)
line.unit = unit
line.fee_ = self.id
if self.mode == 'pprice':
line.quantity = 1
line.unit_price = round(Decimal(self.amount or 0), 4)
return
if getattr(self, 'price', None) is not None:
fee_price = self.get_price_per_qt()
logger.info("GET_FEE_PRICE_PER_QT:%s", fee_price)