Skip to content

Commit

Permalink
added custom transaction types
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Aug 26, 2020
1 parent d64917a commit 68f31b8
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 23 deletions.
7 changes: 5 additions & 2 deletions app/Models/Banking/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -36,12 +39,12 @@ public function currency()

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()
Expand Down
18 changes: 15 additions & 3 deletions app/Models/Banking/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
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';

Expand Down Expand Up @@ -103,7 +105,7 @@ public function scopeType($query, $types)
*/
public function scopeIncome($query)
{
return $query->where($this->table . '.type', '=', 'income');
return $query->whereIn($this->table . '.type', (array) $this->getIncomeTypes());
}

/**
Expand All @@ -114,7 +116,7 @@ public function scopeIncome($query)
*/
public function scopeExpense($query)
{
return $query->where($this->table . '.type', '=', 'expense');
return $query->whereIn($this->table . '.type', (array) $this->getExpenseTypes());
}

/**
Expand Down Expand Up @@ -279,4 +281,14 @@ public function getAttachmentAttribute($value)

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);
}
}
12 changes: 7 additions & 5 deletions app/Models/Common/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

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;
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';

Expand Down Expand Up @@ -92,7 +94,7 @@ public function currencies()

public function customers()
{
return $this->contacts()->where('type', 'customer');
return $this->contacts()->whereIn('type', (array) $this->getCustomerTypes());
}

public function dashboards()
Expand All @@ -107,12 +109,12 @@ public function email_templates()

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()
Expand Down Expand Up @@ -197,7 +199,7 @@ public function users()

public function vendors()
{
return $this->contacts()->where('type', 'vendor');
return $this->contacts()->whereIn('type', (array) $this->getVendorTypes());
}

public function widgets()
Expand Down
7 changes: 4 additions & 3 deletions app/Models/Common/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
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';

Expand Down Expand Up @@ -41,12 +42,12 @@ public function currency()

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()
Expand Down
7 changes: 5 additions & 2 deletions app/Models/Setting/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -29,12 +32,12 @@ public function bills()

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()
Expand Down
11 changes: 7 additions & 4 deletions app/Models/Setting/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -40,17 +43,17 @@ public function contacts()

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()
Expand All @@ -65,7 +68,7 @@ public function transactions()

public function vendors()
{
return $this->contacts()->where('type', 'vendor');
return $this->contacts()->whereIn('type', (array) $this->getVendorTypes());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Traits/Contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getVendorTypes($return = 'array')

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;
}
Expand All @@ -47,7 +47,7 @@ public function addVendorType($new_type)

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;
Expand Down
62 changes: 62 additions & 0 deletions app/Traits/Transactions.php
Original file line number Diff line number Diff line change
@@ -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();
}
}
2 changes: 1 addition & 1 deletion app/Widgets/CashFlow.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private function calculateTotals($type, $start, $end, $period)
}
}

$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);

Expand Down
6 changes: 6 additions & 0 deletions config/setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@
'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'),
],
],
],

/*
Expand Down
2 changes: 1 addition & 1 deletion resources/views/banking/transactions/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down

0 comments on commit 68f31b8

Please sign in to comment.