bug Th qt

This commit is contained in:
2026-04-09 20:31:24 +02:00
parent 8b9787d4c0
commit 65482b4a8b
3 changed files with 129 additions and 75 deletions

View File

@@ -1636,12 +1636,13 @@ class Line(metaclass=PoolMeta):
pricings = Pricing.search(['price_component','=',pc.id],order=[('pricing_date', 'ASC')])
if pricings:
cumul_qt = Decimal(0)
base_quantity = self._get_pricing_base_quantity()
index = 0
for pr in pricings:
cumul_qt += pr.quantity
pr.fixed_qt = cumul_qt
pr.fixed_qt_price = pr.get_fixed_price()
pr.unfixed_qt = Decimal(pr.line.quantity_theorical) - pr.fixed_qt
pr.unfixed_qt = base_quantity - pr.fixed_qt
pr.unfixed_qt_price = pr.fixed_qt_price
pr.eod_price = pr.get_eod_price_purchase()
if index == len(pricings) - 1:
@@ -1699,11 +1700,18 @@ class Line(metaclass=PoolMeta):
return pl_sorted[len(pl)-1][t]
return Decimal(0)
def _get_pricing_base_quantity(self):
quantity = self.quantity_theorical
if quantity is None:
quantity = self.quantity
return Decimal(str(quantity or 0))
def generate_pricing(self,pc,dl,pl):
Pricing = Pool().get('pricing.pricing')
pricing = Pricing.search(['price_component','=',pc.id])
if pricing:
Pricing.delete(pricing)
base_quantity = self._get_pricing_base_quantity()
cumul_qt = 0
index = 0
dl_sorted = sorted(dl)
@@ -1724,10 +1732,10 @@ class Line(metaclass=PoolMeta):
#p.fixed_qt_price = p.get_fixed_price()
if p.fixed_qt_price == 0:
p.fixed_qt_price = round(Decimal(self.getnearprice(pl,d,'avg_minus_1')),4)
p.unfixed_qt = round(Decimal(self.quantity_theorical) - Decimal(cumul_qt),5)
p.unfixed_qt = round(base_quantity - Decimal(cumul_qt),5)
if p.unfixed_qt < 0.001:
p.unfixed_qt = Decimal(0)
p.fixed_qt = Decimal(self.quantity_theorical)
p.fixed_qt = base_quantity
if price > 0:
p.unfixed_qt_price = price
else:

View File

@@ -1250,12 +1250,13 @@ class SaleLine(metaclass=PoolMeta):
pricings = Pricing.search(['price_component','=',pc.id],order=[('pricing_date', 'ASC')])
if pricings:
cumul_qt = Decimal(0)
base_quantity = self._get_pricing_base_quantity()
index = 0
for pr in pricings:
cumul_qt += pr.quantity
pr.fixed_qt = cumul_qt
pr.fixed_qt_price = pr.get_fixed_price()
pr.unfixed_qt = Decimal(pr.sale_line.quantity_theorical) - pr.fixed_qt
pr.unfixed_qt = base_quantity - pr.fixed_qt
pr.unfixed_qt_price = pr.fixed_qt_price
pr.eod_price = pr.get_eod_price_sale()
if index == len(pricings) - 1:
@@ -1313,11 +1314,18 @@ class SaleLine(metaclass=PoolMeta):
return pl_sorted[len(pl)-1][t]
return Decimal(0)
def _get_pricing_base_quantity(self):
quantity = self.quantity_theorical
if quantity is None:
quantity = self.quantity
return Decimal(str(quantity or 0))
def generate_pricing(self,pc,dl,pl):
Pricing = Pool().get('pricing.pricing')
pricing = Pricing.search(['price_component','=',pc.id])
if pricing:
Pricing.delete(pricing)
base_quantity = self._get_pricing_base_quantity()
cumul_qt = 0
index = 0
dl_sorted = sorted(dl)
@@ -1340,10 +1348,10 @@ class SaleLine(metaclass=PoolMeta):
#p.fixed_qt_price = p.get_fixed_price()
if p.fixed_qt_price == 0:
p.fixed_qt_price = round(Decimal(self.getnearprice(pl,d,'avg_minus_1',pc.pricing_date)),4)
p.unfixed_qt = round(Decimal(self.quantity_theorical) - Decimal(cumul_qt),4)
p.unfixed_qt = round(base_quantity - Decimal(cumul_qt),4)
if p.unfixed_qt < 0.001:
p.unfixed_qt = Decimal(0)
p.fixed_qt = Decimal(self.quantity_theorical)
p.fixed_qt = base_quantity
if price > 0:
logger.info("GENERATE_1:%s",price)
p.unfixed_qt_price = price

View File

@@ -1,6 +1,7 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import datetime
from decimal import Decimal
from unittest.mock import Mock, patch
@@ -208,6 +209,43 @@ class PurchaseTradeTestCase(ModuleTestCase):
purchase_component.get_quota_purchase('quota'),
Decimal('15.00000'))
def test_sale_and_purchase_generate_pricing_use_quantity_fallback(self):
'pricing generation uses quantity when theoretical quantity is missing'
Sale = Pool().get('sale.sale')
Purchase = Pool().get('purchase.purchase')
sale = Sale()
sale.id = 1
sale.quantity = Decimal('10')
sale.quantity_theorical = None
sale.unit = Mock()
sale.sale = Mock(currency=Mock())
sale.getnearprice = Mock(return_value=Decimal('0'))
purchase = Purchase()
purchase.id = 1
purchase.quantity = Decimal('12')
purchase.quantity_theorical = None
purchase.unit = Mock()
purchase.purchase = Mock(currency=Mock())
purchase.getnearprice = Mock(return_value=Decimal('0'))
pricing_model = Mock()
pricing_model.search.return_value = []
pc_sale = Mock(id=1, quota_sale=Decimal('2'), pricing_date=None, price_index=Mock(get_price=Mock(return_value=Decimal('1'))))
pc_purchase = Mock(id=1, quota=Decimal('3'), pricing_date=None, price_index=Mock(get_price=Mock(return_value=Decimal('1'))))
with patch('trytond.modules.purchase_trade.sale.Pool') as SalePool, patch(
'trytond.modules.purchase_trade.purchase.Pool') as PurchasePool:
SalePool.return_value.get.return_value = pricing_model
PurchasePool.return_value.get.return_value = pricing_model
sale.generate_pricing(pc_sale, [datetime.datetime(2026, 4, 1)], [])
purchase.generate_pricing(pc_purchase, [datetime.datetime(2026, 4, 1)], [])
self.assertEqual(pricing_model.save.call_args_list[0].args[0][0].unfixed_qt, Decimal('10'))
self.assertEqual(pricing_model.save.call_args_list[1].args[0][0].unfixed_qt, Decimal('12'))
def test_sale_and_purchase_trader_operator_domains_use_explicit_categories(self):
'sale and purchase trader/operator fields are filtered by TRADER/OPERATOR categories'
Sale = Pool().get('sale.sale')