Zum Inhalt springen

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 startet für jedes Dokument vom Typ incoming-invoice. Seine Datei workflow.yaml:

$schema: 'https://docs.acriva.de/schemas/workflow.schema.json'
format: 1
schemaVersion: 4
sdk: '1.0.0'
key: route-by-cost-center
name: Nach Kostenstelle weiterleiten
template: custom
trigger:
documentType: incoming-invoice
fields:
- key: costCenter
kind: text
labelDe: Kostenstelle
labelEn: Cost center
documentTypes:
- incoming-invoice
start: route
steps:
- 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: rejected

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);
},
});

Ein Test führt jeden dieser Fälle in der Sandbox aus und vergleicht das Ergebnis.

  • Eingabe: costCenter null
  • Ausgang: accounting
  • Änderungen: keine
  • Eingabe: costCenter 4200
  • Ausgang: accounting
  • Änderungen: keine
  • Eingabe: costCenter 4100
  • Ausgang: purchasing
  • Änderungen: keine
  • Eingabe: costCenter 4300
  • Ausgang: sales
  • Änderungen: keine