/** * Removes prices on the future effective date * * @param string $platform * @param string $cloudLocation * @param string $effectiveDate The date when the prices will be applied * @param string $url optional */ public function xDeleteAction($platform, $cloudLocation, $effectiveDate, $url = '') { list($curDate, $effectiveDate) = $this->handleEffectiveDate($effectiveDate); if ($effectiveDate <= $curDate) { throw new OutOfRangeException(sprintf("It is forbidden to remove prices either on the past or ongoing day.")); } $service = $this->getContainer()->analytics->prices; $entity = PriceHistoryEntity::findOne([['platform' => $platform], ['url' => $service->normalizeUrl($url) ?: ''], ['cloudLocation' => $cloudLocation], ['applied' => $effectiveDate], ['accountId' => $this->user->getAccountId() ?: 0]]); if ($entity !== null) { $cadb = $this->getContainer()->cadb; try { $cadb->BeginTrans(); $cadb->Execute("DELETE FROM `prices` WHERE `price_id` = " . $entity->qstr('priceId', $entity->priceId)); $entity->delete(); $cadb->CommitTrans(); } catch (Exception $e) { $cadb->RollbackTrans(); $this->response->failure(sprintf("Database error")); return; } $this->response->success('Prices have been successfully removed'); return; } throw new NotFoundException("Could not find any price with specified parameters"); }
/** * Saves price * * @param PriceHistoryEntity $price The PriceHistoryEntity with details set. * @throws \InvalidArgumentException * @throws \ADODB_Exception */ public function save(PriceHistoryEntity $price) { if (!isset($price->platform) || !isset($price->cloudLocation)) { throw new \InvalidArgumentException(sprintf("Both platform and cloudLocation properties must be set")); } if (!isset($price->applied)) { $price->applied = new \DateTime('now', new \DateTimeZone('UTC')); } else { if (!$price->applied instanceof \DateTime) { $price->applied = new \DateTime($price->applied, new \DateTimeZone('UTC')); } } if (!$price->priceId) { //Trying to find if the price already exists on this day $found = PriceHistoryEntity::findOne([['platform' => $price->platform], ['url' => $price->url ?: ''], ['cloudLocation' => $price->cloudLocation], ['accountId' => $price->accountId ?: 0], ['applied' => $price->applied->format('Y-m-d')]]); if ($found) { $price->priceId = $found->priceId; } } if (!$price->priceId) { $bNew = true; $price->priceId = \Scalr::GenerateUID(); } else { $bNew = false; } $sId = "`price_id` = " . $price->qstr('priceId'); $this->cadb->BeginTrans(); try { $this->cadb->Execute("\n " . ($bNew ? "INSERT" : "UPDATE") . " " . $price->table() . "\n SET " . ($bNew ? $sId . "," : "") . "\n platform = ?,\n url = ?,\n cloud_location = ?,\n account_id = ?,\n applied = ?,\n deny_override = ?\n " . ($bNew ? "" : "WHERE " . $sId . " LIMIT 1") . "\n ", [$price->platform, $price->url ?: '', $price->cloudLocation, $price->accountId ?: 0, $price->applied->format('Y-m-d'), $price->denyOverride ? 1 : 0]); //Removes previous values if (!$bNew) { $this->cadb->Execute("DELETE FROM `prices` WHERE price_id = " . $price->qstr('priceId')); } $stmt = ""; foreach ($price->getDetails() as $priceEntity) { if ($priceEntity instanceof PriceEntity) { $stmt .= ", (" . $price->qstr('priceId') . ", " . $priceEntity->qstr('instanceType') . ", " . $priceEntity->qstr('os') . ", " . $priceEntity->qstr('name') . ", " . $priceEntity->qstr('cost') . ")"; } else { throw new \InvalidArgumentException(sprintf("Details should contain collection of the PriceEntity objects. %s given.", gettype($priceEntity))); } } if ($stmt !== '') { $this->cadb->Execute("REPLACE `prices` (`price_id`,`instance_type`, `os`, `name`, `cost`) VALUES " . ltrim($stmt, ',')); } $this->cadb->CommitTrans(); } catch (\Exception $e) { $this->cadb->RollbackTrans(); throw $e; } }