Nach Kostenstelle weiterleiten
Die Kostenstelle einer Rechnung entscheidet, wer freigibt: Kostenstellen mit 41 am Anfang gibt der Einkauf frei, mit 43 der Vertrieb, alle anderen die Buchhaltung. Das Feld Kostenstelle deklariert der Workflow selbst; fehlt es, gibt ebenfalls die Buchhaltung frei.
Der Workflow
Abschnitt betitelt „Der Workflow“Der Workflow startet für jedes Dokument vom Typ incoming-invoice. Seine Datei workflow.yaml:
$schema: 'https://docs.acriva.de/schemas/workflow.schema.json'format: 1schemaVersion: 4sdk: '1.0.0'key: route-by-cost-centername: Nach Kostenstelle weiterleitentemplate: customtrigger: documentType: incoming-invoicefields: - key: costCenter kind: text labelDe: Kostenstelle labelEn: Cost center documentTypes: - incoming-invoicestart: routesteps: - kind: script name: route label: Nach Kostenstelle weiterleiten script: route.ts hook: onDocumentArrived inputs: - kind: field key: costCenter type: text returns: - key: group type: enumeration values: - purchasing - sales - accounting branch: group exits: - value: purchasing next: purchasing - value: sales next: sales - value: accounting next: accounting next: accounting - kind: humanTask name: purchasing label: Freigabe Einkauf taskKind: approve assignees: - when: null assignee: kind: member member: einkauf@example.com fallback: kind: role role: Buchhaltung options: excludeRequester: true excludeActorsOfSteps: [] pickFile: false next: approved onRejected: rejected - kind: humanTask name: sales label: Freigabe Vertrieb taskKind: approve assignees: - when: null assignee: kind: role role: approver fallback: kind: role role: Buchhaltung options: excludeRequester: true excludeActorsOfSteps: [] pickFile: false next: approved onRejected: rejected - kind: humanTask name: accounting label: Freigabe Buchhaltung taskKind: approve assignees: [] fallback: kind: role role: Buchhaltung options: excludeRequester: true excludeActorsOfSteps: [] pickFile: false next: approved onRejected: rejected - kind: end name: approved label: Freigegeben outcome: approved - kind: end name: rejected label: Zurückgewiesen outcome: rejectedDas Skript route.ts
Abschnitt betitelt „Das Skript route.ts“Hook: onDocumentArrived · Eingaben: costCenter · Rückgaben: group (purchasing, sales, accounting)
import { defineScript } from '@acriva/workflow-sdk';
/** Which group approves: the cost centers whose number starts with the prefix. */const GROUPS = [ { prefix: '41', group: 'purchasing' }, { prefix: '43', group: 'sales' },] as const;
export default defineScript({ hook: 'onDocumentArrived', outputs: { group: ['purchasing', 'sales', 'accounting'] }, run(ctx) { const costCenter = ctx.document.fields.costCenter?.trim() ?? ''; const match = GROUPS.find((entry) => costCenter.startsWith(entry.prefix)); // Every other cost center, and an invoice without one, goes to accounting. if (!match) { ctx.log('No group for the cost center', costCenter || '(empty)'); return ctx.branch('accounting'); } return ctx.branch(match.group); },});Was es tut
Abschnitt betitelt „Was es tut“Ein Test führt jeden dieser Fälle in der Sandbox aus und vergleicht das Ergebnis.
Eine Rechnung ohne Kostenstelle
Abschnitt betitelt „Eine Rechnung ohne Kostenstelle“- Eingabe:
costCenternull - Ausgang:
accounting - Änderungen: keine
Kostenstelle 4200 (Verwaltung)
Abschnitt betitelt „Kostenstelle 4200 (Verwaltung)“- Eingabe:
costCenter4200 - Ausgang:
accounting - Änderungen: keine
Kostenstelle 4100 (Einkauf)
Abschnitt betitelt „Kostenstelle 4100 (Einkauf)“- Eingabe:
costCenter4100 - Ausgang:
purchasing - Änderungen: keine
Kostenstelle 4300 (Vertrieb)
Abschnitt betitelt „Kostenstelle 4300 (Vertrieb)“- Eingabe:
costCenter4300 - Ausgang:
sales - Änderungen: keine