public function action($request) { $db = JFactory::getDBO(); $userflags = $request->metaUser->focusSubscription->getMIflags($request->plan->id, $this->id); $total_coupons = array(); $existing_coupons = array(); if (is_array($userflags)) { if (!empty($userflags['coupons'])) { $existing_coupons = explode(',', $userflags['coupons']); $total_coupons = array_merge($total_coupons, $existing_coupons); if (!empty($this->settings['inc_old_coupons'])) { foreach ($existing_coupons as $cid) { $ocph = new couponHandler(); $ocph->load($cid); $ocph->coupon->active = 1; $ocph->restrictions['max_reuse'] += $this->settings['inc_old_coupons']; $ocph->coupon->setParams($ocph->restrictions, 'restrictions'); $ocph->coupon->check(); $ocph->coupon->store(); } } } } $newcodes = array(); if (!empty($existing_coupons) && !empty($this->settings['always_new_coupons']) || empty($existing_coupons)) { if (!empty($this->settings['create_new_coupons']) && !empty($this->settings['master_coupon'])) { $cph = new CouponHandler(); $cph->load($this->settings['master_coupon']); if (is_object($cph->coupon)) { for ($i = 0; $i < $this->settings['create_new_coupons']; $i++) { $newcode = $cph->coupon->generateCouponCode(); $newcodes[] = $newcode; $cph->coupon->id = 0; $cph->coupon->coupon_code = $newcode; $cph->coupon->active = 1; $cph->restrictions['usecount'] = 0; if (!empty($this->settings['max_reuse'])) { $cph->restrictions['max_reuse'] = $this->settings['max_reuse']; } else { $cph->restrictions['max_reuse'] = 1; } if (!empty($this->settings['bind_subscription'])) { $cph->restrictions['depend_on_subscr_id'] = 1; $cph->restrictions['subscr_id_dependency'] = $request->metaUser->focusSubscription->id; } $cph->coupon->storeload(); if (!empty($this->settings['switch_type'])) { $cph->switchType(); } } if (!empty($this->settings['mail_out_coupons'])) { $this->mailOut($request, $newcodes); } } } } $total_coupons = array_merge($total_coupons, $newcodes); $newflags['coupons'] = implode(',', $total_coupons); $request->metaUser->focusSubscription->setMIflags($request->plan->id, $this->id, $newflags); return true; }
public function removeCoupon($coupon_code, $fromId = false) { if (empty($this->coupons)) { return null; } if ($fromId) { $coupon_code = couponHandler::codeFromId($coupon_code); $coupon_code = $coupon_code['coupon_code']; } if (!in_array($coupon_code, $this->coupons)) { return null; } foreach ($this->coupons as $id => $cc) { if ($cc == $coupon_code) { unset($this->coupons[$id]); } } $cph = new couponHandler(); $cph->load($coupon_code); if (!empty($cph->coupon->id)) { $cph->decrementCount($this); } if (empty($this->usage)) { return; } $usage = explode('.', $this->usage); // Update old notation if (!isset($usage[1])) { $temp = $usage[0]; $usage[0] = 'p'; $usage[1] = $temp; } switch (strtolower($usage[0])) { case 'c': case 'cart': $cart = new aecCart(); $cart->load($usage[1]); $cart->removeCoupon($coupon_code); $cart->storeload(); break; } }
public function save() { $row = new Invoice(); $row->load($_POST['id']); $returnTask = $_POST['returnTask']; unset($_POST['id']); unset($_POST['returnTask']); if (empty($_POST['coupons'])) { $_POST['coupons'] = array(); } $previous = array(); if (!empty($row->coupons)) { foreach ($row->coupons as $coupon_code) { $id = couponHandler::idFromCode($coupon_code); $previous[] = $id['id']; } } $added = array(); foreach ($_POST['coupons'] as $coupon_id) { if (!in_array($coupon_id, $previous)) { $added[] = $coupon_id; } else { unset($previous[array_search($coupon_id, $previous)]); } } if (!empty($added)) { foreach ($added as $coupon_id) { $row->addCoupon((int) $coupon_id, true); } } if (!empty($previous)) { foreach ($previous as $coupon_id) { $row->removeCoupon((int) $coupon_id, true); } } unset($_POST['coupons']); $row->savePOSTsettings($_POST); $row->storeload(); $this->setMessage(JText::_('AEC_CONFIG_SAVED')); }
public function loadCoupon($coupon_code, $strict = true) { if (in_array($coupon_code, $this->delete_list) && $strict) { return false; } $cph = new couponHandler(); $cph->load($coupon_code); if (empty($cph->coupon->id)) { $this->setError(JText::_('COUPON_ERROR_INVALID')); $this->delete_list[] = $coupon_code; return false; } if ($cph->coupon->coupon_code !== $coupon_code) { $this->setError(JText::_('COUPON_ERROR_INVALID')); $this->delete_list[] = $coupon_code; return false; } if (!$cph->status) { $this->setError($cph->error); $this->delete_list[] = $coupon_code; return false; } if ($cph->coupon->restrictions['cart_multiple_items'] && !empty($cph->coupon->restrictions['cart_multiple_items_amount'])) { if (!array_key_exists($coupon_code, $this->max_amount_list)) { $this->coupons_list[] = array('coupon_code' => $coupon_code); $this->max_amount_list[$coupon_code] = $this->coupon->restrictions['cart_multiple_items_amount']; } if ($this->max_amount_list[$coupon_code]) { $this->max_amount_list[$coupon_code]--; } else { return false; } } else { $this->coupons_list[] = array('coupon_code' => $coupon_code); } $this->cph = $cph; return true; }