Files
MKM/app/Filament/Resources/ExpenseResource.php
Jp 5d427cdea4 feat: add discount management and PDF export for transmittals
- Create Discount model, migration, and Filament resource with relation to Client
- Add PDF export functionality for transmittals using DomPDF
- Include discount type selection in sales transactions
- Fix account filtering logic in expense resource
- Update export job to generate PDF instead of Excel
2026-02-18 01:42:44 +08:00

295 lines
11 KiB
PHP

<?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'),
];
}
}