blob: e567f771e95843c58fcc3389b56b53071f266075 [file] [log] [blame]
Faris Ansarifd345f82017-10-05 11:17:30 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
4from __future__ import unicode_literals
5
6import frappe
Chillar Anand915b3432021-09-02 16:44:59 +05307from frappe.utils import cint, flt, fmt_money, getdate, nowdate
8
Faris Ansarifd345f82017-10-05 11:17:30 +05309from erpnext.accounts.doctype.pricing_rule.pricing_rule import get_pricing_rule_for_item
tundebabzyc14f1f12018-01-27 06:28:29 +010010from erpnext.stock.doctype.batch.batch import get_batch_qty
Faris Ansarifd345f82017-10-05 11:17:30 +053011
Chillar Anand915b3432021-09-02 16:44:59 +053012
tundebabzyc14f1f12018-01-27 06:28:29 +010013def get_qty_in_stock(item_code, item_warehouse_field, warehouse=None):
Faris Ansarifd345f82017-10-05 11:17:30 +053014 in_stock, stock_qty = 0, ''
Manas Solankia521efc2018-01-05 12:47:20 +053015 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 +053016
tundebabzyc14f1f12018-01-27 06:28:29 +010017 if not warehouse:
18 warehouse = frappe.db.get_value("Item", item_code, item_warehouse_field)
19
Faris Ansarifd345f82017-10-05 11:17:30 +053020 if not warehouse and template_item_code and template_item_code != item_code:
21 warehouse = frappe.db.get_value("Item", template_item_code, item_warehouse_field)
22
23 if warehouse:
Britloge3a9b9f2018-03-20 09:58:44 +010024 stock_qty = frappe.db.sql("""
Rushabh Mehta708e47a2018-08-08 16:37:31 +053025 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 +010026 from tabBin S
27 inner join `tabItem` I on S.item_code = I.Item_code
Rushabh Mehta708e47a2018-08-08 16:37:31 +053028 left join `tabUOM Conversion Detail` C on I.sales_uom = C.uom and C.parent = I.Item_code
Britloge3a9b9f2018-03-20 09:58:44 +010029 where S.item_code=%s and S.warehouse=%s""", (item_code, warehouse))
Faris Ansarifd345f82017-10-05 11:17:30 +053030
Britloge3a9b9f2018-03-20 09:58:44 +010031 if stock_qty:
32 stock_qty = adjust_qty_for_expired_items(item_code, stock_qty, warehouse)
33 in_stock = stock_qty[0][0] > 0 and 1 or 0
tundebabzyc14f1f12018-01-27 06:28:29 +010034
Manas Solankia521efc2018-01-05 12:47:20 +053035 return frappe._dict({"in_stock": in_stock, "stock_qty": stock_qty, "is_stock_item": is_stock_item})
Faris Ansarifd345f82017-10-05 11:17:30 +053036
tundebabzyc14f1f12018-01-27 06:28:29 +010037
tundebabzy29c81422018-01-31 10:36:31 +010038def adjust_qty_for_expired_items(item_code, stock_qty, warehouse):
39 batches = frappe.get_all('Batch', filters=[{'item': item_code}], fields=['expiry_date', 'name'])
tundebabzyc14f1f12018-01-27 06:28:29 +010040 expired_batches = get_expired_batches(batches)
41 stock_qty = [list(item) for item in stock_qty]
Rushabh Mehta708e47a2018-08-08 16:37:31 +053042
tundebabzy29c81422018-01-31 10:36:31 +010043 for batch in expired_batches:
44 if warehouse:
45 stock_qty[0][0] = max(0, stock_qty[0][0] - get_batch_qty(batch, warehouse))
46 else:
47 stock_qty[0][0] = max(0, stock_qty[0][0] - qty_from_all_warehouses(get_batch_qty(batch)))
tundebabzyc14f1f12018-01-27 06:28:29 +010048
tundebabzy29c81422018-01-31 10:36:31 +010049 if not stock_qty[0][0]:
50 break
51
tundebabzyc14f1f12018-01-27 06:28:29 +010052 return stock_qty
53
54
55def get_expired_batches(batches):
56 """
57 :param batches: A list of dict in the form [{'expiry_date': datetime.date(20XX, 1, 1), 'name': 'batch_id'}, ...]
58 """
59 return [b.name for b in batches if b.expiry_date and b.expiry_date <= getdate(nowdate())]
60
61
62def qty_from_all_warehouses(batch_info):
63 """
64 :param batch_info: A list of dict in the form [{u'warehouse': u'Stores - I', u'qty': 0.8}, ...]
65 """
66 qty = 0
67 for batch in batch_info:
68 qty = qty + batch.qty
69
70 return qty
71
Faris Ansarifd345f82017-10-05 11:17:30 +053072def get_price(item_code, price_list, customer_group, company, qty=1):
73 template_item_code = frappe.db.get_value("Item", item_code, "variant_of")
74
75 if price_list:
76 price = frappe.get_all("Item Price", fields=["price_list_rate", "currency"],
77 filters={"price_list": price_list, "item_code": item_code})
78
79 if template_item_code and not price:
80 price = frappe.get_all("Item Price", fields=["price_list_rate", "currency"],
81 filters={"price_list": price_list, "item_code": template_item_code})
82
83 if price:
84 pricing_rule = get_pricing_rule_for_item(frappe._dict({
85 "item_code": item_code,
86 "qty": qty,
kasgela59f8642020-09-05 12:15:49 +020087 "stock_qty": qty,
Faris Ansarifd345f82017-10-05 11:17:30 +053088 "transaction_type": "selling",
89 "price_list": price_list,
90 "customer_group": customer_group,
91 "company": company,
92 "conversion_rate": 1,
Charles-Henri Decultot614959d2018-08-06 11:12:04 +020093 "for_shopping_cart": True,
94 "currency": frappe.db.get_value("Price List", price_list, "currency")
Faris Ansarifd345f82017-10-05 11:17:30 +053095 }))
96
97 if pricing_rule:
98 if pricing_rule.pricing_rule_for == "Discount Percentage":
99 price[0].price_list_rate = flt(price[0].price_list_rate * (1.0 - (flt(pricing_rule.discount_percentage) / 100.0)))
100
Charles-Henri Decultot614959d2018-08-06 11:12:04 +0200101 if pricing_rule.pricing_rule_for == "Rate":
Faris Ansarifd345f82017-10-05 11:17:30 +0530102 price[0].price_list_rate = pricing_rule.price_list_rate
103
104 price_obj = price[0]
105 if price_obj:
106 price_obj["formatted_price"] = fmt_money(price_obj["price_list_rate"], currency=price_obj["currency"])
107
108 price_obj["currency_symbol"] = not cint(frappe.db.get_default("hide_currency_symbol")) \
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530109 and (frappe.db.get_value("Currency", price_obj.currency, "symbol", cache=True) or price_obj.currency) \
Faris Ansarifd345f82017-10-05 11:17:30 +0530110 or ""
111
Britloge3a9b9f2018-03-20 09:58:44 +0100112 uom_conversion_factor = frappe.db.sql("""select C.conversion_factor
113 from `tabUOM Conversion Detail` C
Britlog7d9689c2018-11-13 07:06:41 +0100114 inner join `tabItem` I on C.parent = I.name and C.uom = I.sales_uom
115 where I.name = %s""", item_code)
Britloge3a9b9f2018-03-20 09:58:44 +0100116
117 uom_conversion_factor = uom_conversion_factor[0][0] if uom_conversion_factor else 1
118 price_obj["formatted_price_sales_uom"] = fmt_money(price_obj["price_list_rate"] * uom_conversion_factor, currency=price_obj["currency"])
119
Faris Ansarifd345f82017-10-05 11:17:30 +0530120 if not price_obj["price_list_rate"]:
121 price_obj["price_list_rate"] = 0
122
123 if not price_obj["currency"]:
124 price_obj["currency"] = ""
125
126 if not price_obj["formatted_price"]:
127 price_obj["formatted_price"] = ""
128
129 return price_obj
Marica40dffab2020-01-24 15:52:27 +0530130
131def get_non_stock_item_status(item_code, item_warehouse_field):
Ankush Menat010060d2021-09-01 14:49:47 +0530132 #if item belongs to product bundle, check if bundle items are in stock
Marica40dffab2020-01-24 15:52:27 +0530133 if frappe.db.exists("Product Bundle", item_code):
134 items = frappe.get_doc("Product Bundle", item_code).get_all_children()
Saqib56837bc2020-02-20 12:23:08 +0530135 bundle_warehouse = frappe.db.get_value('Item', item_code, item_warehouse_field)
Ankush Menat98917802021-06-11 18:40:22 +0530136 return all(get_qty_in_stock(d.item_code, item_warehouse_field, bundle_warehouse).in_stock for d in items)
Marica40dffab2020-01-24 15:52:27 +0530137 else:
138 return 1