Example #1
0
def get_dates_from_timegrain(from_date, to_date, timegrain="Daily"):
    from_date = getdate(from_date)
    to_date = getdate(to_date)

    days = months = years = 0
    if "Daily" == timegrain:
        days = 1
    elif "Weekly" == timegrain:
        days = 7
    elif "Monthly" == timegrain:
        months = 1
    elif "Quarterly" == timegrain:
        months = 3

    if "Weekly" == timegrain:
        dates = [get_last_day_of_week(from_date)]
    else:
        dates = [get_period_ending(from_date, timegrain)]

    while getdate(dates[-1]) < getdate(to_date):
        if "Weekly" == timegrain:
            date = get_last_day_of_week(
                add_to_date(dates[-1], years=years, months=months, days=days))
        else:
            date = get_period_ending(
                add_to_date(dates[-1], years=years, months=months, days=days),
                timegrain)
        dates.append(date)
    return dates
Example #2
0
    def test_period_ending(self):
        self.assertEqual(get_period_ending('2019-04-10', 'Daily'),
                         getdate('2019-04-10'))

        # fun fact: week ends on the day before 1st Jan of the year.
        # for 2019 it is Monday
        self.assertEqual(get_period_ending('2019-04-10', 'Weekly'),
                         getdate('2019-04-15'))

        self.assertEqual(get_period_ending('2019-04-10', 'Monthly'),
                         getdate('2019-04-30'))
        self.assertEqual(get_period_ending('2019-04-30', 'Monthly'),
                         getdate('2019-04-30'))
        self.assertEqual(get_period_ending('2019-03-31', 'Monthly'),
                         getdate('2019-03-31'))

        self.assertEqual(get_period_ending('2019-04-10', 'Quarterly'),
                         getdate('2019-06-30'))
        self.assertEqual(get_period_ending('2019-06-30', 'Quarterly'),
                         getdate('2019-06-30'))
        self.assertEqual(get_period_ending('2019-10-01', 'Quarterly'),
                         getdate('2019-12-31'))

        self.assertEqual(get_period_ending('2019-10-01', 'Yearly'),
                         getdate('2019-12-31'))
def get(chart_name=None, from_date = None, to_date = None):
	chart = frappe.get_doc('Dashboard Chart', chart_name)
	timespan = chart.timespan
	timegrain = chart.time_interval
	filters = json.loads(chart.filters_json)

	account = filters.get("account")
	company = filters.get("company")

	if not to_date:
		to_date = nowdate()
	if not from_date:
		if timegrain in ('Monthly', 'Quarterly'):
			from_date = get_from_date_from_timespan(to_date, timespan)

	# fetch dates to plot
	dates = get_dates_from_timegrain(from_date, to_date, timegrain)

	# get all the entries for this account and its descendants
	gl_entries = get_gl_entries(account, get_period_ending(to_date, timegrain))

	# compile balance values
	result = build_result(account, dates, gl_entries)

	return {
		"labels": [formatdate(r[0].strftime('%Y-%m-%d')) for r in result],
		"datasets": [{
			"name": account,
			"values": [r[1] for r in result]
		}]
	}
def get(chart_name=None,
        chart=None,
        no_cache=None,
        filters=None,
        from_date=None,
        to_date=None,
        timespan=None,
        time_interval=None):
    if chart_name:
        chart = frappe.get_doc('Dashboard Chart', chart_name)
    else:
        chart = frappe._dict(frappe.parse_json(chart))
    timespan = chart.timespan

    if chart.timespan == 'Select Date Range':
        from_date = chart.from_date
        to_date = chart.to_date

    timegrain = chart.time_interval
    filters = frappe.parse_json(filters) or frappe.parse_json(
        chart.filters_json)

    account = filters.get("account")
    company = filters.get("company")

    if not account and chart_name:
        frappe.throw(
            _("Account is not set for the dashboard chart {0}").format(
                get_link_to_form("Dashboard Chart", chart_name)))

    if not frappe.db.exists("Account", account) and chart_name:
        frappe.throw(
            _("Account {0} does not exists in the dashboard chart {1}").format(
                account, get_link_to_form("Dashboard Chart", chart_name)))

    if not to_date:
        to_date = nowdate()
    if not from_date:
        if timegrain in ('Monthly', 'Quarterly'):
            from_date = get_from_date_from_timespan(to_date, timespan)

    # fetch dates to plot
    dates = get_dates_from_timegrain(from_date, to_date, timegrain)

    # get all the entries for this account and its descendants
    gl_entries = get_gl_entries(account, get_period_ending(to_date, timegrain))

    # compile balance values
    result = build_result(account, dates, gl_entries)

    return {
        "labels": [formatdate(r[0].strftime('%Y-%m-%d')) for r in result],
        "datasets": [{
            "name": account,
            "values": [r[1] for r in result]
        }]
    }
Example #5
0
	def test_period_ending(self):
		self.assertEqual(get_period_ending('2019-04-10', 'Daily'),
			getdate('2019-04-10'))

		# week starts on monday
		self.assertEqual(get_period_ending('2019-04-10', 'Weekly'),
			getdate('2019-04-14'))

		self.assertEqual(get_period_ending('2019-04-10', 'Monthly'),
			getdate('2019-04-30'))
		self.assertEqual(get_period_ending('2019-04-30', 'Monthly'),
			getdate('2019-04-30'))
		self.assertEqual(get_period_ending('2019-03-31', 'Monthly'),
			getdate('2019-03-31'))

		self.assertEqual(get_period_ending('2019-04-10', 'Quarterly'),
			getdate('2019-06-30'))
		self.assertEqual(get_period_ending('2019-06-30', 'Quarterly'),
			getdate('2019-06-30'))
		self.assertEqual(get_period_ending('2019-10-01', 'Quarterly'),
			getdate('2019-12-31'))