2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2014-10-21 10:46:30 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import frappe
|
2016-11-22 16:53:31 +00:00
|
|
|
from frappe.utils import cint, fmt_money, flt
|
2014-10-21 10:46:30 +00:00
|
|
|
from erpnext.shopping_cart.cart import _get_cart_quotation
|
2016-11-22 16:53:31 +00:00
|
|
|
from erpnext.shopping_cart.doctype.shopping_cart_settings.shopping_cart_settings \
|
2017-03-21 10:58:03 +00:00
|
|
|
import is_cart_enabled, get_shopping_cart_settings, show_quantity_in_website
|
2016-11-22 16:53:31 +00:00
|
|
|
from erpnext.accounts.doctype.pricing_rule.pricing_rule import get_pricing_rule_for_item
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
@frappe.whitelist(allow_guest=True)
|
|
|
|
def get_product_info(item_code):
|
|
|
|
"""get product price / stock info"""
|
2015-07-13 10:53:42 +00:00
|
|
|
if not is_cart_enabled():
|
2014-10-21 10:46:30 +00:00
|
|
|
return {}
|
|
|
|
|
|
|
|
qty = 0
|
2015-10-20 12:00:02 +00:00
|
|
|
cart_quotation = _get_cart_quotation()
|
|
|
|
template_item_code = frappe.db.get_value("Item", item_code, "variant_of")
|
2017-03-21 10:58:03 +00:00
|
|
|
stock_status = get_qty_in_stock(item_code, template_item_code)
|
|
|
|
in_stock = stock_status.in_stock
|
|
|
|
stock_qty = stock_status.stock_qty
|
2015-10-20 12:00:02 +00:00
|
|
|
price = get_price(item_code, template_item_code, cart_quotation.selling_price_list)
|
2014-10-21 10:46:30 +00:00
|
|
|
|
|
|
|
if price:
|
|
|
|
price["formatted_price"] = fmt_money(price["price_list_rate"], currency=price["currency"])
|
|
|
|
|
|
|
|
price["currency"] = not cint(frappe.db.get_default("hide_currency_symbol")) \
|
|
|
|
and (frappe.db.get_value("Currency", price.currency, "symbol") or price.currency) \
|
|
|
|
or ""
|
|
|
|
|
|
|
|
if frappe.session.user != "Guest":
|
|
|
|
item = cart_quotation.get({"item_code": item_code})
|
|
|
|
if item:
|
|
|
|
qty = item[0].qty
|
|
|
|
|
|
|
|
return {
|
|
|
|
"price": price,
|
2017-03-21 10:58:03 +00:00
|
|
|
"stock_qty": stock_qty,
|
|
|
|
"in_stock": in_stock,
|
2014-10-21 10:46:30 +00:00
|
|
|
"uom": frappe.db.get_value("Item", item_code, "stock_uom"),
|
2017-03-21 10:58:03 +00:00
|
|
|
"qty": qty,
|
|
|
|
"show_stock_qty": show_quantity_in_website()
|
2014-10-21 10:46:30 +00:00
|
|
|
}
|
2015-10-20 12:00:02 +00:00
|
|
|
|
|
|
|
def get_qty_in_stock(item_code, template_item_code):
|
2017-08-08 05:59:11 +00:00
|
|
|
in_stock, stock_qty = 0, ''
|
2015-10-20 12:00:02 +00:00
|
|
|
warehouse = frappe.db.get_value("Item", item_code, "website_warehouse")
|
|
|
|
if not warehouse and template_item_code and template_item_code != item_code:
|
|
|
|
warehouse = frappe.db.get_value("Item", template_item_code, "website_warehouse")
|
|
|
|
|
|
|
|
if warehouse:
|
2017-03-21 10:58:03 +00:00
|
|
|
stock_qty = frappe.db.sql("""select actual_qty from tabBin where
|
2015-10-20 12:00:02 +00:00
|
|
|
item_code=%s and warehouse=%s""", (item_code, warehouse))
|
2017-03-21 10:58:03 +00:00
|
|
|
if stock_qty:
|
|
|
|
in_stock = stock_qty[0][0] > 0 and 1 or 0
|
2015-10-20 12:00:02 +00:00
|
|
|
|
2017-03-21 10:58:03 +00:00
|
|
|
return frappe._dict({"in_stock": in_stock, "stock_qty": stock_qty})
|
2015-10-20 12:00:02 +00:00
|
|
|
|
2016-11-22 16:53:31 +00:00
|
|
|
def get_price(item_code, template_item_code, price_list, qty=1):
|
2015-10-20 12:00:02 +00:00
|
|
|
if price_list:
|
2016-11-22 16:53:31 +00:00
|
|
|
cart_settings = get_shopping_cart_settings()
|
|
|
|
|
2015-10-20 12:00:02 +00:00
|
|
|
price = frappe.get_all("Item Price", fields=["price_list_rate", "currency"],
|
|
|
|
filters={"price_list": price_list, "item_code": item_code})
|
|
|
|
|
2017-05-17 13:50:01 +00:00
|
|
|
if template_item_code and not price:
|
2015-10-20 12:00:02 +00:00
|
|
|
price = frappe.get_all("Item Price", fields=["price_list_rate", "currency"],
|
|
|
|
filters={"price_list": price_list, "item_code": template_item_code})
|
|
|
|
|
|
|
|
if price:
|
2016-11-22 16:53:31 +00:00
|
|
|
pricing_rule = get_pricing_rule_for_item(frappe._dict({
|
|
|
|
"item_code": item_code,
|
|
|
|
"qty": qty,
|
|
|
|
"transaction_type": "selling",
|
|
|
|
"price_list": price_list,
|
|
|
|
"customer_group": cart_settings.default_customer_group,
|
|
|
|
"company": cart_settings.company,
|
|
|
|
"conversion_rate": 1,
|
|
|
|
"for_shopping_cart": True
|
|
|
|
}))
|
|
|
|
|
|
|
|
if pricing_rule:
|
|
|
|
if pricing_rule.pricing_rule_for == "Discount Percentage":
|
2017-08-02 16:37:51 +00:00
|
|
|
price[0].price_list_rate = flt(price[0].price_list_rate * (1.0 - (flt(pricing_rule.discount_percentage) / 100.0)))
|
2016-11-22 16:53:31 +00:00
|
|
|
|
|
|
|
if pricing_rule.pricing_rule_for == "Price":
|
|
|
|
price[0].price_list_rate = pricing_rule.price_list_rate
|
|
|
|
|
2015-10-20 12:00:02 +00:00
|
|
|
return price[0]
|