58 lines
2.6 KiB
JavaScript
58 lines
2.6 KiB
JavaScript
// Generated, do not edit. sha256:a607e071a277540b2c65e9bb3a8807791f7326ce16b2766440a5c537f44ad532
|
|||
|
|
import { bookReservation, deleteReservation } from '../data/reservations.js';
|
||
|
|
import { listSelectableAccounts } from '../data/accounts.js';
|
||
|
|
import { t } from '../i18n/index.js';
|
||
|
|
import * as dialog from '../ui/dialog.js';
|
||
|
|
import { fromSubunit, toSubunit } from '../ui/format.js';
|
||
|
|
import { localDate, toUtcIso } from '../../shared/time.js';
|
||
|
|
|
||
|
|
export const reservationMeta = r => ({ id: r.assetId, kind: r.asset_kind, decimals: r.asset_decimals,
|
||
|
|
unit: r.asset_unit, subunit: r.asset_subunit, displayUnit: r.asset_display_unit });
|
||
|
|
export const reservationDir = r => (r.fromAccountId != null ? 'out' : 'in');
|
||
|
|
export const reservationAccount = r => (r.fromAccountId != null
|
||
|
|
? { id: r.fromAccountId, name: r.from_name } : { id: r.toAccountId, name: r.to_name });
|
||
|
|
|
||
|
|
export async function bookReservationFlow(r) {
|
||
|
|
const own = reservationAccount(r).id;
|
||
|
|
const accounts = (await listSelectableAccounts({ ookId: null })).filter(a => a.id !== own);
|
||
|
|
const res = await dialog.form({
|
||
|
|
title: t('reservations.bookTitle'),
|
||
|
|
fields: [
|
||
|
|
{ key: 'date', label: t('reservations.colDate'), type: 'date', value: localDate(r.occurredAt) },
|
||
|
|
{ key: 'amount', label: `${t('reservations.colAmount')} (${r.asset_symbol})`, type: 'number',
|
||
|
|
value: String(fromSubunit(r.amount, reservationMeta(r))) },
|
||
|
|
{ key: 'other', label: t(reservationDir(r) === 'out' ? 'reservations.otherTo' : 'reservations.otherFrom'),
|
||
|
|
type: 'select', value: '',
|
||
|
|
options: [{ value: '', label: t('reservations.outside') }, ...accounts.map(a => ({ value: a.id, label: a.name }))] },
|
||
|
|
],
|
||
|
|
confirmLabel: t('reservations.book'),
|
||
|
|
});
|
||
|
|
if (!res) return false;
|
||
|
|
const qty = parseFloat(res.amount);
|
||
|
|
if (!res.date || !Number.isFinite(qty) || qty <= 0) {
|
||
|
|
await dialog.alert({ title: t('dialog.error'), message: t('reservations.fill') });
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
await bookReservation(r.id, {
|
||
|
|
occurredAt: toUtcIso(`${res.date}T12:00:00`),
|
||
|
|
amount: toSubunit(qty, reservationMeta(r)),
|
||
|
|
otherAccountId: res.other ? Number(res.other) : null,
|
||
|
|
});
|
||
|
|
return true;
|
||
|
|
} catch (err) {
|
||
|
|
await dialog.alert({ title: t('dialog.error'), message: err.message });
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function settleReservationFlow(r) {
|
||
|
|
if (!(await dialog.confirm({ message: t('reservations.confirmSettle') }))) return false;
|
||
|
|
try {
|
||
|
|
await deleteReservation(r.id);
|
||
|
|
return true;
|
||
|
|
} catch (err) {
|
||
|
|
await dialog.alert({ title: t('dialog.error'), message: err.message });
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|