Debian Codegraph @

addons/l10n_latam_check/wizards/account_payment_register.py

raw
1 from odoo import models, fields, api, Command, _
2 from odoo.exceptions import ValidationError
3
4
5 class AccountPaymentRegister(models.TransientModel):
6 _inherit = 'account.payment.register'
7
8 l10n_latam_new_check_ids = fields.One2many('l10n_latam.payment.register.check', 'payment_register_id', string="New Checks")
9 l10n_latam_move_check_ids = fields.Many2many(
10 comodel_name='l10n_latam.check',
11 string='Checks',
12 )
13
14 @api.depends('l10n_latam_move_check_ids.amount', 'l10n_latam_new_check_ids.amount', 'payment_method_code')
15 def _compute_amount(self):
16 super()._compute_amount()
17 for wizard in self.filtered(lambda x: x._is_latam_check_payment(check_subtype='new_check')):
18 wizard.amount = sum(wizard.l10n_latam_new_check_ids.mapped('amount'))
19 for wizard in self.filtered(lambda x: x._is_latam_check_payment(check_subtype='move_check')):
20 wizard.amount = sum(wizard.l10n_latam_move_check_ids.mapped('amount'))
21
22 @api.depends('l10n_latam_move_check_ids.currency_id')
23 def _compute_currency_id(self):
24 super()._compute_currency_id()
25 for wizard in self.filtered(lambda x: x._is_latam_check_payment(check_subtype='move_check')):
26 if wizard.l10n_latam_move_check_ids:
27 wizard.currency_id = wizard.l10n_latam_move_check_ids[0].currency_id
28
29 def _is_latam_check_payment(self, check_subtype=False):
30 if check_subtype == 'move_check':
31 codes = ['in_third_party_checks', 'out_third_party_checks', 'return_third_party_checks']
32 elif check_subtype == 'new_check':
33 codes = ['new_third_party_checks', 'own_checks']
34 else:
35 codes = ['in_third_party_checks', 'out_third_party_checks', 'return_third_party_checks', 'new_third_party_checks', 'own_checks']
36 return self.payment_method_code in codes
37
38 def _create_payment_vals_from_wizard(self, batch_result):
39 vals = super()._create_payment_vals_from_wizard(batch_result)
40 if self.l10n_latam_new_check_ids:
41 vals.update({'l10n_latam_new_check_ids': [Command.create({
42 'name': x.name,
43 'bank_id': x.bank_id.id,
44 'issuer_vat': x.issuer_vat,
45 'payment_date': x.payment_date,
46 'amount': x.amount}) for x in self.l10n_latam_new_check_ids
47 ]})
48 if self.l10n_latam_move_check_ids:
49 vals.update({
50 'l10n_latam_move_check_ids': [Command.link(x.id) for x in self.l10n_latam_move_check_ids]
51 })
52 return vals
53
54 def action_create_payments(self):
55 if self._is_latam_check_payment(check_subtype="move_check"):
56 latam_check_currencies = self.l10n_latam_move_check_ids.mapped("currency_id")
57 if latam_check_currencies and (len(latam_check_currencies) > 1 or latam_check_currencies != self.currency_id):
58 raise ValidationError(_(
59 "You can't mix checks of different currencies in one payment, "
60 "and you can't change the payment's currency if checks are already created in that currency.\n"
61 "Please create separate payments for each currency."
62 ))
63 return super().action_create_payments()