public function execute()
 {
     $order_id = waRequest::request('order_id', 0, 'int');
     $id = waRequest::request('id', 0, 'int');
     $to = waRequest::request('to');
     $nm = new shopNotificationModel();
     $n = $nm->getById($id);
     if (!$n) {
         $this->errors = sprintf_wp('%s entry not found', _w('Notification'));
         return;
     }
     $om = new shopOrderModel();
     $o = $om->getById($order_id);
     if (!$o) {
         $this->errors = _w('Order not found');
         return;
     }
     shopHelper::workupOrders($o, true);
     $opm = new shopOrderParamsModel();
     $o['params'] = $opm->get($order_id);
     try {
         $contact = $o['contact_id'] ? new shopCustomer($o['contact_id']) : wa()->getUser();
         $contact->getName();
     } catch (Exception $e) {
         $contact = new shopCustomer(wa()->getUser()->getId());
     }
     $cm = new shopCustomerModel();
     $customer = $cm->getById($contact->getId());
     if (!$customer) {
         $customer = $cm->getEmptyRow();
     }
     $workflow = new shopWorkflow();
     // send notifications
     shopNotifications::sendOne($id, array('order' => $o, 'customer' => $contact, 'status' => $workflow->getStateById($o['state_id'])->getName()), $to);
 }
 public function execute()
 {
     if (!($order_id = waRequest::post('id', 0, 'int'))) {
         throw new waException('No order id given.');
     }
     if (!($action_id = waRequest::post('action_id'))) {
         throw new waException('No action id given.');
     }
     $workflow = new shopWorkflow();
     // @todo: check action availablity in state
     $action = $workflow->getActionById($action_id);
     if ($html = $action->getHTML($order_id)) {
         // display html
         echo $html;
     } else {
         // perform action and reload
         $result = $action->run($order_id);
         // counters
         $order_model = new shopOrderModel();
         $state_counters = $order_model->getStateCounters();
         $pending_counters = (!empty($state_counters['new']) ? $state_counters['new'] : 0) + (!empty($state_counters['processing']) ? $state_counters['processing'] : 0) + (!empty($state_counters['paid']) ? $state_counters['paid'] : 0);
         // update app coutner
         wa('shop')->getConfig()->setCount($state_counters['new']);
         echo "<script>";
         echo "\$.order_list.updateCounters(" . json_encode(array('state_counters' => $state_counters, 'common_counters' => array('pending_counters' => $pending_counters))) . ");";
         echo "\$.order.reload();</script>";
     }
 }
 public function execute()
 {
     $order_id = waRequest::post('order_id', null, waRequest::TYPE_INT);
     if ($order_id) {
         $order_model = new shopOrderModel();
         $order = $order_model->getOrder($order_id);
         $customer_model = new shopCustomerModel();
         $customer = $customer_model->getById($order['contact_id']);
         $customer_model->updateById($order['contact_id'], array('is_spamer' => 1));
         $plugin = waSystem::getInstance()->getPlugin('orderantispam');
         $action_id = $plugin->getSettings('action_id');
         $workflow = new shopWorkflow();
         $action = $workflow->getActionById($action_id);
         $action->run($order_id);
         // counters
         $state_counters = $order_model->getStateCounters();
         $pending_counters = (!empty($state_counters['new']) ? $state_counters['new'] : 0) + (!empty($state_counters['processing']) ? $state_counters['processing'] : 0) + (!empty($state_counters['paid']) ? $state_counters['paid'] : 0);
         // update app coutner
         wa('shop')->getConfig()->setCount($state_counters['new']);
         $script = "<script>";
         $script .= "\$.order_list.updateCounters(" . json_encode(array('state_counters' => $state_counters, 'common_counters' => array('pending_counters' => $pending_counters))) . ");";
         $script .= "\$.order.reload();</script>";
         $this->response['script'] = $script;
     }
 }
 public function execute()
 {
     $lazy = waRequest::get('lazy', 0, waRequest::TYPE_INT);
     $offset = waRequest::get('offset', 0, waRequest::TYPE_INT);
     $limit = 30;
     $model = new shopDiscountcardsPluginOrderModel();
     $sql = "SELECT count(*) " . $this->getSql();
     $total_count = (int) $model->query($sql)->fetchField();
     $sql = "SELECT * " . $this->getSql() . " LIMIT {$offset}, {$limit}";
     $discountcards_orders = $model->query($sql)->fetchAll();
     $discountcards_model = new shopDiscountcardsPluginModel();
     $order_model = new shopOrderModel();
     $workflow = new shopWorkflow();
     foreach ($discountcards_orders as &$discountcards_order) {
         $order = $order_model->getOrder($discountcards_order['order_id']);
         $discountcards_order['order'] = $order;
         $discountcards_order['order_id_encode'] = shopHelper::encodeOrderId($order['id']);
         if ($discountcard = $discountcards_model->getByField('discountcard', $discountcards_order['discountcard'])) {
             $discountcards_order['_discountcard'] = $discountcard;
         }
         $discountcards_order['state'] = $workflow->getStateById($order['state_id']);
     }
     unset($discountcards_order);
     $this->view->assign(array('discountcards_orders' => $discountcards_orders, 'offset' => $offset, 'limit' => $limit, 'count' => count($discountcards_orders), 'total_count' => $total_count, 'lazy' => $lazy));
 }
 public function execute()
 {
     $app_settings_model = new waAppSettingsModel();
     $settings = $app_settings_model->get(shopOrderantispamPlugin::$plugin_id);
     $workflow = new shopWorkflow();
     $actions = $workflow->getAvailableActions();
     $this->view->assign('actions', $actions);
     $this->view->assign('settings', $settings);
 }
 public function backendOrders()
 {
     $view = wa()->getView();
     $plugin_model = new shopPluginModel();
     $workflow = new shopWorkflow();
     $view->assign('states', $workflow->getAvailableStates());
     $view->assign('payments', $plugin_model->listPlugins(shopPluginModel::TYPE_PAYMENT));
     $view->assign('shippings', $plugin_model->listPlugins(shopPluginModel::TYPE_SHIPPING));
     return array('sidebar_section' => $view->fetch($this->path . '/templates/actions/backend/BackendOrders.html'));
 }
 public function execute()
 {
     $model_settings = new waAppSettingsModel();
     $settings = $model_settings->get($key = array('shop', 'orderstock'));
     $workflow = new shopWorkflow();
     $state_names = array();
     foreach ($workflow->getAvailableStates() as $state_id => $state) {
         $state_names[$state_id] = $state['name'];
     }
     $this->view->assign('state_names', $state_names);
     $this->view->assign('settings', $settings);
 }
 public function execute()
 {
     if (!($order_id = waRequest::post('id', 0, 'int'))) {
         throw new waException('No order id given.');
     }
     if (!($action_id = waRequest::post('action_id'))) {
         throw new waException('No action id given.');
     }
     $workflow = new shopWorkflow();
     // @todo: check action availablity in state
     $action = $workflow->getActionById($action_id);
     $this->response = $action->run($order_id);
 }
 public function execute()
 {
     $id = waRequest::post('id', null, waRequest::TYPE_STRING_TRIM);
     if (!$id) {
         $this->errors[] = _w("Unknown state");
         return;
     }
     $before_id = waRequest::post('before_id', null, waRequest::TYPE_STRING_TRIM);
     $config = shopWorkflow::getConfig();
     $item = $config['states'][$id];
     if (!isset($config['states'][$id])) {
         $this->errors[] = _w("Unknown state");
         return;
     }
     unset($config['states'][$id]);
     if (!$before_id) {
         $config['states'][$id] = $item;
     } else {
         if (!isset($config['states'][$before_id])) {
             $this->errors[] = _w("Unknown state");
             return;
         }
         $states = array();
         foreach ($config['states'] as $state_id => $state) {
             if ($state_id == $before_id) {
                 $states[$id] = $item;
             }
             $states[$state_id] = $state;
         }
         $config['states'] = $states;
     }
     if (!shopWorkflow::setConfig($config)) {
         $this->errors[] = _w("Error when save config");
     }
 }
 public function getEvents()
 {
     $workflow = new shopWorkflow();
     $actions = $workflow->getAllActions();
     $events = array();
     foreach ($actions as $action) {
         /**
          * @var shopWorkflowAction $action
          */
         $name = $action->getOption('log_record');
         if (!$name) {
             $name = $action->getName();
         }
         $events['order.' . $action->getId()] = array('name' => $name);
     }
     return $events;
 }
 public function execute()
 {
     $action_id = waRequest::get('id', null, waRequest::TYPE_STRING_TRIM);
     if (!$action_id) {
         throw new waException('No action id given.');
     }
     $chunk_size = 100;
     $offset = (int) waRequest::get('offset');
     $workflow = new shopWorkflow();
     $order_model = new shopOrderModel();
     // collect orders under which has performed actions
     $updated_orders_ids = array();
     $hash = $this->getHash();
     if ($hash === null) {
         return;
     }
     $collection = new shopOrdersCollection($hash);
     $total_count = $collection->count();
     $orders = $collection->getOrders('*', $offset, $chunk_size);
     foreach ($orders as $order) {
         $actions = $workflow->getStateById($order['state_id'])->getActions();
         if (isset($actions[$action_id])) {
             $actions[$action_id]->run($order['id']);
             $updated_orders_ids[] = $order['id'];
         }
     }
     if (!$updated_orders_ids) {
         return;
     }
     $this->response = array('offset' => $offset + count($orders), 'total_count' => $total_count);
     // sidebar counters
     if ($this->response['offset'] >= $this->response['total_count']) {
         $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);
         $this->response['state_counters'] = $state_counters;
         $this->response['pending_count'] = $pending_count;
     }
     $collection = new shopOrdersCollection('id/' . implode(',', $updated_orders_ids));
     $total_count = $collection->count();
     $orders = $collection->getOrders('*,contact', 0, $total_count);
     // orders for update items in table
     shopHelper::workupOrders($orders);
     $this->response['orders'] = array_values($orders);
 }
