def get(
    chart_name=None,
    chart=None,
    no_cache=None,
    filters=None,
    from_date=None,
    to_date=None,
    timespan=None,
    time_interval=None,
    heatmap_year=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]
        }],
    }
Beispiel #2
0
def get_dates_from_timegrain(from_date, to_date, timegrain):
    days = months = years = 0
    if "Daily" == timegrain:
        days = 1
    elif "Weekly" == timegrain:
        days = 7
    elif "Monthly" == timegrain:
        months = 1
    elif "Quarterly" == timegrain:
        months = 3

    dates = [get_period_ending(from_date, timegrain)]
    while getdate(dates[-1]) < getdate(to_date):
        date = get_period_ending(
            add_to_date(dates[-1], years=years, months=months, days=days),
            timegrain)
        dates.append(date)
    return dates
Beispiel #3
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'))
Beispiel #4
0
    def test_period_ending(self):
        self.assertEqual(get_period_ending("2019-04-10", "Daily"),
                         getdate("2019-04-10"))

        # week starts on monday
        with patch.object(frappe.utils.data,
                          "get_first_day_of_the_week",
                          return_value="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"))