diff --git a/account_internal_transfer/models/account_payment.py b/account_internal_transfer/models/account_payment.py index d72f825f1..854584cbb 100644 --- a/account_internal_transfer/models/account_payment.py +++ b/account_internal_transfer/models/account_payment.py @@ -29,6 +29,7 @@ class AccountPayment(models.Model): compute="_compute_main_company", ) available_partner_bank_ids = fields.Many2many(compute_sudo=True) + show_warning = fields.Html(compute="_compute_show_warning") @api.depends("company_id", "is_internal_transfer") def _compute_destination_company_id(self): @@ -194,3 +195,98 @@ def _compute_partner_id(self): super()._compute_partner_id() for pay in self.filtered("is_internal_transfer"): pay.partner_id = False + + def _prepare_move_line_default_vals(self, write_off_line_vals=None, force_balance=None): + # Call the parent method to get the default move line values + line_vals_list = super()._prepare_move_line_default_vals( + write_off_line_vals=write_off_line_vals, force_balance=force_balance + ) + # If the destination journal uses a different currency than the company and this is not a paired internal transfer + # (to avoid conversion in the entries of new payment, that should be in secondary currency) + if ( + # This is to avoid dependency on payment_pro + "amount_company_currency" in self._fields + and self.is_internal_transfer + and self.destination_journal_id.currency_id != self.company_id.currency_id + and not self.paired_internal_transfer_payment_id + ): + for line_vals in line_vals_list: + if "amount_currency" in line_vals: + # Set the currency to the company's currency + line_vals["currency_id"] = self.company_id.currency_id.id + # Adjust the amount_currency based on whether it's a debit or credit + if line_vals["debit"] > 0: + line_vals["amount_currency"] = self.amount_company_currency + elif line_vals["credit"] > 0: + line_vals["amount_currency"] = -self.amount_company_currency + else: + # When both debit and credit are zero, ensure amount_currency is neutral + line_vals["amount_currency"] = 0.0 + return line_vals_list + + def write(self, vals): + res = super().write(vals) + # Avoid recursion when updating paired payments + if self.env.context.get('skip_paired_payment_update'): + return res + + # Update paired payment when amount or journal changes + for payment in self.filtered(lambda p: p.is_internal_transfer and p.paired_internal_transfer_payment_id): + paired_payment = payment.paired_internal_transfer_payment_id + updates = {} + + # Sync amount + if 'amount' in vals and payment.amount != paired_payment.amount: + updates['amount'] = payment.amount + + # Sync journals (swapped relationship) + if 'journal_id' in vals and payment.journal_id != paired_payment.destination_journal_id: + updates['destination_journal_id'] = payment.journal_id.id + + if 'destination_journal_id' in vals and payment.destination_journal_id != paired_payment.journal_id: + updates['journal_id'] = payment.destination_journal_id.id + + # Apply updates to paired payment + if updates: + paired_payment.with_context(skip_paired_payment_update=True).write(updates) + + return res + + @api.depends("state", "paired_internal_transfer_payment_id.state") + def _compute_show_warning(self): + for pay in self: + paired_pay = pay.paired_internal_transfer_payment_id + if pay.is_internal_transfer and paired_pay and paired_pay.state != pay.state: + state_labels = dict(pay._fields['state']._description_selection(pay.env)) + base_url = pay.env['ir.config_parameter'].sudo().get_param('web.base.url') + action_url = f"{base_url}/web#id={paired_pay.id}&model=account.payment&view_type=form" + pay.show_warning = _( + '', + state_labels.get(pay.state, pay.state), + state_labels.get(paired_pay.state, paired_pay.state), + action_url + ) + else: + pay.show_warning = "" + + def action_open_paired_payment(self): + """Navigate to the paired internal transfer payment.""" + self.ensure_one() + if not self.paired_internal_transfer_payment_id: + return + + return { + 'name': _('Paired Payment'), + 'type': 'ir.actions.act_window', + 'res_model': 'account.payment', + 'view_mode': 'form', + 'res_id': self.paired_internal_transfer_payment_id.id, + 'target': 'current', + } diff --git a/account_internal_transfer/views/account_payment_views.xml b/account_internal_transfer/views/account_payment_views.xml index 6063297f0..c478f20dd 100644 --- a/account_internal_transfer/views/account_payment_views.xml +++ b/account_internal_transfer/views/account_payment_views.xml @@ -26,6 +26,9 @@ account.payment + + +