caiocesar173 / laravel-bookings
Laravel 预订包
Requires
- php: ^7.3|^8.0
- caiocesar173/laravel-utils: dev-main
- doctrine/dbal: ^3.4
- laravel/passport: ^10.4
- laravel/socialite: ^5.5
- laravel/ui: ^3.4
- owen-it/laravel-auditing: ^12.0
- prettus/l5-repository: ^2.7
- spatie/eloquent-sortable: ^3.11
- spatie/laravel-schemaless-attributes: ^1.8
- spatie/laravel-sluggable: ^2.6
- spatie/laravel-translatable: ^4.6
- stevebauman/location: ^6.2
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.3
This package is auto-updated.
Last update: 2024-09-27 07:43:24 UTC
README
Laravel Bookings
这是一个用于在您的应用程序中添加预订功能的 Laravel 包。此包基于 laravel-bookings。
待办事项
- 添加取消预订的能力。
- 完成可预订可用性的实现,并编写文档。
注意事项
- Laravel Bookings 假设您的资源模型至少有三个字段,
price作为十进制字段,最后是unit作为字符串字段,分别接受(分钟、小时、天、月)之一。 - Laravel Bookings 不包括支付和订单,因此您需要自己处理。预订价格由此包计算,因此您可能需要挂钩到该过程或监听已保存的预订以开出发票或触发支付过程。
- 您可以扩展 Laravel Bookings 的功能以添加诸如最小和最大单位等功能。这些功能可能在未来某个时候得到本地支持。
安装
-
通过 composer 安装此包
composer require caiocesar173/laravel-bookings
-
通过以下命令执行迁移
php artisan migrate
-
完成!
用法
将可预订功能添加到您的资源模型
要将可预订功能添加到您的资源模型,只需像这样扩展 Caiocesar173\Booking\Entities\Bookable 模型
namespace App\Models; use Caiocesar173\Booking\Entities\Bookable; class Room extends Bookable { ... }
就是这样,您只需在 Room 模型中使用该 trait!现在您的房间将是可预订的。
将可预订功能添加到您的客户模型
要将可预订功能添加到您的客户模型,只需使用 \Caiocesar173\Booking\Traits\HasBookingsTrait trait,如下所示
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Caiocesar173\Booking\Traits\HasBookingsTrait; class Customer extends Model { use HasBookingsTrait; }
再次,这就是您需要做的所有事情!现在您的 Customer 模型可以预订资源。
创建新的预订
创建新的预订非常简单,可以以多种方式完成。让我们看看如何做到这一点
$room = \App\Models\Room::find(1); $customer = \App\Models\Customer::find(1); // Extends \Caiocesar173\Booking\Models\BookableBooking $serviceBooking = new \App\Models\ServiceBooking; // 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' => \Carbon\Carbon::now(), 'ends_at' => \Carbon\Carbon::tomorrow()]) ->customer()->associate($customer) ->bookable()->associate($room) ->save();
注意
- 如您所见,有许多创建新预订的方法,请使用适合您上下文的方法。
- 预订价格会根据资源价格、自定义价格和可预订费率自动计算。
- Laravel Bookings 足够智能,可以检测日期格式并在需要时进行转换,上述示例显示显式正确的格式,但您仍然可以写入类似:“明天下午1点”的内容,它将自动为您转换。
查询预订模型
您可以通过以下方式获取有关特定预订的更多详细信息
// Extends \Caiocesar173\Booking\Models\BookableBooking $serviceBooking = \App\Models\ServiceBooking::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
如预期,您还可以按日期查询预订
// Extends \Caiocesar173\Booking\Models\BookableBooking $serviceBooking = new \App\Models\ServiceBooking; $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 = \App\Models\Room::find(1); $serviceBookingsOfBookable = $serviceBooking->ofBookable($room)->get(); // Get bookings of the given resource $customer = \App\Models\Customer::find(1); $serviceBookingsOfCustomer = $serviceBooking->ofCustomer($customer)->get(); // Get bookings of the given customer
创建新的预订费率
可预订价格是用于修改默认预订价格的特定标准。例如,假设您有一个按小时计费的资源,您需要设置前“2”小时的价格以覆盖某些成本,而预订超过“5”小时则提供折扣价格。这完全可以通过可预订价格实现。只需设置应用此标准的单元数量,并使用加减符号声明您希望增加或减少的默认价格百分比,例如 -10%,当然,您还可以从以下选项中选择操作符:(^表示前X个单元,<表示预订少于X个单元时,>表示预订超过X个单元时)。允许的百分比介于-100%和+100%之间。
要创建新的预订价格,请按照以下步骤操作
$room = \App\Models\Room::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
或者,您可以按以下方式显式创建新的预订价格
$room = \App\Models\Room::find(1); // Extends \Caiocesar173\Booking\Models\BookableFee $serviceRate = new \App\Models\ServiceRate; $serviceRate->make(['percentage' => '15', 'operator' => '^', 'amount' => 2]) ->bookable()->associate($room) ->save();
以下是预订价格的关系
$bookable = $serviceRate->bookable; // Get the owning resource model
注意
- 所有预订价格百分比绝对不应包含
%符号,已知该字段是用于百分比的。- 当添加具有正百分比的新的预订价格时,
+符号不是必需的,并且如果输入,将被省略。
创建新的自定义价格
自定义价格是按照特定的时间标准设置的。例如,假设您有一家共享办公空间业务,您的一个房间是会议室,您希望周一和周三对此收费不同。假设周一从上午9:00到下午5:00是高峰时段,因此您需要收取更多费用,而周三从上午11:30到下午3:45是低谷时段,因此您希望收取较少费用!这完全可以通过自定义价格实现,您可以使用加减百分比设置时间范围及其价格。它的工作方式与可预订价格相同,但基于时间标准。太棒了,不是吗?
要创建自定义价格,请按照以下步骤操作
$room = \App\Models\Room::find(1); $room->newPrice('mon', '09:00:00', '17:00:00', '26'); // Increase pricing on Monday from 09:00 am to 05:00 pm by 26% $room->newPrice('wed', '11:30:00', '15:45:00', '-10.5'); // Decrease pricing on Wednesday from 11:30 am to 03:45 pm by 10.5%
轻而易举,您只需设置日期、从-到时间以及增加/减少单元价格的加减百分比。
以下是自定义价格的关系
$bookable = $room->bookable; // Get the owning resource model
注意
- 如果您没有创建任何自定义价格,则资源将按默认资源价格预订。
- Laravel预订足够智能,可以检测时间格式并在需要时进行转换,上述示例显示了显式正确的格式,但您仍然可以写类似于“上午9:00”的内容,它将为您自动转换。
查询资源模型
您可以使用以下直观API查询资源模型以获取更多详细信息
$room = \App\Models\Room::find(1); $room->bookings; // Get all bookings $room->pastBookings; // Get past bookings $room->futureBookings; // Get future bookings $room->currentBookings; // Get current bookings $room->cancelledBookings; // Get cancelled bookings $room->bookingsStartsBefore('2017-06-21 19:28:51')->get(); // Get bookings starts before the given date $room->bookingsStartsAfter('2017-06-21 19:28:51')->get(); // Get bookings starts after the given date $room->bookingsStartsBetween('2017-06-21 19:28:51', '2017-07-01 12:00:00')->get(); // Get bookings starts between the given dates $room->bookingsEndsBefore('2017-06-21 19:28:51')->get(); // Get bookings starts before the given date $room->bookingsEndsAfter('2017-06-21 19:28:51')->get(); // Get bookings starts after the given date $room->bookingsEndsBetween('2017-06-21 19:28:51', '2017-07-01 12:00:00')->get(); // Get bookings starts between the given dates $room->bookingsCancelledBefore('2017-06-21 19:28:51')->get(); // Get bookings starts before the given date $room->bookingsCancelledAfter('2017-06-21 19:28:51')->get(); // Get bookings starts after the given date $room->bookingsCancelledBetween('2017-06-21 19:28:51', '2017-07-01 12:00:00')->get(); // Get bookings starts between the given dates $customer = \App\Models\Customer::find(1); $room->bookingsOf($customer)->get(); // Get bookings of the given customer $room->rates; // Get all bookable Rates $room->prices; // Get all custom prices
上述所有属性和方法实际上是关系,因此您可以调用原始关系方法并像任何正常的Eloquent关系一样进行链式调用。例如,$room->bookings()->where('starts_at', '>', new \Carbon\Carbon())->first()。
查询客户模型
就像查询您的资源一样,您可以通过查询客户轻松获取相关的预订信息。看看这些示例
$customer = \App\Models\Customer::find(1); $customer->bookings; // Get all bookings $customer->pastBookings; // Get past bookings $customer->futureBookings; // Get future bookings $customer->currentBookings; // Get current bookings $customer->cancelledBookings; // Get cancelled bookings $customer->bookingsStartsBefore('2017-06-21 19:28:51')->get(); // Get bookings starts before the given date $customer->bookingsStartsAfter('2017-06-21 19:28:51')->get(); // Get bookings starts after the given date $customer->bookingsStartsBetween('2017-06-21 19:28:51', '2017-07-01 12:00:00')->get(); // Get bookings starts between the given dates $customer->bookingsEndsBefore('2017-06-21 19:28:51')->get(); // Get bookings starts before the given date $customer->bookingsEndsAfter('2017-06-21 19:28:51')->get(); // Get bookings starts after the given date $customer->bookingsEndsBetween('2017-06-21 19:28:51', '2017-07-01 12:00:00')->get(); // Get bookings starts between the given dates $customer->bookingsCancelledBefore('2017-06-21 19:28:51')->get(); // Get bookings starts before the given date $customer->bookingsCancelledAfter('2017-06-21 19:28:51')->get(); // Get bookings starts after the given date $customer->bookingsCancelledBetween('2017-06-21 19:28:51', '2017-07-01 12:00:00')->get(); // Get bookings starts between the given dates $room = \App\Models\Room::find(1); $customer->isBooked($room); // Check if the customer booked the given room $customer->bookingsOf($room)->get(); // Get bookings by the customer for the given room
与资源模型一样,上述所有属性和方法实际上是关系,因此您可以调用原始关系方法并像任何正常的Eloquent关系一样进行链式调用。例如,$customer->bookings()->where('starts_at', '>', new \Carbon\Carbon())->first()。