akaunting/app/Models/Banking/Transaction.php

671 lines
20 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Models\Banking;
2019-11-16 10:21:14 +03:00
use App\Abstracts\Model;
use App\Models\Common\Media as MediaModel;
2020-12-26 16:13:34 +03:00
use App\Scopes\Transaction as Scope;
2019-11-16 10:21:14 +03:00
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Media;
use App\Traits\Recurring;
2020-08-26 15:14:16 +03:00
use App\Traits\Transactions;
2019-11-16 10:21:14 +03:00
use Bkwld\Cloner\Cloneable;
2022-06-01 10:15:55 +03:00
use Illuminate\Database\Eloquent\Builder;
2020-10-14 17:07:59 +03:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2020-08-26 15:14:16 +03:00
use Illuminate\Support\Str;
2017-09-14 22:21:00 +03:00
class Transaction extends Model
{
2020-10-14 17:07:59 +03:00
use Cloneable, Currencies, DateTime, HasFactory, Media, Recurring, Transactions;
2019-11-16 10:21:14 +03:00
2022-06-01 10:15:55 +03:00
public const INCOME_TYPE = 'income';
public const INCOME_TRANSFER_TYPE = 'income-transfer';
2022-06-01 10:15:55 +03:00
public const INCOME_SPLIT_TYPE = 'income-split';
public const INCOME_RECURRING_TYPE = 'income-recurring';
public const EXPENSE_TYPE = 'expense';
public const EXPENSE_TRANSFER_TYPE = 'expense-transfer';
2022-06-01 10:15:55 +03:00
public const EXPENSE_SPLIT_TYPE = 'expense-split';
public const EXPENSE_RECURRING_TYPE = 'expense-recurring';
2019-11-16 10:21:14 +03:00
protected $table = 'transactions';
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
2021-06-17 10:59:07 +03:00
protected $fillable = [
'company_id',
'type',
2022-06-01 10:15:55 +03:00
'number',
2021-06-17 10:59:07 +03:00
'account_id',
'paid_at',
'amount',
'currency_code',
'currency_rate',
'document_id',
'contact_id',
'description',
'category_id',
'payment_method',
'reference',
'parent_id',
2022-06-01 10:15:55 +03:00
'split_id',
2021-09-07 10:33:34 +03:00
'created_from',
2021-06-17 10:59:07 +03:00
'created_by',
];
2019-11-16 10:21:14 +03:00
2020-11-13 15:15:27 +03:00
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
2023-03-16 16:36:13 +03:00
'paid_at' => 'datetime',
'amount' => 'double',
'currency_rate' => 'double',
'deleted_at' => 'datetime',
2020-11-13 15:15:27 +03:00
];
2019-11-16 10:21:14 +03:00
/**
* Sortable columns.
*
* @var array
*/
2022-07-14 01:27:36 +03:00
public $sortable = ['type', 'number', 'paid_at', 'amount', 'category.name', 'account.name', 'customer.name', 'invoice.document_number'];
2019-11-16 10:21:14 +03:00
/**
* Clonable relationships.
*
* @var array
*/
public $cloneable_relations = ['recurring'];
2020-12-26 16:13:34 +03:00
/**
2021-01-19 17:27:19 +03:00
* The "booted" method of the model.
2020-12-26 16:13:34 +03:00
*
* @return void
*/
2021-01-19 17:27:19 +03:00
protected static function booted()
2020-12-26 16:13:34 +03:00
{
static::addGlobalScope(new Scope);
}
2019-11-16 10:21:14 +03:00
public function account()
{
return $this->belongsTo('App\Models\Banking\Account')->withDefault(['name' => trans('general.na')]);
}
public function bill()
{
2022-06-10 19:26:32 +03:00
return $this->belongsTo('App\Models\Document\Document', 'document_id')->withoutGlobalScope('App\Scopes\Document');
2019-11-16 10:21:14 +03:00
}
public function category()
{
2022-07-22 00:52:06 +03:00
return $this->belongsTo('App\Models\Setting\Category')->withoutGlobalScope('App\Scopes\Category')->withDefault(['name' => trans('general.na')]);
2019-11-16 10:21:14 +03:00
}
2022-06-01 10:15:55 +03:00
public function children()
{
return $this->hasMany('App\Models\Banking\Transaction', 'parent_id');
}
2019-11-16 10:21:14 +03:00
public function contact()
{
return $this->belongsTo('App\Models\Common\Contact')->withDefault(['name' => trans('general.na')]);
}
public function currency()
{
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
}
public function invoice()
{
2022-06-10 19:26:32 +03:00
return $this->belongsTo('App\Models\Document\Document', 'document_id')->withoutGlobalScope('App\Scopes\Document');
2020-12-24 01:28:38 +03:00
}
public function document()
{
2022-06-10 19:26:32 +03:00
return $this->belongsTo('App\Models\Document\Document', 'document_id')->withoutGlobalScope('App\Scopes\Document');
2019-11-16 10:21:14 +03:00
}
2022-06-01 10:15:55 +03:00
public function parent()
{
2022-06-05 10:15:59 +03:00
return $this->belongsTo('App\Models\Banking\Transaction', 'parent_id')->isRecurring();
2022-06-01 10:15:55 +03:00
}
2019-11-16 10:21:14 +03:00
public function recurring()
{
return $this->morphOne('App\Models\Common\Recurring', 'recurable');
}
public function transfer()
{
if ($this->type == static::INCOME_TRANSFER_TYPE) {
return $this->belongsTo('App\Models\Banking\Transfer', 'id', 'income_transaction_id');
}
if ($this->type == static::EXPENSE_TRANSFER_TYPE) {
return $this->belongsTo('App\Models\Banking\Transfer', 'id', 'expense_transaction_id');
}
return null;
}
2022-06-01 10:15:55 +03:00
public function splits()
2019-11-16 10:21:14 +03:00
{
2022-06-01 10:15:55 +03:00
return $this->hasMany('App\Models\Banking\Transaction', 'split_id');
2019-11-16 10:21:14 +03:00
}
2022-06-01 10:15:55 +03:00
public function user()
{
2022-06-01 10:15:55 +03:00
return $this->belongsTo('App\Models\Auth\User', 'contact_id', 'id');
}
public function scopeNumber(Builder $query, string $number): Builder
{
return $query->where('number', '=', $number);
}
2022-06-01 10:15:55 +03:00
public function scopeType(Builder $query, $types): Builder
2019-11-16 10:21:14 +03:00
{
if (empty($types)) {
return $query;
2017-09-14 22:21:00 +03:00
}
2021-09-10 09:41:15 +03:00
return $query->whereIn($this->qualifyColumn('type'), (array) $types);
2019-11-16 10:21:14 +03:00
}
2022-06-01 10:15:55 +03:00
public function scopeIncome(Builder $query): Builder
{
2021-09-10 09:41:15 +03:00
return $query->whereIn($this->qualifyColumn('type'), (array) $this->getIncomeTypes());
}
public function scopeIncomeTransfer(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), '=', self::INCOME_TRANSFER_TYPE);
}
2022-06-01 10:15:55 +03:00
public function scopeIncomeRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), '=', self::INCOME_RECURRING_TYPE)
->whereHas('recurring', function (Builder $query) {
$query->whereNull('deleted_at');
});
2022-06-01 10:15:55 +03:00
}
public function scopeExpense(Builder $query): Builder
{
2021-09-10 09:41:15 +03:00
return $query->whereIn($this->qualifyColumn('type'), (array) $this->getExpenseTypes());
}
public function scopeExpenseTransfer(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), '=', self::EXPENSE_TRANSFER_TYPE);
}
2022-06-01 10:15:55 +03:00
public function scopeExpenseRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), '=', self::EXPENSE_RECURRING_TYPE)
->whereHas('recurring', function (Builder $query) {
$query->whereNull('deleted_at');
});
2022-06-01 10:15:55 +03:00
}
public function scopeIsTransfer(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), 'like', '%-transfer');
}
public function scopeIsNotTransfer(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), 'not like', '%-transfer');
}
2022-06-01 10:15:55 +03:00
public function scopeIsRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), 'like', '%-recurring');
}
public function scopeIsNotRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), 'not like', '%-recurring');
}
2022-06-02 00:50:48 +03:00
public function scopeIsSplit(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), 'like', '%-split');
}
public function scopeIsNotSplit(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), 'not like', '%-split');
}
2022-06-01 10:15:55 +03:00
public function scopeIsDocument(Builder $query): Builder
2019-11-16 10:21:14 +03:00
{
2020-01-02 17:39:06 +03:00
return $query->whereNotNull('document_id');
}
2022-06-01 10:15:55 +03:00
public function scopeIsNotDocument(Builder $query): Builder
2020-01-02 17:39:06 +03:00
{
return $query->whereNull('document_id');
2019-11-16 10:21:14 +03:00
}
2022-06-01 10:15:55 +03:00
public function scopeDocumentId(Builder $query, int $document_id): Builder
2019-11-16 10:21:14 +03:00
{
return $query->where('document_id', '=', $document_id);
}
2022-06-01 10:15:55 +03:00
public function scopeAccountId(Builder $query, int $account_id): Builder
2021-01-02 13:06:02 +03:00
{
return $query->where('account_id', '=', $account_id);
}
2022-06-01 10:15:55 +03:00
public function scopeContactId(Builder $query, int $contact_id): Builder
2021-01-02 13:06:02 +03:00
{
return $query->where('contact_id', '=', $contact_id);
}
2022-06-01 10:15:55 +03:00
public function scopeCategoryId(Builder $query, int $category_id): Builder
2021-01-02 13:06:02 +03:00
{
return $query->where('category_id', '=', $category_id);
}
2019-11-16 10:21:14 +03:00
/**
* Order by paid date.
*/
2022-06-01 10:15:55 +03:00
public function scopeLatest(Builder $query): Builder
2019-11-16 10:21:14 +03:00
{
return $query->orderBy('paid_at', 'desc');
}
2022-06-01 10:15:55 +03:00
public function scopePaid(Builder $query): Builder
2019-11-16 10:21:14 +03:00
{
return $query->sum('amount');
}
2022-06-01 10:15:55 +03:00
public function scopeIsReconciled(Builder $query): Builder
{
return $query->where('reconciled', 1);
}
2022-06-01 10:15:55 +03:00
public function scopeIsNotReconciled(Builder $query): Builder
{
return $query->where('reconciled', 0);
}
public function onCloning($src, $child = null)
{
2022-06-01 10:15:55 +03:00
if (app()->has(\App\Console\Commands\RecurringCheck::class)) {
$suffix = '';
} else {
$suffix = $src->isRecurringTransaction() ? '-recurring' : '';
}
$this->number = $this->getNextTransactionNumber($suffix);
$this->document_id = null;
$this->split_id = null;
unset($this->reconciled);
}
/**
* Convert amount to double.
*
2020-06-08 23:40:09 +03:00
* @return float
*/
2020-07-06 09:54:18 +03:00
public function getAmountForAccountAttribute()
{
2020-06-08 23:40:09 +03:00
$amount = $this->amount;
// Convert amount if not same currency
if ($this->account->currency_code != $this->currency_code) {
2020-07-06 09:54:18 +03:00
$to_code = $this->account->currency_code;
2023-07-10 12:53:43 +03:00
$to_rate = config('money.currencies.' . $this->account->currency_code . '.rate');
2020-07-06 09:54:18 +03:00
$amount = $this->convertBetween($amount, $this->currency_code, $this->currency_rate, $to_code, $to_rate);
}
2020-06-08 23:40:09 +03:00
return $amount;
}
2019-11-16 10:21:14 +03:00
/**
* Get the current balance.
*
* @return string
*/
public function getAttachmentAttribute($value)
{
if (!empty($value) && !$this->hasMedia('attachment')) {
return $value;
} elseif (!$this->hasMedia('attachment')) {
return false;
}
return $this->getMedia('attachment')->all();
}
2022-06-01 10:15:55 +03:00
/**
* Get the splittable status.
*
* @return bool
*/
public function getIsSplittableAttribute()
{
return is_null($this->split_id);
}
public function delete_attachment()
{
if ($attachments = $this->attachment) {
foreach ($attachments as $file) {
MediaModel::where('id', $file->id)->delete();
}
}
2019-11-16 10:21:14 +03:00
}
2020-08-26 15:14:16 +03:00
/**
* Get the title of type.
*
* @return string
*/
public function getTypeTitleAttribute($value)
{
2022-06-01 10:15:55 +03:00
$type = $this->getRealTypeOfRecurringTransaction($this->type);
$type = $this->getRealTypeOfTransferTransaction($type);
2022-10-31 00:34:13 +03:00
$type = $this->getRealTypeOfSplitTransaction($type);
$type = str_replace('-', '_', $type);
2022-06-01 10:15:55 +03:00
return $value ?? trans_choice('general.' . Str::plural($type), 1);
2020-08-26 15:14:16 +03:00
}
2020-09-03 10:42:53 +03:00
/**
* Get the route name.
*
* @return string
*/
public function getRouteNameAttribute($value)
{
if ($value) {
return $value;
}
if ($this->isIncome()) {
2022-06-01 10:15:55 +03:00
if (! empty($this->document_id) && $this->document->type != 'invoice') {
return $this->getRouteFromConfig();
} else {
2022-06-01 10:15:55 +03:00
return !empty($this->document_id) ? 'invoices.show' : 'transactions.show';
}
2020-09-03 10:42:53 +03:00
}
if ($this->isExpense()) {
2022-06-01 10:15:55 +03:00
if (! empty($this->document_id) && $this->document->type != 'bill') {
return $this->getRouteFromConfig();
} else {
2022-06-01 10:15:55 +03:00
return !empty($this->document_id) ? 'bills.show' : 'transactions.show';
}
}
return 'transactions.index';
}
public function getRouteFromConfig()
{
$route = '';
2022-06-01 10:15:55 +03:00
$alias = config('type.document.' . $this->document->type . '.alias');
$prefix = config('type.document.' . $this->document->type . '.route.prefix');
// if use module set module alias
if (!empty($alias)) {
$route .= $alias . '.';
}
if (!empty($prefix)) {
$route .= $prefix . '.';
}
if ($route) {
return $route . 'show';
2020-09-03 10:42:53 +03:00
}
return 'transactions.index';
}
/**
* Get the route id.
*
* @return string
*/
public function getRouteIdAttribute($value)
{
2021-02-26 11:34:34 +03:00
return !empty($value) ? $value : (!empty($this->document_id) ? $this->document_id : $this->id);
2020-09-03 10:42:53 +03:00
}
2020-10-14 17:07:59 +03:00
2022-06-01 10:15:55 +03:00
/**
* Get the line actions.
*
* @return array
*/
public function getLineActionsAttribute()
{
$actions = [];
$prefix = 'transactions';
if (Str::contains($this->type, 'recurring')) {
$prefix = 'recurring-transactions';
}
try {
$actions[] = [
'title' => trans('general.show'),
'icon' => 'visibility',
'url' => route($prefix. '.show', $this->id),
'permission' => 'read-banking-transactions',
'attributes' => [
2022-09-06 13:54:56 +03:00
'id' => 'index-line-actions-show-' . $this->type . '-' . $this->id,
2022-06-01 10:15:55 +03:00
],
];
} catch (\Exception $e) {}
try {
if (! $this->reconciled && $this->isNotTransferTransaction()) {
2022-06-01 10:15:55 +03:00
$actions[] = [
'title' => trans('general.edit'),
'icon' => 'edit',
'url' => route($prefix. '.edit', $this->id),
'permission' => 'update-banking-transactions',
'attributes' => [
2022-09-06 13:54:56 +03:00
'id' => 'index-line-actions-edit-' . $this->type . '-' . $this->id,
2022-06-01 10:15:55 +03:00
],
];
}
} catch (\Exception $e) {}
try {
if (empty($this->document_id) && $this->isNotTransferTransaction()) {
2022-06-01 10:15:55 +03:00
$actions[] = [
'title' => trans('general.duplicate'),
'icon' => 'file_copy',
'url' => route($prefix. '.duplicate', $this->id),
'permission' => 'create-banking-transactions',
'attributes' => [
2022-09-06 13:54:56 +03:00
'id' => 'index-line-actions-duplicate-' . $this->type . '-' . $this->id,
2022-06-01 10:15:55 +03:00
],
];
}
} catch (\Exception $e) {}
try {
2023-08-24 12:09:55 +03:00
if (
$this->is_splittable
&& empty($this->document_id)
&& empty($this->recurring)
&& $this->isNotTransferTransaction()
) {
2022-06-01 14:39:18 +03:00
$connect = [
2022-06-01 10:15:55 +03:00
'type' => 'button',
'title' => trans('general.connect'),
'icon' => 'sensors',
'permission' => 'create-banking-transactions',
'attributes' => [
2022-09-06 13:54:56 +03:00
'id' => 'index-line-actions-connect-' . $this->type . '-' . $this->id,
2022-06-16 12:27:19 +03:00
'@click' => 'onConnectTransactions(\'' . route('transactions.dial', $this->id) . '\')',
2022-06-01 10:15:55 +03:00
],
];
2022-06-01 14:39:18 +03:00
$actions[] = $connect;
2022-06-01 10:15:55 +03:00
$actions[] = [
'type' => 'divider',
];
}
} catch (\Exception $e) {}
try {
$actions[] = [
'title' => trans('general.print'),
'icon' => 'print',
'url' => route($prefix. '.print', $this->id),
'permission' => 'read-banking-transactions',
'attributes' => [
2022-09-06 13:54:56 +03:00
'id' => 'index-line-actions-print-' . $this->type . '-' . $this->id,
2022-06-01 10:15:55 +03:00
'target' => '_blank',
],
];
} catch (\Exception $e) {}
try {
$actions[] = [
'title' => trans('general.download_pdf'),
'icon' => 'picture_as_pdf',
2022-06-01 10:15:55 +03:00
'url' => route($prefix. '.pdf', $this->id),
'permission' => 'read-banking-transactions',
'attributes' => [
2022-09-06 13:54:56 +03:00
'id' => 'index-line-actions-pdf-' . $this->type . '-' . $this->id,
2022-06-01 10:15:55 +03:00
'target' => '_blank',
],
];
} catch (\Exception $e) {}
if ($prefix != 'recurring-transactions') {
if ($this->isNotTransferTransaction()) {
2022-06-01 10:15:55 +03:00
$actions[] = [
'type' => 'divider',
2022-06-01 10:15:55 +03:00
];
try {
$actions[] = [
'type' => 'button',
'title' => trans('general.share_link'),
'icon' => 'share',
'url' => route('modals.transactions.share.create', $this->id),
'permission' => 'read-banking-transactions',
'attributes' => [
2022-09-06 13:54:56 +03:00
'id' => 'index-line-actions-share-' . $this->type . '-' . $this->id,
'@click' => 'onShareLink("' . route('modals.transactions.share.create', $this->id) . '")',
],
];
} catch (\Exception $e) {}
2022-06-01 10:15:55 +03:00
try {
2022-07-14 01:27:36 +03:00
if (! empty($this->contact) && $this->contact->email) {
$actions[] = [
'type' => 'button',
'title' => trans('invoices.send_mail'),
'icon' => 'email',
'url' => route('modals.transactions.emails.create', $this->id),
'permission' => 'read-banking-transactions',
'attributes' => [
2022-09-06 13:54:56 +03:00
'id' => 'index-line-actions-send-email-' . $this->type . '-' . $this->id,
'@click' => 'onSendEmail("' . route('modals.transactions.emails.create', $this->id) . '")',
2022-07-14 01:27:36 +03:00
],
];
}
} catch (\Exception $e) {}
$actions[] = [
'type' => 'divider',
];
try {
if (! $this->reconciled) {
$actions[] = [
'type' => 'delete',
'icon' => 'delete',
2023-08-23 15:14:55 +03:00
'title' => ! empty($this->recurring) ? 'transactions' : 'recurring_template',
'route' => $prefix. '.destroy',
'permission' => 'delete-banking-transactions',
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-delete-' . $this->type . '-' . $this->id,
],
'model' => $this,
];
}
} catch (\Exception $e) {}
}
2022-06-01 10:15:55 +03:00
} else {
try {
$actions[] = [
'title' => trans('general.end'),
'icon' => 'block',
'url' => route($prefix. '.end', $this->id),
'permission' => 'update-banking-transactions',
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-end-' . $this->type . '-' . $this->id,
],
2022-06-01 10:15:55 +03:00
];
} catch (\Exception $e) {}
}
return $actions;
}
/**
* Get the recurring status label.
*
* @return string
*/
public function getRecurringStatusLabelAttribute()
2021-06-24 13:52:49 +03:00
{
2022-06-01 10:15:55 +03:00
return match($this->recurring->status) {
'active' => 'status-partial',
'ended' => 'status-success',
default => 'status-success',
};
2021-06-24 13:52:49 +03:00
}
/**
* Retrieve the model for a bound value.
*
* @param mixed $value
* @param string|null $field
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function resolveRouteBinding($value, $field = null)
{
$query = $this->where('id', $value);
if (request()->route()->hasParameter('recurring_transaction')) {
$query->isRecurring();
}
return $query->firstOrFail();
}
2020-10-14 17:07:59 +03:00
/**
* Create a new factory instance for the model.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
protected static function newFactory()
{
return \Database\Factories\Transaction::new();
}
2017-09-14 22:21:00 +03:00
}