Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 23 additions & 10 deletions sale_advance_payment/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,31 @@ class AccountMove(models.Model):

def _post(self, soft=True):
# Automatic reconciliation of payment when invoice confirmed.
res = super(AccountMove, self)._post(soft=soft)
res = super()._post(soft=soft)
for move in self:
sale_order = move.mapped("line_ids.sale_line_ids.order_id")
if (
not sale_order
or move.invoice_outstanding_credits_debits_widget is False
):
if not sale_order:
continue
json_invoice_outstanding_data = (
move.invoice_outstanding_credits_debits_widget.get("content", [])

# Get payment move IDs first
payment_move_ids = sale_order.account_payment_ids.move_id.ids
if not payment_move_ids:
continue

# Domain search (most efficient for large datasets)
advance_payment_lines = self.env["account.move.line"].search(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@ljsalvatierra-factorlibre For a better performance, do the search before the loop, then, filter records on payment_move_ids.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi! Thank you for your review! You mean like this?

def _post(self, soft=True):
        # Automatic reconciliation of payment when invoice confirmed.
        res = super()._post(soft=soft)
        sale_orders = self.mapped("line_ids.sale_line_ids.order_id")
        all_payment_move_ids = sale_orders.mapped("account_payment_ids.move_id").ids
        advance_payment_lines = self.env["account.move.line"].search(
            [
                ("move_id", "in", all_payment_move_ids),
                (
                    "account_id.account_type",
                    "in",
                    ("asset_receivable", "liability_payable"),
                ),
                ("reconciled", "=", False),
                ("parent_state", "=", "posted"),
            ]
        )
        for move in self:
            sale_order = move.mapped("line_ids.sale_line_ids.order_id")
            if not sale_order:
                continue

            payment_move_ids = sale_order.account_payment_ids.move_id.ids
            if not payment_move_ids:
                continue

            payment_lines = advance_payment_lines.filtered(
                lambda x: x.move_id.id in payment_move_ids
            )

            for line in payment_lines:
                move.js_assign_outstanding_line(line_id=line.id)
        return res

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @rousseldenis did you have time to review my proposed solution?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes!

[
("move_id", "in", payment_move_ids),
(
"account_id.account_type",
"in",
("asset_receivable", "liability_payable"),
),
("reconciled", "=", False),
("parent_state", "=", "posted"),
]
)
for data in json_invoice_outstanding_data:
if data.get("move_id") in sale_order.account_payment_ids.move_id.ids:
move.js_assign_outstanding_line(line_id=data.get("id"))

for line in advance_payment_lines:
move.js_assign_outstanding_line(line_id=line.id)
return res
64 changes: 54 additions & 10 deletions sale_advance_payment/tests/test_sale_advance_payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,34 @@ def test_01_sale_advance_payment(self):
invoice = self.sale_order_1._create_invoices()
invoice.action_post()

# Compare payments
# Verify that advance payments are reconciled with the invoice
rate = self.currency_rate.rate
payment_list = [100 * rate, 200, 250 * rate, 400]
payments = invoice.invoice_outstanding_credits_debits_widget
result = [d["amount"] for d in payments["content"]]
self.assertEqual(set(payment_list), set(result))
expected_payment_amounts = [100 * rate, 200, 250 * rate, 400]

# Get all payment lines that should have been reconciled
payment_lines = self.env["account.move.line"].search(
[
("move_id", "in", self.sale_order_1.account_payment_ids.move_id.ids),
(
"account_id.account_type",
"in",
("asset_receivable", "liability_payable"),
),
("parent_state", "=", "posted"),
]
)

# Check that all payments are reconciled
self.assertTrue(all(line.reconciled for line in payment_lines))

# Verify the payment amounts match
actual_payment_amounts = []
for line in payment_lines:
actual_payment_amounts.append(abs(line.balance))

actual_payment_amounts_sorted = sorted(actual_payment_amounts)
expected_payment_amounts_sorted = sorted(expected_payment_amounts)
self.assertEqual(actual_payment_amounts_sorted, expected_payment_amounts_sorted)

def test_02_residual_amount_with_invoice(self):
self.assertEqual(
Expand Down Expand Up @@ -476,12 +498,34 @@ def test_04_sale_advance_payment_multi_inv_validate_wiz(self):
self.assertEqual(invoice.state, "posted")
self.assertEqual(invoice.payment_state, "partial")

# Compare payments
# Verify that advance payments are reconciled with the invoice
rate = self.currency_rate.rate
payment_list = [100 * rate, 200, 250 * rate, 400]
payments = invoice.invoice_outstanding_credits_debits_widget
result = [d["amount"] for d in payments["content"]]
self.assertEqual(set(payment_list), set(result))
expected_payment_amounts = [100 * rate, 200, 250 * rate, 400]

# Get all payment lines that should have been reconciled
payment_lines = self.env["account.move.line"].search(
[
("move_id", "in", self.sale_order_1.account_payment_ids.move_id.ids),
(
"account_id.account_type",
"in",
("asset_receivable", "liability_payable"),
),
("parent_state", "=", "posted"),
]
)

# Check that all payments are reconciled
self.assertTrue(all(line.reconciled for line in payment_lines))

# Verify the payment amounts match
actual_payment_amounts = []
for line in payment_lines:
actual_payment_amounts.append(abs(line.balance))

actual_payment_amounts_sorted = sorted(actual_payment_amounts)
expected_payment_amounts_sorted = sorted(expected_payment_amounts)
self.assertEqual(actual_payment_amounts_sorted, expected_payment_amounts_sorted)

def test_05_residual_amount_credit_note(self):
self.sale_order_1.action_confirm()
Expand Down