# CreateInvoiceToleranceConfig

## Permission Scope

accountPayableDocument

## Overview

createInvoiceToleranceConfig creates a supplier-specific invoice tolerance configuration for AP registration. The row is scoped by company and supplier. During AP document registration, this configuration relaxes unit price and quantity variance checks for the matching supplier before SYSTEM holds are planned.

## Business Rules

- The referenced company must exist
- The referenced supplier supplier account must exist
- Only one InvoiceToleranceConfig can exist for each company and supplier pair
- Tolerance values must be non-negative when provided
- Null or omitted tolerance fields mean strict matching for that tolerance field

## Process Flow

```mermaid
flowchart TD
    A[Receive create invoice tolerance config request] --> B{Company exists?}
    B -->|No| C[Return error: COMPANY_NOT_FOUND]
    B -->|Yes| D{Supplier exists?}
    D -->|No| E[Return error: AP_SUPPLIER_ACCOUNT_NOT_FOUND]
    D -->|Yes| F{Tolerance values valid?}
    F -->|No| G[Return error: INVALID_TOLERANCE_VALUE]
    F -->|Yes| H{Config already exists?}
    H -->|Yes| I[Return error: INVOICE_TOLERANCE_CONFIG_ALREADY_EXISTS]
    H -->|No| J[Create invoice tolerance config]
    J --> K[Return created invoice tolerance config]
```

## External Dependencies

- None

## Error Scenarios

- **AP_INVALID_SUPPLIER_ACCOUNT**: Business partner is unavailable for transactions or belongs to another company

- **COMPANY_NOT_FOUND**: Referenced company does not exist
- **AP_SUPPLIER_ACCOUNT_NOT_FOUND**: Referenced supplier account does not exist
- **INVALID_TOLERANCE_VALUE**: Invoice tolerance value must not be negative
- **INVOICE_TOLERANCE_CONFIG_ALREADY_EXISTS**: Invoice tolerance config already exists for the company and supplier

## Test Cases

- creates supplier invoice tolerance config
- passes custom config fields through to the insert
- returns error when company does not exist
- returns error when supplier does not exist
- returns error when tolerance value is negative
- returns error when config already exists for company and supplier
