Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
| 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | |
| 6 | import frappe |
| 7 | from frappe.utils import cint, fmt_money, flt |
| 8 | from erpnext.accounts.doctype.pricing_rule.pricing_rule import get_pricing_rule_for_item |
| 9 | |
| 10 | def get_qty_in_stock(item_code, item_warehouse_field): |
| 11 | in_stock, stock_qty = 0, '' |
| 12 | template_item_code = frappe.db.get_value("Item", item_code, "variant_of") |
| 13 | |
| 14 | warehouse = frappe.db.get_value("Item", item_code, item_warehouse_field) |
| 15 | if not warehouse and template_item_code and template_item_code != item_code: |
| 16 | warehouse = frappe.db.get_value("Item", template_item_code, item_warehouse_field) |
| 17 | |
| 18 | if warehouse: |
| 19 | stock_qty = frappe.db.sql("""select actual_qty from tabBin where |
| 20 | item_code=%s and warehouse=%s""", (item_code, warehouse)) |
| 21 | if stock_qty: |
| 22 | in_stock = stock_qty[0][0] > 0 and 1 or 0 |
| 23 | |
| 24 | return frappe._dict({"in_stock": in_stock, "stock_qty": stock_qty}) |
| 25 | |
| 26 | def get_price(item_code, price_list, customer_group, company, qty=1): |
| 27 | template_item_code = frappe.db.get_value("Item", item_code, "variant_of") |
| 28 | |
| 29 | if price_list: |
| 30 | price = frappe.get_all("Item Price", fields=["price_list_rate", "currency"], |
| 31 | filters={"price_list": price_list, "item_code": item_code}) |
| 32 | |
| 33 | if template_item_code and not price: |
| 34 | price = frappe.get_all("Item Price", fields=["price_list_rate", "currency"], |
| 35 | filters={"price_list": price_list, "item_code": template_item_code}) |
| 36 | |
| 37 | if price: |
| 38 | pricing_rule = get_pricing_rule_for_item(frappe._dict({ |
| 39 | "item_code": item_code, |
| 40 | "qty": qty, |
| 41 | "transaction_type": "selling", |
| 42 | "price_list": price_list, |
| 43 | "customer_group": customer_group, |
| 44 | "company": company, |
| 45 | "conversion_rate": 1, |
| 46 | "for_shopping_cart": True |
| 47 | })) |
| 48 | |
| 49 | if pricing_rule: |
| 50 | if pricing_rule.pricing_rule_for == "Discount Percentage": |
| 51 | price[0].price_list_rate = flt(price[0].price_list_rate * (1.0 - (flt(pricing_rule.discount_percentage) / 100.0))) |
| 52 | |
| 53 | if pricing_rule.pricing_rule_for == "Price": |
| 54 | price[0].price_list_rate = pricing_rule.price_list_rate |
| 55 | |
| 56 | price_obj = price[0] |
| 57 | if price_obj: |
| 58 | price_obj["formatted_price"] = fmt_money(price_obj["price_list_rate"], currency=price_obj["currency"]) |
| 59 | |
| 60 | price_obj["currency_symbol"] = not cint(frappe.db.get_default("hide_currency_symbol")) \ |
| 61 | and (frappe.db.get_value("Currency", price_obj.currency, "symbol") or price_obj.currency) \ |
| 62 | or "" |
| 63 | |
| 64 | if not price_obj["price_list_rate"]: |
| 65 | price_obj["price_list_rate"] = 0 |
| 66 | |
| 67 | if not price_obj["currency"]: |
| 68 | price_obj["currency"] = "" |
| 69 | |
| 70 | if not price_obj["formatted_price"]: |
| 71 | price_obj["formatted_price"] = "" |
| 72 | |
| 73 | return price_obj |