> For the complete documentation index, see [llms.txt](https://docs.docbits.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.docbits.com/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/sample-scripts/tax-code-detection.md).

# Tax Code Detection

## What does this script do?

Automatically determines the correct tax code based on the document's fulltext content and tax/net amounts. Detects reverse charge scenarios, tax-free invoices, and calculates the tax rate to assign the appropriate code (e.g., S1 for 19%, S2 for 7%).

## Trigger

`AFTER_FORMATTING` on document type **INVOICE**

## Full Script

```python
# Get document fulltext and amounts
content = get_document_content(document_data)
tax_amount = get_field_value(document_data, "tax_amount", "0")
net_amount = get_field_value(document_data, "net_amount", "0")

try:
    tax = float(tax_amount) if tax_amount else 0
    net = float(net_amount) if net_amount else 0
except ValueError:
    tax = 0
    net = 0

# Rule 1: Reverse charge detection via fulltext
if "REVERSE CHARGE" in content.upper() or "UMKEHR DER STEUERSCHULD" in content.upper():
    set_field_value(document_data, "tax_code", "RC")

# Rule 2: Zero tax = tax-free
elif tax == 0:
    set_field_value(document_data, "tax_code", "Z0")

# Rule 3: Calculate tax rate from amounts
elif net > 0:
    tax_rate = round((tax / net) * 100, 0)
    if tax_rate == 19:
        set_field_value(document_data, "tax_code", "S1")    # Standard rate
    elif tax_rate == 7:
        set_field_value(document_data, "tax_code", "S2")    # Reduced rate
    else:
        set_field_value(document_data, "tax_code", "S3")    # Other rate
```

## Step-by-Step Explanation

1. **Read fulltext** with `get_document_content()` for keyword detection
2. **Read tax and net amounts** for tax rate calculation
3. **Check for reverse charge** keywords in the document text (German and English)
4. **Check for zero tax** — if tax amount is 0, assign tax-free code
5. **Calculate tax rate** from tax/net ratio and assign the matching code

## Functions Used

* [get\_document\_content()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/business-logic-functions.md#get_document_content) — Read OCR fulltext
* [get\_field\_value()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/field-functions.md#get_field_value) — Read field values
* [set\_field\_value()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/field-functions.md#set_field_value) — Set tax code


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.docbits.com/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/sample-scripts/tax-code-detection.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
