예제 #1
0
 /**
  * Sends a test notification.
  * 
  * @param int $id Notification id stored in table shop_notification
  * @param array $data Order data array
  * @param string|null $to Recipient address/number. If not specified, recipient address/number from notification parameters is used.
  */
 public static function sendOne($id, $data, $to = null)
 {
     $n = self::getModel()->getOne($id);
     if ($n) {
         $params_model = new shopOrderParamsModel();
         $data['order']['params'] = $params_model->get($data['order']['id']);
         $items_model = new shopOrderItemsModel();
         $data['order']['items'] = $items_model->getItems($data['order']['id']);
         foreach ($data['order']['items'] as &$i) {
             if (!empty($i['file_name'])) {
                 $i['download_link'] = wa()->getRouteUrl('/frontend/myOrderDownload', array('id' => $data['order']['id'], 'code' => $data['order']['params']['auth_code'], 'item' => $i['id']), true);
             }
         }
         unset($i);
         if (!empty($data['order']['params']['shipping_id'])) {
             try {
                 $data['shipping_plugin'] = shopShipping::getPlugin($data['order']['params']['shipping_plugin'], $data['order']['params']['shipping_id']);
             } catch (waException $e) {
             }
         }
         $method = 'send' . ucfirst($n['transport']);
         if (method_exists('shopNotifications', $method)) {
             if ($to !== null) {
                 $n['to'] = $to;
             }
             self::$method($n, $data);
         }
     }
 }
예제 #2
0
 public static function getInstance()
 {
     if (!isset(self::$instance)) {
         self::$instance = new self();
     }
     return self::$instance;
 }
 protected function getRates($shipping_id, $items, $address, $total)
 {
     $plugin = shopShipping::getPlugin(null, $shipping_id);
     $weight_unit = $plugin->allowedWeightUnit();
     $dimension = shopDimension::getInstance()->getDimension('weight');
     if ($weight_unit != $dimension['base_unit']) {
         foreach ($items as $item_id => $item) {
             if ($item['weight']) {
                 $items[$item_id]['weight'] = $item['weight'] / $dimension['units'][$weight_unit]['multiplier'];
             }
         }
     }
     $currency = $plugin->allowedCurrency();
     $currrent_currency = wa()->getConfig()->getCurrency(false);
     if ($currency != $currrent_currency) {
         $total = shop_currency($total, $currrent_currency, $currency, false);
     }
     $rates = $plugin->getRates($items, $address, array('total_price' => $total));
     if (is_array($rates)) {
         $is_html = waRequest::request('html');
         foreach ($rates as $r_id => &$r) {
             $r['id'] = $r_id;
             $r['rate_html'] = $is_html ? shop_currency_html($r['rate'], $r['currency']) : shop_currency($r['rate'], $r['currency']);
             $r['rate'] = shop_currency($r['rate'], $r['currency']);
         }
         unset($r);
         return array_values($rates);
     }
     return $rates;
 }
 public function execute()
 {
     if (!$this->getUser()->getRights('shop', 'settings')) {
         throw new waRightsException(_w('Access denied'));
     }
     $this->view->assign('plugin_id', $plugin_id = waRequest::get('plugin_id'));
     $this->view->assign('plugin', $info = shopShipping::getPluginInfo($plugin_id));
     $params = array('namespace' => "shipping[settings]", 'value' => waRequest::post('shipping[settings]'));
     $this->view->assign('settings_html', shopShipping::getPlugin($info['plugin'], $plugin_id)->getSettingsHTML($params));
 }
 public function execute()
 {
     if (!$this->getUser()->getRights('shop', 'settings')) {
         throw new waRightsException(_w('Access denied'));
     }
     $model = new shopPluginModel();
     $this->view->assign('instances', $model->listPlugins(shopPluginModel::TYPE_SHIPPING, array('all' => true)));
     $this->view->assign('plugins', shopShipping::getList());
     $feature_model = new shopFeatureModel();
     $this->view->assign('no_weight', $feature_model->getByCode('weight') ? false : true);
     $this->view->assign('installer', $this->getUser()->getRights('installer', 'backend'));
 }
 public function execute()
 {
     $plugin_id = waRequest::param('plugin_id', 0, 'int');
     if (!$plugin_id) {
         throw new waException('Plugin not found', 404);
     }
     $plugin = shopShipping::getPlugin(null, $plugin_id);
     $action = waRequest::param('action_id');
     $method = $action . 'Action';
     if (!$action || !method_exists($plugin, $method)) {
         throw new waException('Action not found', 404);
     }
     $plugin->{$method}();
 }
 public function execute()
 {
     if (!$this->getUser()->getRights('shop', 'settings')) {
         throw new waRightsException(_w('Access denied'));
     }
     if ($plugin = waRequest::post('shipping')) {
         try {
             if (!isset($plugin['settings'])) {
                 $plugin['settings'] = array();
             }
             shopShipping::savePlugin($plugin);
             $this->response['message'] = _w('Saved');
         } catch (waException $ex) {
             $this->setError($ex->getMessage());
         }
     }
 }
