blob: 6b514b659384af07542b3d3a67cae33ecbe606e1 [file] [log] [blame]
marination60261852021-04-13 00:39:26 +05301# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
Faris Ansarifd345f82017-10-05 11:17:30 +05302# License: GNU General Public License v3. See license.txt
3
Faris Ansarifd345f82017-10-05 11:17:30 +05304import frappe
Chillar Anand915b3432021-09-02 16:44:59 +05305from frappe.utils import cint, flt, fmt_money, getdate, nowdate
6
Faris Ansarifd345f82017-10-05 11:17:30 +05307from erpnext.accounts.doctype.pricing_rule.pricing_rule import get_pricing_rule_for_item
tundebabzyc14f1f12018-01-27 06:28:29 +01008from erpnext.stock.doctype.batch.batch import get_batch_qty
Faris Ansarifd345f82017-10-05 11:17:30 +05309
marination60261852021-04-13 00:39:26 +053010def get_web_item_qty_in_stock(item_code, item_warehouse_field, warehouse=None):
Faris Ansarifd345f82017-10-05 11:17:30 +053011 in_stock, stock_qty = 0, ''
Manas Solankia521efc2018-01-05 12:47:20 +053012 template_item_code, is_stock_item = frappe.db.get_value("Item", item_code, ["variant_of", "is_stock_item"])
Faris Ansarifd345f82017-10-05 11:17:30 +053013
tundebabzyc14f1f12018-01-27 06:28:29 +010014 if not warehouse:
marination60261852021-04-13 00:39:26 +053015 warehouse = frappe.db.get_value("Website Item", {"item_code": item_code}, item_warehouse_field)
tundebabzyc14f1f12018-01-27 06:28:29 +010016
Faris Ansarifd345f82017-10-05 11:17:30 +053017 if not warehouse and template_item_code and template_item_code != item_code:
marination1d949142021-04-20 21:54:52 +053018 warehouse = frappe.db.get_value("Website Item", {"item_code": template_item_code}, item_warehouse_field)
Faris Ansarifd345f82017-10-05 11:17:30 +053019
20 if warehouse:
Britloge3a9b9f2018-03-20 09:58:44 +010021 stock_qty = frappe.db.sql("""
Rushabh Mehta708e47a2018-08-08 16:37:31 +053022 select GREATEST(S.actual_qty - S.reserved_qty - S.reserved_qty_for_production - S.reserved_qty_for_sub_contract, 0) / IFNULL(C.conversion_factor, 1)
Britloge3a9b9f2018-03-20 09:58:44 +010023 from tabBin S
24 inner join `tabItem` I on S.item_code = I.Item_code
Rushabh Mehta708e47a2018-08-08 16:37:31 +053025 left join `tabUOM Conversion Detail` C on I.sales_uom = C.uom and C.parent = I.Item_code
Britloge3a9b9f2018-03-20 09:58:44 +010026 where S.item_code=%s and S.warehouse=%s""", (item_code, warehouse))
Faris Ansarifd345f82017-10-05 11:17:30 +053027
Britloge3a9b9f2018-03-20 09:58:44 +010028 if stock_qty:
29 stock_qty = adjust_qty_for_expired_items(item_code, stock_qty, warehouse)
30 in_stock = stock_qty[0][0] > 0 and 1 or 0
tundebabzyc14f1f12018-01-27 06:28:29 +010031
Manas Solankia521efc2018-01-05 12:47:20 +053032 return frappe._dict({"in_stock": in_stock, "stock_qty": stock_qty, "is_stock_item": is_stock_item})
Faris Ansarifd345f82017-10-05 11:17:30 +053033
tundebabzyc14f1f12018-01-27 06:28:29 +010034
tundebabzy29c81422018-01-31 10:36:31 +010035def adjust_qty_for_expired_items(item_code, stock_qty, warehouse):
36 batches = frappe.get_all('Batch', filters=[{'item': item_code}], fields=['expiry_date', 'name'])
tundebabzyc14f1f12018-01-27 06:28:29 +010037 expired_batches = get_expired_batches(batches)
38 stock_qty = [list(item) for item in stock_qty]
Rushabh Mehta708e47a2018-08-08 16:37:31 +053039
tundebabzy29c81422018-01-31 10:36:31 +010040 for batch in expired_batches:
41 if warehouse:
42 stock_qty[0][0] = max(0, stock_qty[0][0] - get_batch_qty(batch, warehouse))
43 else:
44 stock_qty[0][0] = max(0, stock_qty[0][0] - qty_from_all_warehouses(get_batch_qty(batch)))
tundebabzyc14f1f12018-01-27 06:28:29 +010045
tundebabzy29c81422018-01-31 10:36:31 +010046 if not stock_qty[0][0]:
47 break
48
tundebabzyc14f1f12018-01-27 06:28:29 +010049 return stock_qty
50
51
52def get_expired_batches(batches):
53 """
54 :param batches: A list of dict in the form [{'expiry_date': datetime.date(20XX, 1, 1), 'name': 'batch_id'}, ...]
55 """
56 return [b.name for b in batches if b.expiry_date and b.expiry_date <= getdate(nowdate())]
57
58
59def qty_from_all_warehouses(batch_info):
60 """
61 :param batch_info: A list of dict in the form [{u'warehouse': u'Stores - I', u'qty': 0.8}, ...]
62 """
63 qty = 0
64 for batch in batch_info:
65 qty = qty + batch.qty
66
67 return qty
68
Faris Ansarifd345f82017-10-05 11:17:30 +053069def get_price(item_code, price_list, customer_group, company, qty=1):
70 template_item_code = frappe.db.get_value("Item", item_code, "variant_of")
71
72 if price_list:
73 price = frappe.get_all("Item Price", fields=["price_list_rate", "currency"],
74 filters={"price_list": price_list, "item_code": item_code})
75
76 if template_item_code and not price:
77 price = frappe.get_all("Item Price", fields=["price_list_rate", "currency"],
78 filters={"price_list": price_list, "item_code": template_item_code})
79
80 if price:
81 pricing_rule = get_pricing_rule_for_item(frappe._dict({
82 "item_code": item_code,
83 "qty": qty,
kasgela59f8642020-09-05 12:15:49 +020084 "stock_qty": qty,
Faris Ansarifd345f82017-10-05 11:17:30 +053085 "transaction_type": "selling",
86 "price_list": price_list,
87 "customer_group": customer_group,
88 "company": company,
89 "conversion_rate": 1,
Charles-Henri Decultot614959d2018-08-06 11:12:04 +020090 "for_shopping_cart": True,
91 "currency": frappe.db.get_value("Price List", price_list, "currency")
Faris Ansarifd345f82017-10-05 11:17:30 +053092 }))
marination60261852021-04-13 00:39:26 +053093 price_obj = price[0]
Faris Ansarifd345f82017-10-05 11:17:30 +053094
95 if pricing_rule:
marination60261852021-04-13 00:39:26 +053096 # price without any rules applied
97 mrp = price_obj.price_list_rate or 0
98
Faris Ansarifd345f82017-10-05 11:17:30 +053099 if pricing_rule.pricing_rule_for == "Discount Percentage":
marination1d949142021-04-20 21:54:52 +0530100 price_obj.discount_percent = pricing_rule.discount_percentage
marination60261852021-04-13 00:39:26 +0530101 price_obj.formatted_discount_percent = str(flt(pricing_rule.discount_percentage, 0)) + "%"
102 price_obj.price_list_rate = flt(price_obj.price_list_rate * (1.0 - (flt(pricing_rule.discount_percentage) / 100.0)))
Faris Ansarifd345f82017-10-05 11:17:30 +0530103
Charles-Henri Decultot614959d2018-08-06 11:12:04 +0200104 if pricing_rule.pricing_rule_for == "Rate":
marination60261852021-04-13 00:39:26 +0530105 rate_discount = flt(mrp) - flt(pricing_rule.price_list_rate)
106 if rate_discount > 0:
107 price_obj.formatted_discount_rate = fmt_money(rate_discount, currency=price_obj["currency"])
108 price_obj.price_list_rate = pricing_rule.price_list_rate or 0
Faris Ansarifd345f82017-10-05 11:17:30 +0530109
Faris Ansarifd345f82017-10-05 11:17:30 +0530110 if price_obj:
111 price_obj["formatted_price"] = fmt_money(price_obj["price_list_rate"], currency=price_obj["currency"])
marination60261852021-04-13 00:39:26 +0530112 if mrp != price_obj["price_list_rate"]:
113 price_obj["formatted_mrp"] = fmt_money(mrp, currency=price_obj["currency"])
Faris Ansarifd345f82017-10-05 11:17:30 +0530114
115 price_obj["currency_symbol"] = not cint(frappe.db.get_default("hide_currency_symbol")) \
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530116 and (frappe.db.get_value("Currency", price_obj.currency, "symbol", cache=True) or price_obj.currency) \
Faris Ansarifd345f82017-10-05 11:17:30 +0530117 or ""
118
Britloge3a9b9f2018-03-20 09:58:44 +0100119 uom_conversion_factor = frappe.db.sql("""select C.conversion_factor
120 from `tabUOM Conversion Detail` C
Britlog7d9689c2018-11-13 07:06:41 +0100121 inner join `tabItem` I on C.parent = I.name and C.uom = I.sales_uom
122 where I.name = %s""", item_code)
Britloge3a9b9f2018-03-20 09:58:44 +0100123
124 uom_conversion_factor = uom_conversion_factor[0][0] if uom_conversion_factor else 1
125 price_obj["formatted_price_sales_uom"] = fmt_money(price_obj["price_list_rate"] * uom_conversion_factor, currency=price_obj["currency"])
126
Faris Ansarifd345f82017-10-05 11:17:30 +0530127 if not price_obj["price_list_rate"]:
128 price_obj["price_list_rate"] = 0
129
130 if not price_obj["currency"]:
131 price_obj["currency"] = ""
132
133 if not price_obj["formatted_price"]:
marination60261852021-04-13 00:39:26 +0530134 price_obj["formatted_price"], price_obj["formatted_mrp"] = "", ""
Faris Ansarifd345f82017-10-05 11:17:30 +0530135
136 return price_obj
Marica40dffab2020-01-24 15:52:27 +0530137
138def get_non_stock_item_status(item_code, item_warehouse_field):
marination60261852021-04-13 00:39:26 +0530139 # if item is a product bundle, check if its bundle items are in stock
Marica40dffab2020-01-24 15:52:27 +0530140 if frappe.db.exists("Product Bundle", item_code):
141 items = frappe.get_doc("Product Bundle", item_code).get_all_children()
marination6b2b9dc2021-08-25 13:09:35 +0530142 bundle_warehouse = frappe.db.get_value("Website Item", {"item_code": item_code}, item_warehouse_field)
143 return all(get_web_item_qty_in_stock(d.item_code, item_warehouse_field, bundle_warehouse).in_stock for d in items)
Marica40dffab2020-01-24 15:52:27 +0530144 else:
145 return 1