invoiceninja/app/Filters/QuoteFilters.php

178 lines
4.7 KiB
PHP
Raw 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
*
2022-04-27 13:20:41 +10:00
* @copyright Copyright (c) 2022. 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 App\Models\User;
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
*/
public function filter(string $filter = '') : Builder
{
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) {
2022-11-10 17:28:00 +11:00
$query->where('quotes.number', 'like', '%'.$filter.'%')
->orwhere('quotes.custom_value1', 'like', '%'.$filter.'%')
->orWhere('quotes.custom_value2', 'like', '%'.$filter.'%')
->orWhere('quotes.custom_value3', 'like', '%'.$filter.'%')
->orWhere('quotes.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
*/
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;
}
if (in_array('draft', $status_parameters)) {
$this->builder->where('status_id', Quote::STATUS_DRAFT);
}
if (in_array('sent', $status_parameters)) {
$this->builder->where('status_id', Quote::STATUS_SENT);
}
if (in_array('approved', $status_parameters)) {
$this->builder->where('status_id', Quote::STATUS_APPROVED);
}
if (in_array('expired', $status_parameters)) {
$this->builder->where('status_id', Quote::STATUS_SENT)
2022-12-12 10:05:14 +11:00
->where('due_date', '>=', now()->toDateString());
}
if (in_array('upcoming', $status_parameters)) {
$this->builder->where('status_id', Quote::STATUS_SENT)
->where('due_date', '<=', now()->toDateString())
->orderBy('due_date', 'DESC');
2022-11-19 10:20:25 +11:00
}
return $this->builder;
}
2019-05-02 21:07:38 +10:00
/**
* Filters the list based on the status
* archived, active, deleted.
*
2020-10-28 21:10:49 +11:00
* @param string filter
* @return Builder
2019-05-02 21:07:38 +10:00
*/
public function status(string $filter = '') : Builder
{
if (strlen($filter) == 0) {
2019-05-02 21:07:38 +10:00
return $this->builder;
}
2019-05-02 21:07:38 +10:00
$table = 'quotes';
$filters = explode(',', $filter);
return $this->builder->where(function ($query) use ($filters, $table) {
$query->whereNull($table.'.id');
2019-05-02 21:07:38 +10:00
if (in_array(parent::STATUS_ACTIVE, $filters)) {
$query->orWhereNull($table.'.deleted_at');
2019-05-02 21:07:38 +10:00
}
if (in_array(parent::STATUS_ARCHIVED, $filters)) {
$query->orWhere(function ($query) use ($table) {
$query->whereNotNull($table.'.deleted_at');
2019-05-02 21:07:38 +10:00
if (! in_array($table, ['users'])) {
$query->where($table.'.is_deleted', '=', 0);
2019-05-02 21:07:38 +10:00
}
});
}
if (in_array(parent::STATUS_DELETED, $filters)) {
$query->orWhere($table.'.is_deleted', '=', 1);
2019-05-02 21:07:38 +10:00
}
});
}
public function number($number = '')
{
return $this->builder->where('number', 'like', '%'.$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
{
$sort_col = explode('|', $sort);
2022-07-11 07:15:06 +10:00
if($sort_col[0] == 'valid_until')
$sort_col[0] = 'due_date';
2019-05-02 21:07:38 +10:00
return $this->builder->orderBy($sort_col[0], $sort_col[1]);
}
/**
* Returns the base query.
*
2020-10-28 21:10:49 +11:00
* @param int company_id
* @param User $user
* @return Builder
2019-05-02 21:07:38 +10:00
* @deprecated
*/
public function baseQuery(int $company_id, User $user) : Builder
{
}
/**
* Filters the query by the users company ID.
*
2019-05-02 21:07:38 +10:00
* @return Illuminate\Database\Query\Builder
*/
public function entityFilter()
2019-05-02 21:07:38 +10:00
{
return $this->builder->company();
2019-05-02 21:07:38 +10:00
}
}