Compare commits
4
Commits
4a7f18ff80
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4af4bdbb85 | ||
|
|
94f11ea38f | ||
|
|
695c1e769f | ||
|
|
eed6dbf38b |
@@ -78,7 +78,7 @@ public class AccountingModule extends BaseHandler implements AccountingService {
|
||||
case null -> getAccounts(user.get(),ex);
|
||||
default -> {
|
||||
try {
|
||||
yield getAccount(user.get(),Long.parseLong(head),ex);
|
||||
yield getAccount(user.get(),Long.parseLong(head), path.pop(), ex);
|
||||
} catch (NumberFormatException ignored) {}
|
||||
yield super.doGet(path,ex);
|
||||
}
|
||||
@@ -184,9 +184,17 @@ public class AccountingModule extends BaseHandler implements AccountingService {
|
||||
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);
|
||||
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 {
|
||||
|
||||
@@ -2,18 +2,18 @@
|
||||
package de.srsoftware.umbrella.core.api;
|
||||
|
||||
import static de.srsoftware.umbrella.core.Util.mapValues;
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
|
||||
import de.srsoftware.tools.Mappable;
|
||||
import de.srsoftware.umbrella.core.constants.Field;
|
||||
import de.srsoftware.umbrella.core.model.Account;
|
||||
import de.srsoftware.umbrella.core.model.Transaction;
|
||||
import de.srsoftware.umbrella.core.model.Translatable;
|
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
|
||||
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) {
|
||||
return new AccountData(account, transactions, userMap);
|
||||
}
|
||||
@@ -26,6 +26,16 @@ public interface AccountingService {
|
||||
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()
|
||||
.map(transaction -> transaction.csvLine(users, keys,";"))
|
||||
.forEach(line -> sb.append("\n").append(line));
|
||||
return sb.toString().getBytes(UTF_8);
|
||||
}
|
||||
}
|
||||
|
||||
AccountData loadAccount(long accountId);
|
||||
|
||||
@@ -9,11 +9,10 @@ import java.sql.SQLException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Transaction extends UmbrellaObject {
|
||||
public class Transaction extends UmbrellaObject implements Comparable<Transaction>{
|
||||
private final long accountId;
|
||||
private LocalDateTime date;
|
||||
private IdOrString source, destination;
|
||||
@@ -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){
|
||||
super(id);
|
||||
this.accountId = accountId;
|
||||
this.date = date;
|
||||
this.source = source;
|
||||
this.destination = destination;
|
||||
this.amount = amount;
|
||||
this.date = date;
|
||||
this.destination = destination;
|
||||
this.purpose = purpose;
|
||||
this.source = source;
|
||||
this.tags = tags == null ? new HashSet<>() : tags;
|
||||
}
|
||||
|
||||
@@ -52,6 +51,16 @@ public class Transaction extends UmbrellaObject {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Transaction other) {
|
||||
var order = date.compareTo(other.date);
|
||||
return order == 0 ? Long.compare(id(), other.id()) : order;
|
||||
}
|
||||
|
||||
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(){
|
||||
return date;
|
||||
}
|
||||
@@ -76,6 +85,19 @@ public class Transaction extends UmbrellaObject {
|
||||
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(){
|
||||
return !dirtyFields.isEmpty();
|
||||
}
|
||||
@@ -102,6 +124,18 @@ public class Transaction extends UmbrellaObject {
|
||||
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(){
|
||||
return source;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<script>
|
||||
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 { t } from '../../translations.svelte';
|
||||
|
||||
@@ -16,6 +16,10 @@
|
||||
let users = {};
|
||||
let sums = $derived.by(calcSums);
|
||||
|
||||
function addToFilter(tag){
|
||||
filter.push(tag.toLowerCase());
|
||||
}
|
||||
|
||||
function calcSums(){
|
||||
let sums = {};
|
||||
sums[ 0] = 0;
|
||||
@@ -32,10 +36,6 @@
|
||||
return sums;
|
||||
}
|
||||
|
||||
function addToFilter(tag){
|
||||
filter.push(tag.toLowerCase());
|
||||
}
|
||||
|
||||
function checker(taglist, filter){
|
||||
for (var f of filter){
|
||||
var included = false;
|
||||
@@ -47,6 +47,10 @@
|
||||
return true;
|
||||
}
|
||||
|
||||
function csvexport(ev){
|
||||
download(api(`accounting/${id}/csv`),account.name+".csv");
|
||||
}
|
||||
|
||||
function dropTag(tag){
|
||||
filter = filter.filter(x => x != tag.toLowerCase());
|
||||
}
|
||||
@@ -167,7 +171,9 @@
|
||||
{t('external expenses')}<br/>
|
||||
{t('sum external')}
|
||||
</td>
|
||||
<td colspan="2"></td>
|
||||
<td colspan="2">
|
||||
<button onclick={csvexport}>{t('export as {format}',{format:'CSV'})}</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
@@ -4,19 +4,22 @@
|
||||
import { t } from '../../translations.svelte';
|
||||
let { module, entityId } = $props();
|
||||
|
||||
let data = $state({journal:[]});
|
||||
let data = $state(null);
|
||||
let loading = $state(false);
|
||||
|
||||
async function loadJournal(){
|
||||
async function onclick(ev){
|
||||
loading = true;
|
||||
const url = api(`journal/${module}/${entityId}`);
|
||||
const res = await get(url);
|
||||
if (res.ok) {
|
||||
data = await res.json();
|
||||
} else error(res);
|
||||
loading = false;
|
||||
}
|
||||
|
||||
$effect(loadJournal);
|
||||
</script>
|
||||
|
||||
{#if data}
|
||||
<ul>
|
||||
{#each data.journal as entry (entry.id)}
|
||||
<li>
|
||||
@@ -27,3 +30,10 @@
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
{:else}
|
||||
{#if loading}
|
||||
{t('loading…')}
|
||||
{:else}
|
||||
<button {onclick}>{t('load {object}',{object:t('journal')})}</button>
|
||||
{/if}
|
||||
{/if}
|
||||
@@ -60,7 +60,6 @@
|
||||
}
|
||||
|
||||
function calcYearMap(){
|
||||
console.log('calcYearMap called');
|
||||
let result = {
|
||||
months : {},
|
||||
years : {}
|
||||
|
||||
@@ -10,12 +10,13 @@ export async function loadTranslation(lang){
|
||||
export function t(key,args = {}){
|
||||
if (key === undefined) return "";
|
||||
if (key instanceof Response) key = 'status.'+key.status;
|
||||
let set = translations.values;
|
||||
let set = translations.values;
|
||||
let keys = key.split('.');
|
||||
for (let token of keys){
|
||||
if (!set[token]){
|
||||
console.warn('Missing translation for '+key);
|
||||
return keys[keys.length-1].replaceAll('_',' ');
|
||||
set = keys[keys.length-1].replaceAll('_',' ');
|
||||
break;
|
||||
}
|
||||
set = set[token];
|
||||
}
|
||||
|
||||
@@ -8,6 +8,13 @@ export function get(url){
|
||||
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){
|
||||
let data = {
|
||||
credentials:'include',
|
||||
|
||||
@@ -198,6 +198,7 @@
|
||||
"items": "Artikel",
|
||||
|
||||
"join_objects" : "{objects} zusammenführen",
|
||||
"journal": "Journal",
|
||||
|
||||
"kanban": "Kanban",
|
||||
"key": "Suchbegriff",
|
||||
@@ -208,6 +209,7 @@
|
||||
"loading": "lade…",
|
||||
"loading_data": "Daten werden geladen…",
|
||||
"loading_object": "lade {object}…",
|
||||
"load {object}": "{object} laden",
|
||||
"local_court": "Amtsgericht",
|
||||
"locality": "Ort",
|
||||
"location": "Ort",
|
||||
|
||||
@@ -198,6 +198,7 @@
|
||||
"items": "items",
|
||||
|
||||
"join_objects" : "join {objects}",
|
||||
"journal": "Journal",
|
||||
|
||||
"kanban": "Kanban",
|
||||
"key": "search term",
|
||||
@@ -208,6 +209,7 @@
|
||||
"loading": "loading…",
|
||||
"loading_data": "loading data…",
|
||||
"loading_object": "loading {object}…",
|
||||
"load {object}": "load {object}",
|
||||
"local_court": "local court",
|
||||
"locality": "locality",
|
||||
"location": "location",
|
||||
|
||||
Reference in New Issue
Block a user