From 155d1405d006314b05bbbd63abf25ca672093b9d Mon Sep 17 00:00:00 2001 From: rohitwaghchaure Date: Thu, 9 Nov 2023 12:22:26 +0530 Subject: [PATCH] fix: make adjustment entry using stock reconciliation (#37995) fix: do adjustment entry using stock reconciliation (cherry picked from commit a8216b9727401bfb6900f15447ab935756d7f353) --- .../stock_ledger_entry.json | 9 +++++- .../stock_reconciliation.py | 20 +++++++++++++ erpnext/stock/stock_ledger.py | 30 +++++++++++++++++-- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.json b/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.json index 569f58a69ff5..dcbd9b2d0626 100644 --- a/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.json +++ b/erpnext/stock/doctype/stock_ledger_entry/stock_ledger_entry.json @@ -11,6 +11,7 @@ "warehouse", "posting_date", "posting_time", + "is_adjustment_entry", "column_break_6", "voucher_type", "voucher_no", @@ -333,6 +334,12 @@ "fieldname": "has_serial_no", "fieldtype": "Check", "label": "Has Serial No" + }, + { + "default": "0", + "fieldname": "is_adjustment_entry", + "fieldtype": "Check", + "label": "Is Adjustment Entry" } ], "hide_toolbar": 1, @@ -341,7 +348,7 @@ "in_create": 1, "index_web_pages_for_search": 1, "links": [], - "modified": "2023-04-03 16:33:16.270722", + "modified": "2023-10-23 18:07:42.063615", "modified_by": "Administrator", "module": "Stock", "name": "Stock Ledger Entry", diff --git a/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py b/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py index 98b4ffdfcfd0..c7a278821dc5 100644 --- a/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py +++ b/erpnext/stock/doctype/stock_reconciliation/stock_reconciliation.py @@ -413,6 +413,11 @@ def update_stock_ledger(self): sl_entries = [] for row in self.items: + + if not row.qty and not row.valuation_rate and not row.current_qty: + self.make_adjustment_entry(row, sl_entries) + continue + item = frappe.get_cached_value( "Item", row.item_code, ["has_serial_no", "has_batch_no"], as_dict=1 ) @@ -463,6 +468,21 @@ def update_stock_ledger(self): ) self.make_sl_entries(sl_entries, allow_negative_stock=allow_negative_stock) + def make_adjustment_entry(self, row, sl_entries): + from erpnext.stock.stock_ledger import get_stock_value_difference + + difference_amount = get_stock_value_difference( + row.item_code, row.warehouse, self.posting_date, self.posting_time + ) + + if not difference_amount: + return + + args = self.get_sle_for_items(row) + args.update({"stock_value_difference": -1 * difference_amount, "is_adjustment_entry": 1}) + + sl_entries.append(args) + def get_sle_for_serialized_items(self, row, sl_entries): if row.current_serial_and_batch_bundle: args = self.get_sle_for_items(row) diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py index f2db5755bc79..000b295039fc 100644 --- a/erpnext/stock/stock_ledger.py +++ b/erpnext/stock/stock_ledger.py @@ -752,9 +752,11 @@ def process_sle(self, sle): sle.valuation_rate = self.wh_data.valuation_rate sle.stock_value = self.wh_data.stock_value sle.stock_queue = json.dumps(self.wh_data.stock_queue) - sle.stock_value_difference = stock_value_difference - sle.doctype = "Stock Ledger Entry" + if not sle.is_adjustment_entry or not self.args.get("sle_id"): + sle.stock_value_difference = stock_value_difference + + sle.doctype = "Stock Ledger Entry" frappe.get_doc(sle).db_update() if not self.args.get("sle_id"): @@ -1929,3 +1931,27 @@ def is_internal_transfer(sle): if data.is_internal_supplier and data.represents_company == data.company: return True + + +def get_stock_value_difference(item_code, warehouse, posting_date, posting_time, voucher_no=None): + table = frappe.qb.DocType("Stock Ledger Entry") + + query = ( + frappe.qb.from_(table) + .select(Sum(table.stock_value_difference).as_("value")) + .where( + (table.is_cancelled == 0) + & (table.item_code == item_code) + & (table.warehouse == warehouse) + & ( + (table.posting_date < posting_date) + | ((table.posting_date == posting_date) & (table.posting_time <= posting_time)) + ) + ) + ) + + if voucher_no: + query = query.where(table.voucher_no != voucher_no) + + difference_amount = query.run() + return flt(difference_amount[0][0]) if difference_amount else 0