marination | 6026185 | 2021-04-13 00:39:26 +0530 | [diff] [blame] | 1 | # Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
| 3 | |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 4 | import frappe |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 5 | from frappe.utils import cint, flt, fmt_money, getdate, nowdate |
| 6 | |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 7 | 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] | 8 | from erpnext.stock.doctype.batch.batch import get_batch_qty |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 9 | |
marination | 9fb61ef | 2022-02-01 00:39:14 +0530 | [diff] [blame] | 10 | |
marination | 6026185 | 2021-04-13 00:39:26 +0530 | [diff] [blame] | 11 | def get_web_item_qty_in_stock(item_code, item_warehouse_field, warehouse=None): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 12 | in_stock, stock_qty = 0, "" |
| 13 | template_item_code, is_stock_item = frappe.db.get_value( |
| 14 | "Item", item_code, ["variant_of", "is_stock_item"] |
| 15 | ) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 16 | |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 17 | if not warehouse: |
marination | 6026185 | 2021-04-13 00:39:26 +0530 | [diff] [blame] | 18 | warehouse = frappe.db.get_value("Website Item", {"item_code": item_code}, item_warehouse_field) |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 19 | |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 20 | if not warehouse and template_item_code and template_item_code != item_code: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 21 | warehouse = frappe.db.get_value( |
| 22 | "Website Item", {"item_code": template_item_code}, item_warehouse_field |
| 23 | ) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 24 | |
| 25 | if warehouse: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 26 | stock_qty = frappe.db.sql( |
| 27 | """ |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 28 | 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] | 29 | from tabBin S |
| 30 | inner join `tabItem` I on S.item_code = I.Item_code |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 31 | left join `tabUOM Conversion Detail` C on I.sales_uom = C.uom and C.parent = I.Item_code |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 32 | where S.item_code=%s and S.warehouse=%s""", |
| 33 | (item_code, warehouse), |
| 34 | ) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 35 | |
Britlog | e3a9b9f | 2018-03-20 09:58:44 +0100 | [diff] [blame] | 36 | if stock_qty: |
| 37 | stock_qty = adjust_qty_for_expired_items(item_code, stock_qty, warehouse) |
| 38 | in_stock = stock_qty[0][0] > 0 and 1 or 0 |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 39 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 40 | return frappe._dict( |
| 41 | {"in_stock": in_stock, "stock_qty": stock_qty, "is_stock_item": is_stock_item} |
| 42 | ) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 43 | |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 44 | |
tundebabzy | 29c8142 | 2018-01-31 10:36:31 +0100 | [diff] [blame] | 45 | def adjust_qty_for_expired_items(item_code, stock_qty, warehouse): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 46 | batches = frappe.get_all("Batch", filters=[{"item": item_code}], fields=["expiry_date", "name"]) |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 47 | expired_batches = get_expired_batches(batches) |
| 48 | stock_qty = [list(item) for item in stock_qty] |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 49 | |
tundebabzy | 29c8142 | 2018-01-31 10:36:31 +0100 | [diff] [blame] | 50 | for batch in expired_batches: |
| 51 | if warehouse: |
| 52 | stock_qty[0][0] = max(0, stock_qty[0][0] - get_batch_qty(batch, warehouse)) |
| 53 | else: |
| 54 | 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] | 55 | |
tundebabzy | 29c8142 | 2018-01-31 10:36:31 +0100 | [diff] [blame] | 56 | if not stock_qty[0][0]: |
| 57 | break |
| 58 | |
tundebabzy | c14f1f1 | 2018-01-27 06:28:29 +0100 | [diff] [blame] | 59 | return stock_qty |
| 60 | |
| 61 | |
| 62 | def get_expired_batches(batches): |
| 63 | """ |
| 64 | :param batches: A list of dict in the form [{'expiry_date': datetime.date(20XX, 1, 1), 'name': 'batch_id'}, ...] |
| 65 | """ |
| 66 | return [b.name for b in batches if b.expiry_date and b.expiry_date <= getdate(nowdate())] |
| 67 | |
| 68 | |
| 69 | def qty_from_all_warehouses(batch_info): |
| 70 | """ |
| 71 | :param batch_info: A list of dict in the form [{u'warehouse': u'Stores - I', u'qty': 0.8}, ...] |
| 72 | """ |
| 73 | qty = 0 |
| 74 | for batch in batch_info: |
| 75 | qty = qty + batch.qty |
| 76 | |
| 77 | return qty |
| 78 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 79 | |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 80 | def get_price(item_code, price_list, customer_group, company, qty=1): |
Devin Slauenwhite | 282fbf4 | 2021-12-18 16:03:16 -0500 | [diff] [blame] | 81 | from erpnext.e_commerce.shopping_cart.cart import get_party |
| 82 | |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 83 | template_item_code = frappe.db.get_value("Item", item_code, "variant_of") |
| 84 | |
| 85 | if price_list: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 86 | price = frappe.get_all( |
| 87 | "Item Price", |
| 88 | fields=["price_list_rate", "currency"], |
| 89 | filters={"price_list": price_list, "item_code": item_code}, |
| 90 | ) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 91 | |
| 92 | if template_item_code and not price: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 93 | price = frappe.get_all( |
| 94 | "Item Price", |
| 95 | fields=["price_list_rate", "currency"], |
| 96 | filters={"price_list": price_list, "item_code": template_item_code}, |
| 97 | ) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 98 | |
| 99 | if price: |
Devin Slauenwhite | 282fbf4 | 2021-12-18 16:03:16 -0500 | [diff] [blame] | 100 | party = get_party() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 101 | pricing_rule_dict = frappe._dict( |
| 102 | { |
| 103 | "item_code": item_code, |
| 104 | "qty": qty, |
| 105 | "stock_qty": qty, |
| 106 | "transaction_type": "selling", |
| 107 | "price_list": price_list, |
| 108 | "customer_group": customer_group, |
| 109 | "company": company, |
| 110 | "conversion_rate": 1, |
| 111 | "for_shopping_cart": True, |
| 112 | "currency": frappe.db.get_value("Price List", price_list, "currency"), |
| 113 | } |
| 114 | ) |
Devin Slauenwhite | 282fbf4 | 2021-12-18 16:03:16 -0500 | [diff] [blame] | 115 | |
| 116 | if party and party.doctype == "Customer": |
| 117 | pricing_rule_dict.update({"customer": party.name}) |
| 118 | |
| 119 | pricing_rule = get_pricing_rule_for_item(pricing_rule_dict) |
marination | 6026185 | 2021-04-13 00:39:26 +0530 | [diff] [blame] | 120 | price_obj = price[0] |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 121 | |
| 122 | if pricing_rule: |
marination | 6026185 | 2021-04-13 00:39:26 +0530 | [diff] [blame] | 123 | # price without any rules applied |
| 124 | mrp = price_obj.price_list_rate or 0 |
| 125 | |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 126 | if pricing_rule.pricing_rule_for == "Discount Percentage": |
marination | 1d94914 | 2021-04-20 21:54:52 +0530 | [diff] [blame] | 127 | price_obj.discount_percent = pricing_rule.discount_percentage |
marination | 6026185 | 2021-04-13 00:39:26 +0530 | [diff] [blame] | 128 | price_obj.formatted_discount_percent = str(flt(pricing_rule.discount_percentage, 0)) + "%" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 129 | price_obj.price_list_rate = flt( |
| 130 | price_obj.price_list_rate * (1.0 - (flt(pricing_rule.discount_percentage) / 100.0)) |
| 131 | ) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 132 | |
Charles-Henri Decultot | 614959d | 2018-08-06 11:12:04 +0200 | [diff] [blame] | 133 | if pricing_rule.pricing_rule_for == "Rate": |
marination | 6026185 | 2021-04-13 00:39:26 +0530 | [diff] [blame] | 134 | rate_discount = flt(mrp) - flt(pricing_rule.price_list_rate) |
| 135 | if rate_discount > 0: |
| 136 | price_obj.formatted_discount_rate = fmt_money(rate_discount, currency=price_obj["currency"]) |
| 137 | price_obj.price_list_rate = pricing_rule.price_list_rate or 0 |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 138 | |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 139 | if price_obj: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 140 | price_obj["formatted_price"] = fmt_money( |
| 141 | price_obj["price_list_rate"], currency=price_obj["currency"] |
| 142 | ) |
marination | 6026185 | 2021-04-13 00:39:26 +0530 | [diff] [blame] | 143 | if mrp != price_obj["price_list_rate"]: |
| 144 | price_obj["formatted_mrp"] = fmt_money(mrp, currency=price_obj["currency"]) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 145 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 146 | price_obj["currency_symbol"] = ( |
| 147 | not cint(frappe.db.get_default("hide_currency_symbol")) |
| 148 | and ( |
| 149 | frappe.db.get_value("Currency", price_obj.currency, "symbol", cache=True) |
| 150 | or price_obj.currency |
| 151 | ) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 152 | or "" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 153 | ) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 154 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 155 | uom_conversion_factor = frappe.db.sql( |
| 156 | """select C.conversion_factor |
Britlog | e3a9b9f | 2018-03-20 09:58:44 +0100 | [diff] [blame] | 157 | from `tabUOM Conversion Detail` C |
Britlog | 7d9689c | 2018-11-13 07:06:41 +0100 | [diff] [blame] | 158 | inner join `tabItem` I on C.parent = I.name and C.uom = I.sales_uom |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 159 | where I.name = %s""", |
| 160 | item_code, |
| 161 | ) |
Britlog | e3a9b9f | 2018-03-20 09:58:44 +0100 | [diff] [blame] | 162 | |
| 163 | uom_conversion_factor = uom_conversion_factor[0][0] if uom_conversion_factor else 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 164 | price_obj["formatted_price_sales_uom"] = fmt_money( |
| 165 | price_obj["price_list_rate"] * uom_conversion_factor, currency=price_obj["currency"] |
| 166 | ) |
Britlog | e3a9b9f | 2018-03-20 09:58:44 +0100 | [diff] [blame] | 167 | |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 168 | if not price_obj["price_list_rate"]: |
| 169 | price_obj["price_list_rate"] = 0 |
| 170 | |
| 171 | if not price_obj["currency"]: |
| 172 | price_obj["currency"] = "" |
| 173 | |
| 174 | if not price_obj["formatted_price"]: |
marination | 6026185 | 2021-04-13 00:39:26 +0530 | [diff] [blame] | 175 | price_obj["formatted_price"], price_obj["formatted_mrp"] = "", "" |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 176 | |
| 177 | return price_obj |
Marica | 40dffab | 2020-01-24 15:52:27 +0530 | [diff] [blame] | 178 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 179 | |
Marica | 40dffab | 2020-01-24 15:52:27 +0530 | [diff] [blame] | 180 | def get_non_stock_item_status(item_code, item_warehouse_field): |
marination | 6026185 | 2021-04-13 00:39:26 +0530 | [diff] [blame] | 181 | # if item is a product bundle, check if its bundle items are in stock |
Marica | 40dffab | 2020-01-24 15:52:27 +0530 | [diff] [blame] | 182 | if frappe.db.exists("Product Bundle", item_code): |
| 183 | items = frappe.get_doc("Product Bundle", item_code).get_all_children() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 184 | bundle_warehouse = frappe.db.get_value( |
| 185 | "Website Item", {"item_code": item_code}, item_warehouse_field |
| 186 | ) |
| 187 | return all( |
| 188 | get_web_item_qty_in_stock(d.item_code, item_warehouse_field, bundle_warehouse).in_stock |
| 189 | for d in items |
| 190 | ) |
Marica | 40dffab | 2020-01-24 15:52:27 +0530 | [diff] [blame] | 191 | else: |
| 192 | return 1 |