From d18a5e4e79bad1193198160abeff73302767f509 Mon Sep 17 00:00:00 2001 From: Mqdd Date: Sun, 1 Mar 2026 22:59:52 +0700 Subject: fix reverse invoice (kelar) --- fixco_custom/__manifest__.py | 1 + fixco_custom/models/account_move_reversal.py | 2 + fixco_custom/models/stock_picking.py | 96 ++++++++++++++++-------- fixco_custom/views/account_move_reversal_wiz.xml | 15 ++++ 4 files changed, 83 insertions(+), 31 deletions(-) create mode 100644 fixco_custom/views/account_move_reversal_wiz.xml diff --git a/fixco_custom/__manifest__.py b/fixco_custom/__manifest__.py index b72622a..5d9a65a 100755 --- a/fixco_custom/__manifest__.py +++ b/fixco_custom/__manifest__.py @@ -58,6 +58,7 @@ 'views/stock_inventory.xml', 'views/kartu_stock.xml', 'views/account_lock_date.xml', + 'views/account_move_reversal_wiz.xml', ], 'demo': [], 'css': [], diff --git a/fixco_custom/models/account_move_reversal.py b/fixco_custom/models/account_move_reversal.py index c3cc5cf..128809f 100644 --- a/fixco_custom/models/account_move_reversal.py +++ b/fixco_custom/models/account_move_reversal.py @@ -6,6 +6,8 @@ from odoo.exceptions import UserError class AccountMoveReversal(models.TransientModel): _inherit = 'account.move.reversal' + + picking_id = fields.Many2one('stock.picking', string='Picking') def reverse_moves(self): self.ensure_one() diff --git a/fixco_custom/models/stock_picking.py b/fixco_custom/models/stock_picking.py index 1c90d51..449959b 100755 --- a/fixco_custom/models/stock_picking.py +++ b/fixco_custom/models/stock_picking.py @@ -247,43 +247,77 @@ class StockPicking(models.Model): return res def automatic_reversed_invoice(self): - origin = self.origin or '' - clean_origin = origin.replace('Return of ', '') + origin = self.origin or '' + clean_origin = origin.replace('Return of ', '') - return_picking = self.env['stock.picking'].search([ - ('name', '=', clean_origin), - ('state', '=', 'done') - ], limit=1) + return_picking = self.env['stock.picking'].search([ + ('name', '=', clean_origin), + ('state', '=', 'done') + ], limit=1) - if not return_picking: - return False + if not return_picking: + return False - account_move = self.env['account.move'].search([ - ('picking_id', '=', return_picking.id), - ('state', '=', 'posted') - ], limit=1) + account_move = self.env['account.move'].search([ + ('picking_id', '=', return_picking.id), + ('state', '=', 'posted') + ], limit=1) - if not account_move: - return False - - reversal = self.env['account.move.reversal'].create({ - 'move_ids': [(6, 0, account_move.ids)], - 'date_mode': 'custom', - 'date': fields.Date.context_today(self), - 'refund_method': 'refund', - 'reason': _('Auto reverse from return picking %s') % self.name, - 'company_id': account_move.company_id.id, - }) - - action = reversal.reverse_moves() - # Force write invoice date using date done SP - new_move = self.env['account.move'].browse(action.get('res_id')) - if new_move: - new_move.write({ - 'invoice_date': self.date_done, + if not account_move: + return False + + reversal = self.env['account.move.reversal'].create({ + 'move_ids': [(6, 0, account_move.ids)], + 'picking_id': self.id, + 'date_mode': 'custom', + 'date': fields.Date.context_today(self), + 'refund_method': 'refund', + 'reason': _('Auto reverse from return picking %s') % self.name, + 'company_id': account_move.company_id.id, }) - return action + action = reversal.reverse_moves() + new_move = self.env['account.move'].browse(action.get('res_id')) + + if new_move: + if new_move.state == 'posted': + new_move.button_draft() + + # skip move validity + new_move = new_move.with_context(check_move_validity=False) + + #Ambil data retur + retur_data = {} + for line in self.move_ids_without_package: + retur_data[line.product_id.id] = retur_data.get(line.product_id.id, 0) + line.quantity_done + + #Update Qty atau Unlink + for inv_line in new_move.invoice_line_ids: + p_id = inv_line.product_id.id + if p_id in retur_data and retur_data[p_id] > 0: + inv_line.write({'quantity': retur_data[p_id]}) + retur_data[p_id] = 0 + else: + inv_line.unlink() + + new_move._recompute_tax_lines() + + # Recompute baris piutang/hutang + # bakal nyesuain angka debit/kredit biar balance sama total invoice + new_move._recompute_payment_terms_lines() + + new_move._recompute_dynamic_lines(recompute_all_taxes=True) + + # invoice date ikutin date done stock picking + new_move.write({ + 'invoice_date': self.date_done.date(), + 'date': self.date_done.date() + }) + + # check validity diakhir + new_move.with_context(check_move_validity=True).action_post() + + return action diff --git a/fixco_custom/views/account_move_reversal_wiz.xml b/fixco_custom/views/account_move_reversal_wiz.xml new file mode 100644 index 0000000..c9aaae9 --- /dev/null +++ b/fixco_custom/views/account_move_reversal_wiz.xml @@ -0,0 +1,15 @@ + + + + + account.move.reversal.form + account.move.reversal + + + + + + + + + -- cgit v1.2.3