public function execute()
 {
     $order_id = waRequest::get('order_id', null, waRequest::TYPE_INT);
     $customer_id = waRequest::get('customer_id', null, waRequest::TYPE_INT);
     $order_id = $order_id ? $order_id : null;
     $currency = waRequest::get('currency');
     if (!$currency && $order_id) {
         $order_model = new shopOrderModel();
         $order = $order_model->getOrder($order_id);
         $currency = $order['currency'];
     }
     $product_id = waRequest::get('product_id', 0, waRequest::TYPE_INT);
     if (!$product_id) {
         $this->errors[] = _w("Unknown product");
         return;
     }
     $sku_id = waRequest::get('sku_id', 0, waRequest::TYPE_INT);
     if ($sku_id) {
         $sku = $this->getSku($sku_id, $order_id);
         $skus = shopPricePlugin::prepareSkus(array($sku_id => $sku), $customer_id);
         if (!empty($skus[$sku_id])) {
             $sku = $skus[$sku_id];
         }
         $this->response['sku'] = $sku;
         $this->response['service_ids'] = array_keys($sku['services']);
     } else {
         $product = $this->getProduct($product_id, $order_id);
         $products = shopPricePlugin::prepareProducts(array($product_id => $product), $customer_id, $currency);
         if (!empty($products[$product_id])) {
             $product = $products[$product_id];
         }
         $product['skus'] = shopPricePlugin::prepareSkus($product['skus'], $customer_id, $currency);
         foreach ($product['skus'] as &$sku) {
             if (isset($sku['price'])) {
                 $sku['price_str'] = wa_currency($sku['price'], $currency);
                 $sku['price_html'] = wa_currency_html($sku['price'], $currency);
             }
         }
         unset($sku);
         $this->response['product'] = $product;
         $this->response['sku_ids'] = array_keys($product['skus']);
         $this->response['service_ids'] = array_keys($product['services']);
     }
 }
Пример #2
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;
     }
 }
 public function productsAutocomplete($q, $limit = null)
 {
     $limit = $limit !== null ? $limit : $this->limit;
     $product_model = new shopProductModel();
     $q = $product_model->escape($q, 'like');
     $fields = 'id, name AS value, price, count, sku_id';
     $products = $product_model->select($fields)->where("name LIKE '{$q}%'")->limit($limit)->fetchAll('id');
     $count = count($products);
     if ($count < $limit) {
         $product_skus_model = new shopProductSkusModel();
         $product_ids = array_keys($product_skus_model->select('id, product_id')->where("sku LIKE '{$q}%'")->limit($limit)->fetchAll('product_id'));
         if ($product_ids) {
             $data = $product_model->select($fields)->where('id IN (' . implode(',', $product_ids) . ')')->limit($limit - $count)->fetchAll('id');
             // not array_merge, because it makes first reset numeric keys and then make merge
             $products = $products + $data;
         }
     }
     // try find with LIKE %query%
     if (!$products) {
         $products = $product_model->select($fields)->where("name LIKE '%{$q}%'")->limit($limit)->fetchAll();
     }
     $currency = wa()->getConfig()->getCurrency();
     foreach ($products as &$p) {
         $p['price_str'] = wa_currency($p['price'], $currency);
     }
     unset($p);
     if (waRequest::get('with_sku_name')) {
         $sku_ids = array();
         foreach ($products as $p) {
             $sku_ids[] = $p['sku_id'];
         }
         $product_skus_model = new shopProductSkusModel();
         $skus = $product_skus_model->getByField('id', $sku_ids, 'id');
         $sku_names = array();
         foreach ($skus as $sku_id => $sku) {
             $name = '';
             if ($sku['name']) {
                 $name = $sku['name'];
                 if ($sku['sku']) {
                     $name .= ' (' . $sku['sku'] . ')';
                 }
             } else {
                 $name = $sku['sku'];
             }
             $sku_names[$sku_id] = $name;
         }
         foreach ($products as &$p) {
             $p['sku_name'] = $sku_names[$p['sku_id']];
         }
         unset($p);
     }
     return array_values($products);
 }
 protected function workupProducts(&$products)
 {
     $currency = $this->getConfig()->getCurrency();
     foreach ($products as &$p) {
         if ($p['min_price'] == $p['max_price']) {
             $p['price_range'] = wa_currency($p['min_price'], $currency);
         } else {
             $p['price_range'] = wa_currency($p['min_price'], $currency) . '...' . wa_currency($p['max_price'], $currency);
         }
         if ($p['badge']) {
             $p['badge'] = shopHelper::getBadgeHtml($p['badge']);
         }
         unset($p['meta_description'], $p['meta_keywords'], $p['meta_title'], $p['description'], $p['summary']);
     }
     unset($p);
     if ($this->sort == 'count') {
         foreach ($products as &$p) {
             $p['icon'] = shopHelper::getStockCountIcon($p['count']);
         }
     } else {
         if ($this->sort == 'create_datetime') {
             foreach ($products as &$p) {
                 $p['create_datetime_str'] = wa_date('humandatetime', $p['create_datetime']);
             }
         } else {
             if ($this->sort == 'rating') {
                 foreach ($products as &$p) {
                     $p['rating_str'] = shopHelper::getRatingHtml($p['rating'], 10, true);
                 }
             } else {
                 if ($this->sort == 'total_sales') {
                     $currency = wa('shop')->getConfig()->getCurrency();
                     foreach ($products as &$p) {
                         $p['total_sales_str'] = wa_currency($p['total_sales'], $currency);
                     }
                 }
             }
         }
     }
     unset($p);
     $info = $this->collection->getInfo();
     if ($info['hash'] == 'category') {
         $product_ids = array_keys($products);
         $category_products_model = new shopCategoryProductsModel();
         $ids = $category_products_model->filterByEnteringInCategories($product_ids, $info['id']);
         $ids = array_flip($ids);
         foreach ($products as $id => &$product) {
             $product['alien'] = $info['type'] == shopCategoryModel::TYPE_STATIC && !isset($ids[$id]);
         }
         unset($product);
     }
 }
