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 | |
| 4 | from __future__ import unicode_literals |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 6 | from frappe.utils import getdate |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 7 | from frappe import _ |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 8 | |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 9 | def get_columns(filters, trans): |
Nabin Hait | b8ebbca | 2013-06-20 13:03:10 +0530 | [diff] [blame] | 10 | validate_filters(filters) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 11 | |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 12 | # get conditions for based_on filter cond |
Nabin Hait | b743889 | 2014-06-05 16:14:28 +0530 | [diff] [blame] | 13 | based_on_details = based_wise_columns_query(filters.get("based_on"), trans) |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 14 | # get conditions for periodic filter cond |
Nabin Hait | b743889 | 2014-06-05 16:14:28 +0530 | [diff] [blame] | 15 | period_cols, period_select = period_wise_columns_query(filters, trans) |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 16 | # get conditions for grouping filter cond |
| 17 | group_by_cols = group_wise_column(filters.get("group_by")) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 18 | |
81552433qqcom | 951da0c | 2014-09-11 16:15:27 +0800 | [diff] [blame] | 19 | columns = based_on_details["based_on_cols"] + period_cols + [_("Total(Qty)") + ":Float:120", _("Total(Amt)") + ":Currency:120"] |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 20 | if group_by_cols: |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 21 | columns = based_on_details["based_on_cols"] + group_by_cols + period_cols + \ |
81552433qqcom | 951da0c | 2014-09-11 16:15:27 +0800 | [diff] [blame] | 22 | [_("Total(Qty)") + ":Float:120", _("Total(Amt)") + ":Currency:120"] |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 23 | |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 24 | conditions = {"based_on_select": based_on_details["based_on_select"], "period_wise_select": period_select, |
Saurabh | 36cb3ca | 2013-06-21 16:25:28 +0530 | [diff] [blame] | 25 | "columns": columns, "group_by": based_on_details["based_on_group_by"], "grbc": group_by_cols, "trans": trans, |
Nabin Hait | a6b597a | 2014-08-11 11:54:21 +0530 | [diff] [blame] | 26 | "addl_tables": based_on_details["addl_tables"], "addl_tables_relational_cond": based_on_details.get("addl_tables_relational_cond", "")} |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 27 | |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 28 | return conditions |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 29 | |
Nabin Hait | b8ebbca | 2013-06-20 13:03:10 +0530 | [diff] [blame] | 30 | def validate_filters(filters): |
| 31 | for f in ["Fiscal Year", "Based On", "Period", "Company"]: |
| 32 | if not filters.get(f.lower().replace(" ", "_")): |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 33 | frappe.throw(_("{0} is mandatory").format(f)) |
shreyas | e970ddc | 2016-01-28 16:38:59 +0530 | [diff] [blame] | 34 | |
Nabin Hait | c5b8f7e | 2015-04-30 16:10:58 +0530 | [diff] [blame] | 35 | if not frappe.db.exists("Fiscal Year", filters.get("fiscal_year")): |
| 36 | frappe.throw(_("Fiscal Year: {0} does not exists").format(filters.get("fiscal_year"))) |
shreyas | e970ddc | 2016-01-28 16:38:59 +0530 | [diff] [blame] | 37 | |
Nabin Hait | b8ebbca | 2013-06-20 13:03:10 +0530 | [diff] [blame] | 38 | if filters.get("based_on") == filters.get("group_by"): |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 39 | frappe.throw(_("'Based On' and 'Group By' can not be same")) |
Nabin Hait | b8ebbca | 2013-06-20 13:03:10 +0530 | [diff] [blame] | 40 | |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 41 | def get_data(filters, conditions): |
Nabin Hait | 5c69fed | 2016-09-02 12:53:18 +0530 | [diff] [blame] | 42 | |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 43 | data = [] |
| 44 | inc, cond= '','' |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 45 | query_details = conditions["based_on_select"] + conditions["period_wise_select"] |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 46 | |
rohitwaghchaure | 49ccac5 | 2016-03-20 19:55:17 +0530 | [diff] [blame] | 47 | posting_date = 't1.transaction_date' |
| 48 | if conditions.get('trans') in ['Sales Invoice', 'Purchase Invoice', 'Purchase Receipt', 'Delivery Note']: |
| 49 | posting_date = 't1.posting_date' |
| 50 | |
Neil Trini Lasrado | 6e343e2 | 2016-03-09 17:02:59 +0530 | [diff] [blame] | 51 | if conditions["based_on_select"] in ["t1.project,", "t2.project,"]: |
Saurabh | 4f62c4c | 2016-10-03 12:48:25 +0530 | [diff] [blame] | 52 | cond = ' and '+ conditions["based_on_select"][:-1] +' IS Not NULL' |
Nabin Hait | 5c69fed | 2016-09-02 12:53:18 +0530 | [diff] [blame] | 53 | |
| 54 | if conditions.get('trans') in ['Sales Order', 'Purchase Order']: |
Saurabh | 4f62c4c | 2016-10-03 12:48:25 +0530 | [diff] [blame] | 55 | cond += " and t1.status != 'Closed'" |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 56 | |
rohitwaghchaure | 5b9d517 | 2016-03-17 22:46:09 +0530 | [diff] [blame] | 57 | year_start_date, year_end_date = frappe.db.get_value("Fiscal Year", |
| 58 | filters.get('fiscal_year'), ["year_start_date", "year_end_date"]) |
| 59 | |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 60 | if filters.get("group_by"): |
| 61 | sel_col = '' |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 62 | ind = conditions["columns"].index(conditions["grbc"][0]) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 63 | |
| 64 | if filters.get("group_by") == 'Item': |
| 65 | sel_col = 't2.item_code' |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 66 | elif filters.get("group_by") == 'Customer': |
| 67 | sel_col = 't1.customer' |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 68 | elif filters.get("group_by") == 'Supplier': |
| 69 | sel_col = 't1.supplier' |
| 70 | |
| 71 | if filters.get('based_on') in ['Item','Customer','Supplier']: |
| 72 | inc = 2 |
| 73 | else : |
| 74 | inc = 1 |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 75 | data1 = frappe.db.sql(""" select %s from `tab%s` t1, `tab%s Item` t2 %s |
rohitwaghchaure | 49ccac5 | 2016-03-20 19:55:17 +0530 | [diff] [blame] | 76 | 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] | 77 | t1.docstatus = 1 %s %s |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 78 | group by %s |
| 79 | """ % (query_details, conditions["trans"], conditions["trans"], conditions["addl_tables"], "%s", |
rohitwaghchaure | 49ccac5 | 2016-03-20 19:55:17 +0530 | [diff] [blame] | 80 | posting_date, "%s", "%s", conditions.get("addl_tables_relational_cond"), cond, conditions["group_by"]), (filters.get("company"), |
rohitwaghchaure | 5b9d517 | 2016-03-17 22:46:09 +0530 | [diff] [blame] | 81 | year_start_date, year_end_date),as_list=1) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 82 | |
| 83 | for d in range(len(data1)): |
| 84 | #to add blanck column |
| 85 | dt = data1[d] |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 86 | dt.insert(ind,'') |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 87 | data.append(dt) |
| 88 | |
| 89 | #to get distinct value of col specified by group_by in filter |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 90 | row = frappe.db.sql("""select DISTINCT(%s) from `tab%s` t1, `tab%s Item` t2 %s |
rohitwaghchaure | 49ccac5 | 2016-03-20 19:55:17 +0530 | [diff] [blame] | 91 | 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] | 92 | and t1.docstatus = 1 and %s = %s %s %s |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 93 | """ % |
| 94 | (sel_col, conditions["trans"], conditions["trans"], conditions["addl_tables"], |
Nabin Hait | 5c69fed | 2016-09-02 12:53:18 +0530 | [diff] [blame] | 95 | "%s", posting_date, "%s", "%s", conditions["group_by"], "%s", conditions.get("addl_tables_relational_cond"), cond), |
rohitwaghchaure | 5b9d517 | 2016-03-17 22:46:09 +0530 | [diff] [blame] | 96 | (filters.get("company"), year_start_date, year_end_date, data1[d][0]), as_list=1) |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 97 | |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 98 | for i in range(len(row)): |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 99 | des = ['' for q in range(len(conditions["columns"]))] |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 100 | |
| 101 | #get data for group_by filter |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 102 | row1 = frappe.db.sql(""" select %s , %s from `tab%s` t1, `tab%s Item` t2 %s |
rohitwaghchaure | 49ccac5 | 2016-03-20 19:55:17 +0530 | [diff] [blame] | 103 | 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] | 104 | and t1.docstatus = 1 and %s = %s and %s = %s %s %s |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 105 | """ % |
| 106 | (sel_col, conditions["period_wise_select"], conditions["trans"], |
rohitwaghchaure | 49ccac5 | 2016-03-20 19:55:17 +0530 | [diff] [blame] | 107 | conditions["trans"], conditions["addl_tables"], "%s", posting_date, "%s","%s", sel_col, |
Nabin Hait | 5c69fed | 2016-09-02 12:53:18 +0530 | [diff] [blame] | 108 | "%s", conditions["group_by"], "%s", conditions.get("addl_tables_relational_cond"), cond), |
rohitwaghchaure | 5b9d517 | 2016-03-17 22:46:09 +0530 | [diff] [blame] | 109 | (filters.get("company"), year_start_date, year_end_date, row[i][0], |
Nabin Hait | 4555d3b | 2013-06-21 16:27:34 +0530 | [diff] [blame] | 110 | data1[d][0]), as_list=1) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 111 | |
shreyas | e970ddc | 2016-01-28 16:38:59 +0530 | [diff] [blame] | 112 | des[ind] = row[i][0] |
| 113 | |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 114 | for j in range(1,len(conditions["columns"])-inc): |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 115 | des[j+inc] = row1[0][j] |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 116 | |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 117 | data.append(des) |
| 118 | else: |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 119 | data = frappe.db.sql(""" select %s from `tab%s` t1, `tab%s Item` t2 %s |
rohitwaghchaure | 49ccac5 | 2016-03-20 19:55:17 +0530 | [diff] [blame] | 120 | 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] | 121 | t1.docstatus = 1 %s %s |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 122 | group by %s |
| 123 | """ % |
| 124 | (query_details, conditions["trans"], conditions["trans"], conditions["addl_tables"], |
rohitwaghchaure | 49ccac5 | 2016-03-20 19:55:17 +0530 | [diff] [blame] | 125 | "%s", posting_date, "%s", "%s", cond, conditions.get("addl_tables_relational_cond", ""), conditions["group_by"]), |
rohitwaghchaure | 5b9d517 | 2016-03-17 22:46:09 +0530 | [diff] [blame] | 126 | (filters.get("company"), year_start_date, year_end_date), as_list=1) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 127 | |
| 128 | return data |
| 129 | |
Nabin Hait | b8ebbca | 2013-06-20 13:03:10 +0530 | [diff] [blame] | 130 | def get_mon(dt): |
| 131 | return getdate(dt).strftime("%b") |
Saurabh | d4f2199 | 2013-06-19 14:44:44 +0530 | [diff] [blame] | 132 | |
Nabin Hait | b743889 | 2014-06-05 16:14:28 +0530 | [diff] [blame] | 133 | def period_wise_columns_query(filters, trans): |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 134 | query_details = '' |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 135 | pwc = [] |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 136 | bet_dates = get_period_date_ranges(filters.get("period"), filters.get("fiscal_year")) |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 137 | |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 138 | if trans in ['Purchase Receipt', 'Delivery Note', 'Purchase Invoice', 'Sales Invoice']: |
| 139 | trans_date = 'posting_date' |
| 140 | else: |
| 141 | trans_date = 'transaction_date' |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 142 | |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 143 | if filters.get("period") != 'Yearly': |
| 144 | for dt in bet_dates: |
| 145 | get_period_wise_columns(dt, filters.get("period"), pwc) |
Rohit Waghchaure | 87ad6d0 | 2017-04-10 16:42:11 +0530 | [diff] [blame] | 146 | query_details = get_period_wise_query(dt, trans_date, query_details) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 147 | else: |
81552433qqcom | 951da0c | 2014-09-11 16:15:27 +0800 | [diff] [blame] | 148 | pwc = [_(filters.get("fiscal_year")) + " ("+_("Qty") + "):Float:120", |
| 149 | _(filters.get("fiscal_year")) + " ("+ _("Amt") + "):Currency:120"] |
Rohit Waghchaure | 87ad6d0 | 2017-04-10 16:42:11 +0530 | [diff] [blame] | 150 | query_details = " SUM(t2.stock_qty), SUM(t2.base_net_amount)," |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 151 | |
Rohit Waghchaure | 87ad6d0 | 2017-04-10 16:42:11 +0530 | [diff] [blame] | 152 | query_details += 'SUM(t2.stock_qty), SUM(t2.base_net_amount)' |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 153 | return pwc, query_details |
| 154 | |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 155 | def get_period_wise_columns(bet_dates, period, pwc): |
| 156 | if period == 'Monthly': |
81552433qqcom | 951da0c | 2014-09-11 16:15:27 +0800 | [diff] [blame] | 157 | pwc += [_(get_mon(bet_dates[0])) + " (" + _("Qty") + "):Float:120", |
| 158 | _(get_mon(bet_dates[0])) + " (" + _("Amt") + "):Currency:120"] |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 159 | else: |
81552433qqcom | 951da0c | 2014-09-11 16:15:27 +0800 | [diff] [blame] | 160 | pwc += [_(get_mon(bet_dates[0])) + "-" + _(get_mon(bet_dates[1])) + " (" + _("Qty") + "):Float:120", |
| 161 | _(get_mon(bet_dates[0])) + "-" + _(get_mon(bet_dates[1])) + " (" + _("Amt") + "):Currency:120"] |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 162 | |
Rohit Waghchaure | 87ad6d0 | 2017-04-10 16:42:11 +0530 | [diff] [blame] | 163 | def get_period_wise_query(bet_dates, trans_date, query_details): |
| 164 | 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] | 165 | SUM(IF(t1.%(trans_date)s BETWEEN '%(sd)s' AND '%(ed)s', t2.base_net_amount, NULL)), |
Rohit Waghchaure | 87ad6d0 | 2017-04-10 16:42:11 +0530 | [diff] [blame] | 166 | """ % {"trans_date": trans_date, "sd": bet_dates[0],"ed": bet_dates[1]} |
Nabin Hait | b8ebbca | 2013-06-20 13:03:10 +0530 | [diff] [blame] | 167 | return query_details |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 168 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 169 | @frappe.whitelist(allow_guest=True) |
Akhilesh Darjee | ec0da0b | 2013-11-25 19:51:18 +0530 | [diff] [blame] | 170 | def get_period_date_ranges(period, fiscal_year=None, year_start_date=None): |
| 171 | from dateutil.relativedelta import relativedelta |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 172 | |
Akhilesh Darjee | ec0da0b | 2013-11-25 19:51:18 +0530 | [diff] [blame] | 173 | if not year_start_date: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 174 | year_start_date, year_end_date = frappe.db.get_value("Fiscal Year", |
Akhilesh Darjee | ec0da0b | 2013-11-25 19:51:18 +0530 | [diff] [blame] | 175 | fiscal_year, ["year_start_date", "year_end_date"]) |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 176 | |
Akhilesh Darjee | ec0da0b | 2013-11-25 19:51:18 +0530 | [diff] [blame] | 177 | increment = { |
| 178 | "Monthly": 1, |
| 179 | "Quarterly": 3, |
| 180 | "Half-Yearly": 6, |
| 181 | "Yearly": 12 |
| 182 | }.get(period) |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 183 | |
Akhilesh Darjee | ec0da0b | 2013-11-25 19:51:18 +0530 | [diff] [blame] | 184 | period_date_ranges = [] |
| 185 | for i in xrange(1, 13, increment): |
| 186 | period_end_date = getdate(year_start_date) + relativedelta(months=increment, days=-1) |
| 187 | if period_end_date > getdate(year_end_date): |
| 188 | period_end_date = year_end_date |
| 189 | period_date_ranges.append([year_start_date, period_end_date]) |
| 190 | year_start_date = period_end_date + relativedelta(days=1) |
| 191 | if period_end_date == year_end_date: |
| 192 | break |
| 193 | |
| 194 | return period_date_ranges |
Saurabh | f8f68c5 | 2013-06-20 18:52:31 +0530 | [diff] [blame] | 195 | |
| 196 | def get_period_month_ranges(period, fiscal_year): |
| 197 | from dateutil.relativedelta import relativedelta |
| 198 | period_month_ranges = [] |
| 199 | |
| 200 | for start_date, end_date in get_period_date_ranges(period, fiscal_year): |
| 201 | months_in_this_period = [] |
| 202 | while start_date <= end_date: |
| 203 | months_in_this_period.append(start_date.strftime("%B")) |
| 204 | start_date += relativedelta(months=1) |
| 205 | period_month_ranges.append(months_in_this_period) |
| 206 | |
| 207 | return period_month_ranges |
| 208 | |
Nabin Hait | b743889 | 2014-06-05 16:14:28 +0530 | [diff] [blame] | 209 | def based_wise_columns_query(based_on, trans): |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 210 | based_on_details = {} |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 211 | |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 212 | # based_on_cols, based_on_select, based_on_group_by, addl_tables |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 213 | if based_on == "Item": |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 214 | 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] | 215 | based_on_details["based_on_select"] = "t2.item_code, t2.item_name," |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 216 | based_on_details["based_on_group_by"] = 't2.item_code' |
| 217 | based_on_details["addl_tables"] = '' |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 218 | |
| 219 | elif based_on == "Item Group": |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 220 | based_on_details["based_on_cols"] = ["Item Group:Link/Item Group:120"] |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 221 | based_on_details["based_on_select"] = "t2.item_group," |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 222 | based_on_details["based_on_group_by"] = 't2.item_group' |
| 223 | based_on_details["addl_tables"] = '' |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 224 | |
| 225 | elif based_on == "Customer": |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 226 | based_on_details["based_on_cols"] = ["Customer:Link/Customer:120", "Territory:Link/Territory:120"] |
| 227 | based_on_details["based_on_select"] = "t1.customer_name, t1.territory, " |
shreyas | e970ddc | 2016-01-28 16:38:59 +0530 | [diff] [blame] | 228 | based_on_details["based_on_group_by"] = 't1.customer' |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 229 | based_on_details["addl_tables"] = '' |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 230 | |
| 231 | elif based_on == "Customer Group": |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 232 | based_on_details["based_on_cols"] = ["Customer Group:Link/Customer Group"] |
| 233 | based_on_details["based_on_select"] = "t1.customer_group," |
| 234 | based_on_details["based_on_group_by"] = 't1.customer_group' |
| 235 | based_on_details["addl_tables"] = '' |
| 236 | |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 237 | elif based_on == 'Supplier': |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 238 | based_on_details["based_on_cols"] = ["Supplier:Link/Supplier:120", "Supplier Type:Link/Supplier Type:140"] |
| 239 | based_on_details["based_on_select"] = "t1.supplier, t3.supplier_type," |
| 240 | based_on_details["based_on_group_by"] = 't1.supplier' |
| 241 | based_on_details["addl_tables"] = ',`tabSupplier` t3' |
Nabin Hait | a6b597a | 2014-08-11 11:54:21 +0530 | [diff] [blame] | 242 | based_on_details["addl_tables_relational_cond"] = " and t1.supplier = t3.name" |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 243 | |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 244 | elif based_on == 'Supplier Type': |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 245 | based_on_details["based_on_cols"] = ["Supplier Type:Link/Supplier Type:140"] |
| 246 | based_on_details["based_on_select"] = "t3.supplier_type," |
| 247 | based_on_details["based_on_group_by"] = 't3.supplier_type' |
Nabin Hait | a6b597a | 2014-08-11 11:54:21 +0530 | [diff] [blame] | 248 | based_on_details["addl_tables"] = ',`tabSupplier` t3' |
| 249 | based_on_details["addl_tables_relational_cond"] = " and t1.supplier = t3.name" |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 250 | |
| 251 | elif based_on == "Territory": |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 252 | based_on_details["based_on_cols"] = ["Territory:Link/Territory:120"] |
| 253 | based_on_details["based_on_select"] = "t1.territory," |
| 254 | based_on_details["based_on_group_by"] = 't1.territory' |
| 255 | based_on_details["addl_tables"] = '' |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 256 | |
| 257 | elif based_on == "Project": |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 258 | if trans in ['Sales Invoice', 'Delivery Note', 'Sales Order']: |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 259 | based_on_details["based_on_cols"] = ["Project:Link/Project:120"] |
Neil Trini Lasrado | 6e343e2 | 2016-03-09 17:02:59 +0530 | [diff] [blame] | 260 | based_on_details["based_on_select"] = "t1.project," |
| 261 | based_on_details["based_on_group_by"] = 't1.project' |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 262 | based_on_details["addl_tables"] = '' |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 263 | elif trans in ['Purchase Order', 'Purchase Invoice', 'Purchase Receipt']: |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 264 | based_on_details["based_on_cols"] = ["Project:Link/Project:120"] |
Neil Trini Lasrado | 6e343e2 | 2016-03-09 17:02:59 +0530 | [diff] [blame] | 265 | based_on_details["based_on_select"] = "t2.project," |
| 266 | based_on_details["based_on_group_by"] = 't2.project' |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 267 | based_on_details["addl_tables"] = '' |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 268 | else: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 269 | frappe.throw(_("Project-wise data is not available for Quotation")) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 270 | |
Saurabh | 2b02f14 | 2013-06-21 10:46:26 +0530 | [diff] [blame] | 271 | return based_on_details |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 272 | |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 273 | def group_wise_column(group_by): |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 274 | if group_by: |
| 275 | return [group_by+":Link/"+group_by+":120"] |
| 276 | else: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 277 | return [] |