118 lines
3.1 KiB
PHP
118 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class LoyaltyCard extends Model
|
|
{
|
|
|
|
/**
|
|
* Table name of model
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $table = 'loyalty_card';
|
|
|
|
/**
|
|
* Primary key field name of table
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $primaryKey = 'lcard_id';
|
|
|
|
/**
|
|
* Additional fields from other connected tables
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $appends = [];
|
|
|
|
/**
|
|
* The attributes that should be hidden for arrays.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $hidden = [];
|
|
|
|
/**
|
|
* The attributes that should be mutated to dates.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $dates = ['created_at','updated_at','expiry_date','birthdate'];
|
|
|
|
|
|
public function personalDetails()
|
|
{
|
|
return $this->belongsTo('App\PersonalDetails','pd_id','pd_id');
|
|
}
|
|
|
|
public function codeVehicleOwn()
|
|
{
|
|
return $this->hasOne('App\CodeVehicleOwn','vo_id','vo_id');
|
|
}
|
|
|
|
public function codeCity()
|
|
{
|
|
return $this->hasOne('App\CodeCity','city_id','city_id');
|
|
}
|
|
|
|
public function codeFuelType()
|
|
{
|
|
return $this->hasOne('App\CodeFuelType','fueltype_id','fueltype_id');
|
|
}
|
|
|
|
public function codeCivilStatus()
|
|
{
|
|
return $this->hasOne('App\CodeCivilStatus','civilstatus_id','civilstatus_id');
|
|
}
|
|
|
|
public function codeGender()
|
|
{
|
|
return $this->hasOne('App\CodeGender','gender_id','gender_id');
|
|
}
|
|
|
|
public function codeCardType()
|
|
{
|
|
return $this->hasOne('App\CodeCardType','cardtype_id','cardtype_id');
|
|
}
|
|
|
|
public function otpAttempts()
|
|
{
|
|
return $this->hasMany('App\OtpAttempts','lcard_id','lcard_id');
|
|
}
|
|
|
|
public function scopeSort($query, $field, $order)
|
|
{
|
|
switch ($field) {
|
|
case 'firstname':
|
|
$query->leftJoin('personal_details', 'loyalty_card.pd_id', '=','personal_details.pd_id');
|
|
$query->orderBy('personal_details.firstname', $order);
|
|
break;
|
|
case 'lastname':
|
|
$query->leftJoin('personal_details', 'loyalty_card.pd_id', '=','personal_details.pd_id');
|
|
$query->orderBy('personal_details.lastname', $order);
|
|
break;
|
|
case 'card_type':
|
|
$query->leftJoin('code_card_type', 'loyalty_card.cardtype_id', '=','code_card_type.cardtype_id');
|
|
$query->orderBy('code_card_type.code', $order);
|
|
break;
|
|
case 'status':
|
|
$query->orderBy('is_validated', $order);
|
|
break;
|
|
default:
|
|
return $query->orderBy($field, $order);
|
|
}
|
|
}
|
|
|
|
public function scopeNumberOfOTPAttempts($query)
|
|
{
|
|
$date_now = date('Y-m-d H:i:s');
|
|
$date_yesterday = date('Y-m-d H:i:s',strtotime('-24 hours',strtotime($date_now)));
|
|
$query->leftJoin('otp_attempts', 'loyalty_card.lcard_id', '=','otp_attempts.lcard_id')
|
|
->whereBetween('otp_attempts.created_at',[$date_yesterday,$date_now]);
|
|
|
|
}
|
|
}
|