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; } }