1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 08:02:40 +01:00
invoiceninja/app/Models/BaseModel.php

341 lines
11 KiB
PHP
Raw Normal View History

<?php
2019-05-11 05:32:07 +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
*/
namespace App\Models;
use App\Jobs\Entity\CreateRawPdf;
use App\Jobs\Util\WebhookHandler;
2023-10-26 04:57:44 +02:00
use App\Models\Traits\Excludable;
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-10-26 04:57:44 +02:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;
2023-10-26 04:57:44 +02:00
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
2021-02-13 01:20:15 +01:00
/**
* Class BaseModel
*
* @method scope() static
2023-08-02 11:47:26 +02:00
* @method company() static
2021-02-13 01:20:15 +01:00
* @package App\Models
2023-03-08 08:33:42 +01:00
* @property-read mixed $hashed_id
2023-04-26 16:11:31 +02:00
* @property string $number
* @property int $company_id
2023-07-24 10:57:02 +02:00
* @property int $id
2023-08-06 04:34:41 +02:00
* @property int $user_id
2023-08-06 07:24:13 +02:00
* @property int $assigned_user_id
2023-08-06 09:03:12 +02:00
* @method BaseModel service()
2023-04-26 16:11:31 +02:00
* @property \App\Models\Company $company
2023-10-26 04:57:44 +02:00
* @method static BaseModel find($value)
2023-08-06 09:03:12 +02:00
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel<static> company()
2023-04-26 23:29:40 +02:00
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel|Illuminate\Database\Eloquent\Relations\BelongsTo|\Awobaz\Compoships\Database\Eloquent\Relations\BelongsTo|\App\Models\Company company()
2023-07-24 10:57:02 +02:00
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel|Illuminate\Database\Eloquent\Relations\HasMany|BaseModel orderBy()
2023-08-06 09:35:19 +02:00
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel on(?string $connection = null)
2023-08-01 15:28:30 +02:00
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel exclude($columns)
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel with($value)
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel newModelQuery($query)
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel newQuery($query)
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel query()
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel whereId($query)
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel whereIn($query)
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel where($query)
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel count()
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel create($query)
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel insert($query)
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel orderBy($column, $direction)
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel invitations()
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel whereHas($query)
2023-08-06 09:35:19 +02:00
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel without($query)
2023-07-06 02:12:42 +02:00
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\InvoiceInvitation | \App\Models\CreditInvitation | \App\Models\QuoteInvitation | \App\Models\RecurringInvoiceInvitation> $invitations
* @property-read int|null $invitations_count
2023-04-26 23:29:40 +02:00
* @method int companyId()
2023-07-25 12:24:33 +02:00
* @method createInvitations()
2023-08-02 11:47:26 +02:00
* @method Builder scopeCompany(Builder $builder)
2023-08-11 06:18:58 +02:00
* @method static \Illuminate\Database\Eloquent\Builder<static> company()
2023-08-06 09:03:12 +02:00
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel|\Illuminate\Database\Query\Builder withTrashed(bool $withTrashed = true)
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel|\Illuminate\Database\Query\Builder onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|BaseModel|\Illuminate\Database\Query\Builder withoutTrashed()
2023-03-08 08:33:42 +01:00
* @mixin \Eloquent
2023-04-26 23:46:59 +02:00
* @mixin \Illuminate\Database\Eloquent\Builder
2023-10-26 04:57:44 +02:00
*
2023-08-11 06:18:58 +02:00
* @property \Illuminate\Support\Collection $tax_map
* @property array $total_tax_map
2021-02-13 01:20:15 +01:00
*/
class BaseModel extends Model
{
2019-07-30 00:28:38 +02:00
use MakesHash;
use UserSessionAttributes;
2020-10-01 12:49:47 +02:00
use HasFactory;
use Excludable;
2020-10-28 11:10:49 +01:00
2019-07-30 00:28:38 +02:00
protected $appends = [
'hashed_id',
2019-07-30 00:28:38 +02:00
];
protected $casts = [
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
];
protected $dateFormat = 'Y-m-d H:i:s.u';
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
{
2024-01-04 23:51:41 +01:00
return (new Carbon($value))->format('Y-m-d');
2020-08-15 00:03:29 +02:00
}
2023-09-06 15:49:19 +02:00
// public function __call($method, $params)
// {
// $entity = strtolower(class_basename($this));
2023-09-06 15:49:19 +02:00
// if ($entity) {
// $configPath = "modules.relations.$entity.$method";
2023-09-06 15:49:19 +02:00
// if (config()->has($configPath)) {
// $function = config()->get($configPath);
2023-09-06 15:49:19 +02:00
// return call_user_func_array([$this, $function[0]], $function[1]);
// }
// }
2023-09-06 15:49:19 +02:00
// return parent::__call($method, $params);
// }
2023-08-11 06:18:58 +02:00
/**
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
2023-08-02 11:47:26 +02:00
public function scopeCompany($query): \Illuminate\Database\Eloquent\Builder
2019-05-01 04:23:13 +02:00
{
2023-04-27 00:05:57 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
$query->where('company_id', $user->companyId());
2019-05-01 04:23:13 +02:00
return $query;
}
2023-08-02 11:47:26 +02:00
/**
* @deprecated version
*/
public function scopeScope($query)
{
2023-04-27 00:05:57 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
$query->where($this->getTable().'.company_id', '=', $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 12:11:36 +01:00
* @param $key The key of property
2023-01-17 01:00:12 +01:00
* @deprecated
*/
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};
// }
// }
2023-04-26 16:11:31 +02:00
// 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;
// //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
*
2023-04-26 16:11:31 +02:00
* @return ClientSettings.
*/
2023-04-26 16:11:31 +02:00
// public function getSettings()
// {
// return new ClientSettings($this->settings);
// }
/**
* Retrieve the model for a bound value.
*
2020-10-28 11:10:49 +01: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()
->where('id', $this->decodePrimaryKey($value))->firstOrFail();
}
/**
2020-10-28 11:10:49 +01:00
* @param string $extension
* @return string
*/
public function getFileName($extension = 'pdf')
{
return $this->numberFormatter().'.'.$extension;
}
2023-11-16 01:36:14 +01:00
public function getDeliveryNoteName($extension = 'pdf')
{
2024-01-14 05:05:00 +01:00
2023-11-16 01:36:14 +01:00
$number = ctrans("texts.delivery_note"). "_" . $this->numberFormatter().'.'.$extension;
$formatted_number = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $number);
$formatted_number = mb_ereg_replace("([\.]{2,})", '', $formatted_number);
$formatted_number = preg_replace('/\s+/', '_', $formatted_number);
return \Illuminate\Support\Str::ascii($formatted_number);
}
2023-10-26 04:57:44 +02:00
/**
* @param string $extension
* @return string
*/
2023-08-17 01:24:36 +02:00
public function getEFileName($extension = 'pdf')
{
return ctrans("texts.e_invoice"). "_" . $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-10 22:19:44 +01:00
$formatted_number = preg_replace('/\s+/', '_', $formatted_number);
return \Illuminate\Support\Str::ascii($formatted_number);
}
2021-05-15 04:19:36 +02:00
2022-07-16 08:15:31 +02:00
public function translate_entity()
{
return ctrans('texts.item');
}
2023-02-07 21:09:47 +01:00
/**
* Model helper to send events for webhooks
2023-02-16 02:36:09 +01:00
*
* @param int $event_id
2023-02-07 21:09:47 +01:00
* @param string $additional_data optional includes
2023-02-16 02:36:09 +01:00
*
* @return void
2023-02-07 21:09:47 +01: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-07 21:09:47 +01:00
->where('event_id', $event_id)
->exists();
2024-01-14 05:05:00 +01:00
2023-02-07 09:45:02 +01:00
if ($subscriptions) {
2023-11-10 02:36:27 +01:00
WebhookHandler::dispatch($event_id, $this->withoutRelations(), $this->company, $additional_data);
2023-02-07 09:45:02 +01:00
}
}
/**
* Returns the base64 encoded PDF string of the entity
2023-08-16 11:55:35 +02:00
* @deprecated - unused implementation
*/
public function fullscreenPdfViewer($invitation = null): string
{
if (! $invitation) {
if ($this->invitations()->exists()) {
$invitation = $this->invitations()->first();
} else {
$this->service()->createInvitations();
$invitation = $this->invitations()->first();
}
}
if (! $invitation) {
throw new \Exception('Hard fail, could not create an invitation.');
}
2024-01-14 05:05:00 +01:00
2023-10-26 03:25:56 +02:00
return "data:application/pdf;base64,".base64_encode((new CreateRawPdf($invitation))->handle());
}
2024-02-13 05:25:18 +01:00
/**
* Takes a entity prop as first argument
* along with an array of variables and performs
* a string replace on the prop.
*
* @param string $field
* @param array $variables
* @return string
*/
public function parseHtmlVariables(string $field, array $variables): string
{
2024-02-13 05:25:18 +01:00
if(!$this->{$field}) {
return '';
2024-02-13 05:25:18 +01:00
}
$section = strtr($this->{$field}, $variables['labels']);
return strtr($section, $variables['values']);
}
}