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
|
|
|
|
*
|
2022-04-27 05:20:41 +02:00
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-12-05 14:24:21 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-12-05 14:24:21 +01:00
|
|
|
*/
|
|
|
|
|
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;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
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-07-03 23:46:25 +02:00
|
|
|
->where('company_id', $this->company->id)
|
2022-09-16 06:20:46 +02:00
|
|
|
->where('is_deleted', false)
|
|
|
|
->with('client.gateway_tokens', 'client.contacts')
|
|
|
|
->orderBy($this->sort_field, $this->sort_asc ? 'asc' : 'desc');
|
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
|
2022-02-18 06:38:15 +01:00
|
|
|
->where('client_id', auth()->guard('contact')->user()->client_id)
|
2022-09-16 07:29:48 +02:00
|
|
|
->where('status_id', '<>', Invoice::STATUS_DRAFT)
|
|
|
|
->where('status_id', '<>', Invoice::STATUS_CANCELLED)
|
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,
|
2022-06-21 11:57:17 +02:00
|
|
|
'gateway_available' => ! empty(auth()->user()->client->service()->getPaymentMethods(-1)),
|
2020-04-23 00:49:23 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|