From e2439b60c3b8498944e1623dbcf025d2ef1d3e71 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 16 Dec 2022 09:38:02 +1100 Subject: [PATCH] Prevent relinking of payments with transactions --- app/Filters/ExpenseCategoryFilters.php | 124 ++++++++++++++++++ .../Controllers/ExpenseCategoryController.php | 8 +- .../MatchBankTransactionRequest.php | 5 +- app/Jobs/Bank/MatchBankTransactions.php | 1 - app/Models/ExpenseCategory.php | 4 +- app/Repositories/ExpenseRepository.php | 15 ++- 6 files changed, 147 insertions(+), 10 deletions(-) create mode 100644 app/Filters/ExpenseCategoryFilters.php diff --git a/app/Filters/ExpenseCategoryFilters.php b/app/Filters/ExpenseCategoryFilters.php new file mode 100644 index 0000000000..1c9965a38f --- /dev/null +++ b/app/Filters/ExpenseCategoryFilters.php @@ -0,0 +1,124 @@ +builder; + } + + return $this->builder->where('expense_categories.name', 'like', '%'.$filter.'%'); + + } + + /** + * Filters the list based on the status + * archived, active, deleted. + * + * @param string filter + * @return Builder + */ + public function status(string $filter = '') : Builder + { + if (strlen($filter) == 0) { + return $this->builder; + } + + $table = 'expense_categories'; + $filters = explode(',', $filter); + + return $this->builder->where(function ($query) use ($filters, $table) { + $query->whereNull($table.'.id'); + + if (in_array(parent::STATUS_ACTIVE, $filters)) { + $query->orWhereNull($table.'.deleted_at'); + } + + if (in_array(parent::STATUS_ARCHIVED, $filters)) { + $query->orWhere(function ($query) use ($table) { + $query->whereNotNull($table.'.deleted_at'); + + if (! in_array($table, ['users'])) { + $query->where($table.'.is_deleted', '=', 0); + } + }); + } + + if (in_array(parent::STATUS_DELETED, $filters)) { + $query->orWhere($table.'.is_deleted', '=', 1); + } + }); + } + + /** + * Sorts the list based on $sort. + * + * @param string sort formatted as column|asc + * @return Builder + */ + public function sort(string $sort) : Builder + { + $sort_col = explode('|', $sort); + + if (is_array($sort_col) && in_array($sort_col[1], ['asc', 'desc']) && in_array($sort_col[0], ['name'])) { + return $this->builder->orderBy($sort_col[0], $sort_col[1]); + } + + return $this->builder; + } + + /** + * Returns the base query. + * + * @param int company_id + * @param User $user + * @return Builder + * @deprecated + */ + public function baseQuery(int $company_id, User $user) : Builder + { + + return $this->builder; + + } + + /** + * Filters the query by the users company ID. + * + * @return Illuminate\Database\Query\Builder + */ + public function entityFilter() + { + + //return $this->builder->whereCompanyId(auth()->user()->company()->id); + return $this->builder->company(); + } +} diff --git a/app/Http/Controllers/ExpenseCategoryController.php b/app/Http/Controllers/ExpenseCategoryController.php index 28cc1db719..83d6be93e4 100644 --- a/app/Http/Controllers/ExpenseCategoryController.php +++ b/app/Http/Controllers/ExpenseCategoryController.php @@ -12,12 +12,14 @@ namespace App\Http\Controllers; use App\Factory\ExpenseCategoryFactory; +use App\Filters\ExpenseCategoryFilters; use App\Http\Requests\ExpenseCategory\CreateExpenseCategoryRequest; use App\Http\Requests\ExpenseCategory\DestroyExpenseCategoryRequest; use App\Http\Requests\ExpenseCategory\EditExpenseCategoryRequest; use App\Http\Requests\ExpenseCategory\ShowExpenseCategoryRequest; use App\Http\Requests\ExpenseCategory\StoreExpenseCategoryRequest; use App\Http\Requests\ExpenseCategory\UpdateExpenseCategoryRequest; +use App\Models\Expense; use App\Models\ExpenseCategory; use App\Repositories\BaseRepository; use App\Transformers\ExpenseCategoryTransformer; @@ -79,13 +81,15 @@ class ExpenseCategoryController extends BaseController * * @return Response */ - public function index() + public function index(ExpenseCategoryFilters $filters) { - $expense_categories = ExpenseCategory::scope(); + $expense_categories = ExpenseCategory::filter($filters); return $this->listResponse($expense_categories); } + + /** * Show the form for creating a new resource. * diff --git a/app/Http/Requests/BankTransaction/MatchBankTransactionRequest.php b/app/Http/Requests/BankTransaction/MatchBankTransactionRequest.php index 4306a31587..959e22dbb5 100644 --- a/app/Http/Requests/BankTransaction/MatchBankTransactionRequest.php +++ b/app/Http/Requests/BankTransaction/MatchBankTransactionRequest.php @@ -67,8 +67,9 @@ class MatchBankTransactionRequest extends Request $p = Payment::withTrashed()->find($inputs['transactions'][$key]['payment_id']); /*Ensure we don't relink an existing payment*/ - if(!$p || $p->transaction_id) - $inputs['transactions'][$key]['payment_id'] = null; + if(!$p || $p->transaction_id){ + unset($inputs['transactions'][$key]); + } } diff --git a/app/Jobs/Bank/MatchBankTransactions.php b/app/Jobs/Bank/MatchBankTransactions.php index bc6473d0d8..1b75f85707 100644 --- a/app/Jobs/Bank/MatchBankTransactions.php +++ b/app/Jobs/Bank/MatchBankTransactions.php @@ -246,7 +246,6 @@ class MatchBankTransactions implements ShouldQueue if(!$this->bt || $this->bt->status_id == BankTransaction::STATUS_CONVERTED) return $this; - $expense = ExpenseFactory::create($this->bt->company_id, $this->bt->user_id); $expense->category_id = $this->resolveCategory($input); $expense->amount = $this->bt->amount; diff --git a/app/Models/ExpenseCategory.php b/app/Models/ExpenseCategory.php index 58bb959dbc..d281db3646 100644 --- a/app/Models/ExpenseCategory.php +++ b/app/Models/ExpenseCategory.php @@ -11,13 +11,15 @@ namespace App\Models; +use App\Models\Filterable; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\SoftDeletes; class ExpenseCategory extends BaseModel { use SoftDeletes; - + use Filterable; + protected $fillable = [ 'name', 'color', diff --git a/app/Repositories/ExpenseRepository.php b/app/Repositories/ExpenseRepository.php index d8df5f6fba..bc2440c25c 100644 --- a/app/Repositories/ExpenseRepository.php +++ b/app/Repositories/ExpenseRepository.php @@ -16,6 +16,7 @@ use App\Factory\ExpenseFactory; use App\Libraries\Currency\Conversion\CurrencyApi; use App\Models\Expense; use App\Utils\Traits\GeneratesCounter; +use Carbon\Exceptions\InvalidFormatException; use Illuminate\Support\Carbon; use Illuminate\Database\QueryException; @@ -31,12 +32,12 @@ class ExpenseRepository extends BaseRepository /** * Saves the expense and its contacts. * - * @param array $data The data - * @param \App\Models\Expense $expense The expense + * @param array $data The data + * @param \App\Models\Expense $expense The expense * - * @return \App\Models\Expense|null expense Object + * @return \App\Models\Expense */ - public function save(array $data, Expense $expense): ?Expense + public function save(array $data, Expense $expense): Expense { $expense->fill($data); @@ -71,6 +72,12 @@ class ExpenseRepository extends BaseRepository ); } + /** + * @param mixed $data + * @param mixed $expense + * @return Expense + * @throws InvalidFormatException + */ public function processExchangeRates($data, $expense): Expense { if (array_key_exists('exchange_rate', $data) && isset($data['exchange_rate']) && $data['exchange_rate'] != 1) {