diff options
| author | Azka Nathan <darizkyfaz@gmail.com> | 2025-12-22 13:36:43 +0700 |
|---|---|---|
| committer | Azka Nathan <darizkyfaz@gmail.com> | 2025-12-22 13:36:43 +0700 |
| commit | 74bc0544d9789479f599e808930e01ecea7fbf22 (patch) | |
| tree | ef11ccc1620ab31ee1b560c8b16f0552041a7742 /fixco_custom/models/account_move_reversal.py | |
| parent | b03b5e7d3ab83db6f26610490fc2a4e7dc4d3d9c (diff) | |
push
Diffstat (limited to 'fixco_custom/models/account_move_reversal.py')
| -rw-r--r-- | fixco_custom/models/account_move_reversal.py | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/fixco_custom/models/account_move_reversal.py b/fixco_custom/models/account_move_reversal.py new file mode 100644 index 0000000..eac8660 --- /dev/null +++ b/fixco_custom/models/account_move_reversal.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- +from odoo import models, fields, api +from odoo.tools.translate import _ +from odoo.exceptions import UserError + + +class AccountMoveReversal(models.TransientModel): + _inherit = 'account.move.reversal' + + def reverse_moves(self): + self.ensure_one() + moves = self.move_ids + + if moves.move_type == 'entry' and moves.sale_id and moves.ref.startswith('UANG MUKA PENJUALAN') and self.env.user.id not in [9, 10, 15]: + raise UserError(_('Anda tidak memiliki akses untuk melakukan reverse Uang Muka Penjualan!')) + + # Create default values. + default_values_list = [] + for move in moves: + default_values_list.append(self._prepare_default_reversal(move)) + + batches = [ + [self.env['account.move'], [], True], # Moves to be cancelled by the reverses. + [self.env['account.move'], [], False], # Others. + ] + for move, default_vals in zip(moves, default_values_list): + is_auto_post = bool(default_vals.get('auto_post')) + is_cancel_needed = not is_auto_post and self.refund_method in ('cancel', 'modify') + batch_index = 0 if is_cancel_needed else 1 + batches[batch_index][0] |= move + batches[batch_index][1].append(default_vals) + + # Handle reverse method. + moves_to_redirect = self.env['account.move'] + for moves, default_values_list, is_cancel_needed in batches: + new_moves = moves._reverse_moves(default_values_list, cancel=is_cancel_needed) + + if self.refund_method == 'modify': + moves_vals_list = [] + for move in moves.with_context(include_business_fields=True): + moves_vals_list.append(move.copy_data({'date': self.date if self.date_mode == 'custom' else move.date})[0]) + new_moves = self.env['account.move'].create(moves_vals_list) + + moves_to_redirect |= new_moves + + self.new_move_ids = moves_to_redirect + + # Create action. + action = { + 'name': _('Reverse Moves'), + 'type': 'ir.actions.act_window', + 'res_model': 'account.move', + } + if len(moves_to_redirect) == 1: + action.update({ + 'view_mode': 'form', + 'res_id': moves_to_redirect.id, + 'context': {'default_move_type': moves_to_redirect.move_type}, + }) + else: + action.update({ + 'view_mode': 'tree,form', + 'domain': [('id', 'in', moves_to_redirect.ids)], + }) + if len(set(moves_to_redirect.mapped('move_type'))) == 1: + action['context'] = {'default_move_type': moves_to_redirect.mapped('move_type').pop()} + return action |
