'boolean', 'updated_at' => 'timestamp', 'created_at' => 'timestamp', 'deleted_at' => 'timestamp', ]; protected $appends = [ 'hashed_id', ]; protected $with = [ 'backup', ]; public function getHashedIdAttribute(): string { return $this->encodePrimaryKey($this->id); } public function getEntityType() { return self::class; } public function backup(): \Illuminate\Database\Eloquent\Relations\HasOne { return $this->hasOne(Backup::class); } public function history(): \Illuminate\Database\Eloquent\Relations\HasOne { return $this->hasOne(Backup::class); } public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(User::class)->withTrashed(); } public function contact(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(ClientContact::class, 'client_contact_id', 'id')->withTrashed(); } public function client(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Client::class)->withTrashed(); } public function invoice(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Invoice::class)->withTrashed(); } public function vendor(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Vendor::class)->withTrashed(); } public function recurring_invoice(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(RecurringInvoice::class)->withTrashed(); } public function credit(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Credit::class)->withTrashed(); } public function quote(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Quote::class)->withTrashed(); } public function subscription(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Subscription::class)->withTrashed(); } public function payment(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Payment::class)->withTrashed(); } public function expense(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Expense::class)->withTrashed(); } public function account(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Account::class); } public function recurring_expense(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(RecurringExpense::class)->withTrashed(); } public function purchase_order(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(PurchaseOrder::class)->withTrashed(); } public function vendor_contact(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(VendorContact::class)->withTrashed(); } public function task(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Task::class)->withTrashed(); } public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(Company::class); } 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', ':gateway', ':adjustment' ]; $found_variables = array_intersect(explode(" ", trans("texts.activity_{$this->activity_type_id}")), $intersect); if($this->activity_type_id == 10 && $this->client_contact_id && !$this->token_id) { $found_variables = array_intersect(explode(" ", trans("texts.activity_10_online")), $intersect); } if($this->activity_type_id == 54 && !$this->token_id) { array_push($found_variables, ':contact'); } $replacements = []; foreach($found_variables as $var) { $replacements = array_merge($replacements, $this->matchVar($var)); } if($this->client) { $replacements['client'] = ['label' => $this->client?->present()->name() ?? '', 'hashed_id' => $this->client->hashed_id ?? '']; } if($this->vendor) { $replacements['vendor'] = ['label' => $this->vendor?->present()->name() ?? '', 'hashed_id' => $this->vendor->hashed_id ?? '']; } if($this->activity_type_id == 4 && $this->recurring_invoice) { $replacements['recurring_invoice'] = ['label' => $this?->recurring_invoice?->number ?? '', 'hashed_id' => $this->recurring_invoice->hashed_id ?? '']; } $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 ?? ''; if($this->activity_type_id == 141) $replacements = $this->harvestNoteEntities($replacements); return $replacements; } private function harvestNoteEntities(array $replacements): array { $entities = [ ':invoice', ':quote', ':credit', ':payment', ':task', ':expense', ':purchase_order', ':recurring_invoice', ':recurring_expense', ':client', ]; foreach($entities as $entity) { $entity_key = substr($entity, 1); if($this?->{$entity_key}) $replacements = array_merge($replacements, $this->matchVar($entity)); } return $replacements; } private function matchVar(string $variable) { $system = ctrans('texts.system'); $translation = ''; match($variable) { ':invoice' => $translation = [substr($variable, 1) => [ 'label' => $this?->invoice?->number ?? '', 'hashed_id' => $this->invoice?->hashed_id ?? '']], ':user' => $translation = [substr($variable, 1) => [ 'label' => $this?->user?->present()->name() ?? $system, 'hashed_id' => $this->user->hashed_id ?? '']], ':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 ?? '' ]], ':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' => '']], ':ip' => $translation = [ 'ip' => $this->ip ?? ''], ':contact' => $translation = $this->resolveContact(), ':notes' => $translation = [ 'notes' => $this->notes ?? ''], default => $translation = [], }; return $translation; } private function resolveContact(): array { $contact = $this->contact ? $this->contact : $this->vendor_contact; $entity = $this->contact ? $this->client : $this->vendor; $contact_entity = $this->contact ? 'clients' : 'vendors'; if(!$contact) { return []; } return ['contact' => [ 'label' => $contact?->present()->name() ?? '', 'hashed_id' => $entity->hashed_id ?? '', 'contact_entity' => $contact_entity]]; } }