Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 1 | # ERPNext - web based ERP (http://erpnext.com) |
| 2 | # Copyright (C) 2012 Web Notes Technologies Pvt Ltd |
| 3 | # |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | from __future__ import unicode_literals |
| 18 | import webnotes |
| 19 | from webnotes.utils import cint, add_days, add_months, cstr |
| 20 | |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 21 | def get_columns(filters, trans): |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 22 | |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 23 | if not (filters.get("period") and filters.get("based_on")): |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 24 | webnotes.msgprint("Value missing in 'Period' or 'Based On'", raise_exception=1) |
| 25 | |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 26 | elif filters.get("based_on") == filters.get("group_by"): |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 27 | webnotes.msgprint("Plese select different values in 'Based On' and 'Group By'", raise_exception=1) |
| 28 | |
| 29 | else: |
Saurabh | bfe9fe7 | 2013-06-19 12:41:28 +0530 | [diff] [blame^] | 30 | bonc, query_bon, based, sup_tab = basedon_wise_colums_query(filters.get("based_on"), trans) |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 31 | pwc, query_pwc = period_wise_colums_query(filters, trans) |
| 32 | grbc = group_wise_column(filters.get("group_by")) |
| 33 | |
Saurabh | bfe9fe7 | 2013-06-19 12:41:28 +0530 | [diff] [blame^] | 34 | columns = bonc + pwc + ["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"] |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 35 | if grbc: |
Saurabh | bfe9fe7 | 2013-06-19 12:41:28 +0530 | [diff] [blame^] | 36 | columns = bonc + grbc + pwc +["TOTAL(Qty):Float:120", "TOTAL(Amt):Currency:120"] |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 37 | |
Saurabh | bfe9fe7 | 2013-06-19 12:41:28 +0530 | [diff] [blame^] | 38 | details = {"query_bon": query_bon, "query_pwc": query_pwc, "columns": columns, "basedon": based, |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 39 | "grbc": grbc, "sup_tab": sup_tab} |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 40 | |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 41 | return details |
| 42 | |
| 43 | def get_data(filters, tab, details): |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 44 | |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 45 | data = [] |
| 46 | inc, cond= '','' |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 47 | query_details = details["query_bon"] + details["query_pwc"] |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 48 | |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 49 | if details["query_bon"] in ["t1.project_name,", "t2.project_name,"]: |
| 50 | cond = 'and '+ details["query_bon"][:-1] +' IS Not NULL' |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 51 | |
| 52 | if filters.get("group_by"): |
| 53 | sel_col = '' |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 54 | ind = details["columns"].index(details["grbc"][0]) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 55 | |
| 56 | if filters.get("group_by") == 'Item': |
| 57 | sel_col = 't2.item_code' |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 58 | elif filters.get("group_by") == 'Customer': |
| 59 | sel_col = 't1.customer' |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 60 | elif filters.get("group_by") == 'Supplier': |
| 61 | sel_col = 't1.supplier' |
| 62 | |
| 63 | if filters.get('based_on') in ['Item','Customer','Supplier']: |
| 64 | inc = 2 |
| 65 | else : |
| 66 | inc = 1 |
| 67 | |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 68 | data1 = webnotes.conn.sql(""" select %s from `%s` t1, `%s` t2 %s |
Saurabh | 5b6d03e | 2013-06-19 12:34:22 +0530 | [diff] [blame] | 69 | where t2.parent = t1.name and t1.company = %s |
| 70 | and t1.fiscal_year = %s and t1.docstatus = 1 %s |
| 71 | group by %s |
| 72 | """ % (query_details, tab[0], tab[1], details["sup_tab"], "%s", |
| 73 | "%s", cond, details["basedon"]), (filters.get("company"), |
| 74 | filters["fiscal_year"]), |
| 75 | as_list=1) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 76 | |
| 77 | for d in range(len(data1)): |
| 78 | #to add blanck column |
| 79 | dt = data1[d] |
| 80 | dt.insert(ind,'') |
| 81 | data.append(dt) |
| 82 | |
| 83 | #to get distinct value of col specified by group_by in filter |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 84 | row = webnotes.conn.sql("""select DISTINCT(%s) from `%s` t1, `%s` t2 %s |
Saurabh | 5b6d03e | 2013-06-19 12:34:22 +0530 | [diff] [blame] | 85 | where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s |
| 86 | and t1.docstatus = 1 and %s = %s |
| 87 | """%(sel_col, tab[0], tab[1], details["sup_tab"], "%s", "%s", details["basedon"], "%s"), |
| 88 | (filters.get("company"), filters.get("fiscal_year"), data1[d][0]), |
| 89 | as_list=1) |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 90 | |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 91 | for i in range(len(row)): |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 92 | des = ['' for q in range(len(details["columns"]))] |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 93 | |
| 94 | #get data for each group_by filter |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 95 | row1 = webnotes.conn.sql(""" select %s , %s from `%s` t1, `%s` t2 %s |
Saurabh | 5b6d03e | 2013-06-19 12:34:22 +0530 | [diff] [blame] | 96 | where t2.parent = t1.name and t1.company = %s and t1.fiscal_year = %s |
| 97 | and t1.docstatus = 1 and %s = %s and %s = %s |
| 98 | """%(sel_col, details["query_pwc"], tab[0], tab[1], details["sup_tab"], |
| 99 | "%s", "%s", sel_col, "%s", details["basedon"], "%s"), |
| 100 | (filters.get("company"), filters.get("fiscal_year"), row[i][0], data1[d][0]), |
| 101 | as_list=1) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 102 | |
| 103 | des[ind] = row[i] |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 104 | for j in range(1,len(details["columns"])-inc): |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 105 | des[j+inc] = row1[0][j] |
| 106 | data.append(des) |
| 107 | else: |
| 108 | |
| 109 | data = webnotes.conn.sql(""" select %s from `%s` t1, `%s` t2 %s |
Saurabh | 5b6d03e | 2013-06-19 12:34:22 +0530 | [diff] [blame] | 110 | where t2.parent = t1.name and t1.company = %s |
| 111 | and t1.fiscal_year = %s and t1.docstatus = 1 %s |
| 112 | group by %s |
| 113 | """%(query_details, tab[0], tab[1], details["sup_tab"], "%s", |
| 114 | "%s", cond,details["basedon"]), (filters.get("company"), |
| 115 | filters.get("fiscal_year")), |
| 116 | as_list=1) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 117 | |
| 118 | return data |
| 119 | |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 120 | def period_wise_colums_query(filters, trans): |
| 121 | |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 122 | query_details = '' |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 123 | pwc = [] |
Saurabh | bfe9fe7 | 2013-06-19 12:41:28 +0530 | [diff] [blame^] | 124 | ysd = webnotes.conn.sql("""select year_start_date from `tabFiscal Year` where name = '%s' |
| 125 | """%filters.get("fiscal_year"))[0][0] |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 126 | |
| 127 | year_start_date = ysd.strftime('%Y-%m-%d') |
| 128 | start_month = cint(year_start_date.split('-')[1]) |
| 129 | |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 130 | if trans in ['Purchase Receipt', 'Delivery Note', 'Purchase Invoice', 'Sales Invoice']: |
| 131 | trans_date = 'posting_date' |
| 132 | else: |
| 133 | trans_date = 'transaction_date' |
| 134 | |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 135 | if filters.get("period") == "Monthly": |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 136 | month_name = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'] |
| 137 | |
| 138 | for month in range(start_month-1,len(month_name)): |
| 139 | pwc.append(month_name[month]+' (Qty):Float:120') |
| 140 | pwc.append(month_name[month]+' (Amt):Currency:120') |
| 141 | |
| 142 | query_details += """ |
Saurabh | 5b6d03e | 2013-06-19 12:34:22 +0530 | [diff] [blame] | 143 | Sum(IF(MONTH(t1.%(trans)s)= %(mon_num)s, t2.qty, NULL)), |
| 144 | SUM(IF(MONTH(t1.%(trans)s)= %(mon_num)s, t1.grand_total, NULL)), |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 145 | """%{"trans": trans_date,"mon_num": cstr(month+1)} |
| 146 | |
| 147 | for month in range(0, start_month-1): |
| 148 | pwc.append(month_name[month]+' (Qty):Float:120') |
| 149 | pwc.append(month_name[month]+' (Amt):Currency:120') |
| 150 | |
| 151 | query_details += """ |
Saurabh | 5b6d03e | 2013-06-19 12:34:22 +0530 | [diff] [blame] | 152 | Sum(IF(MONTH(t1.%(trans)s)= %(mon_num)s, t2.qty, NULL)), |
| 153 | SUM(IF(MONTH(t1.%(trans)s)= %(mon_num)s, t1.grand_total, NULL)), |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 154 | """%{"trans": trans_date, "mon_num": cstr(month+1)} |
| 155 | |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 156 | elif filters.get("period") == "Quarterly": |
| 157 | pwc = ["Q1 (Qty):Float:120", "Q1 (Amt):Currency:120", "Q2 (Qty):Float:120", "Q2 (Amt):Currency:120", |
| 158 | "Q3 (Qty):Float:120", "Q3 (Amt):Currency:120", "Q4 (Qty):Float:120", "Q4 (Amt):Currency:120"] |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 159 | |
| 160 | first_qsd, second_qsd, third_qsd, fourth_qsd = year_start_date, add_months(year_start_date,3), add_months(year_start_date,6), add_months(year_start_date,9) |
| 161 | first_qed, second_qed, third_qed, fourth_qed = add_days(add_months(first_qsd,3),-1), add_days(add_months(second_qsd,3),-1), add_days(add_months(third_qsd,3),-1), add_days(add_months(fourth_qsd,3),-1) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 162 | bet_dates = [[first_qsd,first_qed],[second_qsd,second_qed],[third_qsd,third_qed],[fourth_qsd,fourth_qed]] |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 163 | |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 164 | for d in bet_dates: |
| 165 | query_details += """ |
Saurabh | 5b6d03e | 2013-06-19 12:34:22 +0530 | [diff] [blame] | 166 | SUM(IF(t1.%(trans)s BETWEEN '%(sd)s' AND '%(ed)s', t2.qty, NULL)), |
| 167 | SUM(IF(t1.%(trans)s BETWEEN '%(sd)s' AND '%(ed)s', t1.grand_total, NULL)), |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 168 | """%{"trans": trans_date, "sd": d[0],"ed": d[1]} |
| 169 | |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 170 | elif filters.get("period") == "Half-yearly": |
| 171 | pwc = ["Fisrt Half (Qty):Float:120", "Fisrt Half (Amt):Currency:120", "Second Half (Qty):Float:120", |
| 172 | "Second Half (Amt):Currency:120"] |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 173 | |
| 174 | first_half_start = year_start_date |
| 175 | first_half_end = add_days(add_months(first_half_start,6),-1) |
| 176 | second_half_start = add_days(first_half_end,1) |
| 177 | second_half_end = add_days(add_months(second_half_start,6),-1) |
| 178 | |
| 179 | query_details = """ |
Saurabh | 5b6d03e | 2013-06-19 12:34:22 +0530 | [diff] [blame] | 180 | SUM(IF(t1.%(trans)s BETWEEN '%(fhs)s' AND '%(fhe)s', t2.qty, NULL)), |
| 181 | SUM(IF(t1.%(trans)s BETWEEN '%(fhs)s' AND '%(fhe)s', t1.grand_total, NULL)), |
| 182 | SUM(IF(t1.%(trans)s BETWEEN '%(shs)s' AND '%(she)s', t2.qty, NULL)), |
| 183 | SUM(IF(t1.%(trans)s BETWEEN '%(shs)s' AND '%(she)s', t1.grand_total, NULL)), |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 184 | """%{"trans": trans_date, "fhs": first_half_start, "fhe": first_half_end,"shs": second_half_start, |
| 185 | "she": second_half_end} |
| 186 | |
| 187 | else: |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 188 | pwc = [filters.get("fiscal_year")+" (Qty):Float:120", filters.get("fiscal_year")+" (Amt):Currency:120"] |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 189 | query_details = " SUM(t2.qty), SUM(t1.grand_total)," |
| 190 | |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 191 | query_details += 'SUM(t2.qty), SUM(t1.grand_total)' |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 192 | return pwc, query_details |
| 193 | |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 194 | def basedon_wise_colums_query(based_on, trans): |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 195 | sup_tab = '' |
| 196 | |
| 197 | if based_on == "Item": |
| 198 | bon = ["Item:Link/Item:120", "Item Name:Data:120"] |
| 199 | query_details = "t2.item_code, t2.item_name," |
Saurabh | bfe9fe7 | 2013-06-19 12:41:28 +0530 | [diff] [blame^] | 200 | based = 't2.item_code' |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 201 | |
| 202 | elif based_on == "Item Group": |
| 203 | bon = ["Item Group:Link/Item Group:120"] |
| 204 | query_details = "t2.item_group," |
Saurabh | bfe9fe7 | 2013-06-19 12:41:28 +0530 | [diff] [blame^] | 205 | based = 't2.item_group' |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 206 | |
| 207 | elif based_on == "Customer": |
| 208 | bon = ["Customer:Link/Customer:120", "Territory:Link/Territory:120"] |
| 209 | query_details = "t1.customer_name, t1.territory, " |
Saurabh | bfe9fe7 | 2013-06-19 12:41:28 +0530 | [diff] [blame^] | 210 | based = 't1.customer_name' |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 211 | |
| 212 | elif based_on == "Customer Group": |
| 213 | bon = ["Customer Group:Link/Customer Group"] |
| 214 | query_details = "t1.customer_group," |
Saurabh | bfe9fe7 | 2013-06-19 12:41:28 +0530 | [diff] [blame^] | 215 | based = 't1.customer_group' |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 216 | |
| 217 | elif based_on == 'Supplier': |
| 218 | bon = ["Supplier:Link/Supplier:120", "Supplier Type:Link/Supplier Type:120"] |
| 219 | query_details = "t1.supplier, t3.supplier_type," |
Saurabh | bfe9fe7 | 2013-06-19 12:41:28 +0530 | [diff] [blame^] | 220 | based = 't1.supplier' |
Saurabh | 24208f5 | 2013-06-18 15:45:08 +0530 | [diff] [blame] | 221 | sup_tab = '`tabSupplier` t3', |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 222 | |
| 223 | elif based_on == 'Supplier Type': |
| 224 | bon = ["Supplier Type:Link/Supplier Type:120"] |
| 225 | query_details = "t3.supplier_type," |
Saurabh | bfe9fe7 | 2013-06-19 12:41:28 +0530 | [diff] [blame^] | 226 | based = 't3.supplier_type' |
Saurabh | 24208f5 | 2013-06-18 15:45:08 +0530 | [diff] [blame] | 227 | sup_tab ='`tabSupplier` t3', |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 228 | |
| 229 | elif based_on == "Territory": |
| 230 | bon = ["Territory:Link/Territory:120"] |
| 231 | query_details = "t1.territory," |
Saurabh | bfe9fe7 | 2013-06-19 12:41:28 +0530 | [diff] [blame^] | 232 | based = 't1.territory' |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 233 | |
| 234 | elif based_on == "Project": |
| 235 | |
| 236 | if trans in ['Sales Invoice', 'Delivery Note', 'Sales Order']: |
| 237 | bon = ["Project:Link/Project:120"] |
| 238 | query_details = "t1.project_name," |
Saurabh | bfe9fe7 | 2013-06-19 12:41:28 +0530 | [diff] [blame^] | 239 | based = 't1.project_name' |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 240 | |
| 241 | elif trans in ['Purchase Order', 'Purchase Invoice', 'Purchase Receipt']: |
| 242 | bon = ["Project:Link/Project:120"] |
| 243 | query_details = "t2.project_name," |
Saurabh | bfe9fe7 | 2013-06-19 12:41:28 +0530 | [diff] [blame^] | 244 | based = 't2.project_name' |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 245 | |
| 246 | else: |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 247 | webnotes.msgprint("Information Not Available", raise_exception=1) |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 248 | |
Saurabh | bfe9fe7 | 2013-06-19 12:41:28 +0530 | [diff] [blame^] | 249 | return bon, query_details, based, sup_tab |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 250 | |
Saurabh | 1848b71 | 2013-06-14 15:03:45 +0530 | [diff] [blame] | 251 | def group_wise_column(group_by): |
Saurabh | 0326f54 | 2013-06-13 19:17:56 +0530 | [diff] [blame] | 252 | if group_by: |
| 253 | return [group_by+":Link/"+group_by+":120"] |
| 254 | else: |
| 255 | return [] |