Ejemplo n.º 12
0
 public function execute()
 {
     $contact = wa()->getUser();
     $scm = new shopCustomerModel();
     // Customer orders
     $om = new shopOrderModel();
     $orders = $om->where('contact_id=?', $contact->getId())->order('id DESC')->fetchAll('id');
     // Items for all orders, one query
     $im = new shopOrderItemsModel();
     foreach ($im->getByField('order_id', array_keys($orders), true) as $row) {
         $orders[$row['order_id']]['items'][] = $row;
     }
     // Params for all orders, one query
     $opm = new shopOrderParamsModel();
     foreach ($opm->getByField('order_id', array_keys($orders), true) as $row) {
         $orders[$row['order_id']]['params'][$row['name']] = $row['value'];
     }
     // Prepare order data for template
     $url_tmpl = wa()->getRouteUrl('/frontend/myOrder', array('id' => '%ID%'));
     $workflow = new shopWorkflow();
     foreach ($orders as $k => &$o) {
         if ($o['state_id'] == 'deleted') {
             unset($orders[$k]);
             continue;
         }
         $o['id_str'] = shopHelper::encodeOrderId($o['id']);
         $o['total_formatted'] = waCurrency::format('%{s}', $o['total'], $o['currency']);
         $o['shipping_name'] = ifset($o['params']['shipping_name'], '');
         $o['payment_name'] = ifset($o['params']['payment_name'], '');
         $o['state'] = $workflow->getStateById($o['state_id']);
         $o['url'] = str_replace('%ID%', $o['id'], $url_tmpl);
     }
     $this->view->assign('orders', array_values($orders));
     $this->view->assign('my_nav_selected', 'orders');
     // Set up layout and template from theme
     $this->setThemeTemplate('my.orders.html');
     if (!waRequest::isXMLHttpRequest()) {
         $this->setLayout(new shopFrontendLayout());
         $this->getResponse()->setTitle(_w('Orders'));
         $this->view->assign('breadcrumbs', self::getBreadcrumbs());
         $this->layout->assign('nofollow', true);
     }
 }
 public function save($data)
 {
     $id = $data['state']['id'];
     unset($data['state']['id']);
     $this->config['states'] = !empty($this->config['states']) ? $this->config['states'] : array();
     $this->config['states'][$id] = $data['state'];
     $this->config['actions'] = !empty($this->config['actions']) ? $this->config['actions'] + $data['actions'] : $data['actions'];
     if (!shopWorkflow::setConfig($this->config)) {
         throw new waException(_w("Error when save config"));
     }
 }
 public function orderActionCreate($params)
 {
     if ($this->getSettings('status') && waRequest::cookie('uuid')) {
         $customer_model = new shopCustomerModel();
         $is_spamer = $customer_model->getByField(array('uuid' => waRequest::cookie('uuid'), 'is_spamer' => 1));
         $update = array();
         if ($is_spamer && $this->getSettings('action_id')) {
             $update['is_spamer'] = 1;
             $action_id = $this->getSettings('action_id');
             $workflow = new shopWorkflow();
             $action = $workflow->getActionById($action_id);
             $action->run($params['order_id']);
         }
         $customer = $customer_model->getById($params['contact_id']);
         if (!$customer['uuid']) {
             $update['uuid'] = waRequest::cookie('uuid');
             $customer_model->updateById($params['contact_id'], $update);
         }
     }
 }
 public function execute()
 {
     $workflow = new shopWorkflow();
     $modelNotifierConfig = new shopNotifierConfigModel();
     $modelNotifierTemplate = new shopNotifierTemplateModel();
     $state_names = array();
     foreach ($workflow->getAvailableStates() as $state_id => $state) {
         $state_names[$state_id] = $state['name'];
     }
     $all_notifications = $modelNotifierConfig->getAll();
     $templates = $modelNotifierTemplate->getAll();
     //        timestamp
     //        $time = strtotime('2015-04-01 18:11:44');
     //        $day = strtotime('+20 minute', $time);
     //        print_r(date('Y-m-d H:i:s', $day));
     $this->view->assign('cron', array('command' => 'php ' . wa()->getConfig()->getRootPath() . '/cli.php shop notifierCheck'));
     $this->view->assign('state_names', $state_names);
     $this->view->assign('templates', $templates);
     $this->view->assign('all_notifications', $all_notifications);
     //        $this->view->assign('settings', $settings);
 }
