Пример #1
0
 public function total($code)
 {
     $sql = "SELECT SUM(s.primary_price * c.quantity) FROM " . $this->table . " c\n                JOIN shop_product_skus s ON c.sku_id = s.id\n                WHERE c.code = s:code AND type = 'product'";
     $products_total = $this->query($sql, array('code' => $code))->fetchField();
     $services_total = 0;
     $sql = "SELECT c.*, s.price, s.currency FROM " . $this->table . " c JOIN\n        shop_service s ON c.service_id = s.id WHERE c.code = s:code AND type = 'service'";
     $services = $this->query($sql, array('code' => $code))->fetchAll();
     if (!$services) {
         return shop_currency($products_total, wa('shop')->getConfig()->getCurrency(true), null, false);
     }
     $variant_ids = array();
     $product_ids = array();
     $sku_ids = array();
     foreach ($services as $s) {
         if ($s['service_variant_id']) {
             $variant_ids[] = $s['service_variant_id'];
         }
         $product_ids[] = $s['product_id'];
         if ($s['currency'] == '%') {
             $sku_ids[] = $s['sku_id'];
         }
     }
     $variant_ids = array_unique($variant_ids);
     $product_ids = array_unique($product_ids);
     $sku_ids = array_unique($sku_ids);
     // get variant settings
     $variants_model = new shopServiceVariantsModel();
     $variants = $variants_model->getWithPrice($variant_ids);
     // get products/skus settings
     $product_services_model = new shopProductServicesModel();
     $products_services = $product_services_model->getByProducts($product_ids, true);
     if ($sku_ids) {
         $sku_model = new shopProductSkusModel();
         $sku_prices = $sku_model->getPrices($sku_ids);
     }
     $primary = wa('shop')->getConfig()->getCurrency();
     foreach ($services as $s) {
         $p_id = $s['product_id'];
         $sku_id = $s['sku_id'];
         $s_id = $s['service_id'];
         $v_id = $s['service_variant_id'];
         $p_services = isset($products_services[$p_id]) ? $products_services[$p_id] : array();
         $s['price'] = $variants[$v_id]['price'];
         // price variant for sku
         if (!empty($p_services['skus'][$sku_id][$s_id]['variants'][$v_id]['price'])) {
             $s['price'] = $p_services['skus'][$sku_id][$s_id]['variants'][$v_id]['price'];
         }
         if ($s['currency'] == '%') {
             $s['price'] = $s['price'] * $sku_prices[$sku_id] / 100;
         } else {
             $s['price'] = shop_currency($s['price'], $variants[$v_id]['currency'], $primary, false);
         }
         $services_total += $s['price'] * $s['quantity'];
     }
     $total = $products_total + $services_total;
     $currency = wa('shop')->getConfig()->getCurrency(false);
     if ($currency != $primary) {
         $currencies = wa('shop')->getConfig()->getCurrencies(array($currency));
         $total = $total / $currencies[$currency]['rate'];
     }
     return (double) $total;
 }