schema([ Select::make('branch_id') ->relationship('branch', 'code', fn (Builder $query) => $query->where('client_id', $this->getOwnerRecord()->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 => $this->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 ''; } private static function getTransactionTableHeader(Get $get): array { return [ Header::make('Charge Account'), Header::make('Description'), 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'), ]; } private function getTransactionTableFormSchema(Get $get): array { return [ Select::make('account_id')->options(fn ($get) => $this->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) { $this->setDefaultFormValues($get, $set, $old, $state); })->default(0), TextInput::make('exempt') ->numeric() ->live() ->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) { $this->setDefaultFormValues($get, $set, $old, $state); })->default(0), TextInput::make('zero_rated') ->numeric() ->live() ->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) { $this->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) { $this->setDefaultFormValues($get, $set, $old, $state); })->default(0), TextInput::make('payable_withholding_tax') ->numeric() ->live() ->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) { $this->setDefaultFormValues($get, $set, $old, $state); })->default(0), TextInput::make('net_amount')->numeric()->default(0), ]; } private function getAccountOptions(Get $get): Collection { $query = Account::query(); $query->where([ 'client_id' => $this->getOwnerRecord()->id, ]); 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'); } private 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 ($this->getOwnerRecord()->vatable) { $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 function table(Table $table): Table { return $table ->recordTitleAttribute('supplier') ->columns([ TextColumn::make('supplier'), TextColumn::make('reference_number'), TextColumn::make('voucher_number'), TextColumn::make('branch.code'), TextColumn::make('happened_on'), ]) ->filters([ // ]) ->headerActions([ Tables\Actions\CreateAction::make() ->using(function (array $data, string $model) { $transactions = $data['transactions'] ?? []; $data = Arr::except($data, ['transactions']); $record = $model::create($data); try { $branch = $record->branch; foreach ($transactions as $transaction) { $tData = [ 'branch_id' => $branch->id, 'happened_on' => $record->happened_on, ...$transaction, ]; $payload = new CreateTransactionDTO(data: $tData, transactionable: $record); Pipeline::send(passable: $payload)->through( [ CreateTransactionAction::class, ] )->thenReturn(); } } catch (\Exception $exception) { throw new \Exception('Failed to save transactions : '.$exception->getMessage()); } return $record; }), ]) ->actions([ Tables\Actions\EditAction::make(), Tables\Actions\DeleteAction::make(), ]) ->bulkActions([ Tables\Actions\BulkActionGroup::make([ Tables\Actions\DeleteBulkAction::make(), ]), ]); } }