# Dealer Claim Financial Settlement Workflow

This document outlines the workflow for financial settlement of dealer claims within the Royal Enfield platform, following the transition from direct DMS integration to an Azure File Storage (AFS) based data exchange with SAP.

## Workflow Overview

The financial settlement process ensures that dealer claims are legally documented and financially settled through Royal Enfield's SAP system.

### 1. Legal Compliance: PWC E-Invoicing
Once the **Dealer Completion Documents** are submitted and approved by the **Initiator (Requestor Evaluation)**, the system triggers the legal compliance step.

- **Service**: `PWCIntegrationService`
- **Action**: Generates a signed E-Invoice via PWC API.
- **Output**: IRN (Invoice Reference Number), Ack No, Ack Date, Signed Invoice (PDF/B64), and QR Code.
- **Purpose**: Ensures the claim is legally recognized under GST regulations.

### 2. Financial Posting: AFS/CSV Integration
The financial settlement is handled by exchanging data files with SAP via **Azure File Storage (AFS)**.

- **Action**: The system generates a **CSV file** containing the following details:
    - Invoice Number (from PWC)
    - Invoice Amount (with/without GST as per activity type)
    - GL Code (Resolved based on Activity Type/IO)
    - Internal Order (IO) Number
    - Dealer Code
- **Storage**: CSV is uploaded to a designated folder in AFS.
- **SAP Role**: SAP periodically polls AFS, picks up the CSV, and posts the transaction internally.

### 3. Payment Outcome: Credit Note
The result of the financial posting in SAP is a **Credit Note**.

- **Workflow**:
    - SAP generates a Credit Note and uploads it back to AFS.
    - RE Backend monitors the AFS folder.
    - Once a Credit Note is detected, the system retrieves it and attaches it to the workflow request.
    - An email notification (using `creditNoteSent.template.ts`) is sent to the dealer.

## Sequence Diagram

```mermaid
sequenceDiagram
    participant Dealer
    participant Backend
    participant PWC
    participant AFS as Azure File Storage
    participant SAP

    Dealer->>Backend: Submit Completion Docs (Actuals)
    Backend->>Backend: Initiator Approval
    Backend->>PWC: Generate Signed E-Invoice
    PWC-->>Backend: Return IRN & QR Code
    Backend->>Backend: Generate Settlement CSV
    Backend->>AFS: Upload CSV
    SAP->>AFS: Pick up CSV
    SAP->>SAP: Post Financials
    SAP->>AFS: Upload Credit Note
    Backend->>AFS: Poll/Retrieve Credit Note
    Backend->>Dealer: Send Credit Note Notification
```

## GL Code Resolution
The GL Code is solved dynamically based on:
1.  **Activity Type**: Each activity (e.g., Marketing Event, Demo) has a primary GL mapping.
2.  **Internal Order (IO)**: If specific IO logic is required, the GL can be overridden.

## Summary of Integration Points
| Component | Integration Type | Responsibility |
| :--- | :--- | :--- |
| **PWC** | REST API | Legal E-Invoice |
| **AFS (Azure)** | File Storage SDK | CSV Exchange |
| **SAP** | Batch Processing | Financial Posting & Credit Note |
