diff --git a/app/Models/Banking/Account.php b/app/Models/Banking/Account.php index fa92b774c..4a5b51883 100644 --- a/app/Models/Banking/Account.php +++ b/app/Models/Banking/Account.php @@ -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() diff --git a/app/Models/Banking/Transaction.php b/app/Models/Banking/Transaction.php index 40002fb37..113a2f36a 100644 --- a/app/Models/Banking/Transaction.php +++ b/app/Models/Banking/Transaction.php @@ -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); + } } diff --git a/app/Models/Common/Company.php b/app/Models/Common/Company.php index 32390caa6..e8cff5716 100644 --- a/app/Models/Common/Company.php +++ b/app/Models/Common/Company.php @@ -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() diff --git a/app/Models/Common/Contact.php b/app/Models/Common/Contact.php index bc8d95132..6f61f916c 100644 --- a/app/Models/Common/Contact.php +++ b/app/Models/Common/Contact.php @@ -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() diff --git a/app/Models/Setting/Category.php b/app/Models/Setting/Category.php index d180f41a0..f00bde121 100644 --- a/app/Models/Setting/Category.php +++ b/app/Models/Setting/Category.php @@ -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() diff --git a/app/Models/Setting/Currency.php b/app/Models/Setting/Currency.php index a25f7d46b..c75e19c4e 100644 --- a/app/Models/Setting/Currency.php +++ b/app/Models/Setting/Currency.php @@ -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()); } /** diff --git a/app/Traits/Contacts.php b/app/Traits/Contacts.php index df7cb3d2c..f5bbdc28f 100644 --- a/app/Traits/Contacts.php +++ b/app/Traits/Contacts.php @@ -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; diff --git a/app/Traits/Transactions.php b/app/Traits/Transactions.php new file mode 100644 index 000000000..d70e2b22e --- /dev/null +++ b/app/Traits/Transactions.php @@ -0,0 +1,62 @@ +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(); + } +} diff --git a/app/Widgets/CashFlow.php b/app/Widgets/CashFlow.php index 56b2639c4..e53730ea5 100644 --- a/app/Widgets/CashFlow.php +++ b/app/Widgets/CashFlow.php @@ -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); diff --git a/config/setting.php b/config/setting.php index 7fdfe0f3a..8f0ce3c14 100644 --- a/config/setting.php +++ b/config/setting.php @@ -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'), + ], + ], ], /* diff --git a/resources/views/banking/transactions/index.blade.php b/resources/views/banking/transactions/index.blade.php index b9c749f35..cce956299 100644 --- a/resources/views/banking/transactions/index.blade.php +++ b/resources/views/banking/transactions/index.blade.php @@ -47,7 +47,7 @@