| 1 |
|
| 2 |
|
| 3 |
from odoo import models, api, fields, _, Command |
| 4 |
from odoo.exceptions import UserError |
| 5 |
|
| 6 |
|
| 7 |
class L10n_LatamPaymentMassTransfer(models.TransientModel): |
| 8 |
_name = 'l10n_latam.payment.mass.transfer' |
| 9 |
_description = 'Checks Mass Transfers' |
| 10 |
_check_company_auto = True |
| 11 |
|
| 12 |
payment_date = fields.Date( |
| 13 |
string="Payment Date", |
| 14 |
required=True, |
| 15 |
default=fields.Date.context_today, |
| 16 |
) |
| 17 |
destination_journal_id = fields.Many2one( |
| 18 |
comodel_name='account.journal', |
| 19 |
string='Destination Journal', |
| 20 |
check_company=True, |
| 21 |
domain="[('type', 'in', ('bank', 'cash')), ('id', '!=', journal_id)]", |
| 22 |
) |
| 23 |
communication = fields.Char(string="Memo") |
| 24 |
journal_id = fields.Many2one( |
| 25 |
'account.journal', |
| 26 |
check_company=True, |
| 27 |
compute='_compute_journal_company' |
| 28 |
) |
| 29 |
company_id = fields.Many2one( |
| 30 |
'res.company', |
| 31 |
compute="_compute_journal_company" |
| 32 |
) |
| 33 |
check_ids = fields.Many2many( |
| 34 |
'l10n_latam.check', 'latam_tranfer_check_rel' |
| 35 |
'transfer_id', 'check_id', check_company=True, |
| 36 |
) |
| 37 |
|
| 38 |
@api.depends('check_ids') |
| 39 |
def _compute_journal_company(self): |
| 40 |
|
| 41 |
|
| 42 |
journal = self.check_ids._origin.mapped("current_journal_id") |
| 43 |
if len(journal) != 1: |
| 44 |
raise UserError(_("All selected checks must be on the same journal and on hand")) |
| 45 |
self.journal_id = journal |
| 46 |
self.company_id = journal.company_id.id |
| 47 |
|
| 48 |
@api.model |
| 49 |
def default_get(self, fields): |
| 50 |
res = super().default_get(fields) |
| 51 |
if 'check_ids' in fields and 'check_ids' not in res: |
| 52 |
if self.env.context.get('active_model') != 'l10n_latam.check': |
| 53 |
raise UserError(_("The register payment wizard should only be called on account.payment records.")) |
| 54 |
checks = self.env['l10n_latam.check'].browse(self.env.context.get('active_ids', [])) |
| 55 |
if checks.filtered(lambda x: x.payment_method_line_id.code != 'new_third_party_checks'): |
| 56 |
raise UserError(_('You have selected payments which are not checks. Please call this action from the Third Party Checks menu')) |
| 57 |
elif not all(check.payment_id.state not in ('draft', 'canceled') for check in checks): |
| 58 |
raise UserError(_("All the selected checks must be posted")) |
| 59 |
currency_ids = checks.mapped('currency_id') |
| 60 |
if any(x != currency_ids[0] for x in currency_ids): |
| 61 |
raise UserError(_("All the selected checks must use the same currency")) |
| 62 |
res['check_ids'] = checks.ids |
| 63 |
return res |
| 64 |
|
| 65 |
def _create_payments(self): |
| 66 |
""" This is nedeed because we would like to create a payment of type internal transfer for each check with the |
| 67 |
counterpart journal and then, when posting a second payment will be created automatically """ |
| 68 |
self.ensure_one() |
| 69 |
checks = self.check_ids.filtered(lambda x: x.payment_method_line_id.code == 'new_third_party_checks' and x.currency_id == self.check_ids[0].currency_id) |
| 70 |
currency_id = self.check_ids[0].currency_id |
| 71 |
|
| 72 |
pay_method_line = self.journal_id._get_available_payment_method_lines('outbound').filtered( |
| 73 |
lambda x: x.code in ('out_third_party_checks', 'return_third_party_checks') |
| 74 |
)[:1] |
| 75 |
|
| 76 |
outbound_payment = self.env['account.payment'].create({ |
| 77 |
'date': self.payment_date, |
| 78 |
'amount': sum(checks.mapped('amount')), |
| 79 |
'partner_id': self.env.company.partner_id.id, |
| 80 |
'payment_type': 'outbound', |
| 81 |
'memo': self.communication, |
| 82 |
'journal_id': self.journal_id.id, |
| 83 |
'currency_id': currency_id.id, |
| 84 |
'payment_method_line_id': pay_method_line.id if pay_method_line else False, |
| 85 |
'l10n_latam_move_check_ids': [Command.link(x.id) for x in checks], |
| 86 |
}) |
| 87 |
outbound_payment.action_post() |
| 88 |
|
| 89 |
inbound_payment = self.env['account.payment'].create({ |
| 90 |
'date': self.payment_date, |
| 91 |
'amount': sum(checks.mapped('amount')), |
| 92 |
'partner_id': self.env.company.partner_id.id, |
| 93 |
'payment_type': 'inbound', |
| 94 |
'memo': self.communication, |
| 95 |
'journal_id': self.destination_journal_id.id, |
| 96 |
'currency_id': currency_id.id, |
| 97 |
'l10n_latam_move_check_ids': [Command.link(x.id) for x in checks], |
| 98 |
}) |
| 99 |
|
| 100 |
dest_payment_method = self.destination_journal_id.inbound_payment_method_line_ids.filtered( |
| 101 |
lambda x: x.code == 'in_third_party_checks' |
| 102 |
) |
| 103 |
if dest_payment_method: |
| 104 |
inbound_payment.payment_method_line_id = dest_payment_method |
| 105 |
inbound_payment.action_post() |
| 106 |
else: |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
inbound_payment.with_context(l10n_ar_skip_remove_check=True).action_post() |
| 111 |
|
| 112 |
body_inbound = _("This payment has been created from: ") + outbound_payment._get_html_link() |
| 113 |
inbound_payment.message_post(body=body_inbound) |
| 114 |
body_outbound = _("A second payment has been created: ") + inbound_payment._get_html_link() |
| 115 |
outbound_payment.message_post(body=body_outbound) |
| 116 |
|
| 117 |
(outbound_payment.move_id.line_ids + inbound_payment.move_id.line_ids).filtered( |
| 118 |
lambda l: |
| 119 |
l.account_id == outbound_payment.destination_account_id and not l.reconciled |
| 120 |
).reconcile() |
| 121 |
|
| 122 |
return outbound_payment |
| 123 |
|
| 124 |
def action_create_payments(self): |
| 125 |
payments = self._create_payments() |
| 126 |
|
| 127 |
action = { |
| 128 |
'name': _('Payments'), |
| 129 |
'type': 'ir.actions.act_window', |
| 130 |
'res_model': 'account.payment', |
| 131 |
'context': {'create': False}, |
| 132 |
} |
| 133 |
if len(payments) == 1: |
| 134 |
action.update({ |
| 135 |
'view_mode': 'form', |
| 136 |
'res_id': payments.id, |
| 137 |
}) |
| 138 |
else: |
| 139 |
action.update({ |
| 140 |
'view_mode': 'list,form', |
| 141 |
'domain': [('id', 'in', payments.ids)], |
| 142 |
}) |
| 143 |
return action |