converted transfer check from category to type

This commit is contained in:
Denis Duliçi
2022-07-21 01:07:55 +03:00
parent 16c645b4a0
commit 5a7697f741
18 changed files with 221 additions and 109 deletions

View File

@ -21,13 +21,13 @@ class Transaction extends Model
use Cloneable, Currencies, DateTime, HasFactory, Media, Recurring, Transactions;
public const INCOME_TYPE = 'income';
public const INCOME_TRANSFER_TYPE = 'income-transfer';
public const INCOME_SPLIT_TYPE = 'income-split';
public const INCOME_RECURRING_TYPE = 'income-recurring';
public const INCOME_TRANSFER_TYPE = 'income-transfer'; // TODO - convert transfer from category to type
public const EXPENSE_TYPE = 'expense';
public const EXPENSE_TRANSFER_TYPE = 'expense-transfer';
public const EXPENSE_SPLIT_TYPE = 'expense-split';
public const EXPENSE_RECURRING_TYPE = 'expense-recurring';
public const EXPENSE_TRANSFER_TYPE = 'expense-transfer'; // TODO - convert transfer from category to type
protected $table = 'transactions';
@ -145,11 +145,11 @@ class Transaction extends Model
public function transfer()
{
if ($this->type == self::INCOME_TYPE) {
if ($this->type == static::INCOME_TRANSFER_TYPE) {
return $this->belongsTo('App\Models\Banking\Transfer', 'id', 'income_transaction_id');
}
if ($this->type == self::EXPENSE_TYPE) {
if ($this->type == static::EXPENSE_TRANSFER_TYPE) {
return $this->belongsTo('App\Models\Banking\Transfer', 'id', 'expense_transaction_id');
}
@ -180,6 +180,11 @@ class Transaction extends Model
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);
}
public function scopeIncomeRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), '=', self::INCOME_RECURRING_TYPE);
@ -190,11 +195,26 @@ class Transaction extends Model
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);
}
public function scopeExpenseRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), '=', self::EXPENSE_RECURRING_TYPE);
}
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');
}
public function scopeIsRecurring(Builder $query): Builder
{
return $query->where($this->qualifyColumn('type'), 'like', '%-recurring');
@ -215,16 +235,6 @@ class Transaction extends Model
return $query->where($this->qualifyColumn('type'), 'not like', '%-split');
}
public function scopeIsTransfer(Builder $query): Builder
{
return $query->where('category_id', '=', Category::transfer());
}
public function scopeIsNotTransfer(Builder $query): Builder
{
return $query->where('category_id', '<>', Category::transfer());
}
public function scopeIsDocument(Builder $query): Builder
{
return $query->whereNotNull('document_id');
@ -346,16 +356,6 @@ class Transaction extends Model
}
}
/**
* Check if the record is attached to a transfer.
*
* @return bool
*/
public function getHasTransferRelationAttribute()
{
return (bool) ($this->category?->id == $this->category?->transfer());
}
/**
* Get the title of type.
*
@ -364,6 +364,9 @@ class Transaction extends Model
public function getTypeTitleAttribute($value)
{
$type = $this->getRealTypeOfRecurringTransaction($this->type);
$type = $this->getRealTypeOfTransferTransaction($type);
$type = str_replace('-', '_', $type);
return $value ?? trans_choice('general.' . Str::plural($type), 1);
}
@ -459,7 +462,7 @@ class Transaction extends Model
} catch (\Exception $e) {}
try {
if (! $this->reconciled && ! $this->hasTransferRelation) {
if (! $this->reconciled && $this->isNotTransferTransaction()) {
$actions[] = [
'title' => trans('general.edit'),
'icon' => 'edit',
@ -473,7 +476,7 @@ class Transaction extends Model
} catch (\Exception $e) {}
try {
if (empty($this->document_id) && ! $this->hasTransferRelation) {
if (empty($this->document_id) && $this->isNotTransferTransaction()) {
$actions[] = [
'title' => trans('general.duplicate'),
'icon' => 'file_copy',
@ -487,7 +490,7 @@ class Transaction extends Model
} catch (\Exception $e) {}
try {
if ($this->is_splittable && empty($this->document_id) && empty($this->recurring) && ! $this->hasTransferRelation) {
if ($this->is_splittable && empty($this->document_id) && empty($this->recurring) && $this->isNotTransferTransaction()) {
$connect = [
'type' => 'button',
'title' => trans('general.connect'),
@ -534,7 +537,7 @@ class Transaction extends Model
} catch (\Exception $e) {}
if ($prefix != 'recurring-transactions') {
if (! $this->hasTransferRelation) {
if ($this->isNotTransferTransaction()) {
$actions[] = [
'type' => 'divider',
];