upgrade to filament v4
This commit is contained in:
94
app/Filament/Resources/Expenses/Pages/CreateExpense.php
Normal file
94
app/Filament/Resources/Expenses/Pages/CreateExpense.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ExpenseResource\Pages;
|
||||
|
||||
use App\Actions\Transactions\CreateRecordTransactionsAction;
|
||||
use App\Filament\Resources\ClientResource;
|
||||
use App\Filament\Resources\ExpenseResource;
|
||||
use App\Models\Client;
|
||||
use Exception;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Illuminate\Support\Arr;
|
||||
use Symfony\Component\Console\Exception\LogicException;
|
||||
|
||||
class CreateExpense extends CreateRecord
|
||||
{
|
||||
protected static string $resource = ExpenseResource::class;
|
||||
|
||||
public ?int $clientId = null;
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
parent::mount();
|
||||
|
||||
$this->clientId = request()->integer('client_id');
|
||||
}
|
||||
|
||||
public function getBreadcrumbs(): array
|
||||
{
|
||||
$client = $this->getClient();
|
||||
|
||||
if (! $client) {
|
||||
return parent::getBreadcrumbs();
|
||||
}
|
||||
|
||||
return [
|
||||
ClientResource::getUrl('view', ['record' => $client->id]) => $client->company,
|
||||
ClientResource::getUrl('view', ['record' => $client->id]).'?activeRelationManager=4' => 'Expenses',
|
||||
$this->getResource()::getUrl('create', ['client_id' => $client->id]) => 'Create',
|
||||
];
|
||||
}
|
||||
|
||||
protected function getClient(): Client|null
|
||||
{
|
||||
if (! $this->clientId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Client::find($this->clientId);
|
||||
}
|
||||
|
||||
protected function mutateFormDataBeforeCreate(array $data): array
|
||||
{
|
||||
|
||||
return $this->getFormDataMutation($data);
|
||||
}
|
||||
|
||||
public function getFormDataMutation(array $data): array
|
||||
{
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
|
||||
$data['gross_amount'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['gross_amount'] ?? 0));
|
||||
$data['exempt'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['exempt'] ?? 0));
|
||||
$data['zero_rated'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['zero_rated'] ?? 0));
|
||||
$data['vatable_amount'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['vatable_amount'] ?? 0));
|
||||
$data['input_tax'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['input_tax'] ?? 0));
|
||||
$data['payable_withholding_tax'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['payable_withholding_tax'] ?? 0));
|
||||
$data['net_amount'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['net_amount'] ?? 0));
|
||||
|
||||
return Arr::except($data, ['client', 'transactions']);
|
||||
}
|
||||
|
||||
protected function afterCreate(): void
|
||||
{
|
||||
$transactions = $this->form->getState()['transactions'] ?? [];
|
||||
|
||||
try {
|
||||
app(CreateRecordTransactionsAction::class)($this->getRecord(), $transactions);
|
||||
|
||||
$accountIds = collect($transactions)
|
||||
->pluck('account_id')
|
||||
->filter()
|
||||
->unique()
|
||||
->values()
|
||||
->all();
|
||||
|
||||
$this->getRecord()->accounts()->sync($accountIds);
|
||||
|
||||
$this->commitDatabaseTransaction();
|
||||
} catch (Exception $exception) {
|
||||
$this->rollBackDatabaseTransaction();
|
||||
throw new LogicException('Failed to save transactions : '.$exception->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
27
app/Filament/Resources/Expenses/Pages/EditExpense.php
Normal file
27
app/Filament/Resources/Expenses/Pages/EditExpense.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ExpenseResource\Pages;
|
||||
|
||||
use App\Filament\Resources\ExpenseResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditExpense extends EditRecord
|
||||
{
|
||||
protected static string $resource = ExpenseResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function mutateFormDataBeforeFill(array $data): array
|
||||
{
|
||||
return [
|
||||
'client' => $this->getRecord()->branch->client->id,
|
||||
...$this->getRecord()->toArray(),
|
||||
];
|
||||
}
|
||||
}
|
||||
19
app/Filament/Resources/Expenses/Pages/ListExpenses.php
Normal file
19
app/Filament/Resources/Expenses/Pages/ListExpenses.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ExpenseResource\Pages;
|
||||
|
||||
use App\Filament\Resources\ExpenseResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListExpenses extends ListRecords
|
||||
{
|
||||
protected static string $resource = ExpenseResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user