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'); }
/** * Creates default Cost Center for the Hosted Scalr new account * * @param Scalr_Account $account The account object * @param Scalr_Account_User $user optional The account owner user * @return CostCentreEntity Returns a new Cost Center */ public function createHostedScalrAccountCostCenter(Scalr_Account $account, Scalr_Account_User $user = null) { if (!$user instanceof Scalr_Account_User) { $user = $account->getOwner(); } //New Cost Center should be created in account share mode $cc = new CostCentreEntity(); $cc->ccId = \Scalr::GenerateUID(); $cc->accountId = $account->id; $cc->createdByEmail = $user->getEmail(); $cc->name = "Cost Center " . $account->name . " (" . $account->id . ")"; $cc->createdById = $user->id; $cc->save(); $cc->saveProperty(CostCentrePropertyEntity::NAME_BILLING_CODE, "CC-" . $account->name); $cc->saveProperty(CostCentrePropertyEntity::NAME_DESCRIPTION, "This Cost Center was added automatically."); $cc->saveProperty(CostCentrePropertyEntity::NAME_LEAD_EMAIL, $user->getEmail()); $cc->saveProperty(CostCentrePropertyEntity::NAME_LOCKED, false); //A new Project which corresponds to Cost Center (in account share mode as well) $project = new ProjectEntity(); $project->projectId = \Scalr::GenerateUID(); $project->ccId = $cc->ccId; $project->name = "Project " . $account->name . " (" . $account->id . ")"; $project->accountId = $account->id; $project->createdByEmail = $user->getEmail(); $project->shared = ProjectEntity::SHARED_WITHIN_ACCOUNT; $project->createdById = $user->id; $project->save(); $project->saveProperty(ProjectPropertyEntity::NAME_BILLING_CODE, "PR-" . $account->name); $project->saveProperty(ProjectPropertyEntity::NAME_DESCRIPTION, "This Project was added automatically."); $project->saveProperty(ProjectPropertyEntity::NAME_LEAD_EMAIL, $user->getEmail()); if (\Scalr::getContainer()->analytics->enabled) { \Scalr::getContainer()->analytics->tags->syncValue($account->id, TagEntity::TAG_ID_COST_CENTRE, $cc->ccId, $cc->name); \Scalr::getContainer()->analytics->tags->syncValue($account->id, TagEntity::TAG_ID_PROJECT, $project->projectId, $project->name); } return $cc; }
/** * Initializes default cost centres and projects according to fixtures * * @return void */ public function initDefault() { $fixture = $this->fixture(); foreach ($fixture as $i => $c) { $ccId = key($c); $cc = CostCentreEntity::findPk($ccId); if (!$cc) { $cc = new CostCentreEntity(); $cc->ccId = $ccId; $cc->name = sprintf('Cost center %02d', $i + 1); $cc->save(); $cc->saveProperty(CostCentrePropertyEntity::NAME_DESCRIPTION, 'This is the automatically added cost center'); $cc->saveProperty(CostCentrePropertyEntity::NAME_BILLING_CODE, sprintf('CC-%02d', $i)); $cc->saveProperty(CostCentrePropertyEntity::NAME_LEAD_EMAIL, ''); $this->cadb->Execute("\n INSERT IGNORE `account_tag_values` (`account_id`, `tag_id`, `value_id`, `value_name`)\n VALUES (0, ?, ?, ?)\n ", [TagEntity::TAG_ID_COST_CENTRE, $cc->ccId, $cc->name]); } foreach ($fixture[$i][$ccId] as $j => $projectId) { $pr = ProjectEntity::findPk($projectId); if (!$pr) { $pr = new ProjectEntity(); $pr->projectId = $projectId; $pr->ccId = $ccId; $pr->name = sprintf('Project %d%d', $i, $j + 1); $pr->save(); $pr->saveProperty(ProjectPropertyEntity::NAME_DESCRIPTION, 'This is the automatically generated project'); $pr->saveProperty(ProjectPropertyEntity::NAME_BILLING_CODE, sprintf('PR-%02d-%02d', $i, $j)); $pr->saveProperty(ProjectPropertyEntity::NAME_LEAD_EMAIL, ''); $this->cadb->Execute("\n INSERT IGNORE `account_tag_values` (`account_id`, `tag_id`, `value_id`, `value_name`)\n VALUES (0, ?, ?, ?)\n ", [TagEntity::TAG_ID_PROJECT, $pr->projectId, $pr->name]); } } } //Assigns cost centre to each environment $res = $this->db->Execute("SELECT id FROM client_environments"); while ($rec = $res->FetchRow()) { try { $environment = \Scalr_Environment::init()->loadById($rec['id']); } catch (Exception $e) { continue; } $environment->setPlatformConfig(array(\Scalr_Environment::SETTING_CC_ID => $this->autoCostCentre($environment->id))); } //Assigns project to each farm $res = $this->db->Execute("SELECT id, env_id, clientid FROM farms"); while ($rec = $res->FetchRow()) { try { $dbFarm = DBFarm::LoadByID($rec['id']); } catch (Exception $e) { continue; } $dbFarm->SetSetting(DBFarm::SETTING_PROJECT_ID, $this->autoProject($dbFarm->EnvID, $dbFarm->ID)); } //Initializes servers properties $res = $this->db->Execute("\n SELECT DISTINCT s.server_id, s.env_id, s.farm_id\n FROM servers s\n LEFT JOIN server_properties p ON p.server_id = s.server_id AND p.name = ?\n LEFT JOIN server_properties p2 ON p2.server_id = s.server_id AND p2.name = ?\n WHERE p.server_id IS NULL OR p.`value` IS NULL\n OR (s.farm_id IS NOT NULL AND (p2.server_id IS NULL OR p2.`value` IS NULL))\n ", [SERVER_PROPERTIES::ENV_CC_ID, SERVER_PROPERTIES::FARM_PROJECT_ID]); while ($rec = $res->FetchRow()) { $ccid = $this->autoCostCentre($rec['env_id']); $this->db->Execute("\n INSERT `server_properties` (`server_id`, `name`, `value`)\n VALUE (?, ?, ?)\n ON DUPLICATE KEY UPDATE `value` = IFNULL(`value`, ?)\n ", [$rec['server_id'], SERVER_PROPERTIES::ENV_CC_ID, $ccid, $ccid]); //Farm may not exist for bundle task servers if ($rec['farm_id']) { $projectid = $this->autoProject($rec['env_id'], $rec['farm_id']); $this->db->Execute("\n INSERT `server_properties` (`server_id`, `name`, `value`)\n VALUE (?, ?, ?)\n ON DUPLICATE KEY UPDATE `value` = IFNULL(`value`, ?)\n ", [$rec['server_id'], SERVER_PROPERTIES::FARM_PROJECT_ID, $projectid, $projectid]); } } }