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=3' => 'Sales', $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); } protected function handleRecordCreation(array $data): Model { $transactions = $this->data['transactions'] ?? []; return $this->processCreate($data, $transactions); } public function getFormDataMutation(array $data): array { return Arr::except($data, ['client', 'transactions', 'with_discount']); } public function processCreate(array $data, array $transactions): Model { try { DB::beginTransaction(); $record = Sale::create($data); $branch = $record->branch; foreach ($transactions as $transaction) { $tData = [ 'branch_id' => $branch->id, 'happened_on' => $record->happened_on, ...$transaction, ]; $payload = new CreateTransactionDTO(data: $tData, transactionable: $record); Pipeline::send(passable: $payload)->through( [ CreateTransactionAction::class, ] )->thenReturn(); } DB::commit(); } catch (\Exception $exception) { DB::rollBack(); throw new \Exception('Failed to save transactions : '.$exception->getMessage()); } return $record; } protected function afterCreate(): void { $branch = Branch::find($this->data['branch_id']); } }