File manager - Edit - /home/autoph/public_html/projects/aha-api/app/Models/ServiceBooking.php
Back
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use OwenIt\Auditing\Contracts\Auditable; use Illuminate\Database\Eloquent\SoftDeletes; use OwenIt\Auditing\Auditable as AuditableTrait; use Illuminate\Database\Eloquent\Factories\HasFactory; class ServiceBooking extends Model implements Auditable { use HasFactory, SoftDeletes, AuditableTrait; /** * The attributes that are NOT mass assignable. * */ protected $guarded = [ 'deleted_at', ]; /** * The attributes that should be cast. * * @var array<string, string> */ protected $casts = [ 'created_at' => 'datetime', 'updated_at' => 'datetime', 'deleted_at' => 'datetime', 'validated_at' => 'datetime', 'checked_in_at' => 'datetime', 'checked_out_at' => 'datetime', 'cancelled_at' => 'datetime', 'validated_by' => 'string', 'cancelled_by' => 'string', // 'smart_alert' => 'boolean' 'isApp' => 'boolean', ]; protected $appends = [ 'customer_name', 'customer_contact', 'dealer_name', 'booking_time', 'type_name', 'status_name', 'vehicle_name', 'advisor_name', 'validated_by_name', 'cancelled_by_name', 'promo_amount', 'arrived_date', 'status_update', ]; private $foreignKey = 'customer_id'; private $ownerKey = 'id'; public function vehicle() { return $this->belongsTo(Vehicle::class); } public function dealer() { return $this->belongsTo(Dealership::class); } public function serviceType() { return $this->belongsTo(ServiceType::class); } public function serviceStatus() { return $this->belongsTo(ServiceStatus::class, 'service_status_id', 'code'); } public function serviceAdvisor() { return $this->belongsTo(User::class, 'assigned_service_advisor'); } public function preferredDrink() { return $this->hasOneThrough(DrinkAndBeverage::class, PreferredDrink::class, 'vehicle_plate', 'id', 'car_plate', 'drink_id'); } public function logs() { return $this->hasMany(ServiceBookingLog::class); } public function getCustomerNameAttribute() { $customer = Customer::withTrashed()->find($this->customer_id); if(!$customer) // if customer not found, check using aha_user_id $customer = Customer::withTrashed()->where('aha_user_id', $this->aha_user_id)->first(); return $customer ? ucwords(strtolower("{$customer->firstname} {$customer->lastname}")) : null; } public function getCustomerContactAttribute() { $customer = Customer::withTrashed()->select('email','mobile')->find($this->customer_id); if(!$customer) // if customer not found, check using aha_user_id $customer = Customer::withTrashed()->select('email', 'mobile')->where('aha_user_id', $this->aha_user_id)->first(); return $customer ?? null; } public function getCustomerIdAttribute($value) { if($value === null) { $this->foreignKey = 'aha_user_id'; $this->ownerKey = 'aha_user_id'; $customer = Customer::where('aha_user_id', $this->aha_user_id)->first(); $value = $customer->id ?? null; } return $value; } public function customer() { return $this->belongsTo(Customer::class, $this->foreignKey, $this->ownerKey); } public function getDealerNameAttribute() { return $this->dealer()->withTrashed()->pluck('name')->first(); } public function getBookingTimeAttribute() { return date('M j, Y h:ia (D)', strtotime("$this->date $this->time")); } public function getTypeNameAttribute() { return $this->serviceType()->withTrashed()->pluck('name')->first(); } public function getStatusNameAttribute() { return $this->serviceStatus()->withTrashed()->pluck('name')->first(); } public function getVehicleNameAttribute() { $vehicle = $this->vehicle()->withTrashed()->get(['description', 'plate_no'])->first(); return $vehicle->name ?? null; } public function getAdvisorNameAttribute() { if($this->assigned_service_advisor == null) return null; $employee = User::where('employee_id', $this->assigned_service_advisor)->first(); return $employee ? join(' ',[$employee->first_name, $employee->last_name]) : null; } public function getValidatedByNameAttribute() { if ($this->validated_by) { if($user = User::find($this->validated_by)) { return ucwords(strtolower("{$user->first_name} {$user->last_name}")); } } return null; } public function getCancelledByNameAttribute() { if ($this->cancelled_by) { $cancelledBy = $this->cancelled_by; if($this->aha_user_id && $this->aha_user_id == $this->cancelled_by) { $customer = Customer::where('aha_user_id', $this->cancelled_by)->first(); if($customer) $cancelledBy = ucwords(strtolower($customer->firstname . " " . $customer->lastname)); } else { $user = User::where('employee_id',$this->cancelled_by)->first(); if($user) $cancelledBy = ucwords(strtolower($user->first_name . " " . $user->last_name)); } return $cancelledBy; } return null; } public function promo() { return $this->hasOne(PromoCode::class, 'promo_code', 'promo_code'); } public function getPromoAmountAttribute() { if (!$this->promo_code || $this->promo_code != 'undefined') { $promo = $this->promo()->get()->first(); if($promo) { if($promo->is_percentage){ // Check if '%' is in the text if (strpos($promo->promo_amount, '%') === false) { // Add '%' if not found $promo->promo_amount .= '%'; } return $promo->promo_amount; } else { return '₱' . $promo->promo_amount; } } } return null; } public function getArrivedDateAttribute() { if ($this->service_status_id >= 3) { $logs = ServiceBookingLog::where('service_booking_id', $this->id)->where('status_id', 3)->first(); if($logs) { return $logs->created_at; } return null; } return null; } public function getStatusUpdateAttribute() { if ($this->service_status_id >= 3) { $logs = ServiceBookingLog::where('service_booking_id', $this->id)->max('created_at'); if($logs) { // dd($logs); return $logs; } return null; } return null; } }
| ver. 1.4 |
.
| PHP 8.1.32 | Generation time: 0 |
proxy
|
phpinfo
|
Settings