예제 #8
0
 public function execute()
 {
     $order = $this->getOrder();
     if (!$order) {
         $this->view->assign('order', $order);
         return;
     }
     $workflow = new shopWorkflow();
     $actions = $workflow->getStateById($order['state_id'])->getActions();
     $bottom_buttons = $top_buttons = $buttons = array();
     foreach ($actions as $action) {
         /**
          * @var shopWorkflowAction $action
          */
         if ($action->getOption('top') || $action->getOption('position') == 'top') {
             $top_buttons[] = $action->getButton();
         } elseif ($action->getOption('position') == 'bottom') {
             $bottom_buttons[] = $action->getButton();
         } else {
             $buttons[] = $action->getButton();
         }
     }
     $config = $this->getConfig();
     $last_action_datetime = null;
     $log_model = new shopOrderLogModel();
     $log = $log_model->getLog($order['id']);
     foreach ($log as &$l) {
         if ($l['action_id']) {
             $l['action'] = $workflow->getActionById($l['action_id']);
         }
         if ($order['state_id'] == $l['after_state_id']) {
             $last_action_datetime = $l['datetime'];
         }
     }
     $params = $order['params'];
     $tracking = '';
     if (!empty($params['shipping_id'])) {
         try {
             $plugin = shopShipping::getPlugin(null, $params['shipping_id']);
             if (!empty($params['tracking_number'])) {
                 $tracking = $plugin->tracking($params['tracking_number']);
             }
             if ($custom_fields = $plugin->customFields(new waOrder())) {
                 foreach ($custom_fields as $k => $v) {
                     if (!empty($params['shipping_params_' . $k])) {
                         $custom_fields[$k]['value'] = $params['shipping_params_' . $k];
                     } else {
                         unset($custom_fields[$k]);
                     }
                 }
                 $this->view->assign('custom_fields', $custom_fields);
             }
         } catch (waException $ex) {
             $tracking = $ex->getMessage();
         }
     }
     $this->view->assign('tracking', $tracking);
     $settings = wa('shop')->getConfig()->getCheckoutSettings();
     $form_fields = ifset($settings['contactinfo']['fields'], array());
     $formatter = new waContactAddressSeveralLinesFormatter();
     $shipping_address = shopHelper::getOrderAddress($params, 'shipping');
     $this->view->assign('shipping_address_text', shopHelper::getShippingAddressText($params));
     $shipping_address = $formatter->format(array('data' => $shipping_address));
     $shipping_address = $shipping_address['value'];
     if (isset($form_fields['address.billing'])) {
         $billing_address = shopHelper::getOrderAddress($params, 'billing');
         $billing_address = $formatter->format(array('data' => $billing_address));
         $billing_address = $billing_address['value'];
         if ($billing_address === $shipping_address) {
             $billing_address = null;
         }
     } else {
         $billing_address = null;
     }
     $customer_model = new shopCustomerModel();
     $customer = $customer_model->getById($order['contact_id']);
     $customer_contact = new waContact($order['contact_id']);
     // Customer info
     $main_contact_info = array();
     foreach (array('email', 'phone', 'im') as $f) {
         if ($v = $customer_contact->get($f, 'top,html')) {
             $main_contact_info[] = array('id' => $f, 'name' => waContactFields::get($f)->getName(), 'value' => is_array($v) ? implode(', ', $v) : $v);
         }
     }
     $this->view->assign(array('customer' => $customer, 'customer_contact' => $customer_contact, 'main_contact_info' => $main_contact_info, 'currency' => $config->getCurrency(), 'order' => $order, 'params' => $params, 'log' => $log, 'last_action_datetime' => $last_action_datetime, 'bottom_buttons' => $bottom_buttons, 'top_buttons' => $top_buttons, 'buttons' => $buttons, 'filter_params' => $this->getParams(), 'filter_params_str' => $this->getParams(true), 'count_new' => $this->getModel()->getStateCounters('new'), 'timeout' => $config->getOption('orders_update_list'), 'printable_docs' => shopHelper::getPrintForms(array_merge($order, array('params' => $params))), 'billing_address' => $billing_address, 'shipping_address' => $shipping_address, 'shipping_id' => ifset($params['shipping_id'], '') . '.' . ifset($params['shipping_rate_id'], ''), 'offset' => $this->getModel()->getOffset($order['id'], $this->getParams(), true)));
     /**
      * Backend order profile page
      * UI hook allow extends order profile page
      * @event backend_order
      * @param array $order
      * @return array[string][string]string $return[%plugin_id%]['title_suffix'] html output
      * @return array[string][string]string $return[%plugin_id%]['action_button'] html output
      * @return array[string][string]string $return[%plugin_id%]['action_link'] html output
      * @return array[string][string]string $return[%plugin_id%]['info_section'] html output
      */
     $this->view->assign('backend_order', wa()->event('backend_order', $order, array('title_suffix', 'action_button', 'action_link', 'info_section')));
 }
