invoiceninja/app/Models/BaseModel.php

234 lines
6.4 KiB
PHP
Raw Normal View History

<?php
2019-05-11 13:32:07 +10:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 13:32:07 +10:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-29 09:21:40 +11:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 13:32:07 +10:00
*
2021-06-16 16:58:16 +10:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 13:32:07 +10:00
*/
namespace App\Models;
2023-02-22 17:37:16 +11:00
use App\DataMapper\ClientSettings;
use App\Jobs\Util\WebhookHandler;
use App\Models\Traits\Excludable;
2023-02-22 17:37:16 +11:00
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\UserSessionAttributes;
2020-11-25 15:19:52 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2023-02-22 17:37:16 +11:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;
2023-02-22 17:37:16 +11:00
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
2021-02-12 19:20:15 -05:00
/**
* Class BaseModel
*
* @method scope() static
* @package App\Models
2023-03-08 18:33:42 +11:00
* @property-read mixed $hashed_id
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel company()
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel exclude($columns)
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel query()
* @mixin \Eloquent
2021-02-12 19:20:15 -05:00
*/
class BaseModel extends Model
{
2019-07-30 08:28:38 +10:00
use MakesHash;
use UserSessionAttributes;
2020-10-01 20:49:47 +10:00
use HasFactory;
use Excludable;
2020-10-28 21:10:49 +11:00
2019-07-30 08:28:38 +10:00
protected $appends = [
'hashed_id',
2019-07-30 08:28:38 +10:00
];
protected $casts = [
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
];
protected $dateFormat = 'Y-m-d H:i:s.u';
2019-07-30 08:28:38 +10:00
public function getHashedIdAttribute()
{
return $this->encodePrimaryKey($this->id);
}
2020-08-15 10:40:56 +10:00
public function dateMutator($value)
2020-08-15 08:03:29 +10:00
{
if (! empty($value)) {
2020-08-15 08:03:29 +10:00
return (new Carbon($value))->format('Y-m-d');
}
2020-08-15 08:03:29 +10:00
return $value;
}
public function __call($method, $params)
{
$entity = strtolower(class_basename($this));
if ($entity) {
$configPath = "modules.relations.$entity.$method";
if (config()->has($configPath)) {
$function = config()->get($configPath);
return call_user_func_array([$this, $function[0]], $function[1]);
}
}
return parent::__call($method, $params);
}
/*
V2 type of scope
*/
public function scopeCompany($query)
2019-05-01 12:23:13 +10:00
{
$query->where('company_id', auth()->user()->companyId());
2019-05-01 12:23:13 +10:00
return $query;
}
/*
V1 type of scope
*/
public function scopeScope($query)
{
$query->where($this->getTable().'.company_id', '=', auth()->user()->company()->id);
return $query;
}
/**
* 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.
*
2021-12-17 22:11:36 +11:00
* @param $key The key of property
2023-01-17 11:00:12 +11:00
* @deprecated
*/
2023-01-17 11:00:12 +11: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};
// }
// }
public function setSettingsByEntity($entity, $settings)
{
switch ($entity) {
case Client::class:
$this->settings = $settings;
$this->save();
$this->fresh();
break;
case Company::class:
$this->company->settings = $settings;
$this->company->save();
break;
2023-02-16 12:36:09 +11:00
//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);
}
/**
* Retrieve the model for a bound value.
*
2020-10-28 21:10:49 +11:00
* @param mixed $value
* @param null $field
* @return Model|null
*/
2020-11-25 15:19:52 +01:00
public function resolveRouteBinding($value, $field = null)
{
if (is_numeric($value)) {
throw new ModelNotFoundException("Record with value {$value} not found");
}
return $this
->withTrashed()
2023-01-24 07:25:06 +11:00
// ->company()
->where('id', $this->decodePrimaryKey($value))->firstOrFail();
}
/**
2020-10-28 21:10:49 +11:00
* @param string $extension
* @return string
*/
public function getFileName($extension = 'pdf')
{
return $this->numberFormatter().'.'.$extension;
}
public function numberFormatter()
{
2023-02-07 09:45:02 +01:00
$number = strlen($this->number) >= 1 ? $this->translate_entity() . "_" . $this->number : class_basename($this) . "_" . Str::random(5);
$formatted_number = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $number);
2023-02-07 09:45:02 +01:00
$formatted_number = mb_ereg_replace("([\.]{2,})", '', $formatted_number);
2023-02-07 09:45:02 +01:00
2022-01-11 08:19:44 +11:00
$formatted_number = preg_replace('/\s+/', '_', $formatted_number);
return $formatted_number;
}
2021-05-15 12:19:36 +10:00
2022-07-16 16:15:31 +10:00
public function translate_entity()
{
return ctrans('texts.item');
}
2023-02-08 07:09:47 +11:00
/**
* Model helper to send events for webhooks
2023-02-16 12:36:09 +11:00
*
* @param int $event_id
2023-02-08 07:09:47 +11:00
* @param string $additional_data optional includes
2023-02-16 12:36:09 +11:00
*
* @return void
2023-02-08 07:09:47 +11:00
*/
public function sendEvent(int $event_id, string $additional_data = ""): void
{
2023-02-07 09:45:02 +01:00
$subscriptions = Webhook::where('company_id', $this->company_id)
2023-02-08 07:09:47 +11:00
->where('event_id', $event_id)
->exists();
2023-02-07 09:45:02 +01:00
if ($subscriptions) {
2023-02-08 07:09:47 +11:00
WebhookHandler::dispatch($event_id, $this, $this->company, $additional_data);
2023-02-07 09:45:02 +01:00
}
}
}