1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# -*- 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'):
if not moves.approval_refund:
raise UserError(_('User harus ajukan approval refund!'))
if moves.approval_refund == 'rejected':
raise UserError(_('refund telah ditolak!'))
moves.approval_refund = 'approved'
# 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)
if new_moves.state != 'posted':
new_moves.action_post()
moves_to_redirect |= new_moves
move_line = self.env['account.move.line'].search([('move_id', '=', new_moves.id), ('account_id', '=', 347), ('journal_id', '=', 17)], limit=1)
if move_line and move_line.move_id.reversed_entry_id:
moves.js_assign_outstanding_line(move_line.id)
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
|