예제 #9
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;
 }
예제 #10
0
 private function getParams(&$data, $id)
 {
     $model = new shopPluginModel();
     $shipping_address = array();
     if (!empty($data['contact'])) {
         $address = $data['contact']->getFirst('address.shipping');
         if (!$address) {
             $address = $data['contact']->getFirst('address');
         }
         if (!empty($address['data'])) {
             $shipping_address = $address['data'];
         }
     }
     $empty_address = false;
     // shipping
     if ($shipping_id = waRequest::post('shipping_id')) {
         $shipping_parts = explode('.', $shipping_id);
         $shipping_id = $shipping_parts[0];
         $rate_id = isset($shipping_parts[1]) ? $shipping_parts[1] : '';
         $data['params']['shipping_id'] = $shipping_id;
         $data['params']['shipping_rate_id'] = $rate_id;
         $plugin_info = $model->getById($shipping_id);
         $plugin = shopShipping::getPlugin($plugin_info['plugin'], $shipping_id);
         $rates = $plugin->getRates($this->getOrderItems($data['items'], $plugin->allowedWeightUnit()), $shipping_address);
         // save address
         if ($plugin->allowedAddress() === false) {
             $empty_address = true;
         }
         if (!$rates) {
             $this->errors['order']['common'] = _w('Unknown region for delivery');
             return;
         }
         if (!$rate_id) {
             $rate = reset($rates);
             $data['params']['shipping_rate_id'] = key($rates);
         } else {
             $rate = $rates[$rate_id];
         }
         $data['params']['shipping_plugin'] = $plugin->getId();
         $data['params']['shipping_name'] = $plugin_info['name'] . (!empty($rate['name']) ? ' (' . $rate['name'] . ')' : '');
         $data['params']['shipping_est_delivery'] = $rate['est_delivery'];
         if (waRequest::post('shipping' . $shipping_id)) {
             foreach (waRequest::post('shipping_' . $shipping_id) as $k => $v) {
                 $data['params']['shipping_params_' . $k] = $v;
             }
         }
     } else {
         foreach (array('id', 'rate_id', 'plugin', 'name', 'est_delivery') as $k) {
             $data['params']['shipping_' . $k] = null;
         }
     }
     // payment
     if ($payment_id = waRequest::post('payment_id')) {
         $data['params']['payment_id'] = $payment_id;
         $plugin_info = $model->getById($payment_id);
         $data['params']['payment_plugin'] = $plugin_info['plugin'];
         $data['params']['payment_name'] = $plugin_info['name'];
         if (waRequest::post('payment_' . $payment_id)) {
             foreach (waRequest::post('payment_' . $payment_id) as $k => $v) {
                 $data['params']['payment_params_' . $k] = $v;
             }
         }
     }
     // shipping and billing addreses
     if (!empty($data['contact'])) {
         // Make sure all old address data is removed
         if ($id) {
             $opm = new shopOrderParamsModel();
             foreach ($opm->get($id) as $k => $v) {
                 if (preg_match('~^(billing|shipping)_address\\.~', $k)) {
                     $data['params'][$k] = null;
                 }
             }
         }
         if (!$empty_address && $this->shipping_address) {
             foreach ($this->shipping_address as $k => $v) {
                 $data['params']['shipping_address.' . $k] = $v;
             }
         }
         if ($this->billing_address) {
             foreach ($this->billing_address as $k => $v) {
                 $data['params']['billing_address.' . $k] = $v;
             }
         }
     }
 }
