akaunting/app/Models/Banking/Transfer.php

106 lines
2.7 KiB
PHP
Raw Normal View History

2017-09-14 19:21:00 +00:00
<?php
namespace App\Models\Banking;
2019-11-16 07:21:14 +00:00
use App\Abstracts\Model;
2021-07-09 20:36:41 +00:00
use App\Models\Common\Media as MediaModel;
2017-09-14 19:21:00 +00:00
use App\Traits\Currencies;
2021-07-09 20:36:41 +00:00
use App\Traits\Media;
2020-10-14 14:07:59 +00:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2021-02-10 22:57:17 +00:00
use Znck\Eloquent\Traits\BelongsToThrough;
2017-09-14 19:21:00 +00:00
class Transfer extends Model
{
2021-07-09 20:36:41 +00:00
use BelongsToThrough, Currencies, HasFactory, Media;
2017-09-14 19:21:00 +00:00
protected $table = 'transfers';
2021-07-09 20:36:41 +00:00
protected $appends = ['attachment'];
2017-09-14 19:21:00 +00:00
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
2021-06-17 07:59:07 +00:00
protected $fillable = ['company_id', 'expense_transaction_id', 'income_transaction_id', 'created_by'];
2017-09-14 19:21:00 +00:00
/**
* Sortable columns.
*
* @var array
*/
2019-11-16 07:21:14 +00:00
public $sortable = ['expense.paid_at', 'expense.amount', 'expense.name', 'income.name'];
2017-09-14 19:21:00 +00:00
2019-11-16 07:21:14 +00:00
public function expense_transaction()
2017-09-14 19:21:00 +00:00
{
2019-11-16 07:21:14 +00:00
return $this->belongsTo('App\Models\Banking\Transaction', 'expense_transaction_id');
2017-09-14 19:21:00 +00:00
}
2019-11-16 07:21:14 +00:00
public function expense_account()
2017-09-14 19:21:00 +00:00
{
2021-02-10 22:57:17 +00: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 19:21:00 +00:00
}
2019-11-16 07:21:14 +00:00
public function income_transaction()
2017-09-14 19:21:00 +00:00
{
2019-11-16 07:21:14 +00:00
return $this->belongsTo('App\Models\Banking\Transaction', 'income_transaction_id');
2017-09-14 19:21:00 +00:00
}
2019-11-16 07:21:14 +00:00
public function income_account()
2017-09-14 19:21:00 +00:00
{
2021-02-10 22:57:17 +00: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 14:07:59 +00:00
2021-07-09 20:36:41 +00: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');
}
2020-10-14 14:07:59 +00: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 19:21:00 +00:00
}