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: update payment entry references #33274

Merged
Merged
Show file tree
Hide file tree
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
44 changes: 44 additions & 0 deletions erpnext/accounts/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import frappe
from frappe.test_runner import make_test_objects

from erpnext.accounts.doctype.payment_entry.payment_entry import get_payment_entry
from erpnext.accounts.doctype.purchase_invoice.test_purchase_invoice import make_purchase_invoice
from erpnext.accounts.party import get_party_shipping_address
from erpnext.accounts.utils import (
get_future_stock_vouchers,
get_voucherwise_gl_entries,
sort_stock_vouchers_by_posting_date,
update_reference_in_payment_entry,
)
from erpnext.stock.doctype.item.test_item import make_item
from erpnext.stock.doctype.purchase_receipt.test_purchase_receipt import make_purchase_receipt
Expand Down Expand Up @@ -73,6 +76,47 @@ def test_stock_voucher_sorting(self):
sorted_vouchers = sort_stock_vouchers_by_posting_date(list(reversed(vouchers)))
self.assertEqual(sorted_vouchers, vouchers)

def test_update_reference_in_payment_entry(self):
item = make_item().name

purchase_invoice = make_purchase_invoice(
item=item, supplier="_Test Supplier USD", currency="USD", conversion_rate=82.32
)
purchase_invoice.submit()

payment_entry = get_payment_entry(purchase_invoice.doctype, purchase_invoice.name)
payment_entry.target_exchange_rate = 62.9
payment_entry.paid_amount = 15725
payment_entry.deductions = []
payment_entry.insert()

self.assertEqual(payment_entry.difference_amount, -4855.00)
payment_entry.references = []
payment_entry.submit()

payment_reconciliation = frappe.new_doc("Payment Reconciliation")
payment_reconciliation.company = payment_entry.company
payment_reconciliation.party_type = "Supplier"
payment_reconciliation.party = purchase_invoice.supplier
payment_reconciliation.receivable_payable_account = payment_entry.paid_to
payment_reconciliation.get_unreconciled_entries()
payment_reconciliation.allocate_entries(
{
"payments": [d.__dict__ for d in payment_reconciliation.payments],
"invoices": [d.__dict__ for d in payment_reconciliation.invoices],
}
)
for d in payment_reconciliation.invoices:
# Reset invoice outstanding_amount because allocate_entries will zero this value out.
d.outstanding_amount = d.amount
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have to reset the outstanding_amount here because it will get set to zero here during allocate_entries()

This will then fail during the validate_allocation()

invoice_outstanding = unreconciled_invoices.get(row.invoice_type, {}).get(row.invoice_number)
if flt(row.allocated_amount) - invoice_outstanding > 0.009:
frappe.throw(
_(
"Row {0}: Allocated amount {1} must be less than or equal to invoice outstanding amount {2}"
).format(row.idx, row.allocated_amount, invoice_outstanding)
)

for d in payment_reconciliation.allocation:
d.difference_account = "Exchange Gain/Loss - _TC"
payment_reconciliation.reconcile()

payment_entry.load_from_db()
self.assertEqual(len(payment_entry.references), 1)
self.assertEqual(payment_entry.difference_amount, 0)


ADDRESS_RECORDS = [
{
Expand Down
10 changes: 5 additions & 5 deletions erpnext/accounts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,11 +611,6 @@ def update_reference_in_payment_entry(d, payment_entry, do_not_save=False):
new_row.docstatus = 1
new_row.update(reference_details)

payment_entry.flags.ignore_validate_update_after_submit = True
payment_entry.setup_party_account_field()
payment_entry.set_missing_values()
payment_entry.set_amounts()

if d.difference_amount and d.difference_account:
account_details = {
"account": d.difference_account,
Expand All @@ -627,6 +622,11 @@ def update_reference_in_payment_entry(d, payment_entry, do_not_save=False):

payment_entry.set_gain_or_loss(account_details=account_details)

payment_entry.flags.ignore_validate_update_after_submit = True
payment_entry.setup_party_account_field()
payment_entry.set_missing_values()
payment_entry.set_amounts()

if not do_not_save:
payment_entry.save(ignore_permissions=True)

Expand Down
Empty file.
Empty file.