invoiceninja/app/Filters/PaymentFilters.php

179 lines
4.9 KiB
PHP
Raw Normal View History

2019-05-03 17:57:55 +10:00
<?php
2019-05-11 13:32:07 +10:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 13:32:07 +10:00
*
* @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)
2019-05-11 13:32:07 +10:00
*
2021-06-16 16:58:16 +10:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 13:32:07 +10:00
*/
2019-05-03 17:57:55 +10:00
namespace App\Filters;
2023-02-21 10:44:54 +11:00
use App\Models\Payment;
2019-05-03 17:57:55 +10:00
use Illuminate\Database\Eloquent\Builder;
/**
* PaymentFilters.
2019-05-03 17:57:55 +10:00
*/
class PaymentFilters extends QueryFilters
{
/**
* Filter based on search text.
*
2020-10-28 21:10:49 +11:00
* @param string query filter
* @return Builder
2019-05-03 17:57:55 +10:00
* @deprecated
*/
2023-01-22 19:24:35 +11:00
public function filter(string $filter = ''): Builder
2019-09-11 13:46:23 +10:00
{
if (strlen($filter) == 0) {
2019-05-03 17:57:55 +10:00
return $this->builder;
}
2019-05-03 17:57:55 +10:00
return $this->builder->where(function ($query) use ($filter) {
2023-01-22 19:24:35 +11:00
$query->where('amount', 'like', '%'.$filter.'%')
->orWhere('date', 'like', '%'.$filter.'%')
->orWhere('custom_value1', 'like', '%'.$filter.'%')
->orWhere('custom_value2', 'like', '%'.$filter.'%')
->orWhere('custom_value3', 'like', '%'.$filter.'%')
->orWhere('custom_value4', 'like', '%'.$filter.'%');
});
2019-05-03 17:57:55 +10:00
}
2023-02-21 10:44:54 +11:00
/**
* Filter based on client status.
*
* Statuses we need to handle
* - all
* - pending
* - cancelled
* - failed
* - completed
* - partially refunded
* - refunded
*
* @param string client_status The payment status as seen by the client
* @return Builder
*/
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;
}
$this->builder->where(function ($query) use ($status_parameters) {
$payment_filters = [];
if (in_array('pending', $status_parameters)) {
$payment_filters[] = Payment::STATUS_PENDING;
}
if (in_array('cancelled', $status_parameters)) {
$payment_filters[] = Payment::STATUS_CANCELLED;
}
if (in_array('failed', $status_parameters)) {
$payment_filters[] = Payment::STATUS_FAILED;
}
if (in_array('completed', $status_parameters)) {
$payment_filters[] = Payment::STATUS_COMPLETED;
}
if (in_array('partially_refunded', $status_parameters)) {
$payment_filters[] = Payment::STATUS_PARTIALLY_REFUNDED;
}
if (in_array('refunded', $status_parameters)) {
$payment_filters[] = Payment::STATUS_REFUNDED;
}
if (count($payment_filters) >0) {
$query->whereIn('status_id', $payment_filters);
}
});
return $this->builder;
}
/**
* Returns a list of payments that can be matched to bank transactions
*/
2023-01-22 19:24:35 +11:00
public function match_transactions($value = 'true'): Builder
{
2023-02-16 12:36:09 +11:00
if ($value == 'true') {
2022-12-16 10:29:19 +11:00
return $this->builder
2023-02-16 12:36:09 +11:00
->where('is_deleted', 0)
->where(function ($query) {
2022-12-16 10:29:19 +11:00
$query->whereNull('transaction_id')
2023-02-16 12:36:09 +11:00
->orWhere("transaction_id", "")
2023-01-16 10:05:44 +11:00
->company();
2022-12-16 10:29:19 +11:00
});
}
return $this->builder;
}
2023-01-30 11:13:44 +01:00
public function number(string $number = ''): Builder
{
if (strlen($number) == 0) {
return $this->builder;
}
return $this->builder->where('number', $number);
}
2019-05-03 17:57:55 +10:00
/**
* Sorts the list based on $sort.
*
2020-10-28 21:10:49 +11:00
* @param string sort formatted as column|asc
* @return Builder
2019-05-03 17:57:55 +10:00
*/
public function sort(string $sort = ''): Builder
2019-05-03 17:57:55 +10:00
{
$sort_col = explode('|', $sort);
2023-01-30 11:06:01 +01:00
if (!is_array($sort_col) || count($sort_col) != 2) {
return $this->builder;
}
2023-01-30 11:06:01 +01:00
return $this->builder->orderBy($sort_col[0], $sort_col[1]);
2019-05-03 17:57:55 +10:00
}
/**
* Filters the query by the users company ID.
*
2019-05-03 17:57:55 +10:00
* @return Illuminate\Database\Query\Builder
*/
2023-01-22 19:24:35 +11:00
public function entityFilter(): Builder
2019-05-03 17:57:55 +10:00
{
if (auth()->guard('contact')->user()) {
2019-08-16 15:20:28 +10:00
return $this->contactViewFilter();
} else {
return $this->builder->company();
}
2019-05-03 17:57:55 +10:00
}
2019-08-16 15:20:28 +10:00
/**
* We need additional filters when showing invoices for the
* client portal. Need to automatically exclude drafts and cancelled invoices.
*
2020-10-28 21:10:49 +11:00
* @return Builder
2019-08-16 15:20:28 +10:00
*/
2023-01-22 19:24:35 +11:00
private function contactViewFilter(): Builder
2019-08-16 15:20:28 +10:00
{
return $this->builder
->whereCompanyId(auth()->guard('contact')->user()->company->id)
2019-08-16 15:20:28 +10:00
->whereIsDeleted(false);
}
}