Skip to content

Commit

Permalink
convert Promocodes to be eloquent model
Browse files Browse the repository at this point in the history
added function to generate exact code name
added quanity for code
added expire_date check for each code
  • Loading branch information
Trexology committed Aug 10, 2016
1 parent 60b38b1 commit 6961dc5
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 63 deletions.
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -60,15 +60,10 @@ public $timestamps = false;
protected $fillable = [
'code',
'reward',
'is_used',
'quantity',
'is_used',
];

/**
* @var array
*/
protected $casts = [
'is_used' => 'boolean',
];
```

### Methods
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
116 changes: 72 additions & 44 deletions src/Promocodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

/**
Expand All @@ -13,7 +15,7 @@ class Promocodes
*
* @var array
*/
protected $codes = [];
// protected $codes = [];

/**
* Length of code will be calculated
Expand All @@ -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'), '*');
}

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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();
}

Expand All @@ -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;
}
}

/**
Expand All @@ -161,29 +179,39 @@ 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
*
* @return bool
*/
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 {
Expand Down
10 changes: 2 additions & 8 deletions src/config/promocodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -40,4 +34,4 @@
*
*/
'separator' => '-',
];
];
7 changes: 5 additions & 2 deletions src/migrations/2016_05_17_221000_create_promocodes_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}

Expand All @@ -30,4 +33,4 @@ public function down()
{
Schema::drop('promocodes');
}
}
}

0 comments on commit 6961dc5

Please sign in to comment.