stock_inventory_scrap_backdate/models/stock_scrap.py
2026-06-17 14:59:07 +07:00

34 lines
1.1 KiB
Python

# -*- coding: utf-8 -*-
from odoo import models, fields, api
class StockScrap(models.Model):
_inherit = 'stock.scrap'
date_done = fields.Datetime(
'Date',
readonly=False,
default=fields.Datetime.now,
copy=False,
)
def do_scrap(self):
# Capture the custom date_done for each scrap record in draft state
backdates = {}
for scrap in self:
if scrap.state == 'draft' and scrap.date_done:
backdates[scrap.id] = scrap.date_done
else:
backdates[scrap.id] = fields.Datetime.now()
res = super().do_scrap()
# Shifting date_done and generated moves
for scrap in self:
backdate = backdates.get(scrap.id)
if backdate:
# We need to write date_done to the custom date because super().do_scrap() sets it to Datetime.now()
scrap.write({'date_done': backdate})
if scrap.move_ids:
scrap.move_ids._post_process_validated_moves(backdate)
return res