2018-11-02 11:54:46 +01:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-11 05:32:07 +02:00
|
|
|
*/
|
2018-11-02 11:54:46 +01:00
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2019-05-24 11:23:38 +02:00
|
|
|
use App\DataMapper\ClientSettings;
|
2019-07-30 00:28:38 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2019-02-17 11:34:46 +01:00
|
|
|
use App\Utils\Traits\UserSessionAttributes;
|
2020-11-25 15:19:52 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2018-11-02 11:54:46 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2020-03-16 11:12:10 +01:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;
|
2020-08-15 02:40:56 +02:00
|
|
|
use Illuminate\Support\Carbon;
|
2022-03-15 23:31:35 +01:00
|
|
|
use Illuminate\Support\Str;
|
2018-11-02 11:54:46 +01:00
|
|
|
|
2021-02-13 01:20:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class BaseModel
|
|
|
|
*
|
|
|
|
* @method scope() static
|
|
|
|
*
|
|
|
|
* @package App\Models
|
|
|
|
*/
|
2018-11-02 11:54:46 +01:00
|
|
|
class BaseModel extends Model
|
|
|
|
{
|
2019-07-30 00:28:38 +02:00
|
|
|
use MakesHash;
|
2019-02-17 11:34:46 +01:00
|
|
|
use UserSessionAttributes;
|
2020-10-01 12:49:47 +02:00
|
|
|
use HasFactory;
|
2020-10-28 11:10:49 +01:00
|
|
|
|
2019-07-30 00:28:38 +02:00
|
|
|
protected $appends = [
|
2020-09-06 11:38:10 +02:00
|
|
|
'hashed_id',
|
2019-07-30 00:28:38 +02:00
|
|
|
];
|
|
|
|
|
2019-09-26 00:27:26 +02:00
|
|
|
protected $casts = [
|
|
|
|
'updated_at' => 'timestamp',
|
|
|
|
'created_at' => 'timestamp',
|
|
|
|
'deleted_at' => 'timestamp',
|
|
|
|
];
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-07-09 02:01:29 +02:00
|
|
|
protected $dateFormat = 'Y-m-d H:i:s.u';
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-07-30 00:28:38 +02:00
|
|
|
public function getHashedIdAttribute()
|
|
|
|
{
|
|
|
|
return $this->encodePrimaryKey($this->id);
|
|
|
|
}
|
|
|
|
|
2020-08-15 02:40:56 +02:00
|
|
|
public function dateMutator($value)
|
2020-08-15 00:03:29 +02:00
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! empty($value)) {
|
2020-08-15 00:03:29 +02:00
|
|
|
return (new Carbon($value))->format('Y-m-d');
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-08-15 00:03:29 +02:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
return call_user_func_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-11-27 10:47:59 +01:00
|
|
|
public function scopeCompany($query)
|
2019-05-01 04:23:13 +02:00
|
|
|
{
|
2019-11-27 10:47:59 +01:00
|
|
|
$query->where('company_id', auth()->user()->companyId());
|
2020-09-06 11:38:10 +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)
|
|
|
|
{
|
2020-09-06 11:38:10 +02: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.
|
|
|
|
*
|
2019-12-30 22:59:12 +01:00
|
|
|
* This object can then be mutated by the handling class,
|
|
|
|
* to persist the new settings we will also need to pass back a
|
2019-05-24 11:23:38 +02:00
|
|
|
* reference to the parent class.
|
|
|
|
*
|
2021-12-17 12:11:36 +01:00
|
|
|
* @param $key The key of property
|
2023-01-17 01:00:12 +01:00
|
|
|
* @deprecated
|
2019-05-24 11:23:38 +02:00
|
|
|
*/
|
2023-01-17 01:00:12 +01:00
|
|
|
// public function getSettingsByKey($key)
|
|
|
|
// {
|
|
|
|
// /* Does Setting Exist @ client level */
|
|
|
|
// if (isset($this->getSettings()->{$key})) {
|
|
|
|
// return $this->getSettings()->{$key};
|
|
|
|
// } else {
|
|
|
|
// 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:
|
2019-10-22 13:27:03 +02:00
|
|
|
|
2019-05-24 11:23:38 +02:00
|
|
|
$this->settings = $settings;
|
|
|
|
$this->save();
|
|
|
|
$this->fresh();
|
|
|
|
break;
|
|
|
|
case Company::class:
|
2019-10-22 13:27:03 +02:00
|
|
|
|
2019-05-24 11:23:38 +02:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-05-24 11:23:38 +02:00
|
|
|
/**
|
|
|
|
* Gets the settings.
|
|
|
|
*
|
|
|
|
* Generic getter for client settings
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-05-24 11:23:38 +02:00
|
|
|
* @return ClientSettings The settings.
|
|
|
|
*/
|
|
|
|
public function getSettings()
|
|
|
|
{
|
|
|
|
return new ClientSettings($this->settings);
|
|
|
|
}
|
|
|
|
|
2019-11-05 00:22:36 +01:00
|
|
|
/**
|
|
|
|
* Retrieve the model for a bound value.
|
|
|
|
*
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param mixed $value
|
|
|
|
* @param null $field
|
|
|
|
* @return Model|null
|
2019-11-05 00:22:36 +01:00
|
|
|
*/
|
2020-11-25 15:19:52 +01:00
|
|
|
public function resolveRouteBinding($value, $field = null)
|
2019-11-05 00:22:36 +01:00
|
|
|
{
|
2023-01-13 02:43:38 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (is_numeric($value)) {
|
2020-02-17 20:07:31 +01:00
|
|
|
throw new ModelNotFoundException("Record with value {$value} not found");
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-02-17 20:07:31 +01:00
|
|
|
|
2019-11-05 00:22:36 +01:00
|
|
|
return $this
|
|
|
|
->withTrashed()
|
2023-01-23 21:25:06 +01:00
|
|
|
// ->company()
|
2019-11-05 00:22:36 +01:00
|
|
|
->where('id', $this->decodePrimaryKey($value))->firstOrFail();
|
|
|
|
}
|
2020-03-16 11:12:10 +01:00
|
|
|
|
2020-04-10 07:07:36 +02:00
|
|
|
/**
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param string $extension
|
2020-04-10 07:07:36 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFileName($extension = 'pdf')
|
|
|
|
{
|
2021-03-17 12:29:20 +01:00
|
|
|
return $this->numberFormatter().'.'.$extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function numberFormatter()
|
|
|
|
{
|
2023-01-21 23:45:46 +01:00
|
|
|
$number = strlen($this->number) >= 1 ? $this->translate_entity() . "_" . $this->number : class_basename($this) . "_" . Str::random(5);
|
2021-03-24 10:14:30 +01:00
|
|
|
|
|
|
|
$formatted_number = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $number);
|
2022-12-13 23:20:26 +01:00
|
|
|
|
2021-03-17 12:29:20 +01:00
|
|
|
$formatted_number = mb_ereg_replace("([\.]{2,})", '', $formatted_number);
|
2021-11-15 11:46:58 +01:00
|
|
|
|
2022-01-10 22:19:44 +01:00
|
|
|
$formatted_number = preg_replace('/\s+/', '_', $formatted_number);
|
|
|
|
|
2021-03-17 12:29:20 +01:00
|
|
|
return $formatted_number;
|
2020-04-10 07:07:36 +02:00
|
|
|
}
|
2021-05-15 04:19:36 +02:00
|
|
|
|
2022-07-16 08:15:31 +02:00
|
|
|
public function translate_entity()
|
|
|
|
{
|
|
|
|
return ctrans('texts.item');
|
|
|
|
}
|
|
|
|
|
2018-11-02 11:54:46 +01:00
|
|
|
}
|