diff options
Diffstat (limited to 'indoteknik_custom/models')
| -rw-r--r-- | indoteknik_custom/models/refund_sale_order.py | 67 | ||||
| -rwxr-xr-x | indoteknik_custom/models/sale_order.py | 8 |
2 files changed, 54 insertions, 21 deletions
diff --git a/indoteknik_custom/models/refund_sale_order.py b/indoteknik_custom/models/refund_sale_order.py index 11bfd07f..077809e9 100644 --- a/indoteknik_custom/models/refund_sale_order.py +++ b/indoteknik_custom/models/refund_sale_order.py @@ -124,7 +124,7 @@ class RefundSaleOrder(models.Model): self.env.user.has_group('indoteknik_custom.group_role_fat') or self.env.user.id not in allowed_user_ids ): - raise UserError("❌ Hanya user Sales dan Finance yang boleh membuat refund.") + raise UserError("❌ Hanya Sales dan Finance yang boleh membuat refund.") if vals.get('name', 'New') == 'New': @@ -150,13 +150,18 @@ class RefundSaleOrder(models.Model): refund_type = vals.get('refund_type') invoice_ids_data = vals.get('invoice_ids', []) invoice_ids = invoice_ids_data[0][2] if invoice_ids_data and invoice_ids_data[0][0] == 6 else [] - if invoice_ids and refund_type and refund_type not in ['uang', 'barang_kosong_sebagian', 'barang_kosong', 'retur_half']: raise UserError("Refund type Hanya Bisa Lebih Bayar, Barang Kosong Sebagian, atau Retur Sebagian jika ada invoice") - if not invoice_ids and refund_type and refund_type in ['uang', 'barang_kosong_sebagian', 'barang_kosong', 'retur_half']: + if not invoice_ids and refund_type and refund_type in ['uang', 'barang_kosong_sebagian', 'retur_half']: raise UserError("Refund type Lebih Bayar, Barang Kosong Sebagian, atau Retur Sebagian Hanya Bisa dipilih Jika Ada Invoice") + if refund_type in ['barang_kosong', 'barang_kosong_sebagian'] and so_ids: + sale_orders = self.env['sale.order'].browse(so_ids) + zero_delivery_lines = sale_orders.mapped('order_line').filtered(lambda l: l.qty_delivered == 0) + if not zero_delivery_lines: + raise UserError("❌ Tidak ada barang yang Tidak Terikirim di Sales Order yang dipilih.") + if not so_ids and refund_type != 'lainnya': raise ValidationError("Jika tidak ada Sales Order yang dipilih, maka Tipe Refund hanya boleh 'Lainnya'.") @@ -175,11 +180,18 @@ class RefundSaleOrder(models.Model): if refund_type == 'retur_half' and not invoice_ids: raise ValidationError(f"SO {', '.join(so.mapped('name'))} belum memiliki invoice untuk Retur Sebagian.") - total_invoice = sum(self.env['account.move'].browse(invoice_ids).mapped('amount_total')) if invoice_ids else 0.0 - uang_masuk = vals.get('uang_masuk', 0.0) + total_invoice = sum(self.env['account.move'].browse(invoice_ids).mapped('amount_total_signed')) if invoice_ids else 0.0 ongkir = vals.get('ongkir', 0.0) + vals['total_invoice'] = total_invoice pengurangan = total_invoice + ongkir + if refund_type == 'barang_kosong_sebagian' and so_ids: + sale_orders = self.env['sale.order'].browse(so_ids) + vals['uang_masuk'] = sum(sale_orders.mapped('amount_total')) + + + uang_masuk = vals.get('uang_masuk', 0.0) + if uang_masuk > pengurangan: vals['amount_refund'] = uang_masuk - pengurangan else: @@ -229,8 +241,16 @@ class RefundSaleOrder(models.Model): refund_type = vals.get('refund_type', rec.refund_type) + if refund_type in ['barang_kosong', 'barang_kosong_sebagian'] and sale_orders: + zero_delivery_lines = sale_orders.mapped('order_line').filtered(lambda l: l.qty_delivered == 0) + if not zero_delivery_lines: + raise UserError("❌ Tidak ada barang yang Tidak Terikirim di Sales Order yang dipilih.") + if not so_ids and refund_type != 'lainnya': raise ValidationError("Jika tidak ada Sales Order yang dipilih, maka Tipe Refund hanya boleh 'Lainnya'.") + + if refund_type == 'barang_kosong_sebagian' and sale_orders: + vals['uang_masuk'] = sum(sale_orders.mapped('amount_total')) invoice_ids = vals.get('invoice_ids', False) @@ -248,7 +268,7 @@ class RefundSaleOrder(models.Model): if invoice_ids and vals.get('refund_type', rec.refund_type) not in ['uang', 'barang_kosong_sebagian', 'barang_kosong', 'retur_half']: raise UserError("Refund type Hanya Bisa Lebih Bayar, Barang Kosong Sebagian, atau Retur Sebagian jika ada invoice") - if not invoice_ids and vals.get('refund_type', rec.refund_type) in ['uang', 'barang_kosong_sebagian', 'barang_kosong', 'retur_half']: + if not invoice_ids and vals.get('refund_type', rec.refund_type) in ['uang', 'barang_kosong_sebagian', 'retur_half']: raise UserError("Refund type Lebih Bayar, Barang Kosong Sebagian, atau Retur Sebagian Hanya Bisa dipilih Jika Ada Invoice") if refund_type in ['retur', 'retur_half'] and so_ids: @@ -266,7 +286,8 @@ class RefundSaleOrder(models.Model): raise ValidationError(f"SO {', '.join(so.mapped('name'))} belum memiliki invoice untuk retur sebagian.") if any(field in vals for field in ['uang_masuk', 'invoice_ids', 'ongkir', 'sale_order_ids']): - total_invoice = sum(self.env['account.move'].browse(invoice_ids).mapped('amount_total')) + total_invoice = sum(self.env['account.move'].browse(invoice_ids).mapped('amount_total_signed')) + vals['total_invoice'] = total_invoice uang_masuk = vals.get('uang_masuk', rec.uang_masuk) ongkir = vals.get('ongkir', rec.ongkir) @@ -279,10 +300,10 @@ class RefundSaleOrder(models.Model): return super().write(vals) - @api.depends('status_payment') + @api.depends('status_payment', 'status') def _compute_is_locked(self): for rec in self: - rec.is_locked = rec.status_payment in ['done', 'reject'] + rec.is_locked = rec.status_payment in ['done', 'reject'] or rec.status in ['pengajuan3', 'refund', 'reject'] @api.depends('sale_order_ids.name', 'invoice_ids.name') def _compute_order_invoice_names(self): @@ -325,7 +346,7 @@ class RefundSaleOrder(models.Model): lambda inv: inv.move_type in ['out_invoice', 'out_refund'] and inv.state != 'cancel' ) all_invoices |= valid_invoices - total_invoice += sum(valid_invoices.mapped('amount_total')) + total_invoice += sum(valid_invoices.mapped('amount_total_signed')) self.invoice_ids = all_invoices @@ -358,6 +379,9 @@ class RefundSaleOrder(models.Model): self.line_ids = line_vals + if self.refund_type == 'barang_kosong_sebagian' and self.sale_order_ids: + self.uang_masuk = sum(self.sale_order_ids.mapped('amount_total')) + sum(self.sale_order_ids.mapped('delivery_amt')) + elif self.refund_type in ['retur', 'retur_half'] and self.sale_order_ids: line_vals = [] StockPicking = self.env['stock.picking'] @@ -464,7 +488,7 @@ class RefundSaleOrder(models.Model): if self.refund_type not in ['uang', 'barang_kosong']: self.refund_type = False - self.total_invoice = sum(self.invoice_ids.mapped('amount_total')) + self.total_invoice = sum(self.invoice_ids.mapped('amount_total_signed')) def action_ask_approval(self): for rec in self: @@ -486,19 +510,19 @@ class RefundSaleOrder(models.Model): if not rec.status or rec.status == 'draft': rec.status = 'pengajuan1' - elif rec.status == 'pengajuan1' and self.env.user.id == 19: + elif rec.status == 'pengajuan1': rec.status = 'pengajuan2' rec.approved_by = f"{rec.approved_by}, {user_name}" if rec.approved_by else user_name rec.date_approved_sales = now rec.position_sales = 'Sales Manager' - elif rec.status == 'pengajuan2' and self.env.user.id == 688: + elif rec.status == 'pengajuan2': rec.status = 'pengajuan3' rec.approved_by = f"{rec.approved_by}, {user_name}" if rec.approved_by else user_name rec.date_approved_ar = now rec.position_ar = 'AR' - elif rec.status == 'pengajuan3' and self.env.user.id == 7: + elif rec.status == 'pengajuan3': rec.status = 'refund' rec.approved_by = f"{rec.approved_by}, {user_name}" if rec.approved_by else user_name rec.date_approved_pimpinan = now @@ -512,7 +536,7 @@ class RefundSaleOrder(models.Model): is_fat = self.env.user.has_group('indoteknik_custom.group_role_fat') allowed_user_ids = [19, 688, 7] for rec in self: - if self.user.id not in allowed_user_ids and not is_fat: + if self.env.uid not in allowed_user_ids and not is_fat: raise UserError("❌ Hanya user yang bersangkutan atau Finance (FAT) yang bisa melakukan penolakan.") if rec.status not in ['refund', 'reject']: rec.status = 'reject' @@ -528,7 +552,7 @@ class RefundSaleOrder(models.Model): is_fat = self.env.user.has_group('indoteknik_custom.group_role_fat') for rec in self: if not is_fat: - raise UserError("Hanya Finance yang dapat mengkonfirmasi refund.") + raise UserError("Hanya Finance yang dapat mengkonfirmasi pembayaran refund.") if rec.status_payment == 'pending': rec.status_payment = 'done' rec.refund_date = fields.Date.context_today(self) @@ -586,8 +610,8 @@ class RefundSaleOrder(models.Model): }) amount = refund.amount_refund - - second_account_id = 450 if has_invoice else 668 + # 450 Penerimaan Belum Teridentifikasi, 668 Penerimaan Belum Alokasi + second_account_id = 450 if refund.refund_type not in ['barang_kosong', 'barang_kosong_sebagian'] else 668 debit_line = { 'move_id': account_move.id, @@ -629,6 +653,13 @@ class RefundSaleOrder(models.Model): def action_open_journal_refund(self): self.ensure_one() + + is_fat = self.env.user.has_group('indoteknik_custom.group_role_fat') + allowed_user_ids = [19, 688, 7] + + if not is_fat and self.env.user.id not in allowed_user_ids: + raise UserError(_('Anda tidak memiliki akses untuk membuka Journal Refund.')) + if self.journal_refund_move_id: return { 'name': _('Journal Refund'), diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index 0acfa0b0..aa534d0c 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -3199,6 +3199,7 @@ class SaleOrder(models.Model): self.ensure_one() invoice_ids = self.invoice_ids.filtered(lambda inv: inv.state != 'cancel') + total_so = sum(self.mapped('amount_total')) return { 'name': 'Refund Sale Order', @@ -3209,7 +3210,7 @@ class SaleOrder(models.Model): 'context': { 'default_sale_order_ids': [(6, 0, [self.id])], 'default_invoice_ids': [(6, 0, invoice_ids.ids)], - 'default_uang_masuk': sum(invoice_ids.mapped('amount_total')) + (self.delivery_amt or 0.0) + 1000, + 'default_uang_masuk': total_so, 'default_ongkir': self.delivery_amt or 0.0, 'default_bank': '', # bisa isi default bank kalau mau 'default_account_name': '', @@ -3237,7 +3238,8 @@ class SaleOrder(models.Model): invoice_ids = self.mapped('invoice_ids').filtered(lambda inv: inv.state != 'cancel') delivery_total = sum(self.mapped('delivery_amt')) - total_invoice = sum(invoice_ids.mapped('amount_total')) + total_invoice = sum(invoice_ids.mapped('amount_total_signed')) + total_so = sum(self.mapped('amount_total')) return { 'type': 'ir.actions.act_window', @@ -3248,7 +3250,7 @@ class SaleOrder(models.Model): 'context': { 'default_sale_order_ids': [(6, 0, self.ids)], 'default_invoice_ids': [(6, 0, invoice_ids.ids)], - 'default_uang_masuk': total_invoice + delivery_total + 1000, + 'default_uang_masuk': total_so, 'default_ongkir': delivery_total, 'default_bank': '', 'default_account_name': '', |
