added custom transaction types
This commit is contained in:
parent
d64917a3e0
commit
68f31b895f
@ -3,9 +3,12 @@
|
||||
namespace App\Models\Banking;
|
||||
|
||||
use App\Abstracts\Model;
|
||||
use App\Traits\Transactions;
|
||||
|
||||
class Account extends Model
|
||||
{
|
||||
use Transactions;
|
||||
|
||||
protected $table = 'accounts';
|
||||
|
||||
/**
|
||||
@ -36,12 +39,12 @@ class Account extends Model
|
||||
|
||||
public function expense_transactions()
|
||||
{
|
||||
return $this->transactions()->where('type', 'expense');
|
||||
return $this->transactions()->whereIn('type', (array) $this->getExpenseTypes());
|
||||
}
|
||||
|
||||
public function income_transactions()
|
||||
{
|
||||
return $this->transactions()->where('type', 'income');
|
||||
return $this->transactions()->whereIn('type', (array) $this->getIncomeTypes());
|
||||
}
|
||||
|
||||
public function transactions()
|
||||
|
@ -8,11 +8,13 @@ use App\Traits\Currencies;
|
||||
use App\Traits\DateTime;
|
||||
use App\Traits\Media;
|
||||
use App\Traits\Recurring;
|
||||
use App\Traits\Transactions;
|
||||
use Bkwld\Cloner\Cloneable;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Transaction extends Model
|
||||
{
|
||||
use Cloneable, Currencies, DateTime, Media, Recurring;
|
||||
use Cloneable, Currencies, DateTime, Media, Recurring, Transactions;
|
||||
|
||||
protected $table = 'transactions';
|
||||
|
||||
@ -103,7 +105,7 @@ class Transaction extends Model
|
||||
*/
|
||||
public function scopeIncome($query)
|
||||
{
|
||||
return $query->where($this->table . '.type', '=', 'income');
|
||||
return $query->whereIn($this->table . '.type', (array) $this->getIncomeTypes());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -114,7 +116,7 @@ class Transaction extends Model
|
||||
*/
|
||||
public function scopeExpense($query)
|
||||
{
|
||||
return $query->where($this->table . '.type', '=', 'expense');
|
||||
return $query->whereIn($this->table . '.type', (array) $this->getExpenseTypes());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -279,4 +281,14 @@ class Transaction extends Model
|
||||
|
||||
return $this->getMedia('attachment')->last();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the title of type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getTypeTitleAttribute($value)
|
||||
{
|
||||
return $value ?? trans_choice('general.' . Str::plural($this->type), 1);
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,10 @@
|
||||
|
||||
namespace App\Models\Common;
|
||||
|
||||
use App\Traits\Contacts;
|
||||
use App\Traits\Media;
|
||||
use App\Traits\Tenants;
|
||||
use App\Traits\Transactions;
|
||||
use Illuminate\Database\Eloquent\Model as Eloquent;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Kyslik\ColumnSortable\Sortable;
|
||||
@ -11,7 +13,7 @@ use Lorisleiva\LaravelSearchString\Concerns\SearchString;
|
||||
|
||||
class Company extends Eloquent
|
||||
{
|
||||
use Media, SearchString, SoftDeletes, Sortable, Tenants;
|
||||
use Contacts, Media, SearchString, SoftDeletes, Sortable, Tenants, Transactions;
|
||||
|
||||
protected $table = 'companies';
|
||||
|
||||
@ -92,7 +94,7 @@ class Company extends Eloquent
|
||||
|
||||
public function customers()
|
||||
{
|
||||
return $this->contacts()->where('type', 'customer');
|
||||
return $this->contacts()->whereIn('type', (array) $this->getCustomerTypes());
|
||||
}
|
||||
|
||||
public function dashboards()
|
||||
@ -107,12 +109,12 @@ class Company extends Eloquent
|
||||
|
||||
public function expense_transactions()
|
||||
{
|
||||
return $this->transactions()->where('type', 'expense');
|
||||
return $this->transactions()->whereIn('type', (array) $this->getExpenseTypes());
|
||||
}
|
||||
|
||||
public function income_transactions()
|
||||
{
|
||||
return $this->transactions()->where('type', 'income');
|
||||
return $this->transactions()->whereIn('type', (array) $this->getIncomeTypes());
|
||||
}
|
||||
|
||||
public function invoices()
|
||||
@ -197,7 +199,7 @@ class Company extends Eloquent
|
||||
|
||||
public function vendors()
|
||||
{
|
||||
return $this->contacts()->where('type', 'vendor');
|
||||
return $this->contacts()->whereIn('type', (array) $this->getVendorTypes());
|
||||
}
|
||||
|
||||
public function widgets()
|
||||
|
@ -7,11 +7,12 @@ use Bkwld\Cloner\Cloneable;
|
||||
use App\Traits\Contacts;
|
||||
use App\Traits\Currencies;
|
||||
use App\Traits\Media;
|
||||
use App\Traits\Transactions;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
class Contact extends Model
|
||||
{
|
||||
use Cloneable, Contacts, Currencies, Media, Notifiable;
|
||||
use Cloneable, Contacts, Currencies, Media, Notifiable, Transactions;
|
||||
|
||||
protected $table = 'contacts';
|
||||
|
||||
@ -41,12 +42,12 @@ class Contact extends Model
|
||||
|
||||
public function expense_transactions()
|
||||
{
|
||||
return $this->transactions()->where('type', 'expense');
|
||||
return $this->transactions()->whereIn('type', (array) $this->getExpenseTypes());
|
||||
}
|
||||
|
||||
public function income_transactions()
|
||||
{
|
||||
return $this->transactions()->where('type', 'income');
|
||||
return $this->transactions()->whereIn('type', (array) $this->getIncomeTypes());
|
||||
}
|
||||
|
||||
public function invoices()
|
||||
|
@ -3,9 +3,12 @@
|
||||
namespace App\Models\Setting;
|
||||
|
||||
use App\Abstracts\Model;
|
||||
use App\Traits\Transactions;
|
||||
|
||||
class Category extends Model
|
||||
{
|
||||
use Transactions;
|
||||
|
||||
protected $table = 'categories';
|
||||
|
||||
/**
|
||||
@ -29,12 +32,12 @@ class Category extends Model
|
||||
|
||||
public function expense_transactions()
|
||||
{
|
||||
return $this->transactions()->where('type', 'expense');
|
||||
return $this->transactions()->whereIn('type', (array) $this->getExpenseTypes());
|
||||
}
|
||||
|
||||
public function income_transactions()
|
||||
{
|
||||
return $this->transactions()->where('type', 'income');
|
||||
return $this->transactions()->whereIn('type', (array) $this->getIncomeTypes());
|
||||
}
|
||||
|
||||
public function invoices()
|
||||
|
@ -3,9 +3,12 @@
|
||||
namespace App\Models\Setting;
|
||||
|
||||
use App\Abstracts\Model;
|
||||
use App\Traits\Contacts;
|
||||
use App\Traits\Transactions;
|
||||
|
||||
class Currency extends Model
|
||||
{
|
||||
use Contacts, Transactions;
|
||||
|
||||
protected $table = 'currencies';
|
||||
|
||||
@ -40,17 +43,17 @@ class Currency extends Model
|
||||
|
||||
public function customers()
|
||||
{
|
||||
return $this->contacts()->where('type', 'customer');
|
||||
return $this->contacts()->whereIn('type', (array) $this->getCustomerTypes());
|
||||
}
|
||||
|
||||
public function expense_transactions()
|
||||
{
|
||||
return $this->transactions()->where('type', 'expense');
|
||||
return $this->transactions()->whereIn('type', (array) $this->getExpenseTypes());
|
||||
}
|
||||
|
||||
public function income_transactions()
|
||||
{
|
||||
return $this->transactions()->where('type', 'income');
|
||||
return $this->transactions()->whereIn('type', (array) $this->getIncomeTypes());
|
||||
}
|
||||
|
||||
public function invoices()
|
||||
@ -65,7 +68,7 @@ class Currency extends Model
|
||||
|
||||
public function vendors()
|
||||
{
|
||||
return $this->contacts()->where('type', 'vendor');
|
||||
return $this->contacts()->whereIn('type', (array) $this->getVendorTypes());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -30,7 +30,7 @@ trait Contacts
|
||||
|
||||
public function getContactTypes($index, $return = 'array')
|
||||
{
|
||||
$types = (string) setting('contact.type.' . $index, $index);
|
||||
$types = (string) setting('contact.type.' . $index);
|
||||
|
||||
return ($return == 'array') ? explode(',', $types) : $types;
|
||||
}
|
||||
@ -47,7 +47,7 @@ trait Contacts
|
||||
|
||||
public function addContactType($new_type, $index)
|
||||
{
|
||||
$types = explode(',', setting('contact.type.' . $index, $index));
|
||||
$types = explode(',', setting('contact.type.' . $index));
|
||||
|
||||
if (in_array($new_type, $types)) {
|
||||
return;
|
||||
|
62
app/Traits/Transactions.php
Normal file
62
app/Traits/Transactions.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?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();
|
||||
}
|
||||
}
|
@ -138,7 +138,7 @@ class CashFlow extends Widget
|
||||
}
|
||||
}
|
||||
|
||||
$items = $this->applyFilters(Transaction::type($type)->whereBetween('paid_at', [$start, $end])->isNotTransfer())->get();
|
||||
$items = $this->applyFilters(Transaction::$type()->whereBetween('paid_at', [$start, $end])->isNotTransfer())->get();
|
||||
|
||||
$this->setTotals($totals, $items, $date_format, $period);
|
||||
|
||||
|
@ -138,6 +138,12 @@ return [
|
||||
'vendor' => env('SETTING_FALLBACK_CONTACT_TYPE_VENDOR', 'vendor'),
|
||||
],
|
||||
],
|
||||
'transaction' => [
|
||||
'type' => [
|
||||
'income' => env('SETTING_FALLBACK_TRANSACTION_TYPE_INCOME', 'income'),
|
||||
'expense' => env('SETTING_FALLBACK_TRANSACTION_TYPE_EXPENSE', 'expense'),
|
||||
],
|
||||
],
|
||||
],
|
||||
|
||||
/*
|
||||
|
@ -47,7 +47,7 @@
|
||||
<tr class="row align-items-center border-top-1 tr-py">
|
||||
<td class="col-sm-2 col-md-2 d-none d-sm-block">@date($item->paid_at)</td>
|
||||
<td class="col-xs-4 col-sm-3 col-md-2">{{ $item->account->name }}</td>
|
||||
<td class="col-xs-4 col-sm-3 col-md-2">{{ trans_choice('general.' . Str::plural($item->type), 1) }}</td>
|
||||
<td class="col-xs-4 col-sm-3 col-md-2">{{ $item->type_title }}</td>
|
||||
<td class="col-sm-2 col-md-2 d-none d-sm-block">{{ $item->category->name }}</td>
|
||||
<td class="col-md-2 d-none d-md-block long-texts">{{ $item->description }}</td>
|
||||
<td class="col-xs-4 col-sm-2 col-md-2 text-right">
|
||||
|
Loading…
x
Reference in New Issue
Block a user