// case study
Comarch Invoice Automation
Batch-drafting sales invoices in Comarch Betterfly from messy Excel files, without a single irreversible step.
// 01 context
Seventeen clicks, hundreds of times a month.
An international transport company invoices every shipment it carries. The shipments arrive as Excel files: one row per shipment, one invoice per row. Entered by hand in the ERP, each invoice took seventeen clicks, and a month holds hundreds of rows.
The files are not uniform. Weekly lists and customer sheets, headers in several languages or none at all, notes typed into address cells: whatever reads them has to cope with what people actually send.
// 02 constraints
What the API would and would not do.
- Comarch Betterfly (formerly Comarch ERP XT) offers a public REST API with OAuth 2.0; an access token lives for ten minutes.
- An invoice line cannot be free text: every line must reference a product that already exists in the account.
- Foreign-currency invoices cannot be created or changed through the API; documents always come out in the local currency.
- The source files are messy: several layouts, headers in several languages or missing, and data that is sometimes a note to a colleague.
// 03 decisions
Seven decisions that shaped the tool.
-
Nothing it does is irreversible.
It only ever creates drafts. The endpoint that confirms an invoice is deliberately absent from the codebase, so a mistake costs a deleted draft, never a corrected invoice, and issuing stays a human decision in the ERP.
-
A dry run unless told otherwise.
Without
--livenot a single request is sent: every payload is printed and logged for review first. Sending is something you ask for, not something you forget to prevent. -
Columns are recognised by what they contain.
Headers arrive in Turkish, English, German, Polish or French, or not at all. The tool profiles each column’s content (dates parse as dates, plates look like plates), proposes a mapping, asks once and remembers the answer for that layout.
-
One bad row stops the whole file.
Validation runs before anything is sent and lists every problem at once. Nothing is assumed: an amount typed as
1.050is refused as ambiguous rather than read as 1.05, because a wrong guess becomes a wrong invoice. -
Running it twice is always safe.
Every draft is recorded under a hash of its content. An interrupted run is simply started again: what was already drafted is skipped, and a trip invoiced in an earlier run never gets a second draft.
-
The API client expects failure.
Tokens are refreshed before their ten minutes run out, server errors are retried with exponential back-off, a 429 waits as long as
Retry-Aftersays and a 401 gets exactly one token refresh. In live mode the run stops at the first error and shows the exact payload. -
Limits are found, written down and tested.
Foreign currency looked supported but is not: currency fields are silently ignored on create and rejected on update. That behaviour was verified against the live API and documented, and the currency code is still sent, so the flow works the day the API supports it.
// 04 architecture
Four stages, wired by one entry point.
Each stage can stop the run on its own; only the last one ever talks to the ERP. A separate, read-only checker reads every draft back afterwards and compares it field by field with the reviewed file.
// 05 output
What a dry run looks like.
$ python main.py inbox/week-41/*.xlsx [merge] 3 files: 2 weekly lists, 1 customer sheet [detect] week-41-b.xlsx: layout not recognised, profiling columns date <- column B 97% parse as dates route <- column D header "Trasa" (pl) vehicle <- column E values look like plates amount <- column G numeric, two decimals Confirm mapping? [y]es / [e]dit / [q]uit: y (remembered) [clean] 4 changes: 2 notes moved out of address cells, 2 country codes added [check] 38 rows, 0 duplicates, 0 conflicts [numbers] next free numbers read from the API (read-only) [convert] 38 of 38 rows valid [resume] 0 already drafted, 38 to send DRY RUN: nothing was sent. Add --live to create the drafts. FV 101/10/2026 2026-10-06 Customer A Warsaw - Lyon 1 840.00 FV 102/10/2026 2026-10-07 Customer A Poznan - Milan 2 115.00 FV 103/10/2026 2026-10-08 Customer B Lodz - Rotterdam 1 390.00 FV 104/10/2026 2026-10-08 Customer A Warsaw - Vienna 980.00 FV 105/10/2026 2026-10-09 Customer C Gdansk - Hamburg 1 260.00 FV 106/10/2026 2026-10-10 Customer B Krakow - Brno 760.00 … 32 more 38 payloads written to logs/dry-run-week-41.log
A sample run. Every file name, customer, route and amount here is invented for this page.
// 06 result
What changed.
Hundreds of drafts per real monthly run, with no duplicates and no silent failures. Seventeen clicks per invoice became a review of one merged file and a final pass in the ERP.
Private repository — code available on request.