Exemplo n.º 1
0
 public static function formatValue($c, $curr = null)
 {
     static $currencies = null;
     if ($currencies === null) {
         if ($curr) {
             $currencies = $curr;
         } else {
             $curm = new shopCurrencyModel();
             $currencies = $curm->getAll('code');
         }
     }
     if ($c['type'] == '$FS') {
         return _w('Free shipping');
     } else {
         if ($c['type'] === '%') {
             return waCurrency::format('%0', $c['value'], 'USD') . '%';
         } else {
             if (!empty($currencies[$c['type']])) {
                 return waCurrency::format('%0{s}', $c['value'], $c['type']);
             } else {
                 // Coupon of unknown type. Possibly from a plugin?..
                 return '';
             }
         }
     }
 }
 public function __construct($coupon = array())
 {
     waLocale::loadByDomain(array('shop', 'coupon'));
     waSystem::pushActivePlugin('coupon', 'shop');
     $this->data['code'] = ifempty($coupon['code'], '');
     $this->data['expire'] = ifempty($coupon['expire_datetime']);
     $curm = new shopCurrencyModel();
     $currencies = $curm->getAll('code');
     $this->data['discount'] = shopCouponPlugin::formatValue($coupon, $currencies);
     waSystem::popActivePlugin();
 }
