2018-10-15 07:00:48 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2019-03-02 22:44:08 +01:00
|
|
|
use App\DataMapper\ClientSettings;
|
2019-04-29 02:54:26 +02:00
|
|
|
use App\DataMapper\CompanySettings;
|
2019-04-29 07:50:08 +02:00
|
|
|
use App\Models\Client;
|
2019-01-25 11:47:23 +01:00
|
|
|
use App\Models\Company;
|
2019-01-26 10:34:38 +01:00
|
|
|
use App\Models\Country;
|
2019-03-28 11:07:45 +01:00
|
|
|
use App\Models\Filterable;
|
2019-04-24 02:22:02 +02:00
|
|
|
use App\Models\Timezone;
|
2019-04-29 14:14:11 +02:00
|
|
|
use App\Utils\Traits\GeneratesNumberCounter;
|
2019-04-30 08:02:39 +02:00
|
|
|
use App\Utils\Traits\MakesDates;
|
2018-11-10 14:24:36 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2019-01-25 11:47:23 +01:00
|
|
|
use Hashids\Hashids;
|
2018-11-12 08:52:20 +01:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2019-04-30 14:30:47 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2019-01-25 11:47:23 +01:00
|
|
|
use Laracasts\Presenter\PresentableTrait;
|
2018-10-15 07:00:48 +02:00
|
|
|
|
2018-11-02 11:54:46 +01:00
|
|
|
class Client extends BaseModel
|
2018-10-15 07:00:48 +02:00
|
|
|
{
|
2018-11-02 11:54:46 +01:00
|
|
|
use PresentableTrait;
|
2018-11-10 14:24:36 +01:00
|
|
|
use MakesHash;
|
2019-04-30 08:02:39 +02:00
|
|
|
use MakesDates;
|
2018-11-12 08:52:20 +01:00
|
|
|
use SoftDeletes;
|
2019-03-28 11:07:45 +01:00
|
|
|
use Filterable;
|
2019-04-29 14:14:11 +02:00
|
|
|
use GeneratesNumberCounter;
|
2019-03-28 11:07:45 +01:00
|
|
|
|
2018-11-02 11:54:46 +01:00
|
|
|
protected $presenter = 'App\Models\Presenters\ClientPresenter';
|
|
|
|
|
2019-03-02 22:44:08 +01:00
|
|
|
protected $appends = [
|
|
|
|
];
|
2018-11-10 14:24:36 +01:00
|
|
|
|
2018-11-12 08:52:20 +01:00
|
|
|
protected $guarded = [
|
2018-11-27 07:59:16 +01:00
|
|
|
'id',
|
|
|
|
'updated_at',
|
|
|
|
'created_at',
|
|
|
|
'deleted_at',
|
2018-11-27 08:24:26 +01:00
|
|
|
'contacts',
|
|
|
|
'primary_contact',
|
2019-01-25 11:47:23 +01:00
|
|
|
'q',
|
2019-01-26 10:34:38 +01:00
|
|
|
'company',
|
|
|
|
'country',
|
|
|
|
'shipping_country'
|
2018-11-10 14:24:36 +01:00
|
|
|
];
|
2018-11-27 07:59:16 +01:00
|
|
|
|
2019-04-29 02:54:26 +02:00
|
|
|
protected $with = [
|
|
|
|
'contacts',
|
|
|
|
'primary_contact',
|
|
|
|
'country',
|
|
|
|
'shipping_country',
|
|
|
|
'company'
|
|
|
|
];
|
2019-01-22 10:47:26 +01:00
|
|
|
|
2019-02-17 11:34:46 +01:00
|
|
|
protected $casts = [
|
|
|
|
'settings' => 'object'
|
|
|
|
];
|
|
|
|
|
2018-10-29 04:16:17 +01:00
|
|
|
public function contacts()
|
|
|
|
{
|
2019-01-25 11:47:23 +01:00
|
|
|
return $this->hasMany(ClientContact::class)->orderBy('is_primary', 'desc');
|
2018-10-29 04:16:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function primary_contact()
|
|
|
|
{
|
|
|
|
return $this->hasMany(ClientContact::class)->whereIsPrimary(true);
|
|
|
|
}
|
|
|
|
|
2019-01-25 11:47:23 +01:00
|
|
|
public function company()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Company::class);
|
|
|
|
}
|
|
|
|
|
2019-01-26 10:34:38 +01:00
|
|
|
public function country()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Country::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function shipping_country()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Country::class, 'shipping_country_id', 'id');
|
|
|
|
}
|
|
|
|
|
2019-04-24 02:22:02 +02:00
|
|
|
public function timezone()
|
|
|
|
{
|
2019-04-29 02:54:26 +02:00
|
|
|
return Timezone::find($this->getMergedSettings()->timezone_id);
|
2019-04-24 02:22:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getSettings()
|
|
|
|
{
|
2019-04-29 02:54:26 +02:00
|
|
|
return new ClientSettings($this->settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getMergedSettings()
|
|
|
|
{
|
|
|
|
return ClientSettings::buildClientSettings(new CompanySettings($this->company->settings), new ClientSettings($this->settings));
|
2019-04-24 02:22:02 +02:00
|
|
|
}
|
|
|
|
|
2019-04-29 07:50:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the settings by key.
|
|
|
|
*
|
|
|
|
* When we need to update a setting value, we need to harvest
|
|
|
|
* the object of the setting. This is not possible when using the merged settings
|
|
|
|
* as we do not know which object the setting has come from.
|
|
|
|
*
|
|
|
|
* The following method will return the entire object of the property searched for
|
|
|
|
* where a value exists for $key.
|
|
|
|
*
|
|
|
|
* This object can then be mutated by the handling class,
|
|
|
|
* to persist the new settings we will also need to pass back a
|
|
|
|
* reference to the parent class.
|
|
|
|
*
|
|
|
|
* @param mixes $key The key of property
|
|
|
|
*/
|
|
|
|
public function getSettingsByKey($key)
|
|
|
|
{
|
|
|
|
/* Does Setting Exist @ client level */
|
|
|
|
if(isset($this->getSettings()->{$key}))
|
2019-05-01 05:33:04 +02:00
|
|
|
{
|
|
|
|
//Log::error('harvesting client settings for key = '. $key . ' and it has the value = '. $this->getSettings()->{$key});
|
|
|
|
//Log::error(print_r($this->getSettings(),1));
|
2019-04-29 07:50:08 +02:00
|
|
|
return $this->getSettings();
|
|
|
|
}
|
2019-05-01 05:33:04 +02:00
|
|
|
else {
|
2019-05-01 08:09:55 +02:00
|
|
|
//Log::error(print_r(new CompanySettings($this->company->settings),1));
|
2019-05-01 05:33:04 +02:00
|
|
|
return new CompanySettings($this->company->settings);
|
|
|
|
}
|
2019-04-29 07:50:08 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setSettingsByEntity($entity, $settings)
|
|
|
|
{
|
|
|
|
switch ($entity) {
|
|
|
|
case Client::class:
|
2019-05-01 08:09:55 +02:00
|
|
|
Log::error('saving client settings');
|
2019-04-29 07:50:08 +02:00
|
|
|
$this->settings = $settings;
|
|
|
|
$this->save();
|
2019-05-01 08:09:55 +02:00
|
|
|
$this->fresh();
|
2019-04-29 07:50:08 +02:00
|
|
|
break;
|
|
|
|
case Company::class:
|
2019-05-01 08:09:55 +02:00
|
|
|
Log::error('saving company settings');
|
2019-04-29 07:50:08 +02:00
|
|
|
$this->company->settings = $settings;
|
2019-05-01 05:33:04 +02:00
|
|
|
$this->company->save();
|
2019-05-01 08:09:55 +02:00
|
|
|
$this->company->fresh();
|
2019-04-29 07:50:08 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
# code...
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-28 07:31:32 +02:00
|
|
|
public function documents()
|
|
|
|
{
|
|
|
|
return $this->morphMany(Document::class, 'documentable');
|
|
|
|
}
|
|
|
|
|
2019-04-24 02:22:02 +02:00
|
|
|
|
|
|
|
|
2019-04-29 07:50:08 +02:00
|
|
|
|
2018-10-15 07:00:48 +02:00
|
|
|
}
|