invoiceninja/app/Filters/QuoteFilters.php

152 lines
4.1 KiB
PHP
Raw Permalink Normal View History

2019-05-02 21:07:38 +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-02 21:07:38 +10:00
namespace App\Filters;
2022-11-19 10:20:25 +11:00
use App\Models\Quote;
2019-05-02 21:07:38 +10:00
use Illuminate\Database\Eloquent\Builder;
/**
* QuoteFilters.
2019-05-02 21:07:38 +10:00
*/
class QuoteFilters extends QueryFilters
{
/**
* Filter based on search text.
*
2020-10-28 21:10:49 +11:00
* @param string query filter
* @return Builder
2019-05-02 21:07:38 +10:00
* @deprecated
*/
2023-01-22 19:24:35 +11:00
public function filter(string $filter = ''): Builder
2019-05-02 21:07:38 +10:00
{
if (strlen($filter) == 0) {
2019-05-02 21:07:38 +10:00
return $this->builder;
}
2019-05-02 21:07:38 +10:00
return $this->builder->where(function ($query) use ($filter) {
2023-01-22 19:24:35 +11:00
$query->where('number', 'like', '%'.$filter.'%')
->orwhere('custom_value1', 'like', '%'.$filter.'%')
->orWhere('custom_value2', 'like', '%'.$filter.'%')
->orWhere('custom_value3', 'like', '%'.$filter.'%')
->orWhere('custom_value4', 'like', '%'.$filter.'%');
});
2019-05-02 21:07:38 +10:00
}
2022-11-19 10:20:25 +11:00
/**
* Filter based on client status.
*
* Statuses we need to handle
* - all
* - active
* - paused
* - completed
*
* @param string client_status The invoice status as seen by the client
* @return Builder
*/
2023-01-22 19:24:35 +11:00
public function client_status(string $value = ''): Builder
2022-11-19 10:20:25 +11:00
{
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) {
2023-01-16 10:05:44 +11:00
if (in_array('sent', $status_parameters)) {
2023-02-16 12:36:09 +11:00
$query->orWhere(function ($q) {
$q->where('status_id', Quote::STATUS_SENT)
->whereNull('due_date')
->orWhere('due_date', '>=', now()->toDateString());
});
2023-01-16 10:05:44 +11:00
}
2023-01-16 10:38:52 +11:00
$quote_filters = [];
2019-05-02 21:07:38 +10:00
2023-01-16 10:38:52 +11:00
if (in_array('draft', $status_parameters)) {
$quote_filters[] = Quote::STATUS_DRAFT;
}
2019-05-02 21:07:38 +10:00
2023-01-16 10:38:52 +11:00
2023-01-16 10:05:44 +11:00
if (in_array('approved', $status_parameters)) {
2023-01-16 10:38:52 +11:00
$quote_filters[] = Quote::STATUS_APPROVED;
2019-05-02 21:07:38 +10:00
}
2023-02-16 12:36:09 +11:00
if (count($quote_filters) >0) {
2023-01-16 10:38:52 +11:00
$query->orWhereIn('status_id', $quote_filters);
}
2019-05-02 21:07:38 +10:00
2023-01-16 10:05:44 +11:00
if (in_array('expired', $status_parameters)) {
2023-02-16 12:36:09 +11:00
$query->orWhere(function ($q) {
$q->where('status_id', Quote::STATUS_SENT)
->whereNotNull('due_date')
->where('due_date', '<=', now()->toDateString());
});
2019-05-02 21:07:38 +10:00
}
2023-01-16 10:05:44 +11:00
if (in_array('upcoming', $status_parameters)) {
2023-02-16 12:36:09 +11:00
$query->orWhere(function ($q) {
$q->where('status_id', Quote::STATUS_SENT)
->where('due_date', '>=', now()->toDateString())
->orderBy('due_date', 'DESC');
});
2019-05-02 21:07:38 +10:00
}
});
2023-01-16 10:05:44 +11:00
return $this->builder;
2019-05-02 21:07:38 +10:00
}
2023-01-22 19:24:35 +11:00
public function number($number = ''): Builder
{
2023-01-30 10:36:13 +01:00
if (strlen($number) == 0) {
return $this->builder;
}
return $this->builder->where('number', $number);
}
2019-05-02 21:07:38 +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-02 21:07:38 +10:00
*/
public function sort(string $sort = ''): Builder
2019-05-02 21:07:38 +10:00
{
$sort_col = explode('|', $sort);
if (!is_array($sort_col) || count($sort_col) != 2) {
return $this->builder;
}
2023-02-16 12:36:09 +11:00
if ($sort_col[0] == 'valid_until') {
2022-07-11 07:15:06 +10:00
$sort_col[0] = 'due_date';
2023-02-16 12:36:09 +11:00
}
2022-07-11 07:15:06 +10:00
2019-05-02 21:07:38 +10:00
return $this->builder->orderBy($sort_col[0], $sort_col[1]);
}
/**
* Filters the query by the users company ID.
*
2023-01-22 19:24:35 +11:00
* @return Illuminate\Eloquent\Query\Builder
2019-05-02 21:07:38 +10:00
*/
2023-01-22 19:24:35 +11:00
public function entityFilter(): Builder
2019-05-02 21:07:38 +10:00
{
return $this->builder->company();
2019-05-02 21:07:38 +10:00
}
}