1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00
invoiceninja/app/Http/Livewire/DocumentsTable.php

188 lines
4.7 KiB
PHP
Raw Normal View History

2020-08-14 17:29:26 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
2020-08-14 17:29:26 +02:00
namespace App\Http\Livewire;
use App\Libraries\MultiDB;
2020-10-28 11:10:49 +01:00
use App\Models\Client;
2022-12-23 01:33:14 +01:00
use App\Models\Company;
2021-09-06 17:26:42 +02:00
use App\Models\Credit;
use App\Models\Document;
2021-09-06 17:28:04 +02:00
use App\Models\Expense;
2021-09-06 17:34:27 +02:00
use App\Models\Invoice;
2021-09-06 17:35:15 +02:00
use App\Models\Payment;
2021-09-06 17:36:22 +02:00
use App\Models\Project;
2021-09-06 17:36:58 +02:00
use App\Models\Quote;
2021-09-06 17:38:45 +02:00
use App\Models\RecurringInvoice;
2021-09-06 17:39:25 +02:00
use App\Models\Task;
2020-08-14 17:29:26 +02:00
use App\Utils\Traits\WithSorting;
use Livewire\Component;
use Livewire\WithPagination;
class DocumentsTable extends Component
2020-08-14 17:29:26 +02:00
{
use WithPagination, WithSorting;
2022-12-23 01:33:14 +01:00
public Company $company;
2022-12-23 01:33:14 +01:00
public Client $client;
2020-08-14 17:29:26 +02:00
2022-12-23 01:33:14 +01:00
public int $client_id;
public int $per_page = 10;
2021-09-06 17:26:42 +02:00
public string $tab = 'documents';
2022-12-23 01:33:14 +01:00
public string $db;
2021-09-06 17:26:42 +02:00
protected $query;
2022-12-23 01:33:14 +01:00
public function mount()
{
2022-12-23 01:33:14 +01:00
MultiDB::setDb($this->db);
$this->client = Client::withTrashed()->with('company')->find($this->client_id);
2022-12-23 01:33:14 +01:00
$this->company = $this->client->company;
2021-09-06 17:26:42 +02:00
$this->query = $this->documents();
}
2020-08-14 17:29:26 +02:00
public function render()
{
2022-07-28 00:29:18 +02:00
$this->updateResources(request()->tab ?: $this->tab);
return render('components.livewire.documents-table', [
2021-09-10 21:49:17 +02:00
'documents' => $this->query
->orderBy($this->sort_field, $this->sort_asc ? 'asc' : 'desc')
->withTrashed()
->paginate($this->per_page),
2020-08-14 17:29:26 +02:00
]);
}
2021-09-06 17:26:42 +02:00
public function updateResources(string $resource)
{
$this->tab = $resource;
switch ($resource) {
case 'documents':
$this->query = $this->documents();
break;
case 'credits':
$this->query = $this->credits();
break;
2021-09-06 17:28:04 +02:00
case 'expenses':
2023-02-16 02:36:09 +01:00
// $this->query = $this->expenses();
2021-09-06 17:28:04 +02:00
break;
2021-09-06 17:34:27 +02:00
case 'invoices':
$this->query = $this->invoices();
break;
2021-09-06 17:35:15 +02:00
case 'payments':
$this->query = $this->payments();
break;
2021-09-06 17:36:22 +02:00
case 'projects':
$this->query = $this->projects();
break;
2021-09-06 17:36:58 +02:00
case 'quotes':
$this->query = $this->quotes();
break;
2021-09-06 17:38:45 +02:00
case 'recurringInvoices':
$this->query = $this->recurringInvoices();
break;
2021-09-06 17:39:25 +02:00
case 'tasks':
$this->query = $this->tasks();
break;
2021-09-06 17:26:42 +02:00
default:
$this->query = $this->documents();
break;
}
}
protected function documents()
{
return $this->client->documents();
}
protected function credits()
{
return Document::query()
->whereHasMorph('documentable', [Credit::class], function ($query) {
$query->where('client_id', $this->client->id);
});
}
2021-09-06 17:28:04 +02:00
protected function expenses()
{
return Document::query()
->whereHasMorph('documentable', [Expense::class], function ($query) {
$query->where('client_id', $this->client->id);
});
}
2021-09-06 17:34:27 +02:00
protected function invoices()
{
return Document::query()
->whereHasMorph('documentable', [Invoice::class], function ($query) {
$query->where('client_id', $this->client->id);
});
}
2021-09-06 17:35:15 +02:00
protected function payments()
{
return Document::query()
->whereHasMorph('documentable', [Payment::class], function ($query) {
$query->where('client_id', $this->client->id);
});
}
2021-09-06 17:36:22 +02:00
protected function projects()
{
return Document::query()
->whereHasMorph('documentable', [Project::class], function ($query) {
$query->where('client_id', $this->client->id);
});
}
2021-09-06 17:36:58 +02:00
protected function quotes()
{
return Document::query()
->whereHasMorph('documentable', [Quote::class], function ($query) {
$query->where('client_id', $this->client->id);
});
}
2021-09-06 17:38:45 +02:00
protected function recurringInvoices()
{
return Document::query()
->whereHasMorph('documentable', [RecurringInvoice::class], function ($query) {
$query->where('client_id', $this->client->id);
});
}
2021-09-06 17:39:25 +02:00
protected function tasks()
{
return Document::query()
->whereHasMorph('documentable', [Task::class], function ($query) {
$query->where('client_id', $this->client->id);
});
}
2020-08-14 17:29:26 +02:00
}