I own something that I frequently rent out. It consists of several parts. It can either be rented part by part, or as a whole. If one part is rented out, you wont be able to rent it as a whole.
An example can be that I rent out a car. This car has tires which also are for rent. You can choose to rent the car with the tires («the whole»), or just rent a tire. However you won't be able to rent the car («the whole») if one or more tires are rented out.
I visualize it as a hierarchy.
Whole
_______|_______
| |
Part 1 Part 2
I have used one Google Calendar for «the whole» thing and separate calendars for each containing part. This works, but it's tiresome and I want to be able to just send a link to those who's interested - where they can see what is available.
So I made a database (mariadb 10.4) that is plain simple, having two tables:
# tbl: part
| id | parent_id | name |
The column parent_id
simply refrences another row in the same table, here's an example of data.
| 1 | NULL | Car |
| 2 | 1 | Tire 1 |
| 3 | 1 | Tire 2 |
Then the following table to store dates when each part is booked
(+ example data).
# tbl: booking
| id | part_id | booked_from | booked_until |
--------------------------------------------
| 1 | 1 | 2021-07-31 | 2021-08-03 |
| 2 | 2 | 2021-08-03 | 2021-08-07 |
| 3 | 3 | 2021-08-04 | 2021-08-06 |
| 4 | 3 | 2021-08-09 | 2021-08-10 |
Out from this we know that the car itself is booked from 2021-07-31
- 2021-08-03
, but its only bookable from 2021-08-06
since two of the tires are rented out for this period (They can however be rented out at the same time as they're not strictly related). But just until the 2021-08-09
since a tire is booked again.
What I'm looking for is a query to get a list of dates when something is available. From the pars
-table I'm able to find out which parts are related and that's not my biggest problem - I guess, since can use something like this when querying availability for:
- The car:
part_id IN(1,2,3)
- Tire 1:
part_id IN(1,2)
- Tire 3:
part_id IN(1,3)
My problem is (simply?) how I can orchestrate a query that returns only the dates when something is available, especially for the car when dates can overlap.
E.g results for the car
SELECT
`booked_until` AS `available-from`,
`booked_from` as `available-until`
FROM
booking
/** some JOIN magic? **
WHERE part_id IN(1,2,3)
E.g for tire 1 would be the same but with part_id IN(1,2)
as tire 2 (id: 3)
isn't directly related with tire 1
.
Both should respectively return:
# car
| available-from | available-until |
------------------------------------
| NULL | 2021-07-31 |
| 2021-08-06 | 2021-08-09 |
| 2021-08-10 | NULL |
# tire 1
| available-from | available-until |
------------------------------------
| NULL | 2021-07-31 |
| 2021-08-07 | NULL |
Where the NULL
-values just says there's nothing booked before or ahead. E.g this tire is available from now
until the 2021-07-31
and from 2021-08-07
until the last day earth exists.
Hope this makes sense - and that someone is able to help.
Thank you in advance.