From 93c2a679309825ede65481a070a4e2b608adb9f7 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Tue, 15 Jul 2025 23:06:54 +0530 Subject: [PATCH] fix: stand-alone credit note gl entries (cherry picked from commit f3d6a641560e5fac582fec07b513815ce0d01e34) --- .../purchase_invoice/purchase_invoice.py | 60 ++++++++++--------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py index 73f2764fd67..9ec3555b49a 100644 --- a/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py +++ b/erpnext/accounts/doctype/purchase_invoice/purchase_invoice.py @@ -1310,8 +1310,37 @@ class PurchaseInvoice(BuyingController): net_amt_precision, ) - # Stock ledger value is not matching with the warehouse amount - if ( + if 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) + warehouse_debit_amount = flt( + voucher_wise_stock_value.get((item.name, item.warehouse)), net_amt_precision + ) + + 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") + stock_adjustment_amt = stock_amount - warehouse_debit_amount + + gl_entries.append( + self.get_gl_dict( + { + "account": cost_of_goods_sold_account, + "against": item.expense_account, + "debit": stock_adjustment_amt, + "debit_in_transaction_currency": stock_adjustment_amt / self.conversion_rate, + "remarks": self.get("remarks") or _("Stock Adjustment"), + "cost_center": item.cost_center, + "project": item.project or self.project, + }, + account_currency, + item=item, + ) + ) + + elif ( self.update_stock and voucher_wise_stock_value.get((item.name, item.warehouse)) and warehouse_debit_amount @@ -1339,33 +1368,6 @@ class PurchaseInvoice(BuyingController): warehouse_debit_amount = stock_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) - - 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") - stock_adjustment_amt = stock_amount - warehouse_debit_amount - - gl_entries.append( - self.get_gl_dict( - { - "account": cost_of_goods_sold_account, - "against": item.expense_account, - "debit": stock_adjustment_amt, - "debit_in_transaction_currency": stock_adjustment_amt / self.conversion_rate, - "remarks": self.get("remarks") or _("Stock Adjustment"), - "cost_center": item.cost_center, - "project": item.project or self.project, - }, - account_currency, - item=item, - ) - ) - return warehouse_debit_amount def make_tax_gl_entries(self, gl_entries):