Ejemplo n.º 16
0
 public function execute()
 {
     $config = $this->getConfig();
     $default_view = $config->getOption('orders_default_view');
     $view = waRequest::get('view', $default_view, waRequest::TYPE_STRING_TRIM);
     $orders = $this->getOrders(0, $this->getCount());
     $action_ids = array_flip(array('process', 'pay', 'ship', 'complete', 'delete', 'restore'));
     $workflow = new shopWorkflow();
     $actions = array();
     foreach ($workflow->getAllActions() as $action) {
         if (isset($action_ids[$action->id])) {
             $actions[$action->id] = array('name' => $action->name, 'style' => $action->getOption('style'));
         }
     }
     $state_names = array();
     foreach ($workflow->getAvailableStates() as $state_id => $state) {
         $state_names[$state_id] = $state['name'];
     }
     $counters = array('state_counters' => array('new' => $this->model->getStateCounters('new')));
     $filter_params = $this->getFilterParams();
     if (isset($filter_params['state_id'])) {
         $filter_params['state_id'] = (array) $filter_params['state_id'];
         sort($filter_params['state_id']);
         if ($filter_params['state_id'] == array('new', 'paid', 'processing')) {
             $total = 0;
             foreach ($filter_params['state_id'] as $st) {
                 $total += (int) $this->model->getStateCounters($st);
             }
             $counters['common_counters'] = array('pending' => $total);
         } else {
             foreach ($filter_params['state_id'] as $st) {
                 $counters['state_counters'][$st] = (int) $this->model->getStateCounters($st);
             }
         }
     } else {
         $counters['common_counters'] = array('all' => $this->model->countAll());
     }
     $this->assign(array('orders' => array_values($orders), 'total_count' => $this->getTotalCount(), 'count' => count($orders), 'order' => $this->getOrder($orders), 'currency' => $this->getConfig()->getCurrency(), 'state_names' => $state_names, 'params' => $this->getFilterParams(), 'params_str' => $this->getFilterParams(true), 'view' => $view, 'timeout' => $config->getOption('orders_update_list'), 'actions' => $actions, 'counters' => $counters));
 }
 public function execute()
 {
     $id = waRequest::post('id');
     if (!$id) {
         $this->errors = _w("Unknown state");
         return;
     }
     $order_model = new shopOrderModel();
     if ($order_model->countByField('state_id', $id)) {
         $this->errors = _w("Cannot delete order status while there are active orders in this status");
         return;
     }
     $config = shopWorkflow::getConfig();
     if (isset($config['states'][$id])) {
         unset($config['states'][$id]);
     }
     shopWorkflow::setConfig($config);
 }
