protected function isAuth($order, &$code)
 {
     $result = false;
     if (!$result) {
         $result = $order['id'] == wa()->getStorage()->get('shop/order_id');
     }
     if (!$result) {
         // Check that order exists and belongs to this user
         $result = $order['contact_id'] == wa()->getUser()->getId() && $order['state_id'] != 'deleted';
     }
     if (!$result && $code) {
         // Check auth code
         $opm = new shopOrderParamsModel();
         $params = $opm->get($order['id']);
         if (!empty($params['auth_pin']) && ifset($params['auth_code']) === $code) {
             $pin = wa()->getStorage()->get('shop/pin/' . $order['id']);
             if ($pin && $pin == $params['auth_pin']) {
                 $result = true;
             }
         } else {
             $code = false;
         }
     }
     return $result;
 }
 public function ordersCollection($params)
 {
     /**
      * @var shopOrdersCollection $collection
      */
     $collection = $params['collection'];
     $hash = $collection->getType();
     $filters = self::parseHash(urldecode($hash));
     $model = new shopOrderModel();
     foreach ($filters as $k => $v) {
         $key = $model->escape($k);
         $value = $model->escape($v);
         if (empty($value)) {
             continue;
         }
         if (substr($key, 0, 15) == 'update_datetime') {
             $operators = array("_from" => ">=", "_to" => "<=");
             if (array_key_exists(substr($key, 15), $operators)) {
                 $operator = $operators[substr($key, 15)];
                 $collection->addWhere("o.update_datetime" . $operator . "'" . date('Y-m-d', strtotime($value)) . "'");
             }
         } elseif (substr($key, 0, 7) == 'params.') {
             $model_params = new shopOrderParamsModel();
             $params_table_name = $model_params->getTableName();
             $collection->addJoin($params_table_name, "o.id=:table.order_id AND :table.name='" . substr($key, 7) . "'", ":table.value" . $this->getWhere($value));
         } elseif ($model->fieldExists($key)) {
             $title[] = $key . $this->getWhere($value);
             $collection->addWhere("o." . $key . $this->getWhere($value));
         }
     }
     return true;
 }
 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);
 }
Пример #4
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);
         }
     }
 }
 public function execute()
 {
     $code = waRequest::param('code');
     $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);
     }
     if (!$order_id || $order_id != substr($code, 16, -16)) {
         throw new waException(_w('Order not found'), 404);
     }
     // When user is authorized, check if order belongs to him.
     // When it does, redirect to plain order page.
     if (wa()->getUser()->isAuth()) {
         $om = new shopOrderModel();
         $order = $om->getOrder($order_id);
         if (!$order) {
             throw new waException(_w('Order not found'), 404);
         }
         if ($order['contact_id'] == wa()->getUser()->getId()) {
             $this->redirect(wa()->getRouteUrl('/frontend/myOrder', array('id' => $order_id)));
         }
     }
     // Check auth code
     $opm = new shopOrderParamsModel();
     $params = $opm->get($order_id);
     if (ifset($params['auth_code']) !== $code || empty($params['auth_pin'])) {
         throw new waException(_w('Order not found'), 404);
     }
     // Check auth pin and show order page if pin is correct
     $pin = waRequest::request('pin', wa()->getStorage()->get('shop/pin/' . $order_id));
     if ($pin && $pin == $params['auth_pin']) {
         wa()->getStorage()->set('shop/pin/' . $order_id, $pin);
         parent::execute();
         if (!waRequest::isXMLHttpRequest()) {
             $this->layout->assign('breadcrumbs', self::getBreadcrumbs());
         }
         return;
     }
     //
     // No pin or pin is incorrect: show form to enter pin
     //
     $this->view->assign('wrong_pin', !!$pin);
     $this->view->assign('pin_required', true);
     $this->view->assign('encoded_order_id', $encoded_order_id);
     $this->view->assign('my_nav_selected', 'orders');
     // Set up layout and template from theme
     $this->setThemeTemplate('my.order.html');
     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);
     }
 }
Пример #6
0
 public function delete($id)
 {
     $coupon = $this->getById($id);
     if ($coupon) {
         $opm = new shopOrderParamsModel();
         $order_ids = array_keys($opm->getByField(array('name' => 'coupon_id', 'value' => $id), 'order_id'));
         if ($order_ids) {
             $opm->set($order_ids, array('coupon_code' => $coupon['code']), false);
         }
         $this->deleteById($id);
     }
 }
