public function execute()
 {
     $om = new shopOrderModel();
     $encoded_order_id = waRequest::param('id');
     $code = waRequest::param('code');
     $order_id = shopHelper::decodeOrderId($encoded_order_id);
     if (!$order_id) {
         // fall back to non-encoded id
         $order_id = $encoded_order_id;
         $encoded_order_id = shopHelper::encodeOrderId($order_id);
     }
     $order = $om->getOrder($order_id);
     if (!$order) {
         throw new waException(_w('Order not found'), 404);
     } elseif (!$this->isAuth($order, $code)) {
         if ($code && $order_id != substr($code, 16, -16)) {
             throw new waException(_w('Order not found'), 404);
         } else {
             $redirect = array('id' => $order_id);
             if (!empty($code)) {
                 $redirect['code'] = $code;
             }
             $url = $code ? '/frontend/myOrderByCode' : '/frontend/myOrder';
             $this->redirect(wa()->getRouteUrl($url, $redirect));
         }
     } elseif ($code && $order['contact_id'] == wa()->getUser()->getId()) {
         $redirect = array('id' => $order_id, 'form_type' => waRequest::param('form_type'), 'form_id' => waRequest::param('form_id'));
         $this->redirect(wa()->getRouteUrl('/frontend/myOrderPrintform', $redirect));
     }
     $order_params_model = new shopOrderParamsModel();
     $order['params'] = $order_params_model->get($order['id']);
     $order['id_str'] = $encoded_order_id;
     switch (waRequest::param('form_type')) {
         case 'payment':
             if (empty($order['params']['payment_id']) || !($payment = shopPayment::getPlugin(null, $order['params']['payment_id']))) {
                 throw new waException(_w('Printform not found'), 404);
             }
             $form_id = waRequest::param('form_id');
             $params = null;
             if (strpos($form_id, '.')) {
                 $form = explode('.', $form_id, 2);
                 $form_id = array_shift($form);
                 $params = array_shift($form);
             }
             print $payment->displayPrintForm(ifempty($form_id, $payment->getId()), shopPayment::getOrderData($order, $payment), intval($params));
             exit;
             break;
         default:
             throw new waException(_w('Printform not found'), 404);
             break;
     }
 }
