diff --git a/frontend/mgramseva/lib/providers/bill_generation_details_provider.dart b/frontend/mgramseva/lib/providers/bill_generation_details_provider.dart index a53abcbea..0d0cad63f 100644 --- a/frontend/mgramseva/lib/providers/bill_generation_details_provider.dart +++ b/frontend/mgramseva/lib/providers/bill_generation_details_provider.dart @@ -466,38 +466,20 @@ class BillGenerationProvider with ChangeNotifier { List> getBillingCycle() { dates = []; if (billGenerateDetails.billYear != null && selectedBillYear != null) { - late DatePeriod ytd; - if(DateTime.now().month >= 4) { - ytd = DatePeriod(DateTime(DateTime.now().year, 4) , DateTime(DateTime.now().year + 1, 4, 0, 23,59, 59, 999), DateType.YTD); - }else{ - ytd = DatePeriod(DateTime( DateTime.now().year - 1, 4), DateTime.now(), DateType.YTD); - } - - var date1 = DateFormats.getFormattedDateToDateTime( + DatePeriod ytd; + var fromDate = DateFormats.getFormattedDateToDateTime( DateFormats.timeStampToDate(selectedBillYear.fromDate)) as DateTime; - var isCurrentYtdSelected = date1.year == ytd.startDate.year; + var toDate = DateFormats.getFormattedDateToDateTime( + DateFormats.timeStampToDate(selectedBillYear.toDate)) as DateTime; - /// Get months based on selected billing year - var months = CommonMethods.getPastMonthUntilFinancialYear(date1.year); + ytd = DatePeriod(fromDate,toDate,DateType.YTD); - /// if its current ytd year means removing till current month - if (isCurrentYtdSelected) { - switch (DateTime.now().month) { - case 1: - months.removeRange(0, 3); - break; - case 2: - months.removeRange(0, 2); - break; - case 3: - months.removeRange(0, 1); - break; - } - } + /// Get months based on selected billing year + var months = CommonMethods.getPastMonthUntilFinancialYTD(ytd); /// if selected year is future year means all the months will be removed - if(date1.year >= ytd.endDate.year) months.clear(); + if(fromDate.year > ytd.endDate.year) months.clear(); for (var i = 0; i < months.length; i++) { var prevMonth = months[i].startDate; diff --git a/frontend/mgramseva/lib/providers/consumer_details_provider.dart b/frontend/mgramseva/lib/providers/consumer_details_provider.dart index 4d15130aa..279929a1c 100644 --- a/frontend/mgramseva/lib/providers/consumer_details_provider.dart +++ b/frontend/mgramseva/lib/providers/consumer_details_provider.dart @@ -608,38 +608,20 @@ class ConsumerProvider with ChangeNotifier { List> getBillingCycle() { dates = []; if (billYear != null) { - late DatePeriod ytd; - if (DateTime.now().month >= 4) { - ytd = DatePeriod( - DateTime(DateTime.now().year, 4), - DateTime(DateTime.now().year + 1, 4, 0, 23, 59, 59, 999), - DateType.YTD); - } else { - ytd = DatePeriod( - DateTime(DateTime.now().year - 1, 4), DateTime.now(), DateType.YTD); - } - - var date1 = DateFormats.getFormattedDateToDateTime( + DatePeriod ytd; + var fromDate = DateFormats.getFormattedDateToDateTime( DateFormats.timeStampToDate(billYear?.fromDate)) as DateTime; - var isCurrentYtdSelected = date1.year == ytd.startDate.year; + + var toDate = DateFormats.getFormattedDateToDateTime( + DateFormats.timeStampToDate(billYear?.toDate)) as DateTime; + + ytd = DatePeriod(fromDate,toDate,DateType.YTD); /// Get months based on selected billing year - var months = CommonMethods.getPastMonthUntilFinancialYear(date1.year); - - /// if its current ytd year means removing till current month - if (isCurrentYtdSelected) { - switch (DateTime.now().month) { - case 1: - months.removeRange(0, 3); - break; - case 2: - months.removeRange(0, 2); - break; - case 3: - months.removeRange(0, 1); - break; - } - } + var months = CommonMethods.getPastMonthUntilFinancialYTD(ytd); + + /// if selected year is future year means all the months will be removed + if(fromDate.year > ytd.endDate.year) months.clear(); for (var i = 0; i < months.length; i++) { var prevMonth = months[i].startDate; diff --git a/frontend/mgramseva/lib/utils/common_methods.dart b/frontend/mgramseva/lib/utils/common_methods.dart index 916d6afe9..9325ad8d4 100644 --- a/frontend/mgramseva/lib/utils/common_methods.dart +++ b/frontend/mgramseva/lib/utils/common_methods.dart @@ -51,7 +51,57 @@ class CommonMethods { .reversed .toList(); } +/* + * @author Rahul Dev Garg + * rahul.dev@egovernments.org + * + * */ + static List getPastMonthUntilFinancialYTD(DatePeriod ytd) { + var monthList = []; + final currentTime = DateTime.now(); + if(currentTime.year < ytd.startDate.year){ + return []; + } + if (currentTime.year == ytd.startDate.year) { + //when current year is same as start year of financial year + for (int i = ytd.startDate.month; i < currentTime.month; i++) { + monthList.add(DateTime(currentTime.year, i)); + } + } else if(currentTime.year == ytd.endDate.year){ + //when current year is same as end year of financial year + for (int i = ytd.startDate.month; i <= 12; i++) { + monthList.add(DateTime(ytd.startDate.year, i)); + } + for (int i = 1; + i <= (currentTime.month <= ytd.endDate.month ? currentTime.month-1: ytd.endDate.month); + /* + * if current month is less than or equal to end month of financial year + * we are using months less than current month and if it is more than + * end month of financial year we are using till end month of financial + * year + */ + i++) { + monthList.add(DateTime(ytd.endDate.year, i)); + } + }else{ + for (int i = ytd.startDate.month; i <= 12; i++) { + monthList.add(DateTime(ytd.startDate.year, i)); + } + for (int i = 1; + i <= ytd.endDate.month; + i++) { + monthList.add(DateTime(ytd.endDate.year , i)); + } + } + var list = monthList + .map((e) => DatePeriod(DateTime(e.year, e.month, 1), + DateTime(e.year, e.month + 1, 0, 23, 59, 59, 999), DateType.MONTH)) + .toList() + .reversed + .toList(); + return list; + } static List getFinancialYearList([int count = 5]) { var yearWithMonths = []; @@ -60,13 +110,12 @@ class CommonMethods { DateTime(DateTime.now().year, 4), DateTime(DateTime.now().year + 1, 4, 0, 23, 59, 59, 999), DateType.YTD); - var monthList = getPastMonthUntilFinancialYear(DateTime.now().year); + var monthList = getPastMonthUntilFinancialYTD(year); yearWithMonths.add(YearWithMonths(monthList, year)); } else { var year = DatePeriod( DateTime(DateTime.now().year - 1, 4), DateTime.now(), DateType.YTD); - var monthList = getPastMonthUntilFinancialYear(year.startDate.year, - dateType: DateType.YTD); + var monthList = getPastMonthUntilFinancialYTD(year); yearWithMonths.add(YearWithMonths(monthList, year)); } @@ -77,7 +126,7 @@ class CommonMethods { : DateTime(currentDate.year - i); year = DatePeriod(DateTime(year.year - 1, 4), DateTime(year.year, 4, 0, 23, 59, 59, 999), DateType.YEAR); - var monthList = getPastMonthUntilFinancialYear(year.startDate.year); + var monthList = getPastMonthUntilFinancialYTD(year); yearWithMonths.add(YearWithMonths(monthList, year)); } return yearWithMonths;