invoiceninja/app/Filters/BankTransactionFilters.php

161 lines
4 KiB
PHP
Raw Normal View History

2022-11-11 10:30:53 +11:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-29 09:21:40 +11:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2022-11-11 10:30:53 +11:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Filters;
use App\Models\BankTransaction;
use Illuminate\Database\Eloquent\Builder;
/**
* BankTransactionFilters.
*/
class BankTransactionFilters extends QueryFilters
{
/**
* Filter by name.
*
* @param string $name
* @return Builder
*/
public function name(string $name = ''): Builder
{
2023-01-30 11:13:44 +01:00
if (strlen($name) == 0) {
2023-01-30 10:46:05 +01:00
return $this->builder;
}
return $this->builder->where('bank_account_name', 'like', '%'.$name.'%');
2022-11-11 10:30:53 +11:00
}
/**
* Filter based on search text.
*
* @param string query filter
* @return Builder
* @deprecated
*/
2023-01-30 07:08:26 +01:00
public function filter(string $filter = ''): Builder
2022-11-11 10:30:53 +11:00
{
if (strlen($filter) == 0) {
return $this->builder;
}
return $this->builder->where(function ($query) use ($filter) {
$query->where('bank_transactions.description', 'like', '%'.$filter.'%');
});
}
/**
* Filter based on client status.
*
* Statuses we need to handle
* - all
* - unmatched
* - matched
* - converted
* - deposits
* - withdrawals
*
* @return Builder
*/
2023-01-30 07:08:26 +01:00
public function client_status(string $value = ''): Builder
{
if (strlen($value) == 0) {
return $this->builder;
}
$status_parameters = explode(',', $value);
if (in_array('all', $status_parameters)) {
return $this->builder;
}
2023-02-16 12:36:09 +11:00
$this->builder->where(function ($query) use ($status_parameters) {
$status_array = [];
$debit_or_withdrawal_array = [];
if (in_array('unmatched', $status_parameters)) {
$status_array[] = BankTransaction::STATUS_UNMATCHED;
}
2022-11-11 10:30:53 +11:00
if (in_array('matched', $status_parameters)) {
$status_array[] = BankTransaction::STATUS_MATCHED;
}
2022-11-11 10:30:53 +11:00
if (in_array('converted', $status_parameters)) {
$status_array[] = BankTransaction::STATUS_CONVERTED;
}
2022-11-11 10:30:53 +11:00
if (in_array('deposits', $status_parameters)) {
$debit_or_withdrawal_array[] = 'CREDIT';
2022-11-11 10:30:53 +11:00
}
if (in_array('withdrawals', $status_parameters)) {
$debit_or_withdrawal_array[] = 'DEBIT';
}
2022-11-11 10:30:53 +11:00
2023-02-16 12:36:09 +11:00
if (count($status_array) >=1) {
$query->whereIn('status_id', $status_array);
2022-11-11 10:30:53 +11:00
}
2023-02-16 12:36:09 +11:00
if (count($debit_or_withdrawal_array) >=1) {
$query->orWhereIn('base_type', $debit_or_withdrawal_array);
2022-11-11 10:30:53 +11:00
}
});
return $this->builder;
2022-11-11 10:30:53 +11:00
}
/**
* Sorts the list based on $sort.
*
* @param string sort formatted as column|asc
* @return Builder
*/
public function sort(string $sort = ''): Builder
2022-11-11 10:30:53 +11:00
{
$sort_col = explode('|', $sort);
2022-12-07 13:05:35 +11:00
if (!is_array($sort_col) || count($sort_col) != 2) {
2022-12-07 13:05:35 +11:00
return $this->builder;
}
2022-11-11 10:30:53 +11:00
2023-02-16 12:36:09 +11:00
if ($sort_col[0] == 'deposit') {
2022-12-07 13:05:35 +11:00
return $this->builder->where('base_type', 'CREDIT')->orderBy('amount', $sort_col[1]);
2023-02-16 12:36:09 +11:00
}
2022-12-07 13:05:35 +11:00
2023-02-16 12:36:09 +11:00
if ($sort_col[0] == 'withdrawal') {
2022-12-07 13:05:35 +11:00
return $this->builder->where('base_type', 'DEBIT')->orderBy('amount', $sort_col[1]);
2023-02-16 12:36:09 +11:00
}
2022-12-07 13:05:35 +11:00
2023-02-16 12:36:09 +11:00
if ($sort_col[0] == 'status') {
2022-12-07 13:05:35 +11:00
$sort_col[0] = 'status_id';
2023-02-16 12:36:09 +11:00
}
2022-12-07 13:05:35 +11:00
2023-02-16 12:36:09 +11:00
if (in_array($sort_col[0], ['invoices','expense'])) {
2022-12-07 13:05:35 +11:00
return $this->builder;
2023-02-16 12:36:09 +11:00
}
2022-12-07 13:05:35 +11:00
2022-11-11 10:30:53 +11:00
return $this->builder->orderBy($sort_col[0], $sort_col[1]);
}
/**
* Filters the query by the users company ID.
*
* @return Illuminate\Database\Query\Builder
*/
public function entityFilter()
{
return $this->builder->company();
}
}