├── phpstan.neon.dist ├── config └── config.php ├── LICENSE ├── src ├── Console │ └── Commands │ │ ├── PublishCommand.php │ │ ├── RollbackCommand.php │ │ └── MigrateCommand.php ├── Providers │ └── BookingsServiceProvider.php ├── Models │ ├── BookableAvailability.php │ ├── BookableRate.php │ ├── TicketableBooking.php │ ├── Ticketable.php │ ├── Bookable.php │ ├── TicketableTicket.php │ └── BookableBooking.php └── Traits │ ├── HasBookings.php │ ├── Ticketable.php │ ├── Bookable.php │ └── BookingScopes.php ├── database └── migrations │ ├── 2020_01_01_000003_create_bookable_availabilities_table.php │ ├── 2020_01_01_000002_create_bookable_rates_table.php │ ├── 2020_01_01_000005_create_ticketable_bookings_table.php │ ├── 2020_01_01_000004_create_ticketable_tickets_table.php │ └── 2020_01_01_000001_create_bookable_bookings_table.php ├── CONTRIBUTING.md ├── composer.json ├── CODE_OF_CONDUCT.md ├── CHANGELOG.md └── README.md /phpstan.neon.dist: -------------------------------------------------------------------------------- 1 | includes: 2 | - ./vendor/nunomaduro/larastan/extension.neon 3 | parameters: 4 | level: 5 5 | paths: 6 | - src 7 | -------------------------------------------------------------------------------- /config/config.php: -------------------------------------------------------------------------------- 1 | true, 9 | 10 | // Bookings Database Tables 11 | 'tables' => [ 12 | 'bookable_rates' => 'bookable_rates', 13 | 'bookable_bookings' => 'bookable_bookings', 14 | 'bookable_availabilities' => 'bookable_availabilities', 15 | 'ticketable_bookings' => 'ticketable_bookings', 16 | 'ticketable_tickets' => 'ticketable_tickets', 17 | ], 18 | 19 | ]; 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016-2021, Rinvex LLC, 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Console/Commands/PublishCommand.php: -------------------------------------------------------------------------------- 1 | alert($this->description); 33 | 34 | collect($this->option('resource') ?: ['config', 'migrations'])->each(function ($resource) { 35 | $this->call('vendor:publish', ['--tag' => "rinvex/bookings::{$resource}", '--force' => $this->option('force')]); 36 | }); 37 | 38 | $this->line(''); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /database/migrations/2020_01_01_000003_create_bookable_availabilities_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 21 | $table->morphs('bookable'); 22 | $table->string('range'); 23 | $table->string('from')->nullable(); 24 | $table->string('to')->nullable(); 25 | $table->boolean('is_bookable')->default(false); 26 | $table->smallInteger('priority')->unsigned()->nullable(); 27 | $table->timestamps(); 28 | $table->softDeletes(); 29 | }); 30 | } 31 | 32 | /** 33 | * Reverse the migrations. 34 | * 35 | * @return void 36 | */ 37 | public function down(): void 38 | { 39 | Schema::dropIfExists(config('rinvex.bookings.tables.bookable_availabilities')); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /database/migrations/2020_01_01_000002_create_bookable_rates_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 21 | $table->morphs('bookable'); 22 | $table->string('range'); 23 | $table->string('from')->nullable(); 24 | $table->string('to')->nullable(); 25 | $table->string('base_cost')->nullable(); 26 | $table->string('unit_cost')->nullable(); 27 | $table->smallInteger('priority')->unsigned()->nullable(); 28 | $table->timestamps(); 29 | $table->softDeletes(); 30 | }); 31 | } 32 | 33 | /** 34 | * Reverse the migrations. 35 | * 36 | * @return void 37 | */ 38 | public function down(): void 39 | { 40 | Schema::dropIfExists(config('rinvex.bookings.tables.bookable_rates')); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /database/migrations/2020_01_01_000005_create_ticketable_bookings_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 21 | $table->integer('ticket_id')->unsigned(); 22 | $table->integer('customer_id')->unsigned(); 23 | $table->decimal('paid')->default('0.00'); 24 | $table->string('currency', 3)->nullable(); 25 | $table->boolean('is_approved')->default(false); 26 | $table->boolean('is_confirmed')->default(false); 27 | $table->boolean('is_attended')->default(false); 28 | $table->text('notes')->nullable(); 29 | $table->timestamps(); 30 | $table->softDeletes(); 31 | }); 32 | } 33 | 34 | /** 35 | * Reverse the migrations. 36 | * 37 | * @return void 38 | */ 39 | public function down() 40 | { 41 | Schema::dropIfExists(config('rinvex.bookings.tables.ticketable_bookings')); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Console/Commands/RollbackCommand.php: -------------------------------------------------------------------------------- 1 | alert($this->description); 33 | 34 | $path = config('rinvex.bookings.autoload_migrations') ? 35 | 'vendor/rinvex/laravel-bookings/database/migrations' : 36 | 'database/migrations/rinvex/laravel-bookings'; 37 | 38 | if (file_exists($path)) { 39 | $this->call('migrate:reset', [ 40 | '--path' => $path, 41 | '--force' => $this->option('force'), 42 | ]); 43 | } else { 44 | $this->warn('No migrations found! Consider publish them first: php artisan rinvex:publish:bookings'); 45 | } 46 | 47 | $this->line(''); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /database/migrations/2020_01_01_000004_create_ticketable_tickets_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 21 | $table->morphs('ticketable'); 22 | $table->string('slug'); 23 | $table->json('name'); 24 | $table->json('description')->nullable(); 25 | $table->boolean('is_active')->default(true); 26 | $table->decimal('price')->default('0.00'); 27 | $table->string('currency', 3)->nullable(); 28 | $table->integer('quantity')->nullable()->default(-1); 29 | $table->mediumInteger('sort_order')->unsigned()->default(0); 30 | $table->timestamps(); 31 | $table->softDeletes(); 32 | }); 33 | } 34 | 35 | /** 36 | * Reverse the migrations. 37 | * 38 | * @return void 39 | */ 40 | public function down() 41 | { 42 | Schema::dropIfExists(config('rinvex.bookings.tables.ticketable_tickets')); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/Console/Commands/MigrateCommand.php: -------------------------------------------------------------------------------- 1 | alert($this->description); 33 | 34 | $path = config('rinvex.bookings.autoload_migrations') ? 35 | 'vendor/rinvex/laravel-bookings/database/migrations' : 36 | 'database/migrations/rinvex/laravel-bookings'; 37 | 38 | if (file_exists($path)) { 39 | $this->call('migrate', [ 40 | '--step' => true, 41 | '--path' => $path, 42 | '--force' => $this->option('force'), 43 | ]); 44 | } else { 45 | $this->warn('No migrations found! Consider publish them first: php artisan rinvex:publish:bookings'); 46 | } 47 | 48 | $this->line(''); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /database/migrations/2020_01_01_000001_create_bookable_bookings_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 21 | $table->morphs('bookable'); 22 | $table->morphs('customer'); 23 | $table->dateTime('starts_at')->nullable(); 24 | $table->dateTime('ends_at')->nullable(); 25 | $table->dateTime('canceled_at')->nullable(); 26 | $table->string('timezone')->nullable(); 27 | $table->decimal('price')->default('0.00'); 28 | $table->integer('quantity')->unsigned(); 29 | $table->decimal('total_paid')->default('0.00'); 30 | $table->string('currency', 3); 31 | $table->json('formula')->nullable(); 32 | $table->schemalessAttributes('options'); 33 | $table->text('notes')->nullable(); 34 | $table->timestamps(); 35 | $table->softDeletes(); 36 | }); 37 | } 38 | 39 | /** 40 | * Reverse the migrations. 41 | * 42 | * @return void 43 | */ 44 | public function down(): void 45 | { 46 | Schema::dropIfExists(config('rinvex.bookings.tables.bookable_bookings')); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/Providers/BookingsServiceProvider.php: -------------------------------------------------------------------------------- 1 | 'command.rinvex.bookings.migrate', 24 | PublishCommand::class => 'command.rinvex.bookings.publish', 25 | RollbackCommand::class => 'command.rinvex.bookings.rollback', 26 | ]; 27 | 28 | /** 29 | * Register the application services. 30 | * 31 | * @return void 32 | */ 33 | public function register(): void 34 | { 35 | $this->mergeConfigFrom(realpath(__DIR__.'/../../config/config.php'), 'rinvex.bookings'); 36 | 37 | // Register console commands 38 | $this->registerCommands($this->commands); 39 | } 40 | 41 | /** 42 | * Bootstrap the application services. 43 | * 44 | * @return void 45 | */ 46 | public function boot(): void 47 | { 48 | // Publish Resources 49 | $this->publishesConfig('rinvex/laravel-bookings'); 50 | $this->publishesMigrations('rinvex/laravel-bookings'); 51 | ! $this->autoloadMigrations('rinvex/laravel-bookings') || $this->loadMigrationsFrom(__DIR__.'/../../database/migrations'); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contribution Guide 2 | 3 | This project adheres to the following standards and practices. 4 | 5 | 6 | ## Versioning 7 | 8 | This project is versioned under the [Semantic Versioning](http://semver.org/) guidelines as much as possible. 9 | 10 | Releases will be numbered with the following format: 11 | 12 | - `..` 13 | - `..` 14 | 15 | And constructed with the following guidelines: 16 | 17 | - Breaking backward compatibility bumps the major and resets the minor and patch. 18 | - New additions without breaking backward compatibility bump the minor and reset the patch. 19 | - Bug fixes and misc changes bump the patch. 20 | 21 | 22 | ## Pull Requests 23 | 24 | The pull request process differs for new features and bugs. 25 | 26 | Pull requests for bugs may be sent without creating any proposal issue. If you believe that you know of a solution for a bug that has been filed, please leave a comment detailing your proposed fix or create a pull request with the fix mentioning that issue id. 27 | 28 | 29 | ## Coding Standards 30 | 31 | This project follows the FIG PHP Standards Recommendations compliant with the [PSR-1: Basic Coding Standard](http://www.php-fig.org/psr/psr-1/), [PSR-2: Coding Style Guide](http://www.php-fig.org/psr/psr-2/) and [PSR-4: Autoloader](http://www.php-fig.org/psr/psr-4/) to ensure a high level of interoperability between shared PHP code. If you notice any compliance oversights, please send a patch via pull request. 32 | 33 | 34 | ## Feature Requests 35 | 36 | If you have a proposal or a feature request, you may create an issue with `[Proposal]` in the title. 37 | 38 | The proposal should also describe the new feature, as well as implementation ideas. The proposal will then be reviewed and either approved or denied. Once a proposal is approved, a pull request may be created implementing the new feature. 39 | 40 | 41 | ## Git Flow 42 | 43 | This project follows [Git-Flow](http://nvie.com/posts/a-successful-git-branching-model/), and as such has `master` (latest stable releases), `develop` (latest WIP development) and X.Y support branches (when there's multiple major versions). 44 | 45 | Accordingly all pull requests MUST be sent to the `develop` branch. 46 | 47 | > **Note:** Pull requests which do not follow these guidelines will be closed without any further notice. 48 | -------------------------------------------------------------------------------- /src/Models/BookableAvailability.php: -------------------------------------------------------------------------------- 1 | 'integer', 35 | 'bookable_type' => 'string', 36 | 'range' => 'string', 37 | 'from' => 'string', 38 | 'to' => 'string', 39 | 'is_bookable' => 'boolean', 40 | 'priority' => 'integer', 41 | ]; 42 | 43 | /** 44 | * {@inheritdoc} 45 | */ 46 | protected $observables = [ 47 | 'validating', 48 | 'validated', 49 | ]; 50 | 51 | /** 52 | * The default rules that the model will validate against. 53 | * 54 | * @var array 55 | */ 56 | protected $rules = []; 57 | 58 | /** 59 | * Whether the model should throw a 60 | * ValidationException if it fails validation. 61 | * 62 | * @var bool 63 | */ 64 | protected $throwValidationExceptions = true; 65 | 66 | /** 67 | * Create a new Eloquent model instance. 68 | * 69 | * @param array $attributes 70 | */ 71 | public function __construct(array $attributes = []) 72 | { 73 | $this->setTable(config('rinvex.bookings.tables.bookable_availabilities')); 74 | $this->mergeRules([ 75 | 'bookable_id' => 'required|integer', 76 | 'bookable_type' => 'required|string|strip_tags|max:150', 77 | 'range' => 'required|in:datetimes,dates,months,weeks,days,times,sunday,monday,tuesday,wednesday,thursday,friday,saturday', 78 | 'from' => 'required|string|strip_tags|max:150', 79 | 'to' => 'required|string|strip_tags|max:150', 80 | 'is_bookable' => 'required|boolean', 81 | 'priority' => 'nullable|integer', 82 | ]); 83 | 84 | parent::__construct($attributes); 85 | } 86 | 87 | /** 88 | * Get the owning resource model. 89 | * 90 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 91 | */ 92 | public function bookable(): MorphTo 93 | { 94 | return $this->morphTo('bookable', 'bookable_type', 'bookable_id', 'id'); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rinvex/laravel-bookings", 3 | "description": "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.", 4 | "type": "library", 5 | "keywords": [ 6 | "cost", 7 | "usage", 8 | "rinvex", 9 | "booking", 10 | "resource", 11 | "schedules", 12 | "database", 13 | "laravel", 14 | "price", 15 | "rates" 16 | ], 17 | "license": "MIT", 18 | "homepage": "https://rinvex.com", 19 | "support": { 20 | "email": "help@rinvex.com", 21 | "issues": "https://github.com/rinvex/laravel-bookings/issues", 22 | "source": "https://github.com/rinvex/laravel-bookings", 23 | "docs": "https://github.com/rinvex/laravel-bookings/blob/master/README.md" 24 | }, 25 | "authors": [ 26 | { 27 | "name": "Rinvex LLC", 28 | "homepage": "https://rinvex.com", 29 | "email": "help@rinvex.com" 30 | }, 31 | { 32 | "name": "Abdelrahman Omran", 33 | "homepage": "https://omranic.com", 34 | "email": "me@omranic.com", 35 | "role": "Project Lead" 36 | }, 37 | { 38 | "name": "The Generous Laravel Community", 39 | "homepage": "https://github.com/rinvex/laravel-bookings/contributors" 40 | } 41 | ], 42 | "require": { 43 | "php": "^8.0.0", 44 | "illuminate/console": "^9.0.0 || ^10.0.0", 45 | "illuminate/database": "^9.0.0 || ^10.0.0", 46 | "illuminate/support": "^9.0.0 || ^10.0.0", 47 | "rinvex/laravel-support": "^6.0.0", 48 | "spatie/eloquent-sortable": "^4.0.0", 49 | "spatie/laravel-schemaless-attributes": "^2.3.0", 50 | "spatie/laravel-sluggable": "^3.3.0", 51 | "spatie/laravel-translatable": "^5.2.0" 52 | }, 53 | "require-dev": { 54 | "codedungeon/phpunit-result-printer": "^0.31.0", 55 | "illuminate/container": "^9.0.0 || ^10.0.0", 56 | "phpunit/phpunit": "^9.5.0" 57 | }, 58 | "autoload": { 59 | "psr-4": { 60 | "Rinvex\\Bookings\\": "src" 61 | } 62 | }, 63 | "autoload-dev": { 64 | "psr-4": { 65 | "Rinvex\\Bookings\\Tests\\": "tests" 66 | } 67 | }, 68 | "scripts": { 69 | "test": "vendor/bin/phpunit" 70 | }, 71 | "config": { 72 | "sort-packages": true, 73 | "preferred-install": "dist", 74 | "optimize-autoloader": true 75 | }, 76 | "extra": { 77 | "laravel": { 78 | "providers": [ 79 | "Rinvex\\Bookings\\Providers\\BookingsServiceProvider" 80 | ] 81 | } 82 | }, 83 | "minimum-stability": "dev", 84 | "prefer-stable": true 85 | } 86 | -------------------------------------------------------------------------------- /src/Models/BookableRate.php: -------------------------------------------------------------------------------- 1 | 'integer', 36 | 'bookable_type' => 'string', 37 | 'range' => 'string', 38 | 'from' => 'string', 39 | 'to' => 'string', 40 | 'base_cost' => 'float', 41 | 'unit_cost' => 'float', 42 | 'priority' => 'integer', 43 | ]; 44 | 45 | /** 46 | * {@inheritdoc} 47 | */ 48 | protected $observables = [ 49 | 'validating', 50 | 'validated', 51 | ]; 52 | 53 | /** 54 | * The default rules that the model will validate against. 55 | * 56 | * @var array 57 | */ 58 | protected $rules = []; 59 | 60 | /** 61 | * Whether the model should throw a 62 | * ValidationException if it fails validation. 63 | * 64 | * @var bool 65 | */ 66 | protected $throwValidationExceptions = true; 67 | 68 | /** 69 | * Create a new Eloquent model instance. 70 | * 71 | * @param array $attributes 72 | */ 73 | public function __construct(array $attributes = []) 74 | { 75 | $this->setTable(config('rinvex.bookings.tables.bookable_rates')); 76 | $this->mergeRules([ 77 | 'bookable_id' => 'required|integer', 78 | 'bookable_type' => 'required|string|strip_tags|max:150', 79 | 'range' => 'required|in:datetimes,dates,months,weeks,days,times,sunday,monday,tuesday,wednesday,thursday,friday,saturday', 80 | 'from' => 'required|string|strip_tags|max:150', 81 | 'to' => 'required|string|strip_tags|max:150', 82 | 'base_cost' => 'nullable|numeric', 83 | 'unit_cost' => 'required|numeric', 84 | 'priority' => 'nullable|integer', 85 | ]); 86 | 87 | parent::__construct($attributes); 88 | } 89 | 90 | /** 91 | * Get the owning resource model. 92 | * 93 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 94 | */ 95 | public function bookable(): MorphTo 96 | { 97 | return $this->morphTo('bookable', 'bookable_type', 'bookable_id', 'id'); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/Traits/HasBookings.php: -------------------------------------------------------------------------------- 1 | bookings()->delete(); 44 | }); 45 | } 46 | 47 | /** 48 | * The customer may have many bookings. 49 | * 50 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 51 | */ 52 | public function bookings(): MorphMany 53 | { 54 | return $this->morphMany(static::getBookingModel(), 'customer', 'customer_type', 'customer_id'); 55 | } 56 | 57 | /** 58 | * Get bookings of the given resource. 59 | * 60 | * @param \Illuminate\Database\Eloquent\Model $bookable 61 | * 62 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 63 | */ 64 | public function bookingsOf(Model $bookable): MorphMany 65 | { 66 | return $this->bookings()->where('bookable_type', $bookable->getMorphClass())->where('bookable_id', $bookable->getKey()); 67 | } 68 | 69 | /** 70 | * Check if the person booked the given model. 71 | * 72 | * @param \Illuminate\Database\Eloquent\Model $bookable 73 | * 74 | * @return bool 75 | */ 76 | public function isBooked(Model $bookable): bool 77 | { 78 | return $this->bookings()->where('bookable_id', $bookable->getKey())->exists(); 79 | } 80 | 81 | /** 82 | * Book the given model at the given dates with the given price. 83 | * 84 | * @param \Illuminate\Database\Eloquent\Model $bookable 85 | * @param string $startsAt 86 | * @param string $endsAt 87 | * 88 | * @return \Rinvex\Bookings\Models\BookableBooking 89 | */ 90 | public function newBooking(Model $bookable, string $startsAt, string $endsAt): BookableBooking 91 | { 92 | return $this->bookings()->create([ 93 | 'bookable_id' => $bookable->getKey(), 94 | 'bookable_type' => $bookable->getMorphClass(), 95 | 'customer_id' => $this->getKey(), 96 | 'customer_type' => $this->getMorphClass(), 97 | 'starts_at' => $startsAt, 98 | 'ends_at' => $endsAt, 99 | ]); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at [help@rinvex.com](mailto:help@rinvex.com). All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at [http://contributor-covenant.org/version/1/4][version] 72 | 73 | [homepage]: http://contributor-covenant.org 74 | [version]: http://contributor-covenant.org/version/1/4/ 75 | -------------------------------------------------------------------------------- /src/Models/TicketableBooking.php: -------------------------------------------------------------------------------- 1 | 'integer', 37 | 'customer_id' => 'integer', 38 | 'paid' => 'float', 39 | 'currency' => 'string', 40 | 'is_approved' => 'boolean', 41 | 'is_confirmed' => 'boolean', 42 | 'is_attended' => 'boolean', 43 | 'notes' => 'string', 44 | 'deleted_at' => 'datetime', 45 | ]; 46 | 47 | /** 48 | * {@inheritdoc} 49 | */ 50 | protected $observables = [ 51 | 'validating', 52 | 'validated', 53 | ]; 54 | 55 | /** 56 | * The default rules that the model will validate against. 57 | * 58 | * @var array 59 | */ 60 | protected $rules = []; 61 | 62 | /** 63 | * Whether the model should throw a 64 | * ValidationException if it fails validation. 65 | * 66 | * @var bool 67 | */ 68 | protected $throwValidationExceptions = true; 69 | 70 | /** 71 | * Create a new Eloquent model instance. 72 | * 73 | * @param array $attributes 74 | */ 75 | public function __construct(array $attributes = []) 76 | { 77 | $this->setTable(config('rinvex.bookings.tables.ticketable_bookings')); 78 | $this->mergeRules([ 79 | 'ticket_id' => 'required|integer', 80 | 'customer_id' => 'required|integer', 81 | 'paid' => 'required|numeric', 82 | 'currency' => 'required|alpha|size:3', 83 | 'is_approved' => 'sometimes|boolean', 84 | 'is_confirmed' => 'sometimes|boolean', 85 | 'is_attended' => 'sometimes|boolean', 86 | 'notes' => 'nullable|string|strip_tags|max:32768', 87 | ]); 88 | 89 | parent::__construct($attributes); 90 | } 91 | 92 | /** 93 | * Get the active resources. 94 | * 95 | * @param \Illuminate\Database\Eloquent\Builder $builder 96 | * 97 | * @return \Illuminate\Database\Eloquent\Builder 98 | */ 99 | public function scopeActive(Builder $builder): Builder 100 | { 101 | return $builder->where('is_active', true); 102 | } 103 | 104 | /** 105 | * Get the inactive resources. 106 | * 107 | * @param \Illuminate\Database\Eloquent\Builder $builder 108 | * 109 | * @return \Illuminate\Database\Eloquent\Builder 110 | */ 111 | public function scopeInactive(Builder $builder): Builder 112 | { 113 | return $builder->where('is_active', false); 114 | } 115 | 116 | /** 117 | * Get the options for generating the slug. 118 | * 119 | * @return \Spatie\Sluggable\SlugOptions 120 | */ 121 | public function getSlugOptions(): SlugOptions 122 | { 123 | return SlugOptions::create() 124 | ->doNotGenerateSlugsOnUpdate() 125 | ->generateSlugsFrom('name') 126 | ->saveSlugsTo('slug'); 127 | } 128 | 129 | /** 130 | * Activate the resource. 131 | * 132 | * @return $this 133 | */ 134 | public function activate() 135 | { 136 | $this->update(['is_active' => true]); 137 | 138 | return $this; 139 | } 140 | 141 | /** 142 | * Deactivate the resource. 143 | * 144 | * @return $this 145 | */ 146 | public function deactivate() 147 | { 148 | $this->update(['is_active' => false]); 149 | 150 | return $this; 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /src/Traits/Ticketable.php: -------------------------------------------------------------------------------- 1 | bookings()->delete(); 67 | }); 68 | } 69 | 70 | /** 71 | * Attach the given bookings to the model. 72 | * 73 | * @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids 74 | * @param mixed $bookings 75 | * 76 | * @return void 77 | */ 78 | public function setBookingsAttribute($bookings): void 79 | { 80 | static::saved(function (self $model) use ($bookings) { 81 | $this->bookings()->sync($bookings); 82 | }); 83 | } 84 | 85 | /** 86 | * The resource may have many tickets. 87 | * 88 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 89 | */ 90 | public function tickets(): MorphMany 91 | { 92 | return $this->morphMany(static::getTicketModel(), 'ticketable', 'ticketable_type', 'ticketable_id'); 93 | } 94 | 95 | /** 96 | * The resource may have many bookings. 97 | * 98 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 99 | */ 100 | public function bookings(): MorphMany 101 | { 102 | return $this->morphMany(static::getBookingModel(), 'ticketable', 'ticketable_type', 'ticketable_id'); 103 | } 104 | 105 | /** 106 | * Get bookings by the given customer. 107 | * 108 | * @param \Illuminate\Database\Eloquent\Model $customer 109 | * 110 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 111 | */ 112 | public function bookingsBy(Model $customer): MorphMany 113 | { 114 | return $this->bookings()->where('customer_type', $customer->getMorphClass())->where('customer_id', $customer->getKey()); 115 | } 116 | 117 | /** 118 | * Book the model for the given customer at the given dates with the given price. 119 | * 120 | * @param \Illuminate\Database\Eloquent\Model $customer 121 | * @param float $paid 122 | * @param string $currency 123 | * 124 | * @return \Rinvex\Bookings\Models\TicketableBooking 125 | */ 126 | public function newBooking(Model $customer, float $paid, string $currency): TicketableBooking 127 | { 128 | return $this->bookings()->create([ 129 | 'ticketable_id' => static::getKey(), 130 | 'ticketable_type' => static::getMorphClass(), 131 | 'customer_id' => $customer->getKey(), 132 | 'customer_type' => $customer->getMorphClass(), 133 | 'paid' => $paid, 134 | 'currency' => $currency, 135 | ]); 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /src/Models/Ticketable.php: -------------------------------------------------------------------------------- 1 | 'string', 43 | 'name' => 'string', 44 | 'description' => 'string', 45 | 'is_public' => 'boolean', 46 | 'starts_at' => 'datetime', 47 | 'ends_at' => 'datetime', 48 | 'timezone' => 'string', 49 | 'location' => 'string', 50 | 'deleted_at' => 'datetime', 51 | ]; 52 | 53 | /** 54 | * {@inheritdoc} 55 | */ 56 | protected $observables = [ 57 | 'validating', 58 | 'validated', 59 | ]; 60 | 61 | /** 62 | * {@inheritdoc} 63 | */ 64 | public $translatable = [ 65 | 'name', 66 | 'description', 67 | ]; 68 | 69 | /** 70 | * The default rules that the model will validate against. 71 | * 72 | * @var array 73 | */ 74 | protected $rules = []; 75 | 76 | /** 77 | * Whether the model should throw a 78 | * ValidationException if it fails validation. 79 | * 80 | * @var bool 81 | */ 82 | protected $throwValidationExceptions = true; 83 | 84 | /** 85 | * Create a new Eloquent model instance. 86 | * 87 | * @param array $attributes 88 | */ 89 | public function __construct(array $attributes = []) 90 | { 91 | $this->mergeRules([ 92 | 'slug' => 'required|alpha_dash|max:150', 93 | 'name' => 'required|string|strip_tags|max:150', 94 | 'description' => 'nullable|string|max:32768', 95 | 'is_public' => 'sometimes|boolean', 96 | 'starts_at' => 'required|date', 97 | 'ends_at' => 'required|date', 98 | 'timezone' => 'nullable|string|max:64|timezone', 99 | 'location' => 'nullable|string|strip_tags|max:1500', 100 | ]); 101 | 102 | parent::__construct($attributes); 103 | } 104 | 105 | /** 106 | * Get the public resources. 107 | * 108 | * @param \Illuminate\Database\Eloquent\Builder $builder 109 | * 110 | * @return \Illuminate\Database\Eloquent\Builder 111 | */ 112 | public function scopePublic(Builder $builder): Builder 113 | { 114 | return $builder->where('is_public', true); 115 | } 116 | 117 | /** 118 | * Get the private resources. 119 | * 120 | * @param \Illuminate\Database\Eloquent\Builder $builder 121 | * 122 | * @return \Illuminate\Database\Eloquent\Builder 123 | */ 124 | public function scopePrivate(Builder $builder): Builder 125 | { 126 | return $builder->where('is_public', false); 127 | } 128 | 129 | /** 130 | * Get the options for generating the slug. 131 | * 132 | * @return \Spatie\Sluggable\SlugOptions 133 | */ 134 | public function getSlugOptions(): SlugOptions 135 | { 136 | return SlugOptions::create() 137 | ->doNotGenerateSlugsOnUpdate() 138 | ->generateSlugsFrom('name') 139 | ->saveSlugsTo('slug'); 140 | } 141 | 142 | /** 143 | * Activate the resource. 144 | * 145 | * @return $this 146 | */ 147 | public function makePublic() 148 | { 149 | $this->update(['is_public' => true]); 150 | 151 | return $this; 152 | } 153 | 154 | /** 155 | * Deactivate the resource. 156 | * 157 | * @return $this 158 | */ 159 | public function makePrivate() 160 | { 161 | $this->update(['is_public' => false]); 162 | 163 | return $this; 164 | } 165 | } 166 | -------------------------------------------------------------------------------- /src/Models/Bookable.php: -------------------------------------------------------------------------------- 1 | 'string', 53 | 'name' => 'string', 54 | 'description' => 'string', 55 | 'is_active' => 'boolean', 56 | 'base_cost' => 'float', 57 | 'unit_cost' => 'float', 58 | 'currency' => 'string', 59 | 'unit' => 'string', 60 | 'maximum_units' => 'integer', 61 | 'minimum_units' => 'integer', 62 | 'is_cancelable' => 'boolean', 63 | 'is_recurring' => 'boolean', 64 | 'sort_order' => 'integer', 65 | 'capacity' => 'integer', 66 | 'style' => 'string', 67 | 'deleted_at' => 'datetime', 68 | ]; 69 | 70 | /** 71 | * {@inheritdoc} 72 | */ 73 | protected $observables = [ 74 | 'validating', 75 | 'validated', 76 | ]; 77 | 78 | /** 79 | * {@inheritdoc} 80 | */ 81 | public $translatable = [ 82 | 'name', 83 | 'description', 84 | ]; 85 | 86 | /** 87 | * {@inheritdoc} 88 | */ 89 | public $sortable = [ 90 | 'order_column_name' => 'sort_order', 91 | ]; 92 | 93 | /** 94 | * The default rules that the model will validate against. 95 | * 96 | * @var array 97 | */ 98 | protected $rules = []; 99 | 100 | /** 101 | * Create a new Eloquent model instance. 102 | * 103 | * @param array $attributes 104 | */ 105 | public function __construct(array $attributes = []) 106 | { 107 | $this->mergeRules([ 108 | 'slug' => 'required|alpha_dash|max:150', 109 | 'name' => 'required|string|strip_tags|max:150', 110 | 'description' => 'nullable|string|max:32768', 111 | 'is_active' => 'sometimes|boolean', 112 | 'base_cost' => 'required|numeric', 113 | 'unit_cost' => 'required|numeric', 114 | 'currency' => 'required|string|size:3', 115 | 'unit' => 'required|in:minute,hour,day,month', 116 | 'maximum_units' => 'nullable|integer|max:100000', 117 | 'minimum_units' => 'nullable|integer|max:100000', 118 | 'is_cancelable' => 'nullable|boolean', 119 | 'is_recurring' => 'nullable|boolean', 120 | 'sort_order' => 'nullable|integer|max:100000', 121 | 'capacity' => 'nullable|integer|max:100000', 122 | 'style' => 'nullable|string|strip_tags|max:150', 123 | ]); 124 | 125 | parent::__construct($attributes); 126 | } 127 | 128 | /** 129 | * Get the active resources. 130 | * 131 | * @param \Illuminate\Database\Eloquent\Builder $builder 132 | * 133 | * @return \Illuminate\Database\Eloquent\Builder 134 | */ 135 | public function scopeActive(Builder $builder): Builder 136 | { 137 | return $builder->where('is_active', true); 138 | } 139 | 140 | /** 141 | * Get the inactive resources. 142 | * 143 | * @param \Illuminate\Database\Eloquent\Builder $builder 144 | * 145 | * @return \Illuminate\Database\Eloquent\Builder 146 | */ 147 | public function scopeInactive(Builder $builder): Builder 148 | { 149 | return $builder->where('is_active', false); 150 | } 151 | 152 | /** 153 | * Get the options for generating the slug. 154 | * 155 | * @return \Spatie\Sluggable\SlugOptions 156 | */ 157 | public function getSlugOptions(): SlugOptions 158 | { 159 | return SlugOptions::create() 160 | ->doNotGenerateSlugsOnUpdate() 161 | ->generateSlugsFrom('name') 162 | ->saveSlugsTo('slug'); 163 | } 164 | 165 | /** 166 | * Activate the resource. 167 | * 168 | * @return $this 169 | */ 170 | public function activate() 171 | { 172 | $this->update(['is_active' => true]); 173 | 174 | return $this; 175 | } 176 | 177 | /** 178 | * Deactivate the resource. 179 | * 180 | * @return $this 181 | */ 182 | public function deactivate() 183 | { 184 | $this->update(['is_active' => false]); 185 | 186 | return $this; 187 | } 188 | } 189 | -------------------------------------------------------------------------------- /src/Models/TicketableTicket.php: -------------------------------------------------------------------------------- 1 | 'integer', 47 | 'ticketable_type' => 'string', 48 | 'slug' => 'string', 49 | 'name' => 'string', 50 | 'description' => 'string', 51 | 'is_active' => 'boolean', 52 | 'price' => 'float', 53 | 'currency' => 'string', 54 | 'quantity' => 'integer', 55 | 'sort_order' => 'integer', 56 | 'deleted_at' => 'datetime', 57 | ]; 58 | 59 | /** 60 | * {@inheritdoc} 61 | */ 62 | protected $observables = [ 63 | 'validating', 64 | 'validated', 65 | ]; 66 | 67 | /** 68 | * {@inheritdoc} 69 | */ 70 | public $translatable = [ 71 | 'name', 72 | 'description', 73 | ]; 74 | 75 | /** 76 | * {@inheritdoc} 77 | */ 78 | public $sortable = [ 79 | 'order_column_name' => 'sort_order', 80 | ]; 81 | 82 | /** 83 | * The default rules that the model will validate against. 84 | * 85 | * @var array 86 | */ 87 | protected $rules = []; 88 | 89 | /** 90 | * Whether the model should throw a 91 | * ValidationException if it fails validation. 92 | * 93 | * @var bool 94 | */ 95 | protected $throwValidationExceptions = true; 96 | 97 | /** 98 | * Create a new Eloquent model instance. 99 | * 100 | * @param array $attributes 101 | */ 102 | public function __construct(array $attributes = []) 103 | { 104 | $this->setTable(config('rinvex.bookings.tables.ticketable_tickets')); 105 | $this->mergeRules([ 106 | 'ticketable_id' => 'required|integer', 107 | 'ticketable_type' => 'required|string|strip_tags|max:150', 108 | 'slug' => 'required|alpha_dash|max:150|unique:'.config('rinvex.bookings.tables.ticketable_tickets').',slug,NULL,id,ticketable_id,'.($this->ticketable_id ?? 'null').',ticketable_type,'.($this->ticketable_type ?? 'null'), 109 | 'name' => 'required|string|strip_tags|max:150', 110 | 'description' => 'nullable|string|max:32768', 111 | 'is_active' => 'sometimes|boolean', 112 | 'price' => 'required|numeric', 113 | 'currency' => 'required|alpha|size:3', 114 | 'quantity' => 'nullable|integer|max:100000', 115 | 'sort_order' => 'nullable|integer|max:100000', 116 | ]); 117 | 118 | parent::__construct($attributes); 119 | } 120 | 121 | /** 122 | * Get the active resources. 123 | * 124 | * @param \Illuminate\Database\Eloquent\Builder $builder 125 | * 126 | * @return \Illuminate\Database\Eloquent\Builder 127 | */ 128 | public function scopeActive(Builder $builder): Builder 129 | { 130 | return $builder->where('is_active', true); 131 | } 132 | 133 | /** 134 | * Get the inactive resources. 135 | * 136 | * @param \Illuminate\Database\Eloquent\Builder $builder 137 | * 138 | * @return \Illuminate\Database\Eloquent\Builder 139 | */ 140 | public function scopeInactive(Builder $builder): Builder 141 | { 142 | return $builder->where('is_active', false); 143 | } 144 | 145 | /** 146 | * Get the options for generating the slug. 147 | * 148 | * @return \Spatie\Sluggable\SlugOptions 149 | */ 150 | public function getSlugOptions(): SlugOptions 151 | { 152 | return SlugOptions::create() 153 | ->doNotGenerateSlugsOnUpdate() 154 | ->generateSlugsFrom('name') 155 | ->saveSlugsTo('slug'); 156 | } 157 | 158 | /** 159 | * Activate the resource. 160 | * 161 | * @return $this 162 | */ 163 | public function activate() 164 | { 165 | $this->update(['is_active' => true]); 166 | 167 | return $this; 168 | } 169 | 170 | /** 171 | * Deactivate the resource. 172 | * 173 | * @return $this 174 | */ 175 | public function deactivate() 176 | { 177 | $this->update(['is_active' => false]); 178 | 179 | return $this; 180 | } 181 | 182 | /** 183 | * Get the owning resource model. 184 | * 185 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 186 | */ 187 | public function ticketable(): MorphTo 188 | { 189 | return $this->morphTo('ticketable', 'ticketable_type', 'ticketable_id', 'id'); 190 | } 191 | } 192 | -------------------------------------------------------------------------------- /src/Traits/Bookable.php: -------------------------------------------------------------------------------- 1 | bookings()->delete(); 77 | }); 78 | } 79 | 80 | /** 81 | * Attach the given bookings to the model. 82 | * 83 | * @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids 84 | * @param mixed $bookings 85 | * 86 | * @return void 87 | */ 88 | public function setBookingsAttribute($bookings): void 89 | { 90 | static::saved(function (self $model) use ($bookings) { 91 | $this->bookings()->sync($bookings); 92 | }); 93 | } 94 | 95 | /** 96 | * Attach the given rates to the model. 97 | * 98 | * @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids 99 | * @param mixed $rates 100 | * 101 | * @return void 102 | */ 103 | public function setRatesAttribute($rates): void 104 | { 105 | static::saved(function (self $model) use ($rates) { 106 | $this->rates()->sync($rates); 107 | }); 108 | } 109 | 110 | /** 111 | * Attach the given availabilities to the model. 112 | * 113 | * @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|array $ids 114 | * @param mixed $availabilities 115 | * 116 | * @return void 117 | */ 118 | public function setAvailabilitiesAttribute($availabilities): void 119 | { 120 | static::saved(function (self $model) use ($availabilities) { 121 | $this->availabilities()->sync($availabilities); 122 | }); 123 | } 124 | 125 | /** 126 | * The resource may have many bookings. 127 | * 128 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 129 | */ 130 | public function bookings(): MorphMany 131 | { 132 | return $this->morphMany(static::getBookingModel(), 'bookable', 'bookable_type', 'bookable_id'); 133 | } 134 | 135 | /** 136 | * Get bookings by the given customer. 137 | * 138 | * @param \Illuminate\Database\Eloquent\Model $customer 139 | * 140 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 141 | */ 142 | public function bookingsBy(Model $customer): MorphMany 143 | { 144 | return $this->bookings()->where('customer_type', $customer->getMorphClass())->where('customer_id', $customer->getKey()); 145 | } 146 | 147 | /** 148 | * The resource may have many availabilities. 149 | * 150 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 151 | */ 152 | public function availabilities(): MorphMany 153 | { 154 | return $this->morphMany(static::getAvailabilityModel(), 'bookable', 'bookable_type', 'bookable_id'); 155 | } 156 | 157 | /** 158 | * The resource may have many rates. 159 | * 160 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 161 | */ 162 | public function rates(): MorphMany 163 | { 164 | return $this->morphMany(static::getRateModel(), 'bookable', 'bookable_type', 'bookable_id'); 165 | } 166 | 167 | /** 168 | * Book the model for the given customer at the given dates with the given price. 169 | * 170 | * @param \Illuminate\Database\Eloquent\Model $customer 171 | * @param string $startsAt 172 | * @param string $endsAt 173 | * 174 | * @return \Rinvex\Bookings\Models\BookableBooking 175 | */ 176 | public function newBooking(Model $customer, string $startsAt, string $endsAt): BookableBooking 177 | { 178 | return $this->bookings()->create([ 179 | 'bookable_id' => static::getKey(), 180 | 'bookable_type' => static::getMorphClass(), 181 | 'customer_id' => $customer->getKey(), 182 | 'customer_type' => $customer->getMorphClass(), 183 | 'starts_at' => (new Carbon($startsAt))->toDateTimeString(), 184 | 'ends_at' => (new Carbon($endsAt))->toDateTimeString(), 185 | ]); 186 | } 187 | } 188 | -------------------------------------------------------------------------------- /src/Traits/BookingScopes.php: -------------------------------------------------------------------------------- 1 | bookings() 20 | ->whereNull('canceled_at') 21 | ->whereNotNull('ends_at') 22 | ->where('ends_at', '<', now()); 23 | } 24 | 25 | /** 26 | * Get future bookings. 27 | * 28 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 29 | */ 30 | public function futureBookings(): MorphMany 31 | { 32 | return $this->bookings() 33 | ->whereNull('canceled_at') 34 | ->whereNotNull('starts_at') 35 | ->where('starts_at', '>', now()); 36 | } 37 | 38 | /** 39 | * Get current bookings. 40 | * 41 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 42 | */ 43 | public function currentBookings(): MorphMany 44 | { 45 | return $this->bookings() 46 | ->whereNull('canceled_at') 47 | ->whereNotNull('starts_at') 48 | ->whereNotNull('ends_at') 49 | ->where('starts_at', '<', now()) 50 | ->where('ends_at', '>', now()); 51 | } 52 | 53 | /** 54 | * Get cancelled bookings. 55 | * 56 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 57 | */ 58 | public function cancelledBookings(): MorphMany 59 | { 60 | return $this->bookings() 61 | ->whereNotNull('canceled_at'); 62 | } 63 | 64 | /** 65 | * Get bookings starts before the given date. 66 | * 67 | * @param string $date 68 | * 69 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 70 | */ 71 | public function bookingsStartsBefore(string $date): MorphMany 72 | { 73 | return $this->bookings() 74 | ->whereNull('canceled_at') 75 | ->whereNotNull('starts_at') 76 | ->where('starts_at', '<', new Carbon($date)); 77 | } 78 | 79 | /** 80 | * Get bookings starts after the given date. 81 | * 82 | * @param string $date 83 | * 84 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 85 | */ 86 | public function bookingsStartsAfter(string $date): MorphMany 87 | { 88 | return $this->bookings() 89 | ->whereNull('canceled_at') 90 | ->whereNotNull('starts_at') 91 | ->where('starts_at', '>', new Carbon($date)); 92 | } 93 | 94 | /** 95 | * Get bookings starts between the given dates. 96 | * 97 | * @param string $startsAt 98 | * @param string $endsAt 99 | * 100 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 101 | */ 102 | public function bookingsStartsBetween(string $startsAt, string $endsAt): MorphMany 103 | { 104 | return $this->bookings() 105 | ->whereNull('canceled_at') 106 | ->whereNotNull('starts_at') 107 | ->where('starts_at', '>', new Carbon($startsAt)) 108 | ->where('starts_at', '<', new Carbon($endsAt)); 109 | } 110 | 111 | /** 112 | * Get bookings ends before the given date. 113 | * 114 | * @param string $date 115 | * 116 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 117 | */ 118 | public function bookingsEndsBefore(string $date): MorphMany 119 | { 120 | return $this->bookings() 121 | ->whereNull('canceled_at') 122 | ->whereNotNull('ends_at') 123 | ->where('ends_at', '<', new Carbon($date)); 124 | } 125 | 126 | /** 127 | * Get bookings ends after the given date. 128 | * 129 | * @param string $date 130 | * 131 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 132 | */ 133 | public function bookingsEndsAfter(string $date): MorphMany 134 | { 135 | return $this->bookings() 136 | ->whereNull('canceled_at') 137 | ->whereNotNull('ends_at') 138 | ->where('ends_at', '>', new Carbon($date)); 139 | } 140 | 141 | /** 142 | * Get bookings ends between the given dates. 143 | * 144 | * @param string $startsAt 145 | * @param string $endsAt 146 | * 147 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 148 | */ 149 | public function bookingsEndsBetween(string $startsAt, string $endsAt): MorphMany 150 | { 151 | return $this->bookings() 152 | ->whereNull('canceled_at') 153 | ->whereNotNull('ends_at') 154 | ->where('ends_at', '>', new Carbon($startsAt)) 155 | ->where('ends_at', '<', new Carbon($endsAt)); 156 | } 157 | 158 | /** 159 | * Get bookings cancelled before the given date. 160 | * 161 | * @param string $date 162 | * 163 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 164 | */ 165 | public function bookingsCancelledBefore(string $date): MorphMany 166 | { 167 | return $this->bookings() 168 | ->whereNotNull('canceled_at') 169 | ->where('canceled_at', '<', new Carbon($date)); 170 | } 171 | 172 | /** 173 | * Get bookings cancelled after the given date. 174 | * 175 | * @param string $date 176 | * 177 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 178 | */ 179 | public function bookingsCancelledAfter(string $date): MorphMany 180 | { 181 | return $this->bookings() 182 | ->whereNotNull('canceled_at') 183 | ->where('canceled_at', '>', new Carbon($date)); 184 | } 185 | 186 | /** 187 | * Get bookings cancelled between the given dates. 188 | * 189 | * @param string $startsAt 190 | * @param string $endsAt 191 | * 192 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 193 | */ 194 | public function bookingsCancelledBetween(string $startsAt, string $endsAt): MorphMany 195 | { 196 | return $this->bookings() 197 | ->whereNotNull('canceled_at') 198 | ->where('canceled_at', '>', new Carbon($startsAt)) 199 | ->where('canceled_at', '<', new Carbon($endsAt)); 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Rinvex Bookings Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | This project adheres to [Semantic Versioning](CONTRIBUTING.md). 6 | 7 | 8 | ## [v6.1.0] - 2022-02-14 9 | - Update composer dependencies to Laravel v9 10 | - Add support for model HasFactory 11 | - Comment incomplete code (needs refactor #31) 12 | - Update roadmap 13 | 14 | ## [v6.0.0] - 2021-08-22 15 | - Drop PHP v7 support, and upgrade rinvex package dependencies to next major version 16 | - Update composer dependencies 17 | - Upgrade to GitHub-native Dependabot 18 | - Enable StyleCI risky mode 19 | - Merge rules instead of resetting, to allow adequate model override 20 | - Fix constructor initialization order (fill attributes should come next after merging fillables & rules) 21 | - Set validation rules in constructor for consistency & flexibility 22 | - Drop old MySQL versions support that doesn't support json columns 23 | - Define morphMany parameters explicitly 24 | 25 | ## [v5.0.1] - 2020-12-25 26 | - Add support for PHP v8 27 | 28 | ## [v5.0.0] - 2020-12-22 29 | - Upgrade to Laravel v8 30 | - Update validation rules 31 | 32 | ## [v4.1.0] - 2020-06-15 33 | - Update validation rules 34 | - Drop using rinvex/laravel-cacheable from core packages for more flexibility 35 | - Caching should be handled on the application layer, not enforced from the core packages 36 | - Drop PHP 7.2 & 7.3 support from travis 37 | 38 | ## [v4.0.6] - 2020-05-30 39 | - Remove default indent size config 40 | - Add strip_tags validation rule to string fields 41 | - Specify events queue 42 | - Explicitly specify relationship attributes 43 | - Add strip_tags validation rule 44 | - Explicitly define relationship name 45 | 46 | ## [v4.0.5] - 2020-04-12 47 | - Fix ServiceProvider registerCommands method compatibility 48 | 49 | ## [v4.0.4] - 2020-04-09 50 | - Tweak artisan command registration 51 | - Reverse commit "Convert database int fields into bigInteger" 52 | - Refactor publish command and allow multiple resource values 53 | 54 | ## [v4.0.3] - 2020-04-04 55 | - Fix namespace issue 56 | 57 | ## [v4.0.2] - 2020-04-04 58 | - Enforce consistent artisan command tag namespacing 59 | - Enforce consistent package namespace 60 | - Drop laravel/helpers usage as it's no longer used 61 | 62 | ## [v4.0.1] - 2020-03-20 63 | - Convert into bigInteger database fields 64 | - Add shortcut -f (force) for artisan publish commands 65 | - Fix migrations path 66 | 67 | ## [v4.0.0] - 2020-03-15 68 | - Upgrade to Laravel v7.1.x & PHP v7.4.x 69 | 70 | ## [v3.0.3] - 2020-03-13 71 | - Tweak TravisCI config 72 | - Add migrations autoload option to the package 73 | - Tweak service provider `publishesResources` 74 | - Remove indirect composer dependency 75 | - Drop using global helpers 76 | - Update StyleCI config 77 | 78 | ## [v3.0.2] - 2019-12-18 79 | - Fix `migrate:reset` args as it doesn't accept --step 80 | 81 | ## [v3.0.1] - 2019-09-23 82 | - Fix outdated package version 83 | 84 | ## [v3.0.0] - 2019-09-23 85 | - Upgrade to Laravel v6 and update dependencies 86 | 87 | ## [v2.1.1] - 2019-06-03 88 | - Enforce latest composer package versions 89 | 90 | ## [v2.1.0] - 2019-06-02 91 | - Update composer deps 92 | - Drop PHP 7.1 travis test 93 | - Refactor migrations and artisan commands, and tweak service provider publishes functionality 94 | - Fix outdated documentation 95 | 96 | ## [v2.0.0] - 2019-03-03 97 | - Rename environment variable QUEUE_DRIVER to QUEUE_CONNECTION 98 | - Require PHP 7.2 & Laravel 5.8 99 | - Tweak and simplify FormRequest validations 100 | 101 | ## [v1.0.1] - 2018-12-22 102 | - Add missing use statement 103 | - Update composer dependencies 104 | - Add PHP 7.3 support to travis 105 | - Fix MySQL / PostgreSQL json column compatibility 106 | 107 | ## [v1.0.0] - 2018-10-01 108 | - Enforce Consistency 109 | - Support Laravel 5.7+ 110 | - Rename package to rinvex/laravel-bookings 111 | 112 | ## [v0.0.3] - 2018-09-22 113 | - Update travis php versions 114 | - Define polymorphic relationship parameters explicitly 115 | - Rename booking polymorphic relation "user" to "customer" 116 | - Fix fully qualified booking unit methods 117 | - Move Bookable abstract model from the module cortex/bookings 118 | - Fix few readme typos 119 | - Tweak few things 120 | - Change bookable price to base cost and unit cost 121 | - Refactor bookable fields 122 | - Refactor rates, availabilities and add bookable addons 123 | - Enforce naming conventions consistency 124 | - Delete bookings on bookable resource or resource owner deletion 125 | - Refactor bookings! 126 | - Tweak validation rules 127 | - Apply few tweaks 128 | - Drop StyleCI multi-language support (paid feature now!) 129 | - Update composer dependencies 130 | - Prepare and tweak testing configuration 131 | - Drop service addons for now 132 | - Rename cancelled_at to canceled_at 133 | - Add actual_paid field to bookings 134 | - Update bookable rates and availability database structure 135 | - Fix wrong range values 136 | - Refactor and simplify booking price calculation 137 | - Refactor booking attributes 138 | - Update StyleCI options 139 | - Add quantity field, rename actual_paid to total_paid and "use" unit 140 | - Fix wrong db table name 141 | - Update PHPUnit options 142 | - Rename model activation and deactivation methods 143 | 144 | ## [v0.0.2] - 2018-02-18 145 | - Update supplementary files 146 | - Update composer depedencies 147 | - Major refactor for simplicity & flexibility 148 | - Radical refactor for better pricing features & enforced consistency 149 | - Rewrite price calculation, relationships, booking functionality & enforce consistency 150 | - Make "between" scopes inclusive 151 | - Add start/end between scope 152 | - Rename `between` scope to `range` and include full day events in query results 153 | - Require booking start & end dates 154 | - Add Rollback Console Command 155 | - Add PHPUnitPrettyResultPrinter 156 | - Use Carbon global helper 157 | - Typehint method returns 158 | - Drop useless model contracts (models already swappable through IoC) 159 | - Add Laravel v5.6 support 160 | - Simplify IoC binding 161 | - Fix wrong database table names 162 | - Add force option to artisan commands 163 | - Define abstract morphMany method on trait 164 | - Rename BookingsCustomer trait to HasBookings 165 | - Rename polymorphic relation customer to user 166 | - Drop Laravel 5.5 support 167 | - Convert unit column data type into string from character 168 | 169 | ## v0.0.1 - 2017-09-08 170 | - Tag first release 171 | 172 | [v6.1.0]: https://github.com/rinvex/laravel-bookings/compare/v6.0.0...v6.1.0 173 | [v6.0.0]: https://github.com/rinvex/laravel-bookings/compare/v5.0.1...v6.0.0 174 | [v5.0.1]: https://github.com/rinvex/laravel-bookings/compare/v5.0.0...v5.0.1 175 | [v5.0.0]: https://github.com/rinvex/laravel-bookings/compare/v4.1.0...v5.0.0 176 | [v4.1.0]: https://github.com/rinvex/laravel-bookings/compare/v4.0.6...v4.1.0 177 | [v4.0.6]: https://github.com/rinvex/laravel-bookings/compare/v4.0.5...v4.0.6 178 | [v4.0.5]: https://github.com/rinvex/laravel-bookings/compare/v4.0.4...v4.0.5 179 | [v4.0.4]: https://github.com/rinvex/laravel-bookings/compare/v4.0.3...v4.0.4 180 | [v4.0.3]: https://github.com/rinvex/laravel-bookings/compare/v4.0.2...v4.0.3 181 | [v4.0.2]: https://github.com/rinvex/laravel-bookings/compare/v4.0.1...v4.0.2 182 | [v4.0.1]: https://github.com/rinvex/laravel-bookings/compare/v4.0.0...v4.0.1 183 | [v4.0.0]: https://github.com/rinvex/laravel-bookings/compare/v3.0.3...v4.0.0 184 | [v3.0.3]: https://github.com/rinvex/laravel-bookings/compare/v3.0.2...v3.0.3 185 | [v3.0.2]: https://github.com/rinvex/laravel-bookings/compare/v3.0.1...v3.0.2 186 | [v3.0.1]: https://github.com/rinvex/laravel-bookings/compare/v3.0.0...v3.0.1 187 | [v3.0.0]: https://github.com/rinvex/laravel-bookings/compare/v2.1.1...v3.0.0 188 | [v2.1.1]: https://github.com/rinvex/laravel-bookings/compare/v2.1.0...v2.1.1 189 | [v2.1.0]: https://github.com/rinvex/laravel-bookings/compare/v2.0.0...v2.1.0 190 | [v2.0.0]: https://github.com/rinvex/laravel-bookings/compare/v1.0.1...v2.0.0 191 | [v1.0.1]: https://github.com/rinvex/laravel-bookings/compare/v1.0.0...v1.0.1 192 | [v1.0.0]: https://github.com/rinvex/laravel-bookings/compare/v0.0.3...v1.0.0 193 | [v0.0.3]: https://github.com/rinvex/laravel-bookings/compare/v0.0.2...v0.0.3 194 | [v0.0.2]: https://github.com/rinvex/laravel-bookings/compare/v0.0.1...v0.0.2 195 | -------------------------------------------------------------------------------- /src/Models/BookableBooking.php: -------------------------------------------------------------------------------- 1 | 'integer', 45 | 'bookable_type' => 'string', 46 | 'customer_id' => 'integer', 47 | 'customer_type' => 'string', 48 | 'starts_at' => 'datetime', 49 | 'ends_at' => 'datetime', 50 | 'price' => 'float', 51 | 'quantity' => 'integer', 52 | 'total_paid' => 'float', 53 | 'currency' => 'string', 54 | 'formula' => 'json', 55 | 'canceled_at' => 'datetime', 56 | 'options' => 'array', 57 | 'notes' => 'string', 58 | ]; 59 | 60 | /** 61 | * {@inheritdoc} 62 | */ 63 | protected $observables = [ 64 | 'validating', 65 | 'validated', 66 | ]; 67 | 68 | /** 69 | * The default rules that the model will validate against. 70 | * 71 | * @var array 72 | */ 73 | protected $rules = []; 74 | 75 | /** 76 | * Whether the model should throw a 77 | * ValidationException if it fails validation. 78 | * 79 | * @var bool 80 | */ 81 | protected $throwValidationExceptions = true; 82 | 83 | /** 84 | * Create a new Eloquent model instance. 85 | * 86 | * @param array $attributes 87 | */ 88 | public function __construct(array $attributes = []) 89 | { 90 | $this->setTable(config('rinvex.bookings.tables.bookable_bookings')); 91 | $this->mergeRules([ 92 | 'bookable_id' => 'required|integer', 93 | 'bookable_type' => 'required|string|strip_tags|max:150', 94 | 'customer_id' => 'required|integer', 95 | 'customer_type' => 'required|string|strip_tags|max:150', 96 | 'starts_at' => 'required|date', 97 | 'ends_at' => 'required|date', 98 | 'price' => 'required|numeric', 99 | 'quantity' => 'required|integer', 100 | 'total_paid' => 'required|numeric', 101 | 'currency' => 'required|alpha|size:3', 102 | 'formula' => 'nullable|array', 103 | 'canceled_at' => 'nullable|date', 104 | 'options' => 'nullable|array', 105 | 'notes' => 'nullable|string|strip_tags|max:32768', 106 | ]); 107 | 108 | parent::__construct($attributes); 109 | } 110 | 111 | /** 112 | * @TODO: refactor 113 | * 114 | * {@inheritdoc} 115 | */ 116 | protected static function boot() 117 | { 118 | parent::boot(); 119 | 120 | //static::validating(function (self $bookableAvailability) { 121 | // $calculatedPrice = is_null($bookableAvailability->price) 122 | // ? $bookableAvailability->calculatePrice($bookableAvailability->bookable, $bookableAvailability->starts_at, $bookableAvailability->ends_at) : [$bookableAvailability->price, $bookableAvailability->formula, $bookableAvailability->currency]; 123 | // 124 | // $bookableAvailability->currency = $calculatedPrice['currency']; 125 | // $bookableAvailability->formula = $calculatedPrice['formula']; 126 | // $bookableAvailability->price = $calculatedPrice['price']; 127 | //}); 128 | } 129 | 130 | /** 131 | * Get options attributes. 132 | * 133 | * @return \Spatie\SchemalessAttributes\SchemalessAttributes 134 | */ 135 | public function getOptionsAttribute(): SchemalessAttributes 136 | { 137 | return SchemalessAttributes::createForModel($this, 'options'); 138 | } 139 | 140 | /** 141 | * Scope with options attributes. 142 | * 143 | * @return \Illuminate\Database\Eloquent\Builder 144 | */ 145 | public function scopeWithOptions(): Builder 146 | { 147 | return SchemalessAttributes::scopeWithSchemalessAttributes('options'); 148 | } 149 | 150 | /** 151 | * @TODO: implement rates, availabilites, minimum & maximum units 152 | * 153 | * Calculate the booking price. 154 | * 155 | * @param \Illuminate\Database\Eloquent\Model $bookable 156 | * @param \Carbon\Carbon $startsAt 157 | * @param \Carbon\Carbon $endsAt 158 | * @param int $quantity 159 | * 160 | * @return array 161 | */ 162 | public function calculatePrice(Model $bookable, Carbon $startsAt, Carbon $endsAt = null, int $quantity = 1): array 163 | { 164 | $totalUnits = 0; 165 | 166 | switch ($bookable->unit) { 167 | case 'use': 168 | $totalUnits = 1; 169 | $totalPrice = $bookable->base_cost + ($bookable->unit_cost * $totalUnits * $quantity); 170 | break; 171 | default: 172 | $method = 'add'.ucfirst($bookable->unit); 173 | 174 | for ($date = clone $startsAt; $date->lt($endsAt ?? $date->addDay()); $date->{$method}()) { 175 | $totalUnits++; 176 | } 177 | 178 | $totalPrice = $bookable->base_cost + ($bookable->unit_cost * $totalUnits * $quantity); 179 | break; 180 | } 181 | 182 | return [ 183 | 'base_cost' => $bookable->base_cost, 184 | 'unit_cost' => $bookable->unit_cost, 185 | 'unit' => $bookable->unit, 186 | 'currency' => $bookable->currency, 187 | 'total_units' => $totalUnits, 188 | 'total_price' => $totalPrice, 189 | ]; 190 | } 191 | 192 | /** 193 | * Get the owning resource model. 194 | * 195 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 196 | */ 197 | public function bookable(): MorphTo 198 | { 199 | return $this->morphTo('bookable', 'bookable_type', 'bookable_id', 'id'); 200 | } 201 | 202 | /** 203 | * Get the booking customer. 204 | * 205 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 206 | */ 207 | public function customer(): MorphTo 208 | { 209 | return $this->morphTo('customer', 'customer_type', 'customer_id', 'id'); 210 | } 211 | 212 | /** 213 | * Get bookings of the given resource. 214 | * 215 | * @param \Illuminate\Database\Eloquent\Builder $builder 216 | * @param \Illuminate\Database\Eloquent\Model $bookable 217 | * 218 | * @return \Illuminate\Database\Eloquent\Builder 219 | */ 220 | public function scopeOfBookable(Builder $builder, Model $bookable): Builder 221 | { 222 | return $builder->where('bookable_type', $bookable->getMorphClass())->where('bookable_id', $bookable->getKey()); 223 | } 224 | 225 | /** 226 | * Get bookings of the given customer. 227 | * 228 | * @param \Illuminate\Database\Eloquent\Builder $builder 229 | * @param \Illuminate\Database\Eloquent\Model $customer 230 | * 231 | * @return \Illuminate\Database\Eloquent\Builder 232 | */ 233 | public function scopeOfCustomer(Builder $builder, Model $customer): Builder 234 | { 235 | return $builder->where('customer_type', $customer->getMorphClass())->where('customer_id', $customer->getKey()); 236 | } 237 | 238 | /** 239 | * Get the past bookings. 240 | * 241 | * @param \Illuminate\Database\Eloquent\Builder $builder 242 | * 243 | * @return \Illuminate\Database\Eloquent\Builder 244 | */ 245 | public function scopePast(Builder $builder): Builder 246 | { 247 | return $builder->whereNull('canceled_at') 248 | ->whereNotNull('ends_at') 249 | ->where('ends_at', '<', now()); 250 | } 251 | 252 | /** 253 | * Get the future bookings. 254 | * 255 | * @param \Illuminate\Database\Eloquent\Builder $builder 256 | * 257 | * @return \Illuminate\Database\Eloquent\Builder 258 | */ 259 | public function scopeFuture(Builder $builder): Builder 260 | { 261 | return $builder->whereNull('canceled_at') 262 | ->whereNotNull('starts_at') 263 | ->where('starts_at', '>', now()); 264 | } 265 | 266 | /** 267 | * Get the current bookings. 268 | * 269 | * @param \Illuminate\Database\Eloquent\Builder $builder 270 | * 271 | * @return \Illuminate\Database\Eloquent\Builder 272 | */ 273 | public function scopeCurrent(Builder $builder): Builder 274 | { 275 | return $builder->whereNull('canceled_at') 276 | ->whereNotNull('starts_at') 277 | ->whereNotNull('ends_at') 278 | ->where('starts_at', '<', now()) 279 | ->where('ends_at', '>', now()); 280 | } 281 | 282 | /** 283 | * Get the cancelled bookings. 284 | * 285 | * @param \Illuminate\Database\Eloquent\Builder $builder 286 | * 287 | * @return \Illuminate\Database\Eloquent\Builder 288 | */ 289 | public function scopeCancelled(Builder $builder): Builder 290 | { 291 | return $builder->whereNotNull('canceled_at'); 292 | } 293 | 294 | /** 295 | * Get bookings starts before the given date. 296 | * 297 | * @param \Illuminate\Database\Eloquent\Builder $builder 298 | * @param string $date 299 | * 300 | * @return \Illuminate\Database\Eloquent\Builder 301 | */ 302 | public function scopeStartsBefore(Builder $builder, string $date): Builder 303 | { 304 | return $builder->whereNull('canceled_at') 305 | ->whereNotNull('starts_at') 306 | ->where('starts_at', '<', new Carbon($date)); 307 | } 308 | 309 | /** 310 | * Get bookings starts after the given date. 311 | * 312 | * @param \Illuminate\Database\Eloquent\Builder $builder 313 | * @param string $date 314 | * 315 | * @return \Illuminate\Database\Eloquent\Builder 316 | */ 317 | public function scopeStartsAfter(Builder $builder, string $date): Builder 318 | { 319 | return $builder->whereNull('canceled_at') 320 | ->whereNotNull('starts_at') 321 | ->where('starts_at', '>', new Carbon($date)); 322 | } 323 | 324 | /** 325 | * Get bookings starts between the given dates. 326 | * 327 | * @param \Illuminate\Database\Eloquent\Builder $builder 328 | * @param string $startsAt 329 | * @param string $endsAt 330 | * 331 | * @return \Illuminate\Database\Eloquent\Builder 332 | */ 333 | public function scopeStartsBetween(Builder $builder, string $startsAt, string $endsAt): Builder 334 | { 335 | return $builder->whereNull('canceled_at') 336 | ->whereNotNull('starts_at') 337 | ->where('starts_at', '>=', new Carbon($startsAt)) 338 | ->where('starts_at', '<=', new Carbon($endsAt)); 339 | } 340 | 341 | /** 342 | * Get bookings ends before the given date. 343 | * 344 | * @param \Illuminate\Database\Eloquent\Builder $builder 345 | * @param string $date 346 | * 347 | * @return \Illuminate\Database\Eloquent\Builder 348 | */ 349 | public function scopeEndsBefore(Builder $builder, string $date): Builder 350 | { 351 | return $builder->whereNull('canceled_at') 352 | ->whereNotNull('ends_at') 353 | ->where('ends_at', '<', new Carbon($date)); 354 | } 355 | 356 | /** 357 | * Get bookings ends after the given date. 358 | * 359 | * @param \Illuminate\Database\Eloquent\Builder $builder 360 | * @param string $date 361 | * 362 | * @return \Illuminate\Database\Eloquent\Builder 363 | */ 364 | public function scopeEndsAfter(Builder $builder, string $date): Builder 365 | { 366 | return $builder->whereNull('canceled_at') 367 | ->whereNotNull('ends_at') 368 | ->where('ends_at', '>', new Carbon($date)); 369 | } 370 | 371 | /** 372 | * Get bookings ends between the given dates. 373 | * 374 | * @param \Illuminate\Database\Eloquent\Builder $builder 375 | * @param string $startsAt 376 | * @param string $endsAt 377 | * 378 | * @return \Illuminate\Database\Eloquent\Builder 379 | */ 380 | public function scopeEndsBetween(Builder $builder, string $startsAt, string $endsAt): Builder 381 | { 382 | return $builder->whereNull('canceled_at') 383 | ->whereNotNull('ends_at') 384 | ->where('ends_at', '>=', new Carbon($startsAt)) 385 | ->where('ends_at', '<=', new Carbon($endsAt)); 386 | } 387 | 388 | /** 389 | * Get bookings cancelled before the given date. 390 | * 391 | * @param \Illuminate\Database\Eloquent\Builder $builder 392 | * @param string $date 393 | * 394 | * @return \Illuminate\Database\Eloquent\Builder 395 | */ 396 | public function scopeCancelledBefore(Builder $builder, string $date): Builder 397 | { 398 | return $builder->whereNotNull('canceled_at') 399 | ->where('canceled_at', '<', new Carbon($date)); 400 | } 401 | 402 | /** 403 | * Get bookings cancelled after the given date. 404 | * 405 | * @param \Illuminate\Database\Eloquent\Builder $builder 406 | * @param string $date 407 | * 408 | * @return \Illuminate\Database\Eloquent\Builder 409 | */ 410 | public function scopeCancelledAfter(Builder $builder, string $date): Builder 411 | { 412 | return $builder->whereNotNull('canceled_at') 413 | ->where('canceled_at', '>', new Carbon($date)); 414 | } 415 | 416 | /** 417 | * Get bookings cancelled between the given dates. 418 | * 419 | * @param \Illuminate\Database\Eloquent\Builder $builder 420 | * @param string $startsAt 421 | * @param string $endsAt 422 | * 423 | * @return \Illuminate\Database\Eloquent\Builder 424 | */ 425 | public function scopeCancelledBetween(Builder $builder, string $startsAt, string $endsAt): Builder 426 | { 427 | return $builder->whereNotNull('canceled_at') 428 | ->where('canceled_at', '>=', new Carbon($startsAt)) 429 | ->where('canceled_at', '<=', new Carbon($endsAt)); 430 | } 431 | 432 | /** 433 | * Get bookings between the given dates. 434 | * 435 | * @param \Illuminate\Database\Eloquent\Builder $builder 436 | * @param string $startsAt 437 | * @param string $endsAt 438 | * 439 | * @return \Illuminate\Database\Eloquent\Builder 440 | */ 441 | public function scopeRange(Builder $builder, string $startsAt, string $endsAt): Builder 442 | { 443 | return $builder->whereNull('canceled_at') 444 | ->whereNotNull('starts_at') 445 | ->where('starts_at', '>=', new Carbon($startsAt)) 446 | ->where(function (Builder $builder) use ($endsAt) { 447 | $builder->whereNull('ends_at') 448 | ->orWhere(function (Builder $builder) use ($endsAt) { 449 | $builder->whereNotNull('ends_at') 450 | ->where('ends_at', '<=', new Carbon($endsAt)); 451 | }); 452 | }); 453 | } 454 | 455 | /** 456 | * Check if the booking is cancelled. 457 | * 458 | * @return bool 459 | */ 460 | public function isCancelled(): bool 461 | { 462 | return (bool) $this->canceled_at; 463 | } 464 | 465 | /** 466 | * Check if the booking is past. 467 | * 468 | * @return bool 469 | */ 470 | public function isPast(): bool 471 | { 472 | return ! $this->isCancelled() && $this->ends_at->isPast(); 473 | } 474 | 475 | /** 476 | * Check if the booking is future. 477 | * 478 | * @return bool 479 | */ 480 | public function isFuture(): bool 481 | { 482 | return ! $this->isCancelled() && $this->starts_at->isFuture(); 483 | } 484 | 485 | /** 486 | * Check if the booking is current. 487 | * 488 | * @return bool 489 | */ 490 | public function isCurrent(): bool 491 | { 492 | return ! $this->isCancelled() && Carbon::now()->between($this->starts_at, $this->ends_at); 493 | } 494 | } 495 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rinvex Bookings 2 | 3 | ⚠️ This package is abandoned and no longer maintained. No replacement package was suggested. ⚠️ 4 | 5 | 👉 If you are interested to step on as the main maintainer of this package, please [reach out to me](https://twitter.com/omranic)! 6 | 7 | --- 8 | 9 | **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. 10 | 11 | [![Packagist](https://img.shields.io/packagist/v/rinvex/laravel-bookings.svg?label=Packagist&style=flat-square)](https://packagist.org/packages/rinvex/laravel-bookings) 12 | [![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/rinvex/laravel-bookings.svg?label=Scrutinizer&style=flat-square)](https://scrutinizer-ci.com/g/rinvex/laravel-bookings/) 13 | [![Travis](https://img.shields.io/travis/rinvex/laravel-bookings.svg?label=TravisCI&style=flat-square)](https://travis-ci.org/rinvex/laravel-bookings) 14 | [![StyleCI](https://styleci.io/repos/96481479/shield)](https://styleci.io/repos/96481479) 15 | [![License](https://img.shields.io/packagist/l/rinvex/laravel-bookings.svg?label=License&style=flat-square)](https://github.com/rinvex/laravel-bookings/blob/develop/LICENSE) 16 | 17 | 18 | ## Considerations 19 | 20 | - **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](https://github.com/rinvex/laravel-subscriptions).** 21 | - **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. 22 | - 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. 23 | - 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. 24 | 25 | 26 | ## Installation 27 | 28 | 1. Install the package via composer: 29 | ```shell 30 | composer require rinvex/laravel-bookings 31 | ``` 32 | 33 | 2. Publish resources (migrations and config files): 34 | ```shell 35 | php artisan rinvex:publish:bookings 36 | ``` 37 | 38 | 3. Execute migrations via the following command: 39 | ```shell 40 | php artisan rinvex:migrate:bookings 41 | ``` 42 | 43 | 4. Done! 44 | 45 | 46 | ## Usage 47 | 48 | **Rinvex Bookings** has been specially made for Eloquent and simplicity has been taken very serious as in any other Laravel related aspect. 49 | 50 | ### Add bookable functionality to your resource model 51 | 52 | To add bookable functionality to your resource model just use the `\Rinvex\Bookings\Traits\Bookable` trait like this: 53 | 54 | ```php 55 | namespace App\Models; 56 | 57 | use Rinvex\Bookings\Traits\Bookable; 58 | use Illuminate\Database\Eloquent\Model; 59 | 60 | class Room extends Model 61 | { 62 | use Bookable; 63 | } 64 | ``` 65 | 66 | That's it, you only have to use that trait in your Room model! Now your rooms will be bookable. 67 | 68 | ### Add bookable functionality to your customer model 69 | 70 | To add bookable functionality to your customer model just use the `\Rinvex\Bookings\Traits\HasBookings` trait like this: 71 | 72 | ```php 73 | namespace App\Models; 74 | 75 | use Illuminate\Database\Eloquent\Model; 76 | use Rinvex\Bookings\Traits\HasBookings; 77 | 78 | class Customer extends Model 79 | { 80 | use HasBookings; 81 | } 82 | ``` 83 | 84 | Again, that's all you need to do! Now your Customer model can book resources. 85 | 86 | ### Create a new booking 87 | 88 | Creating a new booking is straight forward, and could be done in many ways. Let's see how could we do that: 89 | 90 | ```php 91 | $room = \App\Models\Room::find(1); 92 | $customer = \App\Models\Customer::find(1); 93 | 94 | // Extends \Rinvex\Bookings\Models\BookableBooking 95 | $serviceBooking = new \App\Models\ServiceBooking; 96 | 97 | // Create a new booking via resource model (customer, starts, ends) 98 | $room->newBooking($customer, '2017-07-05 12:44:12', '2017-07-10 18:30:11'); 99 | 100 | // Create a new booking via customer model (resource, starts, ends) 101 | $customer->newBooking($room, '2017-07-05 12:44:12', '2017-07-10 18:30:11'); 102 | 103 | // Create a new booking explicitly 104 | $serviceBooking->make(['starts_at' => \Carbon\Carbon::now(), 'ends_at' => \Carbon\Carbon::tomorrow()]) 105 | ->customer()->associate($customer) 106 | ->bookable()->associate($room) 107 | ->save(); 108 | ``` 109 | 110 | > **Notes:** 111 | > - As you can see, there's many ways to create a new booking, use whatever suits your context. 112 | > - Booking price is calculated automatically on the fly according to the resource price, custom prices, and bookable Rates. 113 | > - **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. 114 | 115 | ### Query booking models 116 | 117 | You can get more details about a specific booking as follows: 118 | 119 | ```php 120 | // Extends \Rinvex\Bookings\Models\BookableBooking 121 | $serviceBooking = \App\Models\ServiceBooking::find(1); 122 | 123 | $bookable = $serviceBooking->bookable; // Get the owning resource model 124 | $customer = $serviceBooking->customer; // Get the owning customer model 125 | 126 | $serviceBooking->isPast(); // Check if the booking is past 127 | $serviceBooking->isFuture(); // Check if the booking is future 128 | $serviceBooking->isCurrent(); // Check if the booking is current 129 | $serviceBooking->isCancelled(); // Check if the booking is cancelled 130 | ``` 131 | 132 | And as expected, you can query bookings by date as well: 133 | 134 | ```php 135 | // Extends \Rinvex\Bookings\Models\BookableBooking 136 | $serviceBooking = new \App\Models\ServiceBooking; 137 | 138 | $pastBookings = $serviceBooking->past(); // Get the past bookings 139 | $futureBookings = $serviceBooking->future(); // Get the future bookings 140 | $currentBookings = $serviceBooking->current(); // Get the current bookings 141 | $cancelledBookings = $serviceBooking->cancelled(); // Get the cancelled bookings 142 | 143 | $serviceBookingsAfter = $serviceBooking->startsAfter('2017-06-21 19:28:51')->get(); // Get bookings starts after the given date 144 | $serviceBookingsStartsBefore = $serviceBooking->startsBefore('2017-06-21 19:28:51')->get(); // Get bookings starts before the given date 145 | $serviceBookingsBetween = $serviceBooking->startsBetween('2017-06-21 19:28:51', '2017-07-01 12:00:00')->get(); // Get bookings starts between the given dates 146 | 147 | $serviceBookingsEndsAfter = $serviceBooking->endsAfter('2017-06-21 19:28:51')->get(); // Get bookings starts after the given date 148 | $serviceBookingsEndsBefore = $serviceBooking->endsBefore('2017-06-21 19:28:51')->get(); // Get bookings starts before the given date 149 | $serviceBookingsEndsBetween = $serviceBooking->endsBetween('2017-06-21 19:28:51', '2017-07-01 12:00:00')->get(); // Get bookings starts between the given dates 150 | 151 | $serviceBookingsCancelledAfter = $serviceBooking->cancelledAfter('2017-06-21 19:28:51')->get(); // Get bookings starts after the given date 152 | $serviceBookingsCancelledBefore = $serviceBooking->cancelledBefore('2017-06-21 19:28:51')->get(); // Get bookings starts before the given date 153 | $serviceBookingsCancelledBetween = $serviceBooking->cancelledBetween('2017-06-21 19:28:51', '2017-07-01 12:00:00')->get(); // Get bookings starts between the given dates 154 | 155 | $room = \App\Models\Room::find(1); 156 | $serviceBookingsOfBookable = $serviceBooking->ofBookable($room)->get(); // Get bookings of the given resource 157 | 158 | $customer = \App\Models\Customer::find(1); 159 | $serviceBookingsOfCustomer = $serviceBooking->ofCustomer($customer)->get(); // Get bookings of the given customer 160 | ``` 161 | 162 | ### Create a new booking rate 163 | 164 | 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%. 165 | 166 | To create a new booking rate, follow these steps: 167 | 168 | ```php 169 | $room = \App\Models\Room::find(1); 170 | $room->newRate('15', '^', 2); // Increase unit price by 15% for the first 2 units 171 | $room->newRate('-10', '>', 5); // Decrease unit price by 10% if booking is greater than 5 units 172 | ``` 173 | 174 | Alternatively you can create a new booking rate explicitly as follows: 175 | 176 | ```php 177 | $room = \App\Models\Room::find(1); 178 | 179 | // Extends \Rinvex\Bookings\Models\BookableRate 180 | $serviceRate = new \App\Models\ServiceRate; 181 | 182 | $serviceRate->make(['percentage' => '15', 'operator' => '^', 'amount' => 2]) 183 | ->bookable()->associate($room) 184 | ->save(); 185 | ``` 186 | 187 | And here's the booking rate relations: 188 | 189 | ```php 190 | $bookable = $serviceRate->bookable; // Get the owning resource model 191 | ``` 192 | 193 | > **Notes:** 194 | > - All booking rate percentages should NEVER contain the `%` sign, it's known that this field is for percentage already. 195 | > - When adding new booking rate with positive percentage, the `+` sign is NOT required, and will be omitted anyway if entered. 196 | 197 | ### Create a new custom price 198 | 199 | Custom prices are set according to specific time based criteria. For example, let’s say you've a Coworking Space business, and one of your rooms is a Conference Room, and you would like to charge differently for both Monday and Wednesday. Will assume that Monday from 09:00 am till 05:00 pm is a peak hours, so you need to charge more, and Wednesday from 11:30 am to 03:45 pm is dead hours so you'd like to charge less! That's totally achievable through custom prices, where you can set both time frames and their prices too using +/- percentage. It works the same way as [Bookable Rates](#create-a-new-booking-rate) but on a time based criteria. Awesome, huh? 200 | 201 | To create a custom price, follow these steps: 202 | 203 | ```php 204 | $room = \App\Models\Room::find(1); 205 | $room->newPrice('mon', '09:00:00', '17:00:00', '26'); // Increase pricing on Monday from 09:00 am to 05:00 pm by 26% 206 | $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% 207 | ``` 208 | 209 | Piece of cake, right? You just set the day, from-to times, and the +/- percentage to increase/decrease your unit price. 210 | 211 | And here's the custom price relations: 212 | 213 | ```php 214 | $bookable = $room->bookable; // Get the owning resource model 215 | ``` 216 | 217 | > **Notes:** 218 | > - If you don't create any custom prices, then the resource will be booked at the default resource price. 219 | > - **Rinvex Bookings** is intelegent enough to detect time format and convert if required, the above example show the explicitly correct format, but you still can write something like: '09:00 am' and it will be converted automatically for you. 220 | 221 | ### Query resource models 222 | 223 | You can query your resource models for further details, using the intuitive API as follows: 224 | 225 | ```php 226 | $room = \App\Models\Room::find(1); 227 | 228 | $room->bookings; // Get all bookings 229 | $room->pastBookings; // Get past bookings 230 | $room->futureBookings; // Get future bookings 231 | $room->currentBookings; // Get current bookings 232 | $room->cancelledBookings; // Get cancelled bookings 233 | 234 | $room->bookingsStartsBefore('2017-06-21 19:28:51')->get(); // Get bookings starts before the given date 235 | $room->bookingsStartsAfter('2017-06-21 19:28:51')->get(); // Get bookings starts after the given date 236 | $room->bookingsStartsBetween('2017-06-21 19:28:51', '2017-07-01 12:00:00')->get(); // Get bookings starts between the given dates 237 | 238 | $room->bookingsEndsBefore('2017-06-21 19:28:51')->get(); // Get bookings starts before the given date 239 | $room->bookingsEndsAfter('2017-06-21 19:28:51')->get(); // Get bookings starts after the given date 240 | $room->bookingsEndsBetween('2017-06-21 19:28:51', '2017-07-01 12:00:00')->get(); // Get bookings starts between the given dates 241 | 242 | $room->bookingsCancelledBefore('2017-06-21 19:28:51')->get(); // Get bookings starts before the given date 243 | $room->bookingsCancelledAfter('2017-06-21 19:28:51')->get(); // Get bookings starts after the given date 244 | $room->bookingsCancelledBetween('2017-06-21 19:28:51', '2017-07-01 12:00:00')->get(); // Get bookings starts between the given dates 245 | 246 | $customer = \App\Models\Customer::find(1); 247 | $room->bookingsOf($customer)->get(); // Get bookings of the given customer 248 | 249 | $room->rates; // Get all bookable Rates 250 | $room->prices; // Get all custom prices 251 | ``` 252 | 253 | All the above properties and methods are actually relationships, so you can call the raw relation methods and chain like any normal Eloquent relationship. E.g. `$room->bookings()->where('starts_at', '>', new \Carbon\Carbon())->first()`. 254 | 255 | ### Query customer models 256 | 257 | Just like how you query your resources, you can query customers to retrieve related booking info easily. Look at these examples: 258 | 259 | ```php 260 | $customer = \App\Models\Customer::find(1); 261 | 262 | $customer->bookings; // Get all bookings 263 | $customer->pastBookings; // Get past bookings 264 | $customer->futureBookings; // Get future bookings 265 | $customer->currentBookings; // Get current bookings 266 | $customer->cancelledBookings; // Get cancelled bookings 267 | 268 | $customer->bookingsStartsBefore('2017-06-21 19:28:51')->get(); // Get bookings starts before the given date 269 | $customer->bookingsStartsAfter('2017-06-21 19:28:51')->get(); // Get bookings starts after the given date 270 | $customer->bookingsStartsBetween('2017-06-21 19:28:51', '2017-07-01 12:00:00')->get(); // Get bookings starts between the given dates 271 | 272 | $customer->bookingsEndsBefore('2017-06-21 19:28:51')->get(); // Get bookings starts before the given date 273 | $customer->bookingsEndsAfter('2017-06-21 19:28:51')->get(); // Get bookings starts after the given date 274 | $customer->bookingsEndsBetween('2017-06-21 19:28:51', '2017-07-01 12:00:00')->get(); // Get bookings starts between the given dates 275 | 276 | $customer->bookingsCancelledBefore('2017-06-21 19:28:51')->get(); // Get bookings starts before the given date 277 | $customer->bookingsCancelledAfter('2017-06-21 19:28:51')->get(); // Get bookings starts after the given date 278 | $customer->bookingsCancelledBetween('2017-06-21 19:28:51', '2017-07-01 12:00:00')->get(); // Get bookings starts between the given dates 279 | 280 | $room = \App\Models\Room::find(1); 281 | $customer->isBooked($room); // Check if the customer booked the given room 282 | $customer->bookingsOf($room)->get(); // Get bookings by the customer for the given room 283 | ``` 284 | 285 | Just like resource models, all the above properties and methods are actually relationships, so you can call the raw relation methods and chain like any normal Eloquent relationship. E.g. `$customer->bookings()->where('starts_at', '>', new \Carbon\Carbon())->first()`. 286 | 287 | 288 | **⚠️ Documentation not complete, the package is under developement, and some part may encounter refactoring! ⚠️** 289 | 290 | 291 | ## Roadmap 292 | 293 | **Looking for contributors!** 294 | 295 | The following are a set of limitations to be improved, or feature requests that's looking for contributors to implement, all PRs are welcome 🙂 296 | 297 | - [ ] Add the ability to cancel bookings (#43) 298 | - [ ] Complete the bookable availability implementation, and document it (#32, #4) 299 | - [ ] Improve the documentation, and complete missing features, and add a workable example for each. 300 | 301 | 302 | ## Changelog 303 | 304 | Refer to the [Changelog](CHANGELOG.md) for a full history of the project. 305 | 306 | 307 | ## Support 308 | 309 | The following support channels are available at your fingertips: 310 | 311 | - [Chat on Slack](https://bit.ly/rinvex-slack) 312 | - [Help on Email](mailto:help@rinvex.com) 313 | - [Follow on Twitter](https://twitter.com/rinvex) 314 | 315 | 316 | ## Contributing & Protocols 317 | 318 | Thank you for considering contributing to this project! The contribution guide can be found in [CONTRIBUTING.md](CONTRIBUTING.md). 319 | 320 | Bug reports, feature requests, and pull requests are very welcome. 321 | 322 | - [Versioning](CONTRIBUTING.md#versioning) 323 | - [Pull Requests](CONTRIBUTING.md#pull-requests) 324 | - [Coding Standards](CONTRIBUTING.md#coding-standards) 325 | - [Feature Requests](CONTRIBUTING.md#feature-requests) 326 | - [Git Flow](CONTRIBUTING.md#git-flow) 327 | 328 | 329 | ## Security Vulnerabilities 330 | 331 | If you discover a security vulnerability within this project, please send an e-mail to [help@rinvex.com](help@rinvex.com). All security vulnerabilities will be promptly addressed. 332 | 333 | 334 | ## About Rinvex 335 | 336 | Rinvex is a software solutions startup, specialized in integrated enterprise solutions for SMEs established in Alexandria, Egypt since June 2016. We believe that our drive The Value, The Reach, and The Impact is what differentiates us and unleash the endless possibilities of our philosophy through the power of software. We like to call it Innovation At The Speed Of Life. That’s how we do our share of advancing humanity. 337 | 338 | 339 | ## License 340 | 341 | This software is released under [The MIT License (MIT)](LICENSE). 342 | 343 | (c) 2016-2022 Rinvex LLC, Some rights reserved. 344 | --------------------------------------------------------------------------------