blob: 26a53dad46371eef600b1a0dca356d4e4f461c4a [file] [log] [blame]
Aditya Hase953229f2019-01-08 23:06:23 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2# License: GNU General Public License v3. See license.txt
3
4from __future__ import unicode_literals
5from itertools import groupby
6from operator import itemgetter
7import frappe
Aditya Hase88963742019-01-09 11:18:17 +05308from frappe.utils import add_to_date
Aditya Hase953229f2019-01-08 23:06:23 +05309from erpnext.accounts.report.general_ledger.general_ledger import execute
10
11
Aditya Hase88963742019-01-09 11:18:17 +053012def get(filters=None):
Aditya Hase953229f2019-01-08 23:06:23 +053013 filters = frappe._dict({
14 "company": "Gadget Technologies Pvt. Ltd.",
Aditya Hase88963742019-01-09 11:18:17 +053015 "from_date": get_from_date_from_timespan(filters.get("timespan")),
Aditya Hase953229f2019-01-08 23:06:23 +053016 "to_date": "2020-12-12",
17 "account": "Cash - GTPL",
18 "group_by": "Group by Voucher (Consolidated)"
19 })
20 report_columns, report_results = execute(filters=filters)
21
22 interesting_fields = ["posting_date", "balance"]
23
24 columns = [column for column in report_columns if column["fieldname"] in interesting_fields]
25
26 _results = []
27 for row in report_results[1:-2]:
28 _results.append([row[key] for key in interesting_fields])
29
30 grouped_results = groupby(_results, key=itemgetter(0))
31
32 results = [list(values)[-1] for key, values in grouped_results]
33
34 return {
35 "labels": [result[0] for result in results],
36 "datasets": [{
37 "name": "Cash - GTPL",
38 "values": [result[1] for result in results]
39 }]
40 }
Aditya Hase88963742019-01-09 11:18:17 +053041
42def get_from_date_from_timespan(timespan):
43 days = months = years = 0
44 if "Last Week" == timespan:
45 days = -7
46 if "Last Month" == timespan:
47 months = -1
48 elif "Last Quarter" == timespan:
49 months = -3
50 elif "Last Year" == timespan:
51 years = -1
52 return add_to_date(None, years=years, months=months, days=days,
53 as_string=True, as_datetime=True)