Skip to content

Commit

Permalink
feat: provision to select the qty field for Product Page
Browse files Browse the repository at this point in the history
  • Loading branch information
s-aga-r committed Mar 30, 2024
1 parent 6caa0da commit 9b131c6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
3 changes: 2 additions & 1 deletion webshop/patches.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

[post_model_sync]

webshop.patches.add_homepage_field
webshop.patches.add_homepage_field
execute:frappe.db.set_single_value('Webshop Settings', 'show_actual_qty', 1)
2 changes: 1 addition & 1 deletion webshop/setup/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def run_patches():

try:
for patch in patches:
frappe.get_attr(f"webshop.patches.after_install.{patch}.execute")()
frappe.get_attr(f"webshop.patches.{patch}.execute")()

finally:
frappe.flags.in_patch = False
Expand Down
10 changes: 9 additions & 1 deletion webshop/webshop/doctype/webshop_settings/webshop_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"column_break_21",
"default_customer_group",
"quotation_series",
"show_actual_qty",
"checkout_settings_section",
"enable_checkout",
"show_price_in_quotation",
Expand Down Expand Up @@ -366,12 +367,19 @@
"fieldtype": "Check",
"label": "Enable Redisearch",
"read_only_depends_on": "eval:!doc.is_redisearch_loaded"
},
{
"default": "1",
"description": "If enabled Actual Qty will be shown as <b>In Stock</b> on the product page instead of Projected Qty.",
"fieldname": "show_actual_qty",
"fieldtype": "Check",
"label": "Show Actual Qty"
}
],
"index_web_pages_for_search": 1,
"issingle": 1,
"links": [],
"modified": "2022-04-01 18:35:56.106756",
"modified": "2024-01-10 21:06:45.386977",
"modified_by": "Administrator",
"module": "Webshop",
"name": "Webshop Settings",
Expand Down
29 changes: 20 additions & 9 deletions webshop/webshop/utils/product.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import frappe
from frappe.query_builder.functions import IfNull
from frappe.utils import getdate, nowdate

from erpnext.stock.doctype.batch.batch import get_batch_qty
Expand Down Expand Up @@ -26,16 +27,26 @@ def get_web_item_qty_in_stock(item_code, item_warehouse_field, warehouse=None):

total_stock = 0.0
if warehouses:
qty_field = (
"actual_qty"
if frappe.db.get_single_value("Webshop Settings", "show_actual_qty")
else "projected_qty"
)

BIN = frappe.qb.DocType("Bin")
ITEM = frappe.qb.DocType("Item")
UOM = frappe.qb.DocType("UOM Conversion Detail")

for warehouse in warehouses:
stock_qty = frappe.db.sql(
"""
select S.actual_qty / IFNULL(C.conversion_factor, 1)
from tabBin S
inner join `tabItem` I on S.item_code = I.Item_code
left join `tabUOM Conversion Detail` C on I.sales_uom = C.uom and C.parent = I.Item_code
where S.item_code=%s and S.warehouse=%s""",
(item_code, warehouse),
)
stock_qty = (
frappe.qb.from_(BIN)
.select(BIN[qty_field] / IfNull(UOM.conversion_factor, 1))
.inner_join(ITEM)
.on(BIN.item_code == ITEM.item_code)
.left_join(UOM)
.on((ITEM.sales_uom == UOM.uom) & (UOM.parent == ITEM.item_code))
.where((BIN.item_code == item_code) & (BIN.warehouse == warehouse))
).run()

if stock_qty:
total_stock += adjust_qty_for_expired_items(item_code, stock_qty, warehouse)
Expand Down

0 comments on commit 9b131c6

Please sign in to comment.