Пример #7
0
 /**
  * @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()
 {
     $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 execute()
 {
     $order_id = waRequest::request('order_id', 0, 'int');
     $followup_id = waRequest::request('followup_id', 0, 'int');
     $email = waRequest::request('email');
     $fm = new shopFollowupModel();
     $f = $fm->getById($followup_id);
     if (!$f) {
         $this->errors = sprintf_wp('%s entry not found', _w('Follow-up'));
         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();
     }
     $to = array($email => $contact->getName());
     if (!shopFollowupCli::sendOne($f, $o, $customer, $contact, $to)) {
         $this->errors = "Unable to send follow-up #{$f['id']} for order #{$o['id']}: waMessage->send() returned FALSE.";
         return;
     }
     $this->response = 'ok';
 }
 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);
     }
     $om = new shopOrderModel();
     $order = $om->getOrder($order_id);
     if (!$order) {
         throw new waException(_w('Order not found'), 404);
     }
     if (!$this->isAuth($order)) {
         throw new waException(_w('The file will be available for download after the order is paid and processed.'), 404);
     }
     // Check auth code
     $opm = new shopOrderParamsModel();
     $params = $opm->get($order_id);
     $code = waRequest::param('code');
     if (ifset($params['auth_code']) !== $code) {
         throw new waException(_w('Order not found'), 404);
     }
     if ($item = ifempty($order['items'][waRequest::param('item')])) {
         $skus_model = new shopProductSkusModel();
         $sku = $skus_model->getById(ifempty($item['sku_id']));
         if ($sku['file_name'] && $sku['file_size']) {
             $file_path = shopProductSkusModel::getPath($sku);
             waFiles::readFile($file_path, $sku['file_name']);
         } else {
             throw new waException(_w('File not found'), 404);
         }
     } else {
         throw new waException(_w('Order item not found'), 404);
     }
 }
Пример #11
0
 public function postExecute($order_id = null, $result = null)
 {
     if (!$result) {
         return;
     }
     $order_model = new shopOrderModel();
     if (is_array($order_id)) {
         $order = $order_id;
         $order_id = $order['id'];
     } else {
         $order = $order_model->getById($order_id);
     }
     $data = is_array($result) ? $result : array();
     $data['order_id'] = $order_id;
     $data['action_id'] = $this->getId();
     $data['before_state_id'] = $order['state_id'];
     if ($this->state_id) {
         $data['after_state_id'] = $this->state_id;
     } else {
         $data['after_state_id'] = $order['state_id'];
     }
     $order_log_model = new shopOrderLogModel();
     $data['id'] = $order_log_model->add($data);
     $update = isset($result['update']) ? $result['update'] : array();
     $update['update_datetime'] = date('Y-m-d H:i:s');
     $data['update'] = $update;
     if ($this->state_id) {
         $update['state_id'] = $this->state_id;
     }
     $order_model->updateById($order['id'], $update);
     $order_params_model = new shopOrderParamsModel();
     if (isset($update['params'])) {
         $order_params_model->set($order['id'], $update['params'], false);
     }
     $order['params'] = $order_params_model->get($order_id);
     // send notifications
     shopNotifications::send('order.' . $this->getId(), array('order' => $order, 'customer' => new waContact($order['contact_id']), 'status' => $this->getWorkflow()->getStateById($data['after_state_id'])->getName(), 'action_data' => $data));
     /**
      * @event order_action.callback
      * @event order_action.pay
      * @event order_action.ship
      * @event order_action.process
      * @event order_action.delete
      * @event order_action.restore
      * @event order_action.complete
      * @event order_action.comment
      *
      * @param array[string]mixed $data
      * @param array[string]int $data['order_id']
      * @param array[string]int $data['action_id']
      * @param array[string]int $data['before_state_id']
      * @param array[string]int $data['after_state_id']
      * @param array[string]int $data['id'] Order log record id
      */
     wa('shop')->event('order_action.' . $this->getId(), $data);
     return $data;
 }
 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 postExecute($order_id = null, $result = null)
 {
     $order_id = $result['order_id'];
     $data = is_array($result) ? $result : array();
     $data['order_id'] = $order_id;
     $data['action_id'] = $this->getId();
     $data['before_state_id'] = '';
     $data['after_state_id'] = 'new';
     $order_log_model = new shopOrderLogModel();
     $order_log_model->add($data);
     /**
      * @event order_action.create
      */
     wa('shop')->event('order_action.create', $data);
     $order_model = new shopOrderModel();
     $order = $order_model->getById($order_id);
     $params_model = new shopOrderParamsModel();
     $order['params'] = $params_model->get($order_id);
     // send notifications
     shopNotifications::send('order.' . $this->getId(), array('order' => $order, 'customer' => new waContact($order['contact_id']), 'status' => $this->getWorkflow()->getStateById($data['after_state_id'])->getName(), 'action_data' => $data));
     // Update stock count, but take into account 'update_stock_count_on_create_order'-setting
     $app_settings_model = new waAppSettingsModel();
     if ($app_settings_model->get('shop', 'update_stock_count_on_create_order')) {
         // for logging changes in stocks
         shopProductStocksLogModel::setContext(shopProductStocksLogModel::TYPE_ORDER, 'Order %s was placed', array('order_id' => $order_id));
         $order_model->reduceProductsFromStocks($order_id);
         shopProductStocksLogModel::clearContext();
     }
     return $order_id;
 }
 public function execute($data = null)
 {
     $order_model = new shopOrderModel();
     $order = $order_model->getById($data['id']);
     $subtotal = 0;
     $services = $products = array();
     foreach ($data['items'] as $item) {
         if ($item['service_id']) {
             $services[] = $item['service_id'];
         } else {
             $products[] = $item['product_id'];
         }
     }
     $service_model = new shopServiceModel();
     $product_model = new shopProductModel();
     $services = $service_model->getById($services);
     $products = $product_model->getById($products);
     foreach ($data['items'] as &$item) {
         $item['currency'] = $order['currency'];
         $item['price'] = $this->price($item['price']);
         if ($item['service_id']) {
             $item['service'] = $services[$item['service_id']];
         } else {
             $item['product'] = $products[$item['product_id']];
         }
         $subtotal += $item['price'] * $item['quantity'];
     }
     unset($item);
     foreach (array('shipping', 'discount') as $k) {
         if (!isset($data[$k])) {
             $data[$k] = 0;
         }
     }
     $contact = new waContact($order['contact_id']);
     $shipping_address = $contact->getFirst('address.shipping');
     if (!$shipping_address) {
         $shipping_address = $contact->getFirst('address');
     }
     $shipping_address = $shipping_address ? $shipping_address['data'] : array();
     $billing_address = $contact->getFirst('address.billing');
     if (!$billing_address) {
         $billing_address = $contact->getFirst('address');
     }
     $billing_address = $billing_address ? $billing_address['data'] : array();
     $discount_rate = $subtotal ? $data['discount'] / $subtotal : 0;
     $taxes = shopTaxes::apply($data['items'], array('shipping' => $shipping_address, 'billing' => $billing_address, 'discount_rate' => $discount_rate), $order['currency']);
     $tax = $tax_included = 0;
     foreach ($taxes as $t) {
         if (isset($t['sum'])) {
             $tax += $t['sum'];
         }
         if (isset($t['sum_included'])) {
             $tax_included += $t['sum_included'];
         }
     }
     $data['tax'] = $tax_included + $tax;
     $data['total'] = $subtotal + $tax + $this->price($data['shipping']) - $this->price($data['discount']);
     // for logging changes in stocks
     shopProductStocksLogModel::setContext(shopProductStocksLogModel::TYPE_ORDER, 'Order %s was edited', array('order_id' => $data['id']));
     // update
     $order_model->update($data, $data['id']);
     $log_model = new waLogModel();
     $log_model->add('order_edit', $data['id']);
     shopProductStocksLogModel::clearContext();
     if (!empty($data['params'])) {
         $params_model = new shopOrderParamsModel();
         $params_model->set($data['id'], $data['params'], false);
     }
     return true;
 }
