Edit File: OrderResource.php
<?php namespace App\Http\Resources; use App\Models\Setting; use App\Models\Category; use App\Traits\GeneralTrait; use App\Models\Paymentmethod; use App\Models\Order; use App\Models\Review; use App\Models\OrderServices; use App\Http\Resources\StoreResource; use App\Http\Resources\StoresResource; use App\Http\Resources\OrderProductResource; use App\Http\Resources\OrderExtraServices; use App\Http\Resources\PaymentmethodResource; use App\Http\Resources\DeliveryOffersResource; use App\Http\Resources\ReviewsResource; use Illuminate\Http\Resources\Json\JsonResource; use DateTime; use Carbon\Carbon; class OrderResource extends JsonResource { use GeneralTrait; /** * Transform the resource into an array. * * @param \Illuminate\Http\Request $request * @return array */ public static function getdirectDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo) { $earthRadius = 6371000; // convert from degrees to radians $latFrom = deg2rad($latitudeFrom); $lonFrom = deg2rad($longitudeFrom); $latTo = deg2rad($latitudeTo); $lonTo = deg2rad($longitudeTo); $lonDelta = $lonTo - $lonFrom; $a = pow(cos($latTo) * sin($lonDelta), 2) + pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2); $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta); $angle = atan2(sqrt($a), $b); $in_km = ($angle * $earthRadius) / 1000; $in_km += (40 / 100) * $in_km; $in_km = number_format((float) $in_km, 2, '.', ''); return $in_km; } public function toArray( $request ) { $lang = $request->header( 'lang' ) ?? 'ar'; //timer to withdraw $timer = 0; $can_withdraw = false; $delegate_withdraw_time = Setting::where( 'key', 'delegate_withdraw_time' )->first()->value; $time_to_withdraw = date( 'Y-m-d H:i:s', strtotime( '+'.$delegate_withdraw_time.'minutes', strtotime( $this->delegate_acceptance ) ) ); if ( $this->delegate_acceptance && date( 'Y-m-d H:i:s' ) < $time_to_withdraw ) { $timer = \Carbon\Carbon::parse( $time_to_withdraw )->diffInSeconds( date( 'Y-m-d H:i:s' ) ) ; $can_withdraw = true; } else { $timer = 0; $can_withdraw = false; } //user $user = auth( 'api' )->user(); // paymrnt method $payment_method = Paymentmethod::where( 'key', $this->payment_type )->first(); // order products $orderservices =OrderServices::where('order_id',$this->id)->get(); // room $room = $this->rooms()->orderBy( 'created_at', 'desc' )->first(); // delivery_offers $accepted_offer = $this->deliveryOffers()->Where( 'status', 'accepted' )->get(); // delivery_offers $images = $this->images()->where('type','order')->get(); $delegate_order_images = $this->images()->where('type','delegate_image')->get(); // order current status $delivery_offers=[]; if ( $this->user_id == $user->id ) { $statusText = __( 'order.'. $this->statusForUser() ); $status=$this->statusForUser(); } else { $statusText = __( 'order.'. $this->statusForDelegate() ); $status=$this->statusForDelegate(); } //image and name $image = ''; $name = ''; $is_rated=false; $review=Review::where('order_id',$this->id)->where('reviewable_id',$user->id)->first(); if($review){ $is_rated=true; } $services=0.0; foreach($orderservices as $service){ if($service->price){ $services+=$service->price; } } // response data $invoice_details = []; $invoice_details['price'] = number_format($this->price,2, '.', '')??'0.0'; $invoice_details['deposit'] = number_format($this->deposit,2, '.', '')??'0.0'; $invoice_details['remain'] =number_format($this->remain,2, '.', '')??'0.0'; $invoice_details['delivery_price'] = number_format($this->delivery_price,2, '.', '')??'0.0'; $invoice_details['shipping_price'] =number_format($this->shipping_price,2, '.', '')??'0.0'; $invoice_details['added_value'] = number_format($this->added_value,2, '.', '')??'0.0'; $invoice_details['total_price'] = number_format($this->total_price,2, '.', '')??'0.0'; $invoice_accepted='pending'; if($this->invoice_accepted=='false'){ $invoice_accepted='pending'; } if($this->invoice_accepted=='true'){ $invoice_accepted='accepted'; } if($this->invoice_accepted=='refused'){ $invoice_accepted='refused'; } $deliver_time=new DateTime($this->deliver_time); $deliver_date=Carbon::parse($this->deliver_date); $expected_time=(static::getdirectDistance($this->deliver_lat,$this->deliver_long,$this->receive_lat,$this->receive_long))*1.2; $today = Carbon::today()->toDateString(); $scheduled_msg=trans('order.not_active_now'); if(Carbon::parse($this->deliver_date)->diffInDays($today) ==0){ $scheduled_msg=trans('order.active_now'); } $distance=0; $delivery_offers = $this->deliveryOffers()->Where('status','new')->get(); $expireTime = $this->created_at?->addMinutes(20); $remainingTimeMs = Carbon::now()->diffInMilliseconds($expireTime, false); // Get remaining time in milliseconds if ($remainingTimeMs > 0 && $this->payment_status == 'false') { $minutes = floor($remainingTimeMs / 60000); // Convert milliseconds to minutes $seconds = floor(($remainingTimeMs % 60000) / 1000); // Get remaining seconds // $milliseconds = floor(($remainingTimeMs % 1000)); // Get remaining milliseconds $milliseconds = $remainingTimeMs; $formattedTime = sprintf('%02d:%02d:%02d', $minutes, $seconds, $milliseconds); } else { $milliseconds = null ; } return [ 'id' => $this->id, 'ref_code'=>$this->ref_code??'', 'created_at' => $this->created_at->isoFormat( 'YYYY - MMMM - D'), 'status' => $status, 'status_text'=>$statusText, 'car_type_id'=>$this->car_type_id??0, 'car_type'=>$this->car_type_id?$this->carModel->name:'', 'category' => $this->category ? new CategoriesResource( $this->category ) : null, 'deliver_time' => $this->deliver_time?$deliver_time->format( 'g:i A'):'', 'payment_method_key' => $payment_method?$payment_method->key:'not_selected', 'payment_method' => $payment_method?$payment_method->name:trans('order.notselected'), 'payment_status' => $this->payment_status == 'true'?true:false, 'deliver_date' => $this->deliver_date?$deliver_date->isoFormat( 'YYYY - MMMM - D' ):'', 'deliver_lat' => $this->deliver_lat??'', 'deliver_long' => $this->deliver_long??'', 'deliver_address' => $this->deliver_address??'', 'receive_lat' => $this->receive_lat??'', 'receive_long' => $this->receive_long??'', 'receive_address' => $this->receive_address??'', 'receive_name' => $this->receiver_name??'', 'have_invoice' => $this->have_invoice == 'true'?true:false, 'price' => number_format( $this->price, 2 )??'0', 'app_percentage' => number_format( $this->app_percentage, 2 )??'0', 'added_value' => number_format( $this->added_value, 2 )??'0', 'total_price' => number_format( $this->total_price, 2 )??'0', 'invoice_image' => $this->invoiceImagePath??'', // 'invoice_accepted'=>$invoice_accepted, 'payment_confirmed'=>$this->payment_confirmed, 'description' => $this->description??'', 'receiver_phone'=>$this->receiver_phone??'', 'additional_phone'=>$this->additional_phone??'', // 'services' => OrderExtraServices::collection( $orderservices ), 'delegate_id' => $this->delegate?$this->delegate->id?$this->delegate->id:0:0, 'delegate_name' => $this->delegate?$this->delegate->name?$this->delegate->name:'':'', 'delegate_phone' => $this->delegate?$this->delegate->phone?$this->delegate->country_key.$this->delegate->phone:'':'', 'delegate_avatar' => $this->delegate?$this->delegate->avatarPath:'', 'delegate_rate' => $this->delegate?$this->delegate->rate:"", 'delegate_lat' => $this->delegate?( ( string )$this->delegate->lat??'' ):'', 'delegate_long' => $this->delegate?( ( string )$this->delegate->long??'' ):'', 'delegate_distance'=>$distance, 'user_id' => $this->user?$this->user->id?$this->user->id:'':'', 'user_name' => $this->user?$this->user->name?$this->user->name:'':'', 'user_rate' => $this->user?$this->user->rate:"", 'user_phone' => $this->user?$this->user->phone?$this->user->country_key.$this->user->phone:'':'', 'user_avatar' => $this->user?$this->user->avatarPath:'', 'room_id' => $room?$room->id:0, 'images' => OrderImagesResource::collection( $images ), 'delegate_order_images' => OrderImagesResource::collection( $delegate_order_images ), 'is_rated'=>$is_rated, 'code_submitted'=>$this->code_submitted, 'cancel_reason'=>$this->close_reason??'', 'review'=> $review? new ReviewsResource($review):(object)[], 'delivery_offers' => DeliveryOffersResource::collection($delivery_offers), 'time' => $milliseconds, 'starting_city' => [ 'id' => (int)$this->starting_city_id??0 , 'name' => $this->startingCity?->name ?? ''], 'destination_city' => [ 'id' => (int)$this->destination_city_id??0 , 'name' => $this->destinationCity?->name ?? ''], ]; } }
Back to File Manager