# SetSupplierDefaultPurchasePaymentTerm

## Permission Scope

purchasePricing

## Overview

SetSupplierDefaultPurchasePaymentTerm creates or updates the purchase-owned supplier profile for a company and assigns the default payment term used when new draft purchase orders are created for that supplier.

## Business Rules

- Supplier must exist and carry the `SUPPLIER` role
- Supplier profile is unique per `companyId + supplierId`
- Assigned payment term must exist, belong to the same company, and be active for new draft defaulting
- Passing the same default term as the current profile is idempotent
- Clearing the default term is allowed and preserves the supplier profile record for future procurement settings

## Process Flow

```mermaid
flowchart TD
    A[Receive supplier + company + paymentTermId] --> B{Supplier exists and is a SUPPLIER?}
    B -->|No| C[Return supplier error]
    B -->|Yes| D{Payment term provided?}
    D -->|Yes| E{Payment term exists, same company, active?}
    D -->|No| G[Upsert supplier profile with cleared default]
    E -->|No| F[Return payment term error]
    E -->|Yes| G[Upsert supplier profile with default term]
    G --> H[Return supplier profile]
```

## External Dependencies

- [business-partner::GetPartner](../../../business-partner/docs/queries/GetPartner.md) - Validate the supplier reference and partner role

## Error Scenarios

- **SUPPLIER_NOT_FOUND**: Referenced supplier partner does not exist
- **PARTNER_NOT_SUPPLIER**: Referenced partner does not carry the SUPPLIER role
- **PAYMENT_TERM_NOT_FOUND**: Referenced purchase payment term does not exist
- **PAYMENT_TERM_INACTIVE**: Referenced purchase payment term is inactive
- **COMPANY_SCOPE_MISMATCH**: Supplier profile company does not match the payment term company

## Test Cases

- creates a supplier profile when none exists
- updates the default payment term on an existing supplier profile
- clears the default payment term when explicit null is provided
- returns error when supplier does not exist
- returns error when partner is not a supplier
- returns error when payment term does not exist
- returns error when payment term is inactive
- returns error when payment term belongs to a different company
- returns existing profile unchanged when payment term is already set to the same value
