diff --git a/CHANGELOG.md b/CHANGELOG.md index bd454f5..ef8982d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +## [1.0.1] - 2017-08-29 +### Fixed +- Migration file, that was not updated. + +### Added +- Ability to create multi-use or one-time promotion codes. + ## [1.0.0] - 2017-08-29 ### Added - Ability to set expiration date of promocode while creating. @@ -24,6 +31,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Ability of user, that they could create promocodes assigned to them. [Unreleased]: https://github.com/olivierlacan/keep-a-changelog/compare/v1.0.0...HEAD +[1.0.1]: https://github.com/zgabievi/laravel-promocodes/compare/v1.0.0...v1.0.1 [1.0.0]: https://github.com/zgabievi/laravel-promocodes/compare/v0.5.4...v1.0.0 [#12]: https://github.com/zgabievi/laravel-promocodes/issues/12 diff --git a/README.md b/README.md index fe534cd..fe90139 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ Generate as many codes as you wish and output them without saving to database. You will get array of codes in return: ```php -Promocodes::output($amount = 1) +Promocodes::output($amount = 1); ``` --- @@ -84,10 +84,18 @@ Create as many codes as you wish. Set reward (amount). Attach additional data as array. Specify for how many days should this codes stay alive. +By default generated code will be multipass (several users will be able to use this code once). + They will be saved in database and you will get collection of them in return: ```php -Promocodes::create($amount = 1, $reward = null, array $data = [], $expires_in = null) +Promocodes::create($amount = 1, $reward = null, array $data = [], $expires_in = null); +``` + +If you want to create code that will be used only once, here is method for you. + +```php +Promocodes::createDisposable($amount = 1, $reward = null, array $data = [], $expires_in = null); ``` --- @@ -99,7 +107,7 @@ This code may throw `\Gabievi\Promocodes\Exceptions\InvalidPromocodeExceprion` i Returns `Promocode` object if valid, or `false` if not. ```php -Promocodes::check($code) +Promocodes::check($code); ``` --- @@ -113,8 +121,8 @@ Also if authenticated user will try to apply code twice, it will throw an except Returns `Promocode` object if applied, or `false` if not. ```php -Promocodes::redeem($code) -Promocodes::apply($code) +Promocodes::redeem($code); +Promocodes::apply($code); ``` --- @@ -122,7 +130,7 @@ Promocodes::apply($code) You can imediately expire code by calling *disable* function. Returning boolean status of update. ```php -Promocodes::disable($code) +Promocodes::disable($code); ``` --- @@ -132,7 +140,7 @@ And if you want to delete expired, or non-usable codes you can erase them. This method will remove redundant codes from database and their relations to users. ```php -Promocodes::clearRedundant() +Promocodes::clearRedundant(); ``` --- @@ -162,8 +170,8 @@ Redeem or apply code are same. *redeemCode* is alias of *applyCode* Pass promotion code you want to be applied by current user. ```php -User::redeemCode($code, $callback = null) -User::applyCode($code, $callback = null) +User::redeemCode($code, $callback = null); +User::applyCode($code, $callback = null); ``` Example (usage of callback): diff --git a/src/Promocodes.php b/src/Promocodes.php index 564295a..5ed0dfa 100644 --- a/src/Promocodes.php +++ b/src/Promocodes.php @@ -63,16 +63,17 @@ public function output($amount = 1) /** * Save promocodes into database * Successful insert returns generated promocodes - * Fail will return NULL. + * Fail will return empty collection. * * @param int $amount * @param null $reward * @param array $data * @param int|null $expires_in + * @param bool $is_disposable * * @return \Illuminate\Support\Collection */ - public function create($amount = 1, $reward = null, array $data = [], $expires_in = null) + public function create($amount = 1, $reward = null, array $data = [], $expires_in = null, $is_disposable = false) { $records = []; @@ -82,6 +83,7 @@ public function create($amount = 1, $reward = null, array $data = [], $expires_i 'reward' => $reward, 'data' => json_encode($data), 'expires_at' => $expires_in ? Carbon::now()->addDays($expires_in) : null, + 'is_disposable' => $is_disposable, ]; } @@ -92,6 +94,23 @@ public function create($amount = 1, $reward = null, array $data = [], $expires_i return collect([]); } + /** + * Save one-time use promocodes into database + * Successful insert returns generated promocodes + * Fail will return empty collection. + * + * @param int $amount + * @param null $reward + * @param array $data + * @param int|null $expires_in + * + * @return \Illuminate\Support\Collection + */ + public function createDisposable($amount = 1, $reward = null, array $data = [], $expires_in = null) + { + return $this->create($amount, $reward, $data, $expires_in, true); + } + /** * Check promocode in database if it is valid. * diff --git a/src/migrations/2016_05_17_221000_create_promocodes_table.php b/src/migrations/2016_05_17_221000_create_promocodes_table.php index 52914cc..c8e7cfc 100644 --- a/src/migrations/2016_05_17_221000_create_promocodes_table.php +++ b/src/migrations/2016_05_17_221000_create_promocodes_table.php @@ -15,16 +15,25 @@ public function up() Schema::create(config('promocodes.table', 'promocodes'), function (Blueprint $table) { $table->increments('id'); - $table->integer('user_id')->unsigned()->nullable(); - $table->string('code', 32)->unique(); $table->double('reward', 10, 2)->nullable(); - $table->json('data')->nullable(); + $table->text('data')->nullable(); + + $table->boolean('is_disposable')->default(false); + $table->timestamp('expires_at')->nullable(); + }); + + Schema::create(config('promocodes.relation_table', 'promocode_user'), function (Blueprint $table) { + $table->unsignedInteger('user_id'); + $table->unsignedInteger('promocode_id'); + + $table->timestamp('used_at'); - $table->boolean('is_used')->default(false); + $table->primary(['user_id', 'promocode_id']); $table->foreign('user_id')->references('id')->on('users'); + $table->foreign('promocode_id')->references('id')->on(config('promocodes.table', 'promocodes')); }); } @@ -36,5 +45,6 @@ public function up() public function down() { Schema::drop(config('promocodes.table', 'promocodes')); + Schema::drop(config('promocodes.relation_table', 'promocode_user')); } }