1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Models/Activity.php

493 lines
15 KiB
PHP
Raw Normal View History

2019-04-20 03:54:11 +02:00
<?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
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. 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
*/
2019-04-20 03:54:11 +02:00
namespace App\Models;
2023-06-24 06:57:57 +02:00
use App\Utils\Number;
2020-08-12 02:01:27 +02:00
use App\Utils\Traits\MakesHash;
2019-04-20 03:54:11 +02:00
2023-03-08 08:33:42 +01:00
/**
* App\Models\Activity
*
* @property int $id
* @property int|null $user_id
* @property int $company_id
* @property int|null $client_id
* @property int|null $client_contact_id
* @property int|null $account_id
* @property int|null $project_id
* @property int|null $vendor_id
* @property int|null $payment_id
* @property int|null $invoice_id
* @property int|null $credit_id
* @property int|null $invitation_id
* @property int|null $task_id
* @property int|null $expense_id
* @property int|null $activity_type_id
* @property string $ip
* @property bool $is_system
* @property string $notes
* @property int|null $created_at
* @property int|null $updated_at
* @property int|null $token_id
* @property int|null $quote_id
* @property int|null $subscription_id
* @property int|null $recurring_invoice_id
* @property int|null $recurring_expense_id
* @property int|null $recurring_quote_id
* @property int|null $purchase_order_id
* @property int|null $vendor_contact_id
* @property-read \App\Models\Backup|null $backup
* @property-read \App\Models\Client|null $client
* @property-read \App\Models\Company $company
* @property-read \App\Models\ClientContact|null $contact
* @property-read \App\Models\Credit|null $credit
* @property-read \App\Models\Expense|null $expense
* @property-read mixed $hashed_id
* @property-read \App\Models\Backup|null $history
* @property-read \App\Models\Invoice|null $invoice
* @property-read \App\Models\Payment|null $payment
* @property-read \App\Models\PurchaseOrder|null $purchase_order
* @property-read \App\Models\Quote|null $quote
* @property-read \App\Models\RecurringExpense|null $recurring_expense
* @property-read \App\Models\RecurringInvoice|null $recurring_invoice
* @property-read \App\Models\Subscription|null $subscription
* @property-read \App\Models\Task|null $task
* @property-read \App\Models\User|null $user
* @property-read \App\Models\Vendor|null $vendor
* @property-read \App\Models\VendorContact|null $vendor_contact
* @method static \Illuminate\Database\Eloquent\Builder|StaticModel company()
* @method static \Illuminate\Database\Eloquent\Builder|StaticModel exclude($columns)
2023-08-01 14:36:04 +02:00
2023-03-08 08:33:42 +01:00
* @mixin \Eloquent
*/
class Activity extends StaticModel
2019-04-20 03:54:11 +02:00
{
2020-08-12 02:01:27 +02:00
use MakesHash;
2024-01-14 05:05:00 +01:00
public const CREATE_CLIENT = 1; //
2024-01-14 05:05:00 +01:00
public const ARCHIVE_CLIENT = 2; //
2024-01-14 05:05:00 +01:00
public const DELETE_CLIENT = 3; //
2024-01-14 05:05:00 +01:00
public const CREATE_INVOICE = 4; //
2024-01-14 05:05:00 +01:00
public const UPDATE_INVOICE = 5; //
2024-01-14 05:05:00 +01:00
public const EMAIL_INVOICE = 6; //
2024-01-14 05:05:00 +01:00
public const VIEW_INVOICE = 7; //
2024-01-14 05:05:00 +01:00
public const ARCHIVE_INVOICE = 8; //
2024-01-14 05:05:00 +01:00
public const DELETE_INVOICE = 9; //
2024-01-14 05:05:00 +01:00
public const CREATE_PAYMENT = 10; //
2024-01-14 05:05:00 +01:00
public const UPDATE_PAYMENT = 11; //
2024-01-14 05:05:00 +01:00
public const ARCHIVE_PAYMENT = 12; //
2024-01-14 05:05:00 +01:00
public const DELETE_PAYMENT = 13; //
2024-01-14 05:05:00 +01:00
public const CREATE_CREDIT = 14; //
2024-01-14 05:05:00 +01:00
public const UPDATE_CREDIT = 15; //
2024-01-14 05:05:00 +01:00
public const ARCHIVE_CREDIT = 16; //
2024-01-14 05:05:00 +01:00
public const DELETE_CREDIT = 17; //
2024-01-14 05:05:00 +01:00
public const CREATE_QUOTE = 18; //
2024-01-14 05:05:00 +01:00
public const UPDATE_QUOTE = 19; //
2024-01-14 05:05:00 +01:00
public const EMAIL_QUOTE = 20; //
2024-01-14 05:05:00 +01:00
public const VIEW_QUOTE = 21; //
2024-01-14 05:05:00 +01:00
public const ARCHIVE_QUOTE = 22; //
2024-01-14 05:05:00 +01:00
public const DELETE_QUOTE = 23; //
2024-01-14 05:05:00 +01:00
public const RESTORE_QUOTE = 24; //
2024-01-14 05:05:00 +01:00
public const RESTORE_INVOICE = 25; //
2024-01-14 05:05:00 +01:00
public const RESTORE_CLIENT = 26; //
2024-01-14 05:05:00 +01:00
public const RESTORE_PAYMENT = 27; //
2024-01-14 05:05:00 +01:00
public const RESTORE_CREDIT = 28; //
2024-01-14 05:05:00 +01:00
public const APPROVE_QUOTE = 29; //
2024-01-14 05:05:00 +01:00
public const CREATE_VENDOR = 30; //
2024-01-14 05:05:00 +01:00
public const ARCHIVE_VENDOR = 31; //
2024-01-14 05:05:00 +01:00
public const DELETE_VENDOR = 32; //
2024-01-14 05:05:00 +01:00
public const RESTORE_VENDOR = 33; //
2024-01-14 05:05:00 +01:00
public const CREATE_EXPENSE = 34; //
2024-01-14 05:05:00 +01:00
public const ARCHIVE_EXPENSE = 35; //
2024-01-14 05:05:00 +01:00
public const DELETE_EXPENSE = 36; //
2024-01-14 05:05:00 +01:00
public const RESTORE_EXPENSE = 37; //
2024-01-14 05:05:00 +01:00
public const VOIDED_PAYMENT = 39; //
2024-01-14 05:05:00 +01:00
public const REFUNDED_PAYMENT = 40; //
2024-01-14 05:05:00 +01:00
public const FAILED_PAYMENT = 41;
2024-01-14 05:05:00 +01:00
public const CREATE_TASK = 42; //
2024-01-14 05:05:00 +01:00
public const UPDATE_TASK = 43; //
2024-01-14 05:05:00 +01:00
public const ARCHIVE_TASK = 44; //
2024-01-14 05:05:00 +01:00
public const DELETE_TASK = 45; //
2024-01-14 05:05:00 +01:00
public const RESTORE_TASK = 46; //
2024-01-14 05:05:00 +01:00
public const UPDATE_EXPENSE = 47; //
2024-01-14 05:05:00 +01:00
public const CREATE_USER = 48;
2024-01-14 05:05:00 +01:00
public const UPDATE_USER = 49;
2024-01-14 05:05:00 +01:00
public const ARCHIVE_USER = 50;
2024-01-14 05:05:00 +01:00
public const DELETE_USER = 51;
2024-01-14 05:05:00 +01:00
public const RESTORE_USER = 52;
2024-01-14 05:05:00 +01:00
public const MARK_SENT_INVOICE = 53; // not needed?
2024-01-14 05:05:00 +01:00
public const PAID_INVOICE = 54; //
2024-01-14 05:05:00 +01:00
public const EMAIL_INVOICE_FAILED = 57;
2024-01-14 05:05:00 +01:00
public const REVERSED_INVOICE = 58; //
2024-01-14 05:05:00 +01:00
public const CANCELLED_INVOICE = 59; //
2024-01-14 05:05:00 +01:00
public const VIEW_CREDIT = 60; //
2024-01-14 05:05:00 +01:00
public const UPDATE_CLIENT = 61; //
2024-01-14 05:05:00 +01:00
public const UPDATE_VENDOR = 62; //
2020-08-12 04:02:21 +02:00
2024-01-14 05:05:00 +01:00
public const INVOICE_REMINDER1_SENT = 63;
2024-01-14 05:05:00 +01:00
public const INVOICE_REMINDER2_SENT = 64;
2024-01-14 05:05:00 +01:00
public const INVOICE_REMINDER3_SENT = 65;
2024-01-14 05:05:00 +01:00
public const INVOICE_REMINDER_ENDLESS_SENT = 66;
2020-11-09 03:39:42 +01:00
2024-01-14 05:05:00 +01:00
public const CREATE_SUBSCRIPTION = 80;
2024-01-14 05:05:00 +01:00
public const UPDATE_SUBSCRIPTION = 81;
2024-01-14 05:05:00 +01:00
public const ARCHIVE_SUBSCRIPTION = 82;
2024-01-14 05:05:00 +01:00
public const DELETE_SUBSCRIPTION = 83;
2024-01-14 05:05:00 +01:00
public const RESTORE_SUBSCRIPTION = 84;
2024-01-14 05:05:00 +01:00
public const CREATE_RECURRING_INVOICE = 100;
2024-01-14 05:05:00 +01:00
public const UPDATE_RECURRING_INVOICE = 101;
2024-01-14 05:05:00 +01:00
public const ARCHIVE_RECURRING_INVOICE = 102;
2024-01-14 05:05:00 +01:00
public const DELETE_RECURRING_INVOICE = 103;
2024-01-14 05:05:00 +01:00
public const RESTORE_RECURRING_INVOICE = 104;
2024-01-14 05:05:00 +01:00
public const CREATE_RECURRING_QUOTE = 110;
2024-01-14 05:05:00 +01:00
public const UPDATE_RECURRING_QUOTE = 111;
2024-01-14 05:05:00 +01:00
public const ARCHIVE_RECURRING_QUOTE = 112;
2024-01-14 05:05:00 +01:00
public const DELETE_RECURRING_QUOTE = 113;
2024-01-14 05:05:00 +01:00
public const RESTORE_RECURRING_QUOTE = 114;
2021-08-24 07:55:14 +02:00
2024-01-14 05:05:00 +01:00
public const CREATE_RECURRING_EXPENSE = 120;
2024-01-14 05:05:00 +01:00
public const UPDATE_RECURRING_EXPENSE = 121;
2024-01-14 05:05:00 +01:00
public const ARCHIVE_RECURRING_EXPENSE = 122;
2024-01-14 05:05:00 +01:00
public const DELETE_RECURRING_EXPENSE = 123;
2024-01-14 05:05:00 +01:00
public const RESTORE_RECURRING_EXPENSE = 124;
2021-08-24 07:55:14 +02:00
2024-01-14 05:05:00 +01:00
public const CREATE_PURCHASE_ORDER = 130;
2024-01-14 05:05:00 +01:00
public const UPDATE_PURCHASE_ORDER = 131;
2024-01-14 05:05:00 +01:00
public const ARCHIVE_PURCHASE_ORDER = 132;
2024-01-14 05:05:00 +01:00
public const DELETE_PURCHASE_ORDER = 133;
2024-01-14 05:05:00 +01:00
public const RESTORE_PURCHASE_ORDER = 134;
2024-01-14 05:05:00 +01:00
public const EMAIL_PURCHASE_ORDER = 135;
2024-01-14 05:05:00 +01:00
public const VIEW_PURCHASE_ORDER = 136;
2024-01-14 05:05:00 +01:00
public const ACCEPT_PURCHASE_ORDER = 137;
2024-01-14 05:05:00 +01:00
public const PAYMENT_EMAILED = 138;
2023-03-17 07:36:49 +01:00
2024-01-14 05:05:00 +01:00
public const VENDOR_NOTIFICATION_EMAIL = 139;
2023-11-24 06:10:10 +01:00
2019-09-17 12:27:48 +02:00
protected $casts = [
'is_system' => 'boolean',
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
2019-09-17 12:27:48 +02:00
];
2019-04-20 14:09:40 +02:00
2021-05-30 10:03:31 +02:00
protected $appends = [
'hashed_id',
];
2021-10-08 06:00:17 +02:00
protected $with = [
'backup',
];
2023-07-25 12:24:33 +02:00
2023-08-03 05:41:04 +02:00
public function getHashedIdAttribute(): string
2021-05-30 10:03:31 +02:00
{
return $this->encodePrimaryKey($this->id);
}
2023-07-25 12:24:33 +02:00
public function getEntityType()
{
return self::class;
}
2023-08-03 05:41:04 +02:00
public function backup(): \Illuminate\Database\Eloquent\Relations\HasOne
2019-04-21 14:24:26 +02:00
{
return $this->hasOne(Backup::class);
2019-04-21 14:24:26 +02:00
}
2019-04-22 01:15:07 +02:00
2023-08-03 05:41:04 +02:00
public function history(): \Illuminate\Database\Eloquent\Relations\HasOne
{
return $this->hasOne(Backup::class);
}
2023-08-03 05:41:04 +02:00
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2019-04-20 14:09:40 +02:00
{
return $this->belongsTo(User::class)->withTrashed();
}
2023-08-03 05:41:04 +02:00
public function contact(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2019-04-20 14:09:40 +02:00
{
2023-06-24 02:09:14 +02:00
return $this->belongsTo(ClientContact::class, 'client_contact_id', 'id')->withTrashed();
2019-04-20 14:09:40 +02:00
}
2023-08-03 05:41:04 +02:00
public function client(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2019-04-20 14:09:40 +02:00
{
return $this->belongsTo(Client::class)->withTrashed();
}
2023-08-03 05:41:04 +02:00
public function invoice(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2019-04-20 14:09:40 +02:00
{
return $this->belongsTo(Invoice::class)->withTrashed();
}
2023-07-25 12:24:33 +02:00
2023-08-03 05:41:04 +02:00
public function vendor(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2022-08-31 04:05:15 +02:00
{
return $this->belongsTo(Vendor::class)->withTrashed();
}
2023-07-25 12:24:33 +02:00
2023-08-03 05:41:04 +02:00
public function recurring_invoice(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2022-05-27 10:25:32 +02:00
{
return $this->belongsTo(RecurringInvoice::class)->withTrashed();
}
2023-08-03 05:41:04 +02:00
public function credit(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2022-03-05 07:51:04 +01:00
{
return $this->belongsTo(Credit::class)->withTrashed();
}
2023-08-03 05:41:04 +02:00
public function quote(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2021-02-08 21:13:16 +01:00
{
return $this->belongsTo(Quote::class)->withTrashed();
}
2022-11-24 01:38:57 +01:00
2023-08-03 05:41:04 +02:00
public function subscription(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2022-11-24 01:38:57 +01:00
{
return $this->belongsTo(Subscription::class)->withTrashed();
}
2021-02-08 21:13:16 +01:00
2023-08-03 05:41:04 +02:00
public function payment(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2019-04-20 14:09:40 +02:00
{
return $this->belongsTo(Payment::class)->withTrashed();
}
2023-08-03 05:41:04 +02:00
public function expense(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2022-05-27 10:25:32 +02:00
{
return $this->belongsTo(Expense::class)->withTrashed();
}
2023-12-03 21:58:59 +01:00
public function account(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(Account::class);
}
2023-07-25 12:24:33 +02:00
2023-08-03 05:41:04 +02:00
public function recurring_expense(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2023-01-05 12:09:25 +01:00
{
return $this->belongsTo(RecurringExpense::class)->withTrashed();
}
2023-08-03 05:41:04 +02:00
public function purchase_order(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(PurchaseOrder::class)->withTrashed();
}
2023-08-03 05:41:04 +02:00
public function vendor_contact(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(VendorContact::class)->withTrashed();
}
2023-08-03 05:41:04 +02:00
public function task(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2022-05-27 10:25:32 +02:00
{
return $this->belongsTo(Task::class)->withTrashed();
}
2019-04-20 14:09:40 +02:00
2023-08-03 05:41:04 +02:00
public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo
2019-09-27 06:31:13 +02:00
{
return $this->belongsTo(Company::class);
2019-09-27 06:31:13 +02:00
}
2023-06-24 03:30:44 +02:00
public function activity_string()
{
$intersect = [
':invoice',
':client',
':contact',
':user',
':vendor',
':quote',
':credit',
':payment',
':task',
':expense',
':purchase_order',
':subscription',
':recurring_invoice',
':recurring_expense',
':amount',
':balance',
':number',
':payment_amount',
2023-10-26 04:57:44 +02:00
':gateway',
':adjustment'
2023-06-24 03:30:44 +02:00
];
2023-10-26 04:57:44 +02:00
$found_variables = array_intersect(explode(" ", trans("texts.activity_{$this->activity_type_id}")), $intersect);
2023-06-24 03:30:44 +02:00
2024-01-14 05:05:00 +01:00
if($this->activity_type_id == 10 && $this->client_contact_id && !$this->token_id) {
$found_variables = array_intersect(explode(" ", trans("texts.activity_10_online")), $intersect);
2024-01-14 05:05:00 +01:00
}
2024-01-14 05:05:00 +01:00
if($this->activity_type_id == 54 && !$this->token_id) {
array_push($found_variables, ':contact');
2024-01-14 05:05:00 +01:00
}
2023-06-24 03:30:44 +02:00
$replacements = [];
2023-10-26 04:57:44 +02:00
foreach($found_variables as $var) {
2023-06-24 15:02:38 +02:00
$replacements = array_merge($replacements, $this->matchVar($var));
2023-10-26 04:57:44 +02:00
}
2023-06-24 03:30:44 +02:00
2023-10-26 04:57:44 +02:00
if($this->client) {
2023-06-24 15:02:38 +02:00
$replacements['client'] = ['label' => $this?->client?->present()->name() ?? '', 'hashed_id' => $this->client->hashed_id ?? ''];
2023-10-26 04:57:44 +02:00
}
2024-01-14 05:05:00 +01:00
2023-10-26 04:57:44 +02:00
if($this->vendor) {
2023-06-24 15:02:38 +02:00
$replacements['vendor'] = ['label' => $this?->vendor?->present()->name() ?? '', 'hashed_id' => $this->vendor->hashed_id ?? ''];
2023-10-26 04:57:44 +02:00
}
2023-06-24 15:02:38 +02:00
2023-10-26 04:57:44 +02:00
if($this->activity_type_id == 4 && $this->recurring_invoice) {
$replacements['recurring_invoice'] = ['label' => $this?->recurring_invoice?->number ?? '', 'hashed_id' => $this->recurring_invoice->hashed_id ?? ''];
2023-10-26 04:57:44 +02:00
}
2023-06-24 15:02:38 +02:00
$replacements['activity_type_id'] = $this->activity_type_id;
$replacements['id'] = $this->id;
$replacements['hashed_id'] = $this->hashed_id;
$replacements['notes'] = $this->notes ?? '';
$replacements['created_at'] = $this->created_at ?? '';
$replacements['ip'] = $this->ip ?? '';
2023-06-24 06:57:57 +02:00
return $replacements;
2023-06-24 03:30:44 +02:00
}
private function matchVar(string $variable)
{
2023-06-24 15:02:38 +02:00
$system = ctrans('texts.system');
2024-01-14 05:05:00 +01:00
2023-08-02 11:47:26 +02:00
$translation = '';
2024-01-14 05:05:00 +01:00
2023-06-26 14:50:26 +02:00
match($variable) {
':invoice' => $translation = [substr($variable, 1) => [ 'label' => $this?->invoice?->number ?? '', 'hashed_id' => $this->invoice?->hashed_id ?? '']],
2023-06-24 15:02:38 +02:00
':user' => $translation = [substr($variable, 1) => [ 'label' => $this?->user?->present()->name() ?? $system, 'hashed_id' => $this->user->hashed_id ?? '']],
2023-06-24 06:57:57 +02:00
':quote' => $translation = [substr($variable, 1) => [ 'label' => $this?->quote?->number ?? '', 'hashed_id' => $this->quote->hashed_id ?? '']],
':credit' => $translation = [substr($variable, 1) => [ 'label' => $this?->credit?->number ?? '', 'hashed_id' => $this->credit->hashed_id ?? '']],
':payment' => $translation = [substr($variable, 1) => [ 'label' => $this?->payment?->number ?? '', 'hashed_id' => $this->payment->hashed_id ?? '']],
':task' => $translation = [substr($variable, 1) => [ 'label' => $this?->task?->number ?? '', 'hashed_id' => $this->task->hashed_id ?? '']],
':expense' => $translation = [substr($variable, 1) => [ 'label' => $this?->expense?->number ?? '', 'hashed_id' => $this->expense->hashed_id ?? '']],
':purchase_order' => $translation = [substr($variable, 1) => [ 'label' => $this?->purchase_order?->number ?? '', 'hashed_id' => $this->purchase_order->hashed_id ?? '']],
':subscription' => $translation = [substr($variable, 1) => [ 'label' => $this?->subscription?->number ?? '', 'hashed_id' => $this->subscription->hashed_id ?? '' ]],
2024-01-14 05:05:00 +01:00
':recurring_invoice' => $translation = [substr($variable, 1) => [ 'label' => $this?->recurring_invoice?->number ?? '', 'hashed_id' => $this->recurring_invoice->hashed_id ?? '']],
':recurring_expense' => $translation = [substr($variable, 1) => [ 'label' => $this?->recurring_expense?->number ?? '', 'hashed_id' => $this->recurring_expense->hashed_id ?? '']],
':payment_amount' => $translation = [substr($variable, 1) => [ 'label' => Number::formatMoney($this?->payment?->amount, $this?->payment?->client ?? $this->company) ?? '', 'hashed_id' => '']],
':adjustment' => $translation = [substr($variable, 1) => [ 'label' => Number::formatMoney($this?->payment?->refunded, $this?->payment?->client ?? $this->company) ?? '', 'hashed_id' => '']],
2023-06-24 06:57:57 +02:00
':ip' => $translation = [ 'ip' => $this->ip ?? ''],
2023-06-26 14:50:26 +02:00
':contact' => $translation = $this->resolveContact(),
2023-06-24 15:02:38 +02:00
default => $translation = [],
2023-06-24 03:30:44 +02:00
};
return $translation;
}
2024-01-14 05:05:00 +01:00
private function resolveContact(): array
2023-06-24 06:57:57 +02:00
{
2023-06-24 15:02:38 +02:00
$contact = $this->contact ? $this->contact : $this->vendor_contact;
$entity = $this->contact ? $this->client : $this->vendor;
$contact_entity = $this->contact ? 'clients' : 'vendors';
2023-06-24 06:57:57 +02:00
if(!$contact) {
return [];
}
2024-01-14 05:05:00 +01:00
2023-06-24 15:02:38 +02:00
return ['contact' => [ 'label' => $contact?->present()->name() ?? '', 'hashed_id' => $entity->hashed_id ?? '', 'contact_entity' => $contact_entity]];
2023-06-24 06:57:57 +02:00
}
2023-02-16 02:36:09 +01:00
}