Beispiel #1
0
 public function execute()
 {
     $curm = new shopCurrencyModel();
     $currencies = $curm->getAll('code');
     $coupm = new shopCouponModel();
     $coupons = $coupm->order('id DESC')->fetchAll('code');
     foreach ($coupons as &$c) {
         $c['enabled'] = self::isEnabled($c);
         $c['hint'] = self::formatValue($c, $currencies);
     }
     unset($c);
     $order_model = new shopOrderModel();
     $count_new = $order_model->getStateCounters('new');
     $this->view->assign(array('coupons' => $coupons, 'order_count_new' => $count_new));
 }
 /**
  * @param int[] $params Deleted contact_id
  * @see waEventHandler::execute()
  * @return void
  */
 public function execute(&$params)
 {
     $contact_ids = $params;
     // We need some info about (not yet) deleted contacts to save into order params.
     // The idea is to pretend that orders were created by guests with no auth.
     $c = new waContactsCollection('id/' . implode(',', $contact_ids));
     $contacts = $c->getContacts('name,phone,email');
     foreach ($contacts as &$contact) {
         if (is_array($contact['phone'])) {
             $phone = reset($contact['phone']);
             if (is_array($phone)) {
                 if (isset($phone['value'])) {
                     $phone = $phone['value'];
                 } else {
                     $phone = '';
                 }
             }
             $contact['phone'] = $phone;
         }
         if (is_array($contact['email'])) {
             $email = reset($contact['email']);
             $contact['email'] = $email;
         }
     }
     $order_model = new shopOrderModel();
     $order_params_model = new shopOrderParamsModel();
     $product_reviews_model = new shopProductReviewsModel();
     foreach ($contacts as $contact) {
         // Insert customer info into params of their orders
         $order_ids = array_keys($order_model->select('id')->where('contact_id=:contact_id', array('contact_id' => $contact['id']))->fetchAll('id'));
         $order_params_model->set($order_ids, $this->extractContactInfo($contact), false);
         // Insert contact name into their reviews
         $product_reviews_model->updateByField('contact_id', $contact['id'], array('contact_id' => 0, 'name' => $contact['name'], 'auth_provider' => null));
     }
     // Update orders as if they were created by guests with no auth
     $order_model->updateByField('contact_id', $contact_ids, array('contact_id' => null));
     // Forget the customer
     $scm = new shopCustomerModel();
     $scm->deleteById($contact_ids);
     // Forget that this user created coupons
     $coupm = new shopCouponModel();
     $coupm->updateByField('create_contact_id', $contact_ids, array('create_contact_id' => 0));
     // !!! TODO: take a look to other models related with contacts
 }
 public function execute()
 {
     if (!wa()->getUser()->getRights('shop', 'orders')) {
         throw new waException(_w("Access denied"));
     }
     $this->setLayout(new shopBackendLayout());
     $this->getResponse()->setTitle(_w('Orders'));
     $config = $this->getConfig();
     $order_model = new shopOrderModel();
     $state_counters = $order_model->getStateCounters();
     $pending_count = (!empty($state_counters['new']) ? $state_counters['new'] : 0) + (!empty($state_counters['processing']) ? $state_counters['processing'] : 0) + (!empty($state_counters['paid']) ? $state_counters['paid'] : 0);
     $cm = new shopCouponModel();
     /*
      * @event backend_orders
      * @return array[string]array $return[%plugin_id%] array of html output
      * @return array[string][string]string $return[%plugin_id%]['sidebar_top_li'] html output
      * @return array[string][string]string $return[%plugin_id%]['sidebar_bottom_li'] html output
      * @return array[string][string]string $return[%plugin_id%]['sidebar_section'] html output
      */
     $backend_orders = wa()->event('backend_orders');
     $this->getLayout()->assign('backend_orders', $backend_orders);
     $this->view->assign(array('states' => $this->getStates(), 'user_id' => $this->getUser()->getId(), 'contacts' => array(), 'default_view' => $config->getOption('orders_default_view'), 'coupons_count' => $cm->countActive(), 'state_counters' => $state_counters, 'pending_count' => $pending_count, 'all_count' => $order_model->countAll(), 'storefronts' => $order_model->getStorefrontCounters(), 'backend_orders' => $backend_orders));
 }
 /** Coupon discounts implementation. */
 protected static function byCoupons(&$order, $contact, $apply)
 {
     $currency = isset($order['currency']) ? $order['currency'] : wa('shop')->getConfig()->getCurrency(false);
     $checkout_data = wa('shop')->getStorage()->read('shop/checkout');
     if (empty($checkout_data['coupon_code'])) {
         return 0;
         // !!! Will this fail when recalculating existing order?
     }
     $cm = new shopCouponModel();
     $coupon = $cm->getByField('code', $checkout_data['coupon_code']);
     if (!$coupon || !shopCouponsAction::isEnabled($coupon)) {
         return 0;
     }
     switch ($coupon['type']) {
         case '$FS':
             $order['shipping'] = 0;
             $result = 0;
             break;
         case '%':
             $result = max(0.0, min(100.0, (double) $coupon['value'])) * $order['total'] / 100.0;
             break;
         default:
             // Flat value in currency
             $result = max(0.0, (double) $coupon['value']);
             if ($currency != $coupon['type']) {
                 $crm = new shopCurrencyModel();
                 $result = (double) $crm->convert($result, $coupon['type'], $currency);
             }
             break;
     }
     if ($apply) {
         $cm->useOne($coupon['id']);
         if (empty($order['params'])) {
             $order['params'] = array();
         }
         $order['params']['coupon_id'] = $coupon['id'];
         $order['params']['coupon_discount'] = $result;
     }
     return $result;
 }
 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));
 }