Пример #15
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 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();
 }
Пример #17
0
 /**
  * Verify that the plugin is suitable for payment of the order
  * @param int $order_id
  * @return bool|string
  */
 private function isSuitable($order_id)
 {
     $order_params_model = new shopOrderParamsModel();
     if (!$this->merchant_id) {
         return 'Invalid plugin id';
     } else {
         if ($this->merchant_id != $order_params_model->getOne($order_id, 'payment_id')) {
             return 'Plugin does not suitable to payment of the order';
         }
         $shop_plugin_model = new shopPluginModel();
         if ($plugin = $shop_plugin_model->getById($this->merchant_id)) {
             if (!$plugin['status']) {
                 return 'Plugin status is disabled';
             }
         } else {
             return 'Plugin deleted';
         }
     }
     return true;
 }
Пример #18
0
 public function reduceProductsFromStocks($order_id)
 {
     $order_params_model = new shopOrderParamsModel();
     $reduced = $order_params_model->getOne($order_id, 'reduced');
     if ($reduced) {
         return;
     }
     $items_model = new shopOrderItemsModel();
     $items = $items_model->select('*')->where("type='product' AND order_id = " . (int) $order_id)->fetchAll();
     $sku_stock = array();
     foreach ($items as $item) {
         if (!isset($sku_stock[$item['sku_id']][$item['stock_id']])) {
             $sku_stock[$item['sku_id']][$item['stock_id']] = 0;
         }
         $sku_stock[$item['sku_id']][$item['stock_id']] -= $item['quantity'];
     }
     $items_model->updateStockCount($sku_stock);
     $order_params_model->setOne($order_id, 'reduced', 1);
 }
