ifnex/04_Laravel/app/Services/TrackingService.php
Kazem Alghasi d6e04d53fa feat(core): integrate Kavenegar SMS notifications and audit logging
Implement a comprehensive notification system using Kavenegar SMS
gateway and enhance system traceability through audit logging and
detailed shipment status history.

- SMS Integration:
  - Add Kavenegar SMS service with configurable API keys and sender
    numbers via system settings.
  - Implement automated SMS notifications for shipment approval,
    rejection, successful payments, and tracking updates.
  - Add administrative UI in Filament to manage SMS gateway settings
    and toggle specific notification types.
- Audit & Tracking:
  - Apply `Auditable` trait to core models (User, Shipment, Wallet,
    etc.) to track changes.
  - Refactor `ShipmentStatusHistory` to include status transitions
    (`from_status` to `to_status`) and specific reasons for changes.
  - Implement `ShipmentObserver` to automate notification triggers
    on status changes.
- Database & Config:
  - Add migrations for enhanced shipment status history tracking.
  - Update `.env.example` and `config/ifnex.php` with Kavenegar
    configuration parameters.
2026-09-10 00:52:18 +03:30

50 lines
1.5 KiB
PHP

<?php
namespace App\Services;
use App\Models\Shipment;
use App\Models\ShipmentTrackingEvent;
class TrackingService
{
public function getTrackingTimeline(string $awb_no)
{
$shipment = Shipment::where('awb_no', $awb_no)->firstOrFail();
return $shipment->trackingEvents()
->orderByDesc('event_date')
->orderByDesc('event_time')
->get();
}
public function addTrackingEvent(int $shipment_id, array $data): ShipmentTrackingEvent
{
$event = ShipmentTrackingEvent::create(array_merge($data, [
'shipment_id' => $shipment_id,
'source' => 'manual',
]));
if (SystemSetting::get('kavenegar_api_key') && (bool) SystemSetting::get('kavenegar_send_tracking_update', false)) {
$shipment = Shipment::with('user')->find($shipment_id);
if ($shipment && $shipment->user && $shipment->user->phone) {
$shipment->user->notify(new \App\Notifications\TrackingUpdatedSms(
$shipment->awb_no,
$event->event_description ?? 'به‌روزرسانی',
$event->location ?? ''
));
}
}
return $event;
}
public function getShipmentWithTracking(string $awb_no)
{
$shipment = Shipment::where('awb_no', $awb_no)
->with(['carrierMappings', 'trackingEvents'])
->firstOrFail();
return $shipment;
}
}