1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Livewire/InvoicesTable.php

102 lines
2.7 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
2023-12-13 17:52:49 +01:00
namespace App\Livewire;
use App\Libraries\MultiDB;
2022-12-23 01:39:41 +01:00
use App\Models\Company;
use App\Models\Invoice;
use App\Utils\Traits\WithSorting;
use Carbon\Carbon;
use Livewire\Component;
use Livewire\WithPagination;
class InvoicesTable extends Component
{
2024-01-14 05:05:00 +01:00
use WithPagination;
use WithSorting;
2022-12-23 01:39:41 +01:00
public int $per_page = 10;
2022-12-23 01:39:41 +01:00
public array $status = [];
2022-12-23 01:39:41 +01:00
public Company $company;
public int $company_id;
public string $db;
2021-04-10 12:48:51 +02:00
public function mount()
{
2022-12-23 01:39:41 +01:00
MultiDB::setDb($this->db);
$this->company = Company::find($this->company_id);
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
}
public function render()
{
$local_status = [];
$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)
2023-02-04 07:09:04 +01:00
->where('is_proforma', false)
2022-09-16 06:20:46 +02:00
->with('client.gateway_tokens', 'client.contacts')
->orderBy($this->sort_field, $this->sort_asc ? 'asc' : 'desc');
if (in_array('paid', $this->status)) {
$local_status[] = Invoice::STATUS_PAID;
}
if (in_array('unpaid', $this->status)) {
$local_status[] = Invoice::STATUS_SENT;
$local_status[] = Invoice::STATUS_PARTIAL;
}
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
}
if (in_array('overdue', $this->status)) {
$query = $query->where(function ($query) {
$query
->orWhere('due_date', '<', Carbon::now())
->orWhere('partial_due_date', '<', Carbon::now());
});
}
2020-05-14 03:05:18 +02:00
$query = $query
->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);
2023-08-07 00:23:13 +02:00
/** @var \App\Models\ClientContact $client_contact */
$client_contact = auth()->user();
return render('components.livewire.invoices-table', [
'invoices' => $query,
2023-08-07 00:23:13 +02:00
'gateway_available' => ! empty($client_contact->client->service()->getPaymentMethods(-1)),
]);
}
}