fix: stock analytics report date range issues and add company filter (#27014)
* test: tests for correct get_period_date_ranges * fix: stock analytics report date range issues - Upon selecting second half of month with Monthly filter, data from that period was missing. - Solution: "round down" the date as per expected frequency. * chore: drop py2 and fix misleading docstring * test: fix test to avoid FY clash * feat: add company filter in stock analytics report [skip ci] Co-authored-by: Marica <maricadsouza221197@gmail.com>
This commit is contained in:
parent
332ac105b5
commit
0dff0beaba
@ -36,12 +36,26 @@ frappe.query_reports["Stock Analytics"] = {
|
||||
options:"Brand",
|
||||
default: "",
|
||||
},
|
||||
{
|
||||
fieldname: "company",
|
||||
label: __("Company"),
|
||||
fieldtype: "Link",
|
||||
options: "Company",
|
||||
default: frappe.defaults.get_user_default("Company"),
|
||||
reqd: 1,
|
||||
},
|
||||
{
|
||||
fieldname: "warehouse",
|
||||
label: __("Warehouse"),
|
||||
fieldtype: "Link",
|
||||
options:"Warehouse",
|
||||
options: "Warehouse",
|
||||
default: "",
|
||||
get_query: function() {
|
||||
const company = frappe.query_report.get_filter_value('company');
|
||||
return {
|
||||
filters: { 'company': company }
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
fieldname: "from_date",
|
||||
|
@ -1,14 +1,15 @@
|
||||
# Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors
|
||||
# For license information, please see license.txt
|
||||
import datetime
|
||||
|
||||
from __future__ import unicode_literals
|
||||
import frappe
|
||||
from frappe import _, scrub
|
||||
from frappe.utils import getdate, flt
|
||||
from frappe.utils import getdate, get_quarter_start, get_first_day_of_week
|
||||
from frappe.utils import get_first_day as get_first_day_of_month
|
||||
|
||||
from erpnext.stock.report.stock_balance.stock_balance import (get_items, get_stock_ledger_entries, get_item_details)
|
||||
from erpnext.accounts.utils import get_fiscal_year
|
||||
from erpnext.stock.utils import is_reposting_item_valuation_in_progress
|
||||
from six import iteritems
|
||||
|
||||
def execute(filters=None):
|
||||
is_reposting_item_valuation_in_progress()
|
||||
@ -71,7 +72,8 @@ def get_columns(filters):
|
||||
|
||||
def get_period_date_ranges(filters):
|
||||
from dateutil.relativedelta import relativedelta
|
||||
from_date, to_date = getdate(filters.from_date), getdate(filters.to_date)
|
||||
from_date = round_down_to_nearest_frequency(filters.from_date, filters.range)
|
||||
to_date = getdate(filters.to_date)
|
||||
|
||||
increment = {
|
||||
"Monthly": 1,
|
||||
@ -97,6 +99,31 @@ def get_period_date_ranges(filters):
|
||||
|
||||
return periodic_daterange
|
||||
|
||||
|
||||
def round_down_to_nearest_frequency(date: str, frequency: str) -> datetime.datetime:
|
||||
"""Rounds down the date to nearest frequency unit.
|
||||
example:
|
||||
|
||||
>>> round_down_to_nearest_frequency("2021-02-21", "Monthly")
|
||||
datetime.datetime(2021, 2, 1)
|
||||
|
||||
>>> round_down_to_nearest_frequency("2021-08-21", "Yearly")
|
||||
datetime.datetime(2021, 1, 1)
|
||||
"""
|
||||
|
||||
def _get_first_day_of_fiscal_year(date):
|
||||
fiscal_year = get_fiscal_year(date)
|
||||
return fiscal_year and fiscal_year[1] or date
|
||||
|
||||
round_down_function = {
|
||||
"Monthly": get_first_day_of_month,
|
||||
"Quarterly": get_quarter_start,
|
||||
"Weekly": get_first_day_of_week,
|
||||
"Yearly": _get_first_day_of_fiscal_year,
|
||||
}.get(frequency, getdate)
|
||||
return round_down_function(date)
|
||||
|
||||
|
||||
def get_period(posting_date, filters):
|
||||
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
|
||||
|
||||
@ -177,7 +204,7 @@ def get_data(filters):
|
||||
periodic_data = get_periodic_data(sle, filters)
|
||||
ranges = get_period_date_ranges(filters)
|
||||
|
||||
for dummy, item_data in iteritems(item_details):
|
||||
for dummy, item_data in item_details.items():
|
||||
row = {
|
||||
"name": item_data.name,
|
||||
"item_name": item_data.item_name,
|
||||
|
35
erpnext/stock/report/stock_analytics/test_stock_analytics.py
Normal file
35
erpnext/stock/report/stock_analytics/test_stock_analytics.py
Normal file
@ -0,0 +1,35 @@
|
||||
import datetime
|
||||
import unittest
|
||||
|
||||
from frappe import _dict
|
||||
from erpnext.accounts.utils import get_fiscal_year
|
||||
|
||||
from erpnext.stock.report.stock_analytics.stock_analytics import get_period_date_ranges
|
||||
|
||||
|
||||
class TestStockAnalyticsReport(unittest.TestCase):
|
||||
def test_get_period_date_ranges(self):
|
||||
|
||||
filters = _dict(range="Monthly", from_date="2020-12-28", to_date="2021-02-06")
|
||||
|
||||
ranges = get_period_date_ranges(filters)
|
||||
|
||||
expected_ranges = [
|
||||
[datetime.date(2020, 12, 1), datetime.date(2020, 12, 31)],
|
||||
[datetime.date(2021, 1, 1), datetime.date(2021, 1, 31)],
|
||||
[datetime.date(2021, 2, 1), datetime.date(2021, 2, 6)],
|
||||
]
|
||||
|
||||
self.assertEqual(ranges, expected_ranges)
|
||||
|
||||
def test_get_period_date_ranges_yearly(self):
|
||||
|
||||
filters = _dict(range="Yearly", from_date="2021-01-28", to_date="2021-02-06")
|
||||
|
||||
ranges = get_period_date_ranges(filters)
|
||||
first_date = get_fiscal_year("2021-01-28")[1]
|
||||
expected_ranges = [
|
||||
[first_date, datetime.date(2021, 2, 6)],
|
||||
]
|
||||
|
||||
self.assertEqual(ranges, expected_ranges)
|
Loading…
x
Reference in New Issue
Block a user