akaunting/app/Models/Banking/Transfer.php

298 lines
7.7 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;
2021-07-09 23:36:41 +03:00
use App\Models\Common\Media as MediaModel;
2017-09-14 22:21:00 +03:00
use App\Traits\Currencies;
2021-07-09 23:36:41 +03:00
use App\Traits\Media;
use Bkwld\Cloner\Cloneable;
2020-10-14 17:07:59 +03:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2021-02-11 01:57:17 +03:00
use Znck\Eloquent\Traits\BelongsToThrough;
2017-09-14 22:21:00 +03:00
class Transfer extends Model
{
use BelongsToThrough, Cloneable, Currencies, HasFactory, Media;
2017-09-14 22:21:00 +03:00
protected $table = 'transfers';
protected $appends = [
'attachment',
'from_account_id',
'from_currency_code',
'from_account_rate',
'to_account_id',
'to_currency_code',
'to_account_rate',
'transferred_at',
'description',
'amount',
'payment_method',
'reference',
];
2021-07-09 23:36:41 +03:00
2017-09-14 22:21:00 +03:00
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
2021-09-07 10:33:34 +03:00
protected $fillable = ['company_id', 'expense_transaction_id', 'income_transaction_id', 'created_from', 'created_by'];
2017-09-14 22:21:00 +03:00
/**
* Sortable columns.
*
* @var array
*/
2019-11-16 10:21:14 +03:00
public $sortable = ['expense.paid_at', 'expense.amount', 'expense.name', 'income.name'];
2017-09-14 22:21:00 +03:00
/**
* Clonable relationships.
*
* @var array
*/
public $cloneable_relations = ['expense_transaction', 'income_transaction'];
2019-11-16 10:21:14 +03:00
public function expense_transaction()
2017-09-14 22:21:00 +03:00
{
2022-06-10 19:26:32 +03:00
return $this->belongsTo('App\Models\Banking\Transaction', 'expense_transaction_id')
->withoutGlobalScope('App\Scopes\Transaction')
->withDefault(['name' => trans('general.na')]);
2017-09-14 22:21:00 +03:00
}
2019-11-16 10:21:14 +03:00
public function expense_account()
2017-09-14 22:21:00 +03:00
{
2021-02-11 01:57:17 +03:00
return $this->belongsToThrough(
2022-06-10 19:26:32 +03:00
'App\Models\Banking\Account',
'App\Models\Banking\Transaction',
null,
'',
['App\Models\Banking\Transaction' => 'expense_transaction_id']
)
->withoutGlobalScope('App\Scopes\Transaction')
->withDefault(['name' => trans('general.na')]);
2017-09-14 22:21:00 +03:00
}
2019-11-16 10:21:14 +03:00
public function income_transaction()
2017-09-14 22:21:00 +03:00
{
2022-06-10 19:26:32 +03:00
return $this->belongsTo('App\Models\Banking\Transaction', 'income_transaction_id')
->withoutGlobalScope('App\Scopes\Transaction')
->withDefault(['name' => trans('general.na')]);
2017-09-14 22:21:00 +03:00
}
2019-11-16 10:21:14 +03:00
public function income_account()
2017-09-14 22:21:00 +03:00
{
2021-02-11 01:57:17 +03:00
return $this->belongsToThrough(
2022-06-10 19:26:32 +03:00
'App\Models\Banking\Account',
'App\Models\Banking\Transaction',
null,
'',
['App\Models\Banking\Transaction' => 'income_transaction_id']
)
->withoutGlobalScope('App\Scopes\Transaction')
->withDefault(['name' => trans('general.na')]);
}
2020-10-14 17:07:59 +03:00
2021-07-09 23:36:41 +03:00
/**
* Get the current balance.
*
* @return string
*/
public function getAttachmentAttribute($value = null)
{
if (!empty($value) && !$this->hasMedia('attachment')) {
return $value;
} elseif (!$this->hasMedia('attachment')) {
return false;
}
return $this->getMedia('attachment')->all();
}
public function delete_attachment()
{
if ($attachments = $this->attachment) {
foreach ($attachments as $file) {
MediaModel::where('id', $file->id)->delete();
}
}
}
public function getTemplatePathAttribute($value = null)
{
return $value ?: 'banking.transfers.print_' . setting('transfer.template');
}
/**
* Get the current balance.
*
* @return int
*/
public function getFromAccountIdAttribute($value = null)
{
return $value ?: $this->expense_transaction->account_id;
}
/**
* Get the current balance.
*
* @return string
*/
public function getFromCurrencyCodeAttribute($value = null)
{
return $value ?: $this->expense_transaction->currency_code;
}
/**
* Get the current balance.
*
* @return string
*/
public function getFromAccountRateAttribute($value = null)
{
return $value ?: $this->expense_transaction->currency_rate;
}
/**
* Get the current balance.
*
* @return int
*/
public function getToAccountIdAttribute($value = null)
{
return $value ?: $this->income_transaction->account_id;
}
/**
* Get the current balance.
*
* @return string
*/
public function getToCurrencyCodeAttribute($value = null)
{
return $value ?: $this->income_transaction->currency_code;
}
/**
* Get the current balance.
*
* @return string
*/
public function getToAccountRateAttribute($value = null)
{
return $value ?: $this->income_transaction->currency_rate;
}
/**
* Get the current balance.
*
* @return date
*/
public function getTransferredAtAttribute($value = null)
{
return $value ?: $this->expense_transaction->paid_at;
}
/**
* Get the current balance.
*
* @return string
*/
public function getDescriptionAttribute($value = null)
{
return $value ?: $this->expense_transaction->description;
}
/**
* Get the current balance.
*
* @return float
*/
public function getAmountAttribute($value = null)
{
return $value ?: $this->expense_transaction->amount;
}
/**
* Get the current balance.
*
* @return string
*/
public function getPaymentMethodAttribute($value = null)
{
return $value ?: $this->expense_transaction->payment_method;
}
/**
* Get the current balance.
*
* @return string
*/
public function getReferenceAttribute($value = null)
{
return $value ?: $this->expense_transaction->reference;
}
2022-06-01 10:15:55 +03:00
/**
* Get the line actions.
*
* @return array
*/
public function getLineActionsAttribute()
{
$actions = [];
$actions[] = [
'title' => trans('general.show'),
'icon' => 'visibility',
'url' => route('transfers.show', $this->id),
'permission' => 'read-banking-transfers',
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-show-transfer-' . $this->id,
],
2022-06-01 10:15:55 +03:00
];
$actions[] = [
'title' => trans('general.edit'),
'icon' => 'edit',
'url' => route('transfers.edit', $this->id),
'permission' => 'update-banking-transfers',
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-edit-transfer-' . $this->id,
],
2022-06-01 10:15:55 +03:00
];
$actions[] = [
'title' => trans('general.duplicate'),
'icon' => 'file_copy',
'url' => route('transfers.duplicate', $this->id),
'permission' => 'update-banking-transfers',
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-duplicate-transfer-' . $this->id,
],
2022-06-01 10:15:55 +03:00
];
$actions[] = [
'type' => 'delete',
'icon' => 'delete',
'route' => 'transfers.destroy',
'permission' => 'delete-banking-transfers',
2022-09-06 13:54:56 +03:00
'attributes' => [
'id' => 'index-line-actions-delete-transfer-' . $this->id,
],
2022-06-01 10:15:55 +03:00
'model' => $this,
];
return $actions;
}
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\Transfer::new();
}
2017-09-14 22:21:00 +03:00
}