statistics_service.dart 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import '../database/daos/statistiche_dao.dart';
  2. import '../database/daos/articoli_dao.dart';
  3. import '../database/daos/clienti_dao.dart';
  4. import '../database/daos/agenti_dao.dart';
  5. import '../database/database.dart';
  6. class StatisticsService {
  7. final StatisticheDao _statisticheDao;
  8. final ArticoliDao _articoliDao;
  9. final ClientiDao _clientiDao;
  10. final AgentiDao _agentiDao;
  11. final cacheClienti = <int, Cliente>{};
  12. StatisticsService(this._statisticheDao, this._articoliDao, this._clientiDao, this._agentiDao);
  13. Future<List<StatisticheVenditaData>> getStatsForAgent(int idAgente, {String? da, String? a}) async {
  14. final from = da ?? DateTime.now().subtract(const Duration(days: 365)).toIso8601String();
  15. final to = a ?? DateTime.now().toIso8601String();
  16. return _statisticheDao.getPerAgente(idAgente, from, to);
  17. }
  18. Future<double> getTotalForAgent(int idAgente, {String? da, String? a}) async {
  19. final stats = await getStatsForAgent(idAgente, da: da, a: a);
  20. return stats.fold<double>(0.0, (sum, s) => sum + (s.importo ?? 0.0));
  21. }
  22. Future<Map<String, double>> getTotalsByClient(int idAgente, {String? da, String? a}) async {
  23. final stats = await getStatsForAgent(idAgente, da: da, a: a);
  24. final result = <String, double>{};
  25. for (final s in stats) {
  26. final cliente = await getCliente(s.idCliente!);
  27. final key = cliente != null ? '${cliente.codCliente} ${cliente.ragioneSociale}' : s.idCliente.toString();
  28. result[key] = (result[key] ?? 0) + (s.importo ?? 0);
  29. }
  30. return result;
  31. }
  32. Future<Map<String, double>> getTotalsByArticle(int idAgente, {String? da, String? a}) async {
  33. final stats = await getStatsForAgent(idAgente, da: da, a: a);
  34. final result = <String, double>{};
  35. for (final s in stats) {
  36. final key = s.idArticolo.toString();
  37. result[key] = (result[key] ?? 0) + (s.importo ?? 0);
  38. }
  39. return result;
  40. }
  41. Future<Map<String, double>> getMonthlyTotals(int idAgente, {int months = 12}) async {
  42. final to = DateTime.now();
  43. final from = DateTime(to.year, to.month - months, to.day);
  44. final stats = await _statisticheDao.getPerAgente(
  45. idAgente,
  46. from.toIso8601String(),
  47. to.toIso8601String(),
  48. );
  49. final result = <String, double>{};
  50. for (final s in stats) {
  51. if (s.dataFattura == null || s.dataFattura!.length < 7) continue;
  52. final monthKey = s.dataFattura!.substring(0, 7);
  53. result[monthKey] = (result[monthKey] ?? 0) + (s.importo ?? 0);
  54. }
  55. return result;
  56. }
  57. Future<Cliente?> getCliente(int idCliente) async {
  58. if (cacheClienti.containsKey(idCliente)) {
  59. return Future.value(cacheClienti[idCliente]);
  60. } else {
  61. final cliente = await _clientiDao.getById(idCliente);
  62. if (cliente != null) {
  63. cacheClienti[idCliente] = cliente;
  64. return cliente;
  65. }
  66. }
  67. return Future.value(null);
  68. }
  69. }