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

fix: correct invoice order in payment reconcillaiton #46574

Merged
merged 1 commit into from
Mar 24, 2025

Conversation

ljain112
Copy link
Collaborator

Issue: When a query is executed without an ORDER BY clause, the data is sorted according to the primary key.
In a GROUP BY query, non-aggregate values are retrieved either randomly or from the first row of each aggregation (the exact behaviour is unclear). As a result, when retrieving outstanding invoices during payment reconciliation with limit, some invoices that have payments made on a later date do not appear because their posting dates are considered from the another payment ledger entry.

Steps to replicate:

  • Create a Sales Invoice with the posting date as 1/9/2024
  • Create a second sales invoice with the posting date as 1/10/2024
  • Create a Partial Payment against 1 invoice on 2/10/2024 (with payment ledger entry name as 1 so that it will be first in ascending order)
  • Now fetch Unreconciled Entries in Payment Reconciliation with invoice limit as 1.

2nd invoice will be fetched even though 1st invoice should be fetched because the first invoice's posting date will be from payment ledger entry of payment.

Query:

SELECT `against_voucher_no`,name,`amount_in_account_currency`,posting_date FROM `tabPayment Ledger Entry` WHERE `delinked`=0 AND `company`='ABC Ltd' AND `account_type`='Receivable' AND `account` IN ('1301 - Debtors - K') AND `party_type`='Customer' AND `party`='Registered'
+-----------------------+------------+----------------------------+--------------+
| against_voucher_no    | name       | amount_in_account_currency | posting_date |
+-----------------------+------------+----------------------------+--------------+
| ACC-SINV-2025-00034-1 | 03573      |           -20000.000000000 | 2025-03-26   |
| ACC-PAY-2025-00180    | 03574      |            20000.000000000 | 2025-03-18   |
| ACC-SINV-2025-00036   | 1oeddk6mok |           -50000.000000000 | 2025-03-17   |
| ACC-SINV-2025-00034-1 | 3plt76e1s8 |           348404.000000000 | 2024-09-01   |
| ACC-SINV-2025-00034-1 | 4jm3j8h1ib |           -20000.000000000 | 2025-03-17   |
| ACC-SINV-2025-00034-1 | 75qbtni7pn |           -20000.000000000 | 2025-03-18   |
| ACC-SINV-2025-00034-1 | 7ddqvelv59 |           -20000.000000000 | 2025-03-19   |
| ACC-SINV-2025-00034-1 | 7md88h2csr |           -20000.000000000 | 2025-03-19   |
| ACC-SINV-2025-00009   | 9ktanrhuq5 |          -314677.090000000 | 2025-02-28   |
| ACC-SINV-2025-00011   | 9l9nj060fo |           -52428.500000000 | 2025-02-28   |
| ACC-SINV-2025-00010   | 9majg9i9jm |          -126544.280000000 | 2025-02-28   |
| ACC-SINV-2025-00009   | t2ttd44fjc |           314677.090000000 | 2025-01-31   |
| ACC-SINV-2025-00010   | t6otqcv62m |           126544.280000000 | 2025-01-31   |
| ACC-SINV-2025-00011   | t9ma7jinr3 |            52428.500000000 | 2025-01-31   |
| ACC-SINV-2025-00036   | v4e25s0e9g |           295723.710000000 | 2025-02-28   |
| ACC-SINV-2025-00035   | v7i427iv7j |           126544.280000000 | 2025-02-28   |
+-----------------------+------------+----------------------------+--------------+

Before

SELECT `against_voucher_no` `voucher_no`,SUM(`amount_in_account_currency`) `amount_in_account_currency`,posting_date FROM `tabPayment Ledger Entry` WHERE `delinked`=0 AND `company`='ABC Ltd' AND `account_type`='Receivable' AND `account` IN ('1301 - Debtors - K') AND `party_type`='Customer' AND `par
ty`='Registered' GROUP BY `against_voucher_type`,`against_voucher_no`,`party_type`,`party` HAVING `amount_in_account_currency`>0 ORDER BY `posting_date`,`voucher_no`;
+-----------------------+----------------------------+--------------+
| voucher_no            | amount_in_account_currency | posting_date |
+-----------------------+----------------------------+--------------+
| ACC-SINV-2025-00035   |           126544.280000000 | 2025-02-28   |
| ACC-SINV-2025-00036   |           245723.710000000 | 2025-03-17   |
| ACC-PAY-2025-00180    |            20000.000000000 | 2025-03-18   |
| ACC-SINV-2025-00034-1 |           248404.000000000 | 2025-03-26   |
+-----------------------+----------------------------+--------------+

For voucher, ACC-SINV-2025-00034-1 posting date is 26-03-2025 even though voucher is created on 01-09-2024

After:

SELECT `against_voucher_no` `voucher_no`,SUM(`amount_in_account_currency`) `amount_in_account_currency`,MAX(CASE WHEN `voucher_no`=`against_voucher_no` AND `voucher_type`=`against_voucher_type` THEN `posting_date` END) `invoice_date` FROM `tabPayment Ledger Entry` WHERE `delinked`=0 AND `company`='ABC Ltd' AND `account_type`='Receivable' AND `account` IN ('1301 - Debtors - K') AND `party_type`='Customer' AND `party`='Registered' GROUP BY `against_voucher_type`,`against_voucher_no`,`party_type`,`party` HAVING `amount_in_account_currency`>0 ORDER BY `invoice_date`,`voucher_no`
+-----------------------+----------------------------+--------------+
| voucher_no            | amount_in_account_currency | invoice_date |
+-----------------------+----------------------------+--------------+
| ACC-SINV-2025-00034-1 |           248404.000000000 | 2024-09-01   |
| ACC-SINV-2025-00035   |           126544.280000000 | 2025-02-28   |
| ACC-SINV-2025-00036   |           245723.710000000 | 2025-02-28   |
| ACC-PAY-2025-00180    |            20000.000000000 | 2025-03-18   |
+-----------------------+----------------------------+--------------+

Frappe Support Issue: https://support.frappe.io/app/hd-ticket/33761

@ljain112 ljain112 requested a review from ruthra-kumar as a code owner March 18, 2025 07:22
@github-actions github-actions bot added accounts needs-tests This PR needs automated unit-tests. labels Mar 18, 2025
@ljain112 ljain112 changed the title fix: correct invoice order in payment reco fix: correct invoice order in payment reconcillaiton Mar 18, 2025
@ruthra-kumar ruthra-kumar self-assigned this Mar 18, 2025
@ruthra-kumar ruthra-kumar merged commit 8f647b4 into frappe:develop Mar 24, 2025
15 checks passed
ruthra-kumar added a commit that referenced this pull request Mar 24, 2025
…-46574

fix: correct invoice order in payment reconcillaiton (backport #46574)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accounts backport version-15-hotfix needs-tests This PR needs automated unit-tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants