Incoming Invoice GPT Update
This commit is contained in:
@@ -8,9 +8,108 @@ import {
|
|||||||
files,
|
files,
|
||||||
filetags,
|
filetags,
|
||||||
incominginvoices,
|
incominginvoices,
|
||||||
|
vendors,
|
||||||
} from "../../../db/schema"
|
} from "../../../db/schema"
|
||||||
|
|
||||||
import { eq, and, isNull, not } from "drizzle-orm"
|
import { eq, and, isNull, not, desc } from "drizzle-orm"
|
||||||
|
|
||||||
|
type InvoiceAccount = {
|
||||||
|
account?: number | null
|
||||||
|
description?: string | null
|
||||||
|
taxType?: string | number | null
|
||||||
|
}
|
||||||
|
|
||||||
|
const normalizeAccounts = (accounts: unknown): InvoiceAccount[] => {
|
||||||
|
if (!Array.isArray(accounts)) return []
|
||||||
|
return accounts
|
||||||
|
.map((entry: any) => ({
|
||||||
|
account: typeof entry?.account === "number" ? entry.account : null,
|
||||||
|
description: typeof entry?.description === "string" ? entry.description : null,
|
||||||
|
taxType: entry?.taxType ?? null,
|
||||||
|
}))
|
||||||
|
.filter((entry) => entry.account !== null || entry.description || entry.taxType !== null)
|
||||||
|
}
|
||||||
|
|
||||||
|
const buildLearningContext = (historicalInvoices: any[]) => {
|
||||||
|
if (!historicalInvoices.length) return null
|
||||||
|
|
||||||
|
const vendorProfiles = new Map<number, {
|
||||||
|
vendorName: string
|
||||||
|
paymentTypes: Map<string, number>
|
||||||
|
accountUsage: Map<number, number>
|
||||||
|
sampleDescriptions: string[]
|
||||||
|
}>()
|
||||||
|
|
||||||
|
const recentExamples: any[] = []
|
||||||
|
|
||||||
|
for (const invoice of historicalInvoices) {
|
||||||
|
const accounts = normalizeAccounts(invoice.accounts)
|
||||||
|
const vendorId = typeof invoice.vendorId === "number" ? invoice.vendorId : null
|
||||||
|
const vendorName = typeof invoice.vendorName === "string" ? invoice.vendorName : "Unknown"
|
||||||
|
|
||||||
|
if (vendorId) {
|
||||||
|
if (!vendorProfiles.has(vendorId)) {
|
||||||
|
vendorProfiles.set(vendorId, {
|
||||||
|
vendorName,
|
||||||
|
paymentTypes: new Map(),
|
||||||
|
accountUsage: new Map(),
|
||||||
|
sampleDescriptions: [],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const profile = vendorProfiles.get(vendorId)!
|
||||||
|
if (invoice.paymentType) {
|
||||||
|
const key = String(invoice.paymentType)
|
||||||
|
profile.paymentTypes.set(key, (profile.paymentTypes.get(key) ?? 0) + 1)
|
||||||
|
}
|
||||||
|
for (const account of accounts) {
|
||||||
|
if (typeof account.account === "number") {
|
||||||
|
profile.accountUsage.set(account.account, (profile.accountUsage.get(account.account) ?? 0) + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (invoice.description && profile.sampleDescriptions.length < 3) {
|
||||||
|
profile.sampleDescriptions.push(String(invoice.description).slice(0, 120))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (recentExamples.length < 20) {
|
||||||
|
recentExamples.push({
|
||||||
|
vendorId,
|
||||||
|
vendorName,
|
||||||
|
paymentType: invoice.paymentType ?? null,
|
||||||
|
accounts: accounts.map((entry) => ({
|
||||||
|
account: entry.account,
|
||||||
|
description: entry.description ?? null,
|
||||||
|
taxType: entry.taxType ?? null,
|
||||||
|
})),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const vendorPatterns = Array.from(vendorProfiles.entries())
|
||||||
|
.map(([vendorId, profile]) => {
|
||||||
|
const commonPaymentType = Array.from(profile.paymentTypes.entries())
|
||||||
|
.sort((a, b) => b[1] - a[1])[0]?.[0] ?? null
|
||||||
|
const topAccounts = Array.from(profile.accountUsage.entries())
|
||||||
|
.sort((a, b) => b[1] - a[1])
|
||||||
|
.slice(0, 4)
|
||||||
|
.map(([accountId, count]) => ({ accountId, count }))
|
||||||
|
|
||||||
|
return {
|
||||||
|
vendorId,
|
||||||
|
vendorName: profile.vendorName,
|
||||||
|
commonPaymentType,
|
||||||
|
topAccounts,
|
||||||
|
sampleDescriptions: profile.sampleDescriptions,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.slice(0, 50)
|
||||||
|
|
||||||
|
return JSON.stringify({
|
||||||
|
vendorPatterns,
|
||||||
|
recentExamples,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export function prepareIncomingInvoices(server: FastifyInstance) {
|
export function prepareIncomingInvoices(server: FastifyInstance) {
|
||||||
const processInvoices = async (tenantId:number) => {
|
const processInvoices = async (tenantId:number) => {
|
||||||
@@ -72,13 +171,34 @@ export function prepareIncomingInvoices(server: FastifyInstance) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const historicalInvoices = await server.db
|
||||||
|
.select({
|
||||||
|
vendorId: incominginvoices.vendor,
|
||||||
|
vendorName: vendors.name,
|
||||||
|
paymentType: incominginvoices.paymentType,
|
||||||
|
description: incominginvoices.description,
|
||||||
|
accounts: incominginvoices.accounts,
|
||||||
|
})
|
||||||
|
.from(incominginvoices)
|
||||||
|
.leftJoin(vendors, eq(incominginvoices.vendor, vendors.id))
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(incominginvoices.tenant, tenantId),
|
||||||
|
eq(incominginvoices.archived, false)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.orderBy(desc(incominginvoices.createdAt))
|
||||||
|
.limit(120)
|
||||||
|
|
||||||
|
const learningContext = buildLearningContext(historicalInvoices)
|
||||||
|
|
||||||
// -------------------------------------------------------------
|
// -------------------------------------------------------------
|
||||||
// 3️⃣ Jede Datei einzeln durch GPT jagen & IncomingInvoice erzeugen
|
// 3️⃣ Jede Datei einzeln durch GPT jagen & IncomingInvoice erzeugen
|
||||||
// -------------------------------------------------------------
|
// -------------------------------------------------------------
|
||||||
for (const file of filesRes) {
|
for (const file of filesRes) {
|
||||||
console.log(`Processing file ${file.id} for tenant ${tenantId}`)
|
console.log(`Processing file ${file.id} for tenant ${tenantId}`)
|
||||||
|
|
||||||
const data = await getInvoiceDataFromGPT(server,file, tenantId)
|
const data = await getInvoiceDataFromGPT(server,file, tenantId, learningContext ?? undefined)
|
||||||
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
server.log.warn(`GPT returned no data for file ${file.id}`)
|
server.log.warn(`GPT returned no data for file ${file.id}`)
|
||||||
|
|||||||
@@ -91,7 +91,8 @@ const InstructionFormat = z.object({
|
|||||||
export const getInvoiceDataFromGPT = async function (
|
export const getInvoiceDataFromGPT = async function (
|
||||||
server: FastifyInstance,
|
server: FastifyInstance,
|
||||||
file: any,
|
file: any,
|
||||||
tenantId: number
|
tenantId: number,
|
||||||
|
learningContext?: string
|
||||||
) {
|
) {
|
||||||
await initOpenAi();
|
await initOpenAi();
|
||||||
|
|
||||||
@@ -188,8 +189,13 @@ export const getInvoiceDataFromGPT = async function (
|
|||||||
"You extract structured invoice data.\n\n" +
|
"You extract structured invoice data.\n\n" +
|
||||||
`VENDORS: ${JSON.stringify(vendorList)}\n` +
|
`VENDORS: ${JSON.stringify(vendorList)}\n` +
|
||||||
`ACCOUNTS: ${JSON.stringify(accountList)}\n\n` +
|
`ACCOUNTS: ${JSON.stringify(accountList)}\n\n` +
|
||||||
|
(learningContext
|
||||||
|
? `HISTORICAL_PATTERNS: ${learningContext}\n\n`
|
||||||
|
: "") +
|
||||||
"Match issuer by name to vendor.id.\n" +
|
"Match issuer by name to vendor.id.\n" +
|
||||||
"Match invoice items to account id based on label/number.\n" +
|
"Match invoice items to account id based on label/number.\n" +
|
||||||
|
"Use historical patterns as soft hints for vendor/account/payment mapping.\n" +
|
||||||
|
"Do not invent values when the invoice text contradicts the hints.\n" +
|
||||||
"Convert dates to YYYY-MM-DD.\n" +
|
"Convert dates to YYYY-MM-DD.\n" +
|
||||||
"Keep invoice items in original order.\n",
|
"Keep invoice items in original order.\n",
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -118,23 +118,37 @@ const totalCalculated = computed(() => {
|
|||||||
return { totalNet, totalAmount19Tax, totalAmount7Tax, totalGross }
|
return { totalNet, totalAmount19Tax, totalAmount7Tax, totalGross }
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const hasAmount = (value) => value !== null && value !== undefined && value !== ""
|
||||||
|
|
||||||
const recalculateItem = (item, source) => {
|
const recalculateItem = (item, source) => {
|
||||||
const taxRate = Number(taxOptions.value.find(i => i.key === item.taxType)?.percentage || 0);
|
const taxRate = Number(taxOptions.value.find(i => i.key === item.taxType)?.percentage || 0);
|
||||||
|
const calculateFromNet = () => {
|
||||||
if (source === 'net') {
|
if(!hasAmount(item.amountNet)) return
|
||||||
item.amountTax = Number((item.amountNet * (taxRate/100)).toFixed(2))
|
item.amountTax = Number((item.amountNet * (taxRate/100)).toFixed(2))
|
||||||
item.amountGross = Number((Number(item.amountNet) + item.amountTax).toFixed(2))
|
item.amountGross = Number((Number(item.amountNet) + item.amountTax).toFixed(2))
|
||||||
} else if (source === 'gross') {
|
}
|
||||||
|
const calculateFromGross = () => {
|
||||||
|
if(!hasAmount(item.amountGross)) return
|
||||||
item.amountNet = Number((item.amountGross / (1 + taxRate/100)).toFixed(2))
|
item.amountNet = Number((item.amountGross / (1 + taxRate/100)).toFixed(2))
|
||||||
item.amountTax = Number((item.amountGross - item.amountNet).toFixed(2))
|
item.amountTax = Number((item.amountGross - item.amountNet).toFixed(2))
|
||||||
} else if (source === 'taxType') {
|
|
||||||
if(item.amountNet) {
|
|
||||||
item.amountTax = Number((item.amountNet * (taxRate/100)).toFixed(2))
|
|
||||||
item.amountGross = Number((Number(item.amountNet) + item.amountTax).toFixed(2))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (source === 'net') {
|
||||||
|
calculateFromNet()
|
||||||
|
} else if (source === 'gross') {
|
||||||
|
calculateFromGross()
|
||||||
|
} else if (source === 'taxType' || source === 'manual') {
|
||||||
|
if(hasAmount(item.amountNet)) calculateFromNet()
|
||||||
|
else if(hasAmount(item.amountGross)) calculateFromGross()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const moveGrossToNet = (item) => {
|
||||||
|
if(!hasAmount(item.amountGross)) return
|
||||||
|
item.amountNet = Number(item.amountGross)
|
||||||
|
recalculateItem(item, 'net')
|
||||||
|
}
|
||||||
|
|
||||||
// --- Saving ---
|
// --- Saving ---
|
||||||
const updateIncomingInvoice = async (setBooked = false) => {
|
const updateIncomingInvoice = async (setBooked = false) => {
|
||||||
let item = { ...itemInfo.value }
|
let item = { ...itemInfo.value }
|
||||||
@@ -401,24 +415,35 @@ const findIncomingInvoiceErrors = computed(() => {
|
|||||||
</UFormGroup>
|
</UFormGroup>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col-span-12 md:col-span-4">
|
<div class="col-span-12 md:col-span-3">
|
||||||
<UFormGroup :label="useNetMode ? 'Betrag (Netto)' : 'Betrag (Brutto)'">
|
<UFormGroup label="Betrag (Netto)">
|
||||||
<UInput
|
<UInput
|
||||||
type="number"
|
type="number"
|
||||||
step="0.01"
|
step="0.01"
|
||||||
:disabled="mode === 'show'"
|
:disabled="mode === 'show' || !useNetMode"
|
||||||
:model-value="useNetMode ? item.amountNet : item.amountGross"
|
:model-value="item.amountNet"
|
||||||
@update:model-value="(val) => {
|
@update:model-value="(val) => { item.amountNet = Number(val); recalculateItem(item, 'net') }"
|
||||||
if(useNetMode) { item.amountNet = Number(val); recalculateItem(item, 'net') }
|
|
||||||
else { item.amountGross = Number(val); recalculateItem(item, 'gross') }
|
|
||||||
}"
|
|
||||||
>
|
>
|
||||||
<template #trailing>€</template>
|
<template #trailing>€</template>
|
||||||
</UInput>
|
</UInput>
|
||||||
</UFormGroup>
|
</UFormGroup>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col-span-6 md:col-span-4">
|
<div class="col-span-12 md:col-span-3">
|
||||||
|
<UFormGroup label="Betrag (Brutto)">
|
||||||
|
<UInput
|
||||||
|
type="number"
|
||||||
|
step="0.01"
|
||||||
|
:disabled="mode === 'show' || useNetMode"
|
||||||
|
:model-value="item.amountGross"
|
||||||
|
@update:model-value="(val) => { item.amountGross = Number(val); recalculateItem(item, 'gross') }"
|
||||||
|
>
|
||||||
|
<template #trailing>€</template>
|
||||||
|
</UInput>
|
||||||
|
</UFormGroup>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="col-span-6 md:col-span-3">
|
||||||
<UFormGroup label="Steuerschlüssel">
|
<UFormGroup label="Steuerschlüssel">
|
||||||
<USelectMenu
|
<USelectMenu
|
||||||
v-model="item.taxType"
|
v-model="item.taxType"
|
||||||
@@ -431,7 +456,7 @@ const findIncomingInvoiceErrors = computed(() => {
|
|||||||
</UFormGroup>
|
</UFormGroup>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col-span-6 md:col-span-4">
|
<div class="col-span-6 md:col-span-3">
|
||||||
<UFormGroup label="Steuerbetrag" help="Automatisch berechnet">
|
<UFormGroup label="Steuerbetrag" help="Automatisch berechnet">
|
||||||
<UInput :model-value="item.amountTax" disabled color="gray" >
|
<UInput :model-value="item.amountTax" disabled color="gray" >
|
||||||
<template #trailing>€</template>
|
<template #trailing>€</template>
|
||||||
@@ -439,6 +464,27 @@ const findIncomingInvoiceErrors = computed(() => {
|
|||||||
</UFormGroup>
|
</UFormGroup>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="col-span-12 flex justify-end gap-2">
|
||||||
|
<UButton
|
||||||
|
size="xs"
|
||||||
|
variant="outline"
|
||||||
|
icon="i-heroicons-calculator"
|
||||||
|
:disabled="mode === 'show'"
|
||||||
|
@click="recalculateItem(item, 'manual')"
|
||||||
|
>
|
||||||
|
Steuer berechnen
|
||||||
|
</UButton>
|
||||||
|
<UButton
|
||||||
|
size="xs"
|
||||||
|
variant="soft"
|
||||||
|
icon="i-heroicons-arrow-right"
|
||||||
|
:disabled="mode === 'show' || !hasAmount(item.amountGross)"
|
||||||
|
@click="moveGrossToNet(item)"
|
||||||
|
>
|
||||||
|
Brutto als Netto
|
||||||
|
</UButton>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="col-span-12">
|
<div class="col-span-12">
|
||||||
<UInput v-model="item.description" :disabled="mode === 'show'" placeholder="Positionstext (optional)" icon="i-heroicons-bars-3-bottom-left" variant="none" class="border-b border-gray-100 dark:border-gray-800" />
|
<UInput v-model="item.description" :disabled="mode === 'show'" placeholder="Positionstext (optional)" icon="i-heroicons-bars-3-bottom-left" variant="none" class="border-b border-gray-100 dark:border-gray-800" />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ const profileStore = useProfileStore()
|
|||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const url = useRequestURL()
|
const url = useRequestURL()
|
||||||
const supabase = useSupabaseClient()
|
|
||||||
const toast = useToast()
|
const toast = useToast()
|
||||||
|
|
||||||
const showAddBankRequisition = ref(false)
|
const showAddBankRequisition = ref(false)
|
||||||
@@ -57,11 +56,19 @@ const addAccount = async (account) => {
|
|||||||
bankId: account.institution_id
|
bankId: account.institution_id
|
||||||
}
|
}
|
||||||
|
|
||||||
const {data,error} = await supabase.from("bankaccounts").insert(accountData).select()
|
try {
|
||||||
if(error) {
|
// Nutzung von useEntities statt direktem DB-Call
|
||||||
toast.add({title: "Es gab einen Fehler bei hinzufügen des Accounts", color:"rose"})
|
// true als 2. Parameter verhindert den Redirect, da wir im Modal sind
|
||||||
} else if(data) {
|
const res = await useEntities("bankaccounts").create(accountData, true)
|
||||||
toast.add({title: "Account erfolgreich hinzugefügt"})
|
|
||||||
|
if(res) {
|
||||||
|
// useEntities feuert bereits einen Success-Toast ("X hinzugefügt")
|
||||||
|
// Wir laden die Seite neu, um die Buttons im Modal zu aktualisieren (Hinzufügen -> Aktualisieren)
|
||||||
|
await setupPage()
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
toast.add({title: "Es gab einen Fehler beim Hinzufügen des Accounts", color:"rose"})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,15 +76,16 @@ const updateAccount = async (account) => {
|
|||||||
|
|
||||||
let bankaccountId = bankaccounts.value.find(i => i.iban === account.iban).id
|
let bankaccountId = bankaccounts.value.find(i => i.iban === account.iban).id
|
||||||
|
|
||||||
const res = await useEntities("bankaccounts").update(bankaccountId, {accountId: account.id, expired: false})
|
// Fehlerbehandlung analog zu addAccount verbessert
|
||||||
|
try {
|
||||||
|
const res = await useEntities("bankaccounts").update(bankaccountId, {accountId: account.id, expired: false}, true)
|
||||||
|
|
||||||
if(!res) {
|
// useEntities feuert bereits einen Success-Toast
|
||||||
console.log(error)
|
// reqData.value = null // Das würde das Modal leeren, ggf. gewünscht? Im Original war es drin.
|
||||||
toast.add({title: "Es gab einen Fehler bei aktualisieren des Accounts", color:"rose"})
|
|
||||||
} else {
|
|
||||||
toast.add({title: "Account erfolgreich aktualisiert"})
|
|
||||||
reqData.value = null
|
|
||||||
setupPage()
|
setupPage()
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
|
toast.add({title: "Es gab einen Fehler beim Aktualisieren des Accounts", color:"rose"})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,6 +158,7 @@ setupPage()
|
|||||||
</template>
|
</template>
|
||||||
<div
|
<div
|
||||||
v-for="account in reqData.accounts"
|
v-for="account in reqData.accounts"
|
||||||
|
:key="account.id"
|
||||||
class="p-2 m-3 flex justify-between"
|
class="p-2 m-3 flex justify-between"
|
||||||
>
|
>
|
||||||
{{account.iban}} - {{account.owner_name}}
|
{{account.iban}} - {{account.owner_name}}
|
||||||
@@ -170,13 +179,6 @@ setupPage()
|
|||||||
</UCard>
|
</UCard>
|
||||||
</UModal>
|
</UModal>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<!-- <UButton @click="setupPage">Setup</UButton>
|
|
||||||
<div v-if="route.query.reqId">
|
|
||||||
{{reqData}}
|
|
||||||
</div>-->
|
|
||||||
|
|
||||||
<UTable
|
<UTable
|
||||||
:rows="bankaccounts"
|
:rows="bankaccounts"
|
||||||
:columns="[
|
:columns="[
|
||||||
|
|||||||
Reference in New Issue
Block a user