Ejemplo n.º 18
0
 public static function getConfig()
 {
     if (self::$config === null) {
         $file = wa()->getConfig()->getConfigPath('workflow.php', true, 'shop');
         if (!file_exists($file)) {
             $file = wa()->getConfig()->getAppsPath('shop', 'lib/config/data/workflow.php');
         }
         if (file_exists($file)) {
             self::$config = (include $file);
             foreach (self::$config['states'] as &$data) {
                 if (!isset($data['classname'])) {
                     $data['classname'] = 'shopWorkflowState';
                 }
             }
             unset($data);
         } else {
             self::$config = array();
         }
     }
     return self::$config;
 }
Ejemplo n.º 19
0
 protected function createOrder()
 {
     $checkout_data = $this->getStorage()->get('shop/checkout');
     $contact = $this->getUser()->isAuth() ? $this->getUser() : $checkout_data['contact'];
     $cart = new shopCart();
     $items = $cart->items(false);
     // remove id from item
     foreach ($items as &$item) {
         unset($item['id']);
         unset($item['parent_id']);
     }
     unset($item);
     $order = array('contact' => $contact, 'items' => $items, 'total' => $cart->total(false), 'params' => isset($checkout_data['params']) ? $checkout_data['params'] : array());
     $order['discount'] = shopDiscounts::apply($order);
     if (isset($checkout_data['shipping'])) {
         $order['params']['shipping_id'] = $checkout_data['shipping']['id'];
         $order['params']['shipping_rate_id'] = $checkout_data['shipping']['rate_id'];
         $shipping_step = new shopCheckoutShipping();
         $rate = $shipping_step->getRate($order['params']['shipping_id'], $order['params']['shipping_rate_id']);
         $order['params']['shipping_plugin'] = $rate['plugin'];
         $order['params']['shipping_name'] = $rate['name'];
         if (isset($rate['est_delivery'])) {
             $order['params']['shipping_est_delivery'] = $rate['est_delivery'];
         }
         if (!isset($order['shipping'])) {
             $order['shipping'] = $rate['rate'];
         }
         if (!empty($order['params']['shipping'])) {
             foreach ($order['params']['shipping'] as $k => $v) {
                 $order['params']['shipping_params_' . $k] = $v;
             }
             unset($order['params']['shipping']);
         }
     } else {
         $order['shipping'] = 0;
     }
     if (isset($checkout_data['payment'])) {
         $order['params']['payment_id'] = $checkout_data['payment'];
         $plugin_model = new shopPluginModel();
         $plugin_info = $plugin_model->getById($checkout_data['payment']);
         $order['params']['payment_name'] = $plugin_info['name'];
         $order['params']['payment_plugin'] = $plugin_info['plugin'];
         if (!empty($order['params']['payment'])) {
             foreach ($order['params']['payment'] as $k => $v) {
                 $order['params']['payment_params_' . $k] = $v;
             }
             unset($order['params']['payment']);
         }
     }
     if ($skock_id = waRequest::post('stock_id')) {
         $order['params']['stock_id'] = $skock_id;
     }
     $routing_url = wa()->getRouting()->getRootUrl();
     $order['params']['storefront'] = wa()->getConfig()->getDomain() . ($routing_url ? '/' . $routing_url : '');
     if (($ref = wa()->getStorage()->get('shop/referer')) || ($ref = waRequest::cookie('referer'))) {
         $order['params']['referer'] = $ref;
         $ref_parts = parse_url($ref);
         $order['params']['referer_host'] = $ref_parts['host'];
         // try get search keywords
         if (!empty($ref_parts['query'])) {
             $search_engines = array('text' => 'yandex\\.|rambler\\.', 'q' => 'bing\\.com|mail\\.|google\\.', 's' => 'nigma\\.ru', 'p' => 'yahoo\\.com');
             $q_var = false;
             foreach ($search_engines as $q => $pattern) {
                 if (preg_match('/(' . $pattern . ')/si', $ref_parts['host'])) {
                     $q_var = $q;
                     break;
                 }
             }
             // default query var name
             if (!$q_var) {
                 $q_var = 'q';
             }
             parse_str($ref_parts['query'], $query);
             if (!empty($query[$q_var])) {
                 $order['params']['keyword'] = $query[$q_var];
             }
         }
     }
     if ($utm = waRequest::cookie('utm')) {
         $utm = json_decode($utm, true);
         if ($utm && is_array($utm)) {
             foreach ($utm as $k => $v) {
                 $order['params']['utm_' . $k] = $v;
             }
         }
     }
     $order['params']['ip'] = waRequest::getIp();
     $order['params']['user_agent'] = waRequest::getUserAgent();
     foreach (array('shipping', 'billing') as $ext) {
         $address = $contact->getFirst('address.' . $ext);
         if ($address) {
             foreach ($address['data'] as $k => $v) {
                 $order['params'][$ext . '_address.' . $k] = $v;
             }
         }
     }
     if (isset($checkout_data['comment'])) {
         $order['comment'] = $checkout_data['comment'];
     }
     $workflow = new shopWorkflow();
     if ($order_id = $workflow->getActionById('create')->run($order)) {
         $step_number = shopCheckout::getStepNumber();
         $checkout_flow = new shopCheckoutFlowModel();
         $checkout_flow->add(array('step' => $step_number));
         $cart->clear();
         wa()->getStorage()->remove('shop/checkout');
         wa()->getStorage()->set('shop/order_id', $order_id);
         return true;
     }
 }
