Exemplo n.º 1
0
 public function save($data, $id = 0, $not_delete_products = false)
 {
     $primary_currency = wa('shop')->getConfig()->getCurrency();
     if (empty($data['currency'])) {
         $data['currency'] = $primary_currency;
     }
     $just_inserted = false;
     if (!$id) {
         $id = $this->insert(array('name' => $data['name'], 'price' => 0, 'currency' => $data['currency'], 'tax_id' => isset($data['tax_id']) ? $data['tax_id'] : ($data['tax_id'] === null ? null : 0), 'variant_id' => 0));
         if (!$id) {
             return false;
         }
         $just_inserted = true;
     } else {
         $id = (int) $id;
     }
     if ($id) {
         $this->updateVariants($id, $data['variants']);
         $this->updateTypes($id, !empty($data['types']) ? $data['types'] : array());
         if (!empty($data['products'])) {
             $this->updateProducts($id, $data['products']);
         }
         if (!$just_inserted) {
             $update = array();
             $fields = array('name', 'currency', 'price', 'tax_id');
             foreach ($fields as $field) {
                 if (isset($data[$field])) {
                     $update[$field] = $data[$field];
                 } else {
                     if ($field === 'tax_id' && $data[$field] === null) {
                         $update[$field] = $data[$field];
                     }
                 }
             }
             if ($update) {
                 $this->updateById($id, $update);
             }
         }
     }
     $currency = $data['currency'];
     // update primary price
     if ($currency != '%') {
         $currency_model = new shopCurrencyModel();
         $rate = $currency_model->getRate($currency);
     } else {
         $rate = 1;
         // hack for percents
     }
     $sql = "UPDATE `shop_service_variants`\n                SET primary_price = price*{$rate}\n                WHERE service_id = {$id}";
     $this->exec($sql);
     $sql = "UPDATE `shop_product_services` ps\n                SET ps.primary_price = ps.price*{$rate}\n                WHERE ps.service_id = {$id} AND ps.price IS NOT NULL";
     $this->exec($sql);
     $sql = "UPDATE `shop_service` s\n                JOIN `shop_service_variants` sv ON s.id = sv.service_id AND s.variant_id = sv.id\n                SET s.price = sv.primary_price\n                WHERE s.id = {$id}";
     $this->exec($sql);
     return $id;
 }
Exemplo n.º 2
0
 public function getTotalSkuSalesByProduct($product_id, $product_currency = null)
 {
     if ($product_currency) {
         $currency_model = new shopCurrencyModel();
         $rate = $currency_model->getRate($product_currency);
         if (!$rate) {
             $rate = 1;
         }
     } else {
         $rate = 1;
     }
     $sql = "SELECT sku_id, SUM(oi.price * o.rate * oi.quantity) total, SUM(oi.quantity) quantity,\n                SUM(IF(oi.purchase_price > 0, oi.purchase_price*o.rate, ps.purchase_price*" . $this->escape($rate) . ")*oi.quantity) purchase\n                FROM " . $this->table . " o\n                JOIN shop_order_items oi ON o.id = oi.order_id AND oi.product_id = i:product_id AND oi.type = 'product'\n                JOIN shop_product_skus ps ON oi.sku_id = ps.id\n                WHERE paid_date >= DATE_SUB(DATE('" . date('Y-m-d') . "'), INTERVAL 30 DAY)\n                GROUP BY oi.sku_id";
     return $this->query($sql, array('product_id' => $product_id))->fetchAll('sku_id');
 }