blob: 7897c15e94f76160e14db164398534a965f6b720 [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:
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 Ansarifd345f82017-10-05 11:17:30 +053066
Faris Ansarifd345f82017-10-05 11:17:30 +053067 if price_obj:
Ankush Menat494bd9e2022-03-28 18:52:46 +053068 price_obj["formatted_price"] = fmt_money(
69 price_obj["price_list_rate"], currency=price_obj["currency"]
70 )
marination60261852021-04-13 00:39:26 +053071 if mrp != price_obj["price_list_rate"]:
72 price_obj["formatted_mrp"] = fmt_money(mrp, currency=price_obj["currency"])
Faris Ansarifd345f82017-10-05 11:17:30 +053073
Ankush Menat494bd9e2022-03-28 18:52:46 +053074 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 Ansarifd345f82017-10-05 11:17:30 +053080 or ""
Ankush Menat494bd9e2022-03-28 18:52:46 +053081 )
Faris Ansarifd345f82017-10-05 11:17:30 +053082
Ankush Menat494bd9e2022-03-28 18:52:46 +053083 uom_conversion_factor = frappe.db.sql(
84 """select C.conversion_factor
Britloge3a9b9f2018-03-20 09:58:44 +010085 from `tabUOM Conversion Detail` C
Britlog7d9689c2018-11-13 07:06:41 +010086 inner join `tabItem` I on C.parent = I.name and C.uom = I.sales_uom
Ankush Menat494bd9e2022-03-28 18:52:46 +053087 where I.name = %s""",
88 item_code,
89 )
Britloge3a9b9f2018-03-20 09:58:44 +010090
91 uom_conversion_factor = uom_conversion_factor[0][0] if uom_conversion_factor else 1
Ankush Menat494bd9e2022-03-28 18:52:46 +053092 price_obj["formatted_price_sales_uom"] = fmt_money(
93 price_obj["price_list_rate"] * uom_conversion_factor, currency=price_obj["currency"]
94 )
Britloge3a9b9f2018-03-20 09:58:44 +010095
Faris Ansarifd345f82017-10-05 11:17:30 +053096 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"]:
marination60261852021-04-13 00:39:26 +0530103 price_obj["formatted_price"], price_obj["formatted_mrp"] = "", ""
Faris Ansarifd345f82017-10-05 11:17:30 +0530104
105 return price_obj
Marica40dffab2020-01-24 15:52:27 +0530106
Ankush Menat494bd9e2022-03-28 18:52:46 +0530107
Sabu Siyadf900a782023-10-17 17:05:44 +0530108def 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 Menat494bd9e2022-03-28 18:52:46 +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