Exemple #1
0
 public static function getMatchPrices($companyAliases, $sku, $myPrice)
 {
     $myPrice = StringUtilsAbstract::getValueFromCurrency($myPrice);
     //initialize values
     $finalOutputArray = array('sku' => $sku, 'myPrice' => $myPrice, 'minPrice' => 0, 'companyPrices' => array());
     foreach ($companyAliases as $key => $value) {
         $finalOutputArray['companyPrices'][$key] = array('price' => 0, 'priceURL' => '', 'PriceMatchCompanyId' => $value['PriceMatchCompanyId']);
     }
     //getting actual values
     $productPriceArray = PriceMatchConnector::getMatchPrices($sku);
     foreach ($productPriceArray as $productPriceInfo) {
         $companyDetails = $productPriceInfo['PriceMatchCompany'];
         $companyDetails = $companyDetails->getCompanyAlias();
         if ($companyDetails === '') {
             continue;
         }
         foreach ($companyAliases as $key => $value) {
             if (is_array($value) === true && in_array(strtolower($companyDetails), array_map(create_function('$a', 'return strtolower($a);'), $value))) {
                 $price = str_replace(' ', '', str_replace('$', '', str_replace(',', '', $productPriceInfo['price'])));
                 if ($finalOutputArray['minPrice'] == 0 || $finalOutputArray['minPrice'] > $price) {
                     $finalOutputArray['minPrice'] = $price;
                 }
                 $finalOutputArray['companyPrices'][$key] = array('price' => $price, 'priceURL' => $productPriceInfo['url'], 'PriceMatchCompanyId' => $value['PriceMatchCompanyId']);
                 break;
             }
         }
     }
     $companyAliases = null;
     $price = null;
     $productPriceArray = null;
     //return the result
     $finalOutputArray['priceDiff'] = $finalOutputArray['myPrice'] - $finalOutputArray['minPrice'];
     return $finalOutputArray;
 }
 public function newRule($sender, $param)
 {
     $results = $errors = array();
     try {
         Dao::beginTransaction();
         $results = $param->CallbackParameter;
         if (!isset($param->CallbackParameter->productId) || !($product = Product::get(trim($param->CallbackParameter->productId))) instanceof Product) {
             throw new Exception('Invalid Product Id passed in, "' . $param->CallbackParameter->productId . '" given');
         }
         if (!isset($param->CallbackParameter->rule)) {
             throw new Exception('Invalid PriceMatchRule passed in, "' . $param->CallbackParameter->rule . '" given');
         }
         if (!isset($param->CallbackParameter->rule->active)) {
             throw new Exception('Must pass in active (bool) for PriceMatchRule, "' . $param->CallbackParameter->rule->active . '" given');
         } else {
             $active = $param->CallbackParameter->rule->active;
         }
         if ($active === true && (!isset($param->CallbackParameter->rule->company_id) || !($company = PriceMatchCompany::get(trim($param->CallbackParameter->rule->company_id))) instanceof PriceMatchCompany)) {
             throw new Exception('Invalid PriceMatchCompany Id passed in, "' . $param->CallbackParameter->rule->company_id . '" given');
         }
         if ($active === false && ($rule = ProductPriceMatchRule::getByProduct($product)) instanceof ProductPriceMatchRule) {
             $rule->setActive($active)->save();
             $results = $rule->getJson();
         } elseif ($active === true) {
             $rule = ProductPriceMatchRule::create($product, $company, trim($param->CallbackParameter->rule->price_from), trim($param->CallbackParameter->rule->price_to), trim($param->CallbackParameter->rule->offset));
             //file_put_contents('/tmp/datafeed/web.log', __FILE__ .':' . __FUNCTION__ . ':' . __LINE__ . ': run' . date('Y-m-d\TH:i:sP') . PHP_EOL, FILE_APPEND | LOCK_EX);
             PriceMatchConnector::run($product->getSku(), true);
             //file_put_contents('/tmp/datafeed/web.log', __FILE__ .':' . __FUNCTION__ . ':' . __LINE__ . ': getMinRecord' . date('Y-m-d\TH:i:sP'). PHP_EOL, FILE_APPEND | LOCK_EX);
             PriceMatchConnector::getMinRecord($product->getSku(), true);
             //file_put_contents('/tmp/datafeed/web.log', __FILE__ .':' . __FUNCTION__ . ':' . __LINE__ . ': getNewPrice' . date('Y-m-d\TH:i:sP') . PHP_EOL, FILE_APPEND | LOCK_EX);
             PriceMatchConnector::getNewPrice($product->getSku(), true, true);
             //file_put_contents('/tmp/datafeed/web.log', __FILE__ .':' . __FUNCTION__ . ':' . __LINE__ . ': end' . date('Y-m-d\TH:i:sP'). PHP_EOL, FILE_APPEND | LOCK_EX);
             $results = $rule->getJson();
         }
         Dao::commitTransaction();
     } catch (Exception $ex) {
         Dao::rollbackTransaction();
         $errors[] = $ex->getMessage();
     }
     $param->ResponseData = StringUtilsAbstract::getJson($results, $errors);
 }