예제 #1
0
 /**
  * Gets default search criteria
  *
  * @return array Returns array of the search criteria
  *
  * @throws ApiNotImplementedErrorException
  */
 public function getDefaultCriteria()
 {
     switch ($this->getScope()) {
         case ScopeInterface::SCOPE_ENVIRONMENT:
             return [['$and' => [['ccId' => $this->getEnvironmentCostCenterId()]]]];
         case ScopeInterface::SCOPE_ACCOUNT:
             $cc = new CostCentreEntity();
             $accs = new AccountCostCenterEntity();
             return [AbstractEntity::STMT_FROM => "{$cc->table()} LEFT JOIN {$accs->table()} AS `accs` ON {$accs->columnCcId('accs')} = {$cc->columnCcId()}", AbstractEntity::STMT_WHERE => "{$accs->columnAccountId('accs')} = " . $accs->qstr('accountId', $this->getUser()->accountId)];
         case ScopeInterface::SCOPE_SCALR:
             throw new ApiNotImplementedErrorException();
     }
 }
예제 #2
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');
 }
예제 #3
0
 /**
  * Finds cost centres by key
  * It searches by name or billing number
  *
  * @param   string    $key  optional Search key
  * @return  ArrayCollection Returns collection of the CostCentreEntity objects
  */
 public function findByKey($key = null)
 {
     if (is_null($key) || $key === '') {
         return $this->all();
     }
     $collection = new ArrayCollection();
     $ccEntity = new CostCentreEntity();
     $rs = $this->db->Execute("\n            SELECT " . $ccEntity->fields('c') . "\n            FROM " . $ccEntity->table('c') . "\n            WHERE c.`name` LIKE ?\n            OR EXISTS (\n                SELECT 1 FROM cc_properties cp\n                WHERE `cp`.cc_id = `c`.`cc_id`\n                AND `cp`.`name` = ? AND `cp`.`value` LIKE ?\n            )\n        ", ['%' . $key . '%', CostCentrePropertyEntity::NAME_BILLING_CODE, '%' . $key . '%']);
     while ($rec = $rs->FetchRow()) {
         $item = new CostCentreEntity();
         $item->load($rec);
         $collection->append($item);
     }
     return $collection;
 }
예제 #4
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)];
     }
 }
예제 #5
0
파일: Projects.php 프로젝트: rickb838/scalr
 /**
  * Finds projects by key
  * It searches by name or billing number
  *
  * @param   string    $key  optional Search key
  * @return  ArrayCollection Returns collection of the ProjectEntity objects
  */
 public function findByKey($key = null)
 {
     if (is_null($key) || $key === '') {
         return $this->all();
     }
     $collection = new ArrayCollection();
     $projectEntity = new ProjectEntity();
     //Includes archived projects
     $projectPropertyEntity = new ProjectPropertyEntity();
     //Cost center entity
     $ccEntity = new CostCentreEntity();
     $rs = $this->db->Execute("\n            SELECT " . $projectEntity->fields('p') . ", " . $ccEntity->fields('c', true) . "\n            FROM " . $projectEntity->table('p') . "\n            LEFT JOIN " . $ccEntity->table('c') . " ON c.`cc_id` = p.`cc_id`\n            WHERE p.`name` LIKE ?\n            OR EXISTS (\n                SELECT 1 FROM " . $projectPropertyEntity->table('pp') . "\n                WHERE `pp`.project_id = `p`.`project_id`\n                AND `pp`.`name` = ? AND `pp`.`value` LIKE ?\n            )\n        ", ['%' . $key . '%', ProjectPropertyEntity::NAME_BILLING_CODE, '%' . $key . '%']);
     while ($rec = $rs->FetchRow()) {
         $item = new ProjectEntity();
         $item->load($rec);
         if ($rec['c_cc_id']) {
             $cc = new CostCentreEntity();
             $cc->load($rec, 'c');
             $item->setCostCenter($cc);
         }
         $collection->append($item);
     }
     return $collection;
 }
예제 #6
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;
 }