|
| 1 | +/********************************************************************** |
| 2 | + * |
| 3 | + * Copyright (c) 2024 Olaf Willuhn |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * This software is copyrighted work licensed under the terms of the |
| 7 | + * Jameica License. Please consult the file "LICENSE" for details. |
| 8 | + * |
| 9 | + **********************************************************************/ |
| 10 | + |
| 11 | +package de.willuhn.jameica.hbci.forecast; |
| 12 | + |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.Calendar; |
| 15 | +import java.util.Date; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +import de.willuhn.datasource.rmi.DBIterator; |
| 21 | +import de.willuhn.jameica.hbci.HBCI; |
| 22 | +import de.willuhn.jameica.hbci.Settings; |
| 23 | +import de.willuhn.jameica.hbci.rmi.HBCIDBService; |
| 24 | +import de.willuhn.jameica.hbci.rmi.Konto; |
| 25 | +import de.willuhn.jameica.hbci.rmi.Umsatz; |
| 26 | +import de.willuhn.jameica.hbci.server.UmsatzUtil; |
| 27 | +import de.willuhn.jameica.hbci.server.Value; |
| 28 | +import de.willuhn.jameica.system.Application; |
| 29 | +import de.willuhn.jameica.util.DateUtil; |
| 30 | +import de.willuhn.util.I18N; |
| 31 | + |
| 32 | +/** |
| 33 | + * Forecast-Provider für eine Saldo-Prognose anhand der Umsätze der letzten 3 Monate. |
| 34 | + */ |
| 35 | +public class ForecastProviderUmsatz implements ForecastProvider |
| 36 | +{ |
| 37 | + private final static I18N i18n = Application.getPluginLoader().getPlugin(HBCI.class).getResources().getI18N(); |
| 38 | + private final static de.willuhn.jameica.system.Settings settings = new de.willuhn.jameica.system.Settings(ForecastProvider.class); |
| 39 | + |
| 40 | + /** |
| 41 | + * @see de.willuhn.jameica.hbci.forecast.ForecastProvider#getName() |
| 42 | + */ |
| 43 | + @Override |
| 44 | + public String getName() |
| 45 | + { |
| 46 | + return i18n.tr("Umsätze"); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @see de.willuhn.jameica.hbci.forecast.ForecastProvider#getData(de.willuhn.jameica.hbci.rmi.Konto, java.util.Date) |
| 51 | + */ |
| 52 | + @Override |
| 53 | + public List<Value> getData(Konto k, Date to) throws Exception |
| 54 | + { |
| 55 | + if (!settings.getBoolean("forecast.umsatz",false)) |
| 56 | + return null; |
| 57 | + |
| 58 | + // Wir holen uns erstmal eine Liste der Umsätze aus den letzten 6 Monaten |
| 59 | + final Date end = DateUtil.endOfDay(new Date()); |
| 60 | + final Calendar cal = Calendar.getInstance(); |
| 61 | + cal.add(Calendar.MONTH,-6); |
| 62 | + final Date start = DateUtil.startOfDay(cal.getTime()); |
| 63 | + |
| 64 | + final Map<String,SumEntry> map = new HashMap<>(); |
| 65 | + |
| 66 | + final DBIterator<Umsatz> it = UmsatzUtil.find(k,null,start,end,null); |
| 67 | + HBCIDBService service = (HBCIDBService) Settings.getDBService(); |
| 68 | + it.setOrder("ORDER BY " + service.getSQLTimestamp("datum") + ", id"); |
| 69 | + |
| 70 | + while (it.hasNext()) |
| 71 | + { |
| 72 | + final Umsatz u = it.next(); |
| 73 | + final String iban = u.getGegenkontoNummer(); |
| 74 | + final double d = u.getBetrag(); |
| 75 | + final Date date = u.getDatum(); |
| 76 | + |
| 77 | + // Zahlungen unter 10,- EUR ignorieren wir aus Performance-Gründen |
| 78 | + if (Double.isNaN(d) || date == null || Math.abs(d) <= 10d) |
| 79 | + continue; |
| 80 | + |
| 81 | + final SumEntry entry = map.computeIfAbsent(iban,i -> new SumEntry()); |
| 82 | + cal.setTime(date); |
| 83 | + entry.add(cal.get(Calendar.DATE),d); |
| 84 | + } |
| 85 | + |
| 86 | + // OK, jetzt haben wir eine Map mit Zahlungen pro Gegenkonto. |
| 87 | + // Daraus können wir eine Prognose ableiten |
| 88 | + final List<Value> result = new ArrayList<Value>(); |
| 89 | + |
| 90 | + // Wir iterieren tagesweise von heute bis um Ende-Zeitraum und |
| 91 | + // schauen, ob wir Zahlungen an diesen Tagen haben |
| 92 | + int limit = 0; |
| 93 | + cal.setTime(DateUtil.startOfDay(new Date())); |
| 94 | + while (!cal.getTime().after(to)) |
| 95 | + { |
| 96 | + // Maximal ~3 Jahre |
| 97 | + if (limit++ >= 1000) |
| 98 | + break; |
| 99 | + |
| 100 | + int day = cal.get(Calendar.DATE); |
| 101 | + for (SumEntry e:map.values()) |
| 102 | + { |
| 103 | + // Zahlungen, die in dem ganzen Zeitraum nur einmal vorgekommen sind, |
| 104 | + // ignorieren wir. Hier kann nicht mit einer Wiederholung gerechnet werden |
| 105 | + if (e.count < 2) |
| 106 | + continue; |
| 107 | + |
| 108 | + if (e.getDay() == day) |
| 109 | + { |
| 110 | + // An dem Tag findet typischerweise eine Zahlung statt |
| 111 | + final Value v = new Value(cal.getTime(),e.getValue()); |
| 112 | + result.add(v); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + cal.add(Calendar.DATE,1); |
| 117 | + } |
| 118 | + |
| 119 | + return result; |
| 120 | + } |
| 121 | + |
| 122 | + private class SumEntry |
| 123 | + { |
| 124 | + private int count = 0; |
| 125 | + private int days = 0; |
| 126 | + private double sum = 0.0d; |
| 127 | + |
| 128 | + /** |
| 129 | + * Liefert den mittleren Tag der Zahlungen. |
| 130 | + * @return der mittlere Tag der Zahlungen. |
| 131 | + */ |
| 132 | + private int getDay() |
| 133 | + { |
| 134 | + return this.days / this.count; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Liefert den Durchschnittsbetrag. |
| 139 | + * @return der Durchschnittsbetrag. |
| 140 | + */ |
| 141 | + private double getValue() |
| 142 | + { |
| 143 | + return this.sum / this.count; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Fügt den Wert an einem Tag hinzu. |
| 148 | + * @param day der Tag. |
| 149 | + * @param value der Wert. |
| 150 | + */ |
| 151 | + private void add(int day, double value) |
| 152 | + { |
| 153 | + this.count++; |
| 154 | + this.days += day; |
| 155 | + this.sum += value; |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | + |
0 commit comments