blob: 1d6c5dc0be0b129cfbc895e04ddef506f073ea42 [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# License: GNU General Public License v3. See license.txt
Saurabh0326f542013-06-13 19:17:56 +05303
Chillar Anand915b3432021-09-02 16:44:59 +05304
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305import frappe
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05306from frappe import _
Chillar Anand915b3432021-09-02 16:44:59 +05307from frappe.utils import getdate
8
Saurabh0326f542013-06-13 19:17:56 +05309
Saurabh1848b712013-06-14 15:03:45 +053010def get_columns(filters, trans):
Nabin Haitb8ebbca2013-06-20 13:03:10 +053011 validate_filters(filters)
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053012
Saurabhf8f68c52013-06-20 18:52:31 +053013 # get conditions for based_on filter cond
Nabin Haitb7438892014-06-05 16:14:28 +053014 based_on_details = based_wise_columns_query(filters.get("based_on"), trans)
Saurabhf8f68c52013-06-20 18:52:31 +053015 # get conditions for periodic filter cond
Nabin Haitb7438892014-06-05 16:14:28 +053016 period_cols, period_select = period_wise_columns_query(filters, trans)
Saurabhf8f68c52013-06-20 18:52:31 +053017 # get conditions for grouping filter cond
18 group_by_cols = group_wise_column(filters.get("group_by"))
Saurabh0326f542013-06-13 19:17:56 +053019
Ankush Menat494bd9e2022-03-28 18:52:46 +053020 columns = (
21 based_on_details["based_on_cols"]
22 + period_cols
23 + [_("Total(Qty)") + ":Float:120", _("Total(Amt)") + ":Currency:120"]
24 )
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053025 if group_by_cols:
Ankush Menat494bd9e2022-03-28 18:52:46 +053026 columns = (
27 based_on_details["based_on_cols"]
28 + group_by_cols
29 + period_cols
30 + [_("Total(Qty)") + ":Float:120", _("Total(Amt)") + ":Currency:120"]
31 )
Saurabh0326f542013-06-13 19:17:56 +053032
Ankush Menat494bd9e2022-03-28 18:52:46 +053033 conditions = {
34 "based_on_select": based_on_details["based_on_select"],
35 "period_wise_select": period_select,
36 "columns": columns,
37 "group_by": based_on_details["based_on_group_by"],
38 "grbc": group_by_cols,
39 "trans": trans,
40 "addl_tables": based_on_details["addl_tables"],
41 "addl_tables_relational_cond": based_on_details.get("addl_tables_relational_cond", ""),
42 }
Saurabh0326f542013-06-13 19:17:56 +053043
Saurabhf8f68c52013-06-20 18:52:31 +053044 return conditions
Saurabh1848b712013-06-14 15:03:45 +053045
Ankush Menat494bd9e2022-03-28 18:52:46 +053046
Nabin Haitb8ebbca2013-06-20 13:03:10 +053047def validate_filters(filters):
48 for f in ["Fiscal Year", "Based On", "Period", "Company"]:
49 if not filters.get(f.lower().replace(" ", "_")):
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053050 frappe.throw(_("{0} is mandatory").format(f))
shreyase970ddc2016-01-28 16:38:59 +053051
Nabin Haitc5b8f7e2015-04-30 16:10:58 +053052 if not frappe.db.exists("Fiscal Year", filters.get("fiscal_year")):
Michelle Alva97872262020-06-25 22:35:33 +053053 frappe.throw(_("Fiscal Year {0} Does Not Exist").format(filters.get("fiscal_year")))
shreyase970ddc2016-01-28 16:38:59 +053054
Nabin Haitb8ebbca2013-06-20 13:03:10 +053055 if filters.get("based_on") == filters.get("group_by"):
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053056 frappe.throw(_("'Based On' and 'Group By' can not be same"))
Nabin Haitb8ebbca2013-06-20 13:03:10 +053057
Ankush Menat494bd9e2022-03-28 18:52:46 +053058
Saurabhf8f68c52013-06-20 18:52:31 +053059def get_data(filters, conditions):
Saurabh0326f542013-06-13 19:17:56 +053060 data = []
Ankush Menat494bd9e2022-03-28 18:52:46 +053061 inc, cond = "", ""
62 query_details = conditions["based_on_select"] + conditions["period_wise_select"]
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053063
Ankush Menat494bd9e2022-03-28 18:52:46 +053064 posting_date = "t1.transaction_date"
65 if conditions.get("trans") in [
66 "Sales Invoice",
67 "Purchase Invoice",
68 "Purchase Receipt",
69 "Delivery Note",
70 ]:
71 posting_date = "t1.posting_date"
Anurag Mishrabbc1b5c2019-09-06 12:10:37 +053072 if filters.period_based_on:
Ankush Menat494bd9e2022-03-28 18:52:46 +053073 posting_date = "t1." + filters.period_based_on
rohitwaghchaure49ccac52016-03-20 19:55:17 +053074
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +053075 if conditions["based_on_select"] in ["t1.project,", "t2.project,"]:
Ankush Menat494bd9e2022-03-28 18:52:46 +053076 cond = " and " + conditions["based_on_select"][:-1] + " IS Not NULL"
77 if conditions.get("trans") in ["Sales Order", "Purchase Order"]:
Saurabh4f62c4c2016-10-03 12:48:25 +053078 cond += " and t1.status != 'Closed'"
Saurabh0326f542013-06-13 19:17:56 +053079
Ankush Menat494bd9e2022-03-28 18:52:46 +053080 if conditions.get("trans") == "Quotation" and filters.get("group_by") == "Customer":
rohitwaghchaure358a01a2019-09-10 19:18:30 +053081 cond += " and t1.quotation_to = 'Customer'"
82
Ankush Menat494bd9e2022-03-28 18:52:46 +053083 year_start_date, year_end_date = frappe.db.get_value(
84 "Fiscal Year", filters.get("fiscal_year"), ["year_start_date", "year_end_date"]
85 )
rohitwaghchaure5b9d5172016-03-17 22:46:09 +053086
Saurabh0326f542013-06-13 19:17:56 +053087 if filters.get("group_by"):
Ankush Menat494bd9e2022-03-28 18:52:46 +053088 sel_col = ""
Saurabhf8f68c52013-06-20 18:52:31 +053089 ind = conditions["columns"].index(conditions["grbc"][0])
Saurabh0326f542013-06-13 19:17:56 +053090
Ankush Menat494bd9e2022-03-28 18:52:46 +053091 if filters.get("group_by") == "Item":
92 sel_col = "t2.item_code"
93 elif filters.get("group_by") == "Customer":
94 sel_col = "t1.party_name" if conditions.get("trans") == "Quotation" else "t1.customer"
95 elif filters.get("group_by") == "Supplier":
96 sel_col = "t1.supplier"
Saurabh0326f542013-06-13 19:17:56 +053097
Ankush Menat494bd9e2022-03-28 18:52:46 +053098 if filters.get("based_on") in ["Item", "Customer", "Supplier"]:
Saurabh0326f542013-06-13 19:17:56 +053099 inc = 2
Ankush Menat494bd9e2022-03-28 18:52:46 +0530100 else:
Saurabh0326f542013-06-13 19:17:56 +0530101 inc = 1
Ankush Menat494bd9e2022-03-28 18:52:46 +0530102 data1 = frappe.db.sql(
103 """ select %s from `tab%s` t1, `tab%s Item` t2 %s
rohitwaghchaure49ccac52016-03-20 19:55:17 +0530104 where t2.parent = t1.name and t1.company = %s and %s between %s and %s and
Nabin Haita6b597a2014-08-11 11:54:21 +0530105 t1.docstatus = 1 %s %s
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530106 group by %s
Ankush Menat494bd9e2022-03-28 18:52:46 +0530107 """
108 % (
109 query_details,
110 conditions["trans"],
111 conditions["trans"],
112 conditions["addl_tables"],
113 "%s",
114 posting_date,
115 "%s",
116 "%s",
117 conditions.get("addl_tables_relational_cond"),
118 cond,
119 conditions["group_by"],
120 ),
121 (filters.get("company"), year_start_date, year_end_date),
122 as_list=1,
123 )
Saurabh0326f542013-06-13 19:17:56 +0530124
125 for d in range(len(data1)):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530126 # to add blanck column
Saurabh0326f542013-06-13 19:17:56 +0530127 dt = data1[d]
Ankush Menat494bd9e2022-03-28 18:52:46 +0530128 dt.insert(ind, "")
Saurabh0326f542013-06-13 19:17:56 +0530129 data.append(dt)
130
Ankush Menat494bd9e2022-03-28 18:52:46 +0530131 # to get distinct value of col specified by group_by in filter
132 row = frappe.db.sql(
133 """select DISTINCT(%s) from `tab%s` t1, `tab%s Item` t2 %s
rohitwaghchaure49ccac52016-03-20 19:55:17 +0530134 where t2.parent = t1.name and t1.company = %s and %s between %s and %s
Nabin Hait5c69fed2016-09-02 12:53:18 +0530135 and t1.docstatus = 1 and %s = %s %s %s
Ankush Menat494bd9e2022-03-28 18:52:46 +0530136 """
137 % (
138 sel_col,
139 conditions["trans"],
140 conditions["trans"],
141 conditions["addl_tables"],
142 "%s",
143 posting_date,
144 "%s",
145 "%s",
146 conditions["group_by"],
147 "%s",
148 conditions.get("addl_tables_relational_cond"),
149 cond,
150 ),
151 (filters.get("company"), year_start_date, year_end_date, data1[d][0]),
152 as_list=1,
153 )
Saurabh1848b712013-06-14 15:03:45 +0530154
Saurabh0326f542013-06-13 19:17:56 +0530155 for i in range(len(row)):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530156 des = ["" for q in range(len(conditions["columns"]))]
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530157
Ankush Menat494bd9e2022-03-28 18:52:46 +0530158 # get data for group_by filter
159 row1 = frappe.db.sql(
160 """ select %s , %s from `tab%s` t1, `tab%s Item` t2 %s
rohitwaghchaure49ccac52016-03-20 19:55:17 +0530161 where t2.parent = t1.name and t1.company = %s and %s between %s and %s
Nabin Hait5c69fed2016-09-02 12:53:18 +0530162 and t1.docstatus = 1 and %s = %s and %s = %s %s %s
Ankush Menat494bd9e2022-03-28 18:52:46 +0530163 """
164 % (
165 sel_col,
166 conditions["period_wise_select"],
167 conditions["trans"],
168 conditions["trans"],
169 conditions["addl_tables"],
170 "%s",
171 posting_date,
172 "%s",
173 "%s",
174 sel_col,
175 "%s",
176 conditions["group_by"],
177 "%s",
178 conditions.get("addl_tables_relational_cond"),
179 cond,
180 ),
181 (filters.get("company"), year_start_date, year_end_date, row[i][0], data1[d][0]),
182 as_list=1,
183 )
Saurabh0326f542013-06-13 19:17:56 +0530184
shreyase970ddc2016-01-28 16:38:59 +0530185 des[ind] = row[i][0]
186
Ankush Menat494bd9e2022-03-28 18:52:46 +0530187 for j in range(1, len(conditions["columns"]) - inc):
188 des[j + inc] = row1[0][j]
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530189
Saurabh0326f542013-06-13 19:17:56 +0530190 data.append(des)
191 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530192 data = frappe.db.sql(
193 """ select %s from `tab%s` t1, `tab%s Item` t2 %s
rohitwaghchaure49ccac52016-03-20 19:55:17 +0530194 where t2.parent = t1.name and t1.company = %s and %s between %s and %s and
Nabin Haita6b597a2014-08-11 11:54:21 +0530195 t1.docstatus = 1 %s %s
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530196 group by %s
Ankush Menat494bd9e2022-03-28 18:52:46 +0530197 """
198 % (
199 query_details,
200 conditions["trans"],
201 conditions["trans"],
202 conditions["addl_tables"],
203 "%s",
204 posting_date,
205 "%s",
206 "%s",
207 cond,
208 conditions.get("addl_tables_relational_cond", ""),
209 conditions["group_by"],
210 ),
211 (filters.get("company"), year_start_date, year_end_date),
212 as_list=1,
213 )
Saurabh0326f542013-06-13 19:17:56 +0530214
215 return data
216
Ankush Menat494bd9e2022-03-28 18:52:46 +0530217
Nabin Haitb8ebbca2013-06-20 13:03:10 +0530218def get_mon(dt):
219 return getdate(dt).strftime("%b")
Saurabhd4f21992013-06-19 14:44:44 +0530220
Ankush Menat494bd9e2022-03-28 18:52:46 +0530221
Nabin Haitb7438892014-06-05 16:14:28 +0530222def period_wise_columns_query(filters, trans):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530223 query_details = ""
Saurabh1848b712013-06-14 15:03:45 +0530224 pwc = []
Saurabhf8f68c52013-06-20 18:52:31 +0530225 bet_dates = get_period_date_ranges(filters.get("period"), filters.get("fiscal_year"))
Saurabh1848b712013-06-14 15:03:45 +0530226
Ankush Menat494bd9e2022-03-28 18:52:46 +0530227 if trans in ["Purchase Receipt", "Delivery Note", "Purchase Invoice", "Sales Invoice"]:
228 trans_date = "posting_date"
Anurag Mishrafe5890b2019-10-07 14:27:07 +0530229 if filters.period_based_on:
230 trans_date = filters.period_based_on
Saurabh0326f542013-06-13 19:17:56 +0530231 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530232 trans_date = "transaction_date"
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530233
Ankush Menat494bd9e2022-03-28 18:52:46 +0530234 if filters.get("period") != "Yearly":
Saurabhf8f68c52013-06-20 18:52:31 +0530235 for dt in bet_dates:
236 get_period_wise_columns(dt, filters.get("period"), pwc)
Rohit Waghchaure87ad6d02017-04-10 16:42:11 +0530237 query_details = get_period_wise_query(dt, trans_date, query_details)
Saurabh0326f542013-06-13 19:17:56 +0530238 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530239 pwc = [
240 _(filters.get("fiscal_year")) + " (" + _("Qty") + "):Float:120",
241 _(filters.get("fiscal_year")) + " (" + _("Amt") + "):Currency:120",
242 ]
Rohit Waghchaure87ad6d02017-04-10 16:42:11 +0530243 query_details = " SUM(t2.stock_qty), SUM(t2.base_net_amount),"
Saurabh0326f542013-06-13 19:17:56 +0530244
Ankush Menat494bd9e2022-03-28 18:52:46 +0530245 query_details += "SUM(t2.stock_qty), SUM(t2.base_net_amount)"
Saurabh0326f542013-06-13 19:17:56 +0530246 return pwc, query_details
247
Ankush Menat494bd9e2022-03-28 18:52:46 +0530248
Saurabhf8f68c52013-06-20 18:52:31 +0530249def get_period_wise_columns(bet_dates, period, pwc):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530250 if period == "Monthly":
251 pwc += [
252 _(get_mon(bet_dates[0])) + " (" + _("Qty") + "):Float:120",
253 _(get_mon(bet_dates[0])) + " (" + _("Amt") + "):Currency:120",
254 ]
Saurabhf8f68c52013-06-20 18:52:31 +0530255 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530256 pwc += [
257 _(get_mon(bet_dates[0])) + "-" + _(get_mon(bet_dates[1])) + " (" + _("Qty") + "):Float:120",
258 _(get_mon(bet_dates[0])) + "-" + _(get_mon(bet_dates[1])) + " (" + _("Amt") + "):Currency:120",
259 ]
260
Saurabhf8f68c52013-06-20 18:52:31 +0530261
Rohit Waghchaure87ad6d02017-04-10 16:42:11 +0530262def get_period_wise_query(bet_dates, trans_date, query_details):
263 query_details += """SUM(IF(t1.%(trans_date)s BETWEEN '%(sd)s' AND '%(ed)s', t2.stock_qty, NULL)),
Nabin Hait82e3e252015-02-23 16:58:30 +0530264 SUM(IF(t1.%(trans_date)s BETWEEN '%(sd)s' AND '%(ed)s', t2.base_net_amount, NULL)),
Ankush Menat494bd9e2022-03-28 18:52:46 +0530265 """ % {
266 "trans_date": trans_date,
267 "sd": bet_dates[0],
268 "ed": bet_dates[1],
269 }
Nabin Haitb8ebbca2013-06-20 13:03:10 +0530270 return query_details
Saurabh0326f542013-06-13 19:17:56 +0530271
Ankush Menat494bd9e2022-03-28 18:52:46 +0530272
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530273@frappe.whitelist(allow_guest=True)
Akhilesh Darjeeec0da0b2013-11-25 19:51:18 +0530274def get_period_date_ranges(period, fiscal_year=None, year_start_date=None):
275 from dateutil.relativedelta import relativedelta
Saurabhf8f68c52013-06-20 18:52:31 +0530276
Akhilesh Darjeeec0da0b2013-11-25 19:51:18 +0530277 if not year_start_date:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530278 year_start_date, year_end_date = frappe.db.get_value(
279 "Fiscal Year", fiscal_year, ["year_start_date", "year_end_date"]
280 )
Saurabhf8f68c52013-06-20 18:52:31 +0530281
Ankush Menat494bd9e2022-03-28 18:52:46 +0530282 increment = {"Monthly": 1, "Quarterly": 3, "Half-Yearly": 6, "Yearly": 12}.get(period)
Saurabhf8f68c52013-06-20 18:52:31 +0530283
Akhilesh Darjeeec0da0b2013-11-25 19:51:18 +0530284 period_date_ranges = []
Achilles Rasquinha96698c92018-02-28 16:12:51 +0530285 for i in range(1, 13, increment):
Akhilesh Darjeeec0da0b2013-11-25 19:51:18 +0530286 period_end_date = getdate(year_start_date) + relativedelta(months=increment, days=-1)
287 if period_end_date > getdate(year_end_date):
288 period_end_date = year_end_date
289 period_date_ranges.append([year_start_date, period_end_date])
290 year_start_date = period_end_date + relativedelta(days=1)
291 if period_end_date == year_end_date:
292 break
293
294 return period_date_ranges
Saurabhf8f68c52013-06-20 18:52:31 +0530295
Ankush Menat494bd9e2022-03-28 18:52:46 +0530296
Saurabhf8f68c52013-06-20 18:52:31 +0530297def get_period_month_ranges(period, fiscal_year):
298 from dateutil.relativedelta import relativedelta
Ankush Menat494bd9e2022-03-28 18:52:46 +0530299
Saurabhf8f68c52013-06-20 18:52:31 +0530300 period_month_ranges = []
301
302 for start_date, end_date in get_period_date_ranges(period, fiscal_year):
303 months_in_this_period = []
304 while start_date <= end_date:
305 months_in_this_period.append(start_date.strftime("%B"))
306 start_date += relativedelta(months=1)
307 period_month_ranges.append(months_in_this_period)
308
309 return period_month_ranges
310
Ankush Menat494bd9e2022-03-28 18:52:46 +0530311
Nabin Haitb7438892014-06-05 16:14:28 +0530312def based_wise_columns_query(based_on, trans):
Saurabh2b02f142013-06-21 10:46:26 +0530313 based_on_details = {}
Saurabh0326f542013-06-13 19:17:56 +0530314
Saurabh2b02f142013-06-21 10:46:26 +0530315 # based_on_cols, based_on_select, based_on_group_by, addl_tables
Saurabh0326f542013-06-13 19:17:56 +0530316 if based_on == "Item":
Saurabh2b02f142013-06-21 10:46:26 +0530317 based_on_details["based_on_cols"] = ["Item:Link/Item:120", "Item Name:Data:120"]
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530318 based_on_details["based_on_select"] = "t2.item_code, t2.item_name,"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530319 based_on_details["based_on_group_by"] = "t2.item_code"
320 based_on_details["addl_tables"] = ""
Saurabh0326f542013-06-13 19:17:56 +0530321
322 elif based_on == "Item Group":
Saurabh2b02f142013-06-21 10:46:26 +0530323 based_on_details["based_on_cols"] = ["Item Group:Link/Item Group:120"]
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530324 based_on_details["based_on_select"] = "t2.item_group,"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530325 based_on_details["based_on_group_by"] = "t2.item_group"
326 based_on_details["addl_tables"] = ""
Saurabh0326f542013-06-13 19:17:56 +0530327
328 elif based_on == "Customer":
Ankush Menat494bd9e2022-03-28 18:52:46 +0530329 based_on_details["based_on_cols"] = [
330 "Customer:Link/Customer:120",
331 "Territory:Link/Territory:120",
332 ]
Saurabh2b02f142013-06-21 10:46:26 +0530333 based_on_details["based_on_select"] = "t1.customer_name, t1.territory, "
Ankush Menat494bd9e2022-03-28 18:52:46 +0530334 based_on_details["based_on_group_by"] = (
335 "t1.party_name" if trans == "Quotation" else "t1.customer"
336 )
337 based_on_details["addl_tables"] = ""
Saurabh0326f542013-06-13 19:17:56 +0530338
339 elif based_on == "Customer Group":
Saurabh2b02f142013-06-21 10:46:26 +0530340 based_on_details["based_on_cols"] = ["Customer Group:Link/Customer Group"]
341 based_on_details["based_on_select"] = "t1.customer_group,"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530342 based_on_details["based_on_group_by"] = "t1.customer_group"
343 based_on_details["addl_tables"] = ""
Saurabh2b02f142013-06-21 10:46:26 +0530344
Ankush Menat494bd9e2022-03-28 18:52:46 +0530345 elif based_on == "Supplier":
346 based_on_details["based_on_cols"] = [
347 "Supplier:Link/Supplier:120",
348 "Supplier Group:Link/Supplier Group:140",
349 ]
Zlash652e080982018-04-19 18:37:53 +0530350 based_on_details["based_on_select"] = "t1.supplier, t3.supplier_group,"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530351 based_on_details["based_on_group_by"] = "t1.supplier"
352 based_on_details["addl_tables"] = ",`tabSupplier` t3"
Nabin Haita6b597a2014-08-11 11:54:21 +0530353 based_on_details["addl_tables_relational_cond"] = " and t1.supplier = t3.name"
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530354
Ankush Menat494bd9e2022-03-28 18:52:46 +0530355 elif based_on == "Supplier Group":
Zlash652e080982018-04-19 18:37:53 +0530356 based_on_details["based_on_cols"] = ["Supplier Group:Link/Supplier Group:140"]
357 based_on_details["based_on_select"] = "t3.supplier_group,"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530358 based_on_details["based_on_group_by"] = "t3.supplier_group"
359 based_on_details["addl_tables"] = ",`tabSupplier` t3"
Nabin Haita6b597a2014-08-11 11:54:21 +0530360 based_on_details["addl_tables_relational_cond"] = " and t1.supplier = t3.name"
Saurabh0326f542013-06-13 19:17:56 +0530361
362 elif based_on == "Territory":
Saurabh2b02f142013-06-21 10:46:26 +0530363 based_on_details["based_on_cols"] = ["Territory:Link/Territory:120"]
364 based_on_details["based_on_select"] = "t1.territory,"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530365 based_on_details["based_on_group_by"] = "t1.territory"
366 based_on_details["addl_tables"] = ""
Saurabh0326f542013-06-13 19:17:56 +0530367
368 elif based_on == "Project":
Ankush Menat494bd9e2022-03-28 18:52:46 +0530369 if trans in ["Sales Invoice", "Delivery Note", "Sales Order"]:
Saurabh2b02f142013-06-21 10:46:26 +0530370 based_on_details["based_on_cols"] = ["Project:Link/Project:120"]
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530371 based_on_details["based_on_select"] = "t1.project,"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530372 based_on_details["based_on_group_by"] = "t1.project"
373 based_on_details["addl_tables"] = ""
374 elif trans in ["Purchase Order", "Purchase Invoice", "Purchase Receipt"]:
Saurabh2b02f142013-06-21 10:46:26 +0530375 based_on_details["based_on_cols"] = ["Project:Link/Project:120"]
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530376 based_on_details["based_on_select"] = "t2.project,"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530377 based_on_details["based_on_group_by"] = "t2.project"
378 based_on_details["addl_tables"] = ""
Saurabh0326f542013-06-13 19:17:56 +0530379 else:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530380 frappe.throw(_("Project-wise data is not available for Quotation"))
Saurabh0326f542013-06-13 19:17:56 +0530381
Saurabh2b02f142013-06-21 10:46:26 +0530382 return based_on_details
Saurabh0326f542013-06-13 19:17:56 +0530383
Ankush Menat494bd9e2022-03-28 18:52:46 +0530384
Saurabh1848b712013-06-14 15:03:45 +0530385def group_wise_column(group_by):
Saurabh0326f542013-06-13 19:17:56 +0530386 if group_by:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530387 return [group_by + ":Link/" + group_by + ":120"]
Saurabh0326f542013-06-13 19:17:56 +0530388 else:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530389 return []