Server : Apache System : Linux 145.162.205.92.host.secureserver.net 5.14.0-611.45.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Apr 1 05:56:53 EDT 2026 x86_64 User : tradze ( 1001) PHP Version : 8.1.34 Disable Function : NONE Directory : /home/tradze/public_html/app/Modules/Schedules/Models/ |
<?php
namespace App\Modules\Schedules\Models;
use App\Modules\Schedules\Repositories\BookingRepository;
use App\Modules\Schedules\Scopes\ActiveScope;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class BookingOrder extends Model
{
use SoftDeletes;
/**
* The database table used by the model.
*
* @var string
*/
protected $table="booking_orders";
protected $appends = ['order'];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user_id',
'order_id',
'salon_id',
'provider_id',
'is_active',
'is_modal_triggered',
'amount',
'date',
'hour',
'duration',
'duration_min',
'massage_type',
'address',
'location',
'locationGeo',
'orderInfo',
'card_trans_id',
'card_trans_status',
'card_details',
'internal_notes',
'updatedby_id'
];
/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();
//add global scope: the booking must be active
static::addGlobalScope(new ActiveScope);
}
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at','date'];
/**
* The profile that belong to the user account.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo('App\User');
}
/**
* The booking invoice
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function invoice()
{
return $this->hasOne('App\Modules\Invoices\Models\Invoice','booking_id');
}
/**
* The booking invoice
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function invoiceMorph()
{
return $this->morphMany('App\Modules\Invoices\Models\Invoice','invoiceable');
}
/**
* The booking invoice
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function thinvoices()
{
return $this->morphMany('App\Modules\Invoices\Models\Invoice','invoiceable');
}
/**
* The services that belong to the invoice.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function forTherapists()
{
return $this->belongsToMany('App\User','invoices_bookings_therapists','booking_id','therapist_id')->withTimestamps()->withPivot('invoice_id');
}
/**
* The services that belong to the invoice.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function invTherapists()
{
return $this->belongsToMany('App\Modules\Invoices\Models\Invoice','invoices_bookings_therapists','booking_id','invoice_id')->withTimestamps()->withPivot('therapist_id');
}
/**
* The profile that belong to the user account.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function therapists()
{
return $this->belongsToMany('App\User','booking_therapists','booking_id','therapist_id')->withTimestamps()->withPivot('hour','date','duration', 'invoice_table');
}
/**
* Get the user's first name.
*
* @param string $value
* @return string
*/
public function getDateToHumanAttribute()
{
$date = $this->date;
return $date->format('d M Y');
}
/**
* Get the user's first name.
*
* @param string $value
* @return string
*/
public function getHourToHumanAttribute($value)
{
$date = Carbon::createFromFormat('H:i',$this->hour);
return $date->format('h:i A');
}
/**
* Get the "can be tracked" attribute.
*
* @param string $value
* @return string
*/
public function getCanBeTrackedAttribute()
{
$status = false;
if ($this->time_left_in_seconds <= 3600)
$status=true;
if (!$status)
return $status;
//continue...
//prevent therapist to be tracked once the booking is finished
$bookdate=Carbon::createFromFormat('Y-m-d H:i',$this->date->format('Y-m-d').' '.$this->hour);
$bookdate->addMinutes($this->duration_min);
$diff = $bookdate->diffInSeconds(\Carbon\Carbon::now(),false);
if ($diff>0)
$status=false;
// $status=true;
//return
return $status;
}
/**
* Booking time left in seconds
*
* @param string $value
* @return string
*/
public function getTimeLeftInSecondsAttribute($value)
{
$bookdate= Carbon::createFromFormat('Y-m-d H:i',$this->date->format('Y-m-d').' '.$this->hour);
$remaining_time = $bookdate->diffInSeconds(null,false)*(-1);
if ($remaining_time<0)
$remaining_time=0;
return $remaining_time;
}
/**
* Can generate invoice attribute
*
* @param $value
* @return bool
*/
public function getCanGenerateInvoiceAttribute($value)
{
$status=false;
if (!$this->is_active)
return $status;
//prevent duplicating invoice
if ($this->invoice)
return $status;
//all conditions are passed, then can generate invoice
$status=true;
//return status
return $status;
}
/**
* Can be cancelled attribute
* @param $value
* @return bool
*/
public function getStatusCanceledAttribute($value)
{
$check = new BookingRepository();
$status = $check->can_be_safe_canceled($this);
//return status
return $status;
}
/**
* Get the thread subject attribute.
*
* @param string $value
* @return string
*/
public function getTreadSubjectAttribute()
{
if (!$this->user)
return $subject='';
$subject = $this->date->format('d M Y').' '.$this->hour.' - '.$this->duration.' - '.$this->user->name.' - #'.$this->id;
return $subject;
}
public function getOrderAttribute($value)
{
if($this->orderInfo){
return json_decode($this->orderInfo);
}
}
/**
* Scope by therapist
* @param $query
* @param null $search
* @return mixed
*/
public function scopeOfTherapist($query,$search=null)
{
// if ($search)
// $query->where('therapist_id', '=', $search);
if ($search)
$query->whereHas('therapists',function($query) use ($search){
return $query->where('therapist_id',$search);
});
return $query;
}
public function scopeOfSalon($query,$search=null){
// { dd($search);
if ($search)
$query->where('salon_id',$search);
return $query;
}
/**
* Scope by therapist
* @param $query
* @param null $search
* @return mixed
*/
public function scopeOfClient($query,$search=null)
{
if ($search)
$query->where('user_id', '=', $search);
return $query;
}
/**
* Scope by booking ID
* @param $query
* @param null $search
* @return mixed
*/
public function scopeOfNumber($query,$search=null)
{
if ($search)
$query->where('id', '=', $search);
return $query;
}
/**
* Scope by order ID
* @param $query
* @param null $search
* @return mixed
*/
public function scopeOfOrder($query,$search=null)
{
if ($search)
$query->where('order_id', '=', $search);
return $query;
}
/**
* Scope by order date from
* @param $query
* @param null $search
* @return mixed
*/
public function scopeOfDateFrom($query,$search=null)
{
if ($search){
$date = Carbon::createFromFormat('d/m/Y',$search)->format('Y-m-d');
$query->where('date', '>=', $date);
}
return $query;
}
/**
* Scope by order date to
* @param $query
* @param null $search
* @return mixed
*/
public function scopeOfDateTo($query,$search=null)
{
if ($search){
$date = Carbon::createFromFormat('d/m/Y',$search)->format('Y-m-d');
$query->where('date', '<=', $date);
}
return $query;
}
}