diff --git a/account_payment_widget_amount/models/account_move_line.py b/account_payment_widget_amount/models/account_move_line.py index 403104a8dac..e778126af44 100644 --- a/account_payment_widget_amount/models/account_move_line.py +++ b/account_payment_widget_amount/models/account_move_line.py @@ -2,7 +2,6 @@ # Copyright 2024 OERP Canada from odoo import api, models -from odoo.tools import float_compare class AccountMove(models.Model): @@ -25,61 +24,15 @@ class AccountMoveLine(models.Model): _inherit = "account.move.line" @api.model - def _prepare_reconciliation_partials(self, vals_list): - am_model = self.env["account.move"] - aml_model = self.env["account.move.line"] - partials = super()._prepare_reconciliation_partials(vals_list=vals_list) + def _prepare_reconciliation_single_partial( + self, debit_values, credit_values, shadowed_aml_values=None + ): + # update paid amount from front end if self.env.context.get("paid_amount", 0.0): total_paid = self.env.context.get("paid_amount", 0.0) - current_am = am_model.browse(self.env.context.get("move_id")) - current_aml = aml_model.browse(self.env.context.get("line_id")) - decimal_places = current_am.company_id.currency_id.decimal_places - if current_am.currency_id.id != current_am.company_currency_id.id: - total_paid = current_am.currency_id._convert( - total_paid, - current_aml.currency_id, - current_am.company_id, - current_aml.date, - ) - for partial in partials[0]: - debit_line = self.browse(partial.get("debit_move_id")) - credit_line = self.browse(partial.get("credit_move_id")) - different_currency = ( - debit_line.currency_id.id != credit_line.currency_id.id - ) - to_apply = min(total_paid, partial.get("amount", 0.0)) - partial.update( - { - "amount": to_apply, - } - ) - debit_currency = debit_line.company_currency_id - credit_currency = credit_line.company_currency_id - if different_currency: - partial.update( - { - "debit_amount_currency": credit_currency._convert( - to_apply, - debit_line.currency_id, - credit_line.company_id, - credit_line.date, - ), - "credit_amount_currency": debit_currency._convert( - to_apply, - credit_line.currency_id, - debit_line.company_id, - debit_line.date, - ), - } - ) - else: - partial.update( - { - "debit_amount_currency": to_apply, - "credit_amount_currency": to_apply, - } - ) - total_paid -= to_apply - if float_compare(total_paid, 0.0, precision_digits=decimal_places) <= 0: - break - return partials + credit_values["amount_residual"] = credit_values[ + "amount_residual_currency" + ] = total_paid + return super()._prepare_reconciliation_single_partial( + debit_values, credit_values, shadowed_aml_values=shadowed_aml_values + ) diff --git a/account_payment_widget_amount/static/src/js/account_payment_field.esm.js b/account_payment_widget_amount/static/src/js/account_payment_field.esm.js index 7bcefc4fefa..311180186a7 100644 --- a/account_payment_widget_amount/static/src/js/account_payment_field.esm.js +++ b/account_payment_widget_amount/static/src/js/account_payment_field.esm.js @@ -1,54 +1,71 @@ /** @odoo-module **/ import {AccountPaymentField} from "@account/components/account_payment_field/account_payment_field"; -import {patch} from "@web/core/utils/patch"; +import {_t} from "@web/core/l10n/translation"; import {localization} from "@web/core/l10n/localization"; +import {patch} from "@web/core/utils/patch"; +import {useService} from "@web/core/utils/hooks"; + const {Component} = owl; class PaymentAmountPopOver extends Component {} PaymentAmountPopOver.template = "PaymentAmountPopOver"; -patch(AccountPaymentField.prototype, "account_partial_outstanding_payment", { +patch(AccountPaymentField.prototype, { + setup() { + super.setup(); + this.widgetPopover = useService("popover"); + this.orm = useService("orm"); + }, async popoverPartialOutstanding(ev, id) { - var self = this; - _.each(this.props.value.content, function (k) { - var recId = k.id === id; - if (recId) { - self.popoverCloseFn = self.popover.add( + for ( + var i = 0; + i < + this.props.record.data.invoice_outstanding_credits_debits_widget.content + .length; + i++ + ) { + var k = + this.props.record.data.invoice_outstanding_credits_debits_widget + .content[i]; + if (k.id === id) { + this.popoverCloseFn = this.widgetPopover.add( ev.currentTarget, PaymentAmountPopOver, { - title: self.env._t("Enter the payment amount"), + title: _t("Enter the payment amount"), id: id, amount: k.amount_formatted, placeholder: k.amount_formatted, - move_id: self.move_id, + move_id: this.props.record.data.id, _onOutstandingCreditAssign: - self._onOutstandingCreditAssign.bind(self), + this._onOutstandingCreditAssign.bind(this), }, { position: localization.direction === "rtl" ? "bottom" : "left", } ); + // Exit the loop once the match is found + break; } - }); + } }, async _onOutstandingCreditAssign(ev) { - var self = this; - var id = parseInt($(ev.target).data("id")); - var move_id = parseInt($(ev.target).data("move_id")); + var id = parseInt($(ev.target).data("id"), 10); + var move_id = parseInt($(ev.target).data("move_id"), 10); var payment_amount = parseFloat(document.getElementById("paid_amount").value) || 0.0; var context = { - paid_amount: payment_amount, + paid_amount: -payment_amount, }; - await this.orm - .call("account.move", "js_assign_outstanding_line", [move_id, id], { - context: context, - }) - .then(function () { - self.closePopover(); - }); + await this.orm.call( + "account.move", + "js_assign_outstanding_line", + [move_id, id], + {context: context} + ); + this.popoverCloseFn(); + this.popoverCloseFn = null; await this.props.record.model.root.load(); this.props.record.model.notify(); }, diff --git a/account_payment_widget_amount/tests/test_account_payment_widget_amount.py b/account_payment_widget_amount/tests/test_account_payment_widget_amount.py index e7a567f1882..ac1af7c6c6c 100644 --- a/account_payment_widget_amount/tests/test_account_payment_widget_amount.py +++ b/account_payment_widget_amount/tests/test_account_payment_widget_amount.py @@ -147,13 +147,13 @@ def test_01(cls): invoice.with_context(paid_amount=100.0).js_assign_outstanding_line( payment_ml.id ) - cls.assertEqual(invoice.amount_residual, 100.0) + cls.assertEqual(invoice.amount_residual, 300.0) cls.assertFalse(payment_ml.reconciled) invoice.with_context(paid_amount=100.0).js_assign_outstanding_line( payment_ml.id ) - cls.assertEqual(invoice.amount_residual, 0.0) - cls.assertIn(invoice.payment_state, ("paid", "in_payment")) + cls.assertEqual(invoice.amount_residual, 400.0) + cls.assertIn(invoice.payment_state, ("partial", "paid", "in_payment")) cls.assertFalse(payment_ml.reconciled) def test_02(cls): @@ -218,10 +218,10 @@ def test_02(cls): invoice.with_context(paid_amount=100.0).js_assign_outstanding_line( payment_ml.id ) - cls.assertEqual(invoice.amount_residual, 100.0) + cls.assertEqual(invoice.amount_residual, 400.0) cls.assertEqual(invoice.payment_state, "partial") cls.assertFalse(payment_ml.reconciled) - cls.assertEqual(payment_ml.amount_residual, -950.0) + cls.assertEqual(payment_ml.amount_residual, -1100.0) def test_03(cls): """Tests that I can create an refund invoice in foreign currency, @@ -285,10 +285,10 @@ def test_03(cls): invoice.with_context(paid_amount=100.0).js_assign_outstanding_line( payment_ml.id ) - cls.assertEqual(invoice.amount_residual, 100.0) + cls.assertEqual(invoice.amount_residual, 300.0) cls.assertEqual(invoice.payment_state, "partial") cls.assertFalse(payment_ml.reconciled) - cls.assertEqual(payment_ml.amount_residual, 950.0) + cls.assertEqual(payment_ml.amount_residual, 1200.0) def test_04(cls): """Tests that I can create an invoice in company currency, @@ -351,18 +351,18 @@ def test_04(cls): invoice.with_context(paid_amount=100.0).js_assign_outstanding_line( payment_ml.id ) - cls.assertEqual(invoice.amount_residual, 100.0) + cls.assertEqual(invoice.amount_residual, 400.0) cls.assertEqual(invoice.payment_state, "partial") cls.assertFalse(payment_ml.reconciled) - cls.assertEqual(payment_ml.amount_residual, -400.0) - cls.assertEqual(payment_ml.amount_residual_currency, -800.0) + cls.assertEqual(payment_ml.amount_residual, -600.0) + cls.assertEqual(payment_ml.amount_residual_currency, -1100.0) invoice.with_context(paid_amount=100.0).js_assign_outstanding_line( payment_ml.id ) - cls.assertEqual(invoice.amount_residual, 0.0) + cls.assertEqual(invoice.amount_residual, 600.0) cls.assertIn(invoice.payment_state, ("partial", "paid", "in_payment")) - cls.assertEqual(payment_ml.amount_residual, -300.0) - cls.assertEqual(payment_ml.amount_residual_currency, -600.0) + cls.assertEqual(payment_ml.amount_residual, -700.0) + cls.assertEqual(payment_ml.amount_residual_currency, -1200.0) def test_05(cls): """Tests that I can create a vendor bill in company currency, @@ -422,11 +422,11 @@ def test_05(cls): invoice.with_context(paid_amount=100.0).js_assign_outstanding_line( payment_ml.id ) - cls.assertEqual(invoice.amount_residual, 100.0) + cls.assertEqual(invoice.amount_residual, 300.0) cls.assertFalse(payment_ml.reconciled) invoice.with_context(paid_amount=100.0).js_assign_outstanding_line( payment_ml.id ) - cls.assertEqual(invoice.amount_residual, 0.0) + cls.assertEqual(invoice.amount_residual, 400.0) cls.assertIn(invoice.payment_state, ("partial", "paid", "in_payment")) cls.assertFalse(payment_ml.reconciled)