Th qt correction

This commit is contained in:
2026-04-07 13:42:17 +02:00
parent 51a84f1f2e
commit 9f06398b2c
5 changed files with 274 additions and 28 deletions

View File

@@ -777,7 +777,7 @@ class SaleLine(metaclass=PoolMeta):
del_period = fields.Many2One('product.month',"Delivery Period")
lots = fields.One2Many('lot.lot','sale_line',"Lots",readonly=True)
fees = fields.One2Many('fee.fee', 'sale_line', 'Fees')
quantity_theorical = fields.Numeric("Th. quantity", digits='unit', readonly=True)
quantity_theorical = fields.Numeric("Th. quantity", digits='unit', readonly=False)
premium = fields.Numeric("Premium/Discount",digits='unit')
price_type = fields.Selection([
('cash', 'Cash Price'),
@@ -1245,28 +1245,77 @@ class SaleLine(metaclass=PoolMeta):
Pricing.save([p])
index += 1
# @classmethod
# def write(cls, records, values):
# if 'quantity' in values:
# for record in records:
# old_qt = record.quantity
# new_qt = values['quantity']
# logger.info("WRITE_OLD_QT:%s",old_qt)
# logger.info("WRITE_NEW_QT:%s",new_qt)
# if old_qt != new_qt:
# LotQt = Pool().get('lot.qt')
# lqts = LotQt.search(['lot_s','=',record.lots[0]])
# if len(lqts)>1:
# raise UserError("You cannot changed quantity with open quantities defined !")
# return
# elif len(lqts)==1:
# if lqts[0].lot_p or lqts[0].lot_shipment_origin:
# raise UserError("You cannot changed quantity with open quantities defined !")
# return
# lqts[0].lot_quantity = new_qt
# LotQt.save(lqts)
# super().write(records, values)
@classmethod
def write(cls, *args):
Lot = Pool().get('lot.lot')
LotQt = Pool().get('lot.qt')
old_values = {}
for records, values in zip(args[::2], args[1::2]):
if 'quantity_theorical' in values:
for record in records:
old_values[record.id] = record.quantity_theorical
super().write(*args)
lines = sum(args[::2], [])
for line in lines:
if line.id not in old_values:
continue
old = Decimal(old_values[line.id] or 0)
new = Decimal(line.quantity_theorical or 0)
delta = new - old
if delta == 0:
continue
virtual_lots = [
lot for lot in (line.lots or [])
if lot.lot_type == 'virtual'
]
if not virtual_lots:
continue
vlot = virtual_lots[0]
lqts = LotQt.search([
('lot_s', '=', vlot.id),
('lot_p', '=', None),
('lot_shipment_in', '=', None),
('lot_shipment_internal', '=', None),
('lot_shipment_out', '=', None),
])
if delta > 0:
new_qty = round(
Decimal(vlot.get_current_quantity_converted() or 0) + delta,
5)
vlot.set_current_quantity(new_qty, new_qty, 1)
Lot.save([vlot])
if lqts:
lqt = lqts[0]
lqt.lot_quantity = round(
Decimal(lqt.lot_quantity or 0) + delta, 5)
LotQt.save([lqt])
else:
lqt = LotQt()
lqt.lot_p = None
lqt.lot_s = vlot.id
lqt.lot_quantity = round(delta, 5)
lqt.lot_unit = line.unit
LotQt.save([lqt])
elif delta < 0:
decrease = abs(delta)
if not lqts or Decimal(lqts[0].lot_quantity or 0) < decrease:
raise UserError("Please unlink or unmatch lot")
new_qty = round(
Decimal(vlot.get_current_quantity_converted() or 0)
- decrease,
5)
vlot.set_current_quantity(new_qty, new_qty, 1)
Lot.save([vlot])
lqt = lqts[0]
lqt.lot_quantity = round(
Decimal(lqt.lot_quantity or 0) - decrease, 5)
LotQt.save([lqt])
@classmethod
def delete(cls, lines):