akaunting/app/Models/Banking/Transfer.php

237 lines
5.5 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
{
2019-11-16 10:21:14 +03:00
return $this->belongsTo('App\Models\Banking\Transaction', 'expense_transaction_id');
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(
'App\Models\Banking\Account',
'App\Models\Banking\Transaction',
null,
'',
['App\Models\Banking\Transaction' => 'expense_transaction_id']
)->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
{
2019-11-16 10:21:14 +03:00
return $this->belongsTo('App\Models\Banking\Transaction', 'income_transaction_id');
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(
'App\Models\Banking\Account',
'App\Models\Banking\Transaction',
null,
'',
['App\Models\Banking\Transaction' => 'income_transaction_id']
)->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;
}
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
}