upgrade to filament v4
This commit is contained in:
294
app/Filament/Resources/Expenses/ExpenseResource.php
Normal file
294
app/Filament/Resources/Expenses/ExpenseResource.php
Normal file
@@ -0,0 +1,294 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources;
|
||||
|
||||
use App\Commands\Expenses\GenerateVoucher;
|
||||
use App\Filament\Resources\ExpenseResource\Pages;
|
||||
use App\Models\Account;
|
||||
use App\Models\Branch;
|
||||
use App\Models\Client;
|
||||
use App\Models\Expense;
|
||||
use Awcodes\TableRepeater\Components\TableRepeater;
|
||||
use Awcodes\TableRepeater\Header;
|
||||
use Filament\Forms\Components\DatePicker;
|
||||
use Filament\Forms\Components\Hidden;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Forms\Get;
|
||||
use Filament\Forms\Set;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Support\Collection;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
|
||||
class ExpenseResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Expense::class;
|
||||
|
||||
protected static bool $isVatable;
|
||||
|
||||
protected static ?string $navigationIcon = 'heroicon-o-banknotes';
|
||||
|
||||
protected static bool $shouldRegisterNavigation = false;
|
||||
|
||||
public static function form(Form $form): Form
|
||||
{
|
||||
return $form
|
||||
->schema(static::getExpenseFormFields());
|
||||
}
|
||||
|
||||
public static function getExpenseFormFields(): array
|
||||
{
|
||||
return [
|
||||
Select::make('client')
|
||||
->default(request()->query('client_id'))
|
||||
->options(Client::query()->get()->pluck('company', 'id'))
|
||||
->afterStateUpdated(function ($set, $get) {
|
||||
$set('branch_id', '');
|
||||
$set('voucher_number', static::getVoucherNumber($get));
|
||||
})
|
||||
->required()
|
||||
->live(),
|
||||
Select::make('branch_id')
|
||||
->relationship('branch', 'code')
|
||||
->options(fn ($get) => Branch::query()->where('client_id', $get('client'))->get()->pluck('code', 'id'))
|
||||
->afterStateUpdated(function ($set, $get) {
|
||||
$set('voucher_number', static::getVoucherNumber($get));
|
||||
$set('transactions.*.branch_id', $get('branch_id'));
|
||||
})
|
||||
->required()
|
||||
->live(),
|
||||
TextInput::make('supplier')->label('Supplier Name')->required(),
|
||||
TextInput::make('reference_number')->label('Reference Number'),
|
||||
TextInput::make('voucher_number')->label('Voucher Number')
|
||||
->default(fn ($get) => static::getVoucherNumber($get))
|
||||
->readOnly(),
|
||||
DatePicker::make('happened_on')->label('Date')
|
||||
->required()
|
||||
->afterStateUpdated(function ($set, $get) {
|
||||
$set('transactions.*.happened_on', $get('happened_on'));
|
||||
})
|
||||
->live()
|
||||
->native(false),
|
||||
|
||||
TableRepeater::make('transactions')
|
||||
->headers(fn (Get $get): array => static::getTransactionTableHeader($get))
|
||||
->relationship('transactions')
|
||||
->schema(fn (Get $get): array => static::getTransactionTableFormSchema($get))
|
||||
->visible(fn (Get $get) => $get('branch_id') != null)
|
||||
->columnSpan('full'),
|
||||
];
|
||||
}
|
||||
|
||||
public static function getVoucherNumber(Get $get): string
|
||||
{
|
||||
$branch = Branch::find($get('branch_id'));
|
||||
|
||||
if ($branch) {
|
||||
return GenerateVoucher::execute($branch);
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
public static function getTransactionTableHeader(Get $get): array
|
||||
{
|
||||
|
||||
// if (! static::getIsVatable($get)) {
|
||||
// return [
|
||||
// Header::make('Charge Account'),
|
||||
// Header::make('Description'),
|
||||
// Header::make('Gross Amount'),
|
||||
// Header::make('Withholding Tax'),
|
||||
// Header::make('Net Amount'),
|
||||
// ];
|
||||
// }
|
||||
|
||||
return [
|
||||
Header::make('Charge Account'),
|
||||
Header::make('Description'),
|
||||
// Header::make('Branch'),
|
||||
Header::make('Gross Amount'),
|
||||
Header::make('Exempt'),
|
||||
Header::make('Zero Rated'),
|
||||
Header::make('Vatable Amount'),
|
||||
Header::make('Input Tax'),
|
||||
Header::make('Withholding Tax'),
|
||||
Header::make('Net Amount'),
|
||||
];
|
||||
}
|
||||
|
||||
public static function getTransactionTableFormSchema(Get $get): array
|
||||
{
|
||||
// if (! static::getIsVatable($get)) {
|
||||
// return [
|
||||
// Select::make('account_id')->options(fn ($get) => static::getAccountOptions($get)),
|
||||
// TextInput::make('description')->label('Description'),
|
||||
// TextInput::make('gross_amount')->numeric()
|
||||
// ->live()
|
||||
// ->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
|
||||
// static::setDefaultFormValues($get, $set, $old, $state);
|
||||
// })->default(0),
|
||||
// TextInput::make('payable_withholding_tax')->numeric()->live()
|
||||
// ->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
|
||||
// static::setDefaultFormValues($get, $set, $old, $state);
|
||||
// })->default(0),
|
||||
// TextInput::make('net_amount')->numeric()->default(0),
|
||||
// ];
|
||||
// }
|
||||
|
||||
return [
|
||||
Select::make('account_id')->options(fn ($get) => static::getAccountOptions($get)),
|
||||
TextInput::make('description')->label('Description'),
|
||||
Hidden::make('branch_id')->default(fn (Get $get) => $get('../../branch_id')),
|
||||
TextInput::make('gross_amount')
|
||||
->numeric()
|
||||
->live(false, 500)
|
||||
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
|
||||
static::setDefaultFormValues($get, $set, $old, $state);
|
||||
})->default(0),
|
||||
TextInput::make('exempt')
|
||||
->numeric()
|
||||
->live()
|
||||
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
|
||||
static::setDefaultFormValues($get, $set, $old, $state);
|
||||
})->default(0),
|
||||
TextInput::make('zero_rated')
|
||||
->numeric()
|
||||
->live()
|
||||
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
|
||||
|
||||
static::setDefaultFormValues($get, $set, $old, $state);
|
||||
})->default(0),
|
||||
TextInput::make('vatable_amount')
|
||||
->numeric()
|
||||
->nullable()
|
||||
->live()
|
||||
->readOnly()
|
||||
->default(0),
|
||||
Hidden::make('happened_on')->default(fn (Get $get) => $get('../../happened_on')),
|
||||
TextInput::make('input_tax')
|
||||
->numeric()
|
||||
->live()
|
||||
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
|
||||
|
||||
static::setDefaultFormValues($get, $set, $old, $state);
|
||||
})->default(0),
|
||||
TextInput::make('payable_withholding_tax')
|
||||
->numeric()
|
||||
->live()
|
||||
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
|
||||
|
||||
static::setDefaultFormValues($get, $set, $old, $state);
|
||||
})->default(0),
|
||||
TextInput::make('net_amount')->numeric()->default(0),
|
||||
];
|
||||
}
|
||||
|
||||
public static function getAccountOptions(Get $get): Collection
|
||||
{
|
||||
$query = Account::query();
|
||||
|
||||
$query->where([
|
||||
'client_id' => $get('../../client'),
|
||||
]);
|
||||
|
||||
// if ($get('../../branch_id')) {
|
||||
// $query->whereHas('balances', function ($query) use ($get) {
|
||||
// return $query->where('branch_id', $get('../../branch_id'));
|
||||
// });
|
||||
// }
|
||||
|
||||
$query->whereHas('accountType', function ($query) {
|
||||
return $query->where('type', 'Expenses');
|
||||
});
|
||||
|
||||
return $query->get()->pluck('account', 'id');
|
||||
}
|
||||
|
||||
public static function setDefaultFormValues(Get $get, Set $set, ?string $old, ?string $state): void
|
||||
{
|
||||
|
||||
$exempt = (float) $get('exempt');
|
||||
$withHoldingTax = (float) $get('payable_withholding_tax');
|
||||
$vatableSales = $get('gross_amount');
|
||||
$vatableAmount = 0;
|
||||
if ($vatableSales) {
|
||||
$vatableAmount = $vatableSales / 1.12;
|
||||
}
|
||||
|
||||
$inputTax = $vatableAmount * 0.12;
|
||||
|
||||
$netAmount = (int) $vatableSales - $get('payable_withholding_tax');
|
||||
|
||||
if (static::getIsVatable($get)) {
|
||||
$netAmount = ($vatableAmount + $exempt) - $withHoldingTax;
|
||||
}
|
||||
|
||||
$set('input_tax', number_format($inputTax, 2, '.', ''));
|
||||
$set('vatable_amount', number_format($vatableAmount, 2, '.', ''));
|
||||
$set('net_amount', number_format($netAmount, 2, '.', ''));
|
||||
}
|
||||
|
||||
public static function getIsVatable(Get $get): bool
|
||||
{
|
||||
$client = Client::find($get('client'));
|
||||
|
||||
return $client && $client->vatable;
|
||||
|
||||
}
|
||||
|
||||
public static function isVatable(bool $value): void
|
||||
{
|
||||
static::$isVatable = $value;
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns(static::getTableColumns())
|
||||
->filters([
|
||||
//
|
||||
])
|
||||
->actions([
|
||||
Tables\Actions\DeleteAction::make(),
|
||||
Tables\Actions\EditAction::make(),
|
||||
])
|
||||
->bulkActions([
|
||||
Tables\Actions\BulkActionGroup::make([
|
||||
Tables\Actions\DeleteBulkAction::make(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getTableColumns(): array
|
||||
{
|
||||
return [
|
||||
Tables\Columns\TextColumn::make('supplier'),
|
||||
Tables\Columns\TextColumn::make('reference_number'),
|
||||
Tables\Columns\TextColumn::make('voucher_number'),
|
||||
Tables\Columns\TextColumn::make('branch.client.company'),
|
||||
Tables\Columns\TextColumn::make('branch.code'),
|
||||
Tables\Columns\TextColumn::make('happened_on'),
|
||||
Tables\Columns\TextColumn::make('accounts_list')->label('Accounts'),
|
||||
];
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ListExpenses::route('/'),
|
||||
'create' => Pages\CreateExpense::route('/create'),
|
||||
'edit' => Pages\EditExpense::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
94
app/Filament/Resources/Expenses/Pages/CreateExpense.php
Normal file
94
app/Filament/Resources/Expenses/Pages/CreateExpense.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ExpenseResource\Pages;
|
||||
|
||||
use App\Actions\Transactions\CreateRecordTransactionsAction;
|
||||
use App\Filament\Resources\ClientResource;
|
||||
use App\Filament\Resources\ExpenseResource;
|
||||
use App\Models\Client;
|
||||
use Exception;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Illuminate\Support\Arr;
|
||||
use Symfony\Component\Console\Exception\LogicException;
|
||||
|
||||
class CreateExpense extends CreateRecord
|
||||
{
|
||||
protected static string $resource = ExpenseResource::class;
|
||||
|
||||
public ?int $clientId = null;
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
parent::mount();
|
||||
|
||||
$this->clientId = request()->integer('client_id');
|
||||
}
|
||||
|
||||
public function getBreadcrumbs(): array
|
||||
{
|
||||
$client = $this->getClient();
|
||||
|
||||
if (! $client) {
|
||||
return parent::getBreadcrumbs();
|
||||
}
|
||||
|
||||
return [
|
||||
ClientResource::getUrl('view', ['record' => $client->id]) => $client->company,
|
||||
ClientResource::getUrl('view', ['record' => $client->id]).'?activeRelationManager=4' => 'Expenses',
|
||||
$this->getResource()::getUrl('create', ['client_id' => $client->id]) => 'Create',
|
||||
];
|
||||
}
|
||||
|
||||
protected function getClient(): Client|null
|
||||
{
|
||||
if (! $this->clientId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Client::find($this->clientId);
|
||||
}
|
||||
|
||||
protected function mutateFormDataBeforeCreate(array $data): array
|
||||
{
|
||||
|
||||
return $this->getFormDataMutation($data);
|
||||
}
|
||||
|
||||
public function getFormDataMutation(array $data): array
|
||||
{
|
||||
$transactions = $data['transactions'] ?? [];
|
||||
|
||||
$data['gross_amount'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['gross_amount'] ?? 0));
|
||||
$data['exempt'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['exempt'] ?? 0));
|
||||
$data['zero_rated'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['zero_rated'] ?? 0));
|
||||
$data['vatable_amount'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['vatable_amount'] ?? 0));
|
||||
$data['input_tax'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['input_tax'] ?? 0));
|
||||
$data['payable_withholding_tax'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['payable_withholding_tax'] ?? 0));
|
||||
$data['net_amount'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['net_amount'] ?? 0));
|
||||
|
||||
return Arr::except($data, ['client', 'transactions']);
|
||||
}
|
||||
|
||||
protected function afterCreate(): void
|
||||
{
|
||||
$transactions = $this->form->getState()['transactions'] ?? [];
|
||||
|
||||
try {
|
||||
app(CreateRecordTransactionsAction::class)($this->getRecord(), $transactions);
|
||||
|
||||
$accountIds = collect($transactions)
|
||||
->pluck('account_id')
|
||||
->filter()
|
||||
->unique()
|
||||
->values()
|
||||
->all();
|
||||
|
||||
$this->getRecord()->accounts()->sync($accountIds);
|
||||
|
||||
$this->commitDatabaseTransaction();
|
||||
} catch (Exception $exception) {
|
||||
$this->rollBackDatabaseTransaction();
|
||||
throw new LogicException('Failed to save transactions : '.$exception->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
27
app/Filament/Resources/Expenses/Pages/EditExpense.php
Normal file
27
app/Filament/Resources/Expenses/Pages/EditExpense.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ExpenseResource\Pages;
|
||||
|
||||
use App\Filament\Resources\ExpenseResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditExpense extends EditRecord
|
||||
{
|
||||
protected static string $resource = ExpenseResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function mutateFormDataBeforeFill(array $data): array
|
||||
{
|
||||
return [
|
||||
'client' => $this->getRecord()->branch->client->id,
|
||||
...$this->getRecord()->toArray(),
|
||||
];
|
||||
}
|
||||
}
|
||||
19
app/Filament/Resources/Expenses/Pages/ListExpenses.php
Normal file
19
app/Filament/Resources/Expenses/Pages/ListExpenses.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\ExpenseResource\Pages;
|
||||
|
||||
use App\Filament\Resources\ExpenseResource;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListExpenses extends ListRecords
|
||||
{
|
||||
protected static string $resource = ExpenseResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
Actions\CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user