File: D:/HostingSpaces/SBogers85/equichecker.com/app/KommaApp/Customers/Customer.php
<?php
namespace KommaApp\Customers;
use Carbon\Carbon;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Kbwebs\MultiAuth\PasswordResets\CanResetPassword;
use Kbwebs\MultiAuth\PasswordResets\Contracts\CanResetPassword as CanResetPasswordContract;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
class Customer extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
protected $table = 'customers';
protected $hidden = ['hash','remember_token'];
protected $with= ['language'];
protected $fillable = [
'site_id',
'customer_number',
'active_until',
'active',
'language_id',
'username',
'password',
'email',
'gender',
'title',
'first_name',
'name_insertion',
'last_name',
'postal',
'street',
'house_number',
'house_number_suffix',
'city',
'country',
'company',
'company_vat',
'remember_token', // Todo: Needs to be not fillable ?!
'validate_token', // Todo: Needs to be not fillable ?!
'telephone'
];
public function orders()
{
return $this->hasMany('\KommaApp\Orders\Order','customer_id', 'id');
}
public function ordersDesc()
{
return $this->orders()->orderBy('ordered_at', 'desc');
}
public function getProcessedOrders()
{
return $this->orders()->where('status', '!=', 'openstaand')->whereNotNull('status')->get();
}
public function getFullName(){
$name = [];
if($this->first_name) $name[] = $this->first_name;
if($this->name_insertion) $name[] = $this->name_insertion;
if($this->last_name) $name[] = $this->last_name;
return implode(' ', $name);
}
public function getFullNameAndAddress(){
$address = [];
$address[] = $this->getFullName();
$address[] = $this->street . ' ' . $this->house_number . $this->house_number_suffix;
$address[] = $this->postal . ' ' . $this->city;
$address[] = $this->country;
if (!empty($this->telephone)) $address[] = '<br />' . $this->telephone;
return implode('<br/>', $address);
}
public static function getCustomersWithUnnotifiedOpenPayments()
{
return self::with(['orders' => function($query){
$query->whereNull('payment_open_notification_on');
$query->where('status', Order::OPEN);
$query->where('ordered_at', '<=', Carbon::now()->subDays(2));
}])->get();
}
public static function getNewCustomerNumber()
{
if(self::max('customer_number') > 0)
return self::max('customer_number') + 1;
return \Config::get('komma/komma.customerNumberStart');
}
public function language()
{
return $this->hasOne('KommaApp\Languages\Models\Language', 'id', 'language_id');
}
}