Ejemplo n.º 20
0
 public function getStates()
 {
     $workflow = new shopWorkflow();
     return $workflow->getAllStates();
 }
Ejemplo n.º 21
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);
     }
 }
Ejemplo n.º 22
0
 /**
  * @param array $transaction_data
  * @return array
  */
 public function callbackConfirmationHandler($transaction_data)
 {
     $result = $this->workflowAction('callback', $transaction_data);
     if (empty($result['error'])) {
         $order_model = new shopOrderModel();
         $order = $order_model->getById($transaction_data['order_id']);
         $result['result'] = true;
         $total = $transaction_data['amount'];
         if ($transaction_data['currency_id'] != $order['currency']) {
             $total = shop_currency($total, $transaction_data['currency_id'], $order['currency'], false);
         }
         if (abs($order['total'] - $total) > 0.01) {
             $result['result'] = false;
             $result['error'] = sprintf('Invalid order amount: expect %f, but get %f', $order['total'], $total);
         } else {
             $workflow = new shopWorkflow();
             $workflow->getActionById('process')->run($transaction_data['order_id']);
         }
     }
     return $result;
 }
 protected function createOrder()
 {
     $checkout_data = wa()->getStorage()->get('shop/checkout');
     if (wa()->getUser()->isAuth()) {
         $contact = wa()->getUser();
     } else {
         if (!empty($checkout_data['contact']) && $checkout_data['contact'] instanceof waContact) {
             $contact = $checkout_data['contact'];
         } else {
             $contact = new waContact();
         }
     }
     $cart = new shopCart();
     $items = $cart->items(false);
     // remove id from item
     foreach ($items as &$item) {
         unset($item['id']);
         unset($item['parent_id']);
     }
     unset($item);
     $order = array('contact' => $contact, 'items' => $items, 'total' => $cart->total(false), 'params' => isset($checkout_data['params']) ? $checkout_data['params'] : array());
     $order['discount_description'] = null;
     $order['discount'] = shopDiscounts::apply($order, $order['discount_description']);
     if (isset($checkout_data['shipping'])) {
         $order['params']['shipping_id'] = $checkout_data['shipping']['id'];
         $order['params']['shipping_rate_id'] = $checkout_data['shipping']['rate_id'];
         $shipping_step = new shopOnestepCheckoutShipping();
         $rate = $shipping_step->getRate($order['params']['shipping_id'], $order['params']['shipping_rate_id']);
         $order['params']['shipping_plugin'] = $rate['plugin'];
         $order['params']['shipping_name'] = $rate['name'];
         if (isset($rate['est_delivery'])) {
             $order['params']['shipping_est_delivery'] = $rate['est_delivery'];
         }
         if (!isset($order['shipping'])) {
             $order['shipping'] = $rate['rate'];
         }
         if (!empty($order['params']['shipping'])) {
             foreach ($order['params']['shipping'] as $k => $v) {
                 $order['params']['shipping_params_' . $k] = $v;
             }
             unset($order['params']['shipping']);
         }
     } else {
         $order['shipping'] = 0;
     }
     if (isset($checkout_data['payment'])) {
         $order['params']['payment_id'] = $checkout_data['payment'];
         $plugin_model = new shopPluginModel();
         $plugin_info = $plugin_model->getById($checkout_data['payment']);
         $order['params']['payment_name'] = $plugin_info['name'];
         $order['params']['payment_plugin'] = $plugin_info['plugin'];
         if (!empty($order['params']['payment'])) {
             foreach ($order['params']['payment'] as $k => $v) {
                 $order['params']['payment_params_' . $k] = $v;
             }
             unset($order['params']['payment']);
         }
     }
     if ($skock_id = waRequest::post('stock_id')) {
         $order['params']['stock_id'] = $skock_id;
     }
     $routing_url = wa()->getRouting()->getRootUrl();
     $order['params']['storefront'] = wa()->getConfig()->getDomain() . ($routing_url ? '/' . $routing_url : '');
     if ($ref = waRequest::cookie('referer')) {
         $order['params']['referer'] = $ref;
         $ref_parts = @parse_url($ref);
         $order['params']['referer_host'] = $ref_parts['host'];
         // try get search keywords
         if (!empty($ref_parts['query'])) {
             $search_engines = array('text' => 'yandex\\.|rambler\\.', 'q' => 'bing\\.com|mail\\.|google\\.', 's' => 'nigma\\.ru', 'p' => 'yahoo\\.com');
             $q_var = false;
             foreach ($search_engines as $q => $pattern) {
                 if (preg_match('/(' . $pattern . ')/si', $ref_parts['host'])) {
                     $q_var = $q;
                     break;
                 }
             }
             // default query var name
             if (!$q_var) {
                 $q_var = 'q';
             }
             parse_str($ref_parts['query'], $query);
             if (!empty($query[$q_var])) {
                 $order['params']['keyword'] = $query[$q_var];
             }
         }
     }
     if ($utm = waRequest::cookie('utm')) {
         $utm = json_decode($utm, true);
         if ($utm && is_array($utm)) {
             foreach ($utm as $k => $v) {
                 $order['params']['utm_' . $k] = $v;
             }
         }
     }
     if (($landing = waRequest::cookie('landing')) && ($landing = @parse_url($landing))) {
         if (!empty($landing['query'])) {
             @parse_str($landing['query'], $arr);
             if (!empty($arr['gclid']) && !empty($order['params']['referer_host']) && strpos($order['params']['referer_host'], 'google') !== false) {
                 $order['params']['referer_host'] .= ' (cpc)';
                 $order['params']['cpc'] = 1;
             } else {
                 if (!empty($arr['_openstat']) && !empty($order['params']['referer_host']) && strpos($order['params']['referer_host'], 'yandex') !== false) {
                     $order['params']['referer_host'] .= ' (cpc)';
                     $order['params']['openstat'] = $arr['_openstat'];
                     $order['params']['cpc'] = 1;
                 }
             }
         }
         $order['params']['landing'] = $landing['path'];
     }
     // A/B tests
     /*
             $abtest_variants_model = new shopAbtestVariantsModel();
             foreach (waRequest::cookie() as $k => $v) {
                 if (substr($k, 0, 5) == 'waabt') {
                     $variant_id = $v;
                     $abtest_id = substr($k, 5);
                     if (wa_is_int($abtest_id) && wa_is_int($variant_id)) {
                         $row = $abtest_variants_model->getById($variant_id);
                         if ($row && $row['abtest_id'] == $abtest_id) {
                             $order['params']['abt' . $abtest_id] = $variant_id;
                         }
                     }
                 }
             }
     */
     $order['params']['ip'] = waRequest::getIp();
     $order['params']['user_agent'] = waRequest::getUserAgent();
     foreach (array('shipping', 'billing') as $ext) {
         $address = $contact->getFirst('address.' . $ext);
         if ($address) {
             foreach ($address['data'] as $k => $v) {
                 $order['params'][$ext . '_address.' . $k] = $v;
             }
         }
     }
     if (isset($checkout_data['comment'])) {
         $order['comment'] = $checkout_data['comment'];
     }
     $workflow = new shopWorkflow();
     if ($order_id = $workflow->getActionById('create')->run($order)) {
         $step_number = shopOnestepCheckout::getStepNumber();
         $checkout_flow = new shopCheckoutFlowModel();
         $checkout_flow->add(array('step' => $step_number));
         $cart->clear();
         wa()->getStorage()->remove('shop/checkout');
         wa()->getStorage()->set('shop/order_id', $order_id);
         return $order_id;
     } else {
         return false;
     }
 }
