1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Http/Livewire/DocumentsTable.php

181 lines
4.5 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
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. 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;
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;
public $client;
2020-08-14 17:29:26 +02:00
public $per_page = 10;
public $company;
2021-09-06 17:26:42 +02:00
public string $tab = 'documents';
protected $query;
public function mount($client)
{
MultiDB::setDb($this->company->db);
$this->client = $client;
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':
// $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
}