Пример #19
0
 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;
 }
Пример #20
0
 public function execute()
 {
     $fm = new shopFollowupModel();
     $opm = new shopOrderParamsModel();
     $asm = new waAppSettingsModel();
     $olm = new shopOrderLogModel();
     $cm = new shopCustomerModel();
     $om = new shopOrderModel();
     $asm->set('shop', 'last_followup_cli', time());
     $view = wa()->getView();
     $empty_customer = $cm->getEmptyRow();
     $general = wa('shop')->getConfig()->getGeneralSettings();
     foreach ($fm->getAllEnabled() as $f) {
         $between_from = date('Y-m-d', strtotime($f['last_cron_time']) - 24 * 3600);
         $between_to = date('Y-m-d 23:59:59', time() - $f['delay'] - 10 * 3600);
         $orders = $om->where('paid_date >= ? AND paid_date < ?', $between_from, $between_to)->fetchAll('id');
         if ($orders) {
             $f_param_key = 'followup_' . $f['id'];
             // Params for all orders with one query
             $params = $opm->get(array_keys($orders));
             // Customer data for all orders with one query
             $cids = array();
             foreach ($orders as $o) {
                 $cids[] = $o['contact_id'];
             }
             $customers = $cm->getById($cids);
             $sent_count = 0;
             foreach ($orders as $o) {
                 try {
                     // Is there a recipient in the first place?
                     if (empty($o['contact_id'])) {
                         if (waSystemConfig::isDebug()) {
                             waLog::log("Unable to send follow-up #{$f['id']} for order #{$o['id']}: no contact_id");
                         }
                         continue;
                     }
                     // Check that this is the first order of this customer
                     if ($f['first_order_only']) {
                         $first_order_id = $om->select('MIN(id)')->where('contact_id=? AND paid_date IS NOT NULL', $o['contact_id'])->fetchField();
                         if ($first_order_id != $o['id']) {
                             if (waSystemConfig::isDebug()) {
                                 waLog::log("Skipping follow-up #{$f['id']} for order #{$o['id']}: not the first order of a customer.");
                             }
                             continue;
                         }
                     }
                     $o['params'] = ifset($params[$o['id']], array());
                     $source = 'backend';
                     if (!empty($o['params']['storefront'])) {
                         $source = rtrim($o['params']['storefront'], '/') . '/*';
                     }
                     if ($f['source'] && $f['source'] != $source) {
                         continue;
                     }
                     // Make sure we have not send follow-up for this order yet
                     if (isset($o['params'][$f_param_key])) {
                         if (waSystemConfig::isDebug()) {
                             waLog::log("Skipping follow-up #{$f['id']} for order #{$o['id']}: already sent before.");
                         }
                         continue;
                     }
                     shopHelper::workupOrders($o, true);
                     // Recipient info
                     $customer = ifset($customers[$o['contact_id']], $empty_customer);
                     $contact = new shopCustomer($o['contact_id']);
                     $email = $contact->get('email', 'default');
                     // this with throw exception if contact does not exist; that's ok
                     if (!$email) {
                         if (waSystemConfig::isDebug()) {
                             waLog::log("Unable to send follow-up #{$f['id']} for order #{$o['id']}: contact has no email");
                         }
                         continue;
                     }
                     $to = array($email => $contact->getName());
                     if (self::sendOne($f, $o, $customer, $contact, $to, $view, $general)) {
                         $sent_count++;
                         // Write to order log
                         $olm->add(array('order_id' => $o['id'], 'contact_id' => null, 'action_id' => '', 'text' => sprintf_wp("Follow-up <strong>%s</strong> (%s) sent to customer.", htmlspecialchars($f['name']), $f['id']), 'before_state_id' => $o['state_id'], 'after_state_id' => $o['state_id']));
                         // Write to order params
                         $opm->insert(array('order_id' => $o['id'], 'name' => $f_param_key, 'value' => date('Y-m-d H:i:s')));
                     } else {
                         waLog::log("Unable to send follow-up #{$f['id']} for order #{$o['id']}: waMessage->send() returned FALSE.");
                     }
                 } catch (Exception $e) {
                     waLog::log("Unable to send follow-up #{$f['id']} for order #{$o['id']}:\n" . $e);
                 }
             }
             /**
              * Notify plugins about sending followup
              * @event followup_send
              * @param array[string]int $params['sent_count'] number of emails successfully sent
              * @param array[string]int $params['id'] followup_id
              * @return void
              */
             $event_params = $f;
             $event_params['sent_count'] = $sent_count;
             wa()->event('followup_send', $event_params);
         }
         $fm->updateById($f['id'], array('last_cron_time' => $between_to));
     }
 }
Пример #21
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;
             }
         }
     }
 }