2020-04-23 00:49:23 +02:00
|
|
|
<?php
|
|
|
|
|
2020-12-05 14:24:21 +01:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-12-05 14:24:21 +01:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
2020-04-23 00:49:23 +02:00
|
|
|
namespace App\Http\Livewire;
|
|
|
|
|
2021-06-07 03:06:31 +02:00
|
|
|
use App\Libraries\MultiDB;
|
2020-04-23 00:49:23 +02:00
|
|
|
use App\Models\Invoice;
|
2020-05-04 13:13:46 +02:00
|
|
|
use App\Utils\Traits\WithSorting;
|
2020-04-25 01:17:37 +02:00
|
|
|
use Carbon\Carbon;
|
2020-04-23 00:49:23 +02:00
|
|
|
use Livewire\Component;
|
|
|
|
use Livewire\WithPagination;
|
|
|
|
|
|
|
|
class InvoicesTable extends Component
|
|
|
|
{
|
|
|
|
use WithPagination, WithSorting;
|
|
|
|
|
|
|
|
public $per_page = 10;
|
|
|
|
|
|
|
|
public $status = [];
|
|
|
|
|
2021-06-07 03:06:31 +02:00
|
|
|
public $company;
|
|
|
|
|
2021-04-10 12:48:51 +02:00
|
|
|
public function mount()
|
|
|
|
{
|
2021-06-07 03:06:31 +02:00
|
|
|
MultiDB::setDb($this->company->db);
|
|
|
|
|
2021-04-10 12:48:51 +02:00
|
|
|
$this->sort_asc = false;
|
2021-04-19 16:57:25 +02:00
|
|
|
|
|
|
|
$this->sort_field = 'date';
|
2021-04-10 12:48:51 +02:00
|
|
|
}
|
|
|
|
|
2020-04-23 00:49:23 +02:00
|
|
|
public function render()
|
|
|
|
{
|
2020-09-03 11:45:47 +02:00
|
|
|
$local_status = [];
|
|
|
|
|
2020-05-09 00:21:35 +02:00
|
|
|
$query = Invoice::query()
|
2021-05-11 14:03:44 +02:00
|
|
|
->orderBy($this->sort_field, $this->sort_asc ? 'asc' : 'desc')
|
|
|
|
->where('is_deleted', false);
|
2020-04-23 00:49:23 +02:00
|
|
|
|
2020-04-25 01:17:37 +02:00
|
|
|
if (in_array('paid', $this->status)) {
|
2020-09-03 11:45:47 +02:00
|
|
|
$local_status[] = Invoice::STATUS_PAID;
|
2020-04-25 01:17:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array('unpaid', $this->status)) {
|
2020-09-03 11:45:47 +02:00
|
|
|
$local_status[] = Invoice::STATUS_SENT;
|
|
|
|
$local_status[] = Invoice::STATUS_PARTIAL;
|
2020-04-25 01:17:37 +02:00
|
|
|
}
|
2020-04-23 00:49:23 +02:00
|
|
|
|
2020-09-03 11:45:47 +02:00
|
|
|
if (in_array('overdue', $this->status)) {
|
|
|
|
$local_status[] = Invoice::STATUS_SENT;
|
|
|
|
$local_status[] = Invoice::STATUS_PARTIAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($local_status) > 0) {
|
|
|
|
$query = $query->whereIn('status_id', array_unique($local_status));
|
2020-09-02 22:08:12 +02:00
|
|
|
}
|
|
|
|
|
2020-04-25 01:17:37 +02:00
|
|
|
if (in_array('overdue', $this->status)) {
|
2020-09-03 11:45:47 +02:00
|
|
|
$query = $query->where(function ($query) {
|
|
|
|
$query
|
|
|
|
->orWhere('due_date', '<', Carbon::now())
|
|
|
|
->orWhere('partial_due_date', '<', Carbon::now());
|
|
|
|
});
|
2020-04-23 00:49:23 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 03:05:18 +02:00
|
|
|
$query = $query
|
2020-06-10 15:33:53 +02:00
|
|
|
->where('client_id', auth('contact')->user()->client->id)
|
2020-07-23 12:17:09 +02:00
|
|
|
->where('status_id', '<>', Invoice::STATUS_DRAFT)
|
2021-04-10 10:56:48 +02:00
|
|
|
->withTrashed()
|
2020-05-14 03:05:18 +02:00
|
|
|
->paginate($this->per_page);
|
2020-04-23 00:49:23 +02:00
|
|
|
|
|
|
|
return render('components.livewire.invoices-table', [
|
2020-04-25 01:17:37 +02:00
|
|
|
'invoices' => $query,
|
2020-04-23 00:49:23 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|