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 |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 7 | from frappe.utils import cint, fmt_money, flt, nowdate, getdate |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 8 | from erpnext.accounts.doctype.pricing_rule.pricing_rule import get_pricing_rule_for_item |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 9 | from erpnext.stock.doctype.batch.batch import get_batch_qty |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 10 | |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 11 | def get_qty_in_stock(item_code, item_warehouse_field, warehouse=None): |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 12 | in_stock, stock_qty = 0, '' |
Manas Solanki | a521efc | 2018-01-05 12:47:20 +0530 | [diff] [blame] | 13 | template_item_code, is_stock_item = frappe.db.get_value("Item", item_code, ["variant_of", "is_stock_item"]) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 14 | |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 15 | if not warehouse: |
| 16 | warehouse = frappe.db.get_value("Item", item_code, item_warehouse_field) |
| 17 | |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 18 | 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: |
Britlog | 2a89a50 | 2017-11-17 07:11:07 +0100 | [diff] [blame] | 22 | stock_qty = frappe.db.sql("""select GREATEST(actual_qty - reserved_qty, 0) from tabBin where |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 23 | item_code=%s and warehouse=%s""", (item_code, warehouse)) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 24 | |
tundebabzy | 5df64d8 | 2018-02-01 17:05:54 +0100 | [diff] [blame] | 25 | if stock_qty: |
tundebabzy | 29c8142 | 2018-01-31 10:36:31 +0100 | [diff] [blame] | 26 | stock_qty = adjust_qty_for_expired_items(item_code, stock_qty, warehouse) |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 27 | |
tundebabzy | 5df64d8 | 2018-02-01 17:05:54 +0100 | [diff] [blame] | 28 | if stock_qty: |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 29 | in_stock = stock_qty[0][0] > 0 and 1 or 0 |
| 30 | |
Manas Solanki | a521efc | 2018-01-05 12:47:20 +0530 | [diff] [blame] | 31 | return frappe._dict({"in_stock": in_stock, "stock_qty": stock_qty, "is_stock_item": is_stock_item}) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 32 | |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 33 | |
tundebabzy | 29c8142 | 2018-01-31 10:36:31 +0100 | [diff] [blame] | 34 | def adjust_qty_for_expired_items(item_code, stock_qty, warehouse): |
| 35 | batches = frappe.get_all('Batch', filters=[{'item': item_code}], fields=['expiry_date', 'name']) |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 36 | expired_batches = get_expired_batches(batches) |
| 37 | stock_qty = [list(item) for item in stock_qty] |
| 38 | |
tundebabzy | 29c8142 | 2018-01-31 10:36:31 +0100 | [diff] [blame] | 39 | for batch in expired_batches: |
| 40 | if warehouse: |
| 41 | stock_qty[0][0] = max(0, stock_qty[0][0] - get_batch_qty(batch, warehouse)) |
| 42 | else: |
| 43 | stock_qty[0][0] = max(0, stock_qty[0][0] - qty_from_all_warehouses(get_batch_qty(batch))) |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 44 | |
tundebabzy | 29c8142 | 2018-01-31 10:36:31 +0100 | [diff] [blame] | 45 | if not stock_qty[0][0]: |
| 46 | break |
| 47 | |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 48 | return stock_qty |
| 49 | |
| 50 | |
| 51 | def get_expired_batches(batches): |
| 52 | """ |
| 53 | :param batches: A list of dict in the form [{'expiry_date': datetime.date(20XX, 1, 1), 'name': 'batch_id'}, ...] |
| 54 | """ |
| 55 | return [b.name for b in batches if b.expiry_date and b.expiry_date <= getdate(nowdate())] |
| 56 | |
| 57 | |
| 58 | def qty_from_all_warehouses(batch_info): |
| 59 | """ |
| 60 | :param batch_info: A list of dict in the form [{u'warehouse': u'Stores - I', u'qty': 0.8}, ...] |
| 61 | """ |
| 62 | qty = 0 |
| 63 | for batch in batch_info: |
| 64 | qty = qty + batch.qty |
| 65 | |
| 66 | return qty |
| 67 | |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 68 | def get_price(item_code, price_list, customer_group, company, qty=1): |
| 69 | template_item_code = frappe.db.get_value("Item", item_code, "variant_of") |
| 70 | |
| 71 | if price_list: |
| 72 | price = frappe.get_all("Item Price", fields=["price_list_rate", "currency"], |
| 73 | filters={"price_list": price_list, "item_code": item_code}) |
| 74 | |
| 75 | if template_item_code and not price: |
| 76 | price = frappe.get_all("Item Price", fields=["price_list_rate", "currency"], |
| 77 | filters={"price_list": price_list, "item_code": template_item_code}) |
| 78 | |
| 79 | if price: |
| 80 | pricing_rule = get_pricing_rule_for_item(frappe._dict({ |
| 81 | "item_code": item_code, |
| 82 | "qty": qty, |
| 83 | "transaction_type": "selling", |
| 84 | "price_list": price_list, |
| 85 | "customer_group": customer_group, |
| 86 | "company": company, |
| 87 | "conversion_rate": 1, |
| 88 | "for_shopping_cart": True |
| 89 | })) |
| 90 | |
| 91 | if pricing_rule: |
| 92 | if pricing_rule.pricing_rule_for == "Discount Percentage": |
| 93 | price[0].price_list_rate = flt(price[0].price_list_rate * (1.0 - (flt(pricing_rule.discount_percentage) / 100.0))) |
| 94 | |
| 95 | if pricing_rule.pricing_rule_for == "Price": |
| 96 | price[0].price_list_rate = pricing_rule.price_list_rate |
| 97 | |
| 98 | price_obj = price[0] |
| 99 | if price_obj: |
| 100 | price_obj["formatted_price"] = fmt_money(price_obj["price_list_rate"], currency=price_obj["currency"]) |
| 101 | |
| 102 | price_obj["currency_symbol"] = not cint(frappe.db.get_default("hide_currency_symbol")) \ |
| 103 | and (frappe.db.get_value("Currency", price_obj.currency, "symbol") or price_obj.currency) \ |
| 104 | or "" |
| 105 | |
| 106 | if not price_obj["price_list_rate"]: |
| 107 | price_obj["price_list_rate"] = 0 |
| 108 | |
| 109 | if not price_obj["currency"]: |
| 110 | price_obj["currency"] = "" |
| 111 | |
| 112 | if not price_obj["formatted_price"]: |
| 113 | price_obj["formatted_price"] = "" |
| 114 | |
| 115 | return price_obj |