Edit File: Order.php
<?php namespace App\Models; use Illuminate\Support\Facades\File; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Factories\HasFactory; use App\Traits\Uploadable; use App\Enums\OrderStatus; class Order extends Model { use HasFactory,Uploadable; protected $fillable = ['store_id','payment_confirmed' , 'country_key','deposit_is_returned','receiver_phone','deposit','remain','deliver_date','category_id','invoice_accepted','is_settlement','car_type_id','provider_type', 'store_name', 'store_icon', 'place_id', 'place_name', 'place_icon', 'deliver_time', 'receive_lat', 'receive_long', 'receive_address', 'deliver_lat', 'deliver_long', 'deliver_address', 'coupon', 'type', 'payment_type', 'description', 'delivery_price', 'price', 'app_percentage', 'added_value', 'discount', 'total_price', 'user_id', 'min_expected_price', 'max_expected_price', 'delegate_id', 'payment_status', 'delivery_status', 'status', 'price', 'total_price', 'invoice_image', 'have_invoice', 'close_reason', 'store_status', 'needs_delivery', 'delegate_acceptance','ref_code','received_by_me','need_shipping' ,'receiver_name','cargo','additional_phone','amount','is_scheduled','starting_city_id','destination_city_id','shipping_price','code_submitted','deposit_paid']; public function deliveryOffers() { return $this->hasMany(DeliveryOffer::class); } public function statusForUser() { $status = $this->status; // store status is the order status $offers = $this->deliveryOffers()->Where( 'status', 'new' )->get(); $accepted_offer = $this->deliveryOffers()->Where( 'status', 'accepted' )->get(); if ($this->status == 'open' && $offers->count() <=0) { $status = 'open'; } if ($this->status == 'open' && $offers->count() >0) { $status = 'under_review'; } if ($this->status == 'open' && $accepted_offer->count() >0 && $this->payment_status=='false') { $status = 'waiting_payment'; } if ($this->status == 'open' && $accepted_offer->count() >0 && $this->payment_status=='true') { $status = 'payment_done'; } if ($this->status == 'inprogress' && $this->delivery_status == 'delivering') { $status = 'delivering'; } elseif ($this->status == 'inprogress' && $this->delivery_status == 'reached_receive_location') { $status = 'reached_receive_location'; } elseif ($this->status == 'inprogress' && $this->delivery_status == 'reached_deliver_location') { $status = 'reached_deliver_location'; } elseif ($this->delivery_status == 'delivered') { $status = 'delivered'; } // order main status being finished or closed if ($this->status == 'finished') { $status = 'finished'; } elseif ($this->status == 'closed') { $status = 'closed'; } return $status; } public function statusForDelegate() { $status = $this->status; // store status is the order status $offers = $this->deliveryOffers()->Where( 'status', 'new' ) ->where('user_id',auth('api')->user()->id)->get(); $accepted_offer = $this->deliveryOffers()->Where( 'status', 'accepted') ->where('user_id',auth('api')->user()->id)->get(); if ($this->status == 'open' && $offers->count() <=0) { $status = 'open'; } if ($this->status == 'open' && $offers->count() >0) { $status = 'under_review'; } if ($this->status == 'open' && $accepted_offer->count() >0 && $this->payment_status=='false') { $status = 'waiting_payment'; } if ($this->status == 'open' && $accepted_offer->count() >0 && $this->payment_status=='true') { $status = 'payment_done'; } if ($this->status == 'inprogress' && $this->delivery_status == 'delivering') { $status = 'delivering'; } elseif ($this->status == 'inprogress' && $this->delivery_status == 'reached_receive_location') { $status = 'reached_receive_location'; } elseif ($this->status == 'inprogress' && $this->delivery_status == 'reached_deliver_location' && $this->code_submitted == false) { $status = 'reached_deliver_location'; } elseif ( ( $this->status == 'inprogress' && $this->delivery_status == 'reached_deliver_location' && $this->code_submitted == true ) || $this->delivery_status == 'delivered') { $status = 'delivered'; } // order main status being finished or closed if ($this->status == 'finished') { $status = 'finished'; } elseif ($this->status == 'closed') { $status = 'closed'; } return $status; } // public function statusForDelegate() // { // $status = $this->status; // // store status is the order status // if ($this->status == 'open' && $this->deliveryOffers()->where('user_id',auth()->user()->id)->whereNotIn('status',['rejected'])->count() <=0 ) { // $status = 'waiting_offers'; // }elseif ($this->status == 'open' && $this->deliveryOffers()->Where( 'status', 'new' )->where('user_id',auth()->user()->id)->count() > 0 ) { // $status = 'waiting_acceptance'; // } // elseif ($this->status == 'open' && $this->deliveryOffers()->Where( 'status', 'accepted' )->where('user_id',auth()->user()->id)->count() > 0 ) { // $status = 'waiting_to_receive_package'; // } // elseif ($this->status == 'inprogress' && $this->delivery_status=='delivering') { // $status = 'intransit'; // } // elseif ($this->delivery_status == 'reached_deliver_location') { // $status = 'received_location'; // } // elseif ($this->delivery_status == 'delivered') { // $status = 'finished_from_delegate'; // } // // // order main status being finished or closed // if ($this->status == 'finished') { // $status = 'finished'; // } elseif ($this->status == 'closed') { // $status = 'closed'; // } // return $status; // } public function getStatusTextAttribute() { return trans('order.' . $this->status); } public function getInvoiceImagePathAttribute() { return $this->invoice_image ? asset('assets/uploads/invoices/' . $this->invoice_image) : ''; } public function image(){ return $this->hasMany(OrderImage::class); } // git public function images(){ return $this->hasMany(OrderImage::class); } public function getImagePathAttribute() { return $this->images->name ? asset('assets/uploads/order_images/' . $this->images->name) : ''; } public function startingCity(){ return $this->belongsTo(City::class,'starting_city_id'); } public function destinationCity(){ return $this->belongsTo(City::class,'destination_city_id'); } public function category(){ return $this->belongsTo(Category::class); } public static function boot() { parent::boot(); /* creating, created, updating, updated, deleting, deleted, forceDeleted, restored */ self::deleted(function ($model) { $model->deleteFile($model->attributes['invoice_image'], 'invoices'); }); } public function orderproducts() { return $this->hasMany(OrderProduct::class); } public function orderservices() { return $this->hasMany(Service::class, 'OrderServices', 'order_id'); } public function store() { return $this->belongsTo(Store::class); } public function carModel() { return $this->belongsTo(Cartype::class ,'car_type_id'); } public function user() { return $this->belongsTo(User::class); } public function delegate() { return $this->belongsTo(User::class, 'delegate_id'); } public function rooms() { return $this->hasMany(Room::class); } public function withdrawReasons() { return $this->hasMany(WithdrawReason::class); } public function scopeSearch($query, $searchArray = []) { $query->where(function ($query) use ($searchArray) { if ($searchArray) { foreach ($searchArray as $key => $value) { if (str_contains($key, '_id')) { if (null != $value) { $query->Where($key, $value); } } elseif ('order' == $key) { } elseif ('created_at_min' == $key) { if (null != $value) { $query->WhereDate('created_at', '>=', $value); } } elseif ('created_at_max' == $key) { if (null != $value) { $query->WhereDate('created_at', '<=', $value); } } elseif ('name' == $key) { if (null != $value) { $query->whereHas('user', function ($query) use ($value) { $query->where('name', 'like', '%' . $value . '%'); }); } } else { if (null != $value) { $query->Where($key, 'like', '%' . $value . '%'); } } } } } ); } }
Back to File Manager