- Disable branch selection in sale form when creating from client context - Pass client ID to create pages via URL parameter - Update breadcrumbs to reflect client navigation path - Simplify relation managers by reusing resource tables and adding custom create actions
95 lines
2.6 KiB
PHP
95 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\ExpenseResource\Pages;
|
|
|
|
use App\Actions\Transactions\CreateTransactionAction;
|
|
use App\DataObjects\CreateTransactionDTO;
|
|
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 Illuminate\Support\Facades\Pipeline;
|
|
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
|
|
{
|
|
return Arr::except($data, ['client', 'transactions']);
|
|
}
|
|
|
|
protected function afterCreate(): void
|
|
{
|
|
$transactions = $this->form->getState()['transactions'] ?? [];
|
|
|
|
try {
|
|
$branch = $this->getRecord()->branch;
|
|
|
|
foreach ($transactions as $transaction) {
|
|
|
|
$data = [
|
|
'branch_id' => $branch->id,
|
|
'happened_on' => $this->getRecord()->happened_on,
|
|
...$transaction,
|
|
];
|
|
|
|
$payload = new CreateTransactionDTO(data: $data, transactionable: $this->getRecord());
|
|
|
|
Pipeline::send(passable: $payload)->through(
|
|
[
|
|
CreateTransactionAction::class,
|
|
]
|
|
)->thenReturn();
|
|
}
|
|
|
|
$this->commitDatabaseTransaction();
|
|
} catch (Exception $exception) {
|
|
$this->rollBackDatabaseTransaction();
|
|
throw new LogicException('Failed to save transactions : '.$exception->getMessage());
|
|
}
|
|
}
|
|
}
|