CouponApplication.php
3 years ago
CouponEntity.php
1 year ago
CouponFactory.php
3 years ago
CouponType.php
2 years ago
CouponUnit.php
3 years ago
index.php
3 years ago
CouponEntity.php
311 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Membership\Models\Coupon; |
| 4 | |
| 5 | use ProfilePress\Core\Membership\Models\AbstractModel; |
| 6 | use ProfilePress\Core\Membership\Models\Customer\CustomerFactory; |
| 7 | use ProfilePress\Core\Membership\Models\ModelInterface; |
| 8 | use ProfilePress\Core\Membership\Models\Order\OrderStatus; |
| 9 | use ProfilePress\Core\Membership\Models\Order\OrderType; |
| 10 | use ProfilePress\Core\Membership\Repositories\CouponRepository; |
| 11 | use ProfilePress\Core\Membership\Repositories\OrderRepository; |
| 12 | use ProfilePressVendor\Carbon\Carbon; |
| 13 | use ProfilePressVendor\Carbon\CarbonImmutable; |
| 14 | |
| 15 | /** |
| 16 | * @property int $id |
| 17 | * @property string $code |
| 18 | * @property string $description |
| 19 | * @property string $coupon_type |
| 20 | * @property string $coupon_application |
| 21 | * @property string $is_onetime_use |
| 22 | * @property string $amount |
| 23 | * @property string $unit |
| 24 | * @property array $plan_ids |
| 25 | * @property int $usage_limit |
| 26 | * @property string $status |
| 27 | * @property string $start_date |
| 28 | * @property string $end_date |
| 29 | */ |
| 30 | class CouponEntity extends AbstractModel implements ModelInterface |
| 31 | { |
| 32 | protected $id = 0; |
| 33 | |
| 34 | protected $code = ''; |
| 35 | |
| 36 | protected $description = ''; |
| 37 | |
| 38 | protected $coupon_type = CouponType::RECURRING; |
| 39 | |
| 40 | protected $coupon_application = CouponApplication::NEW_PURCHASE; |
| 41 | |
| 42 | protected $amount = 0; |
| 43 | |
| 44 | protected $unit = CouponUnit::PERCENTAGE; |
| 45 | |
| 46 | protected $plan_ids = []; |
| 47 | |
| 48 | protected $usage_limit = ''; |
| 49 | |
| 50 | protected $status = 'true'; |
| 51 | |
| 52 | protected $is_onetime_use = 'false'; |
| 53 | |
| 54 | protected $start_date = ''; |
| 55 | |
| 56 | protected $end_date = ''; |
| 57 | |
| 58 | public function __construct($data = []) |
| 59 | { |
| 60 | if (is_array($data) && ! empty($data)) { |
| 61 | |
| 62 | foreach ($data as $key => $value) { |
| 63 | $this->$key = $value; |
| 64 | |
| 65 | if ($key == 'plan_ids') { |
| 66 | $this->plan_ids = ! empty($value) && ppress_is_json($value) ? \json_decode($value, true) : []; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @return bool |
| 74 | */ |
| 75 | public function exists() |
| 76 | { |
| 77 | return ! empty($this->id); |
| 78 | } |
| 79 | |
| 80 | public function is_active() |
| 81 | { |
| 82 | return $this->status == 'true'; |
| 83 | } |
| 84 | |
| 85 | public function is_onetime_use() |
| 86 | { |
| 87 | return $this->is_onetime_use == 'true'; |
| 88 | } |
| 89 | |
| 90 | public function is_recurring() |
| 91 | { |
| 92 | return $this->get_coupon_type() == CouponType::RECURRING; |
| 93 | } |
| 94 | |
| 95 | public function is_expired() |
| 96 | { |
| 97 | $end_date = $this->get_end_date(); |
| 98 | |
| 99 | if ( ! empty($end_date) && time() > CarbonImmutable::parse($end_date, 'UTC')->endOfDay()->getTimestamp()) { |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | protected function set_coupon_application($val) |
| 107 | { |
| 108 | $valid_options = (new \ReflectionClass(CouponApplication::class))->getConstants(); |
| 109 | |
| 110 | if (in_array($val, $valid_options)) { |
| 111 | $this->coupon_application = $val; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | public function get_coupon_type() |
| 116 | { |
| 117 | return ! empty($this->coupon_type) ? $this->coupon_type : CouponType::RECURRING; |
| 118 | } |
| 119 | |
| 120 | public function get_coupon_application() |
| 121 | { |
| 122 | return ! empty($this->coupon_application) ? $this->coupon_application : CouponApplication::NEW_PURCHASE; |
| 123 | } |
| 124 | |
| 125 | public function get_id() |
| 126 | { |
| 127 | return absint($this->id); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @return string |
| 132 | */ |
| 133 | public function get_description() |
| 134 | { |
| 135 | return sanitize_textarea_field($this->description); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @return string |
| 140 | */ |
| 141 | public function get_amount() |
| 142 | { |
| 143 | return ! empty($this->amount) ? (float)$this->amount : ''; |
| 144 | } |
| 145 | |
| 146 | protected function set_plan_ids($value) |
| 147 | { |
| 148 | $this->plan_ids = is_array($value) ? \wp_json_encode($value) : $value; |
| 149 | } |
| 150 | |
| 151 | public function get_plan_ids() |
| 152 | { |
| 153 | return $this->plan_ids; |
| 154 | } |
| 155 | |
| 156 | public function get_usage_limit() |
| 157 | { |
| 158 | return ! empty($this->usage_limit) ? absint($this->usage_limit) : ''; |
| 159 | } |
| 160 | |
| 161 | public function get_start_date() |
| 162 | { |
| 163 | return ! empty($this->start_date) && (string)$this->start_date != '0000-00-00' ? (string)$this->start_date : ''; |
| 164 | } |
| 165 | |
| 166 | public function get_end_date() |
| 167 | { |
| 168 | return ! empty($this->end_date) && (string)$this->end_date != '0000-00-00' ? (string)$this->end_date : ''; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Check if a coupon is valid |
| 173 | * |
| 174 | * @param int $plan_id |
| 175 | * @param string $order_type |
| 176 | * |
| 177 | * @return bool |
| 178 | */ |
| 179 | public function is_valid($plan_id = 0, $order_type = OrderType::NEW_ORDER) |
| 180 | { |
| 181 | if ( ! $this->is_active()) return false; |
| 182 | |
| 183 | if ( |
| 184 | $order_type == OrderType::NEW_ORDER && |
| 185 | $this->get_coupon_application() == CouponApplication::EXISTING_PURCHASE |
| 186 | ) { |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | if ( |
| 191 | $order_type != OrderType::NEW_ORDER && |
| 192 | $this->get_coupon_application() == CouponApplication::NEW_PURCHASE |
| 193 | ) { |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | if ( |
| 198 | in_array($order_type, [OrderType::UPGRADE, OrderType::DOWNGRADE]) && |
| 199 | $this->get_coupon_application() == CouponApplication::NEW_PURCHASE |
| 200 | ) { |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | if (is_user_logged_in()) { |
| 205 | |
| 206 | $current_user_id = get_current_user_id(); |
| 207 | |
| 208 | $customer = CustomerFactory::fromUserId($current_user_id); |
| 209 | |
| 210 | if ($this->is_onetime_use() && $customer->exists()) { |
| 211 | |
| 212 | $count = OrderRepository::init()->retrieveBy([ |
| 213 | 'customer_id' => $customer->get_id(), |
| 214 | 'coupon_code' => $this->code |
| 215 | ], true); |
| 216 | |
| 217 | if ($count > 0) return false; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | $start_date = ! empty($this->get_start_date()) ? $this->get_start_date() . ' 00:00:00' : ''; |
| 222 | $end_date = ! empty($this->get_end_date()) ? $this->get_end_date() . ' 23:59:59' : ''; |
| 223 | |
| 224 | if ( ! empty($start_date) && empty($end_date)) { |
| 225 | $result = (new CarbonImmutable($start_date, wp_timezone())) |
| 226 | ->lessThanOrEqualTo(CarbonImmutable::now(wp_timezone())); |
| 227 | |
| 228 | if ( ! $result) return false; |
| 229 | } |
| 230 | |
| 231 | if ( ! empty($end_date) && empty($start_date)) { |
| 232 | $result = (new CarbonImmutable($end_date, wp_timezone())) |
| 233 | ->greaterThanOrEqualTo(CarbonImmutable::now(wp_timezone())); |
| 234 | |
| 235 | if ( ! $result) return false; |
| 236 | } |
| 237 | |
| 238 | if ( ! empty($start_date) && ! empty($end_date)) { |
| 239 | $result = CarbonImmutable::now(wp_timezone())->isBetween( |
| 240 | new CarbonImmutable($start_date, wp_timezone()), |
| 241 | new CarbonImmutable($end_date, wp_timezone()) |
| 242 | ); |
| 243 | |
| 244 | if ( ! $result) return false; |
| 245 | } |
| 246 | |
| 247 | if ( ! empty($plan_id) && ! empty($this->get_plan_ids()) && ! in_array($plan_id, $this->get_plan_ids())) { |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | $usage_limit = absint($this->get_usage_limit()); |
| 252 | $usage_count = absint($this->get_usage_count()); |
| 253 | |
| 254 | if ($usage_limit > 0 && $usage_count >= $usage_limit) { |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Return how many times coupon has been used. update this after every successful order |
| 263 | * |
| 264 | * @return int |
| 265 | */ |
| 266 | public function get_usage_count() |
| 267 | { |
| 268 | return OrderRepository::init()->retrieveBy([ |
| 269 | 'status' => [OrderStatus::COMPLETED, OrderStatus::REFUNDED], |
| 270 | 'coupon_code' => $this->code, |
| 271 | 'number' => 0 |
| 272 | ], true); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @return false|int |
| 277 | */ |
| 278 | public function save() |
| 279 | { |
| 280 | if ($this->exists()) { |
| 281 | |
| 282 | $result = CouponRepository::init()->update($this); |
| 283 | |
| 284 | do_action('ppress_membership_update_coupon', $result, $this); |
| 285 | |
| 286 | return $result; |
| 287 | } |
| 288 | |
| 289 | $result = CouponRepository::init()->add($this); |
| 290 | |
| 291 | do_action('ppress_membership_add_coupon', $result, $this); |
| 292 | |
| 293 | return $result; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * @return false|int |
| 298 | */ |
| 299 | public function activate() |
| 300 | { |
| 301 | return CouponRepository::init()->updateColumn($this->id, 'status', 'true'); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * @return false|int |
| 306 | */ |
| 307 | public function deactivate() |
| 308 | { |
| 309 | return CouponRepository::init()->updateColumn($this->id, 'status', 'false'); |
| 310 | } |
| 311 | } |