Here's an interesting one for you all. I've found myself in a implementation conundrum.
I'm developing a booking app in Laravel that allows people to book rooms and equipment for spaces at specific times. The amount of equipment available is limited, so the amount of stock has to be queried in tandem with the time slots associated with the booking.
I thought I'd solved the problem by iterating through the 'other bookings' that occur at the same time and count up the items that are currently in use -> then check that against what's available in stock.
For 90% of my tests this was working fine but have just managed to find a bug that won't allow me to do this.
$guitarheadcount = 0;
$guitarcabcount = 0;
$guitarcombocount = 0;
$bassheadcount = 0;
$basscabcount = 0;
$basscombocount = 0;
$drumkitcount = 0;
$cymbalscount = 0;
$otherbookings = Booking::where('Room_id', '!=', $bookinginfo->Room_id)
->where(function ($query) use ($begin, $end) {
$query->where(function ($q) use ($begin, $end) {
$q->where('Booking_start', '>=', $begin)
->where('Booking_start', '<', $end);
})->orWhere(function ($q) use ($begin, $end) {
$q->where('Booking_start', '<=', $begin)
->where('Booking_end', '>', $end);
})->orWhere(function ($q) use ($begin, $end) {
$q->where('Booking_end', '>', $begin)
->where('Booking_end', '<=', $end);
})->orWhere(function ($q) use ($begin, $end) {
$q->where('Booking_start', '>=', $begin)
->where('Booking_end', '<=', $end);
});
})->get();
//'amounts' are attributes for Booking objects.
foreach($otherbookings as $other){
$guitarheadcount = $other->Equip->guitarheadamount + $guitarheadcount;
$guitarcabcount = $other->Equip->guitarcabamount + $guitarcabcount;
$guitarcombocount = $other->Equip->guitarcomboamount + $guitarcombocount;
$bassheadcount = $other->Equip->bassheadamount + $bassheadcount;
$basscabcount = $other->Equip->basscabamount + $basscabcount;
$basscombocount = $other->Equip->basscomboamount + $basscombocount;
$drumkitcount = $other->Equip->drumkitamount + $drumkitcount;
$cymbalscount = $other->Equip->cymbalsamount + $cymbalscount;
}
I then used individual if statements to redirect if the count wound up larger than the amount in the stock.
An example of the bug in action is:
2 drum kits in stock. Make one booking for 1-2pm... then another in the same room for 2-3pm. Both requiring a drumkit.
If I then try and make a booking in a different space from 1-3pm, the count is already 2, even if one set of drums is free again (If that makes sense).
I'm really stumped. I can't envisage if I need to:
- Continue with 'counting', but produce something that deducts 1 for each item that is in the same room as another (?).
- Whether I should scrap counting completely and write individual queries for each equipment item... and how I would even do that!
Any help would be much obliged.