upgrade to filament v4
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
<?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(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ClientResource\RelationManagers;
|
||||
|
||||
use App\Filament\Resources\BranchResource\Pages\EditBranch;
|
||||
use App\Filament\Resources\ClientResource;
|
||||
use App\Models\Branch;
|
||||
use App\Processes\Branch\CreateBranchProcess;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Support\RawJs;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Validation\Rules\Unique;
|
||||
|
||||
class BranchesRelationManager extends RelationManager
|
||||
{
|
||||
protected static string $relationship = 'branches';
|
||||
|
||||
protected static bool $shouldCheckPolicyExistence = true;
|
||||
|
||||
protected CreateBranchProcess $createBranchProcess;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->createBranchProcess = new CreateBranchProcess;
|
||||
}
|
||||
|
||||
public function form(Form $form): Form
|
||||
{
|
||||
return $form
|
||||
->schema([
|
||||
Forms\Components\Hidden::make('id'),
|
||||
Forms\Components\TextInput::make('code')->required()
|
||||
->unique(
|
||||
'branches',
|
||||
'code',
|
||||
ignoreRecord: true,
|
||||
modifyRuleUsing: fn (Unique $rule) => $rule->where('client_id', $this->getOwnerRecord()->id)
|
||||
),
|
||||
Forms\Components\TextInput::make('series')->label('Current Series')
|
||||
->required()
|
||||
->numeric()
|
||||
->integer()
|
||||
->maxLength(6)
|
||||
->minLength(1)
|
||||
->mask(RawJs::make(<<<'JS'
|
||||
'999999'
|
||||
JS)),
|
||||
])->columns(1);
|
||||
}
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->recordTitleAttribute('client_id')
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('code')->label('Branch Code'),
|
||||
Tables\Columns\TextColumn::make('current_series')->label('Current Series'),
|
||||
])
|
||||
->filters([
|
||||
//
|
||||
])
|
||||
->headerActions([
|
||||
Tables\Actions\CreateAction::make()
|
||||
->mutateFormDataUsing(fn ($data) => $this->appendCientId($data))
|
||||
->using(fn ($data) => $this->saveBranch($data)),
|
||||
])
|
||||
->actions([
|
||||
// Tables\Actions\ViewAction::make()->url(fn ($record) => EditBranch::getUrl(['record' => $record->id])),
|
||||
Tables\Actions\EditAction::make()
|
||||
->fillForm(fn ($record) => ['id' => $record->id, 'code' => $record->code, 'series' => $record->current_series])
|
||||
->mutateFormDataUsing(fn ($data) => $this->appendCientId($data))
|
||||
->using(fn ($data) => $this->saveBranch($data))
|
||||
->url(fn ($record) => EditBranch::getUrl(['record' => $record->id])),
|
||||
Tables\Actions\DeleteAction::make(),
|
||||
])
|
||||
->bulkActions([
|
||||
Tables\Actions\BulkActionGroup::make([
|
||||
Tables\Actions\DeleteBulkAction::make(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
public function appendCientId($data): array
|
||||
{
|
||||
$data['client_id'] = $this->ownerRecord->id;
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function saveBranch($data): Branch
|
||||
{
|
||||
return ClientResource::saveBranch($data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ClientResource\RelationManagers;
|
||||
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||
|
||||
class DiscountRelationManager extends RelationManager
|
||||
{
|
||||
protected static string $relationship = 'discounts';
|
||||
|
||||
public function form(Form $form): Form
|
||||
{
|
||||
return $form
|
||||
->schema([
|
||||
Forms\Components\TextInput::make('discount')
|
||||
->required()
|
||||
->maxLength(255),
|
||||
]);
|
||||
}
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->recordTitleAttribute('discount')
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('discount'),
|
||||
])
|
||||
->filters([
|
||||
//
|
||||
])
|
||||
->headerActions([
|
||||
Tables\Actions\CreateAction::make(),
|
||||
])
|
||||
->actions([
|
||||
Tables\Actions\EditAction::make(),
|
||||
Tables\Actions\DeleteAction::make(),
|
||||
])
|
||||
->bulkActions([
|
||||
Tables\Actions\BulkActionGroup::make([
|
||||
Tables\Actions\DeleteBulkAction::make(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ClientResource\RelationManagers;
|
||||
|
||||
use App\Filament\Resources\ExpenseResource;
|
||||
use App\Models\Expense;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class ExpensesRelationManager extends RelationManager
|
||||
{
|
||||
protected static string $relationship = 'expenses';
|
||||
|
||||
protected static ?string $title = 'Expenses';
|
||||
|
||||
public function form(Form $form): Form
|
||||
{
|
||||
return $form
|
||||
->schema([]);
|
||||
}
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return ExpenseResource::table($table)->headerActions([
|
||||
Tables\Actions\Action::make('New Expense')->action('openCreateForm'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function openCreateForm()
|
||||
{
|
||||
return redirect()->route('filament.admin.resources.expenses.create', ['client_id' => $this->getOwnerRecord()->id]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ClientResource\RelationManagers;
|
||||
|
||||
use App\Actions\Balances\CreateBalanceAction;
|
||||
use App\Actions\Ledgers\CreateLedgerAction;
|
||||
use App\DataObjects\CreateLedgerDTO;
|
||||
use App\Models\Branch;
|
||||
use App\Models\Journal;
|
||||
use Awcodes\TableRepeater\Components\TableRepeater;
|
||||
use Awcodes\TableRepeater\Header;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Components\DatePicker;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Pipeline;
|
||||
|
||||
class JournalsRelationManager extends RelationManager
|
||||
{
|
||||
protected static string $relationship = 'journals';
|
||||
|
||||
protected static ?string $title = 'Journal Entries (Adjustments)';
|
||||
|
||||
public function form(Form $form): Form
|
||||
{
|
||||
return $form
|
||||
->schema([
|
||||
Select::make('branch_id')
|
||||
->label('Branch')
|
||||
->options(fn () => Branch::where('client_id', $this->getOwnerRecord()->id)->pluck('code', 'id'))
|
||||
->required()
|
||||
->default(fn () => Branch::where('client_id', $this->getOwnerRecord()->id)->first()?->id),
|
||||
|
||||
DatePicker::make('happened_on')
|
||||
->label('Date')
|
||||
->required()
|
||||
->default(now()),
|
||||
|
||||
TextInput::make('series')
|
||||
->label('Reference/Series #')
|
||||
->required(),
|
||||
|
||||
Textarea::make('description')
|
||||
->label('Description')
|
||||
->columnSpanFull(),
|
||||
|
||||
TableRepeater::make('ledgers')
|
||||
->relationship('ledgers')
|
||||
->headers([
|
||||
Header::make('Account'),
|
||||
Header::make('Description'),
|
||||
Header::make('Debit'),
|
||||
Header::make('Credit'),
|
||||
])
|
||||
->schema([
|
||||
Select::make('account_id')
|
||||
->relationship('account', 'account', fn (Builder $query) => $query->where('client_id', $this->getOwnerRecord()->id))
|
||||
->searchable()
|
||||
->preload()
|
||||
->required(),
|
||||
TextInput::make('description'),
|
||||
TextInput::make('debit_amount')
|
||||
->numeric()
|
||||
->default(0),
|
||||
TextInput::make('credit_amount')
|
||||
->numeric()
|
||||
->default(0),
|
||||
])
|
||||
->columnSpanFull()
|
||||
->minItems(2)
|
||||
->live()
|
||||
->rules([
|
||||
fn (): \Closure => function (string $attribute, $value, \Closure $fail) {
|
||||
$debit = collect($value)->sum('debit_amount');
|
||||
$credit = collect($value)->sum('credit_amount');
|
||||
if (abs($debit - $credit) > 0.01) {
|
||||
$fail("Total Debit (" . number_format($debit, 2) . ") must equal Total Credit (" . number_format($credit, 2) . ").");
|
||||
}
|
||||
},
|
||||
])
|
||||
->afterStateUpdated(function ($state, $component) {
|
||||
// Optional: Validation logic
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->recordTitleAttribute('description')
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('happened_on')
|
||||
->date()
|
||||
->sortable(),
|
||||
Tables\Columns\TextColumn::make('series')
|
||||
->searchable(),
|
||||
Tables\Columns\TextColumn::make('description')
|
||||
->limit(50),
|
||||
Tables\Columns\TextColumn::make('total_debit')
|
||||
->label('Total Debit')
|
||||
->state(fn (Journal $record) => $record->ledgers->sum('debit_amount'))
|
||||
->money('PHP'),
|
||||
Tables\Columns\TextColumn::make('total_credit')
|
||||
->label('Total Credit')
|
||||
->state(fn (Journal $record) => $record->ledgers->sum('credit_amount'))
|
||||
->money('PHP'),
|
||||
])
|
||||
->filters([
|
||||
//
|
||||
])
|
||||
->headerActions([
|
||||
Tables\Actions\CreateAction::make()
|
||||
->label('Add Adjustment Entry')
|
||||
->using(function (array $data, string $model) {
|
||||
return DB::transaction(function () use ($data, $model) {
|
||||
$ledgersData = $data['ledgers'] ?? [];
|
||||
$journalData = Arr::except($data, ['ledgers']);
|
||||
$journalData['client_id'] = $this->getOwnerRecord()->id;
|
||||
|
||||
$journal = $model::create($journalData);
|
||||
|
||||
foreach ($ledgersData as $ledger) {
|
||||
$ledgerPayload = new CreateLedgerDTO(
|
||||
branch_id: $journal->branch_id,
|
||||
amount: ($ledger['debit_amount'] > 0) ? $ledger['debit_amount'] : $ledger['credit_amount'],
|
||||
ledger: null, // Will be created
|
||||
transaction: null, // No transaction
|
||||
journal: $journal,
|
||||
account: \App\Models\Account::find($ledger['account_id']),
|
||||
type: ($ledger['debit_amount'] > 0) ? 'debit' : 'credit'
|
||||
);
|
||||
|
||||
Pipeline::send(passable: $ledgerPayload)->through(
|
||||
[
|
||||
CreateLedgerAction::class,
|
||||
]
|
||||
)->thenReturn();
|
||||
}
|
||||
return $journal;
|
||||
});
|
||||
}),
|
||||
])
|
||||
->actions([
|
||||
Tables\Actions\EditAction::make(),
|
||||
Tables\Actions\DeleteAction::make(),
|
||||
])
|
||||
->bulkActions([
|
||||
Tables\Actions\BulkActionGroup::make([
|
||||
Tables\Actions\DeleteBulkAction::make(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ClientResource\RelationManagers;
|
||||
|
||||
use App\Filament\Resources\SaleResource;
|
||||
use App\Models\Sale;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class SalesRelationManager extends RelationManager
|
||||
{
|
||||
protected static string $relationship = 'sales';
|
||||
|
||||
protected static ?string $title = 'Sales';
|
||||
|
||||
public function form(Form $form): Form
|
||||
{
|
||||
return $form->schema([]);
|
||||
}
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return SaleResource::table($table)->headerActions([
|
||||
Tables\Actions\Action::make('New Sale')->action('openCreateForm'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function openCreateForm()
|
||||
{
|
||||
return redirect()->route('filament.admin.resources.sales.create', ['client_id' => $this->getOwnerRecord()->id]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ClientResource\RelationManagers;
|
||||
|
||||
use App\Filament\Resources\TransmittalResource;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class TransmittalsRelationManager extends RelationManager
|
||||
{
|
||||
protected static string $relationship = 'transmittals';
|
||||
|
||||
public static function canViewForRecord(Model $ownerRecord, string $pageClass): bool
|
||||
{
|
||||
return auth()->user()->can('update_transmittal');
|
||||
}
|
||||
|
||||
public function form(Form $form): Form
|
||||
{
|
||||
return TransmittalResource::form($form)
|
||||
->fill(
|
||||
['client_id' => $this->getOwnerRecord()->id]
|
||||
);
|
||||
}
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return TransmittalResource::table($table)->headerActions([
|
||||
Tables\Actions\Action::make('New Transmittal')->action('openCreateForm'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function openCreateForm()
|
||||
{
|
||||
return redirect()->route('filament.admin.resources.transmittals.create');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user