private function _setPrices(Product &$product, $param)
 {
     if (isset($param->CallbackParameter->prices) && count($prices = $param->CallbackParameter->prices) > 0) {
         //delete all price first
         $deleteIds = array();
         foreach ($prices as $price) {
             if (trim($price->active) === '0' && isset($price->id)) {
                 $deleteIds[] = trim($price->id);
             }
         }
         if (count($deleteIds) > 0) {
             ProductPrice::updateByCriteria('active = 0', 'id in (' . str_repeat('?', count($deleteIds)) . ')', $deleteIds);
         }
         //update or create new
         foreach ($prices as $price) {
             if (isset($price->id) && in_array(trim($price->id), $deleteIds)) {
                 continue;
             }
             if (!($type = ProductPriceType::get(trim($price->typeId))) instanceof ProductPriceType) {
                 continue;
             }
             $priceValue = trim($price->value);
             $start = trim($price->start);
             $end = trim($price->end);
             if (!is_numeric(StringUtilsAbstract::getValueFromCurrency($priceValue))) {
                 throw new Exception('Invalid price: ' . $priceValue);
             }
             if (!isset($price->id) || ($id = trim($price->id)) === '') {
                 if (trim($price->active) === '1') {
                     ProductPrice::create($product, $type, $priceValue, $start, $end);
                 }
                 //if it's deactivated one, ignore
             } else {
                 if (($productPrice = ProductPrice::get($id)) instanceof ProductPrice) {
                     $productPrice->setPrice($priceValue)->setType($type)->setProduct($product)->setStart($start)->setEnd($end)->save();
                 }
             }
         }
     }
     return $this;
 }
 private function _getNewPrice($updateMagento)
 {
     $result = null;
     $sku = $this->sku;
     $updateMagento = $updateMagento === true ? true : false;
     $product = Product::getBySku($sku);
     if (!$product instanceof Product) {
         throw new Exception('Invalid sku passed in, "' . $sku . '" given');
     }
     $min = PriceMatchMin::getBySku($sku);
     $rule = ProductPriceMatchRule::getByProduct($product);
     $prices = ProductPrice::getPrices($product, ProductPriceType::get(ProductPriceType::ID_RRP));
     if (count($prices) === 0) {
         $newPrice = ProductPrice::create($product, ProductPriceType::get(ProductPriceType::ID_RRP), 0);
         $prices = array($newPrice);
     }
     // 		if(($magePrice = $this->_getMagentoProductPrice($sku)) !== null)
     // 			$prices[0]->setPrice($magePrice)->save();
     $myPrice = $prices[0]->getPrice();
     if (!$min instanceof PriceMatchMin) {
         $min = PriceMatchMin::create($this->sku);
     }
     if ($rule instanceof ProductPriceMatchRule) {
         $company = $rule->getCompany();
         $price_from = $rule->getPrice_from();
         $price_to = $rule->getPrice_to();
         $offset = $rule->getOffset();
         $where = array(1);
         $params = array();
         $where[] = "minId = ? ";
         $params[] = $min->getId();
         $from_date = UDate::now('Australia/Melbourne')->setTime(0, 0, 0)->setTimeZone('UTC');
         $to_date = UDate::now('Australia/Melbourne')->setTime(23, 59, 59)->setTimeZone('UTC');
         $where[] = "created >= ? ";
         $params[] = $from_date;
         $where[] = "created <= ? ";
         $params[] = $to_date;
         $companies = $company->getAllAlias();
         $companyIds = array_map(create_function('$a', 'return $a->getId();'), $companies);
         $where[] = 'companyId IN (' . implode(", ", array_fill(0, count($companyIds), "?")) . ')';
         $params = array_merge($params, $companyIds);
         //calculate target compatitor price
         $records = PriceMatchRecord::getAllByCriteria(implode(' AND ', $where), $params, true, null, DaoQuery::DEFAUTL_PAGE_SIZE, array('price' => 'asc'));
         $base_price = null;
         foreach ($records as $record) {
             if ($base_price === null || doubleval($record->getPrice()) !== doubleval(0) && doubleval($record->getPrice()) < doubleval($base_price)) {
                 $base_price = doubleval($record->getPrice());
             }
         }
         if ($base_price !== null) {
             if ($price_from !== null) {
                 if (strpos($price_from, '%') !== false) {
                     $price_from = $base_price - $base_price * doubleval(0.01 * doubleval(str_replace('%', '', $price_from)));
                 } else {
                     $price_from = $base_price - doubleval($price_from);
                 }
                 if (doubleval($price_from) <= doubleval(0)) {
                     $price_from = doubleval(0);
                 }
             }
             if ($price_to !== null) {
                 if (strpos($price_to, '%') !== false) {
                     $price_to = $base_price + $base_price * doubleval(0.01 * doubleval(str_replace('%', '', $price_to)));
                 } else {
                     $price_to = $base_price + doubleval($price_to);
                 }
             }
             // check if in range
             if (($price_from === null || $myPrice >= $price_from) && ($price_to === null || $myPrice <= $price_to)) {
                 $result = $base_price;
                 // apply offset
                 if ($offset !== null) {
                     if (strpos($offset, '%') !== false) {
                         $result = $result + $result * doubleval(0.01 * doubleval(str_replace('%', '', $offset)));
                     } else {
                         $result = $result + doubleval($offset);
                     }
                 }
                 // set product price
                 if (isset($prices[0]) && $prices[0] instanceof ProductPrice) {
                     $newmatchprice = doubleval($result);
                     if ($newmatchprice > 0) {
                         $oldPrice = $prices[0]->getPrice();
                         echo 'update price from old price : ' . $oldPrice . ' to new price :' . $newmatchprice . "\n";
                         $prices[0]->setPrice(doubleval($result))->save()->addLog('PriceMatch change price from $' . $oldPrice . 'to new price $' . $result, Log::TYPE_SYSTEM);
                         // 						if($updateMagento === true)
                         // 							$this->updateMagentoPrice(doubleval($result));
                     } else {
                         echo 'not update price because new match price is 0 ( ' . $newmatchprice . ' )' . "\n";
                     }
                 }
             }
         } else {
             if ($this->debug === true) {
                 echo "cannot find price for PriceMatchCompany " . $company->getCompanyName() . ', ' . $product->getSku() . '(id=' . $product->getId() . ', min(id=' . $min->getId() . '), records found:' . count($records) . "\n";
             }
         }
         if ($this->debug === true) {
             echo 'new price= ' . ($result === null ? 'N/A' : $result) . ', my price= ' . (isset($myPrice) ? $myPrice : 'N/A') . ', ' . $company->getCompanyName() . ' price= ' . $base_price . ', matching range=[' . $price_from . ',' . $price_to . '], offset=' . ($offset === null ? 'null' : $offset) . "\n";
         }
     } elseif ($this->debug === true) {
         echo ($min instanceof PriceMatchMin ? '' : 'Cannot find result on StaticIce for all known PriceMatchCompanies') . ($rule instanceof ProductPriceMatchRule ? '' : 'cannot find ProductPriceMatchRule for product ' . $product->getSku() . '(id=' . $product->getId() . ')') . "\n";
     }
     return $result;
 }
Exemple #3
0
 /**
  * Adding a price to a product
  *
  * @param ProductPriceType $type
  * @param number           $value
  * @param string           $fromDate
  * @param string           $toDate
  *
  * @return Product
  */
 public function addPrice(ProductPriceType $type, $value, $fromDate = null, $toDate = null)
 {
     ProductPrice::create($this, $type, $value, $fromDate, $toDate);
     return $this;
 }