Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 3 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 4 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 6 | from frappe import _ |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 7 | from frappe.utils import getdate |
| 8 | |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 9 | |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 10 | def get_columns(filters, trans): |
Nabin Hait | b8ebbca | 2013-06-20 13:03:10 +0530 | [diff] [blame] | 11 | validate_filters(filters) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 12 | |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 13 | # get conditions for based_on filter cond |
Nabin Hait | b743889 | 2014-06-05 16:14:28 +0530 | [diff] [blame] | 14 | based_on_details = based_wise_columns_query(filters.get("based_on"), trans) |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 15 | # get conditions for periodic filter cond |
Nabin Hait | b743889 | 2014-06-05 16:14:28 +0530 | [diff] [blame] | 16 | period_cols, period_select = period_wise_columns_query(filters, trans) |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 17 | # get conditions for grouping filter cond |
| 18 | group_by_cols = group_wise_column(filters.get("group_by")) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 19 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 20 | columns = ( |
| 21 | based_on_details["based_on_cols"] |
| 22 | + period_cols |
| 23 | + [_("Total(Qty)") + ":Float:120", _("Total(Amt)") + ":Currency:120"] |
| 24 | ) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 25 | if group_by_cols: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 26 | 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 | ) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 32 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 33 | 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 | } |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 43 | |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 44 | return conditions |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 45 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 46 | |
Nabin Hait | b8ebbca | 2013-06-20 13:03:10 +0530 | [diff] [blame] | 47 | def validate_filters(filters): |
| 48 | for f in ["Fiscal Year", "Based On", "Period", "Company"]: |
| 49 | if not filters.get(f.lower().replace(" ", "_")): |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 50 | frappe.throw(_("{0} is mandatory").format(f)) |
shreyas | e970ddc | 2016-01-28 16:38:59 +0530 | [diff] [blame] | 51 | |
Nabin Hait | c5b8f7e | 2015-04-30 16:10:58 +0530 | [diff] [blame] | 52 | if not frappe.db.exists("Fiscal Year", filters.get("fiscal_year")): |
Michelle Alva | 9787226 | 2020-06-25 22:35:33 +0530 | [diff] [blame] | 53 | frappe.throw(_("Fiscal Year {0} Does Not Exist").format(filters.get("fiscal_year"))) |
shreyas | e970ddc | 2016-01-28 16:38:59 +0530 | [diff] [blame] | 54 | |
Nabin Hait | b8ebbca | 2013-06-20 13:03:10 +0530 | [diff] [blame] | 55 | if filters.get("based_on") == filters.get("group_by"): |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 56 | frappe.throw(_("'Based On' and 'Group By' can not be same")) |
Nabin Hait | b8ebbca | 2013-06-20 13:03:10 +0530 | [diff] [blame] | 57 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 58 | |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 59 | def get_data(filters, conditions): |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 60 | data = [] |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 61 | inc, cond = "", "" |
| 62 | query_details = conditions["based_on_select"] + conditions["period_wise_select"] |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 63 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 64 | 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 Mishra | bbc1b5c | 2019-09-06 12:10:37 +0530 | [diff] [blame] | 72 | if filters.period_based_on: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 73 | posting_date = "t1." + filters.period_based_on |
rohitwaghchaure | 49ccac5 | 2016-03-20 19:55:17 +0530 | [diff] [blame] | 74 | |
Neil Trini Lasrado | 6e343e2 | 2016-03-09 17:02:59 +0530 | [diff] [blame] | 75 | if conditions["based_on_select"] in ["t1.project,", "t2.project,"]: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 76 | cond = " and " + conditions["based_on_select"][:-1] + " IS Not NULL" |
| 77 | if conditions.get("trans") in ["Sales Order", "Purchase Order"]: |
Saurabh | 4f62c4c | 2016-10-03 12:48:25 +0530 | [diff] [blame] | 78 | cond += " and t1.status != 'Closed'" |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 79 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 80 | if conditions.get("trans") == "Quotation" and filters.get("group_by") == "Customer": |
rohitwaghchaure | 358a01a | 2019-09-10 19:18:30 +0530 | [diff] [blame] | 81 | cond += " and t1.quotation_to = 'Customer'" |
| 82 | |
Daizy Modi | 4efc947 | 2022-11-07 09:21:03 +0530 | [diff] [blame] | 83 | year_start_date, year_end_date = frappe.get_cached_value( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 84 | "Fiscal Year", filters.get("fiscal_year"), ["year_start_date", "year_end_date"] |
| 85 | ) |
rohitwaghchaure | 5b9d517 | 2016-03-17 22:46:09 +0530 | [diff] [blame] | 86 | |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 87 | if filters.get("group_by"): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 88 | sel_col = "" |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 89 | ind = conditions["columns"].index(conditions["grbc"][0]) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 90 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 91 | 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" |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 97 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 98 | if filters.get("based_on") in ["Item", "Customer", "Supplier"]: |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 99 | inc = 2 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 100 | else: |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 101 | inc = 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 102 | data1 = frappe.db.sql( |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 103 | """ 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 Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 108 | 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 | ) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 123 | |
| 124 | for d in range(len(data1)): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 125 | # to add blanck column |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 126 | dt = data1[d] |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 127 | dt.insert(ind, "") |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 128 | data.append(dt) |
| 129 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 130 | # to get distinct value of col specified by group_by in filter |
| 131 | row = frappe.db.sql( |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 132 | """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 Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 136 | 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 | ) |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 152 | |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 153 | for i in range(len(row)): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 154 | des = ["" for q in range(len(conditions["columns"]))] |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 155 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 156 | # get data for group_by filter |
| 157 | row1 = frappe.db.sql( |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 158 | """ 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 Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 162 | 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 | ) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 181 | |
shreyas | e970ddc | 2016-01-28 16:38:59 +0530 | [diff] [blame] | 182 | des[ind] = row[i][0] |
| 183 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 184 | for j in range(1, len(conditions["columns"]) - inc): |
| 185 | des[j + inc] = row1[0][j] |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 186 | |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 187 | data.append(des) |
| 188 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 189 | data = frappe.db.sql( |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 190 | """ 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 Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 195 | 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 | ) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 210 | |
| 211 | return data |
| 212 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 213 | |
Nabin Hait | b8ebbca | 2013-06-20 13:03:10 +0530 | [diff] [blame] | 214 | def get_mon(dt): |
| 215 | return getdate(dt).strftime("%b") |
Saurabh | d4f2199 | 2013-06-19 14:44:44 +0530 | [diff] [blame] | 216 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 217 | |
Nabin Hait | b743889 | 2014-06-05 16:14:28 +0530 | [diff] [blame] | 218 | def period_wise_columns_query(filters, trans): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 219 | query_details = "" |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 220 | pwc = [] |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 221 | bet_dates = get_period_date_ranges(filters.get("period"), filters.get("fiscal_year")) |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 222 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 223 | if trans in ["Purchase Receipt", "Delivery Note", "Purchase Invoice", "Sales Invoice"]: |
| 224 | trans_date = "posting_date" |
Anurag Mishra | fe5890b | 2019-10-07 14:27:07 +0530 | [diff] [blame] | 225 | if filters.period_based_on: |
| 226 | trans_date = filters.period_based_on |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 227 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 228 | trans_date = "transaction_date" |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 229 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 230 | if filters.get("period") != "Yearly": |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 231 | for dt in bet_dates: |
| 232 | get_period_wise_columns(dt, filters.get("period"), pwc) |
Rohit Waghchaure | 87ad6d0 | 2017-04-10 16:42:11 +0530 | [diff] [blame] | 233 | query_details = get_period_wise_query(dt, trans_date, query_details) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 234 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 235 | pwc = [ |
| 236 | _(filters.get("fiscal_year")) + " (" + _("Qty") + "):Float:120", |
| 237 | _(filters.get("fiscal_year")) + " (" + _("Amt") + "):Currency:120", |
| 238 | ] |
Rohit Waghchaure | 87ad6d0 | 2017-04-10 16:42:11 +0530 | [diff] [blame] | 239 | query_details = " SUM(t2.stock_qty), SUM(t2.base_net_amount)," |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 240 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 241 | query_details += "SUM(t2.stock_qty), SUM(t2.base_net_amount)" |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 242 | return pwc, query_details |
| 243 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 244 | |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 245 | def get_period_wise_columns(bet_dates, period, pwc): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 246 | if period == "Monthly": |
| 247 | pwc += [ |
| 248 | _(get_mon(bet_dates[0])) + " (" + _("Qty") + "):Float:120", |
| 249 | _(get_mon(bet_dates[0])) + " (" + _("Amt") + "):Currency:120", |
| 250 | ] |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 251 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 252 | 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 | |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 257 | |
Rohit Waghchaure | 87ad6d0 | 2017-04-10 16:42:11 +0530 | [diff] [blame] | 258 | def get_period_wise_query(bet_dates, trans_date, query_details): |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 259 | 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 Hait | b8ebbca | 2013-06-20 13:03:10 +0530 | [diff] [blame] | 266 | return query_details |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 267 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 268 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 269 | @frappe.whitelist(allow_guest=True) |
Akhilesh Darjee | ec0da0b | 2013-11-25 19:51:18 +0530 | [diff] [blame] | 270 | def get_period_date_ranges(period, fiscal_year=None, year_start_date=None): |
| 271 | from dateutil.relativedelta import relativedelta |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 272 | |
Akhilesh Darjee | ec0da0b | 2013-11-25 19:51:18 +0530 | [diff] [blame] | 273 | if not year_start_date: |
Daizy Modi | 4efc947 | 2022-11-07 09:21:03 +0530 | [diff] [blame] | 274 | year_start_date, year_end_date = frappe.get_cached_value( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 275 | "Fiscal Year", fiscal_year, ["year_start_date", "year_end_date"] |
| 276 | ) |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 277 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 278 | increment = {"Monthly": 1, "Quarterly": 3, "Half-Yearly": 6, "Yearly": 12}.get(period) |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 279 | |
Akhilesh Darjee | ec0da0b | 2013-11-25 19:51:18 +0530 | [diff] [blame] | 280 | period_date_ranges = [] |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 281 | for _i in range(1, 13, increment): |
Akhilesh Darjee | ec0da0b | 2013-11-25 19:51:18 +0530 | [diff] [blame] | 282 | 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 |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 291 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 292 | |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 293 | def get_period_month_ranges(period, fiscal_year): |
| 294 | from dateutil.relativedelta import relativedelta |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 295 | |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 296 | 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 Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 307 | |
Nabin Hait | b743889 | 2014-06-05 16:14:28 +0530 | [diff] [blame] | 308 | def based_wise_columns_query(based_on, trans): |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 309 | based_on_details = {} |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 310 | |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 311 | # based_on_cols, based_on_select, based_on_group_by, addl_tables |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 312 | if based_on == "Item": |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 313 | based_on_details["based_on_cols"] = ["Item:Link/Item:120", "Item Name:Data:120"] |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 314 | based_on_details["based_on_select"] = "t2.item_code, t2.item_name," |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 315 | based_on_details["based_on_group_by"] = "t2.item_code" |
| 316 | based_on_details["addl_tables"] = "" |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 317 | |
| 318 | elif based_on == "Item Group": |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 319 | based_on_details["based_on_cols"] = ["Item Group:Link/Item Group:120"] |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 320 | based_on_details["based_on_select"] = "t2.item_group," |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 321 | based_on_details["based_on_group_by"] = "t2.item_group" |
| 322 | based_on_details["addl_tables"] = "" |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 323 | |
| 324 | elif based_on == "Customer": |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 325 | based_on_details["based_on_cols"] = [ |
| 326 | "Customer:Link/Customer:120", |
| 327 | "Territory:Link/Territory:120", |
| 328 | ] |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 329 | based_on_details["based_on_select"] = "t1.customer_name, t1.territory, " |
Akhil Narang | 3effaf2 | 2024-03-27 11:37:26 +0530 | [diff] [blame] | 330 | based_on_details["based_on_group_by"] = "t1.party_name" if trans == "Quotation" else "t1.customer" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 331 | based_on_details["addl_tables"] = "" |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 332 | |
| 333 | elif based_on == "Customer Group": |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 334 | based_on_details["based_on_cols"] = ["Customer Group:Link/Customer Group"] |
| 335 | based_on_details["based_on_select"] = "t1.customer_group," |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 336 | based_on_details["based_on_group_by"] = "t1.customer_group" |
| 337 | based_on_details["addl_tables"] = "" |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 338 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 339 | elif based_on == "Supplier": |
| 340 | based_on_details["based_on_cols"] = [ |
| 341 | "Supplier:Link/Supplier:120", |
| 342 | "Supplier Group:Link/Supplier Group:140", |
| 343 | ] |
Zlash65 | 2e08098 | 2018-04-19 18:37:53 +0530 | [diff] [blame] | 344 | based_on_details["based_on_select"] = "t1.supplier, t3.supplier_group," |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 345 | based_on_details["based_on_group_by"] = "t1.supplier" |
| 346 | based_on_details["addl_tables"] = ",`tabSupplier` t3" |
Nabin Hait | a6b597a | 2014-08-11 11:54:21 +0530 | [diff] [blame] | 347 | based_on_details["addl_tables_relational_cond"] = " and t1.supplier = t3.name" |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 348 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 349 | elif based_on == "Supplier Group": |
Zlash65 | 2e08098 | 2018-04-19 18:37:53 +0530 | [diff] [blame] | 350 | based_on_details["based_on_cols"] = ["Supplier Group:Link/Supplier Group:140"] |
| 351 | based_on_details["based_on_select"] = "t3.supplier_group," |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 352 | based_on_details["based_on_group_by"] = "t3.supplier_group" |
| 353 | based_on_details["addl_tables"] = ",`tabSupplier` t3" |
Nabin Hait | a6b597a | 2014-08-11 11:54:21 +0530 | [diff] [blame] | 354 | based_on_details["addl_tables_relational_cond"] = " and t1.supplier = t3.name" |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 355 | |
| 356 | elif based_on == "Territory": |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 357 | based_on_details["based_on_cols"] = ["Territory:Link/Territory:120"] |
| 358 | based_on_details["based_on_select"] = "t1.territory," |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 359 | based_on_details["based_on_group_by"] = "t1.territory" |
| 360 | based_on_details["addl_tables"] = "" |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 361 | |
| 362 | elif based_on == "Project": |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 363 | if trans in ["Sales Invoice", "Delivery Note", "Sales Order"]: |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 364 | based_on_details["based_on_cols"] = ["Project:Link/Project:120"] |
Neil Trini Lasrado | 6e343e2 | 2016-03-09 17:02:59 +0530 | [diff] [blame] | 365 | based_on_details["based_on_select"] = "t1.project," |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 366 | 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"]: |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 369 | based_on_details["based_on_cols"] = ["Project:Link/Project:120"] |
Neil Trini Lasrado | 6e343e2 | 2016-03-09 17:02:59 +0530 | [diff] [blame] | 370 | based_on_details["based_on_select"] = "t2.project," |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 371 | based_on_details["based_on_group_by"] = "t2.project" |
| 372 | based_on_details["addl_tables"] = "" |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 373 | else: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 374 | frappe.throw(_("Project-wise data is not available for Quotation")) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 375 | |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 376 | return based_on_details |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 377 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 378 | |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 379 | def group_wise_column(group_by): |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 380 | if group_by: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 381 | return [group_by + ":Link/" + group_by + ":120"] |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 382 | else: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 383 | return [] |