Skip to content

Cost center mapping from a data table

Like Cost center from the vendor, only the mapping isn’t in the script but in a data table kostenstellenZuordnung: one row per mapping with the vendor, the cost center and the G/L account, which accounting changes without a new version of the workflow, or uploads from Excel. The script queries every row of the vendor. With exactly one it sets the cost center and the G/L account, two fields the workflow declares itself; with none or several the approval gets a hint, so the invoice is allocated or split by hand. The vendor must be in the table as it is recognized on the invoice. When the table has one row per vendor, a card sets the invoice’s allocation with no script: Allocation from a data table.

The workflow starts for every document of the type incoming-invoice. Its file workflow.yaml:

$schema: 'https://docs.acriva.de/schemas/workflow.schema.json'
format: 1
schemaVersion: 4
sdk: '1.0.0'
key: cost-center-mapping
name: Kostenstellen-Zuordnung aus einer Datentabelle
template: custom
trigger:
documentType: incoming-invoice
fields:
- key: costCenter
kind: text
labelDe: Kostenstelle
labelEn: Cost center
documentTypes:
- incoming-invoice
- key: glAccount
kind: text
labelDe: Sachkonto
labelEn: G/L account
documentTypes:
- incoming-invoice
start: allocate
steps:
- kind: script
name: allocate
label: Kostenstelle zuordnen
script: allocate.ts
hook: onDocumentArrived
inputs:
- kind: field
key: vendorName
type: text
- kind: table
key: kostenstellenZuordnung
returns: []
exits: []
next: approve
- kind: humanTask
name: approve
label: Freigabe
taskKind: approve
assignees:
- when: null
assignee:
kind: costCenterResponsible
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 · Inputs: vendorName, data table kostenstellenZuordnung · Return values: none

import { defineScript } from '@acriva/workflow-sdk';
export default defineScript({
hook: 'onDocumentArrived',
async run(ctx) {
const vendor = ctx.document.fields.vendorName;
if (vendor == null) return;
// Every row of the vendor: a vendor may be mapped to more than one cost center.
const [mapping, ...others] = await ctx.tables.query('kostenstellenZuordnung', {
lieferant: vendor,
});
if (mapping === undefined) {
ctx.hint(
`Für „${vendor}“ gibt es keine Kostenstellen-Zuordnung. Bitte kontieren Sie die Rechnung von Hand.`,
);
return;
}
if (others.length > 0) {
const costCenters = [mapping, ...others].map((row) => row.kostenstelle).join(', ');
ctx.hint(
`„${vendor}“ ist mehreren Kostenstellen zugeordnet (${costCenters}). Bitte teilen Sie die Rechnung von Hand auf.`,
);
return;
}
if (mapping.kostenstelle) ctx.setField('costCenter', mapping.kostenstelle);
if (mapping.sachkonto) ctx.setField('glAccount', mapping.sachkonto);
},
});

A test runs each of these cases in the sandbox and compares the result.

  • Input: vendorName Neue Firma AG
  • Data table kostenstellenZuordnung: 3 rows
    • nummer Z-001, lieferant Telekom Deutschland GmbH, kostenstelle 4400, sachkonto 4920
    • nummer Z-002, lieferant Deutsche Bahn AG, kostenstelle 4300, sachkonto 4663
    • nummer Z-003, lieferant Deutsche Bahn AG, kostenstelle 4100, sachkonto 4663
  • Exit: the default next step
  • Changes: hint on the next task: “Für „Neue Firma AG“ gibt es keine Kostenstellen-Zuordnung. Bitte kontieren Sie die Rechnung von Hand.”
  • Input: vendorName Telekom Deutschland GmbH
  • Data table kostenstellenZuordnung: 3 rows
    • nummer Z-001, lieferant Telekom Deutschland GmbH, kostenstelle 4400, sachkonto 4920
    • nummer Z-002, lieferant Deutsche Bahn AG, kostenstelle 4300, sachkonto 4663
    • nummer Z-003, lieferant Deutsche Bahn AG, kostenstelle 4100, sachkonto 4663
  • Exit: the default next step
  • Changes: field costCenter = 4400; field glAccount = 4920
  • Input: vendorName Deutsche Bahn AG
  • Data table kostenstellenZuordnung: 3 rows
    • nummer Z-001, lieferant Telekom Deutschland GmbH, kostenstelle 4400, sachkonto 4920
    • nummer Z-002, lieferant Deutsche Bahn AG, kostenstelle 4300, sachkonto 4663
    • nummer Z-003, lieferant Deutsche Bahn AG, kostenstelle 4100, sachkonto 4663
  • Exit: the default next step
  • Changes: hint on the next task: “„Deutsche Bahn AG“ ist mehreren Kostenstellen zugeordnet (4300, 4100). Bitte teilen Sie die Rechnung von Hand auf.”