Пример #5
0
 public function getSku($sku_id, $order_id = null)
 {
     $rate = 1;
     $currency = $this->getCurrency();
     $currency_model = $this->getModel('currency');
     if ($order_id) {
         $order = $this->getOrder($order_id);
         $rate = $order['rate'];
         $currency = $order['currency'];
     }
     $data = $this->getModel('sku')->getSku($sku_id);
     $data['price'] = (double) $currency_model->convertByRate($data['primary_price'], 1, $rate);
     $data['price_str'] = wa_currency($data['price'], $currency);
     $data['services'] = $this->getServices($data['product_id'], $sku_id, $order_id, $data['price']);
     $this->workupServices($data['services'], $order_id);
     return $data;
 }
 public function workupData($data)
 {
     $currency = $data['currency'] ? $data['currency'] : $this->getConfig()->getCurrency();
     $file_names = array();
     // sku_id => filename of attachment
     foreach ($data['skus'] as $id => &$sku) {
         if (!isset($sku['file_name'])) {
             $file_names[$sku['id']] = '';
             // need to obtain filename
         }
         // price in light of l18n: if ru - delimeter is ',', if en - delimeter is '.'
         $sku['price_loc'] = (string) (double) $sku['price'];
         $sku['price_str'] = wa_currency($sku['price'], $currency);
         $sku['stock_icon'] = array();
         $sku['stock_icon'][0] = shopHelper::getStockCountIcon($sku['count']);
         if (!empty($sku['stock'])) {
             foreach ($sku['stock'] as $stock_id => $count) {
                 $sku['stock_icon'][$stock_id] = shopHelper::getStockCountIcon($count, $stock_id);
             }
         }
     }
     unset($sku);
     // obtain filename
     if ($file_names) {
         $product_skus_model = new shopProductSkusModel();
         $file_names = $product_skus_model->select('id, file_name')->where("id IN('" . implode("','", array_keys($file_names)) . "')")->fetchAll('id', true);
         foreach ($file_names as $sku_id => $file_name) {
             $data['skus'][$sku_id]['file_name'] = $file_name;
         }
     }
     return $data;
 }
Пример #7
0
function shop_currency($n, $in_currency = null, $out_currency = null, $format = true)
{
    /**
     * @var shopConfig $config
     */
    $config = wa('shop')->getConfig();
    $primary = $config->getCurrency(true);
    // current currency
    $currency = $config->getCurrency(false);
    if (!$in_currency) {
        $in_currency = $primary;
    }
    if ($in_currency === true || $in_currency === 1) {
        $in_currency = $currency;
    }
    if (!$out_currency) {
        $out_currency = $currency;
    }
    if ($in_currency != $out_currency) {
        $currencies = wa('shop')->getConfig()->getCurrencies(array($in_currency, $out_currency));
        if (isset($currencies[$in_currency]) && $in_currency != $primary) {
            $n = $n * $currencies[$in_currency]['rate'];
        }
        if ($out_currency != $primary) {
            $n = $n / ifempty($currencies[$out_currency]['rate'], 1.0);
        }
    }
    if ($format === 'h') {
        return wa_currency_html($n, $out_currency);
    } elseif ($format) {
        return wa_currency($n, $out_currency);
    } else {
        return str_replace(',', '.', $n);
    }
}