blob: 029af44e214f73d600775c78046db56706194f6e [file] [log] [blame]
marination60261852021-04-13 00:39:26 +05301# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
Faris Ansarifd345f82017-10-05 11:17:30 +05302# License: GNU General Public License v3. See license.txt
3
Faris Ansarifd345f82017-10-05 11:17:30 +05304import frappe
Sabu Siyadf900a782023-10-17 17:05:44 +05305from frappe.utils import cint, flt, fmt_money
Chillar Anand915b3432021-09-02 16:44:59 +05306
Faris Ansarifd345f82017-10-05 11:17:30 +05307from erpnext.accounts.doctype.pricing_rule.pricing_rule import get_pricing_rule_for_item
8
marination9fb61ef2022-02-01 00:39:14 +05309
Sabu Siyadf900a782023-10-17 17:05:44 +053010def get_price(item_code, price_list, customer_group, company, qty=1, party=None):
Faris Ansarifd345f82017-10-05 11:17:30 +053011 template_item_code = frappe.db.get_value("Item", item_code, "variant_of")
12
13 if price_list:
Ankush Menat494bd9e2022-03-28 18:52:46 +053014 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 Ansarifd345f82017-10-05 11:17:30 +053019
20 if template_item_code and not price:
Ankush Menat494bd9e2022-03-28 18:52:46 +053021 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 Ansarifd345f82017-10-05 11:17:30 +053026
27 if price:
Ankush Menat494bd9e2022-03-28 18:52:46 +053028 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 Garg6192af52022-12-08 18:04:40 +053040 "doctype": "Quotation",
Ankush Menat494bd9e2022-03-28 18:52:46 +053041 }
42 )
Devin Slauenwhite282fbf42021-12-18 16:03:16 -050043
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)
marination60261852021-04-13 00:39:26 +053048 price_obj = price[0]
Faris Ansarifd345f82017-10-05 11:17:30 +053049
50 if pricing_rule:
marination60261852021-04-13 00:39:26 +053051 # price without any rules applied
52 mrp = price_obj.price_list_rate or 0
53
Faris Ansarifd345f82017-10-05 11:17:30 +053054 if pricing_rule.pricing_rule_for == "Discount Percentage":
marination1d949142021-04-20 21:54:52 +053055 price_obj.discount_percent = pricing_rule.discount_percentage
marination60261852021-04-13 00:39:26 +053056 price_obj.formatted_discount_percent = str(flt(pricing_rule.discount_percentage, 0)) + "%"
Ankush Menat494bd9e2022-03-28 18:52:46 +053057 price_obj.price_list_rate = flt(
58 price_obj.price_list_rate * (1.0 - (flt(pricing_rule.discount_percentage) / 100.0))
59 )
Faris Ansarifd345f82017-10-05 11:17:30 +053060
Charles-Henri Decultot614959d2018-08-06 11:12:04 +020061 if pricing_rule.pricing_rule_for == "Rate":
marination60261852021-04-13 00:39:26 +053062 rate_discount = flt(mrp) - flt(pricing_rule.price_list_rate)
63 if rate_discount > 0:
Akhil Narang3effaf22024-03-27 11:37:26 +053064 price_obj.formatted_discount_rate = fmt_money(
65 rate_discount, currency=price_obj["currency"]
66 )
marination60261852021-04-13 00:39:26 +053067 price_obj.price_list_rate = pricing_rule.price_list_rate or 0
Faris Ansarifd345f82017-10-05 11:17:30 +053068
Faris Ansarifd345f82017-10-05 11:17:30 +053069 if price_obj:
Ankush Menat494bd9e2022-03-28 18:52:46 +053070 price_obj["formatted_price"] = fmt_money(
71 price_obj["price_list_rate"], currency=price_obj["currency"]
72 )
marination60261852021-04-13 00:39:26 +053073 if mrp != price_obj["price_list_rate"]:
74 price_obj["formatted_mrp"] = fmt_money(mrp, currency=price_obj["currency"])
Faris Ansarifd345f82017-10-05 11:17:30 +053075
Ankush Menat494bd9e2022-03-28 18:52:46 +053076 price_obj["currency_symbol"] = (
77 not cint(frappe.db.get_default("hide_currency_symbol"))
78 and (
79 frappe.db.get_value("Currency", price_obj.currency, "symbol", cache=True)
80 or price_obj.currency
81 )
Faris Ansarifd345f82017-10-05 11:17:30 +053082 or ""
Ankush Menat494bd9e2022-03-28 18:52:46 +053083 )
Faris Ansarifd345f82017-10-05 11:17:30 +053084
Ankush Menat494bd9e2022-03-28 18:52:46 +053085 uom_conversion_factor = frappe.db.sql(
86 """select C.conversion_factor
Britloge3a9b9f2018-03-20 09:58:44 +010087 from `tabUOM Conversion Detail` C
Britlog7d9689c2018-11-13 07:06:41 +010088 inner join `tabItem` I on C.parent = I.name and C.uom = I.sales_uom
Ankush Menat494bd9e2022-03-28 18:52:46 +053089 where I.name = %s""",
90 item_code,
91 )
Britloge3a9b9f2018-03-20 09:58:44 +010092
93 uom_conversion_factor = uom_conversion_factor[0][0] if uom_conversion_factor else 1
Ankush Menat494bd9e2022-03-28 18:52:46 +053094 price_obj["formatted_price_sales_uom"] = fmt_money(
95 price_obj["price_list_rate"] * uom_conversion_factor, currency=price_obj["currency"]
96 )
Britloge3a9b9f2018-03-20 09:58:44 +010097
Faris Ansarifd345f82017-10-05 11:17:30 +053098 if not price_obj["price_list_rate"]:
99 price_obj["price_list_rate"] = 0
100
101 if not price_obj["currency"]:
102 price_obj["currency"] = ""
103
104 if not price_obj["formatted_price"]:
marination60261852021-04-13 00:39:26 +0530105 price_obj["formatted_price"], price_obj["formatted_mrp"] = "", ""
Faris Ansarifd345f82017-10-05 11:17:30 +0530106
107 return price_obj
Marica40dffab2020-01-24 15:52:27 +0530108
Ankush Menat494bd9e2022-03-28 18:52:46 +0530109
Sabu Siyadf900a782023-10-17 17:05:44 +0530110def get_item_codes_by_attributes(attribute_filters, template_item_code=None):
111 items = []
112
113 for attribute, values in attribute_filters.items():
114 attribute_values = values
115
116 if not isinstance(attribute_values, list):
117 attribute_values = [attribute_values]
118
119 if not attribute_values:
120 continue
121
122 wheres = []
123 query_values = []
124 for attribute_value in attribute_values:
125 wheres.append("( attribute = %s and attribute_value = %s )")
126 query_values += [attribute, attribute_value]
127
128 attribute_query = " or ".join(wheres)
129
130 if template_item_code:
131 variant_of_query = "AND t2.variant_of = %s"
132 query_values.append(template_item_code)
133 else:
134 variant_of_query = ""
135
Akhil Narang3effaf22024-03-27 11:37:26 +0530136 query = f"""
Sabu Siyadf900a782023-10-17 17:05:44 +0530137 SELECT
138 t1.parent
139 FROM
140 `tabItem Variant Attribute` t1
141 WHERE
142 1 = 1
143 AND (
144 {attribute_query}
145 )
146 AND EXISTS (
147 SELECT
148 1
149 FROM
150 `tabItem` t2
151 WHERE
152 t2.name = t1.parent
153 {variant_of_query}
154 )
155 GROUP BY
156 t1.parent
157 ORDER BY
158 NULL
Akhil Narang3effaf22024-03-27 11:37:26 +0530159 """
Sabu Siyadf900a782023-10-17 17:05:44 +0530160
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