# Order Repricing Version 2 - Implementation Analysis

## 1. Existing Implementation (Version 1 / "Requote")

The current codebase contains a "Requote" feature which functions as the Version 1 implementation.

*   **Service:** `App\Domains\Supply\SubDomains\SupplyOrders\Services\SupplyOrderRequoteService`
    *   `markForRequote`: Sets order status to `REQUOTE` and items to `PENDING_REQUOTE`.
    *   `confirmItemPrice`: Sets item status to `PRICE_APPROVED`.
    *   `checkAndUpdateOrderStatus`: Checks if all items are repriced.
*   **Statuses:**
    *   `SupplyOrderStatus::REQUOTE` exists.
    *   `SupplyOrderItemStatus::PENDING_REQUOTE` exists.
*   **Models:**
    *   `SupplyOrder` has `supervisor_id` and `agent_id`.
    *   `SupplyOrderItem` tracks `selected_alternative_id` and `estimated_unit_price`.
    *   `SupplierQuote` stores individual quotes with `valid_until` date.

---

## 2. Gap Analysis for Version 2

### Business Rules & Operational Flow

| Feature | Existing Status | Action Required |
| :--- | :--- | :--- |
| **Repricing Trigger** | Exists as `markForRequote` on SupplyOrder. | **Update**: Needs to be triggered from **Quotation** context (before Customer PO). <br> **Change**: Rename/Alias status `REQUOTE` to `AWAITING_REPRICING` better aligns with spec. |
| **Status Changes** | `REQUOTE` used on Order. | **Update**: Ensure `awaiting_repricing` is used (Update Enum or map `REQUOTE` to it). |
| **Cycle Assignment** | `SupplyOrder` has `supervisor_id`. | **Keep**: Assign to `supervisor_id`. <br> **Verify**: Ensure generic agent reassignment logic (per item) is exposed effectively. |
| **Supplier Quote Validity** | `markForRequote` resets status blindly. | **New Build**: Logic to check `SupplierQuote::valid_until` vs `today`. <br> Only trigger "expired" status if date passed. |
| **Historical Records** | Quotes soft deleted or just not selected? | **New Build**: Add `SUPERSEDED` to `SupplierApprovalStatus`. <br> Ensure old approved quotes are marked `SUPERSEDED` instead of just unselected/deleted. |

### Delivery Note Changes

| Feature | Existing Status | Action Required |
| :--- | :--- | :--- |
| **Remove `delivery_method` from GRN** | Needs verification in DB/Migration. | **Refactor**: Remove column/field from `GoodsReceipt` if present (or verify it's already gone). |
| **Add `delivery_method` to Delivery** | Missing in `Delivery` model. | **New Build**: Add `delivery_method` to `Sup_Deliveries` table and `Delivery` model. |
| **Delivery Note Model** | `DeliveryNote` exists with `delivery_method`. | **Keep**: Model is ready. |
| **Auto-Generate Note** | No auto-gen logic found on Delivery completion. | **New Build**: Listener/Service to create `DeliveryNote` when `Delivery` marks items as delivered to customer. |
| **Attachment Endpoint** | `DeliveryNote` supports media. | **New Build**: dedicated endpoint to upload signed file -> Update status to `DELIVERED`. |

---

## 3. Implementation Plan

### Phase 1: Repricing Engine (Version 2)

1.  **Status Updates**:
    *   Update `SupplyOrderStatus` to include `AWAITING_REPRICING` (or rename `REQUOTE`).
    *   Update `SupplierApprovalStatus` to include `SUPERSEDED`.
2.  **Service Layer (`RepricingService`)**:
    *   Create/Update logic to check validity (`valid_until` < `now()`).
    *   If **Valid**: usage of existing quote continues.
    *   If **Expired**: Status -> `expired`, allow new quote entry.
    *   **Versioning**: Ensure when a new quote is approved, the old one is marked `SUPERSEDED`.
3.  **Controller**:
    *   Add `POST /quotations/{id}/trigger-repricing`.

### Phase 2: Delivery Note System

1.  **Database**:
    *   Migration: Add `delivery_method` to `sup_deliveries`.
    *   Migration: Remove `delivery_method` from `sup_goods_receipts` (if exists).
2.  **Models**:
    *   Update `Delivery` model fillables.
3.  **Logic**:
    *   **Auto-Generation**: Hook into `Delivery::markAsDelivered` (or equivalent) to generate `DeliveryNote`.
    *   **Completion**: Create endpoint `POST /delivery-notes/{id}/attach-signed` which uploads file and sets status `DELIVERED` (currently `APPROVED` in Enum, might need update).

---

## 4. What Needs to be Built from Scratch

1.  **Validity Check Logic**: The specific logic to compare `valid_until` and auto-expire items is NOT implemented.
2.  **Superseded Logic**: The explicit marking of old quotes as `SUPERSEDED` is new.
3.  **Delivery Note Auto-Generation**: The link between "Customer Delivery" completion and "Delivery Note" creation needs to be built.
4.  **Delivery Method Migration**: Moving the field from GRN to Customer Delivery step.
