summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/stock_move.py
diff options
context:
space:
mode:
authorit-fixcomart <it@fixcomart.co.id>2024-09-30 15:24:37 +0700
committerit-fixcomart <it@fixcomart.co.id>2024-09-30 15:24:37 +0700
commitcb81068b8b695eaf8de5c0ba6b0951bae436626e (patch)
treefd4ce5250bc2e56e91e4ce394365438854909b42 /indoteknik_custom/models/stock_move.py
parent90a29846ae6008b3152d495f2e83a5d00cfdd438 (diff)
parent3b83868fbdb29e8f5208035e5166a13e5d24f382 (diff)
Merge branch 'production' into iman/switch-account
Diffstat (limited to 'indoteknik_custom/models/stock_move.py')
-rw-r--r--indoteknik_custom/models/stock_move.py44
1 files changed, 43 insertions, 1 deletions
diff --git a/indoteknik_custom/models/stock_move.py b/indoteknik_custom/models/stock_move.py
index fe46bf65..ac2e3cc0 100644
--- a/indoteknik_custom/models/stock_move.py
+++ b/indoteknik_custom/models/stock_move.py
@@ -1,5 +1,6 @@
from odoo import fields, models, api
-
+from odoo.tools.misc import format_date, OrderedSet
+from odoo.exceptions import UserError
class StockMove(models.Model):
_inherit = 'stock.move'
@@ -7,6 +8,47 @@ class StockMove(models.Model):
line_no = fields.Integer('No', default=0)
sale_id = fields.Many2one('sale.order', string='SO')
+ def _do_unreserve(self, product=None, quantity=False):
+ moves_to_unreserve = OrderedSet()
+ for move in self:
+ if move.state == 'cancel' or (move.state == 'done' and move.scrapped):
+ continue
+ elif move.state == 'done':
+ raise UserError("You cannot unreserve a stock move that has been set to 'Done'.")
+
+ if product and move.product_id != product:
+ continue # Skip moves that don't match the specified product
+ moves_to_unreserve.add(move.id)
+
+ moves_to_unreserve = self.env['stock.move'].browse(moves_to_unreserve)
+
+ ml_to_update, ml_to_unlink = OrderedSet(), OrderedSet()
+ moves_not_to_recompute = OrderedSet()
+
+ for ml in moves_to_unreserve.move_line_ids:
+ if product and ml.product_id != product:
+ continue # Only affect the specified product
+
+ if quantity and quantity > 0:
+ # Only reduce by the specified quantity if it is greater than zero
+ ml_to_update.add(ml.id)
+ remaining_qty = ml.product_uom_qty - quantity
+ ml.write({'product_uom_qty': remaining_qty if remaining_qty > 0 else 0})
+ quantity = 0 # Set to zero to prevent further unreserving in the same loop
+ elif ml.qty_done:
+ ml_to_update.add(ml.id)
+ else:
+ ml_to_unlink.add(ml.id)
+ moves_not_to_recompute.add(ml.move_id.id)
+
+ ml_to_update, ml_to_unlink = self.env['stock.move.line'].browse(ml_to_update), self.env['stock.move.line'].browse(ml_to_unlink)
+ moves_not_to_recompute = self.env['stock.move'].browse(moves_not_to_recompute)
+
+ ml_to_unlink.unlink()
+ (moves_to_unreserve - moves_not_to_recompute)._recompute_state()
+ return True
+
+
def _prepare_account_move_line_from_mr(self, po_line, qty, move=False):
po_line.ensure_one()
aml_currency = move and move.currency_id or po_line.currency_id