From 52177cffcde045fc7f65ac110b61c21540a4400c Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Sun, 29 Jun 2025 21:45:57 +0530 Subject: [PATCH] fix: accounting entries for standalone credit notes --- .../purchase_invoice/purchase_invoice.py | 9 ++------- erpnext/stock/stock_ledger.py | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index 9336279cfe0..34e48b31c08 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -1338,17 +1338,12 @@ class PurchaseInvoice(BuyingController): warehouse_debit_amount = stock_amount - elif self.is_return and self.update_stock and self.is_internal_supplier and warehouse_debit_amount: + elif self.is_return and self.update_stock and (self.is_internal_supplier or not self.return_against): net_rate = item.base_net_amount if item.sales_incoming_rate: # for internal transfer net_rate = item.qty * item.sales_incoming_rate - stock_amount = ( - net_rate - + item.item_tax_amount - + flt(item.landed_cost_voucher_amount) - + flt(item.get("amount_difference_with_purchase_invoice")) - ) + stock_amount = net_rate + item.item_tax_amount + flt(item.landed_cost_voucher_amount) if flt(stock_amount, net_amt_precision) != flt(warehouse_debit_amount, net_amt_precision): cost_of_goods_sold_account = self.get_company_default("default_expense_account") diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py index 8030d806bcf..42bcf3ee11a 100644 --- a/erpnext/stock/stock_ledger.py +++ b/erpnext/stock/stock_ledger.py @@ -1258,12 +1258,19 @@ class update_entries_after: def update_rate_on_purchase_receipt(self, sle, outgoing_rate): if frappe.db.exists(sle.voucher_type + " Item", sle.voucher_detail_no): - if sle.voucher_type in ["Purchase Receipt", "Purchase Invoice"] and frappe.get_cached_value( - sle.voucher_type, sle.voucher_no, "is_internal_supplier" - ): - frappe.db.set_value( - f"{sle.voucher_type} Item", sle.voucher_detail_no, "valuation_rate", sle.outgoing_rate + if sle.voucher_type in ["Purchase Receipt", "Purchase Invoice"]: + details = frappe.get_cached_value( + sle.voucher_type, + sle.voucher_no, + ["is_internal_supplier", "is_return", "return_against"], + as_dict=True, ) + if details.is_internal_supplier or (details.is_return and not details.return_against): + rate = outgoing_rate if details.is_return else sle.outgoing_rate + + frappe.db.set_value( + f"{sle.voucher_type} Item", sle.voucher_detail_no, "valuation_rate", rate + ) else: frappe.db.set_value( "Purchase Receipt Item Supplied", sle.voucher_detail_no, "rate", outgoing_rate