2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-03-31 11:38:24 +02:00
|
|
|
use Auth;
|
2015-03-26 04:52:42 +01:00
|
|
|
use Eloquent;
|
2017-02-05 08:53:49 +01:00
|
|
|
use Laracasts\Presenter\PresentableTrait;
|
2015-03-26 04:52:42 +01:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Class Activity.
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
class Activity extends Eloquent
|
|
|
|
{
|
2017-02-05 08:53:49 +01:00
|
|
|
use PresentableTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $presenter = 'App\Ninja\Presenters\ActivityPresenter';
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2015-03-31 19:42:37 +02:00
|
|
|
public $timestamps = true;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $query
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function scopeScope($query)
|
|
|
|
{
|
|
|
|
return $query->whereAccountId(Auth::user()->account_id);
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function account()
|
|
|
|
{
|
2015-03-31 11:38:24 +02:00
|
|
|
return $this->belongsTo('App\Models\Account');
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function user()
|
|
|
|
{
|
2015-10-28 20:22:07 +01:00
|
|
|
return $this->belongsTo('App\Models\User')->withTrashed();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-10-28 20:22:07 +01:00
|
|
|
public function contact()
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-10-28 20:22:07 +01:00
|
|
|
return $this->belongsTo('App\Models\Contact')->withTrashed();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-10-28 20:22:07 +01:00
|
|
|
public function client()
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-10-28 20:22:07 +01:00
|
|
|
return $this->belongsTo('App\Models\Client')->withTrashed();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-10-28 20:22:07 +01:00
|
|
|
public function invoice()
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-10-28 20:22:07 +01:00
|
|
|
return $this->belongsTo('App\Models\Invoice')->withTrashed();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-10-28 20:22:07 +01:00
|
|
|
public function credit()
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-10-28 20:22:07 +01:00
|
|
|
return $this->belongsTo('App\Models\Credit')->withTrashed();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-10-28 20:22:07 +01:00
|
|
|
public function payment()
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-10-28 20:22:07 +01:00
|
|
|
return $this->belongsTo('App\Models\Payment')->withTrashed();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-05 18:30:29 +02:00
|
|
|
public function task()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Task')->withTrashed();
|
|
|
|
}
|
|
|
|
|
2016-10-20 22:17:37 +02:00
|
|
|
public function expense()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Expense')->withTrashed();
|
|
|
|
}
|
|
|
|
|
2016-08-17 09:55:50 +02:00
|
|
|
public function key()
|
|
|
|
{
|
|
|
|
return sprintf('%s-%s-%s', $this->activity_type_id, $this->client_id, $this->created_at->timestamp);
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-12-07 14:34:55 +01:00
|
|
|
public function getMessage()
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2015-12-07 14:34:55 +01:00
|
|
|
$activityTypeId = $this->activity_type_id;
|
|
|
|
$account = $this->account;
|
|
|
|
$client = $this->client;
|
|
|
|
$user = $this->user;
|
|
|
|
$invoice = $this->invoice;
|
|
|
|
$contactId = $this->contact_id;
|
2019-05-26 20:22:23 +02:00
|
|
|
$contact = $this->contact;
|
2015-12-07 14:34:55 +01:00
|
|
|
$payment = $this->payment;
|
|
|
|
$credit = $this->credit;
|
2016-10-20 22:17:37 +02:00
|
|
|
$expense = $this->expense;
|
2015-12-07 14:34:55 +01:00
|
|
|
$isSystem = $this->is_system;
|
2016-07-05 18:30:29 +02:00
|
|
|
$task = $this->task;
|
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
$data = [
|
2016-07-10 12:20:22 +02:00
|
|
|
'client' => $client ? link_to($client->getRoute(), $client->getDisplayName()) : null,
|
2017-08-04 15:39:11 +02:00
|
|
|
'user' => $isSystem ? '<i>' . trans('texts.system') . '</i>' : e($user->getDisplayName()),
|
2015-10-28 20:22:07 +01:00
|
|
|
'invoice' => $invoice ? link_to($invoice->getRoute(), $invoice->getDisplayName()) : null,
|
|
|
|
'quote' => $invoice ? link_to($invoice->getRoute(), $invoice->getDisplayName()) : null,
|
2019-05-26 20:22:23 +02:00
|
|
|
'contact' => $contactId ? link_to($client->getRoute(), $contact->getDisplayName()) : e($user->getDisplayName()),
|
2017-08-04 15:39:11 +02:00
|
|
|
'payment' => $payment ? e($payment->transaction_reference) : null,
|
2016-04-23 22:40:19 +02:00
|
|
|
'payment_amount' => $payment ? $account->formatMoney($payment->amount, $payment) : null,
|
2016-07-24 13:40:03 +02:00
|
|
|
'adjustment' => $this->adjustment ? $account->formatMoney($this->adjustment, $this) : null,
|
2015-12-07 14:34:55 +01:00
|
|
|
'credit' => $credit ? $account->formatMoney($credit->amount, $client) : null,
|
2016-07-05 18:30:29 +02:00
|
|
|
'task' => $task ? link_to($task->getRoute(), substr($task->description, 0, 30).'...') : null,
|
2016-10-20 22:17:37 +02:00
|
|
|
'expense' => $expense ? link_to($expense->getRoute(), substr($expense->public_notes, 0, 30).'...') : null,
|
2015-10-28 20:22:07 +01:00
|
|
|
];
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2015-10-28 20:22:07 +01:00
|
|
|
return trans("texts.activity_{$activityTypeId}", $data);
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
2018-02-27 21:52:57 +01:00
|
|
|
|
|
|
|
public function relatedEntityType()
|
|
|
|
{
|
|
|
|
switch ($this->activity_type_id) {
|
|
|
|
case ACTIVITY_TYPE_CREATE_CLIENT:
|
|
|
|
case ACTIVITY_TYPE_ARCHIVE_CLIENT:
|
|
|
|
case ACTIVITY_TYPE_DELETE_CLIENT:
|
|
|
|
case ACTIVITY_TYPE_RESTORE_CLIENT:
|
|
|
|
case ACTIVITY_TYPE_CREATE_CREDIT:
|
|
|
|
case ACTIVITY_TYPE_ARCHIVE_CREDIT:
|
|
|
|
case ACTIVITY_TYPE_DELETE_CREDIT:
|
|
|
|
case ACTIVITY_TYPE_RESTORE_CREDIT:
|
|
|
|
return ENTITY_CLIENT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACTIVITY_TYPE_CREATE_INVOICE:
|
|
|
|
case ACTIVITY_TYPE_UPDATE_INVOICE:
|
|
|
|
case ACTIVITY_TYPE_EMAIL_INVOICE:
|
|
|
|
case ACTIVITY_TYPE_VIEW_INVOICE:
|
|
|
|
case ACTIVITY_TYPE_ARCHIVE_INVOICE:
|
|
|
|
case ACTIVITY_TYPE_DELETE_INVOICE:
|
|
|
|
case ACTIVITY_TYPE_RESTORE_INVOICE:
|
|
|
|
return ENTITY_INVOICE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACTIVITY_TYPE_CREATE_PAYMENT:
|
|
|
|
case ACTIVITY_TYPE_ARCHIVE_PAYMENT:
|
|
|
|
case ACTIVITY_TYPE_DELETE_PAYMENT:
|
|
|
|
case ACTIVITY_TYPE_RESTORE_PAYMENT:
|
|
|
|
case ACTIVITY_TYPE_VOIDED_PAYMENT:
|
|
|
|
case ACTIVITY_TYPE_REFUNDED_PAYMENT:
|
|
|
|
case ACTIVITY_TYPE_FAILED_PAYMENT:
|
|
|
|
return ENTITY_PAYMENT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACTIVITY_TYPE_CREATE_QUOTE:
|
|
|
|
case ACTIVITY_TYPE_UPDATE_QUOTE:
|
|
|
|
case ACTIVITY_TYPE_EMAIL_QUOTE:
|
|
|
|
case ACTIVITY_TYPE_VIEW_QUOTE:
|
|
|
|
case ACTIVITY_TYPE_ARCHIVE_QUOTE:
|
|
|
|
case ACTIVITY_TYPE_DELETE_QUOTE:
|
|
|
|
case ACTIVITY_TYPE_RESTORE_QUOTE:
|
|
|
|
case ACTIVITY_TYPE_APPROVE_QUOTE:
|
|
|
|
return ENTITY_QUOTE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACTIVITY_TYPE_CREATE_VENDOR:
|
|
|
|
case ACTIVITY_TYPE_ARCHIVE_VENDOR:
|
|
|
|
case ACTIVITY_TYPE_DELETE_VENDOR:
|
|
|
|
case ACTIVITY_TYPE_RESTORE_VENDOR:
|
|
|
|
case ACTIVITY_TYPE_CREATE_EXPENSE:
|
|
|
|
case ACTIVITY_TYPE_ARCHIVE_EXPENSE:
|
|
|
|
case ACTIVITY_TYPE_DELETE_EXPENSE:
|
|
|
|
case ACTIVITY_TYPE_RESTORE_EXPENSE:
|
|
|
|
case ACTIVITY_TYPE_UPDATE_EXPENSE:
|
|
|
|
return ENTITY_EXPENSE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACTIVITY_TYPE_CREATE_TASK:
|
|
|
|
case ACTIVITY_TYPE_UPDATE_TASK:
|
|
|
|
case ACTIVITY_TYPE_ARCHIVE_TASK:
|
|
|
|
case ACTIVITY_TYPE_DELETE_TASK:
|
|
|
|
case ACTIVITY_TYPE_RESTORE_TASK:
|
|
|
|
return ENTITY_TASK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|