Compare commits
16 Commits
0f5275b870
...
34f537238e
| Author | SHA1 | Date | |
|---|---|---|---|
| 34f537238e | |||
| 64df33f0fa | |||
| 94ab3350ec | |||
| aa162dcad3 | |||
| c42e57494a | |||
| 582af62fcb | |||
| 743c0e8772 | |||
| d4c39d7d44 | |||
| e60188f043 | |||
| ca4f1ba1c0 | |||
| 5fe823f52a | |||
| 1969610130 | |||
| 2bf52b35fe | |||
| f01881a6ce | |||
| a185c6eb11 | |||
| a8450fc0c6 |
@@ -30,6 +30,7 @@ import userRoutes from "./routes/auth/user";
|
||||
import publiclinksAuthenticatedRoutes from "./routes/publiclinks/publiclinks-authenticated";
|
||||
import wikiRoutes from "./routes/wiki";
|
||||
import portalContractRoutes from "./routes/portal/contracts";
|
||||
import mcpRoutes from "./routes/mcp";
|
||||
|
||||
//Public Links
|
||||
import publiclinksNonAuthenticatedRoutes from "./routes/publiclinks/publiclinks-non-authenticated";
|
||||
@@ -148,6 +149,7 @@ async function main() {
|
||||
await subApp.register(resourceRoutes);
|
||||
await subApp.register(wikiRoutes);
|
||||
await subApp.register(portalContractRoutes);
|
||||
await subApp.register(mcpRoutes);
|
||||
|
||||
},{prefix: "/api"})
|
||||
|
||||
|
||||
88
backend/src/mcp/authz.ts
Normal file
88
backend/src/mcp/authz.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { FastifyInstance, FastifyRequest } from "fastify"
|
||||
import { and, eq, or, isNull, inArray } from "drizzle-orm"
|
||||
import {
|
||||
authRoles,
|
||||
authRolePermissions,
|
||||
authUserRoles,
|
||||
} from "../../db/schema"
|
||||
import { McpContext, McpTool } from "./types"
|
||||
|
||||
export async function loadTenantPermissions(
|
||||
server: FastifyInstance,
|
||||
userId: string,
|
||||
tenantId: number
|
||||
) {
|
||||
const roleRows = await server.db
|
||||
.select({
|
||||
roleId: authUserRoles.role_id,
|
||||
})
|
||||
.from(authUserRoles)
|
||||
.innerJoin(
|
||||
authRoles,
|
||||
and(
|
||||
eq(authRoles.id, authUserRoles.role_id),
|
||||
or(isNull(authRoles.tenant_id), eq(authRoles.tenant_id, tenantId))
|
||||
)
|
||||
)
|
||||
.where(
|
||||
and(
|
||||
eq(authUserRoles.user_id, userId),
|
||||
eq(authUserRoles.tenant_id, tenantId)
|
||||
)
|
||||
)
|
||||
|
||||
const roleIds = Array.from(new Set(roleRows.map((row) => row.roleId)))
|
||||
|
||||
if (roleIds.length === 0) return []
|
||||
|
||||
const permissionRows = await server.db
|
||||
.select({
|
||||
permission: authRolePermissions.permission,
|
||||
})
|
||||
.from(authRolePermissions)
|
||||
.where(inArray(authRolePermissions.role_id, roleIds))
|
||||
|
||||
return Array.from(new Set(permissionRows.map((row) => row.permission)))
|
||||
}
|
||||
|
||||
export async function createMcpContext(
|
||||
server: FastifyInstance,
|
||||
request: FastifyRequest
|
||||
): Promise<McpContext> {
|
||||
const user = request.user
|
||||
|
||||
if (!user?.user_id) {
|
||||
throw Object.assign(new Error("Authentication required"), { statusCode: 401 })
|
||||
}
|
||||
|
||||
if (!user.tenant_id) {
|
||||
throw Object.assign(new Error("MCP benötigt einen aktiven Mandanten"), { statusCode: 403 })
|
||||
}
|
||||
|
||||
const permissions = await loadTenantPermissions(server, user.user_id, user.tenant_id)
|
||||
|
||||
return {
|
||||
server,
|
||||
request,
|
||||
tenantId: user.tenant_id,
|
||||
userId: user.user_id,
|
||||
isAdmin: Boolean(user.is_admin),
|
||||
permissions,
|
||||
}
|
||||
}
|
||||
|
||||
export function assertToolPermission(context: McpContext, tool: McpTool) {
|
||||
if (context.isAdmin) return
|
||||
|
||||
const allowed = tool.requiredPermissions.every((permission) =>
|
||||
context.permissions.includes(permission)
|
||||
)
|
||||
|
||||
if (!allowed) {
|
||||
throw Object.assign(
|
||||
new Error(`Fehlende Berechtigung für ${tool.name}: ${tool.requiredPermissions.join(", ")}`),
|
||||
{ statusCode: 403 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
11
backend/src/mcp/registry.ts
Normal file
11
backend/src/mcp/registry.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { accountingTools } from "./tools/accounting"
|
||||
import { masterdataTools } from "./tools/masterdata"
|
||||
import { organisationTools } from "./tools/organisation"
|
||||
|
||||
export const mcpTools = [
|
||||
...accountingTools,
|
||||
...masterdataTools,
|
||||
...organisationTools,
|
||||
]
|
||||
|
||||
export const mcpToolMap = new Map(mcpTools.map((tool) => [tool.name, tool]))
|
||||
36
backend/src/mcp/result.ts
Normal file
36
backend/src/mcp/result.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { McpToolResult } from "./types"
|
||||
|
||||
export function asToolResult(payload: unknown): McpToolResult {
|
||||
const structuredContent =
|
||||
payload && typeof payload === "object" && !Array.isArray(payload)
|
||||
? payload as Record<string, unknown>
|
||||
: { result: payload }
|
||||
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: JSON.stringify(payload, null, 2),
|
||||
},
|
||||
],
|
||||
structuredContent,
|
||||
}
|
||||
}
|
||||
|
||||
export function asToolError(error: unknown): McpToolResult {
|
||||
const message = error instanceof Error ? error.message : "Unbekannter Fehler"
|
||||
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: message,
|
||||
},
|
||||
],
|
||||
isError: true,
|
||||
structuredContent: {
|
||||
error: message,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
970
backend/src/mcp/tools/accounting.ts
Normal file
970
backend/src/mcp/tools/accounting.ts
Normal file
@@ -0,0 +1,970 @@
|
||||
import { and, desc, eq, ilike, or } from "drizzle-orm"
|
||||
import {
|
||||
accounts,
|
||||
bankstatements,
|
||||
createddocuments,
|
||||
files,
|
||||
incominginvoices,
|
||||
statementallocations,
|
||||
} from "../../../db/schema"
|
||||
import { useNextNumberRangeNumber } from "../../utils/functions"
|
||||
import { McpTool } from "../types"
|
||||
|
||||
const limitFromArgs = (args: Record<string, unknown>, fallback = 25) => {
|
||||
const raw = Number(args.limit ?? fallback)
|
||||
if (!Number.isFinite(raw)) return fallback
|
||||
return Math.min(Math.max(Math.trunc(raw), 1), 100)
|
||||
}
|
||||
|
||||
const stringArg = (args: Record<string, unknown>, key: string) => {
|
||||
const value = args[key]
|
||||
return typeof value === "string" && value.trim() ? value.trim() : null
|
||||
}
|
||||
|
||||
const numberArg = (args: Record<string, unknown>, key: string) => {
|
||||
const value = Number(args[key])
|
||||
return Number.isFinite(value) ? value : null
|
||||
}
|
||||
|
||||
const hasValue = (value: unknown) => value !== null && value !== undefined && value !== ""
|
||||
const hasValidNumber = (value: unknown) => hasValue(value) && Number.isFinite(Number(value))
|
||||
|
||||
const allowedOutgoingDocumentTypes = new Set([
|
||||
"quotes",
|
||||
"costEstimates",
|
||||
"confirmationOrders",
|
||||
"deliveryNotes",
|
||||
"packingSlips",
|
||||
"invoices",
|
||||
"advanceInvoices",
|
||||
"cancellationInvoices",
|
||||
"serialInvoices",
|
||||
])
|
||||
|
||||
const allowedOutgoingDocumentTaxTypes = new Set([
|
||||
"Standard",
|
||||
"13b UStG",
|
||||
"19 UStG",
|
||||
"12.3 UStG",
|
||||
])
|
||||
|
||||
const outgoingDocumentTaxableTypes = new Set([
|
||||
"invoices",
|
||||
"cancellationInvoices",
|
||||
"advanceInvoices",
|
||||
"serialInvoices",
|
||||
"confirmationOrders",
|
||||
"quotes",
|
||||
"costEstimates",
|
||||
])
|
||||
|
||||
const documentTypeArg = (args: Record<string, unknown>, key = "type") => {
|
||||
const type = stringArg(args, key) || "invoices"
|
||||
if (!allowedOutgoingDocumentTypes.has(type)) {
|
||||
throw new Error(`Ungültige Belegart: ${type}`)
|
||||
}
|
||||
return type
|
||||
}
|
||||
|
||||
const normalizeOutgoingDocumentTaxType = (value: unknown) => {
|
||||
const taxType = typeof value === "string" && value.trim() ? value.trim() : "Standard"
|
||||
if (!allowedOutgoingDocumentTaxTypes.has(taxType)) {
|
||||
throw new Error(`Ungültiger Dokument-Steuertyp: ${taxType}`)
|
||||
}
|
||||
return taxType
|
||||
}
|
||||
|
||||
const applyOutgoingDocumentTaxType = (
|
||||
payload: Record<string, unknown>,
|
||||
args: Record<string, unknown>,
|
||||
documentType: string,
|
||||
existingTaxType?: unknown,
|
||||
existingRows?: unknown
|
||||
) => {
|
||||
if (!outgoingDocumentTaxableTypes.has(documentType)) {
|
||||
payload.taxType = null
|
||||
return
|
||||
}
|
||||
|
||||
const taxType = args.taxType !== undefined
|
||||
? normalizeOutgoingDocumentTaxType(args.taxType)
|
||||
: existingTaxType
|
||||
? normalizeOutgoingDocumentTaxType(existingTaxType)
|
||||
: "Standard"
|
||||
|
||||
payload.taxType = taxType
|
||||
|
||||
if (["13b UStG", "19 UStG", "12.3 UStG"].includes(taxType)) {
|
||||
const rows = Array.isArray(payload.rows)
|
||||
? payload.rows
|
||||
: Array.isArray(existingRows)
|
||||
? existingRows
|
||||
: null
|
||||
|
||||
if (!rows) return
|
||||
|
||||
payload.rows = rows.map((row: any) => ({
|
||||
...row,
|
||||
taxPercent: 0,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
const optionalObjectArg = (args: Record<string, unknown>, key: string) => {
|
||||
const value = args[key]
|
||||
return value && typeof value === "object" && !Array.isArray(value) ? value : null
|
||||
}
|
||||
|
||||
const optionalArrayArg = (args: Record<string, unknown>, key: string) => {
|
||||
const value = args[key]
|
||||
return Array.isArray(value) ? value : null
|
||||
}
|
||||
|
||||
const buildOutgoingDocumentPayload = (
|
||||
args: Record<string, unknown>,
|
||||
userId: string,
|
||||
tenantId: number,
|
||||
includeCreateDefaults = false
|
||||
) => {
|
||||
const payload: Record<string, unknown> = {
|
||||
updatedAt: new Date(),
|
||||
updatedBy: userId,
|
||||
}
|
||||
|
||||
if (includeCreateDefaults) {
|
||||
payload.tenant = tenantId
|
||||
payload.createdBy = userId
|
||||
payload.created_by = userId
|
||||
payload.archived = false
|
||||
payload.state = stringArg(args, "state") || "Entwurf"
|
||||
payload.type = documentTypeArg(args)
|
||||
payload.rows = optionalArrayArg(args, "rows") || []
|
||||
}
|
||||
|
||||
const stringFields = [
|
||||
"state",
|
||||
"documentDate",
|
||||
"deliveryDate",
|
||||
"deliveryDateEnd",
|
||||
"deliveryDateType",
|
||||
"payment_type",
|
||||
"title",
|
||||
"description",
|
||||
"startText",
|
||||
"endText",
|
||||
]
|
||||
|
||||
for (const field of stringFields) {
|
||||
if (args[field] !== undefined) payload[field] = stringArg(args, field)
|
||||
}
|
||||
|
||||
for (const field of ["customer", "contact", "contract", "project", "plant", "letterhead", "createddocument"] as const) {
|
||||
if (args[field] !== undefined) payload[field] = numberArg(args, field)
|
||||
}
|
||||
|
||||
for (const field of ["paymentDays"] as const) {
|
||||
if (args[field] !== undefined) payload[field] = numberArg(args, field)
|
||||
}
|
||||
|
||||
if (args.type !== undefined) payload.type = documentTypeArg(args)
|
||||
if (args.address !== undefined) payload.address = optionalObjectArg(args, "address")
|
||||
if (args.info !== undefined) payload.info = optionalObjectArg(args, "info")
|
||||
if (args.agriculture !== undefined) payload.agriculture = optionalObjectArg(args, "agriculture")
|
||||
if (args.report !== undefined) payload.report = optionalObjectArg(args, "report") || {}
|
||||
if (args.serialConfig !== undefined) payload.serialConfig = optionalObjectArg(args, "serialConfig") || {}
|
||||
if (args.rows !== undefined) payload.rows = optionalArrayArg(args, "rows") || []
|
||||
if (args.usedAdvanceInvoices !== undefined) payload.usedAdvanceInvoices = optionalArrayArg(args, "usedAdvanceInvoices") || []
|
||||
if (typeof args.availableInPortal === "boolean") payload.availableInPortal = args.availableInPortal
|
||||
if (typeof args.advanceInvoiceResolved === "boolean") payload.advanceInvoiceResolved = args.advanceInvoiceResolved
|
||||
if (args.customSurchargePercentage !== undefined) payload.customSurchargePercentage = numberArg(args, "customSurchargePercentage") || 0
|
||||
|
||||
return payload
|
||||
}
|
||||
|
||||
const assertNoManualDocumentNumber = (args: Record<string, unknown>) => {
|
||||
if (args.documentNumber !== undefined || args.assignDocumentNumber !== undefined) {
|
||||
throw new Error("Belegnummern werden nur beim Finalisieren vergeben")
|
||||
}
|
||||
}
|
||||
|
||||
const incomingInvoiceAccountsArg = (args: Record<string, unknown>) => {
|
||||
if (args.accounts === undefined) return undefined
|
||||
if (!Array.isArray(args.accounts)) throw new Error("accounts muss ein Array sein")
|
||||
return args.accounts
|
||||
}
|
||||
|
||||
const buildIncomingInvoicePayload = (
|
||||
args: Record<string, unknown>,
|
||||
userId: string,
|
||||
tenantId: number,
|
||||
includeCreateDefaults = false
|
||||
) => {
|
||||
const payload: Record<string, unknown> = {
|
||||
updatedAt: new Date(),
|
||||
updatedBy: userId,
|
||||
}
|
||||
|
||||
if (includeCreateDefaults) {
|
||||
payload.tenant = tenantId
|
||||
payload.state = "Entwurf"
|
||||
payload.expense = args.expense !== undefined ? args.expense === true : true
|
||||
payload.paid = false
|
||||
payload.archived = false
|
||||
}
|
||||
|
||||
for (const field of ["state", "reference", "date", "dueDate", "description", "paymentType"] as const) {
|
||||
if (args[field] !== undefined) payload[field] = stringArg(args, field)
|
||||
}
|
||||
if (args.vendor !== undefined) payload.vendor = numberArg(args, "vendor")
|
||||
if (args.document !== undefined) payload.document = numberArg(args, "document")
|
||||
if (args.expense !== undefined) payload.expense = args.expense === true
|
||||
if (args.paid !== undefined) payload.paid = args.paid === true
|
||||
|
||||
const accountsPayload = incomingInvoiceAccountsArg(args)
|
||||
if (accountsPayload !== undefined) payload.accounts = accountsPayload
|
||||
|
||||
return payload
|
||||
}
|
||||
|
||||
const isDepreciationBookingMode = (value: unknown) =>
|
||||
["depreciation", "depreciation_bundle"].includes(String(value || ""))
|
||||
|
||||
const validateIncomingInvoiceData = (invoice: Record<string, any>) => {
|
||||
const errors: Array<{ message: string; type: "breaking" | "warning" }> = []
|
||||
|
||||
if (!invoice.vendor) errors.push({ message: "Es ist kein Lieferant ausgewählt", type: "breaking" })
|
||||
if (!String(invoice.reference || "").trim()) errors.push({ message: "Es ist keine Referenz angegeben", type: "breaking" })
|
||||
if (!invoice.date) errors.push({ message: "Es ist kein Datum ausgewählt", type: "breaking" })
|
||||
if (!Array.isArray(invoice.accounts) || invoice.accounts.length === 0) {
|
||||
errors.push({ message: "Es ist keine Position vorhanden", type: "breaking" })
|
||||
}
|
||||
|
||||
;(Array.isArray(invoice.accounts) ? invoice.accounts : []).forEach((account: any, idx: number) => {
|
||||
const pos = idx + 1
|
||||
|
||||
if (!account?.account) errors.push({ message: `Pos ${pos}: Keine Kategorie`, type: "breaking" })
|
||||
if (!hasValidNumber(account?.amountNet) && !hasValidNumber(account?.amountGross)) {
|
||||
errors.push({ message: `Pos ${pos}: Kein gültiger Betrag`, type: "breaking" })
|
||||
}
|
||||
if (!account?.taxType) errors.push({ message: `Pos ${pos}: Kein Steuerschlüssel`, type: "breaking" })
|
||||
if (hasValidNumber(account?.amountNet) && !hasValidNumber(account?.amountTax)) {
|
||||
errors.push({ message: `Pos ${pos}: Steuerbetrag fehlt, bitte Steuer neu berechnen`, type: "warning" })
|
||||
}
|
||||
if (isDepreciationBookingMode(account?.bookingMode) && !Number(account?.depreciationMonths)) {
|
||||
errors.push({ message: `Pos ${pos}: Abschreibungsdauer fehlt`, type: "breaking" })
|
||||
}
|
||||
if (account?.bookingMode === "depreciation_bundle" && !String(account?.depreciationGroup || "").trim()) {
|
||||
errors.push({ message: `Pos ${pos}: Sammelposten benötigt einen Gruppennamen`, type: "breaking" })
|
||||
}
|
||||
})
|
||||
|
||||
const order = { breaking: 0, warning: 1 }
|
||||
errors.sort((a, b) => order[a.type] - order[b.type])
|
||||
|
||||
return {
|
||||
valid: errors.every((error) => error.type !== "breaking"),
|
||||
errors,
|
||||
blockingErrors: errors.filter((error) => error.type === "breaking"),
|
||||
warnings: errors.filter((error) => error.type === "warning"),
|
||||
}
|
||||
}
|
||||
|
||||
export const accountingTools: McpTool[] = [
|
||||
{
|
||||
name: "accounting.outgoing_documents.tax_types.list",
|
||||
title: "Steuertypen für Ausgangsbelege auflisten",
|
||||
description: "Listet die unterstützten Dokument-Steuertypen für Ausgangsbelege.",
|
||||
requiredPermissions: ["accounting.outgoing_documents.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {},
|
||||
},
|
||||
async handler() {
|
||||
return {
|
||||
rows: [
|
||||
{ key: "Standard", label: "Standard", forcesZeroTaxPercent: false },
|
||||
{ key: "13b UStG", label: "13b UStG", forcesZeroTaxPercent: true },
|
||||
{ key: "19 UStG", label: "19 UStG Kleinunternehmer", forcesZeroTaxPercent: true },
|
||||
{ key: "12.3 UStG", label: "12.3 UStG", forcesZeroTaxPercent: true },
|
||||
],
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accounting.outgoing_documents.list",
|
||||
title: "Ausgangsbelege auflisten",
|
||||
description: "Listet Ausgangsbelege des aktiven Mandanten mit optionalen Filtern.",
|
||||
requiredPermissions: ["accounting.outgoing_documents.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
type: { type: "string", description: "Belegart, z. B. invoices, quotes oder deliveryNotes." },
|
||||
state: { type: "string", description: "Optionaler Statusfilter, z. B. Entwurf oder Gebucht." },
|
||||
customer: { type: "number" },
|
||||
project: { type: "number" },
|
||||
includeArchived: { type: "boolean", default: false },
|
||||
limit: { type: "number", minimum: 1, maximum: 100 },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const conditions = [eq(createddocuments.tenant, context.tenantId)]
|
||||
const type = stringArg(args, "type")
|
||||
const state = stringArg(args, "state")
|
||||
const customer = numberArg(args, "customer")
|
||||
const project = numberArg(args, "project")
|
||||
|
||||
if (type) {
|
||||
if (!allowedOutgoingDocumentTypes.has(type)) throw new Error(`Ungültige Belegart: ${type}`)
|
||||
conditions.push(eq(createddocuments.type, type))
|
||||
}
|
||||
if (state) conditions.push(eq(createddocuments.state, state))
|
||||
if (customer) conditions.push(eq(createddocuments.customer, customer))
|
||||
if (project) conditions.push(eq(createddocuments.project, project))
|
||||
if (args.includeArchived !== true) conditions.push(eq(createddocuments.archived, false))
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(createddocuments)
|
||||
.where(and(...conditions))
|
||||
.orderBy(desc(createddocuments.createdAt))
|
||||
.limit(limitFromArgs(args))
|
||||
|
||||
return { rows }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accounting.outgoing_documents.get",
|
||||
title: "Ausgangsbeleg laden",
|
||||
description: "Lädt einen Ausgangsbeleg des aktiven Mandanten anhand seiner ID.",
|
||||
requiredPermissions: ["accounting.outgoing_documents.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: {
|
||||
id: { type: "number" },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = numberArg(args, "id")
|
||||
if (!id) throw new Error("id ist erforderlich")
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(createddocuments)
|
||||
.where(and(eq(createddocuments.id, id), eq(createddocuments.tenant, context.tenantId)))
|
||||
.limit(1)
|
||||
|
||||
if (!rows[0]) throw new Error("Ausgangsbeleg nicht gefunden")
|
||||
return { document: rows[0] }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accounting.outgoing_documents.create",
|
||||
title: "Ausgangsbeleg erstellen",
|
||||
description: "Erstellt einen Ausgangsbeleg-Entwurf im aktiven Mandanten. Belegnummern werden erst beim Finalisieren vergeben.",
|
||||
requiredPermissions: ["accounting.outgoing_documents.write"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["type"],
|
||||
properties: {
|
||||
type: { type: "string" },
|
||||
customer: { type: "number" },
|
||||
contact: { type: "number" },
|
||||
contract: { type: "number" },
|
||||
project: { type: "number" },
|
||||
plant: { type: "number" },
|
||||
documentDate: { type: "string" },
|
||||
deliveryDate: { type: "string" },
|
||||
deliveryDateEnd: { type: "string" },
|
||||
deliveryDateType: { type: "string" },
|
||||
paymentDays: { type: "number" },
|
||||
payment_type: { type: "string" },
|
||||
taxType: {
|
||||
type: "string",
|
||||
enum: ["Standard", "13b UStG", "19 UStG", "12.3 UStG"],
|
||||
description: "Steuertyp des gesamten Ausgangsdokuments, nicht der einzelne USt-Satz einer Position.",
|
||||
},
|
||||
title: { type: "string" },
|
||||
description: { type: "string" },
|
||||
startText: { type: "string" },
|
||||
endText: { type: "string" },
|
||||
address: { type: "object" },
|
||||
rows: { type: "array" },
|
||||
letterhead: { type: "number" },
|
||||
availableInPortal: { type: "boolean" },
|
||||
customSurchargePercentage: { type: "number" },
|
||||
report: { type: "object" },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
assertNoManualDocumentNumber(args)
|
||||
const payload = buildOutgoingDocumentPayload(args, context.userId, context.tenantId, true)
|
||||
payload.state = "Entwurf"
|
||||
applyOutgoingDocumentTaxType(payload, args, String(payload.type))
|
||||
|
||||
const [created] = await context.server.db
|
||||
.insert(createddocuments)
|
||||
.values(payload as any)
|
||||
.returning()
|
||||
|
||||
return { document: created }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accounting.outgoing_documents.update",
|
||||
title: "Ausgangsbeleg aktualisieren",
|
||||
description: "Aktualisiert einen Ausgangsbeleg im aktiven Mandanten, solange er noch nicht finalisiert ist.",
|
||||
requiredPermissions: ["accounting.outgoing_documents.write"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: {
|
||||
id: { type: "number" },
|
||||
type: { type: "string" },
|
||||
state: { type: "string" },
|
||||
customer: { type: "number" },
|
||||
contact: { type: "number" },
|
||||
contract: { type: "number" },
|
||||
project: { type: "number" },
|
||||
plant: { type: "number" },
|
||||
documentDate: { type: "string" },
|
||||
deliveryDate: { type: "string" },
|
||||
deliveryDateEnd: { type: "string" },
|
||||
deliveryDateType: { type: "string" },
|
||||
paymentDays: { type: "number" },
|
||||
payment_type: { type: "string" },
|
||||
taxType: {
|
||||
type: "string",
|
||||
enum: ["Standard", "13b UStG", "19 UStG", "12.3 UStG"],
|
||||
description: "Steuertyp des gesamten Ausgangsdokuments, nicht der einzelne USt-Satz einer Position.",
|
||||
},
|
||||
title: { type: "string" },
|
||||
description: { type: "string" },
|
||||
startText: { type: "string" },
|
||||
endText: { type: "string" },
|
||||
address: { type: "object" },
|
||||
rows: { type: "array" },
|
||||
letterhead: { type: "number" },
|
||||
availableInPortal: { type: "boolean" },
|
||||
customSurchargePercentage: { type: "number" },
|
||||
report: { type: "object" },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = numberArg(args, "id")
|
||||
if (!id) throw new Error("id ist erforderlich")
|
||||
assertNoManualDocumentNumber(args)
|
||||
|
||||
const [existing] = await context.server.db
|
||||
.select()
|
||||
.from(createddocuments)
|
||||
.where(and(eq(createddocuments.id, id), eq(createddocuments.tenant, context.tenantId)))
|
||||
.limit(1)
|
||||
|
||||
if (!existing) throw new Error("Ausgangsbeleg nicht gefunden")
|
||||
if (existing.state !== "Entwurf") throw new Error("Finalisierte Ausgangsbelege können nicht über update geändert werden")
|
||||
|
||||
const payload = buildOutgoingDocumentPayload(args, context.userId, context.tenantId)
|
||||
payload.state = "Entwurf"
|
||||
applyOutgoingDocumentTaxType(payload, args, String(payload.type || existing.type), existing.taxType, existing.rows)
|
||||
|
||||
const [updated] = await context.server.db
|
||||
.update(createddocuments)
|
||||
.set(payload as any)
|
||||
.where(and(eq(createddocuments.id, id), eq(createddocuments.tenant, context.tenantId)))
|
||||
.returning()
|
||||
|
||||
return { document: updated }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accounting.outgoing_documents.finalize",
|
||||
title: "Ausgangsbeleg finalisieren",
|
||||
description: "Finalisiert einen Ausgangsbeleg, setzt den Status auf Gebucht und vergibt dabei genau einmal eine Belegnummer.",
|
||||
requiredPermissions: ["accounting.outgoing_documents.write"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: {
|
||||
id: { type: "number" },
|
||||
documentDate: { type: "string" },
|
||||
deliveryDate: { type: "string" },
|
||||
deliveryDateEnd: { type: "string" },
|
||||
paymentDays: { type: "number" },
|
||||
payment_type: { type: "string" },
|
||||
taxType: {
|
||||
type: "string",
|
||||
enum: ["Standard", "13b UStG", "19 UStG", "12.3 UStG"],
|
||||
description: "Steuertyp des gesamten Ausgangsdokuments, nicht der einzelne USt-Satz einer Position.",
|
||||
},
|
||||
rows: { type: "array" },
|
||||
report: { type: "object" },
|
||||
availableInPortal: { type: "boolean" },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = numberArg(args, "id")
|
||||
if (!id) throw new Error("id ist erforderlich")
|
||||
assertNoManualDocumentNumber(args)
|
||||
|
||||
const [existing] = await context.server.db
|
||||
.select()
|
||||
.from(createddocuments)
|
||||
.where(and(eq(createddocuments.id, id), eq(createddocuments.tenant, context.tenantId)))
|
||||
.limit(1)
|
||||
|
||||
if (!existing) throw new Error("Ausgangsbeleg nicht gefunden")
|
||||
if (existing.documentNumber) throw new Error("Ausgangsbeleg wurde bereits finalisiert")
|
||||
|
||||
const payload = buildOutgoingDocumentPayload(args, context.userId, context.tenantId)
|
||||
const result = await useNextNumberRangeNumber(context.server, context.tenantId, existing.type)
|
||||
|
||||
payload.state = "Gebucht"
|
||||
payload.documentNumber = result.usedNumber
|
||||
applyOutgoingDocumentTaxType(payload, args, existing.type, existing.taxType, existing.rows)
|
||||
|
||||
const [updated] = await context.server.db
|
||||
.update(createddocuments)
|
||||
.set(payload as any)
|
||||
.where(and(eq(createddocuments.id, id), eq(createddocuments.tenant, context.tenantId)))
|
||||
.returning()
|
||||
|
||||
return { document: updated }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accounting.outgoing_documents.archive",
|
||||
title: "Ausgangsbeleg archivieren",
|
||||
description: "Archiviert einen Ausgangsbeleg im aktiven Mandanten.",
|
||||
requiredPermissions: ["accounting.outgoing_documents.write"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: {
|
||||
id: { type: "number" },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = numberArg(args, "id")
|
||||
if (!id) throw new Error("id ist erforderlich")
|
||||
|
||||
const [updated] = await context.server.db
|
||||
.update(createddocuments)
|
||||
.set({
|
||||
archived: true,
|
||||
updatedAt: new Date(),
|
||||
updatedBy: context.userId,
|
||||
})
|
||||
.where(and(eq(createddocuments.id, id), eq(createddocuments.tenant, context.tenantId)))
|
||||
.returning()
|
||||
|
||||
if (!updated) throw new Error("Ausgangsbeleg nicht gefunden")
|
||||
return { document: updated }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accounting.accounts.search",
|
||||
title: "Konten suchen",
|
||||
description: "Sucht Sachkonten im aktiven Kontenrahmen des Mandanten.",
|
||||
requiredPermissions: ["accounting.accounts.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
query: { type: "string", description: "Suchtext für Kontonummer, Bezeichnung oder Beschreibung." },
|
||||
limit: { type: "number", minimum: 1, maximum: 100 },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const tenantRows = await context.server.db.query.tenants.findMany({
|
||||
where: (tenant, { eq }) => eq(tenant.id, context.tenantId),
|
||||
columns: {
|
||||
accountChart: true,
|
||||
},
|
||||
limit: 1,
|
||||
})
|
||||
const accountChart = tenantRows[0]?.accountChart || "skr03"
|
||||
const query = stringArg(args, "query")
|
||||
const limit = limitFromArgs(args)
|
||||
|
||||
const whereCond = query
|
||||
? and(
|
||||
eq(accounts.accountChart, accountChart),
|
||||
or(
|
||||
ilike(accounts.number, `%${query}%`),
|
||||
ilike(accounts.label, `%${query}%`),
|
||||
ilike(accounts.description, `%${query}%`)
|
||||
)
|
||||
)
|
||||
: eq(accounts.accountChart, accountChart)
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(accounts)
|
||||
.where(whereCond)
|
||||
.orderBy(accounts.number)
|
||||
.limit(limit)
|
||||
|
||||
return { accountChart, rows }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accounting.incoming_invoices.list",
|
||||
title: "Eingangsrechnungen auflisten",
|
||||
description: "Listet Eingangsrechnungen des aktiven Mandanten.",
|
||||
requiredPermissions: ["accounting.incoming_invoices.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
state: { type: "string", description: "Optionaler Statusfilter." },
|
||||
paid: { type: "boolean", description: "Optionaler Zahlungsstatus." },
|
||||
includeArchived: { type: "boolean", default: false },
|
||||
limit: { type: "number", minimum: 1, maximum: 100 },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const conditions = [eq(incominginvoices.tenant, context.tenantId)]
|
||||
const state = stringArg(args, "state")
|
||||
|
||||
if (state) conditions.push(eq(incominginvoices.state, state))
|
||||
if (typeof args.paid === "boolean") conditions.push(eq(incominginvoices.paid, args.paid))
|
||||
if (args.includeArchived !== true) conditions.push(eq(incominginvoices.archived, false))
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(incominginvoices)
|
||||
.where(and(...conditions))
|
||||
.orderBy(desc(incominginvoices.createdAt))
|
||||
.limit(limitFromArgs(args))
|
||||
|
||||
return { rows }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accounting.incoming_invoices.get",
|
||||
title: "Eingangsrechnung laden",
|
||||
description: "Lädt eine Eingangsrechnung des aktiven Mandanten anhand ihrer ID.",
|
||||
requiredPermissions: ["accounting.incoming_invoices.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: {
|
||||
id: { type: "number" },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = numberArg(args, "id")
|
||||
if (!id) throw new Error("id ist erforderlich")
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(incominginvoices)
|
||||
.where(and(eq(incominginvoices.id, id), eq(incominginvoices.tenant, context.tenantId)))
|
||||
.limit(1)
|
||||
|
||||
if (!rows[0]) throw new Error("Eingangsrechnung nicht gefunden")
|
||||
return { invoice: rows[0] }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accounting.incoming_invoices.files.list",
|
||||
title: "Dateien eines Eingangsbelegs auflisten",
|
||||
description: "Listet Dateien, die mit einem Eingangsbeleg im aktiven Mandanten verknüpft sind.",
|
||||
requiredPermissions: ["accounting.incoming_invoices.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: {
|
||||
id: { type: "number" },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = numberArg(args, "id")
|
||||
if (!id) throw new Error("id ist erforderlich")
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(files)
|
||||
.where(and(
|
||||
eq(files.incominginvoice, id),
|
||||
eq(files.tenant, context.tenantId),
|
||||
eq(files.archived, false)
|
||||
))
|
||||
.orderBy(desc(files.createdAt))
|
||||
|
||||
return { rows }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accounting.incoming_invoices.validate",
|
||||
title: "Eingangsbeleg validieren",
|
||||
description: "Prüft einen Eingangsbeleg mit denselben Pflichtregeln wie die FEDEO-Oberfläche vor dem Buchen.",
|
||||
requiredPermissions: ["accounting.incoming_invoices.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: {
|
||||
id: { type: "number" },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = numberArg(args, "id")
|
||||
if (!id) throw new Error("id ist erforderlich")
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(incominginvoices)
|
||||
.where(and(eq(incominginvoices.id, id), eq(incominginvoices.tenant, context.tenantId)))
|
||||
.limit(1)
|
||||
|
||||
if (!rows[0]) throw new Error("Eingangsbeleg nicht gefunden")
|
||||
return validateIncomingInvoiceData(rows[0] as Record<string, any>)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accounting.incoming_invoices.create",
|
||||
title: "Eingangsbeleg erstellen",
|
||||
description: "Erstellt einen Eingangsbeleg-Entwurf im aktiven Mandanten.",
|
||||
requiredPermissions: ["accounting.incoming_invoices.write"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
vendor: { type: "number" },
|
||||
reference: { type: "string" },
|
||||
date: { type: "string" },
|
||||
dueDate: { type: "string" },
|
||||
document: { type: "number" },
|
||||
description: { type: "string" },
|
||||
paymentType: { type: "string" },
|
||||
accounts: { type: "array" },
|
||||
expense: { type: "boolean" },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const payload = buildIncomingInvoicePayload(args, context.userId, context.tenantId, true)
|
||||
payload.state = "Entwurf"
|
||||
|
||||
const [created] = await context.server.db
|
||||
.insert(incominginvoices)
|
||||
.values(payload as any)
|
||||
.returning()
|
||||
|
||||
return { invoice: created, validation: validateIncomingInvoiceData(created as Record<string, any>) }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accounting.incoming_invoices.update",
|
||||
title: "Eingangsbeleg bearbeiten",
|
||||
description: "Bearbeitet einen noch nicht gebuchten Eingangsbeleg im aktiven Mandanten.",
|
||||
requiredPermissions: ["accounting.incoming_invoices.write"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: {
|
||||
id: { type: "number" },
|
||||
vendor: { type: "number" },
|
||||
reference: { type: "string" },
|
||||
date: { type: "string" },
|
||||
dueDate: { type: "string" },
|
||||
document: { type: "number" },
|
||||
description: { type: "string" },
|
||||
paymentType: { type: "string" },
|
||||
accounts: { type: "array" },
|
||||
expense: { type: "boolean" },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = numberArg(args, "id")
|
||||
if (!id) throw new Error("id ist erforderlich")
|
||||
|
||||
const [existing] = await context.server.db
|
||||
.select()
|
||||
.from(incominginvoices)
|
||||
.where(and(eq(incominginvoices.id, id), eq(incominginvoices.tenant, context.tenantId)))
|
||||
.limit(1)
|
||||
|
||||
if (!existing) throw new Error("Eingangsbeleg nicht gefunden")
|
||||
if (existing.state === "Gebucht") throw new Error("Gebuchte Eingangsbelege können nicht über update geändert werden")
|
||||
|
||||
const payload = buildIncomingInvoicePayload(args, context.userId, context.tenantId)
|
||||
payload.state = existing.state || "Entwurf"
|
||||
|
||||
const [updated] = await context.server.db
|
||||
.update(incominginvoices)
|
||||
.set(payload as any)
|
||||
.where(and(eq(incominginvoices.id, id), eq(incominginvoices.tenant, context.tenantId)))
|
||||
.returning()
|
||||
|
||||
return { invoice: updated, validation: validateIncomingInvoiceData(updated as Record<string, any>) }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accounting.incoming_invoices.book",
|
||||
title: "Eingangsbeleg buchen",
|
||||
description: "Validiert und bucht einen vorbereiteten oder als Entwurf gespeicherten Eingangsbeleg.",
|
||||
requiredPermissions: ["accounting.incoming_invoices.write"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: {
|
||||
id: { type: "number" },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = numberArg(args, "id")
|
||||
if (!id) throw new Error("id ist erforderlich")
|
||||
|
||||
const [existing] = await context.server.db
|
||||
.select()
|
||||
.from(incominginvoices)
|
||||
.where(and(eq(incominginvoices.id, id), eq(incominginvoices.tenant, context.tenantId)))
|
||||
.limit(1)
|
||||
|
||||
if (!existing) throw new Error("Eingangsbeleg nicht gefunden")
|
||||
if (existing.state === "Gebucht") return { invoice: existing, validation: validateIncomingInvoiceData(existing as Record<string, any>) }
|
||||
|
||||
const validation = validateIncomingInvoiceData(existing as Record<string, any>)
|
||||
if (!validation.valid) {
|
||||
return {
|
||||
booked: false,
|
||||
invoice: existing,
|
||||
validation,
|
||||
}
|
||||
}
|
||||
|
||||
const [updated] = await context.server.db
|
||||
.update(incominginvoices)
|
||||
.set({
|
||||
state: "Gebucht",
|
||||
updatedAt: new Date(),
|
||||
updatedBy: context.userId,
|
||||
})
|
||||
.where(and(eq(incominginvoices.id, id), eq(incominginvoices.tenant, context.tenantId)))
|
||||
.returning()
|
||||
|
||||
return {
|
||||
booked: true,
|
||||
invoice: updated,
|
||||
validation: validateIncomingInvoiceData(updated as Record<string, any>),
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accounting.incoming_invoices.archive",
|
||||
title: "Eingangsbeleg archivieren",
|
||||
description: "Archiviert einen Eingangsbeleg im aktiven Mandanten.",
|
||||
requiredPermissions: ["accounting.incoming_invoices.write"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: {
|
||||
id: { type: "number" },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = numberArg(args, "id")
|
||||
if (!id) throw new Error("id ist erforderlich")
|
||||
|
||||
const [updated] = await context.server.db
|
||||
.update(incominginvoices)
|
||||
.set({
|
||||
archived: true,
|
||||
updatedAt: new Date(),
|
||||
updatedBy: context.userId,
|
||||
})
|
||||
.where(and(eq(incominginvoices.id, id), eq(incominginvoices.tenant, context.tenantId)))
|
||||
.returning()
|
||||
|
||||
if (!updated) throw new Error("Eingangsbeleg nicht gefunden")
|
||||
return { invoice: updated }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accounting.bank_statements.list",
|
||||
title: "Bankumsätze auflisten",
|
||||
description: "Listet Bankumsätze des aktiven Mandanten.",
|
||||
requiredPermissions: ["accounting.bank.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
account: { type: "number", description: "Optionale Bankkonto-ID." },
|
||||
includeArchived: { type: "boolean", default: false },
|
||||
limit: { type: "number", minimum: 1, maximum: 100 },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const conditions = [eq(bankstatements.tenant, context.tenantId)]
|
||||
const account = numberArg(args, "account")
|
||||
|
||||
if (account) conditions.push(eq(bankstatements.account, account))
|
||||
if (args.includeArchived !== true) conditions.push(eq(bankstatements.archived, false))
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(bankstatements)
|
||||
.where(and(...conditions))
|
||||
.orderBy(desc(bankstatements.date), desc(bankstatements.id))
|
||||
.limit(limitFromArgs(args))
|
||||
|
||||
return { rows }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accounting.bank_statements.get",
|
||||
title: "Bankumsatz laden",
|
||||
description: "Lädt einen Bankumsatz des aktiven Mandanten anhand seiner ID.",
|
||||
requiredPermissions: ["accounting.bank.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: {
|
||||
id: { type: "number" },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = numberArg(args, "id")
|
||||
if (!id) throw new Error("id ist erforderlich")
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(bankstatements)
|
||||
.where(and(eq(bankstatements.id, id), eq(bankstatements.tenant, context.tenantId)))
|
||||
.limit(1)
|
||||
|
||||
if (!rows[0]) throw new Error("Bankumsatz nicht gefunden")
|
||||
return { bankStatement: rows[0] }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accounting.statement_allocations.list",
|
||||
title: "Buchungszuordnungen auflisten",
|
||||
description: "Listet Buchungszuordnungen des aktiven Mandanten.",
|
||||
requiredPermissions: ["accounting.statement_allocations.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
bankstatement: { type: "number" },
|
||||
incominginvoice: { type: "number" },
|
||||
includeArchived: { type: "boolean", default: false },
|
||||
limit: { type: "number", minimum: 1, maximum: 100 },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const conditions = [eq(statementallocations.tenant, context.tenantId)]
|
||||
const bankstatement = numberArg(args, "bankstatement")
|
||||
const incominginvoice = numberArg(args, "incominginvoice")
|
||||
|
||||
if (bankstatement) conditions.push(eq(statementallocations.bankstatement, bankstatement))
|
||||
if (incominginvoice) conditions.push(eq(statementallocations.incominginvoice, incominginvoice))
|
||||
if (args.includeArchived !== true) conditions.push(eq(statementallocations.archived, false))
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(statementallocations)
|
||||
.where(and(...conditions))
|
||||
.orderBy(desc(statementallocations.created_at))
|
||||
.limit(limitFromArgs(args))
|
||||
|
||||
return { rows }
|
||||
},
|
||||
},
|
||||
]
|
||||
571
backend/src/mcp/tools/masterdata.ts
Normal file
571
backend/src/mcp/tools/masterdata.ts
Normal file
@@ -0,0 +1,571 @@
|
||||
import { and, desc, eq, ilike, or } from "drizzle-orm"
|
||||
import {
|
||||
branches,
|
||||
contacts,
|
||||
costcentres,
|
||||
customers,
|
||||
inventoryitems,
|
||||
products,
|
||||
services,
|
||||
teams,
|
||||
units,
|
||||
vehicles,
|
||||
vendors,
|
||||
} from "../../../db/schema"
|
||||
import { McpTool } from "../types"
|
||||
|
||||
const limitFromArgs = (args: Record<string, unknown>, fallback = 25) => {
|
||||
const raw = Number(args.limit ?? fallback)
|
||||
if (!Number.isFinite(raw)) return fallback
|
||||
return Math.min(Math.max(Math.trunc(raw), 1), 100)
|
||||
}
|
||||
|
||||
const stringArg = (args: Record<string, unknown>, key: string) => {
|
||||
const value = args[key]
|
||||
return typeof value === "string" && value.trim() ? value.trim() : null
|
||||
}
|
||||
|
||||
const numberArg = (args: Record<string, unknown>, key: string) => {
|
||||
const value = Number(args[key])
|
||||
return Number.isFinite(value) ? value : null
|
||||
}
|
||||
|
||||
const uuidArg = (args: Record<string, unknown>, key: string) => {
|
||||
const value = stringArg(args, key)
|
||||
return value && /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value)
|
||||
? value
|
||||
: null
|
||||
}
|
||||
|
||||
export const masterdataTools: McpTool[] = [
|
||||
{
|
||||
name: "masterdata.customers.get",
|
||||
title: "Kunde laden",
|
||||
description: "Lädt einen Kunden des aktiven Mandanten anhand seiner ID.",
|
||||
requiredPermissions: ["masterdata.customers.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: { id: { type: "number" } },
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = numberArg(args, "id")
|
||||
if (!id) throw new Error("id ist erforderlich")
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(customers)
|
||||
.where(and(eq(customers.id, id), eq(customers.tenant, context.tenantId)))
|
||||
.limit(1)
|
||||
|
||||
if (!rows[0]) throw new Error("Kunde nicht gefunden")
|
||||
return { customer: rows[0] }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "masterdata.vendors.search",
|
||||
title: "Lieferanten suchen",
|
||||
description: "Sucht Lieferanten des aktiven Mandanten.",
|
||||
requiredPermissions: ["masterdata.vendors.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
query: { type: "string", description: "Suchtext für Name, Lieferantennummer oder Notizen." },
|
||||
includeArchived: { type: "boolean", default: false },
|
||||
limit: { type: "number", minimum: 1, maximum: 100 },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const conditions = [eq(vendors.tenant, context.tenantId)]
|
||||
const query = stringArg(args, "query")
|
||||
|
||||
if (query) {
|
||||
conditions.push(or(
|
||||
ilike(vendors.name, `%${query}%`),
|
||||
ilike(vendors.vendorNumber, `%${query}%`),
|
||||
ilike(vendors.notes, `%${query}%`)
|
||||
))
|
||||
}
|
||||
if (args.includeArchived !== true) conditions.push(eq(vendors.archived, false))
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(vendors)
|
||||
.where(and(...conditions))
|
||||
.orderBy(vendors.name)
|
||||
.limit(limitFromArgs(args))
|
||||
|
||||
return { rows }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "masterdata.vendors.get",
|
||||
title: "Lieferant laden",
|
||||
description: "Lädt einen Lieferanten des aktiven Mandanten anhand seiner ID.",
|
||||
requiredPermissions: ["masterdata.vendors.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: { id: { type: "number" } },
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = numberArg(args, "id")
|
||||
if (!id) throw new Error("id ist erforderlich")
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(vendors)
|
||||
.where(and(eq(vendors.id, id), eq(vendors.tenant, context.tenantId)))
|
||||
.limit(1)
|
||||
|
||||
if (!rows[0]) throw new Error("Lieferant nicht gefunden")
|
||||
return { vendor: rows[0] }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "masterdata.contacts.search",
|
||||
title: "Kontakte suchen",
|
||||
description: "Sucht Kontakte des aktiven Mandanten, optional zu Kunde oder Lieferant.",
|
||||
requiredPermissions: ["masterdata.contacts.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
query: { type: "string", description: "Suchtext für Name, E-Mail, Telefon, Rolle oder Notizen." },
|
||||
customer: { type: "number" },
|
||||
vendor: { type: "number" },
|
||||
includeArchived: { type: "boolean", default: false },
|
||||
limit: { type: "number", minimum: 1, maximum: 100 },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const conditions = [eq(contacts.tenant, context.tenantId)]
|
||||
const query = stringArg(args, "query")
|
||||
const customer = numberArg(args, "customer")
|
||||
const vendor = numberArg(args, "vendor")
|
||||
|
||||
if (query) {
|
||||
conditions.push(or(
|
||||
ilike(contacts.fullName, `%${query}%`),
|
||||
ilike(contacts.firstName, `%${query}%`),
|
||||
ilike(contacts.lastName, `%${query}%`),
|
||||
ilike(contacts.email, `%${query}%`),
|
||||
ilike(contacts.phoneMobile, `%${query}%`),
|
||||
ilike(contacts.phoneHome, `%${query}%`),
|
||||
ilike(contacts.role, `%${query}%`),
|
||||
ilike(contacts.notes, `%${query}%`)
|
||||
))
|
||||
}
|
||||
if (customer) conditions.push(eq(contacts.customer, customer))
|
||||
if (vendor) conditions.push(eq(contacts.vendor, vendor))
|
||||
if (args.includeArchived !== true) conditions.push(eq(contacts.archived, false))
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(contacts)
|
||||
.where(and(...conditions))
|
||||
.orderBy(contacts.fullName)
|
||||
.limit(limitFromArgs(args))
|
||||
|
||||
return { rows }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "masterdata.products.search",
|
||||
title: "Artikel suchen",
|
||||
description: "Sucht Artikel und Materialstammdaten des aktiven Mandanten.",
|
||||
requiredPermissions: ["masterdata.products.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
query: { type: "string", description: "Suchtext für Name, Artikelnummer, Hersteller, EAN, Barcode oder Beschreibung." },
|
||||
includeArchived: { type: "boolean", default: false },
|
||||
limit: { type: "number", minimum: 1, maximum: 100 },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const conditions = [eq(products.tenant, context.tenantId)]
|
||||
const query = stringArg(args, "query")
|
||||
|
||||
if (query) {
|
||||
conditions.push(or(
|
||||
ilike(products.name, `%${query}%`),
|
||||
ilike(products.article_number, `%${query}%`),
|
||||
ilike(products.manufacturer, `%${query}%`),
|
||||
ilike(products.manufacturer_number, `%${query}%`),
|
||||
ilike(products.ean, `%${query}%`),
|
||||
ilike(products.barcode, `%${query}%`),
|
||||
ilike(products.description, `%${query}%`)
|
||||
))
|
||||
}
|
||||
if (args.includeArchived !== true) conditions.push(eq(products.archived, false))
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(products)
|
||||
.where(and(...conditions))
|
||||
.orderBy(products.name)
|
||||
.limit(limitFromArgs(args))
|
||||
|
||||
return { rows }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "masterdata.products.get",
|
||||
title: "Artikel laden",
|
||||
description: "Lädt einen Artikel des aktiven Mandanten anhand seiner ID.",
|
||||
requiredPermissions: ["masterdata.products.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: { id: { type: "number" } },
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = numberArg(args, "id")
|
||||
if (!id) throw new Error("id ist erforderlich")
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(products)
|
||||
.where(and(eq(products.id, id), eq(products.tenant, context.tenantId)))
|
||||
.limit(1)
|
||||
|
||||
if (!rows[0]) throw new Error("Artikel nicht gefunden")
|
||||
return { product: rows[0] }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "masterdata.services.search",
|
||||
title: "Leistungen suchen",
|
||||
description: "Sucht Leistungsstammdaten des aktiven Mandanten.",
|
||||
requiredPermissions: ["masterdata.services.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
query: { type: "string", description: "Suchtext für Name, Leistungsnummer oder Beschreibung." },
|
||||
includeArchived: { type: "boolean", default: false },
|
||||
limit: { type: "number", minimum: 1, maximum: 100 },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const conditions = [eq(services.tenant, context.tenantId)]
|
||||
const query = stringArg(args, "query")
|
||||
|
||||
if (query) {
|
||||
conditions.push(or(
|
||||
ilike(services.name, `%${query}%`),
|
||||
ilike(services.description, `%${query}%`)
|
||||
))
|
||||
}
|
||||
if (args.includeArchived !== true) conditions.push(eq(services.archived, false))
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(services)
|
||||
.where(and(...conditions))
|
||||
.orderBy(services.name)
|
||||
.limit(limitFromArgs(args))
|
||||
|
||||
return { rows }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "masterdata.services.get",
|
||||
title: "Leistung laden",
|
||||
description: "Lädt eine Leistung des aktiven Mandanten anhand ihrer ID.",
|
||||
requiredPermissions: ["masterdata.services.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: { id: { type: "number" } },
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = numberArg(args, "id")
|
||||
if (!id) throw new Error("id ist erforderlich")
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(services)
|
||||
.where(and(eq(services.id, id), eq(services.tenant, context.tenantId)))
|
||||
.limit(1)
|
||||
|
||||
if (!rows[0]) throw new Error("Leistung nicht gefunden")
|
||||
return { service: rows[0] }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "masterdata.cost_centres.list",
|
||||
title: "Kostenstellen auflisten",
|
||||
description: "Listet Kostenstellen des aktiven Mandanten.",
|
||||
requiredPermissions: ["masterdata.cost_centres.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
query: { type: "string", description: "Suchtext für Nummer, Name oder Beschreibung." },
|
||||
branch: { type: "number" },
|
||||
project: { type: "number" },
|
||||
includeArchived: { type: "boolean", default: false },
|
||||
limit: { type: "number", minimum: 1, maximum: 100 },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const conditions = [eq(costcentres.tenant, context.tenantId)]
|
||||
const query = stringArg(args, "query")
|
||||
const branch = numberArg(args, "branch")
|
||||
const project = numberArg(args, "project")
|
||||
|
||||
if (query) {
|
||||
conditions.push(or(
|
||||
ilike(costcentres.number, `%${query}%`),
|
||||
ilike(costcentres.name, `%${query}%`),
|
||||
ilike(costcentres.description, `%${query}%`)
|
||||
))
|
||||
}
|
||||
if (branch) conditions.push(eq(costcentres.branch, branch))
|
||||
if (project) conditions.push(eq(costcentres.project, project))
|
||||
if (args.includeArchived !== true) conditions.push(eq(costcentres.archived, false))
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(costcentres)
|
||||
.where(and(...conditions))
|
||||
.orderBy(costcentres.number)
|
||||
.limit(limitFromArgs(args))
|
||||
|
||||
return { rows }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "masterdata.cost_centres.get",
|
||||
title: "Kostenstelle laden",
|
||||
description: "Lädt eine Kostenstelle des aktiven Mandanten anhand ihrer UUID.",
|
||||
requiredPermissions: ["masterdata.cost_centres.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: { id: { type: "string" } },
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = uuidArg(args, "id")
|
||||
if (!id) throw new Error("gültige id ist erforderlich")
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(costcentres)
|
||||
.where(and(eq(costcentres.id, id), eq(costcentres.tenant, context.tenantId)))
|
||||
.limit(1)
|
||||
|
||||
if (!rows[0]) throw new Error("Kostenstelle nicht gefunden")
|
||||
return { costCentre: rows[0] }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "masterdata.branches.list",
|
||||
title: "Niederlassungen auflisten",
|
||||
description: "Listet Niederlassungen des aktiven Mandanten.",
|
||||
requiredPermissions: ["masterdata.branches.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
query: { type: "string", description: "Suchtext für Nummer, Name oder Beschreibung." },
|
||||
includeArchived: { type: "boolean", default: false },
|
||||
limit: { type: "number", minimum: 1, maximum: 100 },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const conditions = [eq(branches.tenant, context.tenantId)]
|
||||
const query = stringArg(args, "query")
|
||||
|
||||
if (query) {
|
||||
conditions.push(or(
|
||||
ilike(branches.number, `%${query}%`),
|
||||
ilike(branches.name, `%${query}%`),
|
||||
ilike(branches.description, `%${query}%`)
|
||||
))
|
||||
}
|
||||
if (args.includeArchived !== true) conditions.push(eq(branches.archived, false))
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(branches)
|
||||
.where(and(...conditions))
|
||||
.orderBy(branches.name)
|
||||
.limit(limitFromArgs(args))
|
||||
|
||||
return { rows }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "masterdata.teams.list",
|
||||
title: "Teams auflisten",
|
||||
description: "Listet Teams des aktiven Mandanten.",
|
||||
requiredPermissions: ["masterdata.teams.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
query: { type: "string", description: "Suchtext für Name oder Beschreibung." },
|
||||
branch: { type: "number" },
|
||||
includeArchived: { type: "boolean", default: false },
|
||||
limit: { type: "number", minimum: 1, maximum: 100 },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const conditions = [eq(teams.tenant, context.tenantId)]
|
||||
const query = stringArg(args, "query")
|
||||
const branch = numberArg(args, "branch")
|
||||
|
||||
if (query) {
|
||||
conditions.push(or(
|
||||
ilike(teams.name, `%${query}%`),
|
||||
ilike(teams.description, `%${query}%`)
|
||||
))
|
||||
}
|
||||
if (branch) conditions.push(eq(teams.branch, branch))
|
||||
if (args.includeArchived !== true) conditions.push(eq(teams.archived, false))
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(teams)
|
||||
.where(and(...conditions))
|
||||
.orderBy(teams.name)
|
||||
.limit(limitFromArgs(args))
|
||||
|
||||
return { rows }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "masterdata.vehicles.list",
|
||||
title: "Fahrzeuge auflisten",
|
||||
description: "Listet Fahrzeuge des aktiven Mandanten.",
|
||||
requiredPermissions: ["masterdata.vehicles.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
query: { type: "string", description: "Suchtext für Name, Kennzeichen, FIN oder Farbe." },
|
||||
includeArchived: { type: "boolean", default: false },
|
||||
limit: { type: "number", minimum: 1, maximum: 100 },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const conditions = [eq(vehicles.tenant, context.tenantId)]
|
||||
const query = stringArg(args, "query")
|
||||
|
||||
if (query) {
|
||||
conditions.push(or(
|
||||
ilike(vehicles.name, `%${query}%`),
|
||||
ilike(vehicles.license_plate, `%${query}%`),
|
||||
ilike(vehicles.vin, `%${query}%`),
|
||||
ilike(vehicles.color, `%${query}%`)
|
||||
))
|
||||
}
|
||||
if (args.includeArchived !== true) conditions.push(eq(vehicles.archived, false))
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(vehicles)
|
||||
.where(and(...conditions))
|
||||
.orderBy(vehicles.name)
|
||||
.limit(limitFromArgs(args))
|
||||
|
||||
return { rows }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "masterdata.inventory_items.search",
|
||||
title: "Inventar suchen",
|
||||
description: "Sucht Inventar- und Geräte-Stammdaten des aktiven Mandanten.",
|
||||
requiredPermissions: ["masterdata.inventory_items.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
query: { type: "string", description: "Suchtext für Name, Artikelnummer, Seriennummer, Hersteller oder Beschreibung." },
|
||||
vendor: { type: "number" },
|
||||
includeArchived: { type: "boolean", default: false },
|
||||
limit: { type: "number", minimum: 1, maximum: 100 },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const conditions = [eq(inventoryitems.tenant, context.tenantId)]
|
||||
const query = stringArg(args, "query")
|
||||
const vendor = numberArg(args, "vendor")
|
||||
|
||||
if (query) {
|
||||
conditions.push(or(
|
||||
ilike(inventoryitems.name, `%${query}%`),
|
||||
ilike(inventoryitems.articleNumber, `%${query}%`),
|
||||
ilike(inventoryitems.serialNumber, `%${query}%`),
|
||||
ilike(inventoryitems.manufacturer, `%${query}%`),
|
||||
ilike(inventoryitems.manufacturerNumber, `%${query}%`),
|
||||
ilike(inventoryitems.description, `%${query}%`)
|
||||
))
|
||||
}
|
||||
if (vendor) conditions.push(eq(inventoryitems.vendor, vendor))
|
||||
if (args.includeArchived !== true) conditions.push(eq(inventoryitems.archived, false))
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(inventoryitems)
|
||||
.where(and(...conditions))
|
||||
.orderBy(inventoryitems.name)
|
||||
.limit(limitFromArgs(args))
|
||||
|
||||
return { rows }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "masterdata.inventory_items.get",
|
||||
title: "Inventar laden",
|
||||
description: "Lädt einen Inventar- oder Geräte-Stammdatensatz anhand seiner ID.",
|
||||
requiredPermissions: ["masterdata.inventory_items.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: { id: { type: "number" } },
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = numberArg(args, "id")
|
||||
if (!id) throw new Error("id ist erforderlich")
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(inventoryitems)
|
||||
.where(and(eq(inventoryitems.id, id), eq(inventoryitems.tenant, context.tenantId)))
|
||||
.limit(1)
|
||||
|
||||
if (!rows[0]) throw new Error("Inventar nicht gefunden")
|
||||
return { inventoryItem: rows[0] }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "masterdata.units.list",
|
||||
title: "Einheiten auflisten",
|
||||
description: "Listet globale Mengeneinheiten.",
|
||||
requiredPermissions: ["masterdata.units.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
query: { type: "string", description: "Suchtext für Name, Singular, Plural oder Kürzel." },
|
||||
limit: { type: "number", minimum: 1, maximum: 100 },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const query = stringArg(args, "query")
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(units)
|
||||
.where(query
|
||||
? or(
|
||||
ilike(units.name, `%${query}%`),
|
||||
ilike(units.single, `%${query}%`),
|
||||
ilike(units.multiple, `%${query}%`),
|
||||
ilike(units.short, `%${query}%`)
|
||||
)
|
||||
: undefined)
|
||||
.orderBy(units.name)
|
||||
.limit(limitFromArgs(args))
|
||||
|
||||
return { rows }
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
407
backend/src/mcp/tools/organisation.ts
Normal file
407
backend/src/mcp/tools/organisation.ts
Normal file
@@ -0,0 +1,407 @@
|
||||
import { and, desc, eq, ilike, or } from "drizzle-orm"
|
||||
import { customers, events, plants, projects, tasks } from "../../../db/schema"
|
||||
import { McpTool } from "../types"
|
||||
|
||||
const limitFromArgs = (args: Record<string, unknown>, fallback = 25) => {
|
||||
const raw = Number(args.limit ?? fallback)
|
||||
if (!Number.isFinite(raw)) return fallback
|
||||
return Math.min(Math.max(Math.trunc(raw), 1), 100)
|
||||
}
|
||||
|
||||
const stringArg = (args: Record<string, unknown>, key: string) => {
|
||||
const value = args[key]
|
||||
return typeof value === "string" && value.trim() ? value.trim() : null
|
||||
}
|
||||
|
||||
const numberArg = (args: Record<string, unknown>, key: string) => {
|
||||
const value = Number(args[key])
|
||||
return Number.isFinite(value) ? value : null
|
||||
}
|
||||
|
||||
export const organisationTools: McpTool[] = [
|
||||
{
|
||||
name: "organisation.customers.search",
|
||||
title: "Kunden suchen",
|
||||
description: "Sucht aktive Kunden des aktiven Mandanten.",
|
||||
requiredPermissions: ["organisation.customers.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
query: { type: "string", description: "Suchtext für Name, Kundennummer, Vorname, Nachname oder Notizen." },
|
||||
includeArchived: { type: "boolean", default: false },
|
||||
limit: { type: "number", minimum: 1, maximum: 100 },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const conditions = [eq(customers.tenant, context.tenantId)]
|
||||
const query = stringArg(args, "query")
|
||||
|
||||
if (query) {
|
||||
conditions.push(or(
|
||||
ilike(customers.name, `%${query}%`),
|
||||
ilike(customers.customerNumber, `%${query}%`),
|
||||
ilike(customers.firstname, `%${query}%`),
|
||||
ilike(customers.lastname, `%${query}%`),
|
||||
ilike(customers.notes, `%${query}%`)
|
||||
))
|
||||
}
|
||||
if (args.includeArchived !== true) conditions.push(eq(customers.archived, false))
|
||||
|
||||
const rows = await context.server.db
|
||||
.select({
|
||||
id: customers.id,
|
||||
customerNumber: customers.customerNumber,
|
||||
name: customers.name,
|
||||
firstname: customers.firstname,
|
||||
lastname: customers.lastname,
|
||||
type: customers.type,
|
||||
isCompany: customers.isCompany,
|
||||
active: customers.active,
|
||||
archived: customers.archived,
|
||||
})
|
||||
.from(customers)
|
||||
.where(and(...conditions))
|
||||
.orderBy(customers.name)
|
||||
.limit(limitFromArgs(args))
|
||||
|
||||
return { rows }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "organisation.projects.list",
|
||||
title: "Projekte auflisten",
|
||||
description: "Listet Projekte des aktiven Mandanten mit optionalen Filtern.",
|
||||
requiredPermissions: ["organisation.projects.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
query: { type: "string", description: "Suchtext für Name, Projektnummer, Kundenreferenz oder Notizen." },
|
||||
customer: { type: "number" },
|
||||
activePhase: { type: "string" },
|
||||
includeArchived: { type: "boolean", default: false },
|
||||
limit: { type: "number", minimum: 1, maximum: 100 },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const conditions = [eq(projects.tenant, context.tenantId)]
|
||||
const query = stringArg(args, "query")
|
||||
const customer = numberArg(args, "customer")
|
||||
const activePhase = stringArg(args, "activePhase")
|
||||
|
||||
if (query) {
|
||||
conditions.push(or(
|
||||
ilike(projects.name, `%${query}%`),
|
||||
ilike(projects.projectNumber, `%${query}%`),
|
||||
ilike(projects.customerRef, `%${query}%`),
|
||||
ilike(projects.notes, `%${query}%`)
|
||||
))
|
||||
}
|
||||
if (customer) conditions.push(eq(projects.customer, customer))
|
||||
if (activePhase) conditions.push(eq(projects.active_phase, activePhase))
|
||||
if (args.includeArchived !== true) conditions.push(eq(projects.archived, false))
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(projects)
|
||||
.where(and(...conditions))
|
||||
.orderBy(desc(projects.createdAt))
|
||||
.limit(limitFromArgs(args))
|
||||
|
||||
return { rows }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "organisation.projects.get",
|
||||
title: "Projekt laden",
|
||||
description: "Lädt ein Projekt des aktiven Mandanten anhand seiner ID.",
|
||||
requiredPermissions: ["organisation.projects.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: {
|
||||
id: { type: "number" },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = numberArg(args, "id")
|
||||
if (!id) throw new Error("id ist erforderlich")
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(projects)
|
||||
.where(and(eq(projects.id, id), eq(projects.tenant, context.tenantId)))
|
||||
.limit(1)
|
||||
|
||||
if (!rows[0]) throw new Error("Projekt nicht gefunden")
|
||||
return { project: rows[0] }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "organisation.plants.list",
|
||||
title: "Anlagen auflisten",
|
||||
description: "Listet Anlagen des aktiven Mandanten mit optionalem Kundenfilter.",
|
||||
requiredPermissions: ["organisation.plants.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
query: { type: "string", description: "Suchtext für Name." },
|
||||
customer: { type: "number" },
|
||||
includeArchived: { type: "boolean", default: false },
|
||||
limit: { type: "number", minimum: 1, maximum: 100 },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const conditions = [eq(plants.tenant, context.tenantId)]
|
||||
const query = stringArg(args, "query")
|
||||
const customer = numberArg(args, "customer")
|
||||
|
||||
if (query) conditions.push(ilike(plants.name, `%${query}%`))
|
||||
if (customer) conditions.push(eq(plants.customer, customer))
|
||||
if (args.includeArchived !== true) conditions.push(eq(plants.archived, false))
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(plants)
|
||||
.where(and(...conditions))
|
||||
.orderBy(plants.name)
|
||||
.limit(limitFromArgs(args))
|
||||
|
||||
return { rows }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "organisation.events.list",
|
||||
title: "Termine auflisten",
|
||||
description: "Listet Termine des aktiven Mandanten mit optionalen Projekt- oder Kundenfiltern.",
|
||||
requiredPermissions: ["organisation.events.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
query: { type: "string", description: "Suchtext für Name, Notizen oder Link." },
|
||||
project: { type: "number" },
|
||||
customer: { type: "number" },
|
||||
eventtype: { type: "string" },
|
||||
includeArchived: { type: "boolean", default: false },
|
||||
limit: { type: "number", minimum: 1, maximum: 100 },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const conditions = [eq(events.tenant, context.tenantId)]
|
||||
const query = stringArg(args, "query")
|
||||
const project = numberArg(args, "project")
|
||||
const customer = numberArg(args, "customer")
|
||||
const eventtype = stringArg(args, "eventtype")
|
||||
|
||||
if (query) {
|
||||
conditions.push(or(
|
||||
ilike(events.name, `%${query}%`),
|
||||
ilike(events.notes, `%${query}%`),
|
||||
ilike(events.link, `%${query}%`)
|
||||
))
|
||||
}
|
||||
if (project) conditions.push(eq(events.project, project))
|
||||
if (customer) conditions.push(eq(events.customer, customer))
|
||||
if (eventtype) conditions.push(eq(events.eventtype, eventtype))
|
||||
if (args.includeArchived !== true) conditions.push(eq(events.archived, false))
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(events)
|
||||
.where(and(...conditions))
|
||||
.orderBy(desc(events.startDate))
|
||||
.limit(limitFromArgs(args))
|
||||
|
||||
return { rows }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "organisation.tasks.list",
|
||||
title: "Aufgaben auflisten",
|
||||
description: "Listet Aufgaben des aktiven Mandanten mit optionalen Filtern.",
|
||||
requiredPermissions: ["organisation.tasks.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
properties: {
|
||||
query: { type: "string", description: "Suchtext für Name, Beschreibung oder Kategorie." },
|
||||
project: { type: "number" },
|
||||
customer: { type: "number" },
|
||||
includeArchived: { type: "boolean", default: false },
|
||||
limit: { type: "number", minimum: 1, maximum: 100 },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const conditions = [eq(tasks.tenant, context.tenantId)]
|
||||
const query = stringArg(args, "query")
|
||||
const project = numberArg(args, "project")
|
||||
const customer = numberArg(args, "customer")
|
||||
|
||||
if (query) {
|
||||
conditions.push(or(
|
||||
ilike(tasks.name, `%${query}%`),
|
||||
ilike(tasks.description, `%${query}%`),
|
||||
ilike(tasks.categorie, `%${query}%`)
|
||||
))
|
||||
}
|
||||
if (project) conditions.push(eq(tasks.project, project))
|
||||
if (customer) conditions.push(eq(tasks.customer, customer))
|
||||
if (args.includeArchived !== true) conditions.push(eq(tasks.archived, false))
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(tasks)
|
||||
.where(and(...conditions))
|
||||
.orderBy(desc(tasks.createdAt))
|
||||
.limit(limitFromArgs(args))
|
||||
|
||||
return { rows }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "organisation.tasks.get",
|
||||
title: "Aufgabe laden",
|
||||
description: "Lädt eine Aufgabe des aktiven Mandanten anhand ihrer ID.",
|
||||
requiredPermissions: ["organisation.tasks.read"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: {
|
||||
id: { type: "number" },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = numberArg(args, "id")
|
||||
if (!id) throw new Error("id ist erforderlich")
|
||||
|
||||
const rows = await context.server.db
|
||||
.select()
|
||||
.from(tasks)
|
||||
.where(and(eq(tasks.id, id), eq(tasks.tenant, context.tenantId)))
|
||||
.limit(1)
|
||||
|
||||
if (!rows[0]) throw new Error("Aufgabe nicht gefunden")
|
||||
return { task: rows[0] }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "organisation.tasks.create",
|
||||
title: "Aufgabe erstellen",
|
||||
description: "Erstellt eine neue Aufgabe im aktiven Mandanten.",
|
||||
requiredPermissions: ["organisation.tasks.write"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["name"],
|
||||
properties: {
|
||||
name: { type: "string" },
|
||||
description: { type: "string" },
|
||||
categorie: { type: "string" },
|
||||
project: { type: "number" },
|
||||
plant: { type: "number" },
|
||||
customer: { type: "number" },
|
||||
userId: { type: "string" },
|
||||
profiles: { type: "array", items: { type: "string" } },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const name = stringArg(args, "name")
|
||||
if (!name) throw new Error("name ist erforderlich")
|
||||
|
||||
const [created] = await context.server.db
|
||||
.insert(tasks)
|
||||
.values({
|
||||
name,
|
||||
description: stringArg(args, "description"),
|
||||
categorie: stringArg(args, "categorie"),
|
||||
tenant: context.tenantId,
|
||||
userId: stringArg(args, "userId") || context.userId,
|
||||
project: numberArg(args, "project"),
|
||||
plant: numberArg(args, "plant"),
|
||||
customer: numberArg(args, "customer"),
|
||||
profiles: Array.isArray(args.profiles) ? args.profiles : [],
|
||||
updatedAt: new Date(),
|
||||
updatedBy: context.userId,
|
||||
})
|
||||
.returning()
|
||||
|
||||
return { task: created }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "organisation.tasks.update",
|
||||
title: "Aufgabe aktualisieren",
|
||||
description: "Aktualisiert Felder einer Aufgabe im aktiven Mandanten.",
|
||||
requiredPermissions: ["organisation.tasks.write"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: {
|
||||
id: { type: "number" },
|
||||
name: { type: "string" },
|
||||
description: { type: "string" },
|
||||
categorie: { type: "string" },
|
||||
project: { type: "number" },
|
||||
plant: { type: "number" },
|
||||
customer: { type: "number" },
|
||||
userId: { type: "string" },
|
||||
profiles: { type: "array", items: { type: "string" } },
|
||||
archived: { type: "boolean" },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = numberArg(args, "id")
|
||||
if (!id) throw new Error("id ist erforderlich")
|
||||
|
||||
const update: Record<string, unknown> = {
|
||||
updatedAt: new Date(),
|
||||
updatedBy: context.userId,
|
||||
}
|
||||
|
||||
for (const key of ["name", "description", "categorie", "userId"] as const) {
|
||||
if (args[key] !== undefined) update[key] = stringArg(args, key)
|
||||
}
|
||||
for (const key of ["project", "plant", "customer"] as const) {
|
||||
if (args[key] !== undefined) update[key] = numberArg(args, key)
|
||||
}
|
||||
if (Array.isArray(args.profiles)) update.profiles = args.profiles
|
||||
if (typeof args.archived === "boolean") update.archived = args.archived
|
||||
|
||||
const [updated] = await context.server.db
|
||||
.update(tasks)
|
||||
.set(update)
|
||||
.where(and(eq(tasks.id, id), eq(tasks.tenant, context.tenantId)))
|
||||
.returning()
|
||||
|
||||
if (!updated) throw new Error("Aufgabe nicht gefunden")
|
||||
return { task: updated }
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "organisation.tasks.archive",
|
||||
title: "Aufgabe archivieren",
|
||||
description: "Archiviert eine Aufgabe im aktiven Mandanten.",
|
||||
requiredPermissions: ["organisation.tasks.write"],
|
||||
inputSchema: {
|
||||
type: "object",
|
||||
required: ["id"],
|
||||
properties: {
|
||||
id: { type: "number" },
|
||||
},
|
||||
},
|
||||
async handler(context, args) {
|
||||
const id = numberArg(args, "id")
|
||||
if (!id) throw new Error("id ist erforderlich")
|
||||
|
||||
const [updated] = await context.server.db
|
||||
.update(tasks)
|
||||
.set({
|
||||
archived: true,
|
||||
updatedAt: new Date(),
|
||||
updatedBy: context.userId,
|
||||
})
|
||||
.where(and(eq(tasks.id, id), eq(tasks.tenant, context.tenantId)))
|
||||
.returning()
|
||||
|
||||
if (!updated) throw new Error("Aufgabe nicht gefunden")
|
||||
return { task: updated }
|
||||
},
|
||||
},
|
||||
]
|
||||
36
backend/src/mcp/types.ts
Normal file
36
backend/src/mcp/types.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { FastifyInstance, FastifyRequest } from "fastify"
|
||||
|
||||
export type McpContext = {
|
||||
server: FastifyInstance
|
||||
request: FastifyRequest
|
||||
tenantId: number
|
||||
userId: string
|
||||
isAdmin: boolean
|
||||
permissions: string[]
|
||||
}
|
||||
|
||||
export type McpToolResult = {
|
||||
content: Array<{
|
||||
type: "text"
|
||||
text: string
|
||||
}>
|
||||
structuredContent?: Record<string, unknown>
|
||||
isError?: boolean
|
||||
}
|
||||
|
||||
export type McpTool = {
|
||||
name: string
|
||||
title: string
|
||||
description: string
|
||||
requiredPermissions: string[]
|
||||
inputSchema: Record<string, unknown>
|
||||
handler: (context: McpContext, args: Record<string, unknown>) => Promise<unknown>
|
||||
}
|
||||
|
||||
export type JsonRpcRequest = {
|
||||
jsonrpc?: string
|
||||
id?: string | number | null
|
||||
method?: string
|
||||
params?: any
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
authUsers,
|
||||
} from "../../db/schema"
|
||||
|
||||
import { eq, and } from "drizzle-orm"
|
||||
import { eq, and, inArray } from "drizzle-orm"
|
||||
|
||||
export default fp(async (server: FastifyInstance) => {
|
||||
server.addHook("preHandler", async (req, reply) => {
|
||||
@@ -63,10 +63,12 @@ export default fp(async (server: FastifyInstance) => {
|
||||
const userId = req.user.user_id
|
||||
|
||||
// --------------------------------------------------------
|
||||
// 3️⃣ Rolle des Nutzers im Tenant holen
|
||||
// 3️⃣ Rollen des Nutzers im Tenant holen
|
||||
// --------------------------------------------------------
|
||||
const roleRows = await server.db
|
||||
.select()
|
||||
.select({
|
||||
role_id: authUserRoles.role_id,
|
||||
})
|
||||
.from(authUserRoles)
|
||||
.where(
|
||||
and(
|
||||
@@ -74,7 +76,6 @@ export default fp(async (server: FastifyInstance) => {
|
||||
eq(authUserRoles.tenant_id, tenantId)
|
||||
)
|
||||
)
|
||||
.limit(1)
|
||||
|
||||
if (roleRows.length === 0) {
|
||||
if (req.user.is_admin) {
|
||||
@@ -89,22 +90,22 @@ export default fp(async (server: FastifyInstance) => {
|
||||
.send({ error: "No role assigned for this tenant" })
|
||||
}
|
||||
|
||||
const roleId = roleRows[0].role_id
|
||||
const roleIds = Array.from(new Set(roleRows.map((role) => role.role_id)))
|
||||
|
||||
// --------------------------------------------------------
|
||||
// 4️⃣ Berechtigungen der Rolle laden
|
||||
// 4️⃣ Berechtigungen der Rollen laden
|
||||
// --------------------------------------------------------
|
||||
const permissionRows = await server.db
|
||||
.select()
|
||||
.from(authRolePermissions)
|
||||
.where(eq(authRolePermissions.role_id, roleId))
|
||||
.where(inArray(authRolePermissions.role_id, roleIds))
|
||||
|
||||
const permissions = permissionRows.map((p) => p.permission)
|
||||
const permissions = Array.from(new Set(permissionRows.map((p) => p.permission)))
|
||||
|
||||
// --------------------------------------------------------
|
||||
// 5️⃣ An Request hängen für spätere Nutzung
|
||||
// --------------------------------------------------------
|
||||
req.role = roleId
|
||||
req.role = roleIds[0]
|
||||
req.permissions = permissions
|
||||
req.hasPermission = (perm: string) => permissions.includes(perm)
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import { DE_BANK_CODE_TO_BIC } from "../utils/deBankBics"
|
||||
|
||||
import {
|
||||
bankrequisitions,
|
||||
bankaccounts,
|
||||
bankstatements,
|
||||
accounts,
|
||||
createddocuments,
|
||||
@@ -26,10 +27,12 @@ import {
|
||||
and,
|
||||
isNull,
|
||||
aliasedTable,
|
||||
desc,
|
||||
} from "drizzle-orm"
|
||||
|
||||
|
||||
export default async function bankingRoutes(server: FastifyInstance) {
|
||||
const CASHBOOK_BANK_ID = "fedeo-cashbook"
|
||||
const ContraAccounts = aliasedTable(accounts, "contra_accounts")
|
||||
const ContraCustomers = aliasedTable(customers, "contra_customers")
|
||||
const ContraVendors = aliasedTable(vendors, "contra_vendors")
|
||||
@@ -40,6 +43,24 @@ export default async function bankingRoutes(server: FastifyInstance) {
|
||||
const normalizeManualSide = (payload: any, keys: string[]) =>
|
||||
keys.filter((key) => payload[key] !== null && payload[key] !== undefined && payload[key] !== "")
|
||||
|
||||
const cashbookAccountFilter = (tenantId: number) => and(
|
||||
eq(bankaccounts.tenant, tenantId),
|
||||
eq(bankaccounts.bankId, CASHBOOK_BANK_ID),
|
||||
eq(bankaccounts.archived, false)
|
||||
)
|
||||
|
||||
const buildCashbookCounterPayload = (type: string, id: any) => {
|
||||
const numericId = type === "ownaccount" ? id : Number(id)
|
||||
if (!id || (type !== "ownaccount" && !Number.isFinite(numericId))) return null
|
||||
|
||||
if (type === "account") return { account: numericId }
|
||||
if (type === "customer") return { customer: numericId }
|
||||
if (type === "vendor") return { vendor: numericId }
|
||||
if (type === "ownaccount") return { ownaccount: numericId }
|
||||
if (type === "incominginvoice") return { incominginvoice: numericId }
|
||||
return null
|
||||
}
|
||||
|
||||
const prepareStatementAllocationPayload = (payload: any) => {
|
||||
const next = { ...payload }
|
||||
const isManualBooking = !next.bankstatement
|
||||
@@ -89,6 +110,270 @@ export default async function bankingRoutes(server: FastifyInstance) {
|
||||
return { data: next }
|
||||
}
|
||||
|
||||
server.get("/banking/cashbooks", async (req, reply) => {
|
||||
try {
|
||||
if (!req.user) return reply.code(401).send({ error: "Unauthorized" })
|
||||
|
||||
const rows = await server.db
|
||||
.select()
|
||||
.from(bankaccounts)
|
||||
.where(cashbookAccountFilter(req.user.tenant_id))
|
||||
.orderBy(bankaccounts.name)
|
||||
|
||||
return reply.send(rows)
|
||||
} catch (err) {
|
||||
server.log.error(err)
|
||||
return reply.code(500).send({ error: "Failed to load cashbooks" })
|
||||
}
|
||||
})
|
||||
|
||||
server.post("/banking/cashbooks", async (req, reply) => {
|
||||
try {
|
||||
if (!req.user) return reply.code(401).send({ error: "Unauthorized" })
|
||||
|
||||
const body = req.body as {
|
||||
name?: string
|
||||
datevNumber?: string
|
||||
openingBalance?: number
|
||||
}
|
||||
|
||||
const name = String(body.name || "").trim()
|
||||
const datevNumber = String(body.datevNumber || "").trim()
|
||||
const openingBalance = Number(body.openingBalance || 0)
|
||||
|
||||
if (!name) return reply.code(400).send({ error: "Bitte eine Bezeichnung für die Kasse angeben." })
|
||||
if (!datevNumber) return reply.code(400).send({ error: "Bitte eine Kontennummer für die Kasse angeben." })
|
||||
if (!Number.isFinite(openingBalance)) return reply.code(400).send({ error: "Der Anfangsbestand ist ungültig." })
|
||||
|
||||
const uniquePart = `${req.user.tenant_id}-${Date.now()}`
|
||||
const inserted = await server.db.insert(bankaccounts).values({
|
||||
name,
|
||||
iban: `CASH-${uniquePart}`,
|
||||
tenant: req.user.tenant_id,
|
||||
bankId: CASHBOOK_BANK_ID,
|
||||
ownerName: name,
|
||||
accountId: `cashbook-${uniquePart}`,
|
||||
balance: openingBalance,
|
||||
datevNumber,
|
||||
updatedBy: req.user.user_id,
|
||||
}).returning()
|
||||
|
||||
const createdRecord = inserted[0]
|
||||
return reply.send(createdRecord)
|
||||
} catch (err) {
|
||||
server.log.error(err)
|
||||
return reply.code(500).send({ error: "Failed to create cashbook" })
|
||||
}
|
||||
})
|
||||
|
||||
server.patch("/banking/cashbooks/:id/archive", async (req, reply) => {
|
||||
try {
|
||||
if (!req.user) return reply.code(401).send({ error: "Unauthorized" })
|
||||
|
||||
const { id } = req.params as { id: string }
|
||||
const updated = await server.db.update(bankaccounts)
|
||||
.set({
|
||||
archived: true,
|
||||
updatedAt: new Date(),
|
||||
updatedBy: req.user.user_id,
|
||||
})
|
||||
.where(and(
|
||||
eq(bankaccounts.id, Number(id)),
|
||||
eq(bankaccounts.tenant, req.user.tenant_id),
|
||||
eq(bankaccounts.bankId, CASHBOOK_BANK_ID)
|
||||
))
|
||||
.returning()
|
||||
|
||||
if (!updated[0]) return reply.code(404).send({ error: "Cashbook not found" })
|
||||
return reply.send(updated[0])
|
||||
} catch (err) {
|
||||
server.log.error(err)
|
||||
return reply.code(500).send({ error: "Failed to archive cashbook" })
|
||||
}
|
||||
})
|
||||
|
||||
server.get("/banking/cashbooks/:id/bookings", async (req, reply) => {
|
||||
try {
|
||||
if (!req.user) return reply.code(401).send({ error: "Unauthorized" })
|
||||
|
||||
const { id } = req.params as { id: string }
|
||||
const cashbookId = Number(id)
|
||||
if (!Number.isFinite(cashbookId)) return reply.code(400).send({ error: "Ungültige Kasse." })
|
||||
|
||||
const rows = await server.db.select({
|
||||
statement: bankstatements,
|
||||
allocation: statementallocations,
|
||||
account: accounts,
|
||||
customer: customers,
|
||||
vendor: vendors,
|
||||
ownaccount: ownaccounts,
|
||||
incominginvoice: ManualInvoices,
|
||||
incominginvoiceVendor: ManualInvoiceVendors,
|
||||
})
|
||||
.from(bankstatements)
|
||||
.leftJoin(statementallocations, eq(statementallocations.bankstatement, bankstatements.id))
|
||||
.leftJoin(accounts, eq(statementallocations.account, accounts.id))
|
||||
.leftJoin(customers, eq(statementallocations.customer, customers.id))
|
||||
.leftJoin(vendors, eq(statementallocations.vendor, vendors.id))
|
||||
.leftJoin(ownaccounts, eq(statementallocations.ownaccount, ownaccounts.id))
|
||||
.leftJoin(ManualInvoices, eq(statementallocations.incominginvoice, ManualInvoices.id))
|
||||
.leftJoin(ManualInvoiceVendors, eq(ManualInvoices.vendor, ManualInvoiceVendors.id))
|
||||
.where(and(
|
||||
eq(bankstatements.tenant, req.user.tenant_id),
|
||||
eq(bankstatements.account, cashbookId),
|
||||
eq(bankstatements.archived, false)
|
||||
))
|
||||
.orderBy(desc(bankstatements.date), desc(bankstatements.createdAt))
|
||||
|
||||
return reply.send(rows.map((row) => ({
|
||||
...row.statement,
|
||||
allocation: row.allocation,
|
||||
account: row.account,
|
||||
customer: row.customer,
|
||||
vendor: row.vendor,
|
||||
ownaccount: row.ownaccount,
|
||||
incominginvoice: row.incominginvoice ? {
|
||||
...row.incominginvoice,
|
||||
vendor: row.incominginvoiceVendor,
|
||||
} : null,
|
||||
})))
|
||||
} catch (err) {
|
||||
server.log.error(err)
|
||||
return reply.code(500).send({ error: "Failed to load cashbook bookings" })
|
||||
}
|
||||
})
|
||||
|
||||
server.post("/banking/cashbooks/:id/bookings", async (req, reply) => {
|
||||
try {
|
||||
if (!req.user) return reply.code(401).send({ error: "Unauthorized" })
|
||||
|
||||
const { id } = req.params as { id: string }
|
||||
const cashbookId = Number(id)
|
||||
const body = req.body as {
|
||||
date?: string
|
||||
amount?: number
|
||||
direction?: "income" | "expense"
|
||||
counterType?: string
|
||||
counterId?: string | number
|
||||
description?: string
|
||||
datevTaxKey?: string | null
|
||||
}
|
||||
|
||||
if (!Number.isFinite(cashbookId)) return reply.code(400).send({ error: "Ungültige Kasse." })
|
||||
if (!body.date || !dayjs(body.date).isValid()) return reply.code(400).send({ error: "Bitte ein gültiges Buchungsdatum angeben." })
|
||||
if (!Number.isFinite(Number(body.amount)) || Number(body.amount) <= 0) return reply.code(400).send({ error: "Der Betrag muss größer als 0 sein." })
|
||||
if (body.direction !== "income" && body.direction !== "expense") return reply.code(400).send({ error: "Bitte Einnahme oder Ausgabe auswählen." })
|
||||
|
||||
const cashbook = await server.db.select().from(bankaccounts).where(and(
|
||||
eq(bankaccounts.id, cashbookId),
|
||||
eq(bankaccounts.tenant, req.user.tenant_id),
|
||||
eq(bankaccounts.bankId, CASHBOOK_BANK_ID),
|
||||
eq(bankaccounts.archived, false)
|
||||
)).limit(1)
|
||||
|
||||
if (!cashbook[0]) return reply.code(404).send({ error: "Kasse nicht gefunden." })
|
||||
|
||||
const counterPayload = buildCashbookCounterPayload(String(body.counterType || ""), body.counterId)
|
||||
if (!counterPayload) return reply.code(400).send({ error: "Bitte ein Gegenkonto auswählen." })
|
||||
|
||||
const signedAmount = body.direction === "income"
|
||||
? Math.abs(Number(body.amount))
|
||||
: -Math.abs(Number(body.amount))
|
||||
const description = String(body.description || "").trim() || (body.direction === "income" ? "Bareinnahme" : "Barausgabe")
|
||||
|
||||
const created = await server.db.transaction(async (tx) => {
|
||||
const insertedStatements = await tx.insert(bankstatements).values({
|
||||
account: cashbookId,
|
||||
date: dayjs(body.date).format("YYYY-MM-DD"),
|
||||
amount: signedAmount,
|
||||
tenant: req.user.tenant_id,
|
||||
text: description,
|
||||
currency: "EUR",
|
||||
credName: body.direction === "income" ? cashbook[0].name : description,
|
||||
debName: body.direction === "expense" ? cashbook[0].name : description,
|
||||
updatedBy: req.user.user_id,
|
||||
}).returning()
|
||||
|
||||
const statement = insertedStatements[0]
|
||||
const insertedAllocations = await tx.insert(statementallocations).values({
|
||||
bankstatement: statement.id,
|
||||
amount: signedAmount,
|
||||
tenant: req.user.tenant_id,
|
||||
description,
|
||||
datevTaxKey: body.datevTaxKey ? String(body.datevTaxKey).trim() : null,
|
||||
...counterPayload,
|
||||
}).returning()
|
||||
|
||||
return {
|
||||
statement,
|
||||
allocation: insertedAllocations[0],
|
||||
}
|
||||
})
|
||||
|
||||
await insertHistoryItem(server, {
|
||||
entity: "bankstatements",
|
||||
entityId: Number(created.statement.id),
|
||||
action: "created",
|
||||
created_by: req.user.user_id,
|
||||
tenant_id: req.user.tenant_id,
|
||||
oldVal: null,
|
||||
newVal: created,
|
||||
text: "Kassenbuchung erstellt",
|
||||
})
|
||||
|
||||
return reply.send(created)
|
||||
} catch (err) {
|
||||
server.log.error(err)
|
||||
return reply.code(500).send({ error: "Failed to create cashbook booking" })
|
||||
}
|
||||
})
|
||||
|
||||
server.delete("/banking/cashbook-bookings/:id", async (req, reply) => {
|
||||
try {
|
||||
if (!req.user) return reply.code(401).send({ error: "Unauthorized" })
|
||||
|
||||
const { id } = req.params as { id: string }
|
||||
const statementId = Number(id)
|
||||
if (!Number.isFinite(statementId)) return reply.code(400).send({ error: "Ungültige Buchung." })
|
||||
|
||||
const records = await server.db.select({
|
||||
statement: bankstatements,
|
||||
cashbook: bankaccounts,
|
||||
})
|
||||
.from(bankstatements)
|
||||
.innerJoin(bankaccounts, eq(bankstatements.account, bankaccounts.id))
|
||||
.where(and(
|
||||
eq(bankstatements.id, statementId),
|
||||
eq(bankstatements.tenant, req.user.tenant_id),
|
||||
eq(bankaccounts.bankId, CASHBOOK_BANK_ID)
|
||||
))
|
||||
.limit(1)
|
||||
|
||||
if (!records[0]) return reply.code(404).send({ error: "Kassenbuchung nicht gefunden." })
|
||||
|
||||
await server.db.transaction(async (tx) => {
|
||||
await tx.delete(statementallocations).where(eq(statementallocations.bankstatement, statementId))
|
||||
await tx.delete(bankstatements).where(eq(bankstatements.id, statementId))
|
||||
})
|
||||
|
||||
await insertHistoryItem(server, {
|
||||
entity: "bankstatements",
|
||||
entityId: statementId,
|
||||
action: "deleted",
|
||||
created_by: req.user.user_id,
|
||||
tenant_id: req.user.tenant_id,
|
||||
oldVal: records[0].statement,
|
||||
newVal: null,
|
||||
text: "Kassenbuchung gelöscht",
|
||||
})
|
||||
|
||||
return reply.send({ success: true })
|
||||
} catch (err) {
|
||||
server.log.error(err)
|
||||
return reply.code(500).send({ error: "Failed to delete cashbook booking" })
|
||||
}
|
||||
})
|
||||
|
||||
const normalizeIban = (value?: string | null) =>
|
||||
String(value || "").replace(/\s+/g, "").toUpperCase()
|
||||
|
||||
|
||||
144
backend/src/routes/mcp.ts
Normal file
144
backend/src/routes/mcp.ts
Normal file
@@ -0,0 +1,144 @@
|
||||
import { FastifyInstance } from "fastify"
|
||||
import { assertToolPermission, createMcpContext } from "../mcp/authz"
|
||||
import { mcpToolMap, mcpTools } from "../mcp/registry"
|
||||
import { asToolError, asToolResult } from "../mcp/result"
|
||||
import { JsonRpcRequest } from "../mcp/types"
|
||||
|
||||
const SUPPORTED_PROTOCOL_VERSIONS = [
|
||||
"2025-11-25",
|
||||
"2025-06-18",
|
||||
"2025-03-26",
|
||||
"2024-11-05",
|
||||
]
|
||||
|
||||
function jsonRpcResult(id: JsonRpcRequest["id"], result: unknown) {
|
||||
return {
|
||||
jsonrpc: "2.0",
|
||||
id,
|
||||
result,
|
||||
}
|
||||
}
|
||||
|
||||
function jsonRpcError(id: JsonRpcRequest["id"], code: number, message: string) {
|
||||
return {
|
||||
jsonrpc: "2.0",
|
||||
id: id ?? null,
|
||||
error: {
|
||||
code,
|
||||
message,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function selectProtocolVersion(clientVersion?: string) {
|
||||
if (clientVersion && SUPPORTED_PROTOCOL_VERSIONS.includes(clientVersion)) {
|
||||
return clientVersion
|
||||
}
|
||||
|
||||
return SUPPORTED_PROTOCOL_VERSIONS[0]
|
||||
}
|
||||
|
||||
export default async function mcpRoutes(server: FastifyInstance) {
|
||||
server.post("/mcp", async (req, reply) => {
|
||||
const body = req.body as JsonRpcRequest | JsonRpcRequest[]
|
||||
const requests = Array.isArray(body) ? body : [body]
|
||||
const responses = []
|
||||
|
||||
for (const request of requests) {
|
||||
const id = request?.id
|
||||
|
||||
if (!request || request.jsonrpc !== "2.0" || !request.method) {
|
||||
responses.push(jsonRpcError(id, -32600, "Invalid JSON-RPC request"))
|
||||
continue
|
||||
}
|
||||
|
||||
if (request.method === "notifications/initialized") {
|
||||
continue
|
||||
}
|
||||
|
||||
if (request.method === "initialize") {
|
||||
const clientVersion = request.params?.protocolVersion
|
||||
|
||||
responses.push(jsonRpcResult(id, {
|
||||
protocolVersion: selectProtocolVersion(clientVersion),
|
||||
capabilities: {
|
||||
tools: {
|
||||
listChanged: false,
|
||||
},
|
||||
},
|
||||
serverInfo: {
|
||||
name: "fedeo-mcp",
|
||||
version: "1.0.0",
|
||||
},
|
||||
instructions: "FEDEO MCP-Server für mandantenbezogene Buchhaltungs- und Organisationswerkzeuge. Alle Tools prüfen Rollenberechtigungen und arbeiten im aktiven Mandanten.",
|
||||
}))
|
||||
continue
|
||||
}
|
||||
|
||||
if (request.method === "ping") {
|
||||
responses.push(jsonRpcResult(id, {}))
|
||||
continue
|
||||
}
|
||||
|
||||
if (request.method === "tools/list") {
|
||||
responses.push(jsonRpcResult(id, {
|
||||
tools: mcpTools.map((tool) => ({
|
||||
name: tool.name,
|
||||
title: tool.title,
|
||||
description: tool.description,
|
||||
inputSchema: tool.inputSchema,
|
||||
annotations: {
|
||||
readOnlyHint: !tool.requiredPermissions.some((permission) => permission.endsWith(".write")),
|
||||
},
|
||||
})),
|
||||
}))
|
||||
continue
|
||||
}
|
||||
|
||||
if (request.method === "tools/call") {
|
||||
const toolName = request.params?.name
|
||||
const tool = typeof toolName === "string" ? mcpToolMap.get(toolName) : null
|
||||
|
||||
if (!tool) {
|
||||
responses.push(jsonRpcError(id, -32602, `Unknown tool: ${toolName}`))
|
||||
continue
|
||||
}
|
||||
|
||||
try {
|
||||
const context = await createMcpContext(server, req)
|
||||
assertToolPermission(context, tool)
|
||||
|
||||
const result = await tool.handler(context, request.params?.arguments || {})
|
||||
responses.push(jsonRpcResult(id, asToolResult(result)))
|
||||
} catch (error) {
|
||||
const statusCode = (error as any)?.statusCode
|
||||
|
||||
if (statusCode === 401 || statusCode === 403) {
|
||||
responses.push(jsonRpcError(id, statusCode === 401 ? -32001 : -32003, error instanceof Error ? error.message : "Forbidden"))
|
||||
} else {
|
||||
responses.push(jsonRpcResult(id, asToolError(error)))
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
responses.push(jsonRpcError(id, -32601, `Method not found: ${request.method}`))
|
||||
}
|
||||
|
||||
if (responses.length === 0) {
|
||||
return reply.code(204).send()
|
||||
}
|
||||
|
||||
return Array.isArray(body) ? responses : responses[0]
|
||||
})
|
||||
|
||||
server.get("/mcp", async (_req, reply) => {
|
||||
return reply.send({
|
||||
name: "fedeo-mcp",
|
||||
transport: "http-json-rpc",
|
||||
endpoint: "/api/mcp",
|
||||
tools: mcpTools.map((tool) => tool.name),
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import { sortData } from "../utils/sort"
|
||||
|
||||
// Schema imports
|
||||
import { accounts, units, countrys, tenants } from "../../db/schema"
|
||||
import { defaultCountries } from "../utils/countries"
|
||||
|
||||
const TABLE_MAP: Record<string, any> = {
|
||||
accounts,
|
||||
@@ -96,6 +97,24 @@ export default async function resourceRoutesSpecial(server: FastifyInstance) {
|
||||
|
||||
const data = await query
|
||||
|
||||
if (resource === "countrys") {
|
||||
const countryMap = new Map<string, any>()
|
||||
|
||||
for (const country of defaultCountries) {
|
||||
countryMap.set(country.toLocaleLowerCase("de"), { id: country, name: country })
|
||||
}
|
||||
|
||||
for (const country of data) {
|
||||
countryMap.set(country.name.toLocaleLowerCase("de"), country)
|
||||
}
|
||||
|
||||
return sortData(
|
||||
Array.from(countryMap.values()),
|
||||
sort || "name",
|
||||
sort ? ascQuery === "true" : true
|
||||
)
|
||||
}
|
||||
|
||||
// Falls sort clientseitig wie früher notwendig ist:
|
||||
const sorted = sortData(
|
||||
data,
|
||||
|
||||
258
backend/src/utils/countries.ts
Normal file
258
backend/src/utils/countries.ts
Normal file
@@ -0,0 +1,258 @@
|
||||
const COUNTRY_CODES = [
|
||||
"AF",
|
||||
"AX",
|
||||
"AL",
|
||||
"DZ",
|
||||
"AS",
|
||||
"AD",
|
||||
"AO",
|
||||
"AI",
|
||||
"AQ",
|
||||
"AG",
|
||||
"AR",
|
||||
"AM",
|
||||
"AW",
|
||||
"AU",
|
||||
"AT",
|
||||
"AZ",
|
||||
"BS",
|
||||
"BH",
|
||||
"BD",
|
||||
"BB",
|
||||
"BY",
|
||||
"BE",
|
||||
"BZ",
|
||||
"BJ",
|
||||
"BM",
|
||||
"BT",
|
||||
"BO",
|
||||
"BQ",
|
||||
"BA",
|
||||
"BW",
|
||||
"BV",
|
||||
"BR",
|
||||
"IO",
|
||||
"BN",
|
||||
"BG",
|
||||
"BF",
|
||||
"BI",
|
||||
"CV",
|
||||
"KH",
|
||||
"CM",
|
||||
"CA",
|
||||
"KY",
|
||||
"CF",
|
||||
"TD",
|
||||
"CL",
|
||||
"CN",
|
||||
"CX",
|
||||
"CC",
|
||||
"CO",
|
||||
"KM",
|
||||
"CG",
|
||||
"CD",
|
||||
"CK",
|
||||
"CR",
|
||||
"CI",
|
||||
"HR",
|
||||
"CU",
|
||||
"CW",
|
||||
"CY",
|
||||
"CZ",
|
||||
"DK",
|
||||
"DJ",
|
||||
"DM",
|
||||
"DO",
|
||||
"EC",
|
||||
"EG",
|
||||
"SV",
|
||||
"GQ",
|
||||
"ER",
|
||||
"EE",
|
||||
"SZ",
|
||||
"ET",
|
||||
"FK",
|
||||
"FO",
|
||||
"FJ",
|
||||
"FI",
|
||||
"FR",
|
||||
"GF",
|
||||
"PF",
|
||||
"TF",
|
||||
"GA",
|
||||
"GM",
|
||||
"GE",
|
||||
"DE",
|
||||
"GH",
|
||||
"GI",
|
||||
"GR",
|
||||
"GL",
|
||||
"GD",
|
||||
"GP",
|
||||
"GU",
|
||||
"GT",
|
||||
"GG",
|
||||
"GN",
|
||||
"GW",
|
||||
"GY",
|
||||
"HT",
|
||||
"HM",
|
||||
"VA",
|
||||
"HN",
|
||||
"HK",
|
||||
"HU",
|
||||
"IS",
|
||||
"IN",
|
||||
"ID",
|
||||
"IR",
|
||||
"IQ",
|
||||
"IE",
|
||||
"IM",
|
||||
"IL",
|
||||
"IT",
|
||||
"JM",
|
||||
"JP",
|
||||
"JE",
|
||||
"JO",
|
||||
"KZ",
|
||||
"KE",
|
||||
"KI",
|
||||
"KP",
|
||||
"KR",
|
||||
"KW",
|
||||
"KG",
|
||||
"LA",
|
||||
"LV",
|
||||
"LB",
|
||||
"LS",
|
||||
"LR",
|
||||
"LY",
|
||||
"LI",
|
||||
"LT",
|
||||
"LU",
|
||||
"MO",
|
||||
"MG",
|
||||
"MW",
|
||||
"MY",
|
||||
"MV",
|
||||
"ML",
|
||||
"MT",
|
||||
"MH",
|
||||
"MQ",
|
||||
"MR",
|
||||
"MU",
|
||||
"YT",
|
||||
"MX",
|
||||
"FM",
|
||||
"MD",
|
||||
"MC",
|
||||
"MN",
|
||||
"ME",
|
||||
"MS",
|
||||
"MA",
|
||||
"MZ",
|
||||
"MM",
|
||||
"NA",
|
||||
"NR",
|
||||
"NP",
|
||||
"NL",
|
||||
"NC",
|
||||
"NZ",
|
||||
"NI",
|
||||
"NE",
|
||||
"NG",
|
||||
"NU",
|
||||
"NF",
|
||||
"MK",
|
||||
"MP",
|
||||
"NO",
|
||||
"OM",
|
||||
"PK",
|
||||
"PW",
|
||||
"PS",
|
||||
"PA",
|
||||
"PG",
|
||||
"PY",
|
||||
"PE",
|
||||
"PH",
|
||||
"PN",
|
||||
"PL",
|
||||
"PT",
|
||||
"PR",
|
||||
"QA",
|
||||
"RE",
|
||||
"RO",
|
||||
"RU",
|
||||
"RW",
|
||||
"BL",
|
||||
"SH",
|
||||
"KN",
|
||||
"LC",
|
||||
"MF",
|
||||
"PM",
|
||||
"VC",
|
||||
"WS",
|
||||
"SM",
|
||||
"ST",
|
||||
"SA",
|
||||
"SN",
|
||||
"RS",
|
||||
"SC",
|
||||
"SL",
|
||||
"SG",
|
||||
"SX",
|
||||
"SK",
|
||||
"SI",
|
||||
"SB",
|
||||
"SO",
|
||||
"ZA",
|
||||
"GS",
|
||||
"SS",
|
||||
"ES",
|
||||
"LK",
|
||||
"SD",
|
||||
"SR",
|
||||
"SJ",
|
||||
"SE",
|
||||
"CH",
|
||||
"SY",
|
||||
"TW",
|
||||
"TJ",
|
||||
"TZ",
|
||||
"TH",
|
||||
"TL",
|
||||
"TG",
|
||||
"TK",
|
||||
"TO",
|
||||
"TT",
|
||||
"TN",
|
||||
"TR",
|
||||
"TM",
|
||||
"TC",
|
||||
"TV",
|
||||
"UG",
|
||||
"UA",
|
||||
"AE",
|
||||
"GB",
|
||||
"UM",
|
||||
"US",
|
||||
"UY",
|
||||
"UZ",
|
||||
"VU",
|
||||
"VE",
|
||||
"VN",
|
||||
"VG",
|
||||
"VI",
|
||||
"WF",
|
||||
"EH",
|
||||
"YE",
|
||||
"ZM",
|
||||
"ZW",
|
||||
] as const
|
||||
|
||||
const countryNames = new Intl.DisplayNames(["de"], { type: "region" })
|
||||
|
||||
export const defaultCountries = COUNTRY_CODES
|
||||
.map((code) => countryNames.of(code))
|
||||
.filter((name): name is string => Boolean(name))
|
||||
.sort((a, b) => a.localeCompare(b, "de"))
|
||||
@@ -123,11 +123,33 @@ const saveAllowed = computed(() => {
|
||||
|
||||
|
||||
const setupCreate = () => {
|
||||
const setDefaultValue = (datapoint) => {
|
||||
if (props.mode !== "create" || !("defaultValue" in datapoint)) return
|
||||
|
||||
const defaultValue = typeof datapoint.defaultValue === "function"
|
||||
? datapoint.defaultValue()
|
||||
: datapoint.defaultValue
|
||||
|
||||
if(datapoint.key.includes(".")){
|
||||
const [parentKey, childKey] = datapoint.key.split(".")
|
||||
if (item.value[parentKey][childKey] === undefined) {
|
||||
item.value[parentKey][childKey] = defaultValue
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if (item.value[datapoint.key] === undefined) {
|
||||
item.value[datapoint.key] = defaultValue
|
||||
}
|
||||
}
|
||||
|
||||
dataType.templateColumns.forEach(datapoint => {
|
||||
if(datapoint.key.includes(".")){
|
||||
!item.value[datapoint.key.split(".")[0]] ? item.value[datapoint.key.split(".")[0]] = {} : null
|
||||
}
|
||||
|
||||
setDefaultValue(datapoint)
|
||||
|
||||
if(datapoint.inputType === "editor") {
|
||||
if(datapoint.key.includes(".")){
|
||||
item.value[datapoint.key.split(".")[0]][datapoint.key.split(".")[1]] = {}
|
||||
|
||||
@@ -193,6 +193,11 @@ const links = computed(() => {
|
||||
to: "/banking",
|
||||
icon: "i-heroicons-document-text",
|
||||
} : null,
|
||||
featureEnabled("banking") ? {
|
||||
label: "Kassenbuch",
|
||||
to: "/accounting/cashbooks",
|
||||
icon: "i-heroicons-banknotes",
|
||||
} : null,
|
||||
(featureEnabled("accounts") || featureEnabled("ownaccounts")) ? {
|
||||
label: "Manuelle Buchungen",
|
||||
to: "/accounting/manual-bookings",
|
||||
|
||||
424
frontend/pages/accounting/cashbooks/[id].vue
Normal file
424
frontend/pages/accounting/cashbooks/[id].vue
Normal file
@@ -0,0 +1,424 @@
|
||||
<script setup>
|
||||
import dayjs from "dayjs"
|
||||
|
||||
const toast = useToast()
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
|
||||
const loading = ref(true)
|
||||
const savingBooking = ref(false)
|
||||
const cashbooks = ref([])
|
||||
const bookings = ref([])
|
||||
const accounts = ref([])
|
||||
const customers = ref([])
|
||||
const vendors = ref([])
|
||||
const ownaccounts = ref([])
|
||||
const incomingInvoices = ref([])
|
||||
const selectedCashbookId = computed(() => Number(route.params.id))
|
||||
const counterSearch = ref("")
|
||||
const expandedGroups = ref([])
|
||||
|
||||
const DATEV_TAX_KEY_ITEMS = [
|
||||
{ value: "__none__", label: "Ohne Steuerschlüssel" },
|
||||
{ value: "9", label: "9 - Vorsteuer 19 %" },
|
||||
{ value: "8", label: "8 - Vorsteuer 7 %" },
|
||||
{ value: "19", label: "19 - EU Vorsteuer 19 %" },
|
||||
{ value: "18", label: "18 - EU Vorsteuer 7 %" }
|
||||
]
|
||||
|
||||
const form = reactive({
|
||||
date: dayjs().format("YYYY-MM-DD"),
|
||||
direction: "expense",
|
||||
amount: null,
|
||||
counter: "",
|
||||
datevTaxKey: "__none__",
|
||||
description: ""
|
||||
})
|
||||
|
||||
const displayCurrency = (value) => `${Number(value || 0).toFixed(2).replace(".", ",")} €`
|
||||
const normalizeSearch = (value) => String(value || "").toLowerCase().trim()
|
||||
|
||||
const selectedCashbook = computed(() => cashbooks.value.find((item) => item.id === selectedCashbookId.value) || null)
|
||||
const currentBalance = computed(() => {
|
||||
const openingBalance = Number(selectedCashbook.value?.balance || 0)
|
||||
const movementSum = bookings.value.reduce((sum, booking) => sum + Number(booking.amount || 0), 0)
|
||||
return openingBalance + movementSum
|
||||
})
|
||||
|
||||
const getIncomingInvoiceGross = (invoice) => {
|
||||
return Number((invoice.accounts || []).reduce((sum, account) => {
|
||||
return sum + Number(account.amountNet || 0) + Number(account.amountTax || 0)
|
||||
}, 0))
|
||||
}
|
||||
|
||||
const getIncomingInvoiceOpenAmount = (invoice) => {
|
||||
const gross = getIncomingInvoiceGross(invoice)
|
||||
const allocated = Number((invoice.statementallocations || []).reduce((sum, allocation) => sum + Number(allocation.amount || 0), 0))
|
||||
return Math.abs(gross) - Math.abs(allocated)
|
||||
}
|
||||
|
||||
const buildEntries = (rows, type, labelBuilder) =>
|
||||
(rows || []).map((item) => ({
|
||||
key: `${type}:${item.id}`,
|
||||
id: item.id,
|
||||
type,
|
||||
number: item.number || item.vendorNumber || item.customerNumber || item.reference || "",
|
||||
name: item.label || item.name || item.vendor?.name || "",
|
||||
label: labelBuilder(item),
|
||||
typeLabel:
|
||||
type === "account"
|
||||
? "Sachkonten"
|
||||
: type === "vendor"
|
||||
? "Kreditoren"
|
||||
: type === "customer"
|
||||
? "Debitoren"
|
||||
: type === "incominginvoice"
|
||||
? "Eingangsbelege"
|
||||
: "Zusätzliche Konten"
|
||||
}))
|
||||
|
||||
const entryGroups = computed(() => ([
|
||||
{
|
||||
key: "account",
|
||||
label: "Sachkonten",
|
||||
entries: buildEntries(accounts.value, "account", (item) => `${item.number} - ${item.label}`)
|
||||
},
|
||||
{
|
||||
key: "vendor",
|
||||
label: "Kreditoren",
|
||||
entries: buildEntries(vendors.value, "vendor", (item) => `${item.vendorNumber || "ohne Nr."} - ${item.name}`)
|
||||
},
|
||||
{
|
||||
key: "customer",
|
||||
label: "Debitoren",
|
||||
entries: buildEntries(customers.value, "customer", (item) => `${item.customerNumber || "ohne Nr."} - ${item.name}`)
|
||||
},
|
||||
{
|
||||
key: "ownaccount",
|
||||
label: "Zusätzliche Konten",
|
||||
entries: buildEntries(ownaccounts.value, "ownaccount", (item) => `${item.number} - ${item.name}`)
|
||||
},
|
||||
{
|
||||
key: "incominginvoice",
|
||||
label: "Eingangsbelege",
|
||||
entries: buildEntries(incomingInvoices.value, "incominginvoice", (item) => `${item.reference || "Ohne Referenz"} - ${item.vendor?.name || "Ohne Lieferant"} - Offen ${displayCurrency(getIncomingInvoiceOpenAmount(item))}`)
|
||||
}
|
||||
]))
|
||||
|
||||
const groupedCounterEntries = computed(() =>
|
||||
entryGroups.value.map((group) => ({
|
||||
...group,
|
||||
entries: group.entries.filter((entry) => {
|
||||
const search = normalizeSearch(counterSearch.value)
|
||||
if (!search) return true
|
||||
return [entry.number, entry.name, entry.label, entry.typeLabel]
|
||||
.some((value) => normalizeSearch(value).includes(search))
|
||||
})
|
||||
})).filter((group) => group.entries.length > 0)
|
||||
)
|
||||
|
||||
const selectedCounter = computed(() => entryGroups.value.flatMap((group) => group.entries).find((entry) => entry.key === form.counter))
|
||||
|
||||
const isGroupExpanded = (groupKey) => expandedGroups.value.includes(groupKey)
|
||||
const toggleGroupExpanded = (groupKey) => {
|
||||
if (expandedGroups.value.includes(groupKey)) {
|
||||
expandedGroups.value = expandedGroups.value.filter((item) => item !== groupKey)
|
||||
return
|
||||
}
|
||||
expandedGroups.value = [...expandedGroups.value, groupKey]
|
||||
}
|
||||
|
||||
const visibleEntries = (group) => {
|
||||
if (group.entries.length <= 5 || isGroupExpanded(group.key)) return group.entries
|
||||
return group.entries.slice(0, 5)
|
||||
}
|
||||
|
||||
const getBookingCounter = (booking) => {
|
||||
if (booking.incominginvoice) {
|
||||
return {
|
||||
type: "Eingangsbeleg",
|
||||
number: booking.incominginvoice.reference || "",
|
||||
name: booking.incominginvoice.vendor?.name || booking.incominginvoice.description || ""
|
||||
}
|
||||
}
|
||||
|
||||
const map = [
|
||||
["account", "Sachkonto"],
|
||||
["vendor", "Kreditor"],
|
||||
["customer", "Debitor"],
|
||||
["ownaccount", "Zusätzliches Konto"]
|
||||
]
|
||||
|
||||
for (const [key, type] of map) {
|
||||
const item = booking[key]
|
||||
if (!item) continue
|
||||
return {
|
||||
type,
|
||||
number: item.number || item.vendorNumber || item.customerNumber || "",
|
||||
name: item.label || item.name || ""
|
||||
}
|
||||
}
|
||||
|
||||
return { type: "", number: "", name: "" }
|
||||
}
|
||||
|
||||
const loadBookings = async () => {
|
||||
if (!selectedCashbookId.value || !Number.isFinite(selectedCashbookId.value)) {
|
||||
bookings.value = []
|
||||
return
|
||||
}
|
||||
|
||||
bookings.value = await useNuxtApp().$api(`/api/banking/cashbooks/${selectedCashbookId.value}/bookings`)
|
||||
}
|
||||
|
||||
const loadData = async () => {
|
||||
loading.value = true
|
||||
const [cashbookRows, accountRows, customerRows, vendorRows, ownaccountRows, incomingInvoiceRows] = await Promise.all([
|
||||
useNuxtApp().$api("/api/banking/cashbooks"),
|
||||
useEntities("accounts").selectSpecial("*", "number", true),
|
||||
useEntities("customers").select(),
|
||||
useEntities("vendors").select(),
|
||||
useEntities("ownaccounts").select(),
|
||||
useEntities("incominginvoices").select("*, vendor(*), statementallocations(id,amount)")
|
||||
])
|
||||
|
||||
cashbooks.value = cashbookRows || []
|
||||
accounts.value = accountRows || []
|
||||
customers.value = customerRows || []
|
||||
vendors.value = vendorRows || []
|
||||
ownaccounts.value = ownaccountRows || []
|
||||
incomingInvoices.value = (incomingInvoiceRows || [])
|
||||
.filter((invoice) => invoice.state === "Gebucht" && !invoice.archived)
|
||||
.filter((invoice) => getIncomingInvoiceOpenAmount(invoice) > 0.004)
|
||||
|
||||
await loadBookings()
|
||||
loading.value = false
|
||||
}
|
||||
|
||||
const saveBooking = async () => {
|
||||
if (!selectedCashbookId.value || !form.date || !form.amount || !form.counter) {
|
||||
toast.add({ title: "Bitte Kasse, Datum, Betrag und Gegenkonto auswählen.", color: "warning" })
|
||||
return
|
||||
}
|
||||
|
||||
const [counterType, counterId] = String(form.counter).split(":")
|
||||
savingBooking.value = true
|
||||
try {
|
||||
await useNuxtApp().$api(`/api/banking/cashbooks/${selectedCashbookId.value}/bookings`, {
|
||||
method: "POST",
|
||||
body: {
|
||||
date: form.date,
|
||||
direction: form.direction,
|
||||
amount: Number(form.amount),
|
||||
counterType,
|
||||
counterId,
|
||||
datevTaxKey: form.datevTaxKey === "__none__" ? null : form.datevTaxKey,
|
||||
description: form.description
|
||||
}
|
||||
})
|
||||
toast.add({ title: "Kassenbuchung erstellt." })
|
||||
form.amount = null
|
||||
form.counter = ""
|
||||
form.datevTaxKey = "__none__"
|
||||
form.description = ""
|
||||
counterSearch.value = ""
|
||||
expandedGroups.value = []
|
||||
await loadBookings()
|
||||
} finally {
|
||||
savingBooking.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const deleteBooking = async (booking) => {
|
||||
await useNuxtApp().$api(`/api/banking/cashbook-bookings/${booking.id}`, { method: "DELETE" })
|
||||
toast.add({ title: "Kassenbuchung gelöscht." })
|
||||
await loadBookings()
|
||||
}
|
||||
|
||||
onMounted(loadData)
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<UDashboardNavbar :title="selectedCashbook ? selectedCashbook.name : 'Kassenbuch'">
|
||||
<template #left>
|
||||
<UButton icon="i-heroicons-arrow-left" variant="ghost" color="neutral" @click="router.push('/accounting/cashbooks')">
|
||||
Kassenbücher
|
||||
</UButton>
|
||||
</template>
|
||||
<template #right>
|
||||
<div v-if="selectedCashbook" class="flex flex-wrap items-center justify-end gap-2">
|
||||
<UBadge color="neutral" variant="subtle">
|
||||
{{ selectedCashbook.name }}
|
||||
</UBadge>
|
||||
<UBadge color="neutral" variant="subtle">
|
||||
Konto {{ selectedCashbook.datevNumber }}
|
||||
</UBadge>
|
||||
<UBadge :color="currentBalance < 0 ? 'error' : 'success'" variant="subtle">
|
||||
Bestand {{ displayCurrency(currentBalance) }}
|
||||
</UBadge>
|
||||
</div>
|
||||
</template>
|
||||
</UDashboardNavbar>
|
||||
|
||||
<UDashboardPanelContent>
|
||||
<div v-if="loading" class="py-10 text-center text-gray-500">Lade Kassenbuch...</div>
|
||||
<div v-else-if="selectedCashbook" class="space-y-6">
|
||||
<UCard>
|
||||
<template #header>
|
||||
<div class="flex flex-col gap-3 lg:flex-row lg:items-end lg:justify-between">
|
||||
<div>
|
||||
<h2 class="font-semibold text-gray-900 dark:text-white">Neue Kassenbuchung</h2>
|
||||
<p class="text-sm text-gray-500">Einnahmen erhöhen den Kassenbestand, Ausgaben verringern ihn.</p>
|
||||
</div>
|
||||
<div class="grid gap-3 sm:grid-cols-3">
|
||||
<UFormField label="Buchungsdatum">
|
||||
<UInput v-model="form.date" type="date" />
|
||||
</UFormField>
|
||||
<UFormField label="Betrag">
|
||||
<UInput v-model="form.amount" type="number" min="0" step="0.01" placeholder="0,00" />
|
||||
</UFormField>
|
||||
<UFormField label="DATEV-Steuerschlüssel">
|
||||
<USelect
|
||||
v-model="form.datevTaxKey"
|
||||
:items="DATEV_TAX_KEY_ITEMS"
|
||||
value-key="value"
|
||||
label-key="label"
|
||||
class="w-full"
|
||||
/>
|
||||
</UFormField>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<div class="grid gap-6 xl:grid-cols-[240px_minmax(0,1fr)]">
|
||||
<div class="space-y-3">
|
||||
<button
|
||||
type="button"
|
||||
class="w-full rounded-lg border px-4 py-3 text-left transition"
|
||||
:class="form.direction === 'income'
|
||||
? 'border-emerald-500 bg-emerald-50 dark:bg-emerald-950/30'
|
||||
: 'border-gray-200 bg-white hover:border-gray-300 dark:border-gray-800 dark:bg-gray-900'"
|
||||
@click="form.direction = 'income'"
|
||||
>
|
||||
<div class="font-semibold text-emerald-700 dark:text-emerald-300">Einnahme</div>
|
||||
<div class="text-sm text-gray-500">Bargeld kommt in die Kasse.</div>
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="w-full rounded-lg border px-4 py-3 text-left transition"
|
||||
:class="form.direction === 'expense'
|
||||
? 'border-red-500 bg-red-50 dark:bg-red-950/30'
|
||||
: 'border-gray-200 bg-white hover:border-gray-300 dark:border-gray-800 dark:bg-gray-900'"
|
||||
@click="form.direction = 'expense'"
|
||||
>
|
||||
<div class="font-semibold text-red-700 dark:text-red-300">Ausgabe</div>
|
||||
<div class="text-sm text-gray-500">Bargeld wird aus der Kasse entnommen.</div>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="space-y-4">
|
||||
<div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div>
|
||||
<h3 class="font-semibold text-gray-900 dark:text-white">Gegenkonto</h3>
|
||||
<p class="text-sm text-gray-500">Sachkonto, Kreditor, Debitor, Eingangsbeleg oder zusätzliches Konto.</p>
|
||||
</div>
|
||||
<UInput v-model="counterSearch" icon="i-heroicons-magnifying-glass" placeholder="Gegenkonto durchsuchen..." class="max-w-sm" />
|
||||
</div>
|
||||
|
||||
<div class="space-y-4">
|
||||
<div v-for="group in groupedCounterEntries" :key="group.key" class="space-y-2">
|
||||
<div class="text-xs font-semibold uppercase tracking-wide text-gray-500">{{ group.label }}</div>
|
||||
<div class="grid gap-2 md:grid-cols-2">
|
||||
<button
|
||||
v-for="entry in visibleEntries(group)"
|
||||
:key="entry.key"
|
||||
type="button"
|
||||
class="rounded-lg border px-3 py-2 text-left transition"
|
||||
:class="form.counter === entry.key
|
||||
? 'border-primary-500 bg-primary-50 dark:bg-primary-950/30'
|
||||
: 'border-gray-200 bg-white hover:border-gray-300 dark:border-gray-800 dark:bg-gray-900'"
|
||||
@click="form.counter = entry.key"
|
||||
>
|
||||
<div class="flex items-center gap-2">
|
||||
<span class="font-mono text-sm">{{ entry.number }}</span>
|
||||
<span class="text-sm">{{ entry.name }}</span>
|
||||
</div>
|
||||
</button>
|
||||
</div>
|
||||
<UButton
|
||||
v-if="group.entries.length > 5"
|
||||
size="xs"
|
||||
color="neutral"
|
||||
variant="ghost"
|
||||
:label="isGroupExpanded(group.key) ? 'Weniger anzeigen' : `${group.entries.length - 5} weitere anzeigen`"
|
||||
@click="toggleGroupExpanded(group.key)"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<UAlert
|
||||
v-if="selectedCounter"
|
||||
color="primary"
|
||||
variant="soft"
|
||||
icon="i-heroicons-arrows-right-left"
|
||||
:title="form.direction === 'income' ? `${selectedCashbook.datevNumber} an ${selectedCounter.number}` : `${selectedCounter.number} an ${selectedCashbook.datevNumber}`"
|
||||
:description="`${selectedCounter.typeLabel.slice(0, -1)} ${selectedCounter.name} wird als Gegenkonto verwendet.`"
|
||||
/>
|
||||
|
||||
<UFormField label="Beschreibung">
|
||||
<UTextarea v-model="form.description" placeholder="z. B. Bareinkauf Büromaterial" autoresize class="w-full" />
|
||||
</UFormField>
|
||||
|
||||
<div class="flex justify-end">
|
||||
<UButton color="primary" icon="i-heroicons-check" :loading="savingBooking" @click="saveBooking">
|
||||
Kassenbuchung erstellen
|
||||
</UButton>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</UCard>
|
||||
|
||||
<UCard>
|
||||
<template #header>
|
||||
<div>
|
||||
<h2 class="font-semibold text-gray-900 dark:text-white">Kassenbewegungen</h2>
|
||||
<p class="text-sm text-gray-500">Alle Buchungen dieser Barkasse mit Gegenkonto.</p>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<div v-if="bookings.length === 0" class="py-10 text-center text-gray-500">Noch keine Kassenbuchungen erfasst.</div>
|
||||
<div v-else class="divide-y divide-gray-100 dark:divide-gray-800">
|
||||
<div v-for="booking in bookings" :key="booking.id" class="py-4">
|
||||
<div class="flex flex-wrap items-center justify-between gap-3">
|
||||
<div class="flex flex-wrap items-center gap-2 text-sm">
|
||||
<span class="font-medium">{{ dayjs(booking.date).format("DD.MM.YYYY") }}</span>
|
||||
<UBadge :color="Number(booking.amount) >= 0 ? 'success' : 'error'" variant="subtle">
|
||||
{{ Number(booking.amount) >= 0 ? "Einnahme" : "Ausgabe" }}
|
||||
</UBadge>
|
||||
<span class="font-mono font-semibold">{{ displayCurrency(Math.abs(Number(booking.amount || 0))) }}</span>
|
||||
<span class="text-gray-500">{{ getBookingCounter(booking).type }}</span>
|
||||
<span class="font-mono">{{ getBookingCounter(booking).number }}</span>
|
||||
<span>{{ getBookingCounter(booking).name }}</span>
|
||||
</div>
|
||||
<UButton
|
||||
icon="i-heroicons-trash"
|
||||
color="error"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
@click="deleteBooking(booking)"
|
||||
/>
|
||||
</div>
|
||||
<div v-if="booking.text" class="mt-2 text-sm text-gray-600 dark:text-gray-300">
|
||||
{{ booking.text }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</UCard>
|
||||
|
||||
</div>
|
||||
|
||||
<UCard v-else>
|
||||
<div class="py-16 text-center text-gray-500">Dieses Kassenbuch wurde nicht gefunden.</div>
|
||||
</UCard>
|
||||
</UDashboardPanelContent>
|
||||
</template>
|
||||
163
frontend/pages/accounting/cashbooks/index.vue
Normal file
163
frontend/pages/accounting/cashbooks/index.vue
Normal file
@@ -0,0 +1,163 @@
|
||||
<script setup>
|
||||
const toast = useToast()
|
||||
const router = useRouter()
|
||||
const tempStore = useTempStore()
|
||||
|
||||
const loading = ref(true)
|
||||
const savingCashbook = ref(false)
|
||||
const createCashbookModalOpen = ref(false)
|
||||
const cashbooks = ref([])
|
||||
const searchString = ref(tempStore.searchStrings["cashbooks"] || "")
|
||||
|
||||
const newCashbook = reactive({
|
||||
name: "",
|
||||
datevNumber: "1000",
|
||||
openingBalance: 0
|
||||
})
|
||||
|
||||
const displayCurrency = (value) => `${Number(value || 0).toFixed(2).replace(".", ",")} €`
|
||||
|
||||
const templateColumns = [
|
||||
{
|
||||
key: "name",
|
||||
label: "Bezeichnung"
|
||||
},
|
||||
{
|
||||
key: "datevNumber",
|
||||
label: "Kontennummer"
|
||||
},
|
||||
{
|
||||
key: "balance",
|
||||
label: "Anfangsbestand"
|
||||
},
|
||||
{
|
||||
key: "syncedAt",
|
||||
label: "Erstellt"
|
||||
}
|
||||
]
|
||||
|
||||
const filteredRows = computed(() => useSearch(searchString.value, cashbooks.value))
|
||||
|
||||
const clearSearchString = () => {
|
||||
tempStore.clearSearchString("cashbooks")
|
||||
searchString.value = ""
|
||||
}
|
||||
|
||||
const loadCashbooks = async () => {
|
||||
loading.value = true
|
||||
cashbooks.value = await useNuxtApp().$api("/api/banking/cashbooks")
|
||||
loading.value = false
|
||||
}
|
||||
|
||||
const resetCreateForm = () => {
|
||||
newCashbook.name = ""
|
||||
newCashbook.datevNumber = "1000"
|
||||
newCashbook.openingBalance = 0
|
||||
}
|
||||
|
||||
const createCashbook = async () => {
|
||||
if (!newCashbook.name || !newCashbook.datevNumber) {
|
||||
toast.add({ title: "Bitte Bezeichnung und Kontennummer ausfüllen.", color: "warning" })
|
||||
return
|
||||
}
|
||||
|
||||
savingCashbook.value = true
|
||||
try {
|
||||
const created = await useNuxtApp().$api("/api/banking/cashbooks", {
|
||||
method: "POST",
|
||||
body: {
|
||||
name: newCashbook.name,
|
||||
datevNumber: newCashbook.datevNumber,
|
||||
openingBalance: Number(newCashbook.openingBalance || 0)
|
||||
}
|
||||
})
|
||||
toast.add({ title: "Barkasse erstellt." })
|
||||
createCashbookModalOpen.value = false
|
||||
resetCreateForm()
|
||||
await loadCashbooks()
|
||||
router.push(`/accounting/cashbooks/${created.id}`)
|
||||
} finally {
|
||||
savingCashbook.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(loadCashbooks)
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<UDashboardNavbar title="Kassenbücher" :badge="filteredRows.length">
|
||||
<template #right>
|
||||
<UInput
|
||||
id="searchinput"
|
||||
v-model="searchString"
|
||||
icon="i-heroicons-magnifying-glass"
|
||||
autocomplete="off"
|
||||
placeholder="Suche..."
|
||||
class="hidden lg:block"
|
||||
@keydown.esc="$event.target.blur()"
|
||||
@change="tempStore.modifySearchString('cashbooks', searchString)"
|
||||
/>
|
||||
<UButton
|
||||
v-if="searchString.length > 0"
|
||||
icon="i-heroicons-x-mark"
|
||||
variant="outline"
|
||||
color="error"
|
||||
@click="clearSearchString"
|
||||
/>
|
||||
<UButton icon="i-heroicons-plus" @click="createCashbookModalOpen = true">
|
||||
Barkasse
|
||||
</UButton>
|
||||
</template>
|
||||
</UDashboardNavbar>
|
||||
|
||||
<UTable
|
||||
:data="filteredRows"
|
||||
:columns="normalizeTableColumns(templateColumns)"
|
||||
:loading="loading"
|
||||
class="w-full"
|
||||
:ui="{ divide: 'divide-gray-200 dark:divide-gray-800' }"
|
||||
:on-select="(row) => router.push(`/accounting/cashbooks/${row.original?.id || row.id}`)"
|
||||
:empty="{ icon: 'i-heroicons-banknotes', label: 'Keine Kassenbücher angelegt' }"
|
||||
>
|
||||
<template #datevNumber-cell="{ row }">
|
||||
<span class="font-mono">{{ row.original.datevNumber }}</span>
|
||||
</template>
|
||||
<template #balance-cell="{ row }">
|
||||
{{ displayCurrency(row.original.balance) }}
|
||||
</template>
|
||||
<template #syncedAt-cell="{ row }">
|
||||
{{ row.original.createdAt ? new Date(row.original.createdAt).toLocaleDateString("de-DE") : "-" }}
|
||||
</template>
|
||||
</UTable>
|
||||
|
||||
<UModal v-model:open="createCashbookModalOpen">
|
||||
<template #content>
|
||||
<UCard>
|
||||
<template #header>
|
||||
<div class="text-lg font-semibold">Barkasse anlegen</div>
|
||||
</template>
|
||||
|
||||
<UForm :state="newCashbook" class="space-y-4" @submit.prevent="createCashbook">
|
||||
<UFormField label="Bezeichnung">
|
||||
<UInput v-model="newCashbook.name" placeholder="z. B. Hauptkasse" />
|
||||
</UFormField>
|
||||
<UFormField label="Kontennummer">
|
||||
<UInput v-model="newCashbook.datevNumber" placeholder="1000" />
|
||||
</UFormField>
|
||||
<UFormField label="Anfangsbestand">
|
||||
<UInput v-model="newCashbook.openingBalance" type="number" step="0.01" />
|
||||
</UFormField>
|
||||
|
||||
<div class="flex justify-end gap-3 pt-2">
|
||||
<UButton color="gray" variant="soft" @click="createCashbookModalOpen = false">
|
||||
Abbrechen
|
||||
</UButton>
|
||||
<UButton type="submit" color="primary" :loading="savingCashbook">
|
||||
Barkasse anlegen
|
||||
</UButton>
|
||||
</div>
|
||||
</UForm>
|
||||
</UCard>
|
||||
</template>
|
||||
</UModal>
|
||||
</template>
|
||||
@@ -1,5 +1,6 @@
|
||||
<script setup>
|
||||
import CopyCreatedDocumentModal from "~/components/copyCreatedDocumentModal.vue";
|
||||
import dayjs from "dayjs";
|
||||
|
||||
|
||||
|
||||
@@ -20,17 +21,30 @@ const itemInfo = ref({})
|
||||
const linkedDocument =ref({})
|
||||
const links = ref([])
|
||||
const portalReleaseLoading = ref(false)
|
||||
const bankBookingModalOpen = ref(false)
|
||||
const portalEligibleTypes = ["invoices", "advanceInvoices", "cancellationInvoices"]
|
||||
|
||||
const loadStatementAllocations = async () => {
|
||||
if (!itemInfo.value?.id) return
|
||||
|
||||
const statementAllocations = await useEntities("statementallocations").select("*, bankstatement(*)")
|
||||
itemInfo.value.statementallocations = statementAllocations.filter((allocation) => {
|
||||
const createdDocumentId = allocation.createddocument?.id || allocation.createddocument
|
||||
|
||||
return String(createdDocumentId) === String(itemInfo.value.id)
|
||||
})
|
||||
}
|
||||
|
||||
const setupPage = async () => {
|
||||
if(route.params) {
|
||||
if(route.params.id) itemInfo.value = await useEntities("createddocuments").selectSingle(route.params.id,"*,files(*),linkedDocument(*), statementallocations(bs_id)")
|
||||
if(route.params.id) {
|
||||
itemInfo.value = await useEntities("createddocuments").selectSingle(route.params.id,"*,files(*),linkedDocument(*)")
|
||||
await loadStatementAllocations()
|
||||
}
|
||||
|
||||
if(itemInfo.value.type === "invoices"){
|
||||
const createddocuments = await useEntities("createddocuments").select()
|
||||
console.log(createddocuments)
|
||||
links.value = createddocuments.filter(i => i.createddocument?.id === itemInfo.value.id)
|
||||
console.log(links.value)
|
||||
}
|
||||
|
||||
linkedDocument.value = await useFiles().selectDocument(itemInfo.value.files[0].id)
|
||||
@@ -39,6 +53,106 @@ const setupPage = async () => {
|
||||
|
||||
setupPage()
|
||||
|
||||
const getStatementLike = (allocation) => allocation?.bankstatement || (typeof allocation?.bs_id === "object" ? allocation.bs_id : null)
|
||||
const getStatementId = (allocation) => allocation?.bankstatement?.id || allocation?.bs_id?.id || allocation?.bankstatement || allocation?.bs_id || null
|
||||
const getBankBookingDate = (allocation) => {
|
||||
const statement = getStatementLike(allocation)
|
||||
|
||||
return statement?.date || statement?.valueDate || allocation?.manualBookingDate || null
|
||||
}
|
||||
const formatDate = (value) => value ? dayjs(value).format("DD.MM.YYYY") : "-"
|
||||
const formatCurrency = (value) => `${Number(value || 0).toFixed(2).replace(".", ",")} EUR`
|
||||
const bankBookingDates = computed(() => [...new Set(
|
||||
(itemInfo.value.statementallocations || [])
|
||||
.map(getBankBookingDate)
|
||||
.filter(Boolean)
|
||||
)].sort())
|
||||
const bankBookingDateLabel = computed(() => {
|
||||
if (bankBookingDates.value.length === 0) return "Kein Bankbuchungsdatum"
|
||||
if (bankBookingDates.value.length === 1) return formatDate(bankBookingDates.value[0])
|
||||
|
||||
return bankBookingDates.value.map(formatDate).join(", ")
|
||||
})
|
||||
const linkedDocumentLabel = (document) => {
|
||||
const documentType = dataStore.documentTypesForCreation[document.type]?.labelSingle || "Dokument"
|
||||
|
||||
return `${documentType} - ${document.documentNumber || document.title || document.id}`
|
||||
}
|
||||
const linkedItems = computed(() => {
|
||||
const items = []
|
||||
|
||||
if(itemInfo.value.project) {
|
||||
items.push({
|
||||
label: "Projekt",
|
||||
icon: "i-heroicons-link",
|
||||
onSelect: () => router.push(`/standardEntity/projects/show/${itemInfo.value.project?.id}`)
|
||||
})
|
||||
}
|
||||
|
||||
if(itemInfo.value.customer) {
|
||||
items.push({
|
||||
label: "Kunde",
|
||||
icon: "i-heroicons-link",
|
||||
onSelect: () => router.push(`/standardEntity/customers/show/${itemInfo.value.customer?.id}`)
|
||||
})
|
||||
}
|
||||
|
||||
if(itemInfo.value.plant) {
|
||||
items.push({
|
||||
label: "Objekt",
|
||||
icon: "i-heroicons-link",
|
||||
onSelect: () => router.push(`/standardEntity/plants/show/${itemInfo.value.plant?.id}`)
|
||||
})
|
||||
}
|
||||
|
||||
if(itemInfo.value.contract) {
|
||||
items.push({
|
||||
label: "Vertrag",
|
||||
icon: "i-heroicons-link",
|
||||
onSelect: () => router.push(`/standardEntity/contracts/show/${itemInfo.value.contract?.id}`)
|
||||
})
|
||||
}
|
||||
|
||||
if(itemInfo.value.contact) {
|
||||
items.push({
|
||||
label: "Ansprechpartner",
|
||||
icon: "i-heroicons-link",
|
||||
onSelect: () => router.push(`/standardEntity/contacts/show/${itemInfo.value.contact?.id}`)
|
||||
})
|
||||
}
|
||||
|
||||
if(itemInfo.value.createddocument) {
|
||||
items.push({
|
||||
label: linkedDocumentLabel(itemInfo.value.createddocument),
|
||||
icon: "i-heroicons-document-duplicate",
|
||||
onSelect: () => router.push(`/createDocument/show/${itemInfo.value.createddocument.id}`)
|
||||
})
|
||||
}
|
||||
|
||||
;(itemInfo.value.createddocuments || []).forEach((document) => {
|
||||
items.push({
|
||||
label: linkedDocumentLabel(document),
|
||||
icon: "i-heroicons-document-duplicate",
|
||||
onSelect: () => router.push(`/createDocument/show/${document.id}`)
|
||||
})
|
||||
})
|
||||
|
||||
if(itemInfo.value.statementallocations?.length > 0) {
|
||||
items.push({
|
||||
label: "Bankbuchungen öffnen",
|
||||
icon: "i-heroicons-building-library",
|
||||
onSelect: openBankstatements
|
||||
})
|
||||
items.push({
|
||||
label: "Bankdetails anzeigen",
|
||||
icon: "i-heroicons-calendar-days",
|
||||
onSelect: () => { bankBookingModalOpen.value = true }
|
||||
})
|
||||
}
|
||||
|
||||
return [items]
|
||||
})
|
||||
|
||||
const openEmail = () => {
|
||||
if(["invoices","advanceInvoices"].includes(itemInfo.value.type)){
|
||||
router.push(`/email/new?loadDocuments=["${linkedDocument.value.id}"]&bcc=${encodeURIComponent(auth.activeTenantData.standardEmailForInvoices || "")}`)
|
||||
@@ -48,10 +162,14 @@ const openEmail = () => {
|
||||
}
|
||||
|
||||
const openBankstatements = () => {
|
||||
if(itemInfo.value.statementallocations.length > 1) {
|
||||
navigateTo(`/banking/?filter=${JSON.stringify(itemInfo.value.statementallocations.map(i => i.bankstatement))}`)
|
||||
const statementIds = (itemInfo.value.statementallocations || []).map(getStatementId).filter(Boolean)
|
||||
|
||||
if(statementIds.length === 0) return
|
||||
|
||||
if(statementIds.length > 1) {
|
||||
navigateTo(`/banking/?filter=${JSON.stringify(statementIds)}`)
|
||||
} else {
|
||||
navigateTo(`/banking/statements/edit/${itemInfo.value.statementallocations[0].bankstatement}`)
|
||||
navigateTo(`/banking/statements/edit/${statementIds[0]}`)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,74 +255,68 @@ const togglePortalRelease = async () => {
|
||||
</UButton>
|
||||
</UTooltip>
|
||||
|
||||
<UButton
|
||||
v-if="itemInfo.project"
|
||||
@click="router.push(`/standardEntity/projects/show/${itemInfo.project?.id}`)"
|
||||
icon="i-heroicons-link"
|
||||
variant="outline"
|
||||
<UDropdownMenu
|
||||
v-if="linkedItems[0].length > 0"
|
||||
:items="linkedItems"
|
||||
:content="{ align: 'start' }"
|
||||
>
|
||||
Projekt
|
||||
</UButton>
|
||||
<UButton
|
||||
v-if="itemInfo.customer"
|
||||
@click="router.push(`/standardEntity/customers/show/${itemInfo.customer?.id}`)"
|
||||
icon="i-heroicons-link"
|
||||
variant="outline"
|
||||
>
|
||||
Kunde
|
||||
</UButton>
|
||||
<UButton
|
||||
v-if="itemInfo.plant"
|
||||
@click="router.push(`/standardEntity/plants/show/${itemInfo.plant?.id}`)"
|
||||
icon="i-heroicons-link"
|
||||
variant="outline"
|
||||
>
|
||||
Objekt
|
||||
</UButton>
|
||||
<UButton
|
||||
v-if="itemInfo.contract"
|
||||
@click="router.push(`/standardEntity/contracts/show/${itemInfo.contract?.id}`)"
|
||||
icon="i-heroicons-link"
|
||||
variant="outline"
|
||||
>
|
||||
Vertrag
|
||||
</UButton>
|
||||
<UButton
|
||||
v-if="itemInfo.contact"
|
||||
@click="router.push(`/standardEntity/contacts/show/${itemInfo.contact?.id}`)"
|
||||
icon="i-heroicons-link"
|
||||
variant="outline"
|
||||
>
|
||||
Ansprechpartner
|
||||
</UButton>
|
||||
<UButton
|
||||
v-if="itemInfo.createddocument"
|
||||
@click="router.push(`/createDocument/show/${itemInfo.createddocument.id}`)"
|
||||
icon="i-heroicons-link"
|
||||
variant="outline"
|
||||
>
|
||||
{{dataStore.documentTypesForCreation[itemInfo.createddocument.type].labelSingle}} - {{itemInfo.createddocument.documentNumber}}
|
||||
</UButton>
|
||||
<UButton
|
||||
v-for="item in itemInfo.createddocuments"
|
||||
v-if="itemInfo.createddocuments"
|
||||
@click="router.push(`/createDocument/show/${item.id}`)"
|
||||
icon="i-heroicons-link"
|
||||
variant="outline"
|
||||
>
|
||||
{{dataStore.documentTypesForCreation[item.type].labelSingle}} - {{item.documentNumber}}
|
||||
</UButton>
|
||||
<UButton
|
||||
<UButton
|
||||
icon="i-heroicons-link"
|
||||
variant="outline"
|
||||
trailing-icon="i-heroicons-chevron-down-20-solid"
|
||||
>
|
||||
Verknüpfungen
|
||||
</UButton>
|
||||
</UDropdownMenu>
|
||||
<UBadge
|
||||
v-if="itemInfo.statementallocations?.length > 0"
|
||||
@click="openBankstatements"
|
||||
icon="i-heroicons-link"
|
||||
variant="outline"
|
||||
color="primary"
|
||||
variant="soft"
|
||||
class="self-center"
|
||||
>
|
||||
Bankbuchungen
|
||||
</UButton>
|
||||
Bankbuchungsdatum: {{ bankBookingDateLabel }}
|
||||
</UBadge>
|
||||
|
||||
</template>
|
||||
</UDashboardToolbar>
|
||||
<UModal v-model:open="bankBookingModalOpen" :ui="{ content: 'sm:max-w-2xl' }">
|
||||
<template #content>
|
||||
<UCard>
|
||||
<template #header>
|
||||
<div class="flex items-center justify-between">
|
||||
<h3 class="font-semibold">Bankbuchungen</h3>
|
||||
<UButton
|
||||
icon="i-heroicons-x-mark"
|
||||
color="gray"
|
||||
variant="ghost"
|
||||
@click="bankBookingModalOpen = false"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<div class="space-y-3">
|
||||
<div
|
||||
v-for="allocation in itemInfo.statementallocations"
|
||||
:key="allocation.id"
|
||||
class="grid grid-cols-1 gap-2 rounded-md border border-gray-200 p-3 text-sm dark:border-gray-800 md:grid-cols-3"
|
||||
>
|
||||
<div>
|
||||
<p class="text-xs text-gray-500">Bankbuchungsdatum</p>
|
||||
<p class="font-medium">{{ formatDate(getBankBookingDate(allocation)) }}</p>
|
||||
</div>
|
||||
<div>
|
||||
<p class="text-xs text-gray-500">Betrag</p>
|
||||
<p class="font-medium">{{ formatCurrency(allocation.amount) }}</p>
|
||||
</div>
|
||||
<div>
|
||||
<p class="text-xs text-gray-500">Text</p>
|
||||
<p class="font-medium">{{ getStatementLike(allocation)?.text || allocation.description || "-" }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</UCard>
|
||||
</template>
|
||||
</UModal>
|
||||
<UDashboardPanelContent>
|
||||
<div
|
||||
v-if="portalEligibleTypes.includes(itemInfo.type) && itemInfo.state !== 'Entwurf'"
|
||||
|
||||
@@ -71,11 +71,7 @@ const setupPage = async () => {
|
||||
documents.value = dRes || []
|
||||
filetags.value = tRes || []
|
||||
|
||||
if (route.query?.folder) {
|
||||
currentFolder.value = folders.value.find(i => i.id === route.query.folder) || null
|
||||
} else {
|
||||
currentFolder.value = null
|
||||
}
|
||||
syncCurrentFolderFromRoute()
|
||||
} finally {
|
||||
loadingDocs.value = false
|
||||
loaded.value = true
|
||||
@@ -140,9 +136,24 @@ onUnmounted(() => {
|
||||
|
||||
|
||||
// --- Navigation ---
|
||||
const getRouteFolderId = () => {
|
||||
const folder = route.query?.folder
|
||||
return Array.isArray(folder) ? folder[0] : folder
|
||||
}
|
||||
|
||||
const syncCurrentFolderFromRoute = () => {
|
||||
const folderId = getRouteFolderId()
|
||||
currentFolder.value = folderId ? folders.value.find(i => i.id === folderId) || null : null
|
||||
selectedFileIndex.value = 0
|
||||
}
|
||||
|
||||
const changeFolder = async (folder) => {
|
||||
currentFolder.value = folder
|
||||
await router.push(folder ? `/files?folder=${folder.id}` : `/files`)
|
||||
currentFolder.value = folder || null
|
||||
selectedFileIndex.value = 0
|
||||
await router.push({
|
||||
path: '/files',
|
||||
query: folder ? { folder: folder.id } : {}
|
||||
})
|
||||
}
|
||||
|
||||
const navigateUp = () => {
|
||||
@@ -151,6 +162,10 @@ const navigateUp = () => {
|
||||
changeFolder(parent || null)
|
||||
}
|
||||
|
||||
watch(() => route.query?.folder, () => {
|
||||
syncCurrentFolderFromRoute()
|
||||
})
|
||||
|
||||
// --- Drag & Drop (Internal Sort) ---
|
||||
const handleDragStart = (entry) => {
|
||||
draggedItem.value = entry
|
||||
@@ -178,23 +193,40 @@ const handleDrop = async (targetFolderId) => {
|
||||
}
|
||||
|
||||
// --- Breadcrumbs ---
|
||||
const breadcrumbLinks = computed(() => {
|
||||
const links = [{label: "Home", icon: "i-heroicons-home", click: () => changeFolder(null)}]
|
||||
if (currentFolder.value) {
|
||||
const path = []
|
||||
let curr = currentFolder.value
|
||||
while (curr) {
|
||||
const folderObj = curr
|
||||
path.unshift({
|
||||
label: folderObj.name,
|
||||
icon: "i-heroicons-folder",
|
||||
click: () => changeFolder(folderObj)
|
||||
})
|
||||
curr = folders.value.find(f => f.id === curr.parent)
|
||||
}
|
||||
return [...links, ...path]
|
||||
const breadcrumbItems = computed(() => {
|
||||
const items = [{
|
||||
label: "Dateien",
|
||||
icon: "i-heroicons-home",
|
||||
to: "/files",
|
||||
exact: true
|
||||
}]
|
||||
|
||||
if (!currentFolder.value) return items
|
||||
|
||||
const path = []
|
||||
const seenFolderIds = new Set()
|
||||
let curr = currentFolder.value
|
||||
|
||||
while (curr && !seenFolderIds.has(curr.id)) {
|
||||
seenFolderIds.add(curr.id)
|
||||
path.unshift(curr)
|
||||
curr = folders.value.find(f => f.id === curr.parent)
|
||||
}
|
||||
return links
|
||||
|
||||
return [
|
||||
...items,
|
||||
...path.map((folder, index) => ({
|
||||
label: folder.name,
|
||||
icon: "i-heroicons-folder",
|
||||
to: {
|
||||
path: "/files",
|
||||
query: { folder: folder.id }
|
||||
},
|
||||
exact: true,
|
||||
exactQuery: true,
|
||||
disabled: index === path.length - 1
|
||||
}))
|
||||
]
|
||||
})
|
||||
|
||||
// --- Data Mapping ---
|
||||
@@ -366,7 +398,7 @@ const syncdokubox = async () => {
|
||||
|
||||
<UDashboardToolbar class="sticky top-0 z-30 bg-white dark:bg-gray-900 border-b border-gray-200 dark:border-gray-800">
|
||||
<template #left>
|
||||
<UBreadcrumb :links="breadcrumbLinks"/>
|
||||
<UBreadcrumb :items="breadcrumbItems"/>
|
||||
</template>
|
||||
<template #right>
|
||||
<USelectMenu v-model="displayMode" :items="displayModes" value-key="key" class="w-32" :content="{ zIndex: 'z-50' }">
|
||||
|
||||
@@ -56,7 +56,7 @@ const setup = async () => {
|
||||
vendors.value = await useEntities("vendors").select()
|
||||
accounts.value = await useEntities("accounts").selectSpecial()
|
||||
|
||||
const invoiceData = await useEntities("incominginvoices").selectSingle(route.params.id, "*, files(*)")
|
||||
const invoiceData = await useEntities("incominginvoices").selectSingle(route.params.id, "*, files(*), statementallocations(*, bs_id(*))")
|
||||
|
||||
// 2. Mapping
|
||||
itemInfo.value = {
|
||||
@@ -129,6 +129,34 @@ const setDateFieldToToday = (field) => {
|
||||
}
|
||||
|
||||
const getDateButtonLabel = (value, emptyLabel = "Kein Datum") => value ? dayjs(value).format('DD.MM.YYYY') : emptyLabel
|
||||
const formatDate = (value) => value ? dayjs(value).format("DD.MM.YYYY") : "-"
|
||||
const formatMoney = (value) => `${Number(value || 0).toFixed(2).replace(".", ",")} €`
|
||||
const getStatementLike = (allocation) => allocation?.bankstatement || (typeof allocation?.bs_id === "object" ? allocation.bs_id : null)
|
||||
const getBankBookingDate = (allocation) => {
|
||||
const statement = getStatementLike(allocation)
|
||||
|
||||
return statement?.date || statement?.valueDate || allocation?.manualBookingDate || null
|
||||
}
|
||||
const bankBookingDates = computed(() => [...new Set(
|
||||
(itemInfo.value.statementallocations || [])
|
||||
.map(getBankBookingDate)
|
||||
.filter(Boolean)
|
||||
)].sort())
|
||||
const bankBookingDateLabel = computed(() => {
|
||||
if (bankBookingDates.value.length === 0) return "Nicht zugewiesen"
|
||||
if (bankBookingDates.value.length === 1) return formatDate(bankBookingDates.value[0])
|
||||
|
||||
return bankBookingDates.value.map(formatDate).join(", ")
|
||||
})
|
||||
const vendorName = computed(() => vendors.value.find((vendor) => vendor.id === itemInfo.value.vendor)?.name || "-")
|
||||
const getAccountLabel = (item) => {
|
||||
const account = accounts.value.find((entry) => entry.id === item.account)
|
||||
|
||||
return account ? `${account.number || ""} ${account.label || ""}`.trim() : "-"
|
||||
}
|
||||
const getCostCentreLabel = (item) => costcentres.value.find((costcentre) => costcentre.id === item.costCentre)?.name || "-"
|
||||
const getBookingModeLabel = (item) => EXPENSE_BOOKING_MODE_ITEMS.find((mode) => mode.value === item.bookingMode)?.label || "-"
|
||||
const getTaxLabel = (item) => taxOptions.value.find((tax) => tax.key === item.taxType)?.label || "-"
|
||||
|
||||
const totalCalculated = computed(() => {
|
||||
let totalNet = 0
|
||||
@@ -329,7 +357,7 @@ const hasBlockingIncomingInvoiceErrors = computed(() => blockingIncomingInvoiceE
|
||||
</UButton>
|
||||
|
||||
<UAlert
|
||||
v-if="findIncomingInvoiceErrors.length > 0"
|
||||
v-if="mode !== 'show' && findIncomingInvoiceErrors.length > 0"
|
||||
title="Prüfung erforderlich"
|
||||
:color="hasBlockingIncomingInvoiceErrors ? 'error' : 'orange'"
|
||||
variant="soft"
|
||||
@@ -363,7 +391,79 @@ const hasBlockingIncomingInvoiceErrors = computed(() => blockingIncomingInvoiceE
|
||||
</template>
|
||||
</UAlert>
|
||||
|
||||
<UCard>
|
||||
<div v-if="mode === 'show'" class="space-y-6">
|
||||
<div class="rounded-md border border-gray-200 bg-white p-5 dark:border-gray-800 dark:bg-gray-900">
|
||||
<div class="mb-4 flex items-start justify-between gap-4">
|
||||
<div>
|
||||
<p class="text-sm text-gray-500">Eingangsbeleg</p>
|
||||
<h2 class="text-xl font-semibold">{{ itemInfo.reference || "-" }}</h2>
|
||||
</div>
|
||||
<UBadge :color="itemInfo.state === 'Gebucht' ? 'primary' : 'neutral'" variant="soft">
|
||||
{{ itemInfo.state || "Entwurf" }}
|
||||
</UBadge>
|
||||
</div>
|
||||
|
||||
<dl class="grid grid-cols-1 gap-4 text-sm md:grid-cols-2">
|
||||
<div>
|
||||
<dt class="text-gray-500">Lieferant / Partner</dt>
|
||||
<dd class="font-medium">{{ vendorName }}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt class="text-gray-500">Zahlart</dt>
|
||||
<dd class="font-medium">{{ itemInfo.paymentType || "-" }}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt class="text-gray-500">Rechnungsdatum</dt>
|
||||
<dd class="font-medium">{{ formatDate(itemInfo.date) }}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt class="text-gray-500">Fälligkeitsdatum</dt>
|
||||
<dd class="font-medium">{{ formatDate(itemInfo.dueDate) }}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt class="text-gray-500">Bankbuchungsdatum</dt>
|
||||
<dd class="font-medium">{{ bankBookingDateLabel }}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt class="text-gray-500">Art</dt>
|
||||
<dd class="font-medium">{{ itemInfo.expense ? "Ausgabe" : "Einnahme" }}</dd>
|
||||
</div>
|
||||
<div class="md:col-span-2">
|
||||
<dt class="text-gray-500">Beschreibung / Notiz</dt>
|
||||
<dd class="font-medium whitespace-pre-wrap">{{ itemInfo.description || "-" }}</dd>
|
||||
</div>
|
||||
</dl>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-if="itemInfo.statementallocations?.length > 0"
|
||||
class="rounded-md border border-gray-200 bg-white p-5 dark:border-gray-800 dark:bg-gray-900"
|
||||
>
|
||||
<h3 class="mb-4 font-semibold">Bankbuchungen</h3>
|
||||
<div class="space-y-3">
|
||||
<div
|
||||
v-for="allocation in itemInfo.statementallocations"
|
||||
:key="allocation.id"
|
||||
class="grid grid-cols-1 gap-2 rounded-md border border-gray-200 p-3 text-sm dark:border-gray-800 md:grid-cols-3"
|
||||
>
|
||||
<div>
|
||||
<p class="text-xs text-gray-500">Bankbuchungsdatum</p>
|
||||
<p class="font-medium">{{ formatDate(getBankBookingDate(allocation)) }}</p>
|
||||
</div>
|
||||
<div>
|
||||
<p class="text-xs text-gray-500">Betrag</p>
|
||||
<p class="font-medium">{{ formatMoney(allocation.amount) }}</p>
|
||||
</div>
|
||||
<div>
|
||||
<p class="text-xs text-gray-500">Text</p>
|
||||
<p class="font-medium">{{ getStatementLike(allocation)?.text || allocation.description || "-" }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<UCard v-if="mode !== 'show'">
|
||||
<template #header>
|
||||
<div class="flex justify-between items-center">
|
||||
<h3 class="font-semibold">Stammdaten</h3>
|
||||
@@ -492,7 +592,54 @@ const hasBlockingIncomingInvoiceErrors = computed(() => blockingIncomingInvoiceE
|
||||
</div>
|
||||
</UCard>
|
||||
|
||||
<div class="space-y-4">
|
||||
<div v-if="mode === 'show'" class="space-y-4">
|
||||
<div class="flex justify-between items-end border-b pb-2 dark:border-gray-700">
|
||||
<h3 class="font-semibold text-lg">Positionen</h3>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-for="(item, index) in itemInfo.accounts"
|
||||
:key="`show-${index}`"
|
||||
class="rounded-md border border-gray-200 bg-white p-4 text-sm dark:border-gray-800 dark:bg-gray-900"
|
||||
>
|
||||
<div class="mb-3 flex items-start justify-between gap-4">
|
||||
<div>
|
||||
<p class="text-xs text-gray-500">Position {{ index + 1 }}</p>
|
||||
<p class="font-medium">{{ item.description || "Ohne Positionstext" }}</p>
|
||||
</div>
|
||||
<UBadge variant="soft" color="neutral">{{ getBookingModeLabel(item) }}</UBadge>
|
||||
</div>
|
||||
|
||||
<dl class="grid grid-cols-1 gap-3 md:grid-cols-3">
|
||||
<div>
|
||||
<dt class="text-gray-500">Konto / Kategorie</dt>
|
||||
<dd class="font-medium">{{ getAccountLabel(item) }}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt class="text-gray-500">Kostenstelle</dt>
|
||||
<dd class="font-medium">{{ getCostCentreLabel(item) }}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt class="text-gray-500">Steuerschlüssel</dt>
|
||||
<dd class="font-medium">{{ getTaxLabel(item) }}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt class="text-gray-500">Netto</dt>
|
||||
<dd class="font-medium">{{ formatMoney(item.amountNet) }}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt class="text-gray-500">Steuerbetrag</dt>
|
||||
<dd class="font-medium">{{ formatMoney(item.amountTax) }}</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt class="text-gray-500">Brutto</dt>
|
||||
<dd class="font-medium">{{ formatMoney(item.amountGross) }}</dd>
|
||||
</div>
|
||||
</dl>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-if="mode !== 'show'" class="space-y-4">
|
||||
<div class="flex justify-between items-end border-b pb-2 dark:border-gray-700">
|
||||
<h3 class="font-semibold text-lg">Positionen</h3>
|
||||
<div class="flex items-center gap-2 text-sm">
|
||||
|
||||
@@ -297,6 +297,7 @@ export const useDataStore = defineStore('data', () => {
|
||||
label: "Aktiv",
|
||||
component: active,
|
||||
inputType: "bool",
|
||||
defaultValue: true,
|
||||
inputColumn: "Allgemeines",
|
||||
sortable: true,
|
||||
distinct: true
|
||||
@@ -383,6 +384,7 @@ export const useDataStore = defineStore('data', () => {
|
||||
selectDataType: "countrys",
|
||||
selectOptionAttribute: "name",
|
||||
selectValueAttribute: "name",
|
||||
defaultValue: "Deutschland",
|
||||
disabledInTable: true,
|
||||
inputColumn: "Kontaktdaten",
|
||||
sortable: true
|
||||
@@ -559,6 +561,7 @@ export const useDataStore = defineStore('data', () => {
|
||||
label: "Aktiv",
|
||||
component: active,
|
||||
inputType: "bool",
|
||||
defaultValue: true,
|
||||
inputColumn: "Allgemeines",
|
||||
sortable: true,
|
||||
distinct: true
|
||||
@@ -641,6 +644,7 @@ export const useDataStore = defineStore('data', () => {
|
||||
selectDataType: "countrys",
|
||||
selectOptionAttribute: "name",
|
||||
selectValueAttribute: "name",
|
||||
defaultValue: "Deutschland",
|
||||
disabledInTable: true,
|
||||
inputColumn: "Bank & Kontakt"
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user