diff options
| author | Miqdad <ahmadmiqdad27@gmail.com> | 2025-07-14 16:34:59 +0700 |
|---|---|---|
| committer | Miqdad <ahmadmiqdad27@gmail.com> | 2025-07-14 16:34:59 +0700 |
| commit | f6229d1426fc0823e1b29721f7fbaaec285351ef (patch) | |
| tree | b57f099183e9c1ca5437474259989131e24498b9 | |
| parent | 6bd30cc76c8d822840a6063959bfe50c6625fd12 (diff) | |
<miqdad> fix bugs
| -rw-r--r-- | indoteknik_custom/models/tukar_guling.py | 110 |
1 files changed, 38 insertions, 72 deletions
diff --git a/indoteknik_custom/models/tukar_guling.py b/indoteknik_custom/models/tukar_guling.py index 5762abbb..24dbf3d8 100644 --- a/indoteknik_custom/models/tukar_guling.py +++ b/indoteknik_custom/models/tukar_guling.py @@ -267,8 +267,8 @@ class TukarGuling(models.Model): raise UserError("❌ Tidak bisa retur BU/PICK karena BU/OUT sudah done") if operasi == 30 and pp == 'tukar_guling': raise UserError("❌ BU/PICK tidak boleh di retur tukar guling") - else: - _logger.info("hehhe") + # else: + # _logger.info("hehhe") if 'operations' in vals and not vals.get('origin'): @@ -279,6 +279,8 @@ class TukarGuling(models.Model): return super(TukarGuling, self).write(vals) def unlink(self): + # if self.state == 'done': + # raise UserError ("Tidak Boleh delete ketika sudahh done") for record in self: if record.state == 'done': raise UserError( @@ -344,11 +346,10 @@ class TukarGuling(models.Model): operasi = self.operations.picking_type_id.id tipe = self.return_type - pp = vals.get('return_type', tipe) if operasi == 30 and self.operations.linked_manual_bu_out.state == 'done': raise UserError("❌ Tidak bisa retur BU/PICK karena BU/OUT sudah done") - if operasi == 30 and pp == 'tukar_guling': + if operasi == 30 and tipe == 'tukar_guling': raise UserError("❌ BU/PICK tidak boleh di retur tukar guling") else: _logger.info("hehhe") @@ -398,81 +399,52 @@ class TukarGuling(models.Model): if not record.operations: raise UserError("BU/OUT dari field operations tidak ditemukan.") - operation_picking = record.operations - - related_pickings = self.env['stock.picking'].search([ - ('origin', '=', record.origin), - ('state', '=', 'done'), - ('picking_type_id', 'in', [29, 30]) - ]) - if not related_pickings: - raise UserError( - "Tidak ditemukan BU/PICK atau BU/OUT dari SO: %s" % record.origin) - - # filter based on stock.picking picking type bu_pick_to_return = record.operations.konfirm_koli_lines.pick_id bu_out_to_return = record.operations - # if bu_pick_to_return and self.return_type == 'tukar_guling': - # raise UserError("BU/PICK tidak boleh di retur tukar guling") - 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.") created_returns = [] - # Lokasi default untuk retur + # Picking types & locations srt_type = self.env['stock.picking.type'].browse(73) ort_type = self.env['stock.picking.type'].browse(74) bu_pick_type = self.env['stock.picking.type'].browse(30) bu_out_type = self.env['stock.picking.type'].browse(29) - stock_location = self.env['stock.location'] - - srt_src = stock_location.browse(5) - srt_dest = stock_location.browse(60) - - ort_src = stock_location.browse(60) - ort_dest = stock_location.browse(57) - - if not ort_src or not ort_dest or not srt_src or not srt_dest: - raise UserError("salahwoi") - - # Fungsi membuat retur dari picking tertentu def _create_return_from_picking(picking): - grup = self.operations.group_id + if not picking: + return None + grup = record.operations.group_id PARTNER_LOCATION_ID = 5 BU_OUTPUT_LOCATION_ID = 60 BU_STOCK_LOCATION_ID = 57 - # Determine locations based on picking type if picking.picking_type_id.id == 30: # BU/PICK → ORT return_type = ort_type default_location_id = BU_OUTPUT_LOCATION_ID default_location_dest_id = BU_STOCK_LOCATION_ID - elif picking.picking_type_id.id == 74: # ORT → BU/PICK return_type = bu_pick_type default_location_id = BU_STOCK_LOCATION_ID default_location_dest_id = BU_OUTPUT_LOCATION_ID - elif picking.picking_type_id.id == 29: # BU/OUT → SRT return_type = srt_type default_location_id = PARTNER_LOCATION_ID default_location_dest_id = BU_OUTPUT_LOCATION_ID - elif picking.picking_type_id.id == 73: # SRT → BU/OUT return_type = bu_out_type default_location_id = BU_OUTPUT_LOCATION_ID default_location_dest_id = PARTNER_LOCATION_ID - else: return None + return_context = dict(self.env.context) return_context.update({ 'active_id': picking.id, @@ -487,26 +459,26 @@ class TukarGuling(models.Model): 'original_location_id': default_location_id }) - # Create return lines return_lines = [] + # 🔥 Hanya pakai qty dari tukar guling line for line in record.line_ids: - move = picking.move_lines.filtered(lambda wkwk: wkwk.product_id == line.product_id) + move = picking.move_lines.filtered(lambda m: m.product_id == line.product_id) if move: return_lines.append((0, 0, { 'product_id': line.product_id.id, 'quantity': line.product_uom_qty, - 'move_id': move.id, + 'move_id': move[0].id, })) - if not move: - raise UserError("eror woi") + else: + raise UserError( + _("Tidak ditemukan move line di picking %s untuk produk %s") + % (picking.name, line.product_id.display_name) + ) + if not return_lines: - return None + raise UserError(_("Tidak ada product line valid untuk retur picking %s") % picking.name) return_wizard.product_return_moves = return_lines - - _logger.info("Creating return for picking %s", picking.name) - _logger.info("Default location src: %s", default_location_id) - _logger.info("Default location dest: %s", default_location_dest_id) return_vals = return_wizard.create_returns() return_id = return_vals.get('res_id') return_picking = self.env['stock.picking'].browse(return_id) @@ -514,7 +486,6 @@ class TukarGuling(models.Model): if not return_picking: raise UserError("Retur gagal dibuat. Hasil create_returns: %s" % str(return_vals)) - # Force the destination location return_picking.write({ 'location_dest_id': default_location_dest_id, 'location_id': default_location_id, @@ -524,37 +495,32 @@ class TukarGuling(models.Model): return return_picking - # CASE: Kalau retur BU/PICK jadi ort saja - if record.operations.picking_type_id.id == 30: - ort = _create_return_from_picking(record.operations) + # === PERBAIKI URUTAN === + srt = _create_return_from_picking(bu_out_to_return) + if srt: + created_returns.append(srt) + + picks = self.env['stock.picking'].search([ + ('origin', '=', record.origin), + ('state', '=', 'done'), + ('picking_type_id', '=', 30) + ]) + for picking in picks: + ort = _create_return_from_picking(picking) if ort: created_returns.append(ort) - # Kalau retur BU/OUT - else: - # CASE: Retur dari BU/OUT - srt = _create_return_from_picking(bu_out_to_return) - if srt: - created_returns.append(srt) - - ort = None - if bu_pick_to_return: - ort = _create_return_from_picking(bu_pick_to_return) - if ort: - created_returns.append(ort) - - if record.return_type == 'tukar_guling': - if ort: + if record.return_type == 'tukar_guling': bu_pick = _create_return_from_picking(ort) if bu_pick: created_returns.append(bu_pick) - if srt: - bu_out = _create_return_from_picking(srt) - if bu_out: - created_returns.append(bu_out) + if record.return_type == 'tukar_guling' and srt: + bu_out = _create_return_from_picking(srt) + if bu_out: + created_returns.append(bu_out) if not created_returns: - raise UserError("wkwkwk") + raise UserError("Tidak ada dokumen retur yang berhasil dibuat.") class TukarGulingLine(models.Model): |
