Summary to calculate VAT if not exempt.

Please, I have the following code in my DealsRelationManager.php in my Bookings edit page.
Tables\Columns\TextColumn::make('price')
->money('gbp', divideBy: 100)
->searchable(isIndividual: true)
->sortable()

->summarize(Sum::make()
->label('Sub Total')
->money('gbp', divideBy: 100))

->summarize(
Summarizer::make()
->label('VAT')
->money('gbp', divideBy: 100)
->using(function (\Illuminate\Database\Query\Builder $query) {
$booking = Booking::where('id', 2)->firstOrFail();
$exemptFromVat = $booking->exempt_from_vat;
if (!$exemptFromVat) {
$vatValue = Setting::where('id', 1)->firstOrFail()->bookings_vat;
return $query->sum(DB::raw("price * $vatValue / 100"));
} else {
return 0;
}
})
)
Tables\Columns\TextColumn::make('price')
->money('gbp', divideBy: 100)
->searchable(isIndividual: true)
->sortable()

->summarize(Sum::make()
->label('Sub Total')
->money('gbp', divideBy: 100))

->summarize(
Summarizer::make()
->label('VAT')
->money('gbp', divideBy: 100)
->using(function (\Illuminate\Database\Query\Builder $query) {
$booking = Booking::where('id', 2)->firstOrFail();
$exemptFromVat = $booking->exempt_from_vat;
if (!$exemptFromVat) {
$vatValue = Setting::where('id', 1)->firstOrFail()->bookings_vat;
return $query->sum(DB::raw("price * $vatValue / 100"));
} else {
return 0;
}
})
)
I need to update the code so the hard-coded id '2' In line: $booking = Booking::where('id', 2)->firstOrFail(); is dynamically loaded. Any suggestions, thanks."
2 Replies
Tim van Heugten
Tim van Heugten5mo ago
use Livewire\Component as Livewire;

function (Livewire $livewire) {
$livewire->ownerRecord;
}
use Livewire\Component as Livewire;

function (Livewire $livewire) {
$livewire->ownerRecord;
}
Use livewire component in the closure…
Pablo Torres
Pablo Torres5mo ago
Thank you for help and time... I have followed your advice and now I have it working:
$booking = Booking::where('id', $this->getOwnerRecord()->getKey())->firstOrFail();
$booking = Booking::where('id', $this->getOwnerRecord()->getKey())->firstOrFail();
->summarize(
Summarizer::make()
->label('VAT')
->money('gbp', divideBy: 100)
->using(function (\Illuminate\Database\Query\Builder $query) {
$booking = Booking::where('id', $this->getOwnerRecord()->getKey())->firstOrFail();
$exemptFromVat = $booking->exempt_from_vat;
if (!$exemptFromVat) {
$vatValue = Setting::where('id', 1)->firstOrFail()->bookings_vat;
return $query->sum(DB::raw("price * $vatValue / 100"));
} else {
return 0;
}
})
)
->summarize(
Summarizer::make()
->label('VAT')
->money('gbp', divideBy: 100)
->using(function (\Illuminate\Database\Query\Builder $query) {
$booking = Booking::where('id', $this->getOwnerRecord()->getKey())->firstOrFail();
$exemptFromVat = $booking->exempt_from_vat;
if (!$exemptFromVat) {
$vatValue = Setting::where('id', 1)->firstOrFail()->bookings_vat;
return $query->sum(DB::raw("price * $vatValue / 100"));
} else {
return 0;
}
})
)
From: https://filamentphp.com/docs/3.x/panels/resources/relation-managers#importing-related-records Thank you Tim.