Ejemplo n.º 24
0
 private function _getOrder($id)
 {
     $order = $this->getModel()->getOrder($id);
     if (!$order) {
         return false;
     }
     $workflow = new shopWorkflow();
     $order['state'] = $workflow->getStateById($order['state_id']);
     $order = shopHelper::workupOrders($order, true);
     $sku_ids = array();
     $stock_ids = array();
     foreach ($order['items'] as $item) {
         if ($item['stock_id']) {
             $stock_ids[] = $item['stock_id'];
         }
         if ($item['sku_id']) {
             $sku_ids[] = $item['sku_id'];
         }
     }
     $sku_ids = array_unique($sku_ids);
     $stock_ids = array_unique($stock_ids);
     // extend items by stocks
     $stocks = $this->getStocks($stock_ids);
     foreach ($order['items'] as &$item) {
         if (!empty($stocks[$item['stock_id']])) {
             $item['stock'] = $stocks[$item['stock_id']];
         }
     }
     unset($item);
     $skus = $this->getSkus($sku_ids);
     $sku_stocks = $this->getSkuStocks($sku_ids);
     foreach ($order['items'] as &$item) {
         // product and existing sku
         if (isset($skus[$item['sku_id']])) {
             $s = $skus[$item['sku_id']];
             // for that counts that lower than low_count-thresholds show icon
             if ($s['count'] !== null) {
                 if (isset($item['stock'])) {
                     if (isset($sku_stocks[$s['id']][$item['stock']['id']])) {
                         $count = $sku_stocks[$s['id']][$item['stock']['id']]['count'];
                         if ($count <= $item['stock']['low_count']) {
                             $item['stock_icon'] = shopHelper::getStockCountIcon($count, $item['stock']['id'], true);
                         }
                     }
                 } else {
                     if ($s['count'] <= shopStockModel::LOW_DEFAULT) {
                         $item['stock_icon'] = shopHelper::getStockCountIcon($s['count'], null, true);
                     }
                 }
             }
         }
     }
     unset($item);
     return $order;
 }
