blob: 415056d80bca8930c0a434d8ae1711a307e5a2e4 [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
tundebabzyc14f1f12018-01-27 06:28:29 +01007from frappe.utils import cint, fmt_money, flt, nowdate, getdate
Faris Ansarifd345f82017-10-05 11:17:30 +05308from erpnext.accounts.doctype.pricing_rule.pricing_rule import get_pricing_rule_for_item
tundebabzyc14f1f12018-01-27 06:28:29 +01009from erpnext.stock.doctype.batch.batch import get_batch_qty
Faris Ansarifd345f82017-10-05 11:17:30 +053010
tundebabzyc14f1f12018-01-27 06:28:29 +010011def get_qty_in_stock(item_code, item_warehouse_field, warehouse=None):
Faris Ansarifd345f82017-10-05 11:17:30 +053012 in_stock, stock_qty = 0, ''
Manas Solankia521efc2018-01-05 12:47:20 +053013 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 +053014
tundebabzyc14f1f12018-01-27 06:28:29 +010015 if not warehouse:
16 warehouse = frappe.db.get_value("Item", item_code, item_warehouse_field)
17
Faris Ansarifd345f82017-10-05 11:17:30 +053018 if not warehouse and template_item_code and template_item_code != item_code:
19 warehouse = frappe.db.get_value("Item", template_item_code, item_warehouse_field)
20
21 if warehouse:
Britlog2a89a502017-11-17 07:11:07 +010022 stock_qty = frappe.db.sql("""select GREATEST(actual_qty - reserved_qty, 0) from tabBin where
Faris Ansarifd345f82017-10-05 11:17:30 +053023 item_code=%s and warehouse=%s""", (item_code, warehouse))
Faris Ansarifd345f82017-10-05 11:17:30 +053024
tundebabzy5df64d82018-02-01 17:05:54 +010025 if stock_qty:
tundebabzy29c81422018-01-31 10:36:31 +010026 stock_qty = adjust_qty_for_expired_items(item_code, stock_qty, warehouse)
tundebabzyc14f1f12018-01-27 06:28:29 +010027
tundebabzy5df64d82018-02-01 17:05:54 +010028 if stock_qty:
tundebabzyc14f1f12018-01-27 06:28:29 +010029 in_stock = stock_qty[0][0] > 0 and 1 or 0
30
tundebabzyc7c1def2018-01-27 06:30:56 +010031 if stock_qty:
32 in_stock = stock_qty[0][0] > 0 and 1 or 0
33
Manas Solankia521efc2018-01-05 12:47:20 +053034 return frappe._dict({"in_stock": in_stock, "stock_qty": stock_qty, "is_stock_item": is_stock_item})
Faris Ansarifd345f82017-10-05 11:17:30 +053035
tundebabzyc14f1f12018-01-27 06:28:29 +010036
tundebabzy29c81422018-01-31 10:36:31 +010037def adjust_qty_for_expired_items(item_code, stock_qty, warehouse):
38 batches = frappe.get_all('Batch', filters=[{'item': item_code}], fields=['expiry_date', 'name'])
tundebabzyc14f1f12018-01-27 06:28:29 +010039 expired_batches = get_expired_batches(batches)
40 stock_qty = [list(item) for item in stock_qty]
41
tundebabzy29c81422018-01-31 10:36:31 +010042 for batch in expired_batches:
43 if warehouse:
44 stock_qty[0][0] = max(0, stock_qty[0][0] - get_batch_qty(batch, warehouse))
45 else:
46 stock_qty[0][0] = max(0, stock_qty[0][0] - qty_from_all_warehouses(get_batch_qty(batch)))
tundebabzyc14f1f12018-01-27 06:28:29 +010047
tundebabzy29c81422018-01-31 10:36:31 +010048 if not stock_qty[0][0]:
49 break
50
tundebabzyc14f1f12018-01-27 06:28:29 +010051 return stock_qty
52
53
54def get_expired_batches(batches):
55 """
56 :param batches: A list of dict in the form [{'expiry_date': datetime.date(20XX, 1, 1), 'name': 'batch_id'}, ...]
57 """
58 return [b.name for b in batches if b.expiry_date and b.expiry_date <= getdate(nowdate())]
59
60
61def qty_from_all_warehouses(batch_info):
62 """
63 :param batch_info: A list of dict in the form [{u'warehouse': u'Stores - I', u'qty': 0.8}, ...]
64 """
65 qty = 0
66 for batch in batch_info:
67 qty = qty + batch.qty
68
69 return qty
70
Faris Ansarifd345f82017-10-05 11:17:30 +053071def get_price(item_code, price_list, customer_group, company, qty=1):
72 template_item_code = frappe.db.get_value("Item", item_code, "variant_of")
73
74 if price_list:
75 price = frappe.get_all("Item Price", fields=["price_list_rate", "currency"],
76 filters={"price_list": price_list, "item_code": item_code})
77
78 if template_item_code and not price:
79 price = frappe.get_all("Item Price", fields=["price_list_rate", "currency"],
80 filters={"price_list": price_list, "item_code": template_item_code})
81
82 if price:
83 pricing_rule = get_pricing_rule_for_item(frappe._dict({
84 "item_code": item_code,
85 "qty": qty,
86 "transaction_type": "selling",
87 "price_list": price_list,
88 "customer_group": customer_group,
89 "company": company,
90 "conversion_rate": 1,
91 "for_shopping_cart": True
92 }))
93
94 if pricing_rule:
95 if pricing_rule.pricing_rule_for == "Discount Percentage":
96 price[0].price_list_rate = flt(price[0].price_list_rate * (1.0 - (flt(pricing_rule.discount_percentage) / 100.0)))
97
98 if pricing_rule.pricing_rule_for == "Price":
99 price[0].price_list_rate = pricing_rule.price_list_rate
100
101 price_obj = price[0]
102 if price_obj:
103 price_obj["formatted_price"] = fmt_money(price_obj["price_list_rate"], currency=price_obj["currency"])
104
105 price_obj["currency_symbol"] = not cint(frappe.db.get_default("hide_currency_symbol")) \
106 and (frappe.db.get_value("Currency", price_obj.currency, "symbol") or price_obj.currency) \
107 or ""
108
109 if not price_obj["price_list_rate"]:
110 price_obj["price_list_rate"] = 0
111
112 if not price_obj["currency"]:
113 price_obj["currency"] = ""
114
115 if not price_obj["formatted_price"]:
116 price_obj["formatted_price"] = ""
117
118 return price_obj