2021-03-15 18:35:53 +00:00
|
|
|
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
|
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
import frappe
|
2022-01-31 19:09:14 +00:00
|
|
|
|
|
|
|
from erpnext.e_commerce.doctype.e_commerce_settings.e_commerce_settings import (
|
|
|
|
get_shopping_cart_settings,
|
|
|
|
)
|
2021-04-12 19:09:26 +00:00
|
|
|
from erpnext.e_commerce.shopping_cart.cart import _set_price_list
|
2022-01-31 19:09:14 +00:00
|
|
|
from erpnext.utilities.product import get_price
|
|
|
|
|
2021-03-15 18:35:53 +00:00
|
|
|
|
|
|
|
def get_context(context):
|
2021-08-09 15:30:31 +00:00
|
|
|
is_guest = frappe.session.user == "Guest"
|
2021-03-15 18:35:53 +00:00
|
|
|
|
2021-08-09 15:30:31 +00:00
|
|
|
settings = get_shopping_cart_settings()
|
|
|
|
items = get_wishlist_items() if not is_guest else []
|
|
|
|
selling_price_list = _set_price_list(settings) if not is_guest else None
|
2021-04-12 19:09:26 +00:00
|
|
|
|
2021-08-09 15:30:31 +00:00
|
|
|
items = set_stock_price_details(items, settings, selling_price_list)
|
2021-03-15 18:35:53 +00:00
|
|
|
|
2021-09-01 09:27:50 +00:00
|
|
|
context.body_class = "product-page"
|
2021-03-15 18:35:53 +00:00
|
|
|
context.items = items
|
|
|
|
context.settings = settings
|
2021-05-24 20:05:22 +00:00
|
|
|
context.no_cache = 1
|
2021-03-15 18:35:53 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-04-12 19:09:26 +00:00
|
|
|
def get_stock_availability(item_code, warehouse):
|
2023-09-20 11:57:35 +00:00
|
|
|
from erpnext.stock.doctype.warehouse.warehouse import get_child_warehouses
|
|
|
|
|
|
|
|
if warehouse and frappe.get_cached_value("Warehouse", warehouse, "is_group") == 1:
|
|
|
|
warehouses = get_child_warehouses(warehouse)
|
|
|
|
else:
|
|
|
|
warehouses = [warehouse] if warehouse else []
|
|
|
|
|
|
|
|
stock_qty = 0.0
|
|
|
|
for warehouse in warehouses:
|
|
|
|
stock_qty += frappe.utils.flt(
|
|
|
|
frappe.db.get_value("Bin", {"item_code": item_code, "warehouse": warehouse}, "actual_qty")
|
|
|
|
)
|
|
|
|
|
2021-05-24 20:05:22 +00:00
|
|
|
return bool(stock_qty)
|
2021-04-12 19:09:26 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-03-15 18:35:53 +00:00
|
|
|
def get_wishlist_items():
|
2021-07-13 20:06:50 +00:00
|
|
|
if not frappe.db.exists("Wishlist", frappe.session.user):
|
|
|
|
return []
|
|
|
|
|
|
|
|
return frappe.db.get_all(
|
|
|
|
"Wishlist Item",
|
|
|
|
filters={"parent": frappe.session.user},
|
|
|
|
fields=[
|
|
|
|
"web_item_name",
|
|
|
|
"item_code",
|
|
|
|
"item_name",
|
2021-08-09 15:30:31 +00:00
|
|
|
"website_item",
|
|
|
|
"warehouse",
|
2021-07-13 20:06:50 +00:00
|
|
|
"image",
|
|
|
|
"item_group",
|
|
|
|
"route",
|
|
|
|
],
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-08-09 15:30:31 +00:00
|
|
|
|
|
|
|
def set_stock_price_details(items, settings, selling_price_list):
|
|
|
|
for item in items:
|
|
|
|
if settings.show_stock_availability:
|
|
|
|
item.available = get_stock_availability(item.item_code, item.get("warehouse"))
|
|
|
|
|
|
|
|
price_details = get_price(
|
|
|
|
item.item_code, selling_price_list, settings.default_customer_group, settings.company
|
|
|
|
)
|
|
|
|
|
|
|
|
if price_details:
|
|
|
|
item.formatted_price = price_details.get("formatted_price")
|
|
|
|
item.formatted_mrp = price_details.get("formatted_mrp")
|
|
|
|
if item.formatted_mrp:
|
|
|
|
item.discount = price_details.get("formatted_discount_percent") or price_details.get(
|
|
|
|
"formatted_discount_rate"
|
|
|
|
)
|
|
|
|
|
|
|
|
return items
|