1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00

Prevent relinking of payments with transactions

This commit is contained in:
David Bomba 2022-12-16 09:38:02 +11:00
parent 8170831b73
commit e2439b60c3
6 changed files with 147 additions and 10 deletions

View File

@ -0,0 +1,124 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Filters;
use App\Models\Expense;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
/**
* ExpenseCategoryFilters.
*/
class ExpenseCategoryFilters extends QueryFilters
{
/**
* Filter based on search text.
*
* @param string query filter
* @return Builder
* @deprecated
*/
public function filter(string $filter = '') : Builder
{
if (strlen($filter) == 0) {
return $this->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();
}
}

View File

@ -12,12 +12,14 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Factory\ExpenseCategoryFactory; use App\Factory\ExpenseCategoryFactory;
use App\Filters\ExpenseCategoryFilters;
use App\Http\Requests\ExpenseCategory\CreateExpenseCategoryRequest; use App\Http\Requests\ExpenseCategory\CreateExpenseCategoryRequest;
use App\Http\Requests\ExpenseCategory\DestroyExpenseCategoryRequest; use App\Http\Requests\ExpenseCategory\DestroyExpenseCategoryRequest;
use App\Http\Requests\ExpenseCategory\EditExpenseCategoryRequest; use App\Http\Requests\ExpenseCategory\EditExpenseCategoryRequest;
use App\Http\Requests\ExpenseCategory\ShowExpenseCategoryRequest; use App\Http\Requests\ExpenseCategory\ShowExpenseCategoryRequest;
use App\Http\Requests\ExpenseCategory\StoreExpenseCategoryRequest; use App\Http\Requests\ExpenseCategory\StoreExpenseCategoryRequest;
use App\Http\Requests\ExpenseCategory\UpdateExpenseCategoryRequest; use App\Http\Requests\ExpenseCategory\UpdateExpenseCategoryRequest;
use App\Models\Expense;
use App\Models\ExpenseCategory; use App\Models\ExpenseCategory;
use App\Repositories\BaseRepository; use App\Repositories\BaseRepository;
use App\Transformers\ExpenseCategoryTransformer; use App\Transformers\ExpenseCategoryTransformer;
@ -79,13 +81,15 @@ class ExpenseCategoryController extends BaseController
* *
* @return Response * @return Response
*/ */
public function index() public function index(ExpenseCategoryFilters $filters)
{ {
$expense_categories = ExpenseCategory::scope(); $expense_categories = ExpenseCategory::filter($filters);
return $this->listResponse($expense_categories); return $this->listResponse($expense_categories);
} }
/** /**
* Show the form for creating a new resource. * Show the form for creating a new resource.
* *

View File

@ -67,8 +67,9 @@ class MatchBankTransactionRequest extends Request
$p = Payment::withTrashed()->find($inputs['transactions'][$key]['payment_id']); $p = Payment::withTrashed()->find($inputs['transactions'][$key]['payment_id']);
/*Ensure we don't relink an existing payment*/ /*Ensure we don't relink an existing payment*/
if(!$p || $p->transaction_id) if(!$p || $p->transaction_id){
$inputs['transactions'][$key]['payment_id'] = null; unset($inputs['transactions'][$key]);
}
} }

View File

@ -246,7 +246,6 @@ class MatchBankTransactions implements ShouldQueue
if(!$this->bt || $this->bt->status_id == BankTransaction::STATUS_CONVERTED) if(!$this->bt || $this->bt->status_id == BankTransaction::STATUS_CONVERTED)
return $this; return $this;
$expense = ExpenseFactory::create($this->bt->company_id, $this->bt->user_id); $expense = ExpenseFactory::create($this->bt->company_id, $this->bt->user_id);
$expense->category_id = $this->resolveCategory($input); $expense->category_id = $this->resolveCategory($input);
$expense->amount = $this->bt->amount; $expense->amount = $this->bt->amount;

View File

@ -11,12 +11,14 @@
namespace App\Models; namespace App\Models;
use App\Models\Filterable;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
class ExpenseCategory extends BaseModel class ExpenseCategory extends BaseModel
{ {
use SoftDeletes; use SoftDeletes;
use Filterable;
protected $fillable = [ protected $fillable = [
'name', 'name',

View File

@ -16,6 +16,7 @@ use App\Factory\ExpenseFactory;
use App\Libraries\Currency\Conversion\CurrencyApi; use App\Libraries\Currency\Conversion\CurrencyApi;
use App\Models\Expense; use App\Models\Expense;
use App\Utils\Traits\GeneratesCounter; use App\Utils\Traits\GeneratesCounter;
use Carbon\Exceptions\InvalidFormatException;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Database\QueryException; use Illuminate\Database\QueryException;
@ -31,12 +32,12 @@ class ExpenseRepository extends BaseRepository
/** /**
* Saves the expense and its contacts. * Saves the expense and its contacts.
* *
* @param array $data The data * @param array $data The data
* @param \App\Models\Expense $expense The expense * @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); $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 public function processExchangeRates($data, $expense): Expense
{ {
if (array_key_exists('exchange_rate', $data) && isset($data['exchange_rate']) && $data['exchange_rate'] != 1) { if (array_key_exists('exchange_rate', $data) && isset($data['exchange_rate']) && $data['exchange_rate'] != 1) {