import 'package:intl/intl.dart'; class Revenue { Map mapData = {}; Map mapPOCount = {}; Map mapDOCount = {}; Map mapDelivery = {}; Map mapDoDelivery = {}; List getData1(int index) { var endRange = 0; if (index == 0) { // 7 days endRange = 6 > mapData.length ? mapData.length : 6; } else if (index == 1) { // one months endRange = 30 > mapData.length ? mapData.length : 30; } else { // three months endRange = 90 > mapData.length ? mapData.length : 90; } List result = new List(); if (mapData != null) { mapData.forEach((k, value) { result.add(value.toInt()); }); } return result.getRange(0, endRange).map((v) => v.toDouble()).toList(); } List getData() { List result = new List(); if (mapData != null) { DateTime today = new DateTime.now(); DateTime thirtyDaysAgo = today.subtract(new Duration(days: 30)); String _formatter = new DateFormat("yyyyMMdd").format(thirtyDaysAgo); int _lastThirtyDays = int.tryParse(_formatter); mapData.forEach((k, value) { int mapDay = int.tryParse(k); if (mapDay < _lastThirtyDays) return; var parsedDate = DateTime.parse(k); var data = Data(date: parsedDate, amount: value); result.add(data); }); result.sort((a, b) { return a.date.compareTo(b.date); }); } return result; } List getPOCounts() { List result = new List(); if (mapPOCount != null) { mapPOCount.forEach((k, value) { var parsedDate = DateTime.parse(k); var data = Data(date: parsedDate, count: value); result.add(data); }); result.sort((a, b) { return a.date.compareTo(b.date); }); } return result; } List getDOCounts() { List result = new List(); if (mapDOCount != null) { mapDOCount.forEach((k, value) { var parsedDate = DateTime.parse(k); var data = Data(date: parsedDate, count: value); result.add(data); }); result.sort((a, b) { return a.date.compareTo(b.date); }); } return result; } List getDelivery() { List result = new List(); if (mapDelivery != null) { mapDelivery.forEach((k, value) { var parsedDate = DateTime.parse(k); var data = Data(date: parsedDate, amount: value); result.add(data); }); result.sort((a, b) { return a.date.compareTo(b.date); }); } return result; } List getDeliverySummary() { List _list = new List(); List result = new List(); if (mapDelivery != null) { mapDelivery.forEach((k, value) { var parsedDate = DateTime.parse(k); var data = Data(date: parsedDate, amount: value); _list.add(data); }); _list.sort((a, b) { return a.date.compareTo(b.date); }); int _count = 1; int _index = 0; int _totalDay = 0; int _totalAmount = 0; int lastIndex = _list.length - 1; _list.asMap().forEach((index, value) { _totalAmount += value.amount; if (_count == 10 || index == lastIndex) { _totalDay = (_index + 1) * 10; if (index == lastIndex && lastIndex > 0) { _totalDay = lastIndex + 1; _totalAmount += result.length > 0 ? result[result.length - 1].totalAmount : 0; } if (lastIndex == 0) { _totalDay = 1; } if (_index != 0 && index != lastIndex) { _totalAmount += result[_index - 1].totalAmount; } result.insert( _index, Data(totalDay: _totalDay, totalAmount: _totalAmount)); _totalAmount = 0; _count = 0; _index++; } _count++; }); } return result; } List getDeliveryDo() { List result = new List(); if (mapDoDelivery != null) { mapDoDelivery.forEach((k, value) { var parsedDate = DateTime.parse(k); var data = Data(date: parsedDate, count: value); result.add(data); }); result.sort((a, b) { return a.date.compareTo(b.date); }); } return result; } List getDeliveryDoSummary() { List _list = new List(); List result = new List(); if (mapDoDelivery != null) { mapDoDelivery.forEach((k, value) { var parsedDate = DateTime.parse(k); var data = Data(date: parsedDate, count: value); _list.add(data); }); _list.sort((a, b) { return a.date.compareTo(b.date); }); int _count = 1; int _index = 0; int _totalDay = 0; int _totalCount = 0; int lastIndex = _list.length - 1; _list.asMap().forEach((index, value) { _totalCount += value.count; if (_count == 10 || index == lastIndex) { _totalDay = (_index + 1) * 10; if (index == lastIndex && lastIndex > 0) { _totalDay = lastIndex + 1; _totalCount += result.length > 0 ? result[result.length - 1].totalCount : 0; } if (lastIndex == 0) { _totalDay = 1; } if (_index != 0 && index != lastIndex) { _totalCount += result[_index - 1].totalCount; } result.insert( _index, Data(totalDay: _totalDay, totalCount: _totalCount)); _totalCount = 0; _count = 0; _index++; } _count++; }); } return result; } int getTotal(int index) { var endRange = 0; if (index == 0) { // 7 days endRange = 6 > mapData.length ? mapData.length : 6; } else if (index == 1) { // one months endRange = 30 > mapData.length ? mapData.length : 30; } else { // three months endRange = 90 > mapData.length ? mapData.length : 90; } List result = new List(); if (mapData != null) { DateTime today = new DateTime.now(); DateTime thirtyDaysAgo = today.subtract(new Duration(days: 30)); String _formatter = new DateFormat("yyyyMMdd").format(thirtyDaysAgo); int _lastThirtyDays = int.tryParse(_formatter); mapData.forEach((k, value) { int mapDay = int.tryParse(k); if (mapDay < _lastThirtyDays) return; result.add(value.toInt()); }); } return result.fold(0, (prev, element) => prev + element); } Revenue( {this.mapData, this.mapPOCount, this.mapDOCount, this.mapDelivery, this.mapDoDelivery}); factory Revenue.fromMap(Map map, String docID) { return Revenue( mapData: map['data'], mapPOCount: map['po_count'], mapDOCount: map['do_count'], mapDelivery: map['delivery'], mapDoDelivery: map['do_delivery']); } @override String toString() { return null; } } class Data { DateTime date; int totalDay; int totalCount; int totalAmount; int amount; int count; Data( {this.date, this.amount, this.count, this.totalDay, this.totalCount, this.totalAmount}); }