implemented download of account data as CSV
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -78,7 +78,7 @@ public class AccountingModule extends BaseHandler implements AccountingService {
|
|||||||
case null -> getAccounts(user.get(),ex);
|
case null -> getAccounts(user.get(),ex);
|
||||||
default -> {
|
default -> {
|
||||||
try {
|
try {
|
||||||
yield getAccount(user.get(),Long.parseLong(head),ex);
|
yield getAccount(user.get(),Long.parseLong(head), path.pop(), ex);
|
||||||
} catch (NumberFormatException ignored) {}
|
} catch (NumberFormatException ignored) {}
|
||||||
yield super.doGet(path,ex);
|
yield super.doGet(path,ex);
|
||||||
}
|
}
|
||||||
@@ -184,9 +184,17 @@ public class AccountingModule extends BaseHandler implements AccountingService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean getAccount(UmbrellaUser user, long accountId, HttpExchange ex) throws IOException {
|
private boolean getAccount(UmbrellaUser user, long accountId, String format, HttpExchange ex) throws IOException {
|
||||||
if (!accountDb.getMembers(accountId).contains(user)) throw forbidden("You are not allowed to access account {id}",Field.ID,accountId);
|
if (!accountDb.getMembers(accountId).contains(user)) throw forbidden("You are not allowed to access account {id}",Field.ID,accountId);
|
||||||
return sendContent(ex, loadAccount(accountId));
|
var data = loadAccount(accountId);
|
||||||
|
return switch (format){
|
||||||
|
case "csv" -> {
|
||||||
|
ex.getResponseHeaders().add("Content-Type", "text/csv");
|
||||||
|
var users = userService().loader();
|
||||||
|
yield sendContent(ex,data.toCsv(users));
|
||||||
|
}
|
||||||
|
case null, default -> sendContent(ex,data);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean getAccounts(UmbrellaUser user, HttpExchange ex) throws IOException {
|
private boolean getAccounts(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||||
|
|||||||
@@ -2,18 +2,18 @@
|
|||||||
package de.srsoftware.umbrella.core.api;
|
package de.srsoftware.umbrella.core.api;
|
||||||
|
|
||||||
import static de.srsoftware.umbrella.core.Util.mapValues;
|
import static de.srsoftware.umbrella.core.Util.mapValues;
|
||||||
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||||
|
|
||||||
import de.srsoftware.tools.Mappable;
|
import de.srsoftware.tools.Mappable;
|
||||||
import de.srsoftware.umbrella.core.constants.Field;
|
import de.srsoftware.umbrella.core.constants.Field;
|
||||||
import de.srsoftware.umbrella.core.model.Account;
|
import de.srsoftware.umbrella.core.model.Account;
|
||||||
import de.srsoftware.umbrella.core.model.Transaction;
|
import de.srsoftware.umbrella.core.model.Transaction;
|
||||||
|
import de.srsoftware.umbrella.core.model.Translatable;
|
||||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public interface AccountingService {
|
public interface AccountingService {
|
||||||
public record AccountData(Account account, List<Transaction> transactions, HashMap<Long, UmbrellaUser> userMap) implements Mappable {
|
record AccountData(Account account, List<Transaction> transactions, HashMap<Long, UmbrellaUser> userMap) implements Mappable {
|
||||||
public static AccountData of(Account account, List<Transaction> transactions, HashMap<Long, UmbrellaUser> userMap) {
|
public static AccountData of(Account account, List<Transaction> transactions, HashMap<Long, UmbrellaUser> userMap) {
|
||||||
return new AccountData(account, transactions, userMap);
|
return new AccountData(account, transactions, userMap);
|
||||||
}
|
}
|
||||||
@@ -26,6 +26,16 @@ public interface AccountingService {
|
|||||||
Field.USER_LIST,mapValues(userMap)
|
Field.USER_LIST,mapValues(userMap)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] toCsv(Map<Long, UmbrellaUser> users) {
|
||||||
|
var keys = List.of(Field.DATE, Field.SOURCE, Field.AMOUNT, Field.DESTINATION, Field.PURPOSE,Field.TAGS);
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
keys.stream().map(Translatable::t).forEach(field -> sb.append(field).append(";"));
|
||||||
|
transactions.stream().sorted(Comparator.comparing(Transaction::id))
|
||||||
|
.map(transaction -> transaction.csvLine(users, keys,";"))
|
||||||
|
.forEach(line -> sb.append("\n").append(line));
|
||||||
|
return sb.toString().getBytes(UTF_8);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AccountData loadAccount(long accountId);
|
AccountData loadAccount(long accountId);
|
||||||
|
|||||||
@@ -9,9 +9,8 @@ import java.sql.SQLException;
|
|||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneOffset;
|
import java.time.ZoneOffset;
|
||||||
import java.util.HashSet;
|
import java.util.*;
|
||||||
import java.util.Map;
|
import java.util.stream.Collectors;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class Transaction extends UmbrellaObject {
|
public class Transaction extends UmbrellaObject {
|
||||||
private final long accountId;
|
private final long accountId;
|
||||||
@@ -25,11 +24,11 @@ public class Transaction extends UmbrellaObject {
|
|||||||
public Transaction(long id, long accountId, LocalDateTime date, IdOrString source, IdOrString destination, double amount, String purpose, Set<String> tags){
|
public Transaction(long id, long accountId, LocalDateTime date, IdOrString source, IdOrString destination, double amount, String purpose, Set<String> tags){
|
||||||
super(id);
|
super(id);
|
||||||
this.accountId = accountId;
|
this.accountId = accountId;
|
||||||
this.date = date;
|
|
||||||
this.source = source;
|
|
||||||
this.destination = destination;
|
|
||||||
this.amount = amount;
|
this.amount = amount;
|
||||||
|
this.date = date;
|
||||||
|
this.destination = destination;
|
||||||
this.purpose = purpose;
|
this.purpose = purpose;
|
||||||
|
this.source = source;
|
||||||
this.tags = tags == null ? new HashSet<>() : tags;
|
this.tags = tags == null ? new HashSet<>() : tags;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,6 +51,10 @@ public class Transaction extends UmbrellaObject {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String csvLine(Map<Long, UmbrellaUser> users, List<String> keys, String delimiter) {
|
||||||
|
return keys.stream().map(this::get).map(o -> resolveUsers(users,o)).map(o -> quote(o, delimiter)).collect(Collectors.joining(delimiter));
|
||||||
|
}
|
||||||
|
|
||||||
public LocalDateTime date(){
|
public LocalDateTime date(){
|
||||||
return date;
|
return date;
|
||||||
}
|
}
|
||||||
@@ -76,6 +79,19 @@ public class Transaction extends UmbrellaObject {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Object get(String property){
|
||||||
|
return switch (property){
|
||||||
|
case Field.AMOUNT -> amount;
|
||||||
|
case Field.DATE -> date.toLocalDate();
|
||||||
|
case Field.DESTINATION -> destination;
|
||||||
|
case Field.ID -> id();
|
||||||
|
case Field.PURPOSE -> purpose;
|
||||||
|
case Field.SOURCE -> source;
|
||||||
|
case Field.TAGS -> tags.stream().sorted().collect(Collectors.joining(", "));
|
||||||
|
case null, default -> null;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isDirty(){
|
public boolean isDirty(){
|
||||||
return !dirtyFields.isEmpty();
|
return !dirtyFields.isEmpty();
|
||||||
}
|
}
|
||||||
@@ -102,6 +118,18 @@ public class Transaction extends UmbrellaObject {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String quote(Object o, String delimiter){
|
||||||
|
var str = o == null ? "" : o instanceof Map<?,?> m ? m.get("value").toString() : o.toString();
|
||||||
|
if (str.contains(delimiter)) return '"'+str.replace("\"","\"\"")+'"';
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object resolveUsers(Map<Long, UmbrellaUser> users, Object o) {
|
||||||
|
if (!(o instanceof IdOrString ios)) return o;
|
||||||
|
if (ios.isId()) return users.get(ios.id()) instanceof UmbrellaUser user ? user.name() : "unknown";
|
||||||
|
return ios;
|
||||||
|
}
|
||||||
|
|
||||||
public IdOrString source(){
|
public IdOrString source(){
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<script>
|
<script>
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import { api, eventStream, get } from '../../urls.svelte';
|
import { api, download, eventStream, get } from '../../urls.svelte';
|
||||||
import { error, yikes } from '../../warn.svelte';
|
import { error, yikes } from '../../warn.svelte';
|
||||||
import { t } from '../../translations.svelte';
|
import { t } from '../../translations.svelte';
|
||||||
|
|
||||||
@@ -16,6 +16,10 @@
|
|||||||
let users = {};
|
let users = {};
|
||||||
let sums = $derived.by(calcSums);
|
let sums = $derived.by(calcSums);
|
||||||
|
|
||||||
|
function addToFilter(tag){
|
||||||
|
filter.push(tag.toLowerCase());
|
||||||
|
}
|
||||||
|
|
||||||
function calcSums(){
|
function calcSums(){
|
||||||
let sums = {};
|
let sums = {};
|
||||||
sums[ 0] = 0;
|
sums[ 0] = 0;
|
||||||
@@ -32,10 +36,6 @@
|
|||||||
return sums;
|
return sums;
|
||||||
}
|
}
|
||||||
|
|
||||||
function addToFilter(tag){
|
|
||||||
filter.push(tag.toLowerCase());
|
|
||||||
}
|
|
||||||
|
|
||||||
function checker(taglist, filter){
|
function checker(taglist, filter){
|
||||||
for (var f of filter){
|
for (var f of filter){
|
||||||
var included = false;
|
var included = false;
|
||||||
@@ -47,6 +47,10 @@
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function csvexport(ev){
|
||||||
|
download(api(`accounting/${id}/csv`),account.name+".csv");
|
||||||
|
}
|
||||||
|
|
||||||
function dropTag(tag){
|
function dropTag(tag){
|
||||||
filter = filter.filter(x => x != tag.toLowerCase());
|
filter = filter.filter(x => x != tag.toLowerCase());
|
||||||
}
|
}
|
||||||
@@ -167,7 +171,9 @@
|
|||||||
{t('external expenses')}<br/>
|
{t('external expenses')}<br/>
|
||||||
{t('sum external')}
|
{t('sum external')}
|
||||||
</td>
|
</td>
|
||||||
<td colspan="2"></td>
|
<td colspan="2">
|
||||||
|
<button onclick={csvexport}>{t('export as {format}',{format:'CSV'})}</button>
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|||||||
@@ -8,6 +8,13 @@ export function get(url){
|
|||||||
return fetch(url,{ credentials:'include' });
|
return fetch(url,{ credentials:'include' });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function download(url,name = null){
|
||||||
|
var link=document.createElement('a');
|
||||||
|
link.href = url;
|
||||||
|
link.download = name ? name : url.substr(url.lastIndexOf('/') + 1);
|
||||||
|
link.click();
|
||||||
|
}
|
||||||
|
|
||||||
export function drop(url, payload){
|
export function drop(url, payload){
|
||||||
let data = {
|
let data = {
|
||||||
credentials:'include',
|
credentials:'include',
|
||||||
|
|||||||
Reference in New Issue
Block a user