upgrade to filament v4
This commit is contained in:
135
app/Filament/Resources/Clients/ClientResource.php
Normal file
135
app/Filament/Resources/Clients/ClientResource.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources;
|
||||
|
||||
use App\DataObjects\CreateBranchDTO;
|
||||
use App\Filament\Resources\ClientResource\Pages\EditClient;
|
||||
use App\Filament\Resources\ClientResource\Pages\ListClients;
|
||||
use App\Filament\Resources\ClientResource\Pages\ViewClient;
|
||||
use App\Filament\Resources\ClientResource\Pages\GeneralLedger;
|
||||
use App\Filament\Resources\ClientResource\Pages\TrialBalance;
|
||||
use App\Filament\Resources\ClientResource\RelationManagers\AccountsRelationManager;
|
||||
use App\Filament\Resources\ClientResource\RelationManagers\BranchesRelationManager;
|
||||
use App\Filament\Resources\ClientResource\RelationManagers\DiscountRelationManager;
|
||||
use App\Filament\Resources\ClientResource\RelationManagers\ExpensesRelationManager;
|
||||
use App\Filament\Resources\ClientResource\RelationManagers\JournalsRelationManager;
|
||||
use App\Filament\Resources\ClientResource\RelationManagers\SalesRelationManager;
|
||||
use App\Filament\Resources\ClientResource\RelationManagers\TransmittalsRelationManager;
|
||||
use App\Models\Branch;
|
||||
use App\Models\Client;
|
||||
use App\Processes\Branch\CreateBranchProcess;
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Filament\Pages\SubNavigationPosition;
|
||||
use Filament\Resources\Pages\Page;
|
||||
|
||||
class ClientResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Client::class;
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-user';
|
||||
|
||||
protected static ?string $recordTitleAttribute = 'company';
|
||||
|
||||
protected static SubNavigationPosition $subNavigationPosition = SubNavigationPosition::Top;
|
||||
|
||||
public static function authorizeView(Model $record): void
|
||||
{
|
||||
parent::authorizeView($record);
|
||||
}
|
||||
|
||||
public static function getRecordSubNavigation(Page $page): array
|
||||
{
|
||||
return $page->generateNavigationItems([
|
||||
ViewClient::class,
|
||||
EditClient::class,
|
||||
GeneralLedger::class,
|
||||
TrialBalance::class,
|
||||
]);
|
||||
}
|
||||
|
||||
public static function form(Form $form): Form
|
||||
{
|
||||
return $form
|
||||
->schema([
|
||||
Forms\Components\TextInput::make('firstname')->label('First Name')->required(),
|
||||
Forms\Components\TextInput::make('middlename')->label('Middle Name')->nullable(),
|
||||
Forms\Components\TextInput::make('lastname')->label('Last Name')->required(),
|
||||
Forms\Components\Grid::make()->schema([
|
||||
Forms\Components\TextInput::make('company')->label('Company')->required(),
|
||||
Forms\Components\Select::make('type_id')
|
||||
->relationship('type', 'type')
|
||||
->label('Type')->required(),
|
||||
])->columns(2),
|
||||
])->columns(3);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
Tables\Columns\TextColumn::make('firstname')->label('First Name')->searchable(),
|
||||
Tables\Columns\TextColumn::make('middlename')->label('Middle Name'),
|
||||
Tables\Columns\TextColumn::make('lastname')->label('Last Name'),
|
||||
Tables\Columns\TextColumn::make('company')->label('Company')->searchable(),
|
||||
Tables\Columns\TextColumn::make('type.type')->label('Type'),
|
||||
])
|
||||
->filters([
|
||||
Tables\Filters\Filter::make('Vatable')
|
||||
->query(fn (Builder $query) => $query->orWhereHas('type', function (Builder $query) {
|
||||
$query->where('type', 'Vatable');
|
||||
})),
|
||||
Tables\Filters\Filter::make('Non-Vatable')
|
||||
->query(fn (Builder $query) => $query->orWhereHas('type', function (Builder $query) {
|
||||
$query->where('type', 'Non Vatable');
|
||||
})),
|
||||
])
|
||||
->actions([
|
||||
Tables\Actions\ViewAction::make(),
|
||||
Tables\Actions\EditAction::make(),
|
||||
Tables\Actions\DeleteAction::make()->requiresConfirmation(),
|
||||
])
|
||||
->bulkActions([
|
||||
Tables\Actions\BulkActionGroup::make([
|
||||
Tables\Actions\DeleteBulkAction::make(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
AccountsRelationManager::class,
|
||||
BranchesRelationManager::class,
|
||||
TransmittalsRelationManager::class,
|
||||
SalesRelationManager::class,
|
||||
ExpensesRelationManager::class,
|
||||
JournalsRelationManager::class,
|
||||
DiscountRelationManager::class,
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'view' => ViewClient::route('/{record}'),
|
||||
'edit' => EditClient::route('/{record}/edit'),
|
||||
'index' => ListClients::route('/'),
|
||||
'general-ledger' => GeneralLedger::route('/{record}/general-ledger'),
|
||||
'trial-balance' => TrialBalance::route('/{record}/trial-balance'),
|
||||
];
|
||||
}
|
||||
|
||||
public static function saveBranch($data): Branch
|
||||
{
|
||||
$createBranchProcess = new CreateBranchProcess;
|
||||
$payload = new CreateBranchDTO(data: $data);
|
||||
|
||||
return $createBranchProcess->run($payload)->branch;
|
||||
}
|
||||
}
|
||||
11
app/Filament/Resources/Clients/Pages/CreateClient.php
Normal file
11
app/Filament/Resources/Clients/Pages/CreateClient.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ClientResource\Pages;
|
||||
|
||||
use App\Filament\Resources\ClientResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateClient extends CreateRecord
|
||||
{
|
||||
protected static string $resource = ClientResource::class;
|
||||
}
|
||||
20
app/Filament/Resources/Clients/Pages/EditClient.php
Normal file
20
app/Filament/Resources/Clients/Pages/EditClient.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ClientResource\Pages;
|
||||
|
||||
use App\Filament\Resources\ClientResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditClient extends EditRecord
|
||||
{
|
||||
protected static string $resource = ClientResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\DeleteAction::make()->icon('heroicon-s-trash')->requiresConfirmation(),
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
142
app/Filament/Resources/Clients/Pages/GeneralLedger.php
Normal file
142
app/Filament/Resources/Clients/Pages/GeneralLedger.php
Normal file
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ClientResource\Pages;
|
||||
|
||||
use App\Filament\Resources\ClientResource;
|
||||
use App\Models\Ledger;
|
||||
use Filament\Resources\Pages\Page;
|
||||
use Filament\Tables\Concerns\InteractsWithTable;
|
||||
use Filament\Tables\Contracts\HasTable;
|
||||
use Filament\Tables\Table;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Filters\Filter;
|
||||
use Filament\Forms\Components\DatePicker;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Filament\Resources\Pages\Concerns\InteractsWithRecord;
|
||||
use pxlrbt\FilamentExcel\Actions\Tables\ExportAction;
|
||||
use pxlrbt\FilamentExcel\Exports\ExcelExport;
|
||||
use pxlrbt\FilamentExcel\Columns\Column;
|
||||
|
||||
class GeneralLedger extends Page implements HasTable
|
||||
{
|
||||
use InteractsWithTable;
|
||||
use InteractsWithRecord;
|
||||
|
||||
protected static string $resource = ClientResource::class;
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-document-text';
|
||||
|
||||
protected static string $view = 'filament.resources.client-resource.pages.general-ledger';
|
||||
|
||||
public function mount(int | string $record): void
|
||||
{
|
||||
$this->record = $this->resolveRecord($record);
|
||||
}
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->query(
|
||||
Ledger::query()
|
||||
->where('client_id', $this->getRecord()->id)
|
||||
->with(['account', 'transaction', 'journal'])
|
||||
)
|
||||
->columns([
|
||||
TextColumn::make('date')
|
||||
->label('Date')
|
||||
->state(function (Ledger $record) {
|
||||
return $record->transaction?->happened_on?->format('Y-m-d')
|
||||
?? $record->journal?->happened_on?->format('Y-m-d')
|
||||
?? $record->created_at->format('Y-m-d');
|
||||
})
|
||||
->sortable(),
|
||||
TextColumn::make('account.account')
|
||||
->label('Account')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
TextColumn::make('description')
|
||||
->limit(50)
|
||||
->tooltip(function (TextColumn $column): ?string {
|
||||
$state = $column->getState();
|
||||
if (strlen($state) <= $column->getCharacterLimit()) {
|
||||
return null;
|
||||
}
|
||||
return $state;
|
||||
}),
|
||||
TextColumn::make('debit_amount')
|
||||
->label('Debit')
|
||||
->money('PHP')
|
||||
->sortable(),
|
||||
TextColumn::make('credit_amount')
|
||||
->label('Credit')
|
||||
->money('PHP')
|
||||
->sortable(),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('account')
|
||||
->relationship('account', 'account', fn (Builder $query) => $query->where('client_id', $this->getRecord()->id))
|
||||
->searchable()
|
||||
->preload(),
|
||||
Filter::make('date_range')
|
||||
->form([
|
||||
DatePicker::make('from'),
|
||||
DatePicker::make('to'),
|
||||
])
|
||||
->query(function (Builder $query, array $data): Builder {
|
||||
return $query
|
||||
->when(
|
||||
$data['from'],
|
||||
fn (Builder $query, $date): Builder => $query->where(function ($q) use ($date) {
|
||||
$q->whereHas('transaction', fn ($q) => $q->whereDate('happened_on', '>=', $date))
|
||||
->orWhereHas('journal', fn ($q) => $q->whereDate('happened_on', '>=', $date))
|
||||
->orWhere(fn($q) => $q->whereDoesntHave('transaction')->whereDoesntHave('journal')->whereDate('created_at', '>=', $date));
|
||||
})
|
||||
)
|
||||
->when(
|
||||
$data['to'],
|
||||
fn (Builder $query, $date): Builder => $query->where(function ($q) use ($date) {
|
||||
$q->whereHas('transaction', fn ($q) => $q->whereDate('happened_on', '<=', $date))
|
||||
->orWhereHas('journal', fn ($q) => $q->whereDate('happened_on', '<=', $date))
|
||||
->orWhere(fn($q) => $q->whereDoesntHave('transaction')->whereDoesntHave('journal')->whereDate('created_at', '<=', $date));
|
||||
})
|
||||
);
|
||||
})
|
||||
])
|
||||
->defaultSort('created_at', 'desc')
|
||||
->groups([
|
||||
'account.account',
|
||||
])
|
||||
->headerActions([
|
||||
ExportAction::make()
|
||||
->label('Export General Ledger')
|
||||
->exports([
|
||||
ExcelExport::make()
|
||||
->fromTable()
|
||||
->withFilename(fn () => $this->getRecord()->name . ' - General Ledger - ' . date('Y-m-d'))
|
||||
->withColumns([
|
||||
Column::make('date')
|
||||
->heading('Date')
|
||||
->formatStateUsing(fn ($record) => $record->transaction?->happened_on?->format('Y-m-d')
|
||||
?? $record->journal?->happened_on?->format('Y-m-d')
|
||||
?? $record->created_at->format('Y-m-d')),
|
||||
Column::make('reference')
|
||||
->heading('Reference')
|
||||
->formatStateUsing(fn ($record) => match(true) {
|
||||
$record->transaction && $record->transaction->transactionable =>
|
||||
$record->transaction->transactionable->voucher_number
|
||||
?? $record->transaction->transactionable->reference_number
|
||||
?? 'TR-'.$record->transaction->id,
|
||||
$record->transaction => 'TR-'.$record->transaction->id,
|
||||
$record->journal => $record->journal->series ?? 'JV-'.$record->journal->id,
|
||||
default => 'L-'.$record->id,
|
||||
}),
|
||||
Column::make('account.account')->heading('Account'),
|
||||
Column::make('description')->heading('Description'),
|
||||
Column::make('debit_amount')->heading('Debit'),
|
||||
Column::make('credit_amount')->heading('Credit'),
|
||||
])
|
||||
])
|
||||
]);
|
||||
}
|
||||
}
|
||||
25
app/Filament/Resources/Clients/Pages/ListClients.php
Normal file
25
app/Filament/Resources/Clients/Pages/ListClients.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ClientResource\Pages;
|
||||
|
||||
use App\Filament\Exports\ClientExporter;
|
||||
use App\Filament\Resources\ClientResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListClients extends ListRecords
|
||||
{
|
||||
protected static string $resource = ClientResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\ExportAction::make('Export clients')
|
||||
->exporter(ClientExporter::class)
|
||||
->formats([
|
||||
Actions\Exports\Enums\ExportFormat::Csv,
|
||||
]),
|
||||
Actions\CreateAction::make()->slideOver(),
|
||||
];
|
||||
}
|
||||
}
|
||||
100
app/Filament/Resources/Clients/Pages/TrialBalance.php
Normal file
100
app/Filament/Resources/Clients/Pages/TrialBalance.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ClientResource\Pages;
|
||||
|
||||
use App\Filament\Resources\ClientResource;
|
||||
use App\Models\Account;
|
||||
use Filament\Resources\Pages\Page;
|
||||
use Filament\Tables\Concerns\InteractsWithTable;
|
||||
use Filament\Tables\Contracts\HasTable;
|
||||
use Filament\Tables\Table;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Columns\Summarizers\Summarizer;
|
||||
use Filament\Tables\Columns\Summarizers\Sum;
|
||||
use Filament\Resources\Pages\Concerns\InteractsWithRecord;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class TrialBalance extends Page implements HasTable
|
||||
{
|
||||
use InteractsWithTable;
|
||||
use InteractsWithRecord;
|
||||
|
||||
protected static string $resource = ClientResource::class;
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-scale';
|
||||
|
||||
protected static string $view = 'filament.resources.client-resource.pages.trial-balance';
|
||||
|
||||
public function mount(int | string $record): void
|
||||
{
|
||||
$this->record = $this->resolveRecord($record);
|
||||
}
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->query(
|
||||
Account::query()
|
||||
->where('client_id', $this->getRecord()->id)
|
||||
->with(['accountType', 'latestBalance'])
|
||||
)
|
||||
->columns([
|
||||
TextColumn::make('account')
|
||||
->label('Account Name')
|
||||
->sortable()
|
||||
->searchable(),
|
||||
TextColumn::make('debit')
|
||||
->label('Debit')
|
||||
->money('PHP')
|
||||
->state(function (Account $record) {
|
||||
$balance = $record->latestBalance?->balance ?? 0;
|
||||
$normal = strtolower($record->accountType->normal_balance ?? 'debit');
|
||||
|
||||
return ($normal == 'debit' && $balance >= 0) || ($normal == 'credit' && $balance < 0)
|
||||
? abs($balance) : 0;
|
||||
})
|
||||
->summarize(Summarizer::make()
|
||||
->label('Total Debit')
|
||||
->money('PHP')
|
||||
->using(function ($query) {
|
||||
return Account::query()
|
||||
->whereIn('id', $query->clone()->pluck('accounts.id'))
|
||||
->with(['accountType', 'latestBalance'])
|
||||
->get()
|
||||
->sum(function ($record) {
|
||||
$balance = $record->latestBalance?->balance ?? 0;
|
||||
$normal = strtolower($record->accountType->normal_balance ?? 'debit');
|
||||
return ($normal == 'debit' && $balance >= 0) || ($normal == 'credit' && $balance < 0)
|
||||
? abs($balance) : 0;
|
||||
});
|
||||
})
|
||||
),
|
||||
TextColumn::make('credit')
|
||||
->label('Credit')
|
||||
->money('PHP')
|
||||
->state(function (Account $record) {
|
||||
$balance = $record->latestBalance?->balance ?? 0;
|
||||
$normal = strtolower($record->accountType->normal_balance ?? 'debit');
|
||||
|
||||
return ($normal == 'credit' && $balance >= 0) || ($normal == 'debit' && $balance < 0)
|
||||
? abs($balance) : 0;
|
||||
})
|
||||
->summarize(Summarizer::make()
|
||||
->label('Total Credit')
|
||||
->money('PHP')
|
||||
->using(function ($query) {
|
||||
return Account::query()
|
||||
->whereIn('id', $query->clone()->pluck('accounts.id'))
|
||||
->with(['accountType', 'latestBalance'])
|
||||
->get()
|
||||
->sum(function ($record) {
|
||||
$balance = $record->latestBalance?->balance ?? 0;
|
||||
$normal = strtolower($record->accountType->normal_balance ?? 'debit');
|
||||
return ($normal == 'credit' && $balance >= 0) || ($normal == 'debit' && $balance < 0)
|
||||
? abs($balance) : 0;
|
||||
});
|
||||
})
|
||||
),
|
||||
]);
|
||||
}
|
||||
}
|
||||
49
app/Filament/Resources/Clients/Pages/ViewClient.php
Normal file
49
app/Filament/Resources/Clients/Pages/ViewClient.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ClientResource\Pages;
|
||||
|
||||
use App\Filament\Resources\ClientResource;
|
||||
use Filament\Infolists\Components\Grid;
|
||||
use Filament\Infolists\Components\Section;
|
||||
use Filament\Infolists\Components\TextEntry;
|
||||
use Filament\Infolists\Infolist;
|
||||
use Filament\Resources\Pages\ViewRecord;
|
||||
|
||||
class ViewClient extends ViewRecord
|
||||
{
|
||||
protected static string $resource = ClientResource::class;
|
||||
|
||||
public function infolist(Infolist $infolist): Infolist
|
||||
{
|
||||
return $infolist
|
||||
->schema([
|
||||
Section::make()->schema([
|
||||
Grid::make()->schema([
|
||||
TextEntry::make('firstname')->label('First Name'),
|
||||
TextEntry::make('middlename')->label('Middle Name'),
|
||||
TextEntry::make('lastname')->label('Last Name'),
|
||||
TextEntry::make('company')->label('Company'),
|
||||
TextEntry::make('type.type')->label('Type'),
|
||||
])->columns(3),
|
||||
]),
|
||||
|
||||
// Section::make('Branches')->schema([
|
||||
// RepeatableEntry::make('branches')
|
||||
// ->schema([
|
||||
// TextEntry::make('code')->label('Branch Code'),
|
||||
// TextEntry::make('current_series')->label('Branch Current Series'),
|
||||
// ])
|
||||
// ->hiddenLabel()
|
||||
// ->grid(2),
|
||||
// ])->collapsible(),
|
||||
]);
|
||||
}
|
||||
|
||||
// public function getRelationManagers(): array
|
||||
// {
|
||||
// return [
|
||||
// AccountsRelationManager::class,
|
||||
// TransmittalsRelationManager::class,
|
||||
// ];
|
||||
// }
|
||||
}
|
||||
@@ -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