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 | e3a9b9f | 2018-03-20 09:58:44 +0100 | [diff] [blame] | 22 | stock_qty = frappe.db.sql(""" |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 23 | 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) |
Britlog | e3a9b9f | 2018-03-20 09:58:44 +0100 | [diff] [blame] | 24 | from tabBin S |
| 25 | inner join `tabItem` I on S.item_code = I.Item_code |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 26 | left join `tabUOM Conversion Detail` C on I.sales_uom = C.uom and C.parent = I.Item_code |
Britlog | e3a9b9f | 2018-03-20 09:58:44 +0100 | [diff] [blame] | 27 | where S.item_code=%s and S.warehouse=%s""", (item_code, warehouse)) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 28 | |
Britlog | e3a9b9f | 2018-03-20 09:58:44 +0100 | [diff] [blame] | 29 | if stock_qty: |
| 30 | stock_qty = adjust_qty_for_expired_items(item_code, stock_qty, warehouse) |
| 31 | in_stock = stock_qty[0][0] > 0 and 1 or 0 |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 32 | |
Manas Solanki | a521efc | 2018-01-05 12:47:20 +0530 | [diff] [blame] | 33 | 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] | 34 | |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 35 | |
tundebabzy | 29c8142 | 2018-01-31 10:36:31 +0100 | [diff] [blame] | 36 | def adjust_qty_for_expired_items(item_code, stock_qty, warehouse): |
| 37 | batches = frappe.get_all('Batch', filters=[{'item': item_code}], fields=['expiry_date', 'name']) |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 38 | expired_batches = get_expired_batches(batches) |
| 39 | stock_qty = [list(item) for item in stock_qty] |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 40 | |
tundebabzy | 29c8142 | 2018-01-31 10:36:31 +0100 | [diff] [blame] | 41 | for batch in expired_batches: |
| 42 | if warehouse: |
| 43 | stock_qty[0][0] = max(0, stock_qty[0][0] - get_batch_qty(batch, warehouse)) |
| 44 | else: |
| 45 | 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] | 46 | |
tundebabzy | 29c8142 | 2018-01-31 10:36:31 +0100 | [diff] [blame] | 47 | if not stock_qty[0][0]: |
| 48 | break |
| 49 | |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 50 | return stock_qty |
| 51 | |
| 52 | |
| 53 | def get_expired_batches(batches): |
| 54 | """ |
| 55 | :param batches: A list of dict in the form [{'expiry_date': datetime.date(20XX, 1, 1), 'name': 'batch_id'}, ...] |
| 56 | """ |
| 57 | return [b.name for b in batches if b.expiry_date and b.expiry_date <= getdate(nowdate())] |
| 58 | |
| 59 | |
| 60 | def qty_from_all_warehouses(batch_info): |
| 61 | """ |
| 62 | :param batch_info: A list of dict in the form [{u'warehouse': u'Stores - I', u'qty': 0.8}, ...] |
| 63 | """ |
| 64 | qty = 0 |
| 65 | for batch in batch_info: |
| 66 | qty = qty + batch.qty |
| 67 | |
| 68 | return qty |
| 69 | |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 70 | def get_price(item_code, price_list, customer_group, company, qty=1): |
| 71 | template_item_code = frappe.db.get_value("Item", item_code, "variant_of") |
| 72 | |
| 73 | if price_list: |
| 74 | price = frappe.get_all("Item Price", fields=["price_list_rate", "currency"], |
| 75 | filters={"price_list": price_list, "item_code": item_code}) |
| 76 | |
| 77 | if template_item_code and not price: |
| 78 | price = frappe.get_all("Item Price", fields=["price_list_rate", "currency"], |
| 79 | filters={"price_list": price_list, "item_code": template_item_code}) |
| 80 | |
| 81 | if price: |
| 82 | pricing_rule = get_pricing_rule_for_item(frappe._dict({ |
| 83 | "item_code": item_code, |
| 84 | "qty": qty, |
| 85 | "transaction_type": "selling", |
| 86 | "price_list": price_list, |
| 87 | "customer_group": customer_group, |
| 88 | "company": company, |
| 89 | "conversion_rate": 1, |
Charles-Henri Decultot | 614959d | 2018-08-06 11:12:04 +0200 | [diff] [blame] | 90 | "for_shopping_cart": True, |
| 91 | "currency": frappe.db.get_value("Price List", price_list, "currency") |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 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 | |
Charles-Henri Decultot | 614959d | 2018-08-06 11:12:04 +0200 | [diff] [blame] | 98 | if pricing_rule.pricing_rule_for == "Rate": |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 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")) \ |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 106 | and (frappe.db.get_value("Currency", price_obj.currency, "symbol", cache=True) or price_obj.currency) \ |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 107 | or "" |
| 108 | |
Britlog | e3a9b9f | 2018-03-20 09:58:44 +0100 | [diff] [blame] | 109 | uom_conversion_factor = frappe.db.sql("""select C.conversion_factor |
| 110 | from `tabUOM Conversion Detail` C |
| 111 | inner join `tabItem` I on C.uom = I.sales_uom |
| 112 | where C.parent = %s""", item_code) |
| 113 | |
| 114 | uom_conversion_factor = uom_conversion_factor[0][0] if uom_conversion_factor else 1 |
| 115 | price_obj["formatted_price_sales_uom"] = fmt_money(price_obj["price_list_rate"] * uom_conversion_factor, currency=price_obj["currency"]) |
| 116 | |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 117 | if not price_obj["price_list_rate"]: |
| 118 | price_obj["price_list_rate"] = 0 |
| 119 | |
| 120 | if not price_obj["currency"]: |
| 121 | price_obj["currency"] = "" |
| 122 | |
| 123 | if not price_obj["formatted_price"]: |
| 124 | price_obj["formatted_price"] = "" |
| 125 | |
| 126 | return price_obj |