Example #1
0
 public function xSaveAction()
 {
     $this->request->defineParams(array('name' => array('type' => 'string', 'validator' => array(Scalr_Validator::NOEMPTY => true)), 'billingCode' => array('type' => 'string', 'validator' => array(Scalr_Validator::NOEMPTY => true, Scalr_Validator::ALPHANUM => true)), 'leadEmail' => array('type' => 'string', 'validator' => array(Scalr_Validator::NOEMPTY => true, Scalr_Validator::EMAIL => true))));
     if ($this->getParam('ccId')) {
         $cc = $this->getContainer()->analytics->ccs->get($this->getParam('ccId'));
         if (!$cc) {
             throw new Scalr_UI_Exception_NotFound();
         }
     } else {
         $cc = new CostCentreEntity();
     }
     if (!$this->request->validate()->isValid()) {
         $this->response->data($this->request->getValidationErrors());
         $this->response->failure();
         return;
     }
     $cc->name = $this->getParam('name');
     //Checks whether billing code specified in the request is already used in another Cost Centre
     $criteria = [['name' => CostCentrePropertyEntity::NAME_BILLING_CODE], ['value' => $this->getParam('billingCode')]];
     if ($cc->ccId !== null) {
         $criteria[] = ['ccId' => ['$ne' => $cc->ccId]];
     } else {
         //This is a new cost center.
         //We should set the email address and identifier of the user who creates the record.
         $cc->createdById = $this->user->id;
         $cc->createdByEmail = $this->user->getEmail();
     }
     $ccPropertyEntity = new CostCentrePropertyEntity();
     $record = $this->db->GetRow("\n            SELECT " . $cc->fields('c') . "\n            FROM " . $cc->table('c') . "\n            JOIN " . $ccPropertyEntity->table('cp') . " ON cp.cc_id = c.cc_id\n            WHERE " . $ccPropertyEntity->_buildQuery($criteria, 'AND', 'cp')['where'] . "\n            LIMIT 1\n        ");
     if ($record) {
         $found = new CostCentreEntity();
         $found->load($record);
     }
     if (!empty($found)) {
         throw new AnalyticsException(sprintf('Billing code "%s" is already used in Cost center "%s"', strip_tags($this->getParam('billingCode')), $found->name));
     }
     $this->db->BeginTrans();
     try {
         $cc->save();
         $cc->saveProperty(CostCentrePropertyEntity::NAME_BILLING_CODE, $this->getParam('billingCode'));
         $cc->saveProperty(CostCentrePropertyEntity::NAME_DESCRIPTION, $this->getParam('description'));
         $cc->saveProperty(CostCentrePropertyEntity::NAME_LEAD_EMAIL, $this->getParam('leadEmail'));
         $this->db->CommitTrans();
     } catch (Exception $e) {
         $this->db->RollbackTrans();
         throw $e;
     }
     $this->response->data(array('cc' => $this->getCostCenterData($cc, true)));
     $this->response->success('Cost center has been successfully saved');
 }
Example #2
0
 public function _billingCode($from, $to, $action)
 {
     switch ($action) {
         case static::ACT_CONVERT_TO_OBJECT:
             /* @var $from CostCentreEntity */
             $to->billingCode = $from->getProperty(CostCentrePropertyEntity::NAME_BILLING_CODE);
             break;
         case static::ACT_CONVERT_TO_ENTITY:
             /** @var $to CostCentreEntity */
             throw new NotYetImplementedException();
             break;
         case static::ACT_GET_FILTER_CRITERIA:
             $cc = new CostCentreEntity();
             $property = new CostCentrePropertyEntity();
             return [AbstractEntity::STMT_FROM => $cc->table() . " LEFT JOIN " . $property->table() . " ON {$property->columnCcId} = {$cc->columnCcId}", AbstractEntity::STMT_WHERE => "{$property->columnName} = '" . CostCentrePropertyEntity::NAME_BILLING_CODE . "' AND {$property->columnValue} = " . $property->qstr('value', $from->billingCode)];
     }
 }
Example #3
0
 /**
  * Also Removes Cost Centers properties generated for test
  *
  * {@inheritdoc}
  * @see Scalr\Tests\Functional\Api\ApiTestCase::tearDownAfterClass()
  */
 public static function tearDownAfterClass()
 {
     ksort(static::$testData, SORT_REGULAR);
     foreach (static::$testData as $priority => $data) {
         foreach ($data as $class => $ids) {
             if ($class === 'Scalr\\Stats\\CostAnalytics\\Entity\\CostCentreEntity') {
                 $ids = array_unique($ids, SORT_REGULAR);
                 foreach ($ids as $entry) {
                     /* @var $cc CostCentreEntity */
                     $cc = $class::findPk(...$entry);
                     if (!empty($cc)) {
                         try {
                             CostCentrePropertyEntity::deleteByCcId($cc->ccId);
                             AccountCostCenterEntity::deleteByCcId($cc->ccId);
                             $cc->delete();
                         } catch (\Exception $e) {
                             \Scalr::logException($e);
                         }
                     }
                 }
                 unset(static::$testData[$priority][$class]);
             }
         }
     }
     parent::tearDownAfterClass();
 }
Example #4
0
 /**
  * Loads all properties to entity
  */
 public function loadProperties()
 {
     $this->_properties = new ArrayCollection(array());
     foreach (CostCentrePropertyEntity::findByCcId($this->ccId) as $item) {
         $this->_properties[$item->name] = $item;
     }
 }
Example #5
0
 /**
  * Finds cost centres by key
  * It searches by name or billing number
  *
  * @param   string    $key          optional Search key
  * @param   array     $criteria     optional Search criteria
  * @param   bool      $ignoreCache  optional Should it ignore cache or not
  * @return  ArrayCollection Returns collection of the CostCentreEntity objects
  */
 public function findByKey($key = null, $criteria = null, $ignoreCache = false)
 {
     if (is_null($key) || $key === '') {
         return $this->all(false, $criteria, $ignoreCache);
     }
     $collection = new ArrayCollection();
     $ccEntity = new CostCentreEntity();
     $ccPropertyEntity = new CostCentrePropertyEntity();
     $projectEntity = new ProjectEntity();
     $where = '';
     $join = '';
     $this->parseFindCriteria($criteria, $join, $where);
     $rs = $this->db->Execute("\n            SELECT " . $ccEntity->fields('c') . "\n            FROM " . $ccEntity->table('c') . " " . $join . "\n            WHERE (c.`name` LIKE ?\n            OR EXISTS (\n                SELECT 1 FROM " . $ccPropertyEntity->table('cp') . "\n                WHERE `cp`.cc_id = `c`.`cc_id`\n                AND `cp`.`name` = ? AND `cp`.`value` LIKE ?\n            ))\n            " . $where . "\n        ", ['%' . $key . '%', CostCentrePropertyEntity::NAME_BILLING_CODE, '%' . $key . '%']);
     while ($rec = $rs->FetchRow()) {
         $item = new CostCentreEntity();
         $item->load($rec);
         $collection->append($item);
     }
     return $collection;
 }