# Orders, Delivery, Payments, and Ratings Models

## Localization storage

Any bilingual field referenced below is represented by one JSON model attribute and `Spatie\Translatable\HasTranslations`; it is never persisted as separate Arabic and English columns. Filament uses `->translatable()` for those attributes.

## `Order` → `orders`

Fields by group:

- Identity: `id`, unique `public_id`, nullable indexed `legacy_system_id`, `app_version`.
- Ownership: `customer_id`, nullable `provider_id`, `address_id`.
- State: `status`, `status_changed_at`, `payment_status`, `delivery_type`.
- Totals: decimal `subtotal`, `coupon_discount`, `loyalty_discount`, `delivery_fee`, `gift_wrapping_fee`, `total`.
- Promotion/payment: nullable coupon/loyalty-coupon/payment-method IDs, `payment_type`, transfer reference/company/private attachment.
- Currency: currency/delivery-currency/exchange-rate IDs, decimal rate, JSON currency snapshot.
- Delivery: nullable delivery-time ID/date and JSON snapshot.
- Address snapshot: nullable country/city/zone/type IDs, description and coordinates.
- Audit: customer/change/admin notes, JSON note history/request snapshot, request metadata, timestamps.

Relationships: belongs to customer/provider/address/coupon/loyalty coupon/payment method/currencies/rate/delivery time; has many items, deliveries, payments, status/audit events, ratings, redemptions and ledger entries.

Renames: `SysUniqueId → legacy_system_id`, `user_id → customer_id`, `payment_id → payment_method_id`, `delivery_time → delivery_time_id`, `area_id → zone_id`, `address_type → address_type_id`, `delivery_fees → delivery_fee`, `gift_wrapping_cost → gift_wrapping_fee`, `day_exchange_id/day_exchange → exchange_rate_id/exchange_rate`, `OrderCurrencyJsonInfo → currency_snapshot`, `OrderDeliveryTimeInfo → delivery_time_snapshot`, `PostOrderJsonInfo → request_snapshot`, `notes → customer_notes`, `change_note → change_reason`, `edit_history_admin_notes → admin_notes_history`, misspelled transfer fields → `transfer_*`, `status_update_date → status_changed_at`, `date → created_at`, `ipaddress → ip_address`.

Remove legacy single-product order columns because all current orders have item rows.

## `OrderItem` → `order_items`

Fields: `id`, `order_id`, nullable provider/product/variant/unit IDs, JSON product/request snapshots, decimal quantity/unit price/line total/exchange rate, timestamps.

Relationships: belongs to order/provider/product/variant.

Renames: `order_product_info → product_snapshot`, `post_product_info → request_snapshot`, `price → unit_price`, `order_by_price → line_total` after validation. Keep `OrderSysUniqueId` only during reconciliation.

## `OrderDelivery` → `order_deliveries`

Fields: `id`, `order_id`, `driver_id`, delivery `status`, accepted/started/delivered datetimes, nullable assigning admin, `is_active`, timestamps.

Relationships: order, driver User, assigning Admin. One active assignment per order.

Renames: `driver_accept/accept_date → status + accepted_at`, `add_user_id → assigned_by_admin_id`.

## `OrderStatusEvent` → `order_status_events`

Fields: new `id`, `order_id`, status, actor type/id, notes, occurred/created datetimes. Immutable. Belongs to order and controlled actor.

Renames: `account_id/type → actor_id/type`, `date → occurred_at`, `sys_db_date → created_at`, `sys_notes → notes`.

## `OrderAuditEvent` → `order_audit_events`

Fields: `id`, order/customer/status, actor fields, action/notes, JSON before/after, request metadata, `created_at`. Belongs to order/customer/actor.

Renames: `platform_order_items_admin_events_log → order_audit_events`, `order_user_id → customer_id`, `sys_note → notes`, `by_* → actor_*`, `user_action → action`, `data_before_change/after → before/after`, `sys_date → created_at`.

## `PaymentMethod` → `payment_methods`

Fields: `id`, sort order, bilingual name/description/company, type, icon, encrypted account number, support number, encrypted JSON provider config, status/audit/timestamps.

Relationships: orders, payments, availability records.

Renames: `order_view → sort_order`, `title_* → name_*`, `icon → icon_path`, `number → support_number`, `ElectronicPaymentAccountApiInfo → provider_configuration`.

## `PaymentMethodAvailability` → `payment_method_availabilities` (empty)

Fields: `id`, `payment_method_id`, `location_type`, `location_id`, status/audit/timestamps. Controlled morph to country/city/zone.

Consolidates the five `platform_payment_ways_*` location tables; `payment_id → payment_method_id`.

## `OrderPayment` → `order_payments` (empty)

Fields: `id`, order/payment-method IDs, unique external reference, status, encrypted JSON request/response, amount, attempted time, timestamps.

Renames: `AgentApiStatus → status`, `AgentApiRequestInfo → request_payload`, `DataSaveRequestApiResponse → response_payload`, `date → attempted_at`.

## Rating models

- `RatingQuestion`: sort order, `question_ar/en`, image, status/audit. `title_* → question_*`.
- `OrderRating`: customer/order/driver IDs, comment, moderation, request metadata, timestamps. `user_id → customer_id`; normally unique customer/order.
- `RatingAnswer`: order-rating/question IDs, question snapshot, unsigned rating, moderation/timestamps. `order_rate_id → order_rating_id`, `question_id → rating_question_id`, `title → question_snapshot`, `rate → rating`.
- `OrderEvaluation` remains optional/empty; merge with OrderRating only after semantic confirmation.

Replace duplicated temporary order/item/payment tables with CheckoutSession/PaymentAttempt only if resumable checkout is required.