Exemplo n.º 3
0
 public function execute()
 {
     $id = waRequest::request('id', 0, 'int');
     $coupm = new shopCouponModel();
     $coupon = $coupm->getById($id);
     if ($coupon) {
         $coupon['value'] = (double) $coupon['value'];
         if (waRequest::request('delete')) {
             $coupm->delete($id);
             exit;
         }
     } else {
         if ($id) {
             throw new waException('Coupon not found.', 404);
         } else {
             // show form to create new coupon
             $coupon = $coupm->getEmptyRow();
             $coupon['code'] = self::generateCode();
         }
     }
     //
     // Process POST data
     //
     $duplicate_code_error = null;
     if (waRequest::post()) {
         $post_coupon = waRequest::post('coupon');
         if (is_array($post_coupon)) {
             $post_coupon = array_intersect_key($post_coupon, $coupon) + array('code' => '', 'type' => '%');
             if (empty($post_coupon['limit'])) {
                 $post_coupon['limit'] = null;
             }
             if (!empty($post_coupon['value'])) {
                 $post_coupon['value'] = (double) str_replace(',', '.', $post_coupon['value']);
             }
             if (empty($post_coupon['code'])) {
                 throw new waException('Bad parameters', 500);
                 // rely on JS validation
             }
             if (!empty($post_coupon['expire_datetime']) && strlen($post_coupon['expire_datetime']) == 10) {
                 $post_coupon['expire_datetime'] .= ' 23:59:59';
             }
             if ($post_coupon['type'] == '%') {
                 $post_coupon['value'] = min(max($post_coupon['value'], 0), 100);
             }
             if ($id) {
                 $coupm->updateById($id, $post_coupon);
                 echo '<script>window.location.hash = "#/coupons/' . $id . '";$.orders.dispatch();</script>';
                 exit;
             } else {
                 $post_coupon['create_contact_id'] = wa()->getUser()->getId();
                 $post_coupon['create_datetime'] = date('Y-m-d H:i:s');
                 try {
                     $id = $coupm->insert($post_coupon);
                     echo '<script>' . 'var counter = $("#s-coupons .count");' . 'var cnt = parseInt(counter.text(), 10) || 0;' . 'counter.text(cnt + 1);' . 'window.location.hash = "#/coupons/' . $id . '";' . '</script>';
                     exit;
                 } catch (waDbException $e) {
                     // Duplicate code. Show error in form.
                     $coupon = $post_coupon + $coupon;
                     $duplicate_code_error = true;
                 }
             }
         }
     }
     // Coupon types
     $curm = new shopCurrencyModel();
     $currencies = $curm->getAll('code');
     $types = self::getTypes($currencies);
     // Orders this coupon was used for
     $orders = array();
     $overall_discount = 0;
     $overall_discount_formatted = '';
     if ($coupon['id']) {
         $om = new shopOrderModel();
         $cm = new shopCurrencyModel();
         $orders = $om->getByCoupon($coupon['id']);
         shopHelper::workupOrders($orders);
         foreach ($orders as &$o) {
             $discount = ifset($o['params']['coupon_discount'], 0);
             $o['coupon_discount_formatted'] = waCurrency::format('%{s}', $discount, $o['currency']);
             if ($discount) {
                 $overall_discount += $cm->convert($discount, $o['currency'], $cm->getPrimaryCurrency());
                 $o['coupon_discount_percent'] = round($discount * 100.0 / ($discount + $o['total']), 1);
             } else {
                 $o['coupon_discount_percent'] = 0;
             }
         }
         unset($o);
         $overall_discount_formatted = waCurrency::format('%{s}', $overall_discount, $cm->getPrimaryCurrency());
     }
     $this->view->assign('types', $types);
     $this->view->assign('orders', $orders);
     $this->view->assign('coupon', $coupon);
     $this->view->assign('duplicate_code_error', $duplicate_code_error);
     $this->view->assign('overall_discount', $overall_discount);
     $this->view->assign('overall_discount_formatted', $overall_discount_formatted);
     $this->view->assign('formatted_value', shopCouponsAction::formatValue($coupon, $currencies));
     $this->view->assign('is_enabled', shopCouponsAction::isEnabled($coupon));
 }
 public function execute()
 {
     $id = waRequest::request('id', 0, 'int');
     $m = new shopCouponPluginTemplateModel();
     $gen = $m->getById($id);
     if ($gen) {
         $gen['value'] = (double) $gen['value'];
         if (waRequest::request('delete')) {
             $m->deleteById($id);
             exit;
         }
     } else {
         if ($id) {
             throw new waException('Generator not found.', 404);
         } else {
             // show form to create new coupon
             $gen = $m->getEmptyRow();
         }
     }
     $is_new = false;
     if (waRequest::post()) {
         $post_gen = waRequest::post('gen');
         if (is_array($post_gen)) {
             $post_gen = array_intersect_key($post_gen, $gen) + array('type' => '%');
             if (empty($post_gen['limit'])) {
                 $post_gen['limit'] = null;
             }
             if (!empty($post_gen['value'])) {
                 $post_gen['value'] = (double) str_replace(',', '.', $post_gen['value']);
             }
             if ($post_gen['type'] == '%') {
                 $post_gen['value'] = min(max($post_gen['value'], 0), 100);
             }
             if (empty($post_gen['expire_hours'])) {
                 $post_gen['expire_hours'] = null;
             }
             foreach (array('expire_hours', 'num', 'latin_lowercase', 'latin_uppercase', 'cyr_lowercase', 'cyr_uppercase', 'other') as $k) {
                 if (empty($post_gen[$k])) {
                     $post_gen[$k] = null;
                 }
             }
             if ($id) {
                 $m->updateById($id, $post_gen);
                 $gen = $m->getById($id);
             } else {
                 $post_gen['create_contact_id'] = wa()->getUser()->getId();
                 try {
                     $id = $m->insert($post_gen);
                     $gen = $m->getById($id);
                     $is_new = true;
                 } catch (waDbException $e) {
                     // Duplicate code. Show error in form.
                     $gen = $post_gen + $gen;
                 }
             }
         }
     }
     // Coupon types
     $curm = new shopCurrencyModel();
     $currencies = $curm->getAll('code');
     $types = self::getTypes($currencies);
     $this->view->assign('types', $types);
     $this->view->assign('gen', $gen);
     $this->view->assign('is_new', $is_new);
     $this->view->assign('formatted_value', shopCouponPlugin::formatValue($gen, $currencies));
 }