Move the logic for generating base accounts for a new client from the ClientObserver into a dedicated command class (GenerateBaseAccountCommand). This improves code organization and reusability. - The command is now used in the ClientObserver::created method. - The command is also made available as a manual action in the AccountsRelationManager table header, allowing admins to generate base accounts for existing clients that lack them. - Added necessary imports to the CreateSale page, though the command is not directly used there in this diff, suggesting preparatory work for future integration.
75 lines
2.8 KiB
PHP
75 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\ClientResource\RelationManagers;
|
|
|
|
use App\Commands\Clients\GenerateBaseAccountCommand;
|
|
use App\Filament\Exports\ClientAccountsExporter;
|
|
use App\Models\Account;
|
|
use Filament\Actions\Exports\Enums\ExportFormat;
|
|
use Filament\Forms;
|
|
use Filament\Forms\Form;
|
|
use Filament\Resources\RelationManagers\RelationManager;
|
|
use Filament\Tables;
|
|
use Filament\Tables\Table;
|
|
|
|
class AccountsRelationManager extends RelationManager
|
|
{
|
|
protected static string $relationship = 'accounts';
|
|
|
|
public function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
Forms\Components\TextInput::make('account')->required(),
|
|
Forms\Components\Textarea::make('description')->nullable(),
|
|
Forms\Components\Select::make('account_type_id')
|
|
->relationship('accountType', 'type')
|
|
->required(),
|
|
Forms\Components\Select::make('normal_balance')->options(
|
|
['debit' => 'Debit', 'credit' => 'Credit']
|
|
)->required(),
|
|
]);
|
|
}
|
|
|
|
public function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->recordTitleAttribute('client_id')
|
|
->columns([
|
|
Tables\Columns\TextColumn::make('account')->description(fn (Account $record): string => $record->description ?? ''),
|
|
Tables\Columns\TextColumn::make('accountType.type'),
|
|
Tables\Columns\TextColumn::make('accountType.normal_balance')->label('Normal Balance'),
|
|
])
|
|
->filters([
|
|
//
|
|
])
|
|
->headerActions([
|
|
Tables\Actions\Action::make('generate-base-accounts')
|
|
->requiresConfirmation()
|
|
->label('Generate Base Accounts')
|
|
->action(function () {
|
|
$client = $this->getOwnerRecord();
|
|
if (! $client ) {
|
|
return;
|
|
}
|
|
|
|
if($client->accounts()->count() > 0) {
|
|
return;
|
|
}
|
|
app(GenerateBaseAccountCommand::class)->execute($client);
|
|
}),
|
|
Tables\Actions\ExportAction::make('Export Accounts')->exporter(ClientAccountsExporter::class)->formats([ExportFormat::Csv]),
|
|
Tables\Actions\CreateAction::make()->label('New Account')->icon('heroicon-o-plus')->slideOver(),
|
|
])
|
|
->actions([
|
|
Tables\Actions\EditAction::make()->slideOver(),
|
|
Tables\Actions\DeleteAction::make()->requiresConfirmation(),
|
|
])
|
|
->bulkActions([
|
|
Tables\Actions\BulkActionGroup::make([
|
|
Tables\Actions\DeleteBulkAction::make()->icon('heroicon-s-trash')->requiresConfirmation(),
|
|
]),
|
|
]);
|
|
}
|
|
}
|