2020-08-26 15:14:16 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Traits;
|
|
|
|
|
|
|
|
trait Transactions
|
|
|
|
{
|
|
|
|
public function isIncome()
|
|
|
|
{
|
|
|
|
$type = $this->type ?? $this->transaction->type ?? 'income';
|
|
|
|
|
|
|
|
return in_array($type, $this->getIncomeTypes());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isExpense()
|
|
|
|
{
|
|
|
|
$type = $this->type ?? $this->transaction->type ?? 'expense';
|
|
|
|
|
|
|
|
return in_array($type, $this->getExpenseTypes());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIncomeTypes($return = 'array')
|
|
|
|
{
|
|
|
|
return $this->getTransactionTypes('income', $return);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getExpenseTypes($return = 'array')
|
|
|
|
{
|
|
|
|
return $this->getTransactionTypes('expense', $return);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTransactionTypes($index, $return = 'array')
|
|
|
|
{
|
|
|
|
$types = (string) setting('transaction.type.' . $index);
|
|
|
|
|
|
|
|
return ($return == 'array') ? explode(',', $types) : $types;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addIncomeType($new_type)
|
|
|
|
{
|
|
|
|
$this->addTransactionType($new_type, 'income');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addExpenseType($new_type)
|
|
|
|
{
|
|
|
|
$this->addTransactionType($new_type, 'expense');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addTransactionType($new_type, $index)
|
|
|
|
{
|
|
|
|
$types = explode(',', setting('transaction.type.' . $index));
|
|
|
|
|
|
|
|
if (in_array($new_type, $types)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$types[] = $new_type;
|
|
|
|
|
|
|
|
setting([
|
|
|
|
'transaction.type.' . $index => implode(',', $types),
|
|
|
|
])->save();
|
|
|
|
}
|
2021-06-24 13:52:49 +03:00
|
|
|
|
|
|
|
protected function getSettingKey($type, $setting_key)
|
|
|
|
{
|
|
|
|
$key = '';
|
|
|
|
$alias = config('type.' . $type . '.alias');
|
|
|
|
|
|
|
|
if (!empty($alias)) {
|
|
|
|
$key .= $alias . '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
$prefix = config('type.' . $type . '.setting.prefix');
|
|
|
|
|
|
|
|
$key .= $prefix . '.' . $setting_key;
|
|
|
|
|
|
|
|
return $key;
|
|
|
|
}
|
2020-08-26 15:14:16 +03:00
|
|
|
}
|