diff --git a/backend/src/routes/resources/main.ts b/backend/src/routes/resources/main.ts index 419ab42..cf01f73 100644 --- a/backend/src/routes/resources/main.ts +++ b/backend/src/routes/resources/main.ts @@ -11,7 +11,14 @@ import { sql, } from "drizzle-orm" -import { authProfiles, costcentres, customers, entitybankaccounts } from "../../../db/schema"; +import { + authProfiles, + costcentres, + createddocuments, + customers, + entitybankaccounts, + statementallocations, +} from "../../../db/schema"; import { resourceConfig } from "../../utils/resource.config"; import { useNextNumberRangeNumber } from "../../utils/functions"; import { getHistoryEntityLabel, insertHistoryItem } from "../../utils/history"; @@ -495,6 +502,97 @@ async function validateOutgoingSepaMandatePayload( export default async function resourceRoutes(server: FastifyInstance) { + // ------------------------------------------------------------- + // CREATED DOCUMENT LIST + // ------------------------------------------------------------- + server.get("/resource/createddocuments/list", async (req, reply) => { + try { + const tenantId = req.user?.tenant_id + if (!tenantId) { + return reply.code(400).send({ error: "No tenant selected" }) + } + + const portalCustomerId = await getPortalCustomerId(server, req) + let whereCond: any = eq(createddocuments.tenant, tenantId) + whereCond = applyResourceWhereFilters("createddocuments", createddocuments, whereCond) + whereCond = applyPortalScope( + "createddocuments", + createddocuments, + whereCond, + portalCustomerId + ) + + const documentRows = await server.db + .select({ + id: createddocuments.id, + createdAt: createddocuments.createdAt, + type: createddocuments.type, + documentNumber: createddocuments.documentNumber, + documentDate: createddocuments.documentDate, + state: createddocuments.state, + rows: createddocuments.rows, + paymentDays: createddocuments.paymentDays, + linkedDocumentId: createddocuments.createddocument, + usedAdvanceInvoices: createddocuments.usedAdvanceInvoices, + archived: createddocuments.archived, + customerId: customers.id, + customerName: customers.name, + }) + .from(createddocuments) + .leftJoin(customers, eq(createddocuments.customer, customers.id)) + .where(whereCond) + .orderBy(asc(createddocuments.documentNumber)) + + if (documentRows.length === 0) { + return [] + } + + const documentIds = documentRows.map((document) => document.id) + const allocationRows = await server.db + .select({ + id: statementallocations.id, + createddocument: statementallocations.createddocument, + amount: statementallocations.amount, + }) + .from(statementallocations) + .where(and( + eq(statementallocations.tenant, tenantId), + inArray(statementallocations.createddocument, documentIds) + )) + + const allocationsByDocument = new Map() + for (const allocation of allocationRows) { + if (allocation.createddocument === null) continue + const allocations = allocationsByDocument.get(allocation.createddocument) || [] + allocations.push(allocation) + allocationsByDocument.set(allocation.createddocument, allocations) + } + + return documentRows.map((document) => ({ + id: document.id, + createdAt: document.createdAt, + type: document.type, + documentNumber: document.documentNumber, + documentDate: document.documentDate, + state: document.state, + rows: document.rows || [], + paymentDays: document.paymentDays, + createddocument: document.linkedDocumentId + ? { id: document.linkedDocumentId } + : null, + usedAdvanceInvoices: document.usedAdvanceInvoices || [], + archived: document.archived, + customer: document.customerId + ? { id: document.customerId, name: document.customerName } + : null, + statementallocations: allocationsByDocument.get(document.id) || [], + })) + } catch (err) { + server.log.error({ err }, "Failed to load created document list") + return reply.code(500).send({ error: "Internal Server Error" }) + } + }) + // ------------------------------------------------------------- // LIST // ------------------------------------------------------------- diff --git a/frontend/pages/createDocument/index.vue b/frontend/pages/createDocument/index.vue index 99cfe00..27e2b3d 100644 --- a/frontend/pages/createDocument/index.vue +++ b/frontend/pages/createDocument/index.vue @@ -204,7 +204,9 @@ defineShortcuts({ }) const setupPage = async () => { - items.value = (await useEntities("createddocuments").select("*, customer(id,name), statementallocations(id,amount),linkedDocument(*)", "documentNumber", true, true)) + items.value = await useNuxtApp().$api('/api/resource/createddocuments/list', { + method: 'GET' + }) || [] } setupPage()