Apr 3, 2024
0
Level 6
FIlamentPHP Repeator updating individual items
Hi Everyone,
I'm playing around with FilamentPHP and trying to learn some new things but I'm a little stuck on an issue I'm having and hoping someone could point me in the right direction.
I've created a small function that loops over all of my items in my invoice resource form and calculates a subtotal and total. This works great. Adding it here in case anyone needs this part in the future.
public static function updateTotals(Get $get, Set $set): void {
$lineItems = $get('items');
$subtotal = 0;
foreach($lineItems as $item) {
$linetotal = ($item['quantity'] * $item['rate']);
if($item['discount_type'] === 'fixed') {
$linetotal -= $item['discount'];
} else {
$linetotal -= ($item['discount'] / 100 * $linetotal);
}
$subtotal += $linetotal;
}
$set('sub_total', number_format($subtotal, 2, '.', ''));
$set('total', number_format($subtotal - ($subtotal - ($get('discount'))) + ($subtotal * ($get('tax') / 100)), 2, '.', ''));
}
The issue I'm having is with my updateLineTotal function.
public static function updateLineTotal(Get $get, Set $set): void {
$lineTotal = $get('items.quantity') * $get('items.rate');
if($get('items.discount_type') === 'fixed') {
$lineTotal -= $get('items.discount');
} else {
$lineTotal -= ($get('items.discount') / 100 * $lineTotal);
}
$set('line_total', number_format($lineTotal, 2, '.', ''));
}
Here is the basics of my repeater.
Repeater::make('items')
->schema([
TextInput::make('title')
->required(),
TextInput::make('quantity')
->numeric()
->default(1)
->live()
->afterStateUpdated(function (Get $get, Set $set) {
self::updateLineTotal($get, $set);
}),
TextInput::make('rate')
->numeric()
->default(0.00)
->live()
->afterStateUpdated(function (Get $get, Set $set) {
self::updateLineTotal($get, $set);
}),
TextInput::make('discount')
->numeric()
->default(0.00)
->live()
->afterStateUpdated(function (Get $get, Set $set) {
self::updateLineTotal($get, $set);
}),
TextInput::make('line_total')
->numeric()
->default(0.00)
->readOnly()
])
->live()
->relationship()
->afterStateUpdate(function (Get $get, Set $set) {
self::updateTotals($get, $set);
self::updateLineTotal($get, $set);
});
Please or to participate in this conversation.