예제 #11
0
 /**
  *
  * @param string $type
  * @param int $key
  * @return waSystemPlugin
  */
 private static function getPlugin($type, $key)
 {
     $plugin = null;
     if ($key) {
         switch ($type) {
             case 'payment':
                 $plugin = shopPayment::getPlugin(null, $key);
                 break;
             case 'shipping':
                 $plugin = shopShipping::getPlugin(null, $key);
                 break;
         }
     }
     return $plugin;
 }
 public function execute()
 {
     if ($shipping_id = waRequest::post('shipping_id')) {
         if ($data = waRequest::post('customer_' . $shipping_id)) {
             $settings = wa('shop')->getConfig()->getCheckoutSettings();
             if (!isset($settings['contactinfo']) || !isset($settings['contactinfo']['fields']['address.shipping']) && !isset($settings['contactinfo']['fields']['address'])) {
                 $settings = wa('shop')->getConfig()->getCheckoutSettings(true);
             }
             $plugin = shopShipping::getPlugin(null, $shipping_id);
             $form = $this->getAddressForm($shipping_id, $plugin, $settings, array(), true);
             if (!$form->isValid()) {
                 return false;
             }
             $contact = $this->getContact();
             if (!$contact) {
                 $contact = new waContact();
             }
             if ($data && is_array($data)) {
                 foreach ($data as $field => $value) {
                     if (is_array($value) && ($old = $contact->get($field))) {
                         if (isset($old[0]['data'])) {
                             foreach ($old[0]['data'] as $k => $v) {
                                 if (!isset($value[$k])) {
                                     $value[$k] = $v;
                                 }
                             }
                         }
                     }
                     $contact->set($field, $value);
                 }
                 if (wa()->getUser()->isAuth()) {
                     $contact->save();
                 } else {
                     $this->setSessionData('contact', $contact);
                 }
             }
         }
         $rates = waRequest::post('rate_id');
         $rate_id = isset($rates[$shipping_id]) ? $rates[$shipping_id] : null;
         $rate = $this->getRate($shipping_id, $rate_id);
         if (is_string($rate)) {
             $rate = false;
         }
         $this->setSessionData('shipping', array('id' => $shipping_id, 'rate_id' => $rate_id, 'name' => $rate ? $rate['name'] : '', 'plugin' => $rate ? $rate['plugin'] : ''));
         if (!$rate) {
             return false;
         }
         if ($comment = waRequest::post('comment')) {
             $this->setSessionData('comment', $comment);
         }
         if ($shipping_params = waRequest::post('shipping_' . $shipping_id)) {
             $params = $this->getSessionData('params', array());
             $params['shipping'] = $shipping_params;
             $this->setSessionData('params', $params);
         }
         return true;
     } else {
         return false;
     }
 }
예제 #13
0
 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);
     }
 }
 public function getRate($id = null, $rate_id = null, $contact = null)
 {
     if (!$id) {
         $shipping = $this->getSessionData('shipping');
         if (!$shipping) {
             return array();
         }
         $id = $shipping['id'];
         $rate_id = $shipping['rate_id'];
     }
     if (!$contact) {
         $contact = $this->getContact();
     }
     $plugin_model = new shopPluginModel();
     $plugin_info = $plugin_model->getById($id);
     $plugin = shopShipping::getPlugin($plugin_info['plugin'], $id);
     $cart = new shopCart();
     $total = $cart->total();
     $currency = $plugin->allowedCurrency();
     $currrent_currency = wa()->getConfig()->getCurrency(false);
     if ($currency != $currrent_currency) {
         $total = shop_currency($total, $currrent_currency, $currency, false);
     }
     $rates = $plugin->getRates($this->getItems($plugin->allowedWeightUnit()), $this->getAddress($contact), array('total_price' => $total));
     if (!$rates) {
         return false;
     }
     if (is_string($rates)) {
         return $rates;
     }
     if ($rate_id) {
         $result = $rates[$rate_id];
     } else {
         $result = array('rate' => 0);
     }
     if (is_array($result['rate'])) {
         $result['rate'] = max($result['rate']);
     }
     if ($currency != $currrent_currency) {
         $result['rate'] = shop_currency($result['rate'], $currency, $currrent_currency, false);
     }
     $result['plugin'] = $plugin->getId();
     $result['name'] = $plugin_info['name'] . (!empty($result['name']) ? ' (' . $result['name'] . ')' : '');
     return $result;
 }