Exemple #2
0
 /**
  * Returns array of print forms available for specified order.
  *
  * @param array|null $order Order data; if not specified, print forms applicable to any orders are returned
  * @return array
  */
 public static function getPrintForms($order = null)
 {
     $plugins = wa('shop')->getConfig()->getPlugins();
     foreach ($plugins as $id => $plugin) {
         if (empty($plugin['printform'])) {
             unset($plugins[$id]);
         }
     }
     if ($order) {
         $type = 'payment';
         $key = ifempty($order['params'][$type . '_id']);
         try {
             if (!empty($key) && ($plugin = shopPayment::getPlugin(null, $key)) && method_exists($plugin, 'getPrintForms')) {
                 $forms = $plugin->getPrintForms(shopPayment::getOrderData($order));
                 foreach ($forms as $id => $form) {
                     $plugins["{$type}.{$id}"] = $form;
                 }
             }
         } catch (waException $e) {
         }
         $type = 'shipping';
         $key = ifempty($order['params'][$type . '_id']);
         try {
             if (!empty($key) && ($plugin = shopShipping::getPlugin(null, $key)) && method_exists($plugin, 'getPrintForms')) {
                 $forms = $plugin->getPrintForms(shopPayment::getOrderData($order));
                 foreach ($forms as $id => $form) {
                     $plugins["{$type}.{$id}"] = $form;
                 }
             }
         } catch (waException $e) {
         }
         foreach ($plugins as $plugin_id => &$plugin) {
             if (strpos($plugin_id, '.')) {
                 $plugin['url'] = "?module=order&action=printform&form_id={$plugin_id}&order_id={$order['id']}";
             } else {
                 $plugin['url'] = "?plugin={$plugin_id}&module=printform&action=display&order_id={$order['id']}";
             }
         }
         unset($plugin);
     }
     //TODO separate backend & frontend
     return $plugins;
 }
 public function execute()
 {
     $steps = $this->getConfig()->getCheckoutSettings();
     $current_step = waRequest::param('step', waRequest::request('step'));
     if (!$current_step) {
         $current_step = key($steps);
     }
     $title = _w('Checkout');
     if ($current_step == 'success') {
         $order_id = waRequest::get('order_id');
         if (!$order_id) {
             $order_id = wa()->getStorage()->get('shop/order_id');
             $payment_success = false;
         } else {
             $payment_success = true;
             $this->view->assign('payment_success', true);
         }
         if (!$order_id) {
             wa()->getResponse()->redirect(wa()->getRouteUrl('shop/frontend'));
         }
         $order_model = new shopOrderModel();
         $order = $order_model->getById($order_id);
         if ($order) {
             $order['_id'] = $order['id'];
         }
         if (!$payment_success) {
             $order_params_model = new shopOrderParamsModel();
             $order['params'] = $order_params_model->get($order_id);
             $order_items_model = new shopOrderItemsModel();
             $order['items'] = $order_items_model->getByField('order_id', $order_id, true);
             $payment = '';
             if (!empty($order['params']['payment_id'])) {
                 try {
                     /**
                      * @var waPayment $plugin
                      */
                     $plugin = shopPayment::getPlugin(null, $order['params']['payment_id']);
                     $payment = $plugin->payment(waRequest::post(), shopPayment::getOrderData($order, $plugin), true);
                 } catch (waException $ex) {
                     $payment = $ex->getMessage();
                 }
             }
             $order['id'] = shopHelper::encodeOrderId($order_id);
             $this->getResponse()->addGoogleAnalytics($this->getGoogleAnalytics($order));
         } else {
             $order['id'] = shopHelper::encodeOrderId($order_id);
         }
         $this->view->assign('order', $order);
         if (isset($payment)) {
             $this->view->assign('payment', $payment);
         }
     } else {
         $cart = new shopCart();
         if (!$cart->count() && $current_step != 'error') {
             $current_step = 'error';
             $this->view->assign('error', _w('Your shopping cart is empty. Please add some products to cart, and then proceed to checkout.'));
         }
         if ($current_step != 'error') {
             if (waRequest::method() == 'post') {
                 if (waRequest::post('wa_auth_login')) {
                     $login_action = new shopLoginAction();
                     $login_action->run();
                 } else {
                     $redirect = false;
                     foreach ($steps as $step_id => $step) {
                         if ($step_id == $current_step) {
                             $step_instance = $this->getStep($step_id);
                             if ($step_instance->execute()) {
                                 $redirect = true;
                             }
                         } elseif ($redirect) {
                             $this->redirect(wa()->getRouteUrl('/frontend/checkout', array('step' => $step_id)));
                         }
                     }
                     // last step
                     if ($redirect) {
                         if ($this->createOrder()) {
                             $this->redirect(wa()->getRouteUrl('/frontend/checkout', array('step' => 'success')));
                         }
                     }
                 }
             } else {
                 $this->view->assign('error', '');
             }
             $title .= ' - ' . $steps[$current_step]['name'];
             $steps[$current_step]['content'] = $this->getStep($current_step)->display();
             $this->view->assign('checkout_steps', $steps);
         }
     }
     $this->getResponse()->setTitle($title);
     $this->view->assign('checkout_current_step', $current_step);
     /**
      * @event frontend_checkout
      * @return array[string]string $return[%plugin_id%] html output
      */
     $event_params = array('step' => $current_step);
     $this->view->assign('frontend_checkout', wa()->event('frontend_checkout', $event_params));
     if (waRequest::isXMLHttpRequest()) {
         $this->setThemeTemplate('checkout.' . $current_step . '.html');
     } else {
         $this->setLayout(new shopFrontendLayout());
         $this->setThemeTemplate('checkout.html');
     }
 }
 public function execute()
 {
     $id = waRequest::get('order_id');
     if (!$id) {
         throw new waException("Unknown order", 404);
     }
     $order = $this->getOrder($id);
     if (!$order) {
         $id = shopHelper::decodeOrderId($id);
         $order = $this->getOrder($id);
         if (!$order) {
             throw new waException("Unkown order", 404);
         }
     }
     $product_ids = array();
     foreach ($order['items'] as $item) {
         if ($item['type'] == 'product') {
             $product_ids[] = $item['product_id'];
         }
     }
     $product_ids = array_unique($product_ids);
     $form_id = waRequest::get('form_id');
     if (strpos($form_id, '.')) {
         list($type, $form) = explode('.', $form_id, 2);
     } else {
         $form = null;
         $type = $form_id;
     }
     $order_params_model = new shopOrderParamsModel();
     $params = $order_params_model->get($order['id']);
     $plugin = self::getPlugin($type, ifempty($params[$type . '_id']));
     if ($type == 'shipping') {
         /* add weight info only for shipping modules */
         $feature_model = new shopFeatureModel();
         $f = $feature_model->getByCode('weight');
         if (!$f) {
             $weights = array();
         } else {
             $values_model = $feature_model->getValuesModel($f['type']);
             $weights = $values_model->getProductValues($product_ids, $f['id']);
         }
         if ($weights) {
             $dimension = shopDimension::getInstance()->getDimension('weight');
             $weight_unit = $plugin->allowedWeightUnit();
             $m = null;
             if ($weight_unit != $dimension['base_unit']) {
                 $m = $dimension['units'][$weight_unit]['multiplier'];
             }
             foreach ($order['items'] as &$item) {
                 if ($item['type'] == 'product') {
                     if (isset($weights['skus'][$item['sku_id']])) {
                         $w = $weights['skus'][$item['sku_id']];
                     } else {
                         $w = isset($weights[$item['product_id']]) ? $weights[$item['product_id']] : 0;
                     }
                     if ($m !== null) {
                         $w = $w / $m;
                     }
                     $item['weight'] = $w;
                 }
             }
             unset($item);
         }
     }
     if (!$plugin) {
         throw new waException(_w('Printform not found'), 404);
     }
     print $plugin->displayPrintForm(ifempty($form, $plugin->getId()), shopPayment::getOrderData($order, $plugin));
     exit;
 }
 public function execute()
 {
     $encoded_order_id = waRequest::param('id');
     $order_id = shopHelper::decodeOrderId($encoded_order_id);
     if (!$order_id) {
         // fall back to non-encoded id
         $order_id = $encoded_order_id;
         $encoded_order_id = shopHelper::encodeOrderId($order_id);
     }
     // Check that order exists and belongs to this user
     $om = new shopOrderModel();
     $order = $om->getOrder($order_id);
     if (!$order || !$this->isAuth($order)) {
         throw new waException(_w('Order not found'), 404);
     }
     if ($order['paid_date']) {
         foreach ($order['items'] as &$i) {
             if (!empty($i['file_name'])) {
                 $i['download_link'] = wa()->getRouteUrl('/frontend/myOrderDownload', array('id' => $order['id'], 'code' => $order['params']['auth_code'], 'item' => $i['id']), true);
             }
         }
         unset($i);
     }
     $workflow = new shopWorkflow();
     $order_params_model = new shopOrderParamsModel();
     $order['params'] = $order_params_model->get($order['id']);
     $order['id_str'] = $encoded_order_id;
     $order['state'] = $workflow->getStateById($order['state_id']);
     // Order subtotal
     $subtotal = 0;
     foreach ($order['items'] as $item) {
         $subtotal += $item['price'] * $item['quantity'];
     }
     // Order comment
     $lm = new shopOrderLogModel();
     $l = $lm->getByField(array('action_id' => 'create', 'order_id' => $order['id']));
     $order['comment'] = ifempty($l['text']);
     $order['payment_name'] = ifset($order['params']['payment_name'], '');
     $order['shipping_name'] = ifset($order['params']['shipping_name'], '');
     // Shipping and billing addresses
     $settings = wa('shop')->getConfig()->getCheckoutSettings();
     $form_fields = ifset($settings['contactinfo']['fields'], array());
     $formatter = new waContactAddressSeveralLinesFormatter();
     $shipping_address = shopHelper::getOrderAddress($order['params'], 'shipping');
     if ($shipping_address) {
         $shipping_address = $formatter->format(array('data' => $shipping_address));
         $shipping_address = $shipping_address['value'];
     }
     if (isset($form_fields['address.billing'])) {
         $billing_address = shopHelper::getOrderAddress($order['params'], 'billing');
         $billing_address = $formatter->format(array('data' => $billing_address));
         $billing_address = $billing_address['value'];
     } else {
         $billing_address = null;
     }
     if (wa()->getUser()->getId() == $order['contact_id']) {
         $contact = wa()->getUser();
     } else {
         $contact = new waContact($order['contact_id']);
     }
     $payment = '';
     if (!empty($order['params']['payment_id']) && !$order['paid_date']) {
         try {
             $plugin = shopPayment::getPlugin(null, $order['params']['payment_id']);
             $payment = $plugin->payment(waRequest::post(), shopPayment::getOrderData($order, $plugin), false);
         } catch (waException $ex) {
             $payment = $ex->getMessage();
         }
     }
     $this->view->assign('payment', $payment);
     $tracking = '';
     if (!empty($order['params']['shipping_id']) && !empty($order['params']['tracking_number'])) {
         try {
             $plugin = shopShipping::getPlugin(null, $order['params']['shipping_id']);
             $tracking = $plugin->tracking($order['params']['tracking_number']);
         } catch (waException $ex) {
             $tracking = $ex->getMessage();
         }
     }
     $this->view->assign('tracking', $tracking);
     $this->view->assign('order', $order);
     $this->view->assign('contact', $contact);
     $this->view->assign('shipping_address', $shipping_address);
     $this->view->assign('billing_address', $billing_address);
     $this->view->assign('subtotal', $subtotal);
     // Set up layout and template from theme
     $this->setThemeTemplate('my.order.html');
     $this->view->assign('my_nav_selected', 'orders');
     if (!waRequest::isXMLHttpRequest()) {
         $this->setLayout(new shopFrontendLayout());
         $this->getResponse()->setTitle(_w('Order') . ' ' . $encoded_order_id);
         $this->view->assign('breadcrumbs', self::getBreadcrumbs());
         $this->layout->assign('nofollow', true);
     }
 }