From 531f591c4445144080aafca12d6a014638ad9cce Mon Sep 17 00:00:00 2001 From: SamPoyigi Date: Fri, 18 Jan 2019 02:43:31 +0000 Subject: [PATCH] Make coupon locationable --- app/admin/language/en/lang.php | 1 + app/admin/models/Coupons_model.php | 16 ++++++++++++++++ app/admin/models/config/coupons_model.php | 15 ++++++++++++--- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/app/admin/language/en/lang.php b/app/admin/language/en/lang.php index 73edb0340e..942acd61f4 100644 --- a/app/admin/language/en/lang.php +++ b/app/admin/language/en/lang.php @@ -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 :subject.name', ], diff --git a/app/admin/models/Coupons_model.php b/app/admin/models/Coupons_model.php index ace56ea127..f980d4f4cf 100644 --- a/app/admin/models/Coupons_model.php +++ b/app/admin/models/Coupons_model.php @@ -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 */ @@ -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() @@ -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(); diff --git a/app/admin/models/config/coupons_model.php b/app/admin/models/config/coupons_model.php index e5a1c7362e..7a08bf2f83 100644 --- a/app/admin/models/config/coupons_model.php +++ b/app/admin/models/config/coupons_model.php @@ -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', @@ -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',