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

124 lines
2.9 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
*
* @copyright Copyright (c) 2021. 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;
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()
{
return render('components.livewire.documents-table', [
2021-09-06 17:26:42 +02:00
'documents' => $this->query->orderBy($this->sort_field, $this->sort_asc ? 'asc' : 'desc')->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();
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: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);
});
}
2020-08-14 17:29:26 +02:00
}