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 | |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 4 | |
| 5 | import frappe |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 6 | from frappe.utils import cint, flt, fmt_money, getdate, nowdate |
| 7 | |
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 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 11 | |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 12 | def get_qty_in_stock(item_code, item_warehouse_field, warehouse=None): |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 13 | in_stock, stock_qty = 0, '' |
Manas Solanki | a521efc | 2018-01-05 12:47:20 +0530 | [diff] [blame] | 14 | 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] | 15 | |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 16 | if not warehouse: |
| 17 | warehouse = frappe.db.get_value("Item", item_code, item_warehouse_field) |
| 18 | |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 19 | if not warehouse and template_item_code and template_item_code != item_code: |
| 20 | warehouse = frappe.db.get_value("Item", template_item_code, item_warehouse_field) |
| 21 | |
| 22 | if warehouse: |
Britlog | e3a9b9f | 2018-03-20 09:58:44 +0100 | [diff] [blame] | 23 | stock_qty = frappe.db.sql(""" |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 24 | 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] | 25 | from tabBin S |
| 26 | inner join `tabItem` I on S.item_code = I.Item_code |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 27 | 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] | 28 | where S.item_code=%s and S.warehouse=%s""", (item_code, warehouse)) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 29 | |
Britlog | e3a9b9f | 2018-03-20 09:58:44 +0100 | [diff] [blame] | 30 | if stock_qty: |
| 31 | stock_qty = adjust_qty_for_expired_items(item_code, stock_qty, warehouse) |
| 32 | in_stock = stock_qty[0][0] > 0 and 1 or 0 |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 33 | |
Manas Solanki | a521efc | 2018-01-05 12:47:20 +0530 | [diff] [blame] | 34 | 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] | 35 | |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 36 | |
tundebabzy | 29c8142 | 2018-01-31 10:36:31 +0100 | [diff] [blame] | 37 | def adjust_qty_for_expired_items(item_code, stock_qty, warehouse): |
| 38 | batches = frappe.get_all('Batch', filters=[{'item': item_code}], fields=['expiry_date', 'name']) |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 39 | expired_batches = get_expired_batches(batches) |
| 40 | stock_qty = [list(item) for item in stock_qty] |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 41 | |
tundebabzy | 29c8142 | 2018-01-31 10:36:31 +0100 | [diff] [blame] | 42 | 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))) |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 47 | |
tundebabzy | 29c8142 | 2018-01-31 10:36:31 +0100 | [diff] [blame] | 48 | if not stock_qty[0][0]: |
| 49 | break |
| 50 | |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 51 | return stock_qty |
| 52 | |
| 53 | |
| 54 | def 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 | |
| 61 | def 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 Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 71 | def 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, |
kasgel | a59f864 | 2020-09-05 12:15:49 +0200 | [diff] [blame] | 86 | "stock_qty": qty, |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 87 | "transaction_type": "selling", |
| 88 | "price_list": price_list, |
| 89 | "customer_group": customer_group, |
| 90 | "company": company, |
| 91 | "conversion_rate": 1, |
Charles-Henri Decultot | 614959d | 2018-08-06 11:12:04 +0200 | [diff] [blame] | 92 | "for_shopping_cart": True, |
| 93 | "currency": frappe.db.get_value("Price List", price_list, "currency") |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 94 | })) |
| 95 | |
| 96 | if pricing_rule: |
| 97 | if pricing_rule.pricing_rule_for == "Discount Percentage": |
| 98 | price[0].price_list_rate = flt(price[0].price_list_rate * (1.0 - (flt(pricing_rule.discount_percentage) / 100.0))) |
| 99 | |
Charles-Henri Decultot | 614959d | 2018-08-06 11:12:04 +0200 | [diff] [blame] | 100 | if pricing_rule.pricing_rule_for == "Rate": |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 101 | price[0].price_list_rate = pricing_rule.price_list_rate |
| 102 | |
| 103 | price_obj = price[0] |
| 104 | if price_obj: |
| 105 | price_obj["formatted_price"] = fmt_money(price_obj["price_list_rate"], currency=price_obj["currency"]) |
| 106 | |
| 107 | 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] | 108 | 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] | 109 | or "" |
| 110 | |
Britlog | e3a9b9f | 2018-03-20 09:58:44 +0100 | [diff] [blame] | 111 | uom_conversion_factor = frappe.db.sql("""select C.conversion_factor |
| 112 | from `tabUOM Conversion Detail` C |
Britlog | 7d9689c | 2018-11-13 07:06:41 +0100 | [diff] [blame] | 113 | inner join `tabItem` I on C.parent = I.name and C.uom = I.sales_uom |
| 114 | where I.name = %s""", item_code) |
Britlog | e3a9b9f | 2018-03-20 09:58:44 +0100 | [diff] [blame] | 115 | |
| 116 | uom_conversion_factor = uom_conversion_factor[0][0] if uom_conversion_factor else 1 |
| 117 | price_obj["formatted_price_sales_uom"] = fmt_money(price_obj["price_list_rate"] * uom_conversion_factor, currency=price_obj["currency"]) |
| 118 | |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 119 | if not price_obj["price_list_rate"]: |
| 120 | price_obj["price_list_rate"] = 0 |
| 121 | |
| 122 | if not price_obj["currency"]: |
| 123 | price_obj["currency"] = "" |
| 124 | |
| 125 | if not price_obj["formatted_price"]: |
| 126 | price_obj["formatted_price"] = "" |
| 127 | |
| 128 | return price_obj |
Marica | 40dffab | 2020-01-24 15:52:27 +0530 | [diff] [blame] | 129 | |
| 130 | def get_non_stock_item_status(item_code, item_warehouse_field): |
Ankush Menat | 010060d | 2021-09-01 14:49:47 +0530 | [diff] [blame] | 131 | #if item belongs to product bundle, check if bundle items are in stock |
Marica | 40dffab | 2020-01-24 15:52:27 +0530 | [diff] [blame] | 132 | if frappe.db.exists("Product Bundle", item_code): |
| 133 | items = frappe.get_doc("Product Bundle", item_code).get_all_children() |
Saqib | 56837bc | 2020-02-20 12:23:08 +0530 | [diff] [blame] | 134 | bundle_warehouse = frappe.db.get_value('Item', item_code, item_warehouse_field) |
Ankush Menat | 9891780 | 2021-06-11 18:40:22 +0530 | [diff] [blame] | 135 | return all(get_qty_in_stock(d.item_code, item_warehouse_field, bundle_warehouse).in_stock for d in items) |
Marica | 40dffab | 2020-01-24 15:52:27 +0530 | [diff] [blame] | 136 | else: |
| 137 | return 1 |