summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiqdad <ahmadmiqdad27@gmail.com>2025-07-16 21:49:53 +0700
committerMiqdad <ahmadmiqdad27@gmail.com>2025-07-16 21:49:53 +0700
commit2c5f2513e9380da52d9893c8dc5ad148c298a882 (patch)
tree196934b29e4a8ea71ec76b9efeff1fb0a32d6d64
parent6a763b8aa16bc71b345378a9155c386b4d7bd400 (diff)
<miqdad> temp
-rw-r--r--indoteknik_custom/models/tukar_guling.py227
1 files changed, 119 insertions, 108 deletions
diff --git a/indoteknik_custom/models/tukar_guling.py b/indoteknik_custom/models/tukar_guling.py
index 2c39b547..295ca5d9 100644
--- a/indoteknik_custom/models/tukar_guling.py
+++ b/indoteknik_custom/models/tukar_guling.py
@@ -481,13 +481,8 @@ class TukarGuling(models.Model):
if not record.operations:
raise UserError("BU/OUT dari field operations tidak ditemukan.")
- created_returns = []
-
- bu_pick_to_return = record.operations.konfirm_koli_lines.pick_id
- bu_out_to_return = record.operations
-
- if not bu_pick_to_return and not bu_out_to_return:
- raise UserError("Tidak ada BU/PICK atau BU/OUT yang selesai untuk diretur.")
+ bu_out = record.operations
+ mapping_koli = record.mapping_koli_ids
srt_type = self.env['stock.picking.type'].browse(73)
ort_type = self.env['stock.picking.type'].browse(74)
@@ -498,116 +493,132 @@ class TukarGuling(models.Model):
BU_OUTPUT_LOCATION_ID = 60
BU_STOCK_LOCATION_ID = 57
- def _create_return_from_picking_grouped(picking):
- if not picking:
- return None
-
- grup = record.operations.group_id
-
- if picking.picking_type_id.id == 30:
- default_location_id = BU_OUTPUT_LOCATION_ID
- default_location_dest_id = BU_STOCK_LOCATION_ID
- elif picking.picking_type_id.id == 74:
- default_location_id = BU_STOCK_LOCATION_ID
- default_location_dest_id = BU_OUTPUT_LOCATION_ID
- elif picking.picking_type_id.id == 29:
- default_location_id = PARTNER_LOCATION_ID
- default_location_dest_id = BU_OUTPUT_LOCATION_ID
- elif picking.picking_type_id.id == 73:
- default_location_id = BU_OUTPUT_LOCATION_ID
- default_location_dest_id = PARTNER_LOCATION_ID
- else:
- return None
+ created_returns = []
- return_context = dict(self.env.context)
- return_context.update({
- 'active_id': picking.id,
- 'default_location_id': default_location_id,
- 'default_location_dest_id': default_location_dest_id,
- 'from_ui': False,
- })
+ ### ============= SRT dari BU/OUT ==================
+ srt_return_lines = []
+ for prod in mapping_koli.mapped('product_id'):
+ qty_total_return = sum(mk.qty_return for mk in mapping_koli.filtered(lambda m: m.product_id == prod))
+ move = bu_out.move_lines.filtered(lambda m: m.product_id == prod)
+ if not move:
+ raise UserError(f"Tidak ditemukan move BU/OUT untuk product {prod.display_name}")
+ srt_return_lines.append((0, 0, {
+ 'product_id': prod.id,
+ 'quantity': qty_total_return,
+ 'move_id': move[0].id,
+ }))
- return_wizard = self.env['stock.return.picking'].with_context(return_context).create({
- 'picking_id': picking.id,
- 'location_id': default_location_dest_id,
- 'original_location_id': default_location_id
+ srt_picking = None
+ if srt_return_lines:
+ srt_context = {
+ 'active_id': bu_out.id,
+ 'default_location_id': PARTNER_LOCATION_ID,
+ 'default_location_dest_id': BU_OUTPUT_LOCATION_ID,
+ 'from_ui': False,
+ }
+ srt_wizard = self.env['stock.return.picking'].with_context(srt_context).create({
+ 'picking_id': bu_out.id,
+ 'location_id': BU_OUTPUT_LOCATION_ID,
+ 'original_location_id': PARTNER_LOCATION_ID,
+ 'product_return_moves': srt_return_lines
})
-
- # 🔥 If BU/OUT, ambil qty dari mapping koli (group by product)
- return_lines = []
- if picking.picking_type_id.id == 29 and record.mapping_koli_ids:
- grouped_qty = {}
- for map_line in record.mapping_koli_ids:
- pid = map_line.product_id.id
- grouped_qty[pid] = grouped_qty.get(pid, 0) + map_line.qty_return
-
- for pid, qty in grouped_qty.items():
- move = picking.move_lines.filtered(lambda mv: mv.product_id.id == pid)
- if move:
- return_lines.append((0, 0, {
- 'product_id': pid,
- 'quantity': qty,
- 'move_id': move[0].id,
- }))
- else:
- raise UserError(_("Tidak ditemukan move line di picking %s untuk produk %s")
- % (picking.name, map_line.product_id.display_name))
- else:
- # Default kalau bukan BU/OUT (misalnya ORT), retur full qty done
- for move in picking.move_lines:
- return_lines.append((0, 0, {
- 'product_id': move.product_id.id,
- 'quantity': move.quantity_done or move.product_uom_qty,
- 'move_id': move.id,
- }))
-
- if not return_lines:
- raise UserError(_("Tidak ada product line valid untuk retur picking %s") % picking.name)
-
- return_wizard.product_return_moves = return_lines
- return_vals = return_wizard.create_returns()
- return_picking = self.env['stock.picking'].browse(return_vals.get('res_id'))
-
- if not return_picking:
- raise UserError("Retur gagal dibuat. Hasil create_returns: %s" % str(return_vals))
-
- return_picking.write({
- 'location_id': default_location_id,
- 'location_dest_id': default_location_dest_id,
- 'group_id': grup.id,
+ srt_vals = srt_wizard.create_returns()
+ srt_picking = self.env['stock.picking'].browse(srt_vals.get('res_id'))
+ srt_picking.write({
+ 'location_id': PARTNER_LOCATION_ID,
+ 'location_dest_id': BU_OUTPUT_LOCATION_ID,
+ 'group_id': bu_out.group_id.id,
'tukar_guling_id': record.id,
})
-
- for move in return_picking.move_lines:
- move.write({
- 'location_id': default_location_id,
- 'location_dest_id': default_location_dest_id,
+ created_returns.append(srt_picking)
+
+ ### ============= ORT dari BU/PICK ==================
+ ort_pickings = []
+ for pick in mapping_koli.mapped('pick_id'):
+ ort_return_lines = []
+ pick_lines = mapping_koli.filtered(lambda m: m.pick_id == pick)
+ for mk in pick_lines:
+ move = pick.move_lines.filtered(lambda m: m.product_id == mk.product_id)
+ if not move:
+ raise UserError(
+ f"Tidak ditemukan move di BU/PICK {pick.name} untuk {mk.product_id.display_name}")
+ ort_return_lines.append((0, 0, {
+ 'product_id': mk.product_id.id,
+ 'quantity': mk.qty_return,
+ 'move_id': move[0].id
+ }))
+ if ort_return_lines:
+ ort_context = {
+ 'active_id': pick.id,
+ 'default_location_id': BU_STOCK_LOCATION_ID,
+ 'default_location_dest_id': BU_OUTPUT_LOCATION_ID,
+ 'from_ui': False,
+ }
+ ort_wizard = self.env['stock.return.picking'].with_context(ort_context).create({
+ 'picking_id': pick.id,
+ 'location_id': BU_OUTPUT_LOCATION_ID,
+ 'original_location_id': BU_STOCK_LOCATION_ID,
+ 'product_return_moves': ort_return_lines
})
-
- return return_picking
-
- # === FLOW ===
- srt = _create_return_from_picking_grouped(bu_out_to_return)
- if srt:
- created_returns.append(srt)
-
- for picking in bu_pick_to_return:
- ort = _create_return_from_picking_grouped(picking)
- if ort:
- created_returns.append(ort)
- if record.return_type == 'tukar_guling':
- bu_pick = _create_return_from_picking_grouped(ort)
- if bu_pick:
- created_returns.append(bu_pick)
-
- if record.return_type == 'tukar_guling' and srt:
- bu_out = _create_return_from_picking_grouped(srt)
- if bu_out:
- created_returns.append(bu_out)
+ ort_vals = ort_wizard.create_returns()
+ ort_picking = self.env['stock.picking'].browse(ort_vals.get('res_id'))
+ ort_picking.write({
+ 'location_id': BU_STOCK_LOCATION_ID,
+ 'location_dest_id': BU_OUTPUT_LOCATION_ID,
+ 'group_id': bu_out.group_id.id,
+ 'tukar_guling_id': record.id,
+ })
+ ort_pickings.append(ort_picking)
+ created_returns.append(ort_picking)
+
+ ### ============= BU/PICK & BU/OUT baru (tukar guling) ==============
+ if record.return_type == 'tukar_guling':
+ # Dari SRT → BU/OUT baru
+ if srt_picking:
+ bu_out_new = self.env['stock.return.picking'].with_context({
+ 'active_id': srt_picking.id,
+ 'default_location_id': BU_OUTPUT_LOCATION_ID,
+ 'default_location_dest_id': PARTNER_LOCATION_ID,
+ 'from_ui': False,
+ }).create({
+ 'picking_id': srt_picking.id,
+ 'location_id': PARTNER_LOCATION_ID,
+ 'original_location_id': BU_OUTPUT_LOCATION_ID
+ }).create_returns()
+ new_out = self.env['stock.picking'].browse(bu_out_new.get('res_id'))
+ new_out.write({
+ 'location_id': BU_OUTPUT_LOCATION_ID,
+ 'location_dest_id': PARTNER_LOCATION_ID,
+ 'group_id': bu_out.group_id.id,
+ 'tukar_guling_id': record.id,
+ })
+ created_returns.append(new_out)
+
+ # Dari ORT → BU/PICK baru
+ for ort_p in ort_pickings:
+ bu_pick_new = self.env['stock.return.picking'].with_context({
+ 'active_id': ort_p.id,
+ 'default_location_id': BU_OUTPUT_LOCATION_ID,
+ 'default_location_dest_id': BU_STOCK_LOCATION_ID,
+ 'from_ui': False,
+ }).create({
+ 'picking_id': ort_p.id,
+ 'location_id': BU_STOCK_LOCATION_ID,
+ 'original_location_id': BU_OUTPUT_LOCATION_ID
+ }).create_returns()
+ new_pick = self.env['stock.picking'].browse(bu_pick_new.get('res_id'))
+ new_pick.write({
+ 'location_id': BU_OUTPUT_LOCATION_ID,
+ 'location_dest_id': BU_STOCK_LOCATION_ID,
+ 'group_id': bu_out.group_id.id,
+ 'tukar_guling_id': record.id,
+ })
+ created_returns.append(new_pick)
if not created_returns:
- raise UserError("Tidak ada dokumen retur yang berhasil dibuat.")
+ raise UserError("Tidak ada dokumen retur berhasil dibuat.")
+ _logger.info("✅ Created %s returns: %s", len(created_returns), ", ".join([p.name for p in created_returns]))
class TukarGulingLine(models.Model):