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 |
Sabu Siyad | f900a78 | 2023-10-17 17:05:44 +0530 | [diff] [blame] | 5 | from frappe.utils import cint, flt, fmt_money |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 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 |
| 8 | |
marination | 9fb61ef | 2022-02-01 00:39:14 +0530 | [diff] [blame] | 9 | |
Sabu Siyad | f900a78 | 2023-10-17 17:05:44 +0530 | [diff] [blame] | 10 | def get_price(item_code, price_list, customer_group, company, qty=1, party=None): |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 11 | template_item_code = frappe.db.get_value("Item", item_code, "variant_of") |
| 12 | |
| 13 | if price_list: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 14 | price = frappe.get_all( |
| 15 | "Item Price", |
| 16 | fields=["price_list_rate", "currency"], |
| 17 | filters={"price_list": price_list, "item_code": item_code}, |
| 18 | ) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 19 | |
| 20 | if template_item_code and not price: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 21 | price = frappe.get_all( |
| 22 | "Item Price", |
| 23 | fields=["price_list_rate", "currency"], |
| 24 | filters={"price_list": price_list, "item_code": template_item_code}, |
| 25 | ) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 26 | |
| 27 | if price: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 28 | pricing_rule_dict = frappe._dict( |
| 29 | { |
| 30 | "item_code": item_code, |
| 31 | "qty": qty, |
| 32 | "stock_qty": qty, |
| 33 | "transaction_type": "selling", |
| 34 | "price_list": price_list, |
| 35 | "customer_group": customer_group, |
| 36 | "company": company, |
| 37 | "conversion_rate": 1, |
| 38 | "for_shopping_cart": True, |
| 39 | "currency": frappe.db.get_value("Price List", price_list, "currency"), |
Deepesh Garg | 6192af5 | 2022-12-08 18:04:40 +0530 | [diff] [blame] | 40 | "doctype": "Quotation", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 41 | } |
| 42 | ) |
Devin Slauenwhite | 282fbf4 | 2021-12-18 16:03:16 -0500 | [diff] [blame] | 43 | |
| 44 | if party and party.doctype == "Customer": |
| 45 | pricing_rule_dict.update({"customer": party.name}) |
| 46 | |
| 47 | pricing_rule = get_pricing_rule_for_item(pricing_rule_dict) |
marination | 6026185 | 2021-04-13 00:39:26 +0530 | [diff] [blame] | 48 | price_obj = price[0] |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 49 | |
| 50 | if pricing_rule: |
marination | 6026185 | 2021-04-13 00:39:26 +0530 | [diff] [blame] | 51 | # price without any rules applied |
| 52 | mrp = price_obj.price_list_rate or 0 |
| 53 | |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 54 | if pricing_rule.pricing_rule_for == "Discount Percentage": |
marination | 1d94914 | 2021-04-20 21:54:52 +0530 | [diff] [blame] | 55 | price_obj.discount_percent = pricing_rule.discount_percentage |
marination | 6026185 | 2021-04-13 00:39:26 +0530 | [diff] [blame] | 56 | price_obj.formatted_discount_percent = str(flt(pricing_rule.discount_percentage, 0)) + "%" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 57 | price_obj.price_list_rate = flt( |
| 58 | price_obj.price_list_rate * (1.0 - (flt(pricing_rule.discount_percentage) / 100.0)) |
| 59 | ) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 60 | |
Charles-Henri Decultot | 614959d | 2018-08-06 11:12:04 +0200 | [diff] [blame] | 61 | if pricing_rule.pricing_rule_for == "Rate": |
marination | 6026185 | 2021-04-13 00:39:26 +0530 | [diff] [blame] | 62 | rate_discount = flt(mrp) - flt(pricing_rule.price_list_rate) |
| 63 | if rate_discount > 0: |
| 64 | price_obj.formatted_discount_rate = fmt_money(rate_discount, currency=price_obj["currency"]) |
| 65 | price_obj.price_list_rate = pricing_rule.price_list_rate or 0 |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 66 | |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 67 | if price_obj: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 68 | price_obj["formatted_price"] = fmt_money( |
| 69 | price_obj["price_list_rate"], currency=price_obj["currency"] |
| 70 | ) |
marination | 6026185 | 2021-04-13 00:39:26 +0530 | [diff] [blame] | 71 | if mrp != price_obj["price_list_rate"]: |
| 72 | price_obj["formatted_mrp"] = fmt_money(mrp, currency=price_obj["currency"]) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 73 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 74 | price_obj["currency_symbol"] = ( |
| 75 | not cint(frappe.db.get_default("hide_currency_symbol")) |
| 76 | and ( |
| 77 | frappe.db.get_value("Currency", price_obj.currency, "symbol", cache=True) |
| 78 | or price_obj.currency |
| 79 | ) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 80 | or "" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 81 | ) |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 82 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 83 | uom_conversion_factor = frappe.db.sql( |
| 84 | """select C.conversion_factor |
Britlog | e3a9b9f | 2018-03-20 09:58:44 +0100 | [diff] [blame] | 85 | from `tabUOM Conversion Detail` C |
Britlog | 7d9689c | 2018-11-13 07:06:41 +0100 | [diff] [blame] | 86 | 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] | 87 | where I.name = %s""", |
| 88 | item_code, |
| 89 | ) |
Britlog | e3a9b9f | 2018-03-20 09:58:44 +0100 | [diff] [blame] | 90 | |
| 91 | 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] | 92 | price_obj["formatted_price_sales_uom"] = fmt_money( |
| 93 | price_obj["price_list_rate"] * uom_conversion_factor, currency=price_obj["currency"] |
| 94 | ) |
Britlog | e3a9b9f | 2018-03-20 09:58:44 +0100 | [diff] [blame] | 95 | |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 96 | if not price_obj["price_list_rate"]: |
| 97 | price_obj["price_list_rate"] = 0 |
| 98 | |
| 99 | if not price_obj["currency"]: |
| 100 | price_obj["currency"] = "" |
| 101 | |
| 102 | if not price_obj["formatted_price"]: |
marination | 6026185 | 2021-04-13 00:39:26 +0530 | [diff] [blame] | 103 | price_obj["formatted_price"], price_obj["formatted_mrp"] = "", "" |
Faris Ansari | fd345f8 | 2017-10-05 11:17:30 +0530 | [diff] [blame] | 104 | |
| 105 | return price_obj |
Marica | 40dffab | 2020-01-24 15:52:27 +0530 | [diff] [blame] | 106 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 107 | |
Sabu Siyad | f900a78 | 2023-10-17 17:05:44 +0530 | [diff] [blame] | 108 | def get_item_codes_by_attributes(attribute_filters, template_item_code=None): |
| 109 | items = [] |
| 110 | |
| 111 | for attribute, values in attribute_filters.items(): |
| 112 | attribute_values = values |
| 113 | |
| 114 | if not isinstance(attribute_values, list): |
| 115 | attribute_values = [attribute_values] |
| 116 | |
| 117 | if not attribute_values: |
| 118 | continue |
| 119 | |
| 120 | wheres = [] |
| 121 | query_values = [] |
| 122 | for attribute_value in attribute_values: |
| 123 | wheres.append("( attribute = %s and attribute_value = %s )") |
| 124 | query_values += [attribute, attribute_value] |
| 125 | |
| 126 | attribute_query = " or ".join(wheres) |
| 127 | |
| 128 | if template_item_code: |
| 129 | variant_of_query = "AND t2.variant_of = %s" |
| 130 | query_values.append(template_item_code) |
| 131 | else: |
| 132 | variant_of_query = "" |
| 133 | |
| 134 | query = """ |
| 135 | SELECT |
| 136 | t1.parent |
| 137 | FROM |
| 138 | `tabItem Variant Attribute` t1 |
| 139 | WHERE |
| 140 | 1 = 1 |
| 141 | AND ( |
| 142 | {attribute_query} |
| 143 | ) |
| 144 | AND EXISTS ( |
| 145 | SELECT |
| 146 | 1 |
| 147 | FROM |
| 148 | `tabItem` t2 |
| 149 | WHERE |
| 150 | t2.name = t1.parent |
| 151 | {variant_of_query} |
| 152 | ) |
| 153 | GROUP BY |
| 154 | t1.parent |
| 155 | ORDER BY |
| 156 | NULL |
| 157 | """.format( |
| 158 | attribute_query=attribute_query, variant_of_query=variant_of_query |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 159 | ) |
Sabu Siyad | f900a78 | 2023-10-17 17:05:44 +0530 | [diff] [blame] | 160 | |
| 161 | item_codes = set([r[0] for r in frappe.db.sql(query, query_values)]) |
| 162 | items.append(item_codes) |
| 163 | |
| 164 | res = list(set.intersection(*items)) |
| 165 | |
| 166 | return res |