Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] internal_transfer_with_agreed_amount: modify write vals in one method i#24002 #1631

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ def _get_currency(self):
payment = self.env["account.payment"].browse(active_ids)
return payment.destination_journal_id.currency_id or payment.company_currency_id

def _validate_currencies_in_internal_transfer(self, payment):
def _get_currencies(self, payment):
payment_currency = payment.currency_id
company_currency = payment.company_currency_id
send_currency = payment.journal_id.currency_id or company_currency
receive_currency = self.currency_id
currencies = payment_currency | company_currency | send_currency | receive_currency
return currencies

def _validate_currencies_in_internal_transfer(self, payment):
currencies = self._get_currencies(payment)
if len(currencies) == 2:
return currencies - payment_currency
return currencies - payment.currency_id
raise ValidationError(
_(
"Agreed amount is available when there are two considered currencies. "
Expand All @@ -37,13 +41,23 @@ def _validate_currencies_in_internal_transfer(self, payment):
)
)

def _prepare_values_from_lines(self, lines, currency):
def _prepare_values_from_lines(self, lines, currency=None, is_same_company_currency=False):
line = lines[0]
if is_same_company_currency:
return {"debit": self.agreed_amount} if line.debit > 0 else {"credit": self.agreed_amount}
vals = {"debit": line.debit} if line.amount_currency > 0 else {"credit": line.credit}
sign = 1 if line.amount_currency > 0 else -1
vals.update({"amount_currency": sign * self.agreed_amount, "currency_id": currency.id})
return vals

def _preprocess_payments_hooks(self, payment):
"""This method is to be inherited in case pre-processing is required on the payments."""
return payment

def _postprocess_payments_hooks(self, payment):
"""This method is to be inherited in case post-processing is required on the payments."""
return payment

def apply(self):
active_ids = self._context.get("active_ids")
active_model = self._context.get("active_model")
Expand All @@ -58,14 +72,19 @@ def apply(self):
to_reconcile = amls.filtered(
lambda l: l.account_id == l.company_id.transfer_account_id
).full_reconcile_id.reconciled_line_ids
context = {"check_move_validity": False, "skip_account_move_synchronization": True}
if exchange_currency == payment.company_currency_id:
if len(amls) != 4:
return
amls.move_id.button_draft()
self._preprocess_payments_hooks(payment)
debit = amls.filtered(lambda x: x.debit > 0)
credit = amls.filtered(lambda x: x.credit > 0)
debit.with_context(check_move_validity=False).write({"debit": self.agreed_amount})
credit.with_context(check_move_validity=False).write({"credit": self.agreed_amount})
vals = self._prepare_values_from_lines(debit, is_same_company_currency=True)
debit.with_context(**context).write(vals)
vals = self._prepare_values_from_lines(credit, is_same_company_currency=True)
credit.with_context(**context).write(vals)
self._postprocess_payments_hooks(payment)
amls.move_id.action_post()
to_reconcile.reconcile()
return
Copy link
Contributor

@rolandojduartem rolandojduartem Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, you have to call the method before the return too self._postprocessing_payment_with_agreed_amount(payment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

Expand All @@ -74,10 +93,11 @@ def apply(self):
if len(aml_positive) > 2 or len(aml_negative) > 2:
return
aml_positive.move_id.button_draft()
context = {"check_move_validity": False, "skip_account_move_synchronization": True}
self._preprocess_payments_hooks(payment)
vals = self._prepare_values_from_lines(aml_positive, exchange_currency)
aml_positive.with_context(**context).write(vals)
vals = self._prepare_values_from_lines(aml_negative, exchange_currency)
aml_negative.with_context(**context).write(vals)
self._postprocess_payments_hooks(payment)
aml_positive.move_id.action_post()
to_reconcile.reconcile()