From b7509e326eda7b9685598ad4c71ee959f6589ad5 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 1 Jan 2025 15:29:06 +0530 Subject: [PATCH 1/5] fix: slow stock transactions (backport #45025) (#45026) * fix: slow stock transactions (#45025) (cherry picked from commit e92af10f146491b6e559a61db66dca29311ae7c4) # Conflicts: # erpnext/stock/stock_ledger.py * chore: fix conflicts --------- Co-authored-by: rohitwaghchaure --- erpnext/stock/stock_balance.py | 2 +- erpnext/stock/stock_ledger.py | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/erpnext/stock/stock_balance.py b/erpnext/stock/stock_balance.py index 05d21950ad2f..4d195b85d865 100644 --- a/erpnext/stock/stock_balance.py +++ b/erpnext/stock/stock_balance.py @@ -80,7 +80,7 @@ def get_balance_qty_from_sle(item_code, warehouse): balance_qty = frappe.db.sql( """select qty_after_transaction from `tabStock Ledger Entry` where item_code=%s and warehouse=%s and is_cancelled=0 - order by posting_date desc, posting_time desc, creation desc + order by posting_datetime desc, creation desc limit 1""", (item_code, warehouse), ) diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py index ba2f65dccbcc..c9e5f9fd1efb 100644 --- a/erpnext/stock/stock_ledger.py +++ b/erpnext/stock/stock_ledger.py @@ -1263,7 +1263,7 @@ def get_previous_sle_of_current_voucher(args, operator="<", exclude_current_vouc and ( posting_datetime {operator} %(posting_datetime)s ) - order by posting_date desc, posting_time desc, creation desc + order by posting_datetime desc, creation desc limit 1 for update""", { @@ -1357,7 +1357,7 @@ def get_stock_ledger_entries( where item_code = %(item_code)s and is_cancelled = 0 {conditions} - order by posting_date {order}, posting_time {order}, creation {order} + order by posting_datetime {order}, creation {order} {limit} {for_update}""".format( conditions=conditions, limit=limit or "", @@ -1462,7 +1462,7 @@ def get_valuation_rate( AND valuation_rate >= 0 AND is_cancelled = 0 AND NOT (voucher_no = %s AND voucher_type = %s) - order by posting_date desc, posting_time desc, name desc limit 1""", + order by posting_datetime desc, creation desc limit 1""", (item_code, warehouse, voucher_no, voucher_type), ) @@ -1711,8 +1711,8 @@ def get_future_sle_with_negative_qty(sle): & (SLE.is_cancelled == 0) & (SLE.qty_after_transaction < 0) ) - .orderby(SLE.posting_date) - .orderby(SLE.posting_time) + .orderby(SLE.posting_datetime) + .orderby(SLE.creation) .limit(1) ) @@ -1728,14 +1728,14 @@ def get_future_sle_with_negative_batch_qty(args): with batch_ledger as ( select posting_date, posting_time, posting_datetime, voucher_type, voucher_no, - sum(actual_qty) over (order by posting_date, posting_time, creation) as cumulative_total + sum(actual_qty) over (order by posting_datetime, creation) as cumulative_total from `tabStock Ledger Entry` where item_code = %(item_code)s and warehouse = %(warehouse)s and batch_no=%(batch_no)s and is_cancelled = 0 - order by posting_date, posting_time, creation + order by posting_datetime, creation ) select * from batch_ledger where From 49778432eace22d936107d4f2a50683503cc91d3 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Thu, 2 Jan 2025 09:44:32 +0530 Subject: [PATCH 2/5] fix: BOM cost update issue (cherry picked from commit 28ea3ddd51e3ec3c0043722b5477386a77b5357e) --- .../doctype/bom_update_tool/bom_update_tool.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py index 288b7be35ee0..3e27f4f26609 100644 --- a/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py +++ b/erpnext/manufacturing/doctype/bom_update_tool/bom_update_tool.py @@ -9,6 +9,7 @@ import frappe from frappe.model.document import Document +from frappe.utils import date_diff, get_datetime, now class BOMUpdateTool(Document): @@ -38,13 +39,21 @@ def auto_update_latest_price_in_all_boms() -> None: if frappe.db.get_single_value("Manufacturing Settings", "update_bom_costs_automatically"): wip_log = frappe.get_all( "BOM Update Log", - {"update_type": "Update Cost", "status": ["in", ["Queued", "In Progress"]]}, + fields=["creation", "status"], + filters={"update_type": "Update Cost", "status": ["in", ["Queued", "In Progress"]]}, limit_page_length=1, + order_by="creation desc", ) - if not wip_log: + + if not wip_log or is_older_log(wip_log[0]): create_bom_update_log(update_type="Update Cost") +def is_older_log(log: dict) -> bool: + no_of_days = date_diff(get_datetime(now()), get_datetime(log.creation)) + return no_of_days > 10 + + def create_bom_update_log( boms: dict[str, str] | None = None, update_type: Literal["Replace BOM", "Update Cost"] = "Replace BOM", From 9efc1de40eb19cfd1e805adac5ccae8f413ebab5 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Thu, 2 Jan 2025 10:26:51 +0530 Subject: [PATCH 3/5] fix: removed unused code (cherry picked from commit dc5f2d35ac4367e7e7c17e4b16edf57b169d0aba) # Conflicts: # erpnext/stock/doctype/stock_entry/stock_entry.py --- erpnext/stock/doctype/stock_entry/stock_entry.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py index 2a8bb383a9c2..eaf21b373f2c 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/stock_entry.py @@ -113,7 +113,6 @@ def validate(self): self.validate_purpose() self.validate_item() self.validate_customer_provided_item() - self.validate_qty() self.set_transfer_qty() self.validate_uom_is_integer("uom", "qty") self.validate_uom_is_integer("stock_uom", "transfer_qty") @@ -375,6 +374,7 @@ def validate_item(self): flt(item.qty) * flt(item.conversion_factor), self.precision("transfer_qty", item) ) +<<<<<<< HEAD if ( self.purpose in ("Material Transfer", "Material Transfer for Manufacture") and not item.serial_no @@ -419,6 +419,8 @@ def validate_qty(self): if stock_qty > trans_qty: item_code.append(item.item_code) +======= +>>>>>>> dc5f2d35ac (fix: removed unused code) def validate_fg_completed_qty(self): if self.purpose != "Manufacture": return From b603adce5e42e0025d6bb36ff71465e57734e9b1 Mon Sep 17 00:00:00 2001 From: rohitwaghchaure Date: Thu, 2 Jan 2025 12:21:03 +0530 Subject: [PATCH 4/5] chore: fix conflicts --- .../stock/doctype/stock_entry/stock_entry.py | 37 ------------------- 1 file changed, 37 deletions(-) diff --git a/erpnext/stock/doctype/stock_entry/stock_entry.py b/erpnext/stock/doctype/stock_entry/stock_entry.py index eaf21b373f2c..785995727591 100644 --- a/erpnext/stock/doctype/stock_entry/stock_entry.py +++ b/erpnext/stock/doctype/stock_entry/stock_entry.py @@ -374,7 +374,6 @@ def validate_item(self): flt(item.qty) * flt(item.conversion_factor), self.precision("transfer_qty", item) ) -<<<<<<< HEAD if ( self.purpose in ("Material Transfer", "Material Transfer for Manufacture") and not item.serial_no @@ -385,42 +384,6 @@ def validate_item(self): frappe.MandatoryError, ) - def validate_qty(self): - manufacture_purpose = ["Manufacture", "Material Consumption for Manufacture"] - - if self.purpose in manufacture_purpose and self.work_order: - if not frappe.get_value("Work Order", self.work_order, "skip_transfer"): - item_code = [] - for item in self.items: - if cstr(item.t_warehouse) == "": - req_items = frappe.get_all( - "Work Order Item", - filters={"parent": self.work_order, "item_code": item.item_code}, - fields=["item_code"], - ) - - transferred_materials = frappe.db.sql( - """ - select - sum(sed.qty) as qty - from `tabStock Entry` se,`tabStock Entry Detail` sed - where - se.name = sed.parent and se.docstatus=1 and - (se.purpose='Material Transfer for Manufacture' or se.purpose='Manufacture') - and sed.item_code=%s and se.work_order= %s and ifnull(sed.t_warehouse, '') != '' - """, - (item.item_code, self.work_order), - as_dict=1, - ) - - stock_qty = flt(item.qty) - trans_qty = flt(transferred_materials[0].qty) - if req_items: - if stock_qty > trans_qty: - item_code.append(item.item_code) - -======= ->>>>>>> dc5f2d35ac (fix: removed unused code) def validate_fg_completed_qty(self): if self.purpose != "Manufacture": return From 7b0e19499b1e34145e66a3124de093fe4fc3943f Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Thu, 2 Jan 2025 20:50:28 +0530 Subject: [PATCH 5/5] chore: partial revert #44989 (cherry picked from commit 63d547fb4af311cdb061a029a4e6ca0ae41bbaed) --- erpnext/public/js/controllers/transaction.js | 1 - 1 file changed, 1 deletion(-) diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js index 5d282779e756..6c83756a006a 100644 --- a/erpnext/public/js/controllers/transaction.js +++ b/erpnext/public/js/controllers/transaction.js @@ -713,7 +713,6 @@ erpnext.TransactionController = class TransactionController extends erpnext.taxe } validate() { - this.apply_pricing_rule() this.calculate_taxes_and_totals(false); }