> 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/due-date-calculation.md).

# Due Date Calculation

## What does this script do?

Calculates the payment due date based on the invoice date by adding a configurable number of days (e.g., 30). Weekends are automatically skipped so the due date always falls on a business day.

## Trigger

`AFTER_FORMATTING` on document type **INVOICE**

## Full Script

```python
# Read invoice date
inv_date = get_field_value(document_data, "invoice_date")

if inv_date:
    # Calculate due date: 30 days after invoice date, skip weekends
    set_date_value(document_data, "due_date", inv_date,
                   add_days=30, skip_weekend=True)

    # Also set accounting date = invoice date
    set_date_value(document_data, "accounting_date", inv_date)
```

## Variations

### 14 days, excluding Mondays

```python
set_date_value(document_data, "due_date", inv_date,
               add_days=14, skip_weekend=True, exclude_final_days="MONDAY")
```

### 60 days, no weekend skip

```python
set_date_value(document_data, "due_date", inv_date, add_days=60)
```

### Set delivery date to today

```python
set_date_value(document_data, "delivery_date", None)  # None = today
```

## Step-by-Step Explanation

1. **Read invoice date** from the document
2. **Calculate due date** using `set_date_value()` with `add_days=30` and `skip_weekend=True`
3. **Date formatting** is automatic — uses the document's `date_format_pattern` (e.g., `%d.%m.%Y`)
4. **Weekend skip** ensures the due date falls on Mon-Fri

## Day Codes for `exclude_final_days`

`MONDAY`, `TUESDAY`, `WEDNESDAY`, `THURSDAY`, `FRIDAY`, `SATURDAY`, `SUNDAY`

## Functions Used

* [get\_field\_value()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/field-functions.md#get_field_value) — Read invoice date
* [set\_date\_value()](/administration-and-setup/settings/global-settings/document-types/script/scripting-in-docbits/field-functions.md#set_date_value) — Calculate and set due date


---

# 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/due-date-calculation.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.
