Filament

 avatar
unknown
php
2 years ago
2.6 kB
17
Indexable
Action::make('Aprove')
                ->label('Aprovar')
                ->color('success')
                ->visible(auth()->user()->can('approve_process') && $this->record->status === ProcessStatus::PENDING)
                ->action(fn () => $this->record->aprove())
                ->requiresConfirmation()
                ->modalHeading('Aprovar processo')
                ->modalSubheading('Tem a certeza que deseja aprovar este processo?')
                ->modalButton('Confirmar'),


//Function from Process model

public function aprove(): void
    {
        if (! $this->hasPrivateDocs()) {
            Notification::make()
                ->title('Não é possivel aprovar um processo sem ficheiros')
                ->icon('heroicon-o-document-text')
                ->iconColor('success')
                ->send();

            return;
        }
        try {
            $mapping['customer_type'] = $this->customer_type;
            $mapping['invoice_amount'] = match (1) {
                $this->new_cmvm < 150 => '150',
                $this->new_cmvm < 250 => '150_250',
                $this->new_cmvm < 500 => '250_500',
                default => '500',
            };
            $mapping['provider'] = 'MEO';
            $mapping['product'] = $this->product->name;
            $mapping['contract_due_date'] = $this->end_date;
            $mapping['scheduled_contact'] = null;
            $mapping['customer_id'] = $this->customer_id;
            $mapping['created_at'] = now();
            $mapping['contact_name'] = $this->customer->name;
            $mapping['email'] = $this->customer->email;
            $mapping['phone'] = $this->customer->phone;
            $mapping['role'] = 'N/A';

            $this->mapping()->updateOrCreate(['service_number' => $this->service_number], $mapping);
            $this->status = ProcessStatus::APPROVED;
            $this->customer->depositFloat($this->subsidy, ['key' => 'Descrição', 'value' => "Subsidiação do processo #{$this->id}"]);
            $this->save();
            Notification::make()
                ->title('Processo aprovado com sucesso')
                ->icon('heroicon-o-document-text')
                ->iconColor('success')
                ->send();
        } catch (\Exception $e) { // I don't remember what exception it is specifically
            Notification::make()
                ->title('Ocorreu um erro ao aprovar o processo')
                ->icon('heroicon-o-document-text')
                ->iconColor('error')
                ->send();
        }
    }
Editor is loading...