# SubmitPurchaseOrder

## Permission Scope

purchaseOrder

## Overview

SubmitPurchaseOrder validates that a draft purchase order is commercially complete and moves `orderStatus` to `SUBMITTED` for review or approval.

## Business Rules

- The selected account must match the document company and be available for transactions.

- Target purchase order must exist
- Only orders with `orderStatus` = `DRAFT` can be submitted
- Submitted orders must contain at least one line
- Supplier account must match the order company and remain available for transactions at submission time

## Process Flow

```mermaid
flowchart TD
    A[Receive submit request] --> B{Purchase order exists and is DRAFT?}
    B -->|No| C[Return not found or invalid status]
    B -->|Yes| D{At least one valid line?}
    D -->|No| E[Return line validation error]
    D -->|Yes| F{Supplier account available in this company?}
    F -->|No| G[Return supplier account validation error]
    F -->|Yes| H["Set orderStatus to SUBMITTED"]
    H --> I[Return submitted order]
```

## External Dependencies

- [business-partner::GetSupplierAccount](../../../business-partner/docs/query/GetSupplierAccount.md) - Re-validate supplier account company and transaction availability

## Error Scenarios

- **SUPPLIER_NOT_ACTIVE**: Referenced supplier is not in ACTIVE status

- **SUPPLIER_NOT_FOUND**: Referenced supplier partner does not exist

- **PURCHASE_ORDER_NOT_FOUND**: Referenced purchase order does not exist
- **PURCHASE_ORDER_NOT_DRAFT**: Purchase order is not in `DRAFT` status
- **EMPTY_PURCHASE_ORDER_LINES**: Purchase order has no lines
- **SUPPLIER_ACCOUNT_COMPANY_MISMATCH**: Supplier account belongs to another company

## Test Cases

- submits a valid draft purchase order
- returns error when purchase order does not exist
- returns error when purchase order is not in draft
- returns error when purchase order has no lines
- returns error when supplier account belongs to another company
- submits when line has requiresPhysicalReceipt false and no receiving site
