/**
  * Deletes the given commercial group
  */
 public function deleteAction()
 {
     $commGroupId = $this->getRequest()->getParam('id');
     $commGroup = $this->_cgSrv->load($commGroupId);
     if (empty($commGroup)) {
         throw new NotFoundException('CommercialGroup ' . $commGroupId . ' not found', 404);
     }
     // Check permissions
     $this->_helper->allowed('delete', $commGroup);
     // Remove the user
     $this->_cgSrv->delete($commGroup);
     $this->view->data = true;
 }
 protected function _getCommercialGroup($filterValue, $filterField = 'id', $filters = array())
 {
     try {
         $list = CommercialGroupService::getInstance()->listAll($filters);
         $list = $list->getItems() ?: array();
         foreach ($list as $item) {
             if ($item->{$filterField} === $filterValue) {
                 return $item;
             }
         }
     } catch (Application\Exceptions\GlobalServiceException $e) {
         $this->_error(self::ERROR_ON_CONNECTION, $filterValue);
         return false;
     }
 }
 public function screeninglistsAction()
 {
     if (!$this->getRequest()->isPut()) {
         throw new AppEx\InvalidArgumentException('Screening list update must be a put request');
     }
     $commercialGroup = $this->_getCommercialGroup();
     // Check permissions
     $this->_helper->allowed('update', $commercialGroup);
     $data = $this->_helper->requestData(true, false);
     if (empty($data)) {
         return;
     }
     $data = $this->_cgSrv->cleanDataForUpdateScreening($data);
     $data['id'] = $commercialGroup->getId();
     $commercialGroup = $commercialGroup->importData($data);
     $result = $this->_cgSrv->updateScreening($commercialGroup);
     // Response with the organization id
     //         $this->view->data = $result;
 }
 /**
  * isValid method
  *
  * @param (OrgModelAbstract) $org organization to validate if it has
  *                                commercialGroups or not
  *
  * @return null
  * @author Francisco Marcos <*****@*****.**>
  **/
 public function isValid($org)
 {
     if (!$org instanceof OrgModelAbstract) {
         throw new InvalidArgumentException('OrgModelAbstract class expected');
     }
     $filters = array();
     $service = CommercialGroupService::getInstance();
     $mapper = $service->getMapper();
     $filters['customerId'] = $org->id;
     $list = $mapper->findAll($filters);
     $orgAggregatorCount = count($list);
     $valid = (bool) (!count($list));
     if (!$valid) {
         $message = sprintf($this->_messageTemplates[self::MSG_HAS_COMMERCIALGROUPS], $org->id, $orgAggregatorCount);
         $this->setMessage($message, self::MSG_HAS_COMMERCIALGROUPS);
         $this->_error(self::MSG_HAS_COMMERCIALGROUPS);
     }
     return $valid;
 }
 public function poolsAction()
 {
     if (!$this->getRequest()->isGet()) {
         throw new AppEx\UnexpectedException("Resquest must be GET");
     }
     $id = $this->_getParam('id', null);
     if (!isset($id) && !strlen($id)) {
         throw new AppEx\InvalidArgumentException('You must indicate a commercial group id');
     }
     $commercialGroup = $this->_cgSrv->load($id);
     if (empty($commercialGroup)) {
         throw new NotFoundException("Commercial Group {$id} not found", 404);
     }
     // Check permissions
     $this->_helper->allowed('read', $commercialGroup);
     $pools = $this->_cgSrv->getPools($id);
     //check permissions for pools
     if ($pools) {
         $this->_helper->filterNotAllowedFields('read_field', $pools);
     }
     // Response with the list of pools
     $this->view->data = $pools;
 }
 public function imsiAction()
 {
     if (!$this->getRequest()->isPost()) {
         throw new AppEx\ForbiddenException("Files action must be a post request.");
     }
     $this->_commercialGroupService = CommercialGroupService::getInstance();
     $data = $this->_helper->requestData(true);
     $imsiList = array();
     if (isset($data['IMSI'])) {
         foreach ($data['IMSI'] as $id) {
             try {
                 if (empty($id)) {
                     $imsiList[] = $this->_getNotSuccessCase($id, SteeringListService::RESULT_MISSING_PARAMETER_CODE, 'IMSI');
                     continue;
                 }
                 if (!is_numeric($id)) {
                     $imsiList[] = $this->_getNotSuccessCase($id, SteeringListService::RESULT_REQUEST_VALUES_INCORRECT_CODE, 'IMSI');
                     continue;
                 }
                 $sim = $this->_simService->load($id, \App::getOrgUserLogged(), 'imsi');
                 if (is_null($sim)) {
                     $imsiList[] = $this->_getNotSuccessCase($id, SteeringListService::RESULT_IMSI_NOT_FOUND_CODE, 'IMSI');
                     continue;
                 }
                 $simOrgId = $sim->getServiceProviderCommercialId();
                 $backupOrgId = Application\Model\Mapper\ProtoAbstractMapper::$organizationId;
                 $comGroupid = $sim->getCommercialGroupId();
                 Application\Model\Mapper\ProtoAbstractMapper::$organizationId = $simOrgId;
                 $comm = $this->_commercialGroupService->load($comGroupid);
                 Application\Model\Mapper\ProtoAbstractMapper::$organizationId = $backupOrgId;
                 $spId = $comm->getServicePackId();
                 if ($spId) {
                     $st = $this->_service->loadByServicePack($spId);
                     if ($st) {
                         $imsi = array('IMSI' => $id, 'RESULT' => SteeringListService::RESULT_SUCCESS_CODE, 'DESCRIPTION' => SteeringListService::RESULT_SUCCESS_DESC);
                         if (isset($st->resId)) {
                             $imsi['RSP_ID'] = $st->resId;
                         }
                         if (isset($st->steeringType)) {
                             $imsi['STEERING_TYPE'] = $this->_getSteeringType($st->steeringType);
                         }
                         $logistics = $sim->getLogistics();
                         if (isset($logistics)) {
                             $imsi['CARD_TYPE'] = $logistics;
                         }
                         $imsi['MSISDN'] = isset($sim->msisdn) ? $sim->msisdn : 'UNKNOWN';
                         $imsiList[] = $imsi;
                         continue;
                     } else {
                         $imsiList[] = $this->_getNotSuccessCase($id, SteeringListService::RESULT_REQUEST_VALUES_INCORRECT_CODE, 'IMSI');
                         continue;
                     }
                 } else {
                     $imsiList[] = $this->_getNotSuccessCase($id, SteeringListService::RESULT_INTERNAL_ERROR_CODE, 'IMSI');
                     continue;
                 }
             } catch (Exception $ex) {
                 $imsiList[] = $this->_getNotSuccessCase($id, SteeringListService::RESULT_INTERNAL_ERROR_CODE, 'IMSI');
                 continue;
             }
         }
     }
     $this->view->IMSI_RES = $imsiList;
 }
 public function changeCommercialGroup($commercialGroupId, $data, $idType = SubscriptionId\IdType::ID)
 {
     $this->getCache()->clean();
     $methodName = 'simCommercialGroupChange';
     $watcher = $this->_constructWatcherToTransaction();
     $watcher->params->action = "simChangeCommercialGroup";
     $watcher->params->commercialGroupId = $commercialGroupId;
     $commGroup = Service\CommercialGroupService::getInstance()->load($commercialGroupId);
     $watcher->params->customerId = $commGroup->customerId;
     /**
      * @var $proto \Application\Proto\Inventory\ChangeCommercialGroupQuery
      */
     $proto = $this->_createSubscriptionsProto($methodName, $data, $idType, $watcher);
     $proto->setNewCommercialgroupId($commercialGroupId);
     // Return the transaction ID
     return $this->_sendAsyncRequest($methodName, $proto, array($commercialGroupId), array(), $watcher);
 }
 /**
  * Update a sim identified by its Id
  */
 public function putAction()
 {
     $sim = $this->_getSim();
     $attr = $this->getRequest()->getParam('attribute');
     $value = $this->getRequest()->getParam('value');
     if (is_null($value)) {
         throw new InvalidArgumentException("Invalid value.");
     }
     $this->_helper->allowed('update', $sim);
     switch ($attr) {
         case 'alias':
             $sim->setAlias($value);
             break;
         case 'billingAccount':
             throw new DeprecatedException('Parameter "billingAccount" is deprecated.');
             break;
         case 'customField1':
             $sim->setCustomField_1($value);
             break;
         case 'customField2':
             $sim->setCustomField_2($value);
             break;
         case 'customField3':
             $sim->setCustomField_3($value);
             break;
         case 'customField4':
             $sim->setCustomField_4($value);
             break;
         case 'commercialGroup':
             $commercialGroup = CommercialGroupService::getInstance()->load($value);
             $this->_helper->allowed('read', $commercialGroup);
             $this->_helper->allowed('assign_sim', $commercialGroup);
             $this->_simSrv->changeCommercialGroup($commercialGroup, array($sim->getId()));
             $this->getResponse()->setHttpResponseCode(204);
             return;
         case 'lifeCycleStatus':
             // Generic permission
             $this->_helper->allowed('change_lifecycle_status', $sim);
             // Validate status
             if (!in_array($value, LifeCycleModel::getStatuses())) {
                 throw new InvalidArgumentException("Invalid parameter value: {$attr}.");
             }
             // Status permission
             switch ($value) {
                 case LifeCycleModel::STATUS_RETIRED:
                 case LifeCycleModel::STATUS_SUSPENDED:
                     $this->_helper->allowed('change_lifecycle_status_to_suspended_or_retired', $sim);
                     break;
                 case LifeCycleModel::STATUS_DEACTIVATED:
                     $this->_helper->allowed('change_lifecycle_status_to_deactivated', $sim);
                     break;
                 case LifeCycleModel::STATUS_RESTORE:
                     $this->_helper->allowed('restore_suspended_lifecycle_status', $sim);
                     break;
             }
             // Change status
             $this->_simSrv->changeLifeCycleStateSync($sim->lifeCycleStatus, $value, $sim->getId());
             $this->getResponse()->setHttpResponseCode(204);
             return;
         default:
             throw new InvalidArgumentException("Invalid attribute");
     }
     $this->_simSrv->update($sim);
     $this->getResponse()->setHttpResponseCode(204);
 }
 public function init()
 {
     $this->_cgSrv = CommercialGroupService::getInstance();
 }
 /**
  * Update a sim identified by its Id
  */
 public function putAction()
 {
     $sim = $this->_getSim();
     $data = $this->_helper->requestData();
     if (is_object($data)) {
         $data = get_object_vars($data);
     }
     $this->_helper->allowed('update', $sim);
     $apnsInfoModified = false;
     foreach ($data as $attr => $value) {
         if (is_null($value)) {
             throw new InvalidArgumentException("Invalid parameter value: {$attr}.");
         }
         switch ($attr) {
             case 'alias':
                 $this->_helper->allowedField('update_field', 'alias', $sim);
                 $sim->setAlias($value);
                 break;
             case 'customField1':
                 $this->_helper->allowedField('update_field', 'customField_1', $sim);
                 $sim->setCustomField_1($value);
                 break;
             case 'customField2':
                 $this->_helper->allowedField('update_field', 'customField_2', $sim);
                 $sim->setCustomField_2($value);
                 break;
             case 'customField3':
                 $this->_helper->allowedField('update_field', 'customField_4', $sim);
                 $sim->setCustomField_3($value);
                 break;
             case 'customField4':
                 $this->_helper->allowedField('update_field', 'customField_4', $sim);
                 $sim->setCustomField_4($value);
                 break;
             case 'staticIp':
                 $this->_helper->allowedField('update_field', 'staticIpAddress', $sim);
                 $sim->setStaticIpAddress($value["ipv4"]);
                 $apnsInfoModified = true;
                 break;
             case 'staticApnIndex':
                 $this->_helper->allowedField('update_field', 'apns', $sim);
                 if ($value != -1) {
                     $value++;
                 }
                 $sim->setStaticIpApnIndex($value);
                 $apnsInfoModified = true;
                 break;
             case 'apn0':
             case 'apn1':
             case 'apn2':
             case 'apn3':
             case 'apn4':
             case 'apn5':
             case 'apn6':
             case 'apn7':
             case 'apn8':
             case 'apn9':
                 $this->_helper->allowedField('update_field', 'apns', $sim);
                 //pre validate apn
                 $apnValidator = new App_Validate_SimApn();
                 if ($value == '-') {
                     $value = "";
                 }
                 $isValid = $apnValidator->isValid($value, $sim->exportData());
                 if (!$isValid) {
                     throw new InvalidArgumentException("Invalid parameter value: {$attr}");
                 }
                 $apnId = substr($attr, 3, 1);
                 $apns = $sim->getApns();
                 if ($apns[$apnId] != $value) {
                     $apns[$apnId] = $value;
                     $sim->setApns($apns);
                     $apnsInfoModified = true;
                 }
                 break;
             case 'commercialGroup':
                 if (empty($value)) {
                     throw new InvalidArgumentException("Invalid parameter value: {$attr}. Commercial group must be a not empty string.");
                 }
                 $commercialGroup = CommercialGroupService::getInstance()->load($value);
                 $this->_helper->allowed('read', $commercialGroup);
                 $this->_helper->allowed('assign_sim', $commercialGroup);
                 if ($sim->customerId != $commercialGroup->customerId) {
                     throw new InvalidArgumentException("Invalid parameter value: {$attr}. Sim CustomerId distinct to commercial Group customerId");
                 }
                 // Change commercial group
                 $this->_simSrv->changeCommercialGroup($commercialGroup, array($sim->getId()), true);
                 // End request
                 $this->getResponse()->setHttpResponseCode(204);
                 $this->_helper->viewRenderer->setNoRender(TRUE);
                 return;
             case 'lifeCycleStatus':
                 // Generic permission
                 $this->_helper->allowed('change_lifecycle_status', $sim);
                 // Validate status
                 if (!in_array($value, LifeCycleModel::getStatuses())) {
                     throw new InvalidArgumentException("Invalid parameter value: {$attr}.");
                 }
                 // Status permission
                 switch ($value) {
                     case LifeCycleModel::STATUS_RETIRED:
                     case LifeCycleModel::STATUS_SUSPENDED:
                         $this->_helper->allowed('change_lifecycle_status_to_suspended_or_retired', $sim);
                         break;
                     case LifeCycleModel::STATUS_DEACTIVATED:
                         $this->_helper->allowed('change_lifecycle_status_to_deactivated', $sim);
                         break;
                     case LifeCycleModel::STATUS_RESTORE:
                         $this->_helper->allowed('restore_suspended_lifecycle_status', $sim);
                         break;
                 }
                 // Change status
                 $this->_simSrv->changeLifeCycleStateSync($sim->lifeCycleStatus, $value, $sim->getId());
                 // End request
                 $this->getResponse()->setHttpResponseCode(204);
                 $this->_helper->viewRenderer->setNoRender(TRUE);
                 return;
             case 'dailyConsumptionThreshold':
             case 'monthlyConsumptionThreshold':
                 // Check permission
                 $this->_helper->allowed('update_consumption_control', $sim);
                 // Modification
                 $limits = new \Application\Model\Sim\ConsumptionControlLimitModel($this->_getLimits($value, $attr === 'dailyConsumptionThreshold' ? 'Daily' : 'Monthly'));
                 $this->_simSrv->updateConsumptionControl($limits, $sim);
                 break;
             case 'monthlyExpenseLimit':
                 // Check permission
                 $this->_helper->allowed('async_sim_change_expenselimit', $sim);
                 // Modification
                 $value = $this->_mapLimitDecimal($sim, $value);
                 $limits = new \Application\Model\Sim\ExpenseControlLimitModel($this->_getLimits($value));
                 $this->_simSrv->changeExpenseLimit($limits, $sim);
                 break;
             case 'lteEnabled':
                 // Check permission
                 $this->_helper->allowed('changeLte', $sim);
                 //Modification
                 $this->_simSrv->changeLteSync($value, $sim->getId());
                 break;
             default:
                 throw new InvalidArgumentException("Invalid parameter value: {$attr}.");
         }
     }
     // When some data related with the apns are received we need to send all the related data
     // otherwise staticIpAddress is fixed to null to avoid to call to changeSimApn function and
     // staticIpApnIndex and Apns are fixed to null to avoid error validations.
     if (!$apnsInfoModified) {
         $sim->setStaticIpAddress(null);
         $sim->setStaticIpApnIndex(null);
         $sim->setApns(null);
     } else {
         $this->_validateAPNsInfo($sim, $data);
     }
     try {
         $this->_simSrv->update($sim);
     } catch (ValidateException $ex) {
         throw $this->_mapException($ex);
     }
     $this->getResponse()->setHttpResponseCode(204);
     $this->_helper->viewRenderer->setNoRender(TRUE);
 }
 /**
  * Change life cycle state for a list of sims or a list of filters
  */
 public function commercialGroupAction()
 {
     if (!$this->getRequest()->isPost()) {
         throw new AppEx\UnexpectedException("Resquest must be POST");
     }
     // Check permissions according to the data type
     $data = $this->_checkAndGetListData(null, true);
     if (empty($data['commercialGroupId'])) {
         throw new InvalidArgumentException('Bad Request. Malformed Json. parameter {commercialGroupId} is required');
     }
     // Check commercial group permissions
     $id = $data['commercialGroupId'];
     $commGroup = Service\CommercialGroupService::getInstance()->load($id);
     if (empty($commGroup)) {
         throw new AppEx\NotFoundException("Commercial group {$id} not found");
     }
     $this->_helper->allowed('read', $commGroup);
     $this->_helper->allowed('assign_sim', $commGroup);
     // Respond with the transaction ID
     $watcher = Service\SimService::getInstance()->changeCommercialGroup($commGroup, $data['list']);
     $this->_helper->filterNotAllowedFields('read_field', $watcher);
     $this->view->assign('watcher', $watcher);
 }