2021-01-21 13:24:46 +00:00
|
|
|
<template>
|
2021-01-27 01:40:47 +00:00
|
|
|
<q-card v-bind:class="{ 'bg-grey': isReversed }">
|
|
|
|
<q-card-section class="row items-start justify-between">
|
|
|
|
<div class="col text-center">
|
|
|
|
<div
|
|
|
|
v-bind:class="{ 'text-negative': isNegative() }"
|
|
|
|
class="text-weight-bold"
|
|
|
|
style="font-size: 2em"
|
|
|
|
>
|
|
|
|
<span v-if="isNegative()">-</span>{{ transaction.amount.toFixed(2) }} €
|
|
|
|
</div>
|
|
|
|
<div>{{ text }}</div>
|
|
|
|
<div>{{ timeStr }}</div>
|
|
|
|
</div>
|
|
|
|
<div class="col" style="text-align: right">
|
|
|
|
<q-btn
|
|
|
|
color="negative"
|
|
|
|
aria-label="Reverse transaction"
|
|
|
|
icon="mdi-trash-can"
|
|
|
|
square
|
|
|
|
:disable="!canReverse"
|
|
|
|
@click="reverse"
|
|
|
|
/>
|
|
|
|
</div>
|
2021-01-21 13:24:46 +00:00
|
|
|
</q-card-section>
|
|
|
|
</q-card>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2021-01-27 01:40:47 +00:00
|
|
|
import { ref, computed, defineComponent, onUnmounted, onMounted } from '@vue/composition-api';
|
2021-01-21 13:24:46 +00:00
|
|
|
import { hasPermission } from 'src/utils/permission';
|
|
|
|
import { formatDateTime } from 'src/utils/datetime';
|
|
|
|
import { StateInterfaceBalance } from 'src/plugins/balance/store/balance';
|
|
|
|
import { Store } from 'vuex';
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
transaction: FG.Transaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
name: 'Transaction',
|
2021-01-27 01:40:47 +00:00
|
|
|
props: {
|
|
|
|
transaction: {
|
|
|
|
type: Object,
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
watch: { transaction: 'refreshText' },
|
2021-01-21 13:24:46 +00:00
|
|
|
setup(props: Props, { root, emit }) {
|
|
|
|
const now = ref(Date.now());
|
|
|
|
const ival = setInterval(() => (now.value = Date.now()), 1000);
|
|
|
|
const store: Store<StateInterfaceBalance> = <Store<StateInterfaceBalance>>root.$store;
|
2021-01-27 01:40:47 +00:00
|
|
|
const text = ref('');
|
2021-01-21 13:24:46 +00:00
|
|
|
|
|
|
|
onUnmounted(() => clearInterval(ival));
|
2021-01-27 01:40:47 +00:00
|
|
|
onMounted(() => refreshText());
|
2021-01-21 13:24:46 +00:00
|
|
|
|
2021-01-27 01:40:47 +00:00
|
|
|
const isNegative = () => props.transaction.sender_id === store.state.user.currentUser?.userid;
|
2021-01-21 13:24:46 +00:00
|
|
|
|
2021-01-27 01:40:47 +00:00
|
|
|
const refreshText = async () => {
|
|
|
|
if (isNegative()) {
|
|
|
|
text.value = 'Anschreiben';
|
|
|
|
if (props.transaction.receiver_id !== null) {
|
|
|
|
const user = <FG.User>await store.dispatch('user/getUser', {
|
|
|
|
userid: props.transaction.receiver_id
|
|
|
|
});
|
|
|
|
text.value = `Gesendet an ${user.display_name}`;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
text.value = 'Gutschrift';
|
|
|
|
if (props.transaction.sender_id !== null) {
|
|
|
|
const user = <FG.User>await store.dispatch('user/getUser', {
|
|
|
|
userid: props.transaction.sender_id
|
|
|
|
});
|
|
|
|
text.value = `Bekommen von ${user.display_name}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-01-21 13:24:46 +00:00
|
|
|
|
2021-01-29 01:29:27 +00:00
|
|
|
const isReversed = computed(
|
|
|
|
() => props.transaction.reversal_id != undefined || props.transaction.original_id != undefined
|
|
|
|
);
|
2021-01-27 01:40:47 +00:00
|
|
|
|
|
|
|
const canReverse = computed(
|
|
|
|
() =>
|
|
|
|
!isReversed.value &&
|
|
|
|
(hasPermission('balance_reversal', store) ||
|
|
|
|
(props.transaction.sender_id === store.state.user.currentUser?.userid &&
|
|
|
|
now.value - props.transaction.time.getTime() < 10000))
|
|
|
|
);
|
2021-01-21 13:24:46 +00:00
|
|
|
|
2021-01-27 01:40:47 +00:00
|
|
|
function reverse() {
|
|
|
|
if (canReverse.value)
|
2021-01-21 13:24:46 +00:00
|
|
|
store
|
2021-01-27 01:40:47 +00:00
|
|
|
.dispatch('balance/revert', props.transaction)
|
2021-01-21 13:24:46 +00:00
|
|
|
.then(() => {
|
2021-01-27 01:40:47 +00:00
|
|
|
emit('update:transaction', props.transaction);
|
2021-01-21 13:24:46 +00:00
|
|
|
})
|
|
|
|
.catch(error => console.log(error));
|
|
|
|
}
|
|
|
|
|
|
|
|
const timeStr = computed(() => {
|
|
|
|
const elapsed = (now.value - props.transaction.time.getTime()) / 1000;
|
|
|
|
if (elapsed < 60) return `Vor ${elapsed.toFixed()} s`;
|
|
|
|
return formatDateTime(props.transaction.time, elapsed > 12 * 60 * 60, true, true) + ' Uhr';
|
|
|
|
});
|
|
|
|
|
2021-01-27 01:40:47 +00:00
|
|
|
return { timeStr, reverse, isNegative, text, refreshText, canReverse, isReversed };
|
2021-01-21 13:24:46 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|