Ejemplo n.º 25
0
 /**
  * Adds various extra data to specified orders.
  *
  * @param array $orders Orders array
  * @param bool $single Whether only one order is specified; only in this case modified order data array is returned
  * @return null|array
  */
 public static function workupOrders(&$orders, $single = false)
 {
     if ($single) {
         $orders = array($orders);
     }
     $workflow = new shopWorkflow();
     $states = $workflow->getAllStates();
     foreach ($orders as &$order) {
         $order['id_str'] = self::encodeOrderId($order['id']);
         $order['total_str'] = wa_currency($order['total'], $order['currency']);
         if (!empty($order['create_datetime'])) {
             $order['create_datetime_str'] = wa_date('humandatetime', $order['create_datetime']);
         }
         $state = isset($states[$order['state_id']]) ? $states[$order['state_id']] : null;
         $icon = '';
         $style = '';
         if ($state) {
             /**
              * @var shopWorkflowState $state
              */
             $icon = $state->getOption('icon');
             $style = $state->getStyle();
         }
         $order['icon'] = $icon;
         $order['style'] = $style;
         if (isset($order['params'])) {
             // shipping_address_formatted
             $shipping_address = self::getOrderAddress($order['params'], 'shipping');
             $formatter = new waContactAddressOneLineFormatter();
             $order['shipping_address_formatted'] = $formatter->format(array('data' => $shipping_address));
             $order['shipping_address_formatted'] = $order['shipping_address_formatted']['value'];
             // Shipping and payment option names
             if (isset($order['params']['shipping_name'])) {
                 $order['shipping_name'] = htmlspecialchars($order['params']['shipping_name']);
             } else {
                 $order['shipping_name'] = '<span class="hint">' . _w('not specified') . '</span>';
             }
             if (isset($order['params']['payment_name'])) {
                 $order['payment_name'] = htmlspecialchars($order['params']['payment_name']);
             } else {
                 $order['payment_name'] = '<span class="hint">' . _w('not specified') . '</span>';
             }
         }
     }
     if ($single) {
         $orders = $orders[0];
         return $orders;
     }
 }
Ejemplo n.º 26
0
 public function execute()
 {
     $id = waRequest::request('id', 0, 'int');
     if (!$id || !wa()->getUser()->getRights('shop', 'orders')) {
         $this->redirect(wa()->getAppUrl());
     }
     // Order
     $om = new shopOrderModel();
     $order = $om->getOrder($id);
     shopHelper::workupOrders($order, true);
     $order['tax'] = (double) $order['tax'];
     $order['discount'] = (double) $order['discount'];
     // Order params
     $opm = new shopOrderParamsModel();
     $order['params'] = $opm->get($order['id']);
     // Order subtotal
     $order_subtotal = 0;
     foreach ($order['items'] as $i) {
         $order_subtotal += $i['price'] * $i['quantity'];
     }
     // Format addresses
     $settings = wa('shop')->getConfig()->getCheckoutSettings();
     $form_fields = ifset($settings['contactinfo']['fields'], array());
     $formatter = new waContactAddressSeveralLinesFormatter();
     $shipping_address = shopHelper::getOrderAddress($order['params'], 'shipping');
     $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'];
         if ($billing_address === $shipping_address) {
             $billing_address = null;
         }
     } else {
         $billing_address = null;
     }
     // Order history
     $log_model = new shopOrderLogModel();
     $log = $log_model->getLog($order['id']);
     // Customer
     $contact = $customer = self::getCustomer($order);
     $top = array();
     foreach (array('email', 'phone') as $f) {
         if ($v = $contact->get($f, 'top,html')) {
             $top[] = array('id' => $f, 'name' => waContactFields::get($f)->getName(), 'value' => is_array($v) ? implode(', ', $v) : $v);
         }
     }
     // Workflow stuff: actions and state
     $workflow = new shopWorkflow();
     $workflow_state = $workflow->getStateById($order['state_id']);
     $workflow_buttons = array();
     foreach ($workflow_state->getActions() as $a_id => $action) {
         if ($a_id === 'edit' || $a_id === 'delete') {
             continue;
         }
         $workflow_buttons[] = $action->getButton();
     }
     $this->view->assign('top', $top);
     $this->view->assign('log', $log);
     $this->view->assign('order', $order);
     $this->view->assign('uniqid', uniqid('f'));
     $this->view->assign('customer', $customer);
     $this->view->assign('workflow_state', $workflow_state);
     $this->view->assign('workflow_buttons', $workflow_buttons);
     $this->view->assign('shipping_address', $shipping_address);
     $this->view->assign('billing_address', $billing_address);
     $this->view->assign('order_subtotal', $order_subtotal);
     $this->view->assign('currency', ifempty($order['currency'], wa()->getConfig()->getCurrency()));
     wa()->getResponse()->setTitle(_w('Order') . ' ' . $order['id_str']);
     parent::execute();
 }
