Merge pull request #35061 from ruthra-kumar/refactor_pe_dont_book_gain_loss_for_sales_purchase_orders

refactor: don't book exchange gain/loss for sales/purchase orders
This commit is contained in:
ruthra kumar 2023-05-02 14:03:17 +05:30 committed by GitHub
commit 8f2302a7bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 6 deletions

View File

@ -654,6 +654,28 @@ class PaymentEntry(AccountsController):
self.precision("base_received_amount"), self.precision("base_received_amount"),
) )
def calculate_base_allocated_amount_for_reference(self, d) -> float:
base_allocated_amount = 0
if d.reference_doctype in frappe.get_hooks("advance_payment_doctypes"):
# When referencing Sales/Purchase Order, use the source/target exchange rate depending on payment type.
# This is so there are no Exchange Gain/Loss generated for such doctypes
exchange_rate = 1
if self.payment_type == "Receive":
exchange_rate = self.source_exchange_rate
elif self.payment_type == "Pay":
exchange_rate = self.target_exchange_rate
base_allocated_amount += flt(
flt(d.allocated_amount) * flt(exchange_rate), self.precision("base_paid_amount")
)
else:
base_allocated_amount += flt(
flt(d.allocated_amount) * flt(d.exchange_rate), self.precision("base_paid_amount")
)
return base_allocated_amount
def set_total_allocated_amount(self): def set_total_allocated_amount(self):
if self.payment_type == "Internal Transfer": if self.payment_type == "Internal Transfer":
return return
@ -662,9 +684,7 @@ class PaymentEntry(AccountsController):
for d in self.get("references"): for d in self.get("references"):
if d.allocated_amount: if d.allocated_amount:
total_allocated_amount += flt(d.allocated_amount) total_allocated_amount += flt(d.allocated_amount)
base_total_allocated_amount += flt( base_total_allocated_amount += self.calculate_base_allocated_amount_for_reference(d)
flt(d.allocated_amount) * flt(d.exchange_rate), self.precision("base_paid_amount")
)
self.total_allocated_amount = abs(total_allocated_amount) self.total_allocated_amount = abs(total_allocated_amount)
self.base_total_allocated_amount = abs(base_total_allocated_amount) self.base_total_allocated_amount = abs(base_total_allocated_amount)
@ -881,9 +901,7 @@ class PaymentEntry(AccountsController):
} }
) )
allocated_amount_in_company_currency = flt( allocated_amount_in_company_currency = self.calculate_base_allocated_amount_for_reference(d)
flt(d.allocated_amount) * flt(d.exchange_rate), self.precision("paid_amount")
)
gle.update( gle.update(
{ {

View File

@ -51,6 +51,38 @@ class TestPaymentEntry(FrappeTestCase):
so_advance_paid = frappe.db.get_value("Sales Order", so.name, "advance_paid") so_advance_paid = frappe.db.get_value("Sales Order", so.name, "advance_paid")
self.assertEqual(so_advance_paid, 0) self.assertEqual(so_advance_paid, 0)
def test_payment_against_sales_order_usd_to_inr(self):
so = make_sales_order(
customer="_Test Customer USD", currency="USD", qty=1, rate=100, do_not_submit=True
)
so.conversion_rate = 50
so.submit()
pe = get_payment_entry("Sales Order", so.name)
pe.source_exchange_rate = 55
pe.received_amount = 5500
pe.insert()
pe.submit()
# there should be no difference amount
pe.reload()
self.assertEqual(pe.difference_amount, 0)
self.assertEqual(pe.deductions, [])
expected_gle = dict(
(d[0], d)
for d in [["_Test Receivable USD - _TC", 0, 5500, so.name], ["Cash - _TC", 5500.0, 0, None]]
)
self.validate_gl_entries(pe.name, expected_gle)
so_advance_paid = frappe.db.get_value("Sales Order", so.name, "advance_paid")
self.assertEqual(so_advance_paid, 100)
pe.cancel()
so_advance_paid = frappe.db.get_value("Sales Order", so.name, "advance_paid")
self.assertEqual(so_advance_paid, 0)
def test_payment_entry_for_blocked_supplier_invoice(self): def test_payment_entry_for_blocked_supplier_invoice(self):
supplier = frappe.get_doc("Supplier", "_Test Supplier") supplier = frappe.get_doc("Supplier", "_Test Supplier")
supplier.on_hold = 1 supplier.on_hold = 1