Skip to content

Commit

Permalink
Make coupon locationable
Browse files Browse the repository at this point in the history
  • Loading branch information
sampoyigi committed Jan 18, 2019
1 parent 5c4f192 commit 531f591
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/admin/language/en/lang.php
Expand Up @@ -264,6 +264,7 @@
'help_redemption' => 'The total number of times this coupon can be redeemed. Leave blank to allow any number of redemptions.',
'help_customer_redemption' => 'The number of times a specific customer can redeem this coupon. Leave blank to allow any number of redemptions.',
'help_order_restriction' => 'Whether to restrict the coupon to a specific order type.',
'help_locations' => 'Apply this coupon ONLY on orders made at the selected location(s). Leave blank to make coupon available across all locations',

'activity_event_log' => '{event} coupon <b>:subject.name</b>',
],
Expand Down
16 changes: 16 additions & 0 deletions app/admin/models/Coupons_model.php
Expand Up @@ -14,11 +14,14 @@
class Coupons_model extends Model
{
use LogsActivity;
use \Admin\Traits\Locationable;

const UPDATED_AT = null;

const CREATED_AT = 'date_added';

const LOCATIONABLE_RELATION = 'locations';

/**
* @var string The database table name
*/
Expand Down Expand Up @@ -47,6 +50,9 @@ class Coupons_model extends Model
'hasMany' => [
'history' => 'Admin\Models\Coupons_history_model',
],
'morphToMany' => [
'locations' => ['Admin\Models\Locations_model', 'name' => 'locationable'],
],
];

public function getRecurringEveryOptions()
Expand Down Expand Up @@ -149,6 +155,16 @@ public function hasRestriction($orderType)
return array_get($orderTypes, $orderType) != $this->order_restriction;
}

public function hasLocationRestriction($locationId)
{
if (!$this->locations OR $this->locations->isEmpty())
return FALSE;

$locationKeyColumn = $this->locations()->getModel()->qualifyColumn('location_id');

return !$this->locations()->where($locationKeyColumn, $locationId)->exists();
}

public function hasReachedMaxRedemption()
{
return !$this->redemptions OR $this->redemptions <= $this->countRedemptions();
Expand Down
15 changes: 12 additions & 3 deletions app/admin/models/config/coupons_model.php
Expand Up @@ -58,10 +58,12 @@
'type' => 'text',
'searchable' => TRUE,
],
'type_name' => [
'label' => 'lang:admin::lang.coupons.column_type',
'locations' => [
'label' => 'lang:admin::lang.column_location',
'type' => 'text',
'sortable' => FALSE,
'relation' => 'locations',
'select' => 'location_name',
'invisible' => TRUE,
],
'formatted_discount' => [
'label' => 'lang:admin::lang.coupons.column_discount',
Expand Down Expand Up @@ -268,6 +270,13 @@
'condition' => 'value[recurring]',
],
],
'locations' => [
'label' => 'lang:admin::lang.label_location',
'type' => 'relation',
'valueFrom' => 'locations',
'nameFrom' => 'location_name',
'comment' => 'lang:admin::lang.coupons.help_locations',
],
'description' => [
'label' => 'lang:admin::lang.coupons.label_description',
'type' => 'textarea',
Expand Down

0 comments on commit 531f591

Please sign in to comment.