Skip to main content
  1. All Posts/

laravel-bookings

Tools PHP

Rinvex Bookings

⚠️ This package is abandoned and no longer maintained. No replacement package was suggested. ⚠️
👉 If you are interested to step on as the main maintainer of this package, please reach out to me!

Rinvex Bookings is a generic resource booking system for Laravel, with the required tools to run your SAAS like services efficiently. It has a simple architecture, with powerful underlying to afford solid platform for your business.



Considerations

  • Rinvex Bookings is for bookable resources, and has nothing to do with price plans and subscriptions. If you’re looking for subscription management system, you may have to look at rinvex/laravel-subscriptions.
  • Rinvex Bookings assumes that your resource model has at least three fields, price as a decimal field, and lastly unit as a string field which accepts one of (minute, hour, day, month) respectively.
  • Payments and ordering are out of scope for Rinvex Bookings, so you’ve to take care of this yourself. Booking price is calculated by this package, so you may need to hook into the process or listen to saved bookings to issue invoice, or trigger payment process.
  • You may extend Rinvex Bookings functionality to add features like: minimum and maximum units, and many more. These features may be supported natively sometime in the future.

Installation

  1. Install the package via composer:

    composer require rinvex/laravel-bookings
  2. Publish resources (migrations and config files):

    php artisan rinvex:publish:bookings
  3. Execute migrations via the following command:

    php artisan rinvex:migrate:bookings
  4. Done!

Usage

Rinvex Bookings has been specially made for Eloquent and simplicity has been taken very serious as in any other Laravel related aspect.

Add bookable functionality to your resource model

To add bookable functionality to your resource model just use the RinvexBookingsTraitsBookable trait like this:

namespace AppModels;

use RinvexBookingsTraitsBookable;
use IlluminateDatabaseEloquentModel;

class Room extends Model
{
    use Bookable;
}

That’s it, you only have to use that trait in your Room model! Now your rooms will be bookable.

Add bookable functionality to your customer model

To add bookable functionality to your customer model just use the RinvexBookingsTraitsHasBookings trait like this:

namespace AppModels;

use IlluminateDatabaseEloquentModel;
use RinvexBookingsTraitsHasBookings;

class Customer extends Model
{
    use HasBookings;
}

Again, that’s all you need to do! Now your Customer model can book resources.

Create a new booking

Creating a new booking is straight forward, and could be done in many ways. Let’s see how could we do that:

$room = AppModelsRoom::find(1);
$customer = AppModelsCustomer::find(1);

// Extends RinvexBookingsModelsBookableBooking
$serviceBooking = new AppModelsServiceBooking;

// Create a new booking via resource model (customer, starts, ends)
$room->newBooking($customer, '2017-07-05 12:44:12', '2017-07-10 18:30:11');

// Create a new booking via customer model (resource, starts, ends)
$customer->newBooking($room, '2017-07-05 12:44:12', '2017-07-10 18:30:11');

// Create a new booking explicitly
$serviceBooking->make(['starts_at' => CarbonCarbon::now(), 'ends_at' => CarbonCarbon::tomorrow()])
        ->customer()->associate($customer)
        ->bookable()->associate($room)
        ->save();

Notes:

  • As you can see, there’s many ways to create a new booking, use whatever suits your context.
  • Booking price is calculated automatically on the fly according to the resource price, custom prices, and bookable Rates.
  • Rinvex Bookings is intelegent enough to detect date format and convert if required, the above example show the explicitly correct format, but you still can write something like: ‘Tomorrow 1pm’ and it will be converted automatically for you.

Query booking models

You can get more details about a specific booking as follows:

// Extends RinvexBookingsModelsBookableBooking
$serviceBooking = AppModelsServiceBooking::find(1);

$bookable = $serviceBooking->bookable; // Get the owning resource model
$customer = $serviceBooking->customer; // Get the owning customer model

$serviceBooking->isPast(); // Check if the booking is past
$serviceBooking->isFuture(); // Check if the booking is future
$serviceBooking->isCurrent(); // Check if the booking is current
$serviceBooking->isCancelled(); // Check if the booking is cancelled

And as expected, you can query bookings by date as well:

// Extends RinvexBookingsModelsBookableBooking
$serviceBooking = new AppModelsServiceBooking;

$pastBookings = $serviceBooking->past(); // Get the past bookings
$futureBookings = $serviceBooking->future(); // Get the future bookings
$currentBookings = $serviceBooking->current(); // Get the current bookings
$cancelledBookings = $serviceBooking->cancelled(); // Get the cancelled bookings

$serviceBookingsAfter = $serviceBooking->startsAfter('2017-06-21 19:28:51')->get(); // Get bookings starts after the given date
$serviceBookingsStartsBefore = $serviceBooking->startsBefore('2017-06-21 19:28:51')->get(); // Get bookings starts before the given date
$serviceBookingsBetween = $serviceBooking->startsBetween('2017-06-21 19:28:51', '2017-07-01 12:00:00')->get(); // Get bookings starts between the given dates

$serviceBookingsEndsAfter = $serviceBooking->endsAfter('2017-06-21 19:28:51')->get(); // Get bookings starts after the given date
$serviceBookingsEndsBefore = $serviceBooking->endsBefore('2017-06-21 19:28:51')->get(); // Get bookings starts before the given date
$serviceBookingsEndsBetween = $serviceBooking->endsBetween('2017-06-21 19:28:51', '2017-07-01 12:00:00')->get(); // Get bookings starts between the given dates

$serviceBookingsCancelledAfter = $serviceBooking->cancelledAfter('2017-06-21 19:28:51')->get(); // Get bookings starts after the given date
$serviceBookingsCancelledBefore = $serviceBooking->cancelledBefore('2017-06-21 19:28:51')->get(); // Get bookings starts before the given date
$serviceBookingsCancelledBetween = $serviceBooking->cancelledBetween('2017-06-21 19:28:51', '2017-07-01 12:00:00')->get(); // Get bookings starts between the given dates

$room = AppModelsRoom::find(1);
$serviceBookingsOfBookable = $serviceBooking->ofBookable($room)->get(); // Get bookings of the given resource

$customer = AppModelsCustomer::find(1);
$serviceBookingsOfCustomer = $serviceBooking->ofCustomer($customer)->get(); // Get bookings of the given customer

Create a new booking rate

Bookable Rates are special criteria used to modify the default booking price. For example, let’s assume that you have a resource charged per hour, and you need to set a higher price for the first “2” hours to cover certain costs, while discounting pricing if booked more than “5” hours. That’s totally achievable through bookable Rates. Simply set the amount of units to apply this criteria on, and state the percentage you’d like to have increased or decreased from the default price using +/- signs, i.e. -10%, and of course select the operator from: (^ means the first starting X units, < means when booking is less than X units, > means when booking is greater than X units). Allowed percentages could be between -100% and +100%.
To create a new booking rate, follow these steps:

$room = AppModelsRoom::find(1);
$room->newRate('15', '^', 2); // Increase unit price by 15% for the first 2 units
$room->newRate('-10', '>', 5); // Decrease unit price by 10% if booking is greater than 5 units

Alternatively you can create a new booking rate explicitly as follows:

$room = AppModelsRoom::find(1);

// Extends RinvexBookingsModelsBookableRate
$serviceRate = new AppModelsServiceRate;

$serviceRate->make(['percentage' => '15', 'operator' => '^', 'amount' => 2])
     ->bookable()->associate($room)
     ->save();

And here’s the booking rate relations:

$bookable = $serviceRate->bookable; // Get the owning resource model

Notes:

  • All booking…