Beispiel #6
0
 public function getOrder($id, $extend = false, $escape = true)
 {
     $order = $this->getById($id);
     if (!$order) {
         return array();
     }
     $order_params_model = new shopOrderParamsModel();
     $order['params'] = $order_params_model->get($id);
     if ($order['contact_id']) {
         $contact = new waContact($order['contact_id']);
         $order['contact'] = array('id' => $order['contact_id'], 'name' => $contact->getName(), 'email' => $contact->get('email', 'default'), 'phone' => $contact->get('phone', 'default'));
         $config = wa('shop')->getConfig();
         $use_gravatar = $config->getGeneralSettings('use_gravatar');
         $gravatar_default = $config->getGeneralSettings('gravatar_default');
         if (!$contact->get('photo') && $use_gravatar) {
             $order['contact']['photo_50x50'] = shopHelper::getGravatar($order['contact']['email'], 50, $gravatar_default);
         } else {
             $order['contact']['photo_50x50'] = $contact->getPhoto(50);
         }
     } else {
         $order['contact'] = $this->extractConctactInfo($order['params']);
     }
     if (!empty($order['params']['coupon_id'])) {
         $coupon_model = new shopCouponModel();
         $coupon = $coupon_model->getById($order['params']['coupon_id']);
         $order['coupon'] = array();
         if ($coupon) {
             $order['coupon'] = $coupon;
         } else {
             if (!empty($order['params']['coupon_code'])) {
                 $order['coupon']['code'] = $order['params']['coupon_code'];
             }
         }
     }
     $order_items_model = new shopOrderItemsModel();
     $order['items'] = $order_items_model->getItems($id, $extend);
     if ($escape) {
         if (!empty($order['items'])) {
             foreach ($order['items'] as &$product) {
                 if (!empty($product['name'])) {
                     $product['name'] = htmlspecialchars($product['name']);
                 }
                 if (!empty($product['item']['name'])) {
                     $product['item']['name'] = htmlspecialchars($product['item']['name']);
                 }
                 if (!empty($product['skus'])) {
                     foreach ($product['skus'] as &$sku) {
                         if (!empty($sku['name'])) {
                             $sku['name'] = htmlspecialchars($sku['name']);
                         }
                         unset($sku);
                     }
                 }
                 if (!empty($product['services'])) {
                     foreach ($product['services'] as &$service) {
                         if (!empty($service['name'])) {
                             $service['name'] = htmlspecialchars($service['name']);
                         }
                         if (!empty($service['item']['name'])) {
                             $service['item']['name'] = htmlspecialchars($service['item']['name']);
                         }
                         if (!empty($service['variants'])) {
                             foreach ($service['variants'] as &$variant) {
                                 $variant['name'] = htmlspecialchars($variant['name']);
                                 unset($variant);
                             }
                         }
                         unset($service);
                     }
                 }
                 unset($product);
             }
         }
         $order['contact']['name'] = htmlspecialchars($order['contact']['name']);
     }
     return $order;
 }
 public function backendOrders()
 {
     if ($this->getSettings('remove_expired')) {
         $m = new shopCouponModel();
         $m->query('DELETE FROM ' . $m->getTableName() . ' WHERE expire_datetime < ? AND used = 0', date('Y-m-d H:i:s'));
     }
     return array();
 }