public function getProductStocks($offset, $count, $order_by)
 {
     $data = $this->product_model->getProductStocks($offset, $count, $order_by);
     foreach ($data as &$product) {
         $product['icon'] = shopHelper::getStockCountIcon($product['count']);
         foreach ($product['skus'] as &$sku) {
             $sku['icon'] = shopHelper::getStockCountIcon($sku['count']);
         }
         unset($sku);
         foreach ($product['stocks'] as $stock_id => &$stock) {
             foreach ($stock as &$sku) {
                 $sku['icon'] = shopHelper::getStockCountIcon($sku['count'], $stock_id);
             }
         }
         unset($product, $stock, $sku);
     }
     // because javascript doesn't guarantee any particular order for the keys in object
     // make hash-table to array converting
     $data = array_values($data);
     foreach ($data as &$product) {
         $product['skus'] = array_values($product['skus']);
         $product['stocks'] = array_values($product['stocks']);
         foreach ($product['stocks'] as &$stock) {
             $stock = array_values($stock);
         }
         unset($product, $stock);
     }
     return $data;
 }
 public function execute()
 {
     $sku_id = waRequest::post('sku_id', 0, waRequest::TYPE_INT);
     $src_stock = waRequest::post('src_stock', 0, waRequest::TYPE_INT);
     $dst_stock = waRequest::post('dst_stock', 0, waRequest::TYPE_INT);
     $count = waRequest::post('count', 0, waRequest::TYPE_INT);
     if ($src_stock == $dst_stock || !$src_stock || !$dst_stock || !$count) {
         $this->errors[] = _w("Error when transfer");
         return;
     }
     $product_skus_model = new shopProductSkusModel();
     shopProductStocksLogModel::setContext(shopProductStocksLogModel::TYPE_STOCK);
     if (!$product_skus_model->transfer($sku_id, $count, $src_stock, $dst_stock)) {
         $this->errors[] = _w("Error when transfer");
         return;
     }
     shopProductStocksLogModel::clearContext();
     $sku = $product_skus_model->getById($sku_id);
     /*
     $product_stocks_model = new shopProductStocksModel();
     $data = $product_stocks_model->getStocksOfProduct($sku['product_id'], array($src_stock, $dst_stock), 'sku.count DESC');
     
     foreach ($data as &$stock) {
         foreach ($stock as &$stock_sku) {
             $stock_sku['icon'] = shopHelper::getStockCountIcon($stock_sku['count']);
         }
     }
     unset($stock, $stock_sku);
     */
     $stock_skus = array();
     $product_model = new shopProductModel();
     $data = $product_model->getProductStocksByProductId($sku['product_id']);
     if (isset($data[$sku['product_id']])) {
         $data = $data[$sku['product_id']];
         if (isset($data['stocks'][$src_stock])) {
             $stock_skus[$src_stock] = array();
             foreach ($data['stocks'][$src_stock] as $stock_sku) {
                 $stock_sku['icon'] = shopHelper::getStockCountIcon($stock_sku['count'], $src_stock);
                 $stock_skus[$src_stock][] = $stock_sku;
             }
         }
         if (isset($data['stocks'][$dst_stock])) {
             $stock_skus[$dst_stock] = array();
             foreach ($data['stocks'][$dst_stock] as $stock_sku) {
                 $stock_sku['icon'] = shopHelper::getStockCountIcon($stock_sku['count'], $dst_stock);
                 $stock_skus[$dst_stock][] = $stock_sku;
             }
         }
     }
     $this->response = array('stocks' => $stock_skus ? $stock_skus : new stdClass(), 'product_id' => $sku['product_id']);
 }
 private function workupSku(&$sku, $sku_stocks)
 {
     // detaled stocks count icon for sku
     if (empty($sku_stocks[$sku['id']])) {
         $sku['icon'] = shopHelper::getStockCountIcon($sku['count'], null, true);
     } else {
         $icons = array();
         foreach ($sku_stocks[$sku['id']] as $stock_id => $stock) {
             $icon =& $icons[$stock_id];
             $icon = shopHelper::getStockCountIcon($stock['count'], $stock_id) . " ";
             $icon .= $stock['count'] . " ";
             $icon .= "<span class='small'>@" . htmlspecialchars($stock['name']) . "</span>";
             unset($icon);
         }
         //             $sku['icon'] = implode(', ', $icons);
         $sku['icon'] = shopHelper::getStockCountIcon($sku['count'], null, true);
         $sku['icons'] = $icons;
     }
 }
Exemple #4
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;
 }
 private function workupItems(&$item, $sku_stocks)
 {
     $size = $this->getCropSize();
     if (empty($item['image_id'])) {
         $item['url_crop_small'] = null;
     } else {
         $item['url_crop_small'] = shopImage::getUrl(array('id' => $item['image_id'], 'product_id' => $item['id'], 'ext' => $item['ext']), $size);
     }
     // aggregated stocks count icon for product
     if (empty($item['fake'])) {
         $item['icon'] = shopHelper::getStockCountIcon($item['count'], null, true);
     }
     foreach ($item['skus'] as &$sku) {
         if (empty($sku['fake'])) {
             // detaled stocks count icon for sku
             if (empty($sku_stocks[$sku['id']])) {
                 $sku['icon'] = shopHelper::getStockCountIcon($sku['count'], null, true);
             } else {
                 $icons = array();
                 foreach ($sku_stocks[$sku['id']] as $stock_id => $stock) {
                     $icon =& $icons[$stock_id];
                     $icon = shopHelper::getStockCountIcon($stock['count'], $stock_id) . " ";
                     $icon .= $stock['count'] . " ";
                     $icon .= "<span class='small'>@" . htmlspecialchars($stock['name']) . "</span>";
                     unset($icon);
                 }
                 //$sku['icon'] = implode(', ', $icons);
                 $sku['icon'] = shopHelper::getStockCountIcon($sku['count'], null, true);
                 $sku['icons'] = $icons;
             }
         }
     }
     unset($sku);
 }
 public function ordersAutocomplete($q)
 {
     // search by:
     // 1. order_id,
     // 2. email, phone, firstname, lastname, name
     // 3. product, sku
     $limit = 3;
     // first, assume $q is encoded $order_id, so decode
     $dq = shopHelper::decodeOrderId($q);
     if (!$dq) {
         $dq = self::decodeOrderId($q);
     }
     if ($dq) {
         $orders = $this->getOrders($dq, $limit);
     } else {
         $orders = array();
     }
     $cnt = count($orders);
     if ($cnt < $limit) {
         $orders = array_merge($orders, $this->getOrders($q, $limit - $cnt));
     }
     foreach ($orders as &$o) {
         $o['autocomplete_item_type'] = 'order';
     }
     unset($o);
     $contacts = $this->contactsAutocomplete($q, $limit);
     foreach ($contacts as &$c) {
         $c['autocomplete_item_type'] = 'contact';
         $c['value'] = $c['name'];
     }
     unset($c);
     $products = $this->productsAutocomplete($q, $limit);
     foreach ($products as &$p) {
         $p['autocomplete_item_type'] = 'product';
         if (empty($p['label'])) {
             $p['label'] = htmlspecialchars($p['value']);
         }
         $p['label'] .= ' ' . shopHelper::getStockCountIcon($p['count'], null, true);
     }
     $data = array_merge($orders, $contacts, $products);
     return $data;
 }
 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);
     }
 }
 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;
 }