diff --git a/README.md b/README.md index b73f530..cb735c5 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Open `config/app.php` and find the `providers` key. Add `PromocodesServiceProvid Trexology\Promocodes\PromocodesServiceProvider::class ``` -Find the `aliases` key and add `Facade` to the array. +Find the `aliases` key and add `Facade` to the array. ```php 'Promocodes' => Trexology\Promocodes\Facades\Promocodes::class @@ -60,15 +60,10 @@ public $timestamps = false; protected $fillable = [ 'code', 'reward', - 'is_used', + 'quantity', + 'is_used', ]; -/** - * @var array - */ -protected $casts = [ - 'is_used' => 'boolean', -]; ``` ### Methods @@ -101,6 +96,19 @@ This will generate 5 codes and insert in your DB. --- +You can generate and save codes with the exact name in your database using: + +```php +Promocodes::saveCodeName("OFF1050", 10.50); // $reward = null +``` + +- **$code** string - exact promotional code name to be generated +- **$reward** double - amount of reward of each promocodes + +This will return false if code name already existed + +--- + Check code using method `check`. Method returns boolean. @@ -143,4 +151,3 @@ Promocodes is an open-sourced laravel package licensed under the MIT license ## TODO - [x] Create migration for vendor publish -- [ ] Create Promocode Model trait diff --git a/src/Promocodes.php b/src/Promocodes.php index e71a08b..dddf8d9 100644 --- a/src/Promocodes.php +++ b/src/Promocodes.php @@ -2,9 +2,11 @@ namespace Trexology\Promocodes; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; +use Carbon\Carbon; -class Promocodes +class Promocodes extends Model { /** @@ -13,7 +15,7 @@ class Promocodes * * @var array */ - protected $codes = []; + // protected $codes = []; /** * Length of code will be calculated @@ -24,22 +26,11 @@ class Promocodes */ protected $length; - /** - * Model of promocodes from your config - * will be saved in this variable - * - * @var Model - */ - protected $model; - /** * Promocodes constructor. */ public function __construct() { - $this->model = app()->make(config('promocodes.model')); - - $this->codes = $this->model->lists('code')->toArray(); $this->length = substr_count(config('promocodes.mask'), '*'); } @@ -84,22 +75,30 @@ public function randomize() return $code; } - /** - * Your code will be validted to - * be unique for one request - * - * @param $collection - * @param $new - * - * @return bool - */ - public function validate($collection, $new) + // /** + // * Your code will be validated to + // * be unique for one request + // * + // * @param $collection + // * @param $new + // * + // * @return bool + // */ + public function validate($new) { - if (count($collection) == 0 && count($this->codes) == 0) return true; + // if (count($collection) == 0 && count($this->codes) == 0) return true; - $combined = array_merge($collection, $this->codes); + $count = Promocodes::where('code', $new)->count(); + if ($count == 0) { + return true; + } + else{ + return false; + } - return !in_array($new, $combined); + // $combined = array_merge($collection, $this->codes); + // + // return !in_array($new, $combined); } /** @@ -116,7 +115,7 @@ public function generate($amount = 1) for ($i = 1; $i <= $amount; $i++) { $random = $this->randomize(); - while (!$this->validate($collection, $random)) { + while (!$this->validate($random)) { $random = $this->randomize(); } @@ -137,19 +136,38 @@ public function generate($amount = 1) */ public function save($amount = 1, $reward = null) { - $data = []; + $data = collect([]); foreach ($this->generate($amount) as $key => $code) { - $data[$key]['code'] = $code; - $data[$key]['reward'] = $reward; + $promo = new Promocodes(); + $promo->code = $code; + $promo->reward = $reward; + $promo->save(); + $data->push($promo); } - // if insertion goes well - if ($this->model->insert($data)) { - return collect($data); - } else { - return null; - } + return $data; + } + + /** + * Generates promocodes with specific code + * + * @param string $code + * @param double $reward + * + * @return Promocodes / false + */ + public function saveCodeName($code, $reward = null) + { + if ($this->validate($code)) { + $promo = new Promocodes(); + $promo->code = $code; + $promo->reward = $reward; + $promo->save(); + } + else{ + return false; + } } /** @@ -161,11 +179,16 @@ public function save($amount = 1, $reward = null) */ public function check($code) { - return $this->model->where('code', $code)->where('is_used', false)->count() > 0; + return Promocodes::where('code', $code)->whereNull('is_used')->where('quantity', '!=' , 0) + ->where(function($q) { + $q->whereDate('expiry_date', '<' , Carbon::today()) + ->orWhereNull('expiry_date'); + }) + ->count() > 0; } /** - * Apply pomocode to user that it's used from now + * Apply promocode to user that it's used from now * * @param $code * @@ -173,17 +196,22 @@ public function check($code) */ public function apply($code, $hard_check = false) { - $row = $this->model->where('code', $code)->where('is_used', false); - + $row = Promocodes::where('code', $code) + ->whereNull('is_used') + ->where('quantity', '!=' , 0) + ->where(function($q) { + $q->whereDate('expiry_date', '<' , Carbon::today()) + ->orWhereNull('expiry_date'); + }); // if ($row->count() > 0) { $record = $row->first(); - $record->is_used = true; + if ($record->quantity > 0) { + $record->quantity--; + } + $record->is_used = date('Y-m-d H:i:s'); - // if ($record->save()) { - - // if ($hard_check) { return $record->reward; } else { diff --git a/src/config/promocodes.php b/src/config/promocodes.php index 5d9c65b..38f9d52 100644 --- a/src/config/promocodes.php +++ b/src/config/promocodes.php @@ -2,17 +2,11 @@ return [ - /** - * Promo codes Eloquent model. - * - */ - 'model' => \App\Promocode::class, - /** * List of characters, promo code generated from. * */ - 'characters' => '23456789ABCDEFGHJKLMNPQRSTUVWXYZ', + 'characters' => '1234567890ABCDEFGHJKLMNPQRSTUVWXYZ', /** * Promo code prefix. @@ -40,4 +34,4 @@ * */ 'separator' => '-', -]; \ No newline at end of file +]; 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 4ed99f7..7649c7e 100644 --- a/src/migrations/2016_05_17_221000_create_promocodes_table.php +++ b/src/migrations/2016_05_17_221000_create_promocodes_table.php @@ -17,7 +17,10 @@ public function up() $table->string('code', 32)->unique(); $table->double('reward', 10, 2)->nullable(); - $table->boolean('is_used')->default(false); + $table->integer('quantity')->default(-1); + $table->dateTime('expiry_date')->nullable(); + $table->dateTime('is_used')->nullable(); + $table->timestamps(); }); } @@ -30,4 +33,4 @@ public function down() { Schema::drop('promocodes'); } -} \ No newline at end of file +}