feat: updates on expenses
This commit is contained in:
@@ -15,15 +15,19 @@ 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-rectangle-stack';
|
||||
|
||||
public static function form(Form $form): Form
|
||||
@@ -36,29 +40,147 @@ class ExpenseResource extends Resource
|
||||
{
|
||||
return [
|
||||
Select::make('client')
|
||||
->options(Client::query()->get()->pluck('company', 'id'))->live(),
|
||||
Select::make('branch_id')->options(fn ($get) => Branch::query()->where('client_id', $get('client'))->get()->pluck('code', 'id'))
|
||||
->afterStateUpdated(fn ($state, $set) => $set('voucher_number', GenerateVoucher::execute(Branch::find($state))))->live(),
|
||||
->options(Client::query()->get()->pluck('company', 'id'))
|
||||
->afterStateUpdated(function ($set, $get) {
|
||||
$set('branch_id', '');
|
||||
$set('voucher_number', static::getVoucherNumber($get));
|
||||
})
|
||||
->live(),
|
||||
Select::make('branch_id')
|
||||
->relationship('branch', 'code')
|
||||
->options(fn ($get) => Branch::query()->where('client_id', $get('client'))->get()->pluck('code', 'id'))
|
||||
->afterStateUpdated(fn ($set, $get) => $set('voucher_number', static::getVoucherNumber($get)))
|
||||
->live(),
|
||||
TextInput::make('supplier')->label('Supplier Name'),
|
||||
TextInput::make('reference_number')->label('Reference Number'),
|
||||
TextInput::make('voucher_number')->label('Voucher Number'),
|
||||
TextInput::make('voucher_number')->label('Voucher Number')
|
||||
->default(fn ($get) => static::getVoucherNumber($get))
|
||||
->readOnly(),
|
||||
DatePicker::make('happened_on')->label('Date')->native(false),
|
||||
|
||||
TableRepeater::make('transactions')
|
||||
->headers([
|
||||
Header::make('Charge Account'),
|
||||
Header::make('Description'),
|
||||
Header::make('Gross Amount'),
|
||||
Header::make('Withholding Tax'),
|
||||
Header::make('Net Amount'),
|
||||
])
|
||||
->schema([
|
||||
Select::make('account_id')->options(fn ($get) => static::getAccountOptions($get)),
|
||||
TextInput::make('description')->label('Description'),
|
||||
TextInput::make('gross_amount'),
|
||||
TextInput::make('payable_withholding_tax'),
|
||||
TextInput::make('net_amount'),
|
||||
])->columnSpan('full'),
|
||||
->headers(fn (Get $get): array => static::getTransactionTableHeader($get))
|
||||
->relationship('transactions')
|
||||
->schema(fn (Get $get): array => static::getTransactionTableFormSchema($get))
|
||||
|
||||
->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('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 getIsVatable(Get $get): bool
|
||||
{
|
||||
$client = Client::find($get('client'));
|
||||
|
||||
return $client && $client->vatable;
|
||||
|
||||
}
|
||||
|
||||
public static function isVatable(bool $value): void
|
||||
{
|
||||
static::$isVatable = $value;
|
||||
dump('sett');
|
||||
}
|
||||
|
||||
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'),
|
||||
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('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()
|
||||
->live()
|
||||
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
|
||||
|
||||
static::setDefaultFormValues($get, $set, $old, $state);
|
||||
})->default(0),
|
||||
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),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -83,6 +205,28 @@ class ExpenseResource extends Resource
|
||||
return $query->get()->pluck('account', 'id');
|
||||
}
|
||||
|
||||
#[NoReturn]
|
||||
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 = $vatableSales / 1.12;
|
||||
|
||||
$inputTax = $vatableAmount * 0.12;
|
||||
|
||||
$netAmount = $get('gross_amount') - $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 table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
|
||||
@@ -16,4 +16,12 @@ class EditExpense extends EditRecord
|
||||
Actions\DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function mutateFormDataBeforeFill(array $data): array
|
||||
{
|
||||
return [
|
||||
'client' => $this->getRecord()->branch->client->id,
|
||||
...$this->getRecord()->toArray(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user