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 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 83 | year_start_date, year_end_date = frappe.db.get_value( |
| 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( |
| 103 | """ select %s from `tab%s` t1, `tab%s Item` t2 %s |
rohitwaghchaure | 49ccac5 | 2016-03-20 19:55:17 +0530 | [diff] [blame] | 104 | where t2.parent = t1.name and t1.company = %s and %s between %s and %s and |
Nabin Hait | a6b597a | 2014-08-11 11:54:21 +0530 | [diff] [blame] | 105 | t1.docstatus = 1 %s %s |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 106 | group by %s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 107 | """ |
| 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 | ) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 124 | |
| 125 | for d in range(len(data1)): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 126 | # to add blanck column |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 127 | dt = data1[d] |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 128 | dt.insert(ind, "") |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 129 | data.append(dt) |
| 130 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 131 | # 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 |
rohitwaghchaure | 49ccac5 | 2016-03-20 19:55:17 +0530 | [diff] [blame] | 134 | where t2.parent = t1.name and t1.company = %s and %s between %s and %s |
Nabin Hait | 5c69fed | 2016-09-02 12:53:18 +0530 | [diff] [blame] | 135 | and t1.docstatus = 1 and %s = %s %s %s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 136 | """ |
| 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 | ) |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 154 | |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 155 | for i in range(len(row)): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 156 | des = ["" for q in range(len(conditions["columns"]))] |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 157 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 158 | # get data for group_by filter |
| 159 | row1 = frappe.db.sql( |
| 160 | """ select %s , %s from `tab%s` t1, `tab%s Item` t2 %s |
rohitwaghchaure | 49ccac5 | 2016-03-20 19:55:17 +0530 | [diff] [blame] | 161 | where t2.parent = t1.name and t1.company = %s and %s between %s and %s |
Nabin Hait | 5c69fed | 2016-09-02 12:53:18 +0530 | [diff] [blame] | 162 | and t1.docstatus = 1 and %s = %s and %s = %s %s %s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 163 | """ |
| 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 | ) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 184 | |
shreyas | e970ddc | 2016-01-28 16:38:59 +0530 | [diff] [blame] | 185 | des[ind] = row[i][0] |
| 186 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 187 | for j in range(1, len(conditions["columns"]) - inc): |
| 188 | des[j + inc] = row1[0][j] |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 189 | |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 190 | data.append(des) |
| 191 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 192 | data = frappe.db.sql( |
| 193 | """ select %s from `tab%s` t1, `tab%s Item` t2 %s |
rohitwaghchaure | 49ccac5 | 2016-03-20 19:55:17 +0530 | [diff] [blame] | 194 | where t2.parent = t1.name and t1.company = %s and %s between %s and %s and |
Nabin Hait | a6b597a | 2014-08-11 11:54:21 +0530 | [diff] [blame] | 195 | t1.docstatus = 1 %s %s |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 196 | group by %s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 197 | """ |
| 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 | ) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 214 | |
| 215 | return data |
| 216 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 217 | |
Nabin Hait | b8ebbca | 2013-06-20 13:03:10 +0530 | [diff] [blame] | 218 | def get_mon(dt): |
| 219 | return getdate(dt).strftime("%b") |
Saurabh | d4f2199 | 2013-06-19 14:44:44 +0530 | [diff] [blame] | 220 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 221 | |
Nabin Hait | b743889 | 2014-06-05 16:14:28 +0530 | [diff] [blame] | 222 | def period_wise_columns_query(filters, trans): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 223 | query_details = "" |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 224 | pwc = [] |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 225 | bet_dates = get_period_date_ranges(filters.get("period"), filters.get("fiscal_year")) |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 226 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 227 | if trans in ["Purchase Receipt", "Delivery Note", "Purchase Invoice", "Sales Invoice"]: |
| 228 | trans_date = "posting_date" |
Anurag Mishra | fe5890b | 2019-10-07 14:27:07 +0530 | [diff] [blame] | 229 | if filters.period_based_on: |
| 230 | trans_date = filters.period_based_on |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 231 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 232 | trans_date = "transaction_date" |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 233 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 234 | if filters.get("period") != "Yearly": |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 235 | for dt in bet_dates: |
| 236 | get_period_wise_columns(dt, filters.get("period"), pwc) |
Rohit Waghchaure | 87ad6d0 | 2017-04-10 16:42:11 +0530 | [diff] [blame] | 237 | query_details = get_period_wise_query(dt, trans_date, query_details) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 238 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 239 | pwc = [ |
| 240 | _(filters.get("fiscal_year")) + " (" + _("Qty") + "):Float:120", |
| 241 | _(filters.get("fiscal_year")) + " (" + _("Amt") + "):Currency:120", |
| 242 | ] |
Rohit Waghchaure | 87ad6d0 | 2017-04-10 16:42:11 +0530 | [diff] [blame] | 243 | query_details = " SUM(t2.stock_qty), SUM(t2.base_net_amount)," |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 244 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 245 | query_details += "SUM(t2.stock_qty), SUM(t2.base_net_amount)" |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 246 | return pwc, query_details |
| 247 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 248 | |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 249 | def get_period_wise_columns(bet_dates, period, pwc): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 250 | if period == "Monthly": |
| 251 | pwc += [ |
| 252 | _(get_mon(bet_dates[0])) + " (" + _("Qty") + "):Float:120", |
| 253 | _(get_mon(bet_dates[0])) + " (" + _("Amt") + "):Currency:120", |
| 254 | ] |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 255 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 256 | 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 | |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 261 | |
Rohit Waghchaure | 87ad6d0 | 2017-04-10 16:42:11 +0530 | [diff] [blame] | 262 | def 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 Hait | 82e3e25 | 2015-02-23 16:58:30 +0530 | [diff] [blame] | 264 | SUM(IF(t1.%(trans_date)s BETWEEN '%(sd)s' AND '%(ed)s', t2.base_net_amount, NULL)), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 265 | """ % { |
| 266 | "trans_date": trans_date, |
| 267 | "sd": bet_dates[0], |
| 268 | "ed": bet_dates[1], |
| 269 | } |
Nabin Hait | b8ebbca | 2013-06-20 13:03:10 +0530 | [diff] [blame] | 270 | return query_details |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 271 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 272 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 273 | @frappe.whitelist(allow_guest=True) |
Akhilesh Darjee | ec0da0b | 2013-11-25 19:51:18 +0530 | [diff] [blame] | 274 | def get_period_date_ranges(period, fiscal_year=None, year_start_date=None): |
| 275 | from dateutil.relativedelta import relativedelta |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 276 | |
Akhilesh Darjee | ec0da0b | 2013-11-25 19:51:18 +0530 | [diff] [blame] | 277 | if not year_start_date: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 278 | year_start_date, year_end_date = frappe.db.get_value( |
| 279 | "Fiscal Year", fiscal_year, ["year_start_date", "year_end_date"] |
| 280 | ) |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 281 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 282 | increment = {"Monthly": 1, "Quarterly": 3, "Half-Yearly": 6, "Yearly": 12}.get(period) |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 283 | |
Akhilesh Darjee | ec0da0b | 2013-11-25 19:51:18 +0530 | [diff] [blame] | 284 | period_date_ranges = [] |
Achilles Rasquinha | 96698c9 | 2018-02-28 16:12:51 +0530 | [diff] [blame] | 285 | for i in range(1, 13, increment): |
Akhilesh Darjee | ec0da0b | 2013-11-25 19:51:18 +0530 | [diff] [blame] | 286 | 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 |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 295 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 296 | |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 297 | def get_period_month_ranges(period, fiscal_year): |
| 298 | from dateutil.relativedelta import relativedelta |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 299 | |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 300 | 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 Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 311 | |
Nabin Hait | b743889 | 2014-06-05 16:14:28 +0530 | [diff] [blame] | 312 | def based_wise_columns_query(based_on, trans): |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 313 | based_on_details = {} |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 314 | |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 315 | # based_on_cols, based_on_select, based_on_group_by, addl_tables |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 316 | if based_on == "Item": |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 317 | 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] | 318 | based_on_details["based_on_select"] = "t2.item_code, t2.item_name," |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 319 | based_on_details["based_on_group_by"] = "t2.item_code" |
| 320 | based_on_details["addl_tables"] = "" |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 321 | |
| 322 | elif based_on == "Item Group": |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 323 | based_on_details["based_on_cols"] = ["Item Group:Link/Item Group:120"] |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 324 | based_on_details["based_on_select"] = "t2.item_group," |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 325 | based_on_details["based_on_group_by"] = "t2.item_group" |
| 326 | based_on_details["addl_tables"] = "" |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 327 | |
| 328 | elif based_on == "Customer": |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 329 | based_on_details["based_on_cols"] = [ |
| 330 | "Customer:Link/Customer:120", |
| 331 | "Territory:Link/Territory:120", |
| 332 | ] |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 333 | based_on_details["based_on_select"] = "t1.customer_name, t1.territory, " |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 334 | based_on_details["based_on_group_by"] = ( |
| 335 | "t1.party_name" if trans == "Quotation" else "t1.customer" |
| 336 | ) |
| 337 | based_on_details["addl_tables"] = "" |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 338 | |
| 339 | elif based_on == "Customer Group": |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 340 | based_on_details["based_on_cols"] = ["Customer Group:Link/Customer Group"] |
| 341 | based_on_details["based_on_select"] = "t1.customer_group," |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 342 | based_on_details["based_on_group_by"] = "t1.customer_group" |
| 343 | based_on_details["addl_tables"] = "" |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 344 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 345 | elif based_on == "Supplier": |
| 346 | based_on_details["based_on_cols"] = [ |
| 347 | "Supplier:Link/Supplier:120", |
| 348 | "Supplier Group:Link/Supplier Group:140", |
| 349 | ] |
Zlash65 | 2e08098 | 2018-04-19 18:37:53 +0530 | [diff] [blame] | 350 | based_on_details["based_on_select"] = "t1.supplier, t3.supplier_group," |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 351 | based_on_details["based_on_group_by"] = "t1.supplier" |
| 352 | based_on_details["addl_tables"] = ",`tabSupplier` t3" |
Nabin Hait | a6b597a | 2014-08-11 11:54:21 +0530 | [diff] [blame] | 353 | based_on_details["addl_tables_relational_cond"] = " and t1.supplier = t3.name" |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 354 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 355 | elif based_on == "Supplier Group": |
Zlash65 | 2e08098 | 2018-04-19 18:37:53 +0530 | [diff] [blame] | 356 | based_on_details["based_on_cols"] = ["Supplier Group:Link/Supplier Group:140"] |
| 357 | based_on_details["based_on_select"] = "t3.supplier_group," |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 358 | based_on_details["based_on_group_by"] = "t3.supplier_group" |
| 359 | based_on_details["addl_tables"] = ",`tabSupplier` t3" |
Nabin Hait | a6b597a | 2014-08-11 11:54:21 +0530 | [diff] [blame] | 360 | based_on_details["addl_tables_relational_cond"] = " and t1.supplier = t3.name" |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 361 | |
| 362 | elif based_on == "Territory": |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 363 | based_on_details["based_on_cols"] = ["Territory:Link/Territory:120"] |
| 364 | based_on_details["based_on_select"] = "t1.territory," |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 365 | based_on_details["based_on_group_by"] = "t1.territory" |
| 366 | based_on_details["addl_tables"] = "" |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 367 | |
| 368 | elif based_on == "Project": |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 369 | if trans in ["Sales Invoice", "Delivery Note", "Sales Order"]: |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 370 | based_on_details["based_on_cols"] = ["Project:Link/Project:120"] |
Neil Trini Lasrado | 6e343e2 | 2016-03-09 17:02:59 +0530 | [diff] [blame] | 371 | based_on_details["based_on_select"] = "t1.project," |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 372 | 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"]: |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 375 | based_on_details["based_on_cols"] = ["Project:Link/Project:120"] |
Neil Trini Lasrado | 6e343e2 | 2016-03-09 17:02:59 +0530 | [diff] [blame] | 376 | based_on_details["based_on_select"] = "t2.project," |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 377 | based_on_details["based_on_group_by"] = "t2.project" |
| 378 | based_on_details["addl_tables"] = "" |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 379 | else: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 380 | frappe.throw(_("Project-wise data is not available for Quotation")) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 381 | |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 382 | return based_on_details |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 383 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 384 | |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 385 | def group_wise_column(group_by): |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 386 | if group_by: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 387 | return [group_by + ":Link/" + group_by + ":120"] |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 388 | else: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 389 | return [] |