| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 | <?php
namespace App\Filament\Company\Pages\Setting;
use App\Enums\{DocumentType, Font, PaymentTerms, Template};
use App\Models\Setting\DocumentDefault as InvoiceModel;
use Filament\Actions\{Action, ActionGroup};
use Filament\Forms\Components\{Checkbox, ColorPicker, Component, FileUpload, Group, Section, Select, TextInput, Textarea, ViewField};
use Filament\Forms\{Form, Get};
use Filament\Notifications\Notification;
use Filament\Pages\Concerns\InteractsWithFormActions;
use Filament\Pages\Page;
use Filament\Support\Exceptions\Halt;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
use function Filament\authorize;
/**
 * @property Form $form
 */
class Invoice extends Page
{
    use InteractsWithFormActions;
    protected static ?string $navigationIcon = 'heroicon-o-document-duplicate';
    protected static ?string $navigationLabel = 'Invoice';
    protected static ?string $navigationGroup = 'Settings';
    protected static ?string $slug = 'settings/invoice';
    protected ?string $heading = 'Invoice';
    protected static string $view = 'filament.company.pages.setting.invoice';
    public ?array $data = [];
    public ?InvoiceModel $record = null;
    public function mount(): void
    {
        $this->record = InvoiceModel::invoice()
            ->firstOrNew([
                'company_id' => auth()->user()->currentCompany->id,
                'type' => DocumentType::Invoice->value,
            ]);
        abort_unless(static::canView($this->record), 404);
        $this->fillForm();
    }
    public function fillForm(): void
    {
        $data = $this->record->attributesToArray();
        $data = $this->mutateFormDataBeforeFill($data);
        $this->form->fill($data);
    }
    protected function mutateFormDataBeforeFill(array $data): array
    {
        return $data;
    }
    protected function mutateFormDataBeforeSave(array $data): array
    {
        return $data;
    }
    public function save(): void
    {
        try {
            $data = $this->form->getState();
            $data = $this->mutateFormDataBeforeSave($data);
            $this->handleRecordUpdate($this->record, $data);
        } catch (Halt $exception) {
            return;
        }
        $this->getSavedNotification()?->send();
        if ($redirectUrl = $this->getRedirectUrl()) {
            $this->redirect($redirectUrl);
        }
    }
    protected function getSavedNotification(): ?Notification
    {
        $title = $this->getSavedNotificationTitle();
        if (blank($title)) {
            return null;
        }
        return Notification::make()
            ->success()
            ->title($this->getSavedNotificationTitle());
    }
    protected function getSavedNotificationTitle(): ?string
    {
        return __('filament-panels::pages/tenancy/edit-tenant-profile.notifications.saved.title');
    }
    protected function getRedirectUrl(): ?string
    {
        return null;
    }
    public function form(Form $form): Form
    {
        return $form
            ->schema([
                $this->getGeneralSection(),
                $this->getContentSection(),
                $this->getTemplateSection(),
            ])
            ->model($this->record)
            ->statePath('data')
            ->operation('edit');
    }
    protected function getGeneralSection(): Component
    {
        return Section::make('General')
            ->schema([
                TextInput::make('number_prefix')
                    ->label('Number Prefix')
                    ->live()
                    ->required(),
                Select::make('number_digits')
                    ->label('Number Digits')
                    ->options(InvoiceModel::availableNumberDigits())
                    ->native(false)
                    ->live()
                    ->required(),
                TextInput::make('number_next')
                    ->label('Next Number')
                    ->live()
                    ->maxLength(static fn (Get $get) => $get('number_digits'))
                    ->suffix(static function (Get $get, $state) {
                        $number_prefix = $get('number_prefix');
                        $number_digits = $get('number_digits');
                        $number_next = $state;
                        return InvoiceModel::getNumberNext(true, true, $number_prefix, $number_digits, $number_next);
                    })
                    ->required(),
                Select::make('payment_terms')
                    ->label('Payment Terms')
                    ->options(PaymentTerms::class)
                    ->native(false)
                    ->live()
                    ->required(),
            ])->columns();
    }
    protected function getContentSection(): Component
    {
        return Section::make('Content')
            ->schema([
                TextInput::make('header')
                    ->label('Header')
                    ->live()
                    ->required(),
                TextInput::make('subheader')
                    ->label('Subheader')
                    ->live()
                    ->nullable(),
                Textarea::make('terms')
                    ->label('Terms')
                    ->live()
                    ->nullable(),
                Textarea::make('footer')
                    ->label('Footer / Notes')
                    ->live()
                    ->nullable(),
            ])->columns();
    }
    protected function getTemplateSection(): Component
    {
        return Section::make('Template')
            ->description('Choose the template and edit the column names.')
            ->schema([
                Group::make()
                    ->live()
                    ->schema([
                        FileUpload::make('logo')
                            ->label('Logo')
                            ->disk('public')
                            ->directory('logos/document')
                            ->imageResizeMode('contain')
                            ->imagePreviewHeight('250')
                            ->imageCropAspectRatio('2:1')
                            ->getUploadedFileNameForStorageUsing(
                                static fn (TemporaryUploadedFile $file): string => (string) str($file->getClientOriginalName())
                                    ->prepend(Auth::user()->currentCompany->id . '_'),
                            )
                            ->openable()
                            ->maxSize(2048)
                            ->image()
                            ->visibility('public')
                            ->acceptedFileTypes(['image/png', 'image/jpeg']),
                        Checkbox::make('show_logo')
                            ->label('Show Logo'),
                        ColorPicker::make('accent_color')
                            ->label('Accent Color'),
                        Select::make('font')
                            ->label('Font')
                            ->native(false)
                            ->selectablePlaceholder(false)
                            ->rule('required')
                            ->allowHtml()
                            ->options(
                                collect(Font::cases())
                                    ->mapWithKeys(static fn ($case) => [
                                        $case->value => "<span style='font-family:{$case->getLabel()}'>{$case->getLabel()}</span>",
                                    ]),
                            ),
                        Select::make('template')
                            ->label('Template')
                            ->native(false)
                            ->options(Template::class)
                            ->required(),
                        Select::make('item_name.option')
                            ->label('Item Name')
                            ->native(false)
                            ->required()
                            ->options(InvoiceModel::getAvailableItemNameOptions()),
                        TextInput::make('item_name.custom')
                            ->hiddenLabel()
                            ->disabled(static fn (callable $get) => $get('item_name.option') !== 'other')
                            ->nullable(),
                        Select::make('unit_name.option')
                            ->label('Unit Name')
                            ->native(false)
                            ->required()
                            ->options(InvoiceModel::getAvailableUnitNameOptions()),
                        TextInput::make('unit_name.custom')
                            ->hiddenLabel()
                            ->disabled(static fn (callable $get) => $get('unit_name.option') !== 'other')
                            ->nullable(),
                        Select::make('price_name.option')
                            ->label('Price Name')
                            ->native(false)
                            ->required()
                            ->options(InvoiceModel::getAvailablePriceNameOptions()),
                        TextInput::make('price_name.custom')
                            ->hiddenLabel()
                            ->disabled(static fn (callable $get) => $get('price_name.option') !== 'other')
                            ->nullable(),
                        Select::make('amount_name.option')
                            ->label('Amount Name')
                            ->native(false)
                            ->required()
                            ->options(InvoiceModel::getAvailableAmountNameOptions()),
                        TextInput::make('amount_name.custom')
                            ->hiddenLabel()
                            ->disabled(static fn (callable $get) => $get('amount_name.option') !== 'other')
                            ->nullable(),
                    ])->columns(1),
                Group::make()
                    ->schema([
                        ViewField::make('preview.default')
                            ->label('Preview')
                            ->visible(static fn (callable $get) => $get('template') === 'default')
                            ->view('components.invoice-layouts.default'),
                        ViewField::make('preview.modern')
                            ->label('Preview')
                            ->visible(static fn (callable $get) => $get('template') === 'modern')
                            ->view('components.invoice-layouts.modern'),
                        ViewField::make('preview.classic')
                            ->label('Preview')
                            ->visible(static fn (callable $get) => $get('template') === 'classic')
                            ->view('components.invoice-layouts.classic'),
                    ])->columnSpan(2),
            ])->columns(3);
    }
    protected function handleRecordUpdate(InvoiceModel $record, array $data): InvoiceModel
    {
        $record->update($data);
        return $record;
    }
    /**
     * @return array<Action | ActionGroup>
     */
    protected function getFormActions(): array
    {
        return [
            $this->getSaveFormAction(),
        ];
    }
    protected function getSaveFormAction(): Action
    {
        return Action::make('save')
            ->label(__('filament-panels::pages/tenancy/edit-tenant-profile.form.actions.save.label'))
            ->submit('save')
            ->keyBindings(['mod+s']);
    }
    public static function canView(Model $record): bool
    {
        try {
            return authorize('update', $record)->allowed();
        } catch (AuthorizationException $exception) {
            return $exception->toResponse()->allowed();
        }
    }
}
 |