public function addRoundTripToCache($row, $url) { $price = ceil($row->ReturnPrice); $codesFrom = explode('*', $row->OutboundWebsiteFlightCode); $codesTo = explode('*', $row->InboundWebsiteFlightCode); if (sizeof($codesFrom) > 1 && sizeof($codesTo) > 1) { $airline = $codesFrom[2]; $depAirportDate = $codesFrom[1]; $arrAirportDate = end($codesFrom); $fromAirp = Airport::model()->findByAttributes(array('code' => substr($depAirportDate, 0, 3))); if (!$fromAirp) { echo "Airport {$depAirportDate} not defined \n"; return false; } $toAirp = Airport::model()->findByAttributes(array('code' => substr($arrAirportDate, 0, 3))); if (!$toAirp) { echo "Airport {$arrAirportDate} not defined \n"; return false; } $from = $fromAirp->cityId; $to = $toAirp->cityId; } else { $airline = ''; $from = $this->intRouters[$url][0]; $to = $this->intRouters[$url][1]; if (!$from || !$to) { echo "Cities not defined \n"; return false; } } $timeFrom = strtotime($row->OutboundDepartureDate); $timeBack = strtotime($row->InboundDepartureDate); if ($timeFrom == 0 || $timeBack == 0) { echo "Time does not determined\n"; return false; } $dateFrom = date('Y-m-d', $timeFrom); $dateBack = date('Y-m-d', $timeBack); $flightCache = FlightCache::model()->findByAttributes(array('from' => $from, 'to' => $to, 'dateFrom' => $dateFrom, 'dateBack' => $dateBack)); if (!$flightCache) { echo "Creating record {$from} - {$to} - {$from} at {$dateFrom} <-> {$dateBack}. It is {$price}\n"; $flightCache = new FlightCache(); } else { if ($flightCache->priceBestPrice > $price) { echo "Updating record {$from} - {$to} - {$from} at {$dateFrom} <-> {$dateBack}. It was {$flightCache->priceBestPrice} become {$price}\n"; } else { return true; } } $flightCache->from = $from; $flightCache->to = $to; $flightCache->dateFrom = $dateFrom; $flightCache->dateBack = $dateBack; $flightCache->priceBestPrice = $price; $flightCache->transportBestPrice = $airline; $flightCache->validatorBestPrice = $airline; return $flightCache->save(); }
/** * addCacheFromStack * Adding caches into db, flights with best paraments(price,time,pricetime) * @param FlightVoyageStack $flightVoyageStack */ public static function addCacheFromStack(FlightVoyageStack $flightVoyageStack) { $flightCache = new FlightCache(); if (sizeof($flightVoyageStack->flightVoyages) == 0) { return false; } $firstVoyage = $flightVoyageStack->flightVoyages[0]; //we aren't saving complex voyage if ($firstVoyage->isComplex()) { Yii::log("COMPLEX FLIGHT - DOESNOT SAVE IT", 'info'); return; } $withReturn = count($firstVoyage->flights) == 2; //working on dates $flightCache->dateFrom = $firstVoyage->flights[0]->departureDate; if ($withReturn) { $flightCache->dateBack = $firstVoyage->flights[1]->departureDate; } //working on from and to cities $flightCache->from = $firstVoyage->getDepartureCity()->id; $flightCache->to = $firstVoyage->getArrivalCity()->id; if ($flightVoyageStack->bestPriceInd !== null) { $voyage = $flightVoyageStack->flightVoyages[$flightVoyageStack->bestPriceInd]; $flightCache->setFromFlightVoyage($voyage, 'BestPrice'); } if ($flightVoyageStack->bestTimeInd !== null) { $voyage = $flightVoyageStack->flightVoyages[$flightVoyageStack->bestPriceInd]; $flightCache->setFromFlightVoyage($voyage, 'BestTime'); } if ($flightVoyageStack->bestPriceTimeInd !== null) { $voyage = $flightVoyageStack->flightVoyages[$flightVoyageStack->bestPriceInd]; $flightCache->setFromFlightVoyage($voyage, 'BestPriceTime'); } if (!$flightCache->validate()) { Yii::log("AFTER CALCULATIONS WE GET BAD FLIGHT CACHE: \n" . CVarDumper::dumpAsString($flightCache->errors), 'info'); throw new CException("Can't save flight cache item." . print_r($flightCache->errors, true) . print_r($flightCache->attributes, true)); } Yii::log("TRYING TO SAVE FLIGHT CACHE", 'info'); $flightCache->save(); return $flightCache; }