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']);
 }
 public function execute()
 {
     $sku_id = $this->get('id', true);
     $skus_model = new shopProductSkusModel();
     $sku = $skus_model->getById($sku_id);
     if (!$sku) {
         throw new waAPIException('invalid_param', 'SKU not found', 404);
     }
     $this->checkProductRights($sku['product_id']);
     $data = waRequest::post();
     if ($skus_model->update($sku_id, $data)) {
         $method = new shopProductSkusGetInfoMethod();
         $this->response = $method->getResponse(true);
     } else {
         throw new waAPIException('server_error', 500);
     }
 }
 public function execute()
 {
     $sku_id = $this->post('id', true);
     $skus_model = new shopProductSkusModel();
     // check if sku exists
     $sku = $skus_model->getById($sku_id);
     if (!$sku) {
         throw new waAPIException('invalid_param', 'SKU not found', 404);
     }
     // check access rights
     $this->checkProductRights($sku['product_id']);
     // delete sku
     if ($skus_model->delete($sku_id)) {
         $this->response = true;
     } else {
         throw new waAPIException('server_error', 500);
     }
 }
 public function execute()
 {
     $cart = new shopCart();
     $item_id = waRequest::post('id');
     $cart_items_model = new shopCartItemsModel();
     $item = $cart_items_model->getById($item_id);
     $is_html = waRequest::request('html');
     if ($q = waRequest::post('quantity', 0, 'int')) {
         if (!wa()->getSetting('ignore_stock_count')) {
             if ($item['type'] == 'product') {
                 $product_model = new shopProductModel();
                 $p = $product_model->getById($item['product_id']);
                 $sku_model = new shopProductSkusModel();
                 $sku = $sku_model->getById($item['sku_id']);
                 // check quantity
                 if ($sku['count'] !== null && $q > $sku['count']) {
                     $q = $sku['count'];
                     $name = $p['name'] . ($sku['name'] ? ' (' . $sku['name'] . ')' : '');
                     $this->response['error'] = sprintf(_w('Only %d pcs of %s are available, and you already have all of them in your shopping cart.'), $q, $name);
                     $this->response['q'] = $q;
                 }
             }
         }
         $cart->setQuantity($item_id, $q);
         $this->response['item_total'] = $is_html ? shop_currency_html($cart->getItemTotal($item_id), true) : shop_currency($cart->getItemTotal($item_id), true);
     } elseif ($v = waRequest::post('service_variant_id')) {
         $cart->setServiceVariantId($item_id, $v);
         $this->response['item_total'] = $is_html ? shop_currency_html($cart->getItemTotal($item['parent_id']), true) : shop_currency($cart->getItemTotal($item['parent_id']), true);
     }
     $total = $cart->total();
     $discount = $cart->discount();
     $this->response['total'] = $is_html ? shop_currency_html($total, true) : shop_currency($total, true);
     $this->response['discount'] = $is_html ? shop_currency_html($discount, true) : shop_currency($discount, true);
     $this->response['discount_numeric'] = $discount;
     $this->response['count'] = $cart->count();
     if (shopAffiliate::isEnabled()) {
         $add_affiliate_bonus = shopAffiliate::calculateBonus(array('total' => $total, 'discount' => $discount, 'items' => $cart->items(false)));
         $this->response['add_affiliate_bonus'] = sprintf(_w("This order will add +%s points to your affiliate bonus."), round($add_affiliate_bonus, 2));
     }
 }
