| 1 |
<script lang="ts"> |
| 2 |
import { get } from "../api"; |
| 3 |
import AutocompleteInput from "../AutocompleteInput.svelte"; |
| 4 |
import type { EntryMetadata, Transaction } from "../entries"; |
| 5 |
import { Posting } from "../entries"; |
| 6 |
import { _ } from "../i18n"; |
| 7 |
import { move } from "../lib/array"; |
| 8 |
import { notify_err } from "../notifications"; |
| 9 |
import { payees } from "../stores"; |
| 10 |
import AddMetadataButton from "./AddMetadataButton.svelte"; |
| 11 |
import EntryMetadataSvelte from "./EntryMetadata.svelte"; |
| 12 |
import PostingSvelte from "./Posting.svelte"; |
| 13 |
|
| 14 |
interface Props { |
| 15 |
entry: Transaction; |
| 16 |
} |
| 17 |
|
| 18 |
let { entry = $bindable() }: Props = $props(); |
| 19 |
let suggestions: string[] | undefined = $state.raw(); |
| 20 |
|
| 21 |
let payee = $derived(entry.payee); |
| 22 |
$effect(() => { |
| 23 |
if (payee) { |
| 24 |
suggestions = undefined; |
| 25 |
if ($payees.includes(payee)) { |
| 26 |
get("payee_accounts", { payee }) |
| 27 |
.then((s) => { |
| 28 |
suggestions = s; |
| 29 |
}) |
| 30 |
.catch((error: unknown) => { |
| 31 |
notify_err( |
| 32 |
error, |
| 33 |
(err) => |
| 34 |
`Fetching account suggestions for payee ${payee} failed: ${err.message}`, |
| 35 |
); |
| 36 |
}); |
| 37 |
} |
| 38 |
} |
| 39 |
}); |
| 40 |
|
| 41 |
let narration = $derived(entry.get_narration_tags_links()); |
| 42 |
|
| 43 |
// Autofill complete transactions. |
| 44 |
async function autocompleteSelectPayee() { |
| 45 |
if (entry.narration || entry.postings.some((p) => !p.is_empty())) { |
| 46 |
return; |
| 47 |
} |
| 48 |
const payee_transaction = await get("payee_transaction", { |
| 49 |
payee: entry.payee, |
| 50 |
}); |
| 51 |
entry = payee_transaction.set("date", entry.date); |
| 52 |
} |
| 53 |
|
| 54 |
// Always have one empty posting at the end. |
| 55 |
$effect(() => { |
| 56 |
if (!entry.postings.some((p) => p.is_empty())) { |
| 57 |
entry = entry.set("postings", entry.postings.concat(Posting.empty())); |
| 58 |
} |
| 59 |
}); |
| 60 |
</script> |
| 61 |
|
| 62 |
<div> |
| 63 |
<div class="flex-row"> |
| 64 |
<input |
| 65 |
type="date" |
| 66 |
bind:value={ |
| 67 |
() => entry.date, |
| 68 |
(date: string) => { |
| 69 |
entry = entry.set("date", date); |
| 70 |
} |
| 71 |
} |
| 72 |
required |
| 73 |
/> |
| 74 |
<input |
| 75 |
type="text" |
| 76 |
name="flag" |
| 77 |
bind:value={ |
| 78 |
() => entry.flag, |
| 79 |
(flag: string) => { |
| 80 |
entry = entry.set("flag", flag); |
| 81 |
} |
| 82 |
} |
| 83 |
required |
| 84 |
/> |
| 85 |
<label> |
| 86 |
<span>{_("Payee")}:</span> |
| 87 |
<AutocompleteInput |
| 88 |
className="payee" |
| 89 |
placeholder={_("Payee")} |
| 90 |
bind:value={ |
| 91 |
() => entry.payee, |
| 92 |
(payee: string) => { |
| 93 |
entry = entry.set("payee", payee); |
| 94 |
} |
| 95 |
} |
| 96 |
suggestions={$payees} |
| 97 |
onSelect={autocompleteSelectPayee} |
| 98 |
/> |
| 99 |
</label> |
| 100 |
<label> |
| 101 |
<span>{_("Narration")}:</span> |
| 102 |
<input |
| 103 |
type="text" |
| 104 |
name="narration" |
| 105 |
placeholder={_("Narration")} |
| 106 |
value={narration} |
| 107 |
onchange={({ currentTarget }) => { |
| 108 |
entry = entry.set_narration_tags_links(currentTarget.value); |
| 109 |
}} |
| 110 |
/> |
| 111 |
<AddMetadataButton |
| 112 |
bind:meta={ |
| 113 |
() => entry.meta, |
| 114 |
(meta: EntryMetadata) => { |
| 115 |
entry = entry.set("meta", meta); |
| 116 |
} |
| 117 |
} |
| 118 |
/> |
| 119 |
</label> |
| 120 |
</div> |
| 121 |
<EntryMetadataSvelte |
| 122 |
bind:meta={ |
| 123 |
() => entry.meta, |
| 124 |
(meta: EntryMetadata) => { |
| 125 |
entry = entry.set("meta", meta); |
| 126 |
} |
| 127 |
} |
| 128 |
/> |
| 129 |
<div class="flex-row"> |
| 130 |
<span class="label"> <span>{_("Postings")}:</span> </span> |
| 131 |
</div> |
| 132 |
{#each entry.postings as posting, index} |
| 133 |
<PostingSvelte |
| 134 |
bind:posting={ |
| 135 |
() => posting, |
| 136 |
(posting: Posting) => { |
| 137 |
entry = entry.set("postings", entry.postings.with(index, posting)); |
| 138 |
} |
| 139 |
} |
| 140 |
{index} |
| 141 |
{suggestions} |
| 142 |
date={entry.date} |
| 143 |
move={({ from, to }: { from: number; to: number }) => { |
| 144 |
entry = entry.set("postings", move(entry.postings, from, to)); |
| 145 |
}} |
| 146 |
remove={() => { |
| 147 |
entry = entry.set("postings", entry.postings.toSpliced(index, 1)); |
| 148 |
}} |
| 149 |
/> |
| 150 |
{/each} |
| 151 |
</div> |
| 152 |
|
| 153 |
<style> |
| 154 |
input[name="flag"] { |
| 155 |
width: 1.5em; |
| 156 |
padding-right: 2px; |
| 157 |
padding-left: 2px; |
| 158 |
text-align: center; |
| 159 |
} |
| 160 |
|
| 161 |
div :global(.payee) { |
| 162 |
flex-grow: 1; |
| 163 |
flex-basis: 100px; |
| 164 |
} |
| 165 |
|
| 166 |
input[name="narration"] { |
| 167 |
flex-grow: 1; |
| 168 |
flex-basis: 200px; |
| 169 |
} |
| 170 |
|
| 171 |
label > span:first-child, |
| 172 |
.label > span:first-child { |
| 173 |
display: none; |
| 174 |
} |
| 175 |
|
| 176 |
@media (width <= 767px) { |
| 177 |
label > span:first-child, |
| 178 |
.label > span:first-child { |
| 179 |
display: initial; |
| 180 |
width: 100%; |
| 181 |
} |
| 182 |
} |
| 183 |
</style> |