blob: 18fe7767c5d60b56effa1e309bbed7aa861153eb [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
Daizy Modi4efc9472022-11-07 09:21:03 +053083 year_start_date, year_end_date = frappe.get_cached_value(
Ankush Menat494bd9e2022-03-28 18:52:46 +053084 "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(
Akhil Narang3effaf22024-03-27 11:37:26 +0530103 """ select {} from `tab{}` t1, `tab{} Item` t2 {}
104 where t2.parent = t1.name and t1.company = {} and {} between {} and {} and
105 t1.docstatus = 1 {} {}
106 group by {}
107 """.format(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530108 query_details,
109 conditions["trans"],
110 conditions["trans"],
111 conditions["addl_tables"],
112 "%s",
113 posting_date,
114 "%s",
115 "%s",
116 conditions.get("addl_tables_relational_cond"),
117 cond,
118 conditions["group_by"],
119 ),
120 (filters.get("company"), year_start_date, year_end_date),
121 as_list=1,
122 )
Saurabh0326f542013-06-13 19:17:56 +0530123
124 for d in range(len(data1)):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530125 # to add blanck column
Saurabh0326f542013-06-13 19:17:56 +0530126 dt = data1[d]
Ankush Menat494bd9e2022-03-28 18:52:46 +0530127 dt.insert(ind, "")
Saurabh0326f542013-06-13 19:17:56 +0530128 data.append(dt)
129
Ankush Menat494bd9e2022-03-28 18:52:46 +0530130 # to get distinct value of col specified by group_by in filter
131 row = frappe.db.sql(
Akhil Narang3effaf22024-03-27 11:37:26 +0530132 """select DISTINCT({}) from `tab{}` t1, `tab{} Item` t2 {}
133 where t2.parent = t1.name and t1.company = {} and {} between {} and {}
134 and t1.docstatus = 1 and {} = {} {} {}
135 """.format(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530136 sel_col,
137 conditions["trans"],
138 conditions["trans"],
139 conditions["addl_tables"],
140 "%s",
141 posting_date,
142 "%s",
143 "%s",
144 conditions["group_by"],
145 "%s",
146 conditions.get("addl_tables_relational_cond"),
147 cond,
148 ),
149 (filters.get("company"), year_start_date, year_end_date, data1[d][0]),
150 as_list=1,
151 )
Saurabh1848b712013-06-14 15:03:45 +0530152
Saurabh0326f542013-06-13 19:17:56 +0530153 for i in range(len(row)):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530154 des = ["" for q in range(len(conditions["columns"]))]
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530155
Ankush Menat494bd9e2022-03-28 18:52:46 +0530156 # get data for group_by filter
157 row1 = frappe.db.sql(
Akhil Narang3effaf22024-03-27 11:37:26 +0530158 """ select {} , {} from `tab{}` t1, `tab{} Item` t2 {}
159 where t2.parent = t1.name and t1.company = {} and {} between {} and {}
160 and t1.docstatus = 1 and {} = {} and {} = {} {} {}
161 """.format(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530162 sel_col,
163 conditions["period_wise_select"],
164 conditions["trans"],
165 conditions["trans"],
166 conditions["addl_tables"],
167 "%s",
168 posting_date,
169 "%s",
170 "%s",
171 sel_col,
172 "%s",
173 conditions["group_by"],
174 "%s",
175 conditions.get("addl_tables_relational_cond"),
176 cond,
177 ),
178 (filters.get("company"), year_start_date, year_end_date, row[i][0], data1[d][0]),
179 as_list=1,
180 )
Saurabh0326f542013-06-13 19:17:56 +0530181
shreyase970ddc2016-01-28 16:38:59 +0530182 des[ind] = row[i][0]
183
Ankush Menat494bd9e2022-03-28 18:52:46 +0530184 for j in range(1, len(conditions["columns"]) - inc):
185 des[j + inc] = row1[0][j]
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530186
Saurabh0326f542013-06-13 19:17:56 +0530187 data.append(des)
188 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530189 data = frappe.db.sql(
Akhil Narang3effaf22024-03-27 11:37:26 +0530190 """ select {} from `tab{}` t1, `tab{} Item` t2 {}
191 where t2.parent = t1.name and t1.company = {} and {} between {} and {} and
192 t1.docstatus = 1 {} {}
193 group by {}
194 """.format(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530195 query_details,
196 conditions["trans"],
197 conditions["trans"],
198 conditions["addl_tables"],
199 "%s",
200 posting_date,
201 "%s",
202 "%s",
203 cond,
204 conditions.get("addl_tables_relational_cond", ""),
205 conditions["group_by"],
206 ),
207 (filters.get("company"), year_start_date, year_end_date),
208 as_list=1,
209 )
Saurabh0326f542013-06-13 19:17:56 +0530210
211 return data
212
Ankush Menat494bd9e2022-03-28 18:52:46 +0530213
Nabin Haitb8ebbca2013-06-20 13:03:10 +0530214def get_mon(dt):
215 return getdate(dt).strftime("%b")
Saurabhd4f21992013-06-19 14:44:44 +0530216
Ankush Menat494bd9e2022-03-28 18:52:46 +0530217
Nabin Haitb7438892014-06-05 16:14:28 +0530218def period_wise_columns_query(filters, trans):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530219 query_details = ""
Saurabh1848b712013-06-14 15:03:45 +0530220 pwc = []
Saurabhf8f68c52013-06-20 18:52:31 +0530221 bet_dates = get_period_date_ranges(filters.get("period"), filters.get("fiscal_year"))
Saurabh1848b712013-06-14 15:03:45 +0530222
Ankush Menat494bd9e2022-03-28 18:52:46 +0530223 if trans in ["Purchase Receipt", "Delivery Note", "Purchase Invoice", "Sales Invoice"]:
224 trans_date = "posting_date"
Anurag Mishrafe5890b2019-10-07 14:27:07 +0530225 if filters.period_based_on:
226 trans_date = filters.period_based_on
Saurabh0326f542013-06-13 19:17:56 +0530227 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530228 trans_date = "transaction_date"
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530229
Ankush Menat494bd9e2022-03-28 18:52:46 +0530230 if filters.get("period") != "Yearly":
Saurabhf8f68c52013-06-20 18:52:31 +0530231 for dt in bet_dates:
232 get_period_wise_columns(dt, filters.get("period"), pwc)
Rohit Waghchaure87ad6d02017-04-10 16:42:11 +0530233 query_details = get_period_wise_query(dt, trans_date, query_details)
Saurabh0326f542013-06-13 19:17:56 +0530234 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530235 pwc = [
236 _(filters.get("fiscal_year")) + " (" + _("Qty") + "):Float:120",
237 _(filters.get("fiscal_year")) + " (" + _("Amt") + "):Currency:120",
238 ]
Rohit Waghchaure87ad6d02017-04-10 16:42:11 +0530239 query_details = " SUM(t2.stock_qty), SUM(t2.base_net_amount),"
Saurabh0326f542013-06-13 19:17:56 +0530240
Ankush Menat494bd9e2022-03-28 18:52:46 +0530241 query_details += "SUM(t2.stock_qty), SUM(t2.base_net_amount)"
Saurabh0326f542013-06-13 19:17:56 +0530242 return pwc, query_details
243
Ankush Menat494bd9e2022-03-28 18:52:46 +0530244
Saurabhf8f68c52013-06-20 18:52:31 +0530245def get_period_wise_columns(bet_dates, period, pwc):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530246 if period == "Monthly":
247 pwc += [
248 _(get_mon(bet_dates[0])) + " (" + _("Qty") + "):Float:120",
249 _(get_mon(bet_dates[0])) + " (" + _("Amt") + "):Currency:120",
250 ]
Saurabhf8f68c52013-06-20 18:52:31 +0530251 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530252 pwc += [
253 _(get_mon(bet_dates[0])) + "-" + _(get_mon(bet_dates[1])) + " (" + _("Qty") + "):Float:120",
254 _(get_mon(bet_dates[0])) + "-" + _(get_mon(bet_dates[1])) + " (" + _("Amt") + "):Currency:120",
255 ]
256
Saurabhf8f68c52013-06-20 18:52:31 +0530257
Rohit Waghchaure87ad6d02017-04-10 16:42:11 +0530258def get_period_wise_query(bet_dates, trans_date, query_details):
Akhil Narang3effaf22024-03-27 11:37:26 +0530259 query_details += """SUM(IF(t1.{trans_date} BETWEEN '{sd}' AND '{ed}', t2.stock_qty, NULL)),
260 SUM(IF(t1.{trans_date} BETWEEN '{sd}' AND '{ed}', t2.base_net_amount, NULL)),
261 """.format(
262 trans_date=trans_date,
263 sd=bet_dates[0],
264 ed=bet_dates[1],
265 )
Nabin Haitb8ebbca2013-06-20 13:03:10 +0530266 return query_details
Saurabh0326f542013-06-13 19:17:56 +0530267
Ankush Menat494bd9e2022-03-28 18:52:46 +0530268
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530269@frappe.whitelist(allow_guest=True)
Akhilesh Darjeeec0da0b2013-11-25 19:51:18 +0530270def get_period_date_ranges(period, fiscal_year=None, year_start_date=None):
271 from dateutil.relativedelta import relativedelta
Saurabhf8f68c52013-06-20 18:52:31 +0530272
Akhilesh Darjeeec0da0b2013-11-25 19:51:18 +0530273 if not year_start_date:
Daizy Modi4efc9472022-11-07 09:21:03 +0530274 year_start_date, year_end_date = frappe.get_cached_value(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530275 "Fiscal Year", fiscal_year, ["year_start_date", "year_end_date"]
276 )
Saurabhf8f68c52013-06-20 18:52:31 +0530277
Ankush Menat494bd9e2022-03-28 18:52:46 +0530278 increment = {"Monthly": 1, "Quarterly": 3, "Half-Yearly": 6, "Yearly": 12}.get(period)
Saurabhf8f68c52013-06-20 18:52:31 +0530279
Akhilesh Darjeeec0da0b2013-11-25 19:51:18 +0530280 period_date_ranges = []
Akhil Narang3effaf22024-03-27 11:37:26 +0530281 for _i in range(1, 13, increment):
Akhilesh Darjeeec0da0b2013-11-25 19:51:18 +0530282 period_end_date = getdate(year_start_date) + relativedelta(months=increment, days=-1)
283 if period_end_date > getdate(year_end_date):
284 period_end_date = year_end_date
285 period_date_ranges.append([year_start_date, period_end_date])
286 year_start_date = period_end_date + relativedelta(days=1)
287 if period_end_date == year_end_date:
288 break
289
290 return period_date_ranges
Saurabhf8f68c52013-06-20 18:52:31 +0530291
Ankush Menat494bd9e2022-03-28 18:52:46 +0530292
Saurabhf8f68c52013-06-20 18:52:31 +0530293def get_period_month_ranges(period, fiscal_year):
294 from dateutil.relativedelta import relativedelta
Ankush Menat494bd9e2022-03-28 18:52:46 +0530295
Saurabhf8f68c52013-06-20 18:52:31 +0530296 period_month_ranges = []
297
298 for start_date, end_date in get_period_date_ranges(period, fiscal_year):
299 months_in_this_period = []
300 while start_date <= end_date:
301 months_in_this_period.append(start_date.strftime("%B"))
302 start_date += relativedelta(months=1)
303 period_month_ranges.append(months_in_this_period)
304
305 return period_month_ranges
306
Ankush Menat494bd9e2022-03-28 18:52:46 +0530307
Nabin Haitb7438892014-06-05 16:14:28 +0530308def based_wise_columns_query(based_on, trans):
Saurabh2b02f142013-06-21 10:46:26 +0530309 based_on_details = {}
Saurabh0326f542013-06-13 19:17:56 +0530310
Saurabh2b02f142013-06-21 10:46:26 +0530311 # based_on_cols, based_on_select, based_on_group_by, addl_tables
Saurabh0326f542013-06-13 19:17:56 +0530312 if based_on == "Item":
Saurabh2b02f142013-06-21 10:46:26 +0530313 based_on_details["based_on_cols"] = ["Item:Link/Item:120", "Item Name:Data:120"]
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530314 based_on_details["based_on_select"] = "t2.item_code, t2.item_name,"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530315 based_on_details["based_on_group_by"] = "t2.item_code"
316 based_on_details["addl_tables"] = ""
Saurabh0326f542013-06-13 19:17:56 +0530317
318 elif based_on == "Item Group":
Saurabh2b02f142013-06-21 10:46:26 +0530319 based_on_details["based_on_cols"] = ["Item Group:Link/Item Group:120"]
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530320 based_on_details["based_on_select"] = "t2.item_group,"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530321 based_on_details["based_on_group_by"] = "t2.item_group"
322 based_on_details["addl_tables"] = ""
Saurabh0326f542013-06-13 19:17:56 +0530323
324 elif based_on == "Customer":
Ankush Menat494bd9e2022-03-28 18:52:46 +0530325 based_on_details["based_on_cols"] = [
326 "Customer:Link/Customer:120",
327 "Territory:Link/Territory:120",
328 ]
Saurabh2b02f142013-06-21 10:46:26 +0530329 based_on_details["based_on_select"] = "t1.customer_name, t1.territory, "
Akhil Narang3effaf22024-03-27 11:37:26 +0530330 based_on_details["based_on_group_by"] = "t1.party_name" if trans == "Quotation" else "t1.customer"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530331 based_on_details["addl_tables"] = ""
Saurabh0326f542013-06-13 19:17:56 +0530332
333 elif based_on == "Customer Group":
Saurabh2b02f142013-06-21 10:46:26 +0530334 based_on_details["based_on_cols"] = ["Customer Group:Link/Customer Group"]
335 based_on_details["based_on_select"] = "t1.customer_group,"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530336 based_on_details["based_on_group_by"] = "t1.customer_group"
337 based_on_details["addl_tables"] = ""
Saurabh2b02f142013-06-21 10:46:26 +0530338
Ankush Menat494bd9e2022-03-28 18:52:46 +0530339 elif based_on == "Supplier":
340 based_on_details["based_on_cols"] = [
341 "Supplier:Link/Supplier:120",
342 "Supplier Group:Link/Supplier Group:140",
343 ]
Zlash652e080982018-04-19 18:37:53 +0530344 based_on_details["based_on_select"] = "t1.supplier, t3.supplier_group,"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530345 based_on_details["based_on_group_by"] = "t1.supplier"
346 based_on_details["addl_tables"] = ",`tabSupplier` t3"
Nabin Haita6b597a2014-08-11 11:54:21 +0530347 based_on_details["addl_tables_relational_cond"] = " and t1.supplier = t3.name"
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530348
Ankush Menat494bd9e2022-03-28 18:52:46 +0530349 elif based_on == "Supplier Group":
Zlash652e080982018-04-19 18:37:53 +0530350 based_on_details["based_on_cols"] = ["Supplier Group:Link/Supplier Group:140"]
351 based_on_details["based_on_select"] = "t3.supplier_group,"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530352 based_on_details["based_on_group_by"] = "t3.supplier_group"
353 based_on_details["addl_tables"] = ",`tabSupplier` t3"
Nabin Haita6b597a2014-08-11 11:54:21 +0530354 based_on_details["addl_tables_relational_cond"] = " and t1.supplier = t3.name"
Saurabh0326f542013-06-13 19:17:56 +0530355
356 elif based_on == "Territory":
Saurabh2b02f142013-06-21 10:46:26 +0530357 based_on_details["based_on_cols"] = ["Territory:Link/Territory:120"]
358 based_on_details["based_on_select"] = "t1.territory,"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530359 based_on_details["based_on_group_by"] = "t1.territory"
360 based_on_details["addl_tables"] = ""
Saurabh0326f542013-06-13 19:17:56 +0530361
362 elif based_on == "Project":
Ankush Menat494bd9e2022-03-28 18:52:46 +0530363 if trans in ["Sales Invoice", "Delivery Note", "Sales Order"]:
Saurabh2b02f142013-06-21 10:46:26 +0530364 based_on_details["based_on_cols"] = ["Project:Link/Project:120"]
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530365 based_on_details["based_on_select"] = "t1.project,"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530366 based_on_details["based_on_group_by"] = "t1.project"
367 based_on_details["addl_tables"] = ""
368 elif trans in ["Purchase Order", "Purchase Invoice", "Purchase Receipt"]:
Saurabh2b02f142013-06-21 10:46:26 +0530369 based_on_details["based_on_cols"] = ["Project:Link/Project:120"]
Neil Trini Lasrado6e343e22016-03-09 17:02:59 +0530370 based_on_details["based_on_select"] = "t2.project,"
Ankush Menat494bd9e2022-03-28 18:52:46 +0530371 based_on_details["based_on_group_by"] = "t2.project"
372 based_on_details["addl_tables"] = ""
Saurabh0326f542013-06-13 19:17:56 +0530373 else:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530374 frappe.throw(_("Project-wise data is not available for Quotation"))
Saurabh0326f542013-06-13 19:17:56 +0530375
Saurabh2b02f142013-06-21 10:46:26 +0530376 return based_on_details
Saurabh0326f542013-06-13 19:17:56 +0530377
Ankush Menat494bd9e2022-03-28 18:52:46 +0530378
Saurabh1848b712013-06-14 15:03:45 +0530379def group_wise_column(group_by):
Saurabh0326f542013-06-13 19:17:56 +0530380 if group_by:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530381 return [group_by + ":Link/" + group_by + ":120"]
Saurabh0326f542013-06-13 19:17:56 +0530382 else:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530383 return []