Esempio n. 5
0
 public static function prepareProducts($products = array(), $contact_id = null, $currency = null)
 {
     $app_settings_model = new waAppSettingsModel();
     if ($app_settings_model->get(self::$plugin_id, 'status') && shopPrice::getDomainSetting('status')) {
         $category_ids = self::getUserCategoryId($contact_id);
         $domain_hash = shopPrice::getRouteHash();
         $params = array('domain_hash' => $domain_hash, 'category_id' => $category_ids);
         $price_model = new shopPricePluginModel();
         $prices = $price_model->getPriceByParams($params, true);
         if ($prices) {
             if (!$currency) {
                 $currency = wa('shop')->getConfig()->getCurrency(true);
             }
             $sku_model = new shopProductSkusModel();
             foreach ($products as &$product) {
                 foreach ($prices as $price) {
                     $price_field = "price_plugin_{$price['id']}";
                     $sku = $sku_model->getById($product['sku_id']);
                     if (!empty($sku[$price_field]) && $sku[$price_field] > 0) {
                         if (!empty($product['unconverted_currency']) && !empty($product['currency'])) {
                             $product['price'] = shop_currency($sku[$price_field], $product['unconverted_currency'], $currency, false);
                             $product['currency'] = $product['unconverted_currency'];
                             unset($product['frontend_price']);
                             unset($product['unconverted_currency']);
                             unset($product['frontend_price']);
                             unset($product['unconverted_price']);
                         } else {
                             $product['price'] = shop_currency($sku[$price_field], $product['currency'], $currency, false);
                         }
                         break;
                     }
                 }
             }
             unset($product);
         }
     }
     shopRounding::roundProducts($products);
     return $products;
 }
 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);
     }
 }
 public static function checkCart(&$cart = null)
 {
     $error = false;
     $cart = new shopCart();
     $code = $cart->getCode();
     $view = wa()->getView();
     if (!wa()->getSetting('ignore_stock_count')) {
         $cart_model = new shopCartItemsModel();
         $sku_model = new shopProductSkusModel();
         $product_model = new shopProductModel();
         $items = $cart->items(false);
         foreach ($items as &$item) {
             if (!isset($item['product_id'])) {
                 $sku = $sku_model->getById($item['sku_id']);
                 $product = $product_model->getById($sku['product_id']);
             } else {
                 $product = $product_model->getById($item['product_id']);
                 if (isset($item['sku_id'])) {
                     $sku = $sku_model->getById($item['sku_id']);
                 } else {
                     if (isset($item['features'])) {
                         $product_features_model = new shopProductFeaturesModel();
                         $sku_id = $product_features_model->getSkuByFeatures($product['id'], $item['features']);
                         if ($sku_id) {
                             $sku = $sku_model->getById($sku_id);
                         } else {
                             $sku = null;
                         }
                     } else {
                         $sku = $sku_model->getById($product['sku_id']);
                         if (!$sku['available']) {
                             $sku = $sku_model->getByField(array('product_id' => $product['id'], 'available' => 1));
                         }
                         if (!$sku) {
                             $item['error'] = _w('This product is not available for purchase');
                             $error = true;
                         }
                     }
                 }
             }
             $quantity = $item['quantity'];
             $c = $cart_model->countSku($code, $sku['id']);
             if ($sku['count'] !== null && $c + $quantity > $sku['count']) {
                 $quantity = $sku['count'] - $c;
                 $name = $product['name'] . ($sku['name'] ? ' (' . $sku['name'] . ')' : '');
                 if ($quantity < 0) {
                     $item['error'] = sprintf(_w('Only %d pcs of %s are available, and you already have all of them in your shopping cart.'), $sku['count'], $name);
                     $error = true;
                 }
             }
         }
         unset($item);
         foreach ($items as $item_id => $item) {
             $price = shop_currency($item['price'] * $item['quantity'], $item['currency'], null, false);
             if (isset($item['services'])) {
                 foreach ($item['services'] as $s) {
                     if (!empty($s['id'])) {
                         if (isset($s['variants'])) {
                             $price += shop_currency($s['variants'][$s['variant_id']]['price'] * $item['quantity'], $s['currency'], null, false);
                         } else {
                             $price += shop_currency($s['price'] * $item['quantity'], $s['currency'], null, false);
                         }
                     }
                 }
             }
             $items[$item_id]['full_price'] = $price;
         }
         $cart = array('items' => $items, 'total' => $cart->total(false), 'count' => $cart->count());
     }
     return $error;
 }
 public function execute()
 {
     $code = waRequest::cookie('shop_cart');
     if (!$code) {
         $code = md5(uniqid(time(), true));
         // header for IE
         wa()->getResponse()->addHeader('P3P', 'CP="NOI ADM DEV COM NAV OUR STP"');
         // set cart cookie
         wa()->getResponse()->setCookie('shop_cart', $code, time() + 30 * 86400, null, '', false, true);
     }
     $this->cart = new shopCart($code);
     $this->cart_model = new shopCartItemsModel();
     $data = waRequest::post();
     $this->is_html = waRequest::request('html');
     // add service
     if (isset($data['parent_id'])) {
         $this->addService($data);
         return;
     }
     // add sku
     $sku_model = new shopProductSkusModel();
     $product_model = new shopProductModel();
     if (!isset($data['product_id'])) {
         $sku = $sku_model->getById($data['sku_id']);
         $product = $product_model->getById($sku['product_id']);
     } else {
         $product = $product_model->getById($data['product_id']);
         if (isset($data['sku_id'])) {
             $sku = $sku_model->getById($data['sku_id']);
         } else {
             if (isset($data['features'])) {
                 $product_features_model = new shopProductFeaturesModel();
                 $sku_id = $product_features_model->getSkuByFeatures($product['id'], $data['features']);
                 if ($sku_id) {
                     $sku = $sku_model->getById($sku_id);
                 } else {
                     $sku = null;
                 }
             } else {
                 $sku = $sku_model->getById($product['sku_id']);
                 if (!$sku['available']) {
                     $sku = $sku_model->getByField(array('product_id' => $product['id'], 'available' => 1));
                 }
                 if (!$sku) {
                     $this->errors = _w('This product is not available for purchase');
                     return;
                 }
             }
         }
     }
     $quantity = waRequest::post('quantity', 1);
     if ($product && $sku) {
         // check quantity
         if (!wa()->getSetting('ignore_stock_count')) {
             $c = $this->cart_model->countSku($code, $sku['id']);
             if ($sku['count'] !== null && $c + $quantity > $sku['count']) {
                 $quantity = $sku['count'] - $c;
                 $name = $product['name'] . ($sku['name'] ? ' (' . $sku['name'] . ')' : '');
                 if (!$quantity) {
                     $this->errors = sprintf(_w('Only %d pcs of %s are available, and you already have all of them in your shopping cart.'), $sku['count'], $name);
                     return;
                 } else {
                     $this->response['error'] = sprintf(_w('Only %d pcs of %s are available, and you already have all of them in your shopping cart.'), $sku['count'], $name);
                 }
             }
         }
         $services = waRequest::post('services', array());
         if ($services) {
             $variants = waRequest::post('service_variant');
             $temp = array();
             $service_ids = array();
             foreach ($services as $service_id) {
                 if (isset($variants[$service_id])) {
                     $temp[$service_id] = $variants[$service_id];
                 } else {
                     $service_ids[] = $service_id;
                 }
             }
             if ($service_ids) {
                 $service_model = new shopServiceModel();
                 $temp_services = $service_model->getById($service_ids);
                 foreach ($temp_services as $row) {
                     $temp[$row['id']] = $row['variant_id'];
                 }
             }
             $services = $temp;
         }
         $item_id = null;
         $item = $this->cart_model->getItemByProductAndServices($code, $product['id'], $sku['id'], $services);
         if ($item) {
             $item_id = $item['id'];
             $this->cart->setQuantity($item_id, $item['quantity'] + $quantity);
         }
         if (!$item_id) {
             $data = array('create_datetime' => date('Y-m-d H:i:s'), 'product_id' => $product['id'], 'sku_id' => $sku['id'], 'quantity' => $quantity, 'type' => 'product');
             if ($services) {
                 $data_services = array();
                 foreach ($services as $service_id => $variant_id) {
                     $data_services[] = array('service_id' => $service_id, 'service_variant_id' => $variant_id);
                 }
             } else {
                 $data_services = array();
             }
             $item_id = $this->cart->addItem($data, $data_services);
         }
         if (waRequest::isXMLHttpRequest()) {
             $this->response['item_id'] = $item_id;
             $this->response['total'] = $this->currencyFormat($this->cart->total());
             $this->response['discount'] = $this->currencyFormat($this->cart->discount());
             $this->response['count'] = $this->cart->count();
         } else {
             $this->redirect(waRequest::server('HTTP_REFERER'));
         }
     } else {
         throw new waException('product not found');
     }
 }