2018-11-02 11:54:46 +01:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2018-11-02 11:54:46 +01:00
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2019-05-24 11:23:38 +02:00
|
|
|
use App\DataMapper\ClientSettings;
|
|
|
|
use App\DataMapper\CompanySettings;
|
2019-01-19 11:35:21 +01:00
|
|
|
use App\Filters\QueryFilters;
|
2019-02-17 11:34:46 +01:00
|
|
|
use App\Utils\Traits\UserSessionAttributes;
|
2018-11-02 11:54:46 +01:00
|
|
|
use Hashids\Hashids;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2019-05-01 04:23:13 +02:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2019-03-28 10:05:13 +01:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-11-02 11:54:46 +01:00
|
|
|
|
|
|
|
class BaseModel extends Model
|
|
|
|
{
|
2019-02-17 11:34:46 +01:00
|
|
|
use UserSessionAttributes;
|
2019-05-01 04:23:13 +02:00
|
|
|
use SoftDeletes;
|
2019-02-17 11:34:46 +01:00
|
|
|
|
2019-05-07 12:48:43 +02:00
|
|
|
//protected $dateFormat = 'Y-m-d H:i:s.u';
|
2019-05-07 07:08:10 +02:00
|
|
|
|
2018-12-13 12:01:33 +01:00
|
|
|
public function __call($method, $params)
|
2018-11-02 11:54:46 +01:00
|
|
|
{
|
2018-12-13 12:01:33 +01:00
|
|
|
$entity = strtolower(class_basename($this));
|
2018-11-02 11:54:46 +01:00
|
|
|
|
2018-12-13 12:01:33 +01:00
|
|
|
if ($entity) {
|
|
|
|
$configPath = "modules.relations.$entity.$method";
|
|
|
|
|
|
|
|
if (config()->has($configPath)) {
|
|
|
|
$function = config()->get($configPath);
|
|
|
|
|
2018-12-15 14:04:46 +01:00
|
|
|
return call_user_func_array(array($this, $function[0]), $function[1]);
|
2018-12-13 12:01:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::__call($method, $params);
|
2018-11-02 11:54:46 +01:00
|
|
|
}
|
2019-01-19 11:35:21 +01:00
|
|
|
|
2019-05-24 11:23:38 +02:00
|
|
|
/*
|
|
|
|
V2 type of scope
|
|
|
|
*/
|
2019-05-01 04:23:13 +02:00
|
|
|
public function scopeCompany($query, $company_id)
|
|
|
|
{
|
|
|
|
$query->where('company_id', $company_id);
|
2019-05-10 08:08:33 +02:00
|
|
|
|
2019-05-01 04:23:13 +02:00
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2019-05-24 11:23:38 +02:00
|
|
|
/*
|
|
|
|
V1 type of scope
|
|
|
|
*/
|
2019-02-17 11:34:46 +01:00
|
|
|
public function scopeScope($query)
|
|
|
|
{
|
2019-03-28 10:05:13 +01:00
|
|
|
|
|
|
|
$query->where($this->getTable() .'.company_id', '=', auth()->user()->company()->id);
|
2019-02-17 11:34:46 +01:00
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2019-05-24 11:23:38 +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}))
|
|
|
|
{
|
|
|
|
//Log::error('harvesting client settings for key = '. $key . ' and it has the value = '. $this->getSettings()->{$key});
|
|
|
|
//Log::error(print_r($this->getSettings(),1));
|
2019-05-27 07:26:34 +02:00
|
|
|
return $this->getSettings()->{$key};
|
2019-05-24 11:23:38 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
//Log::error(print_r(new CompanySettings($this->company->settings),1));
|
2019-05-27 07:26:34 +02:00
|
|
|
return (new CompanySettings($this->company->settings))->{$key};
|
2019-05-24 11:23:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function setSettingsByEntity($entity, $settings)
|
|
|
|
{
|
|
|
|
switch ($entity) {
|
|
|
|
case Client::class:
|
|
|
|
// Log::error('saving client settings');
|
|
|
|
$this->settings = $settings;
|
|
|
|
$this->save();
|
|
|
|
$this->fresh();
|
|
|
|
break;
|
|
|
|
case Company::class:
|
|
|
|
// Log::error('saving company settings');
|
|
|
|
$this->company->settings = $settings;
|
|
|
|
$this->company->save();
|
|
|
|
break;
|
|
|
|
//todo check that saving any other entity (Invoice:: RecurringInvoice::) settings is valid using the default:
|
|
|
|
default:
|
|
|
|
$this->client->settings = $settings;
|
|
|
|
$this->client->save();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the settings.
|
|
|
|
*
|
|
|
|
* Generic getter for client settings
|
|
|
|
*
|
|
|
|
* @return ClientSettings The settings.
|
|
|
|
*/
|
|
|
|
public function getSettings()
|
|
|
|
{
|
|
|
|
return new ClientSettings($this->settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-02 11:54:46 +01:00
|
|
|
}
|