Ejemplo n.º 27
0
 /**
  * @param int|null $state_id If null return counters for each state in assoc array
  * @return int|array
  */
 public function getStateCounters($state_id = null)
 {
     $where = "";
     if ($state_id !== null) {
         $where = "WHERE " . $this->getWhereByField('state_id', $state_id);
     }
     $sql = "\n            SELECT state_id, COUNT(state_id) cnt FROM `{$this->table}`\n            {$where} GROUP BY state_id";
     $r = $this->query($sql);
     if ($state_id !== null) {
         $cnt = $r->fetchField('cnt');
         return $cnt ? $cnt : 0;
     }
     $counters = array();
     $workflow = new shopWorkflow();
     foreach (array_keys($workflow->getAvailableStates()) as $state_id) {
         $counters[$state_id] = 0;
     }
     $counters = $r->fetchAll('state_id', true) + $counters;
     return $counters ? $counters : array();
 }
Ejemplo n.º 28
0
 /**
  * Helper to send one message: used during real sending, as well as for test emails from follow-ups settings page.
  */
 public static function sendOne($f, $o, $customer, $contact, $to, $view = null, $general = null)
 {
     if (!$view) {
         $view = wa()->getView();
     }
     if (!$general) {
         $general = wa('shop')->getConfig()->getGeneralSettings();
     }
     $workflow = new shopWorkflow();
     $items_model = new shopOrderItemsModel();
     $o['items'] = $items_model->getItems($o['id']);
     foreach ($o['items'] as &$i) {
         if (!empty($i['file_name'])) {
             $i['download_link'] = wa()->getRouteUrl('/frontend/myOrderDownload', array('id' => $o['id'], 'code' => $o['params']['auth_code'], 'item' => $i['id']), true);
         }
     }
     unset($i);
     // Assign template vars
     $view->clearAllAssign();
     $view->assign('followup', $f);
     // row from shop_followup
     $view->assign('order', $o);
     // row from shop_order, + 'params' key
     $view->assign('customer', $contact);
     // shopCustomer
     $view->assign('order_url', wa()->getRouteUrl('/frontend/myOrderByCode', array('id' => $o['id'], 'code' => $o['params']['auth_code']), true));
     $view->assign('status', $workflow->getStateById($o['state_id'])->getName());
     // $shipping_address, $billing_address
     foreach (array('shipping', 'billing') as $k) {
         $address = shopHelper::getOrderAddress($o['params'], $k);
         $formatter = new waContactAddressOneLineFormatter(array('image' => false));
         $address = $formatter->format(array('data' => $address));
         $view->assign($k . '_address', $address['value']);
     }
     // Build email from template
     $subject = $view->fetch('string:' . $f['subject']);
     $body = $view->fetch('string:' . $f['body']);
     $from = $general['email'];
     if ($f['from']) {
         $from = $f['from'];
     }
     // Send the message
     $message = new waMailMessage($subject, $body);
     $message->setTo($to);
     $message->setFrom($from, $general['name']);
     return $message->send();
 }
Ejemplo n.º 29
0
 public function execute()
 {
     $id = waRequest::get('id', null, waRequest::TYPE_INT);
     // null             - don't add/edit contact info
     // not zero numeric - edit existing contact
     // zero numeric     - add contact
     $customer_id = waRequest::post('customer_id', null, waRequest::TYPE_INT);
     if ($customer_id && !shopHelper::getContactRights($customer_id)) {
         $customer_id = null;
     }
     if ($customer_id === null && !$id) {
         $customer_id = 0;
     }
     if ($customer_id !== null) {
         $contact = new waContact($customer_id);
         $form = shopHelper::getCustomerForm($customer_id);
         $customer_validation_disabled = wa()->getSetting('disable_backend_customer_form_validation');
         if (!$customer_validation_disabled) {
             if (!$form->isValid($contact)) {
                 $this->errors['customer']['html'] = $form->html();
             }
         }
     }
     if ($data = $this->getData($id)) {
         $this->validate($data, $id);
     }
     if ($this->errors) {
         return;
     }
     $params_model = new shopOrderParamsModel();
     $params = $params_model->get($id);
     if ($customer_id !== null) {
         foreach ((array) $form->post() as $fld_id => $fld_data) {
             if (!$fld_data) {
                 continue;
             }
             if ($fld_id == 'address.shipping') {
                 $this->shipping_address = $fld_data;
                 $this->setAddress($contact, $params, 'shipping');
                 continue;
             } elseif ($fld_id == 'address.billing') {
                 $this->billing_address = $fld_data;
                 $this->setAddress($contact, $params, 'billing');
                 continue;
             }
             if (is_array($fld_data) && !empty($fld_data[0])) {
                 $contact[$fld_id] = array();
                 foreach ($fld_data as $v) {
                     $contact->set($fld_id, $v, true);
                 }
             } else {
                 $contact[$fld_id] = $fld_data;
             }
         }
         if ($customer_validation_disabled) {
             $contact->save();
         } else {
             $errors = $contact->save(array(), true);
             if ($errors) {
                 // Only consider errors from visible fields
                 $errors = array_intersect_key($errors, $form->fields);
                 if ($errors) {
                     $this->errors['customer'] = $errors;
                     return;
                 } else {
                     // No errors from visible fields: save anyway
                     $contact->save();
                 }
             }
         }
         $data['contact'] = $contact;
     }
     $workflow = new shopWorkflow();
     $this->getParams($data, $id);
     if (!$id) {
         $id = $workflow->getActionById('create')->run($data);
     } else {
         $data['id'] = $id;
         $workflow->getActionById('edit')->run($data);
     }
     $this->response['order'] = $this->workupOrder($this->getModel()->getOrder($id));
 }