protected static function _getData() { if (count(self::$_dateRange) === 0) { $yesterdayLocal = new UDate('now', 'Australia/Melbourne'); $yesterdayLocal->modify('-1 day'); $fromDate = new UDate($yesterdayLocal->format('Y-m-d') . ' 00:00:00', 'Australia/Melbourne'); $fromDate->setTimeZone('UTC'); $toDate = new UDate($yesterdayLocal->format('Y-m-d') . ' 23:59:59', 'Australia/Melbourne'); $toDate->setTimeZone('UTC'); } else { $fromDate = self::$_dateRange['start']; $toDate = self::$_dateRange['end']; } $productPrices = ProductPrice::getAllByCriteria('updated >= :fromDate and updated < :toDate', array('fromDate' => trim($fromDate), 'toDate' => trim($toDate))); $return = array(); foreach ($productPrices as $productPrice) { if (!($product = $productPrice->getProduct()) instanceof Product) { continue; } if (!isset($return[trim($product->getSku())])) { $return[trim($product->getSku())] = self::_getDefaultData($product); } if (trim($productPrice->getType()->getId()) === trim(ProductPriceType::ID_RRP)) { $return[trim($product->getSku())]['price'] = $productPrice->getPrice(); } else { if (trim($productPrice->getType()->getId()) === trim(ProductPriceType::ID_CASUAL_SPECIAL)) { $return[trim($product->getSku())]['special_price'] = $productPrice->getPrice(); $return[trim($product->getSku())]['special_from_date'] = trim($productPrice->getStart()); $return[trim($product->getSku())]['special_to_date'] = trim($productPrice->getEnd()); } } } return $return; }
/** * getting the data * * @param string $preFix * @param string $debug * * @return array */ private static function _getData(UDate $lastUpdatedTime, $preFix = '', $debug = false) { self::_log('== Trying to get all the updated price for products:', __CLASS__ . '::' . __FUNCTION__, $preFix); //product prices $productPrices = ProductPrice::getAllByCriteria('updated > ?', array(trim($lastUpdatedTime))); self::_log('GOT ' . count($productPrices) . ' Price(s) that has changed after "' . trim($lastUpdatedTime) . '".', '', $preFix); $products = array(); foreach ($productPrices as $productPrice) { if (!$productPrice->getProduct() instanceof Product || array_key_exists($productPrice->getProduct()->getId(), $products)) { continue; } $products[$productPrice->getProduct()->getId()] = $productPrice->getProduct(); } //products $productArr = Product::getAllByCriteria('updated > ?', array(trim($lastUpdatedTime)), true); self::_log('GOT ' . count($productArr) . ' Product(s) that has changed after "' . trim($lastUpdatedTime) . '".', '', $preFix); foreach ($productArr as $product) { if (array_key_exists($product->getId(), $products)) { continue; } $products[$product->getId()] = $product; } //Product_Category $productCates = Product_Category::getAllByCriteria('updated > ?', array(trim($lastUpdatedTime))); self::_log('GOT ' . count($productCates) . ' Product_Category(s) that has changed after "' . trim($lastUpdatedTime) . '".', '', $preFix); foreach ($productCates as $productCate) { if (!$productCate->getProduct() instanceof Product || array_key_exists($productCate->getProduct()->getId(), $products)) { continue; } $products[$productCate->getProduct()->getId()] = $productCate->getProduct(); } //ProductImage $productImages = ProductImage::getAllByCriteria('updated > ? and active = 1', array(trim($lastUpdatedTime))); self::_log('GOT ' . count($productCates) . ' ProductImage(s) that has changed after "' . trim($lastUpdatedTime) . '".', '', $preFix); foreach ($productImages as $productImage) { if (!$productImage->getProduct() instanceof Product || array_key_exists($productImage->getProduct()->getId(), $products)) { continue; } $products[$productCate->getProduct()->getId()] = $productCate->getProduct(); } return $products; }
/** * updateproduct price * * @param unknown $sender * @param unknown $param */ public function updatePrice($sender, $param) { $results = $errors = array(); try { Dao::beginTransaction(); $id = isset($param->CallbackParameter->productId) ? $param->CallbackParameter->productId : ''; if (!($product = Product::get($id)) instanceof Product) { throw new Exception('Invalid product!'); } if (!isset($param->CallbackParameter->newPrice)) { throw new Exception('No New Price Provided!'); } $isSpecial = 0; if (isset($param->CallbackParameter->isSpecial)) { $isSpecial = intval($param->CallbackParameter->isSpecial); } $newPrice = StringUtilsAbstract::getValueFromCurrency(trim($param->CallbackParameter->newPrice)); if ($isSpecial === 0) { $priceType = ProductPriceType::ID_RRP; } else { $priceType = ProductPriceType::ID_CASUAL_SPECIAL; } $prices = ProductPrice::getAllByCriteria('productId = ? and typeId = ?', array($product->getId(), $priceType), true, 1, 1); if (count($prices) > 0) { $msg = 'Update price for product(SKU=' . $product->getSku() . ') to ' . StringUtilsAbstract::getCurrency($newPrice); $price = $prices[0]; } else { $msg = 'New Price Created for product(SKU=' . $product->getSku() . '): ' . StringUtilsAbstract::getCurrency($newPrice); $price = new ProductPrice(); $price->setProduct($product)->setType(ProductPriceType::get($priceType)); } $price->setPrice($newPrice)->save()->addComment($msg, Comments::TYPE_NORMAL)->addLog($msg, Log::TYPE_SYSTEM); $product->addComment($msg, Log::TYPE_SYSTEM)->addLog($msg, Log::TYPE_SYSTEM); $results['item'] = $product->getJson(); Dao::commitTransaction(); } catch (Exception $ex) { Dao::rollbackTransaction(); $errors[] = $ex->getMessage() . $ex->getTraceAsString(); } $param->ResponseData = StringUtilsAbstract::getJson($results, $errors); }