public function delete($model)
 {
     $type = get_class($model);
     $this->getMapper()->delete($model);
     \App::audit("Deleted {$type} with Id {$model->getId()}", $model);
     return true;
 }
 protected function _sendEvent($action, $model)
 {
     try {
         $event = $this->_createModelActionEvent($action, $model);
         WatcherService::getInstance()->publishEvent($event);
     } catch (\Exception $ex) {
         \App::log()->warn($ex);
     }
 }
 /**
  * Retrive the list of enum by type
  *
  * @param string $type
  */
 public function getEnum($type, $filters = array())
 {
     /**
      * @var \Application\Model\Mapper\EnumeratedMapper
      */
     $enumMapper = \Application\Model\Mapper\EnumeratedMapper::getInstance();
     if (!$type) {
         throw new Exceptions\InvalidArgumentException("Enum type is mandatory");
     }
     if ($filters && !is_array($filters)) {
         throw new Exceptions\InvalidArgumentException("filters is not an array");
     }
     switch ($type) {
         case self::COUNTRY_ENUM_NAME:
             return $enumMapper->listCountry();
         case self::CURRENCY_ENUM_NAME:
             return $enumMapper->listCurrency();
         case self::LANGUAGE_ENUM_NAME:
             return $enumMapper->listLanguage();
         case self::SECTOR_ENUM_NAME:
             return $enumMapper->listSector();
         case self::STATUS_ENUM_NAME:
             return $enumMapper->listStatus();
         case self::COMPANY_TYPE_ENUM_NAME:
             return $enumMapper->listCompanyType();
         case self::TIME_ZONE_STATIC_ENUM_NAME:
             return $enumMapper->listStaticTimeZone();
         case self::MNO_ENUM_NAME:
             return $enumMapper->listMNO();
         default:
             // Get service provider id
             if (isset($filters['serviceProvider'])) {
                 $orgId = $filters['serviceProvider'];
             } else {
                 $org = \App::getOrgUserLogged();
                 $orgId = OrgService::getInstance()->getServiceProviderLevelId($org);
             }
             // Get enum by service provider
             switch ($type) {
                 case self::RAID_ENUM_NAME:
                     return $enumMapper->listRaid($orgId);
                 case self::SERVICE_PROVIDER_ZONE_ENUM_NAME:
                     $zones = $enumMapper->listServiceProviderZone($orgId);
                     return $this->_filterDefaultValues($zones);
                 case self::SERVICE_PROVIDER_DESTINATION_ENUM_NAME:
                     $destinations = $enumMapper->listServiceProviderDestination($orgId);
                     return $this->_filterDefaultValues($destinations);
                 case self::TIME_ZONE_ENUM_NAME:
                     return $enumMapper->listTimeZone($orgId);
                 case self::SERVICE_PROVIDER_REGION_ENUM_NAME:
                     $regions = $enumMapper->listServiceProviderRegion($orgId);
                     return $this->_filterDefaultValues($regions);
                 default:
                     throw new AppEx\InvalidArgumentException("Enumerated " . $type . " doesn't exist");
             }
     }
 }
 public function unpublish($preBill)
 {
     if ($preBill instanceof PreBillModel) {
         $mapper = PreBillMapper::getInstance();
         $result = $mapper->unpublish($preBill->getId());
         if ($result) {
             \App::audit('Unpublished prebill with Id ' . $preBill->getId(), $preBill);
             return $result;
         }
     } else {
         throw new AppEx\ValidateException('Invalid prebill');
     }
 }
 /**
  * Builds a filter list based on params.
  * @param  array          $params
  * @return App_ListFilter | null
  */
 public function buildFilterList(array $params)
 {
     $factory = new \App_ListFilter_FilterFactory();
     $factory->setWhiteList(AlarmRuleFilterFields::getWhiteList());
     $filterList = $factory->constructFilter($params);
     $filterList->setResourceId(AlarmRuleFilterFields::getResourceId());
     if (!$filterList->isValid()) {
         $filterList = $factory->constructFilter(array());
     }
     $filterList->addExtraData('filterType', 'alarmRule');
     \App::log()->debug('Unused filters [' . implode(',', $factory->getUnusedList()) . ']');
     return $filterList;
 }
 /**
  * Builds a filter list based on params.
  * @param  array          $params
  * @return App_ListFilter | null
  */
 public function buildFilterList(array $params, $org = null)
 {
     if (empty($org)) {
         throw new InvalidArgumentException('No organization provided');
     }
     $params['organizationId'] = $org->getId();
     $factory = new \App_ListFilter_FilterFactory();
     $fields = TemplateFilterFields::getWhiteList();
     $factory->setWhiteList($fields);
     $filterList = $factory->constructFilter($params);
     $filterList->addExtraData('filterType', 'template');
     $filterList->setResourceId(TemplateFilterFields::getResourceId());
     \App::log()->debug('Unused filters [' . implode(',', $factory->getUnusedList()) . ']');
     return $filterList->isValid() ? $filterList : null;
 }
 public function modifyStatus($data, $orgId)
 {
     if ($data) {
         $mapper = $this->getMapper();
         if ($transactionId = $mapper->modifyStatusEricsson($data, $orgId)) {
             $sim = new SimModel(array($data['subscription_id']['id_type'] => $data['subscription_id']['id']));
             \App::audit('Lifecycle status modified successfully', $sim);
             return $transactionId;
         } else {
             throw new AppEx\InvalidArgumentException('Error modifying lifecycle status');
         }
     } else {
         throw new AppEx\ValidateException('Invalid lifecycle information');
     }
 }
 public function delete($id, $type = null)
 {
     if ($id instanceof TariffPlanLifeCycleModel || $id instanceof TariffPlanServicesModel) {
         $id = $id->getId();
         if ($id instanceof TariffPlanLifeCycleModel) {
             $type = self::TYPE_LIFECYCLE;
         } else {
             $type = self::TYPE_SERVICES;
         }
     }
     if (!is_string($id) || !strlen($id)) {
         throw new AppEx\InvalidArgumentException('Supplied tariff model must have Id');
     }
     switch (strtolower($type)) {
         case self::TYPE_LIFECYCLE:
             $tariff = new TariffPlanLifeCycleModel(array('id' => $id));
             break;
         case self::TYPE_SERVICES:
             $tariff = self::load(self::TYPE_SERVICES, $id);
             $zonePlan = ZonePlanService::getInstance()->load($tariff->zonePlanId);
             $zonePlan->delete();
             break;
         default:
             throw new AppEx\InvalidArgumentException("Invalid Type given");
     }
     $tariff->delete();
     if ($tariff instanceof TariffPlanLifeCycleModel) {
         \App::audit('Deleted life cycle tariff with Id ' . $tariff->getId(), $tariff);
     } else {
         \App::audit('Deleted services tariff with Id ' . $tariff->getId(), $tariff);
     }
     return true;
 }
 public function changeLteSync($state, $simId)
 {
     $this->_validateLteState($state);
     $state = filter_var($state, FILTER_VALIDATE_BOOLEAN);
     $result = $this->getMapper()->changeLteSync($state, $simId);
     \App::audit('Changed LTE status on sim to ' . $state . '. Handler: ' . $result, null);
     return $result;
 }
 public function buildFilterList(array $params, $throwEx = false)
 {
     $factory = new \App_ListFilter_FilterFactory();
     $factory->setWhiteList(CommercialGroupFilterFields::getWhiteList());
     $filterList = $factory->constructFilter($params);
     $filterList->setThrowExceptionOnValidationFail($throwEx);
     $filterList->setValidators(CommercialGroupFilterFields::getValidatorSpec());
     if (!$filterList->isValid()) {
         $filterList = new \App_ListFilter();
     }
     $filterList->setResourceId(CommercialGroupFilterFields::getResourceId());
     $filterList->addExtraData('filterType', 'commercialGroup');
     \App::log()->debug('Unused filters [' . implode(',', $factory->getUnusedList()) . ']');
     return $filterList;
 }
 /**
  *
  * @param  UserModel $user
  * @param  string    $reason
  * @return boolean
  */
 public function blockUser($user, $reason)
 {
     if ($user->getStatus() !== UserModel::USER_STATUS_BLOCKED) {
         // Block user
         $user->status = UserModel::USER_STATUS_BLOCKED;
         $user->save();
         \App::audit("User " . $user->getUserName() . " has been blocked due to {$reason}", $user);
         \App::log()->debug("User " . $user->getUserName() . " status changed to BLOCKED");
         $this->_sendEvent('update', $user);
         $this->_sendEvent('block', $user);
         return true;
     }
     return false;
 }
 public static function createModelActionEvent($action, PersistentAbstract $model)
 {
     $eventData = array('action' => $action);
     try {
         if (\App::getUserLogged() && \App::getUserLogged()->id) {
             $eventData['userId'] = \App::getUserLogged()->id;
         }
     } catch (\Exception $e) {
     }
     $event = new EventModel();
     $event->namespace = 'connectivity';
     $event->entityType = $model->getResourceId();
     $event->entityId = $model->id;
     $event->action = $action;
     $event->eventData = $eventData;
     $event->pushEventData = true;
     $event->hiddenData = array('model' => $model->exportData());
     return $event;
 }
 /**
  * @param  Application\Model\SupplServicesModel $service
  * @return boolean
  */
 public function removeCustomer(SupplServicesModel $service, OrgCustomerModel $org)
 {
     if (!strlen((string) $service->getId())) {
         throw new AppEx\InvalidArgumentException("Supplementary Services does not have an Id");
     }
     if (!$org instanceof OrgCustomerModel) {
         throw new AppEx\InvalidArgumentException("Organization must be a customer.");
     }
     $orgId = $org->getId();
     if (!isset($orgId) && !strlen($orgId)) {
         throw new AppEx\InvalidArgumentException("Organization customer does not have an Id");
     }
     if ($service->getPublished() != \Application\Model\SupplServicesModel::STATUS_PUBLISHED) {
         throw new AppEx\InvalidArgumentException("Supplementary Services must be published to have customers");
     }
     if ($service->subscriptionCount) {
         throw new AppEx\InvalidArgumentException("Customer sims are using supplementary services");
     }
     $result = $this->getMapper()->removeCustomer($service->getId(), $org->getId());
     \App::audit('Removed customer ' . $org->getId() . ' from supplementary services with Id ' . $service->getId(), $service);
     return $result;
 }
 public function countSims($supervGroup)
 {
     $filterList = SimService::getInstance()->buildFilterList(array(SimFilterFields::SUPERVISION_GROUP => $supervGroup->getId()));
     $simList = SimService::getInstance()->listAll($filterList, array('count' => 1), null, \App::getOrgUserLogged());
     return $simList->getCount();
 }
 public function deleteAll($filterList, array $options = array())
 {
     $itemList = $this->listAll($filterList, $options + array('paging' => array('count' => -1)));
     foreach ($itemList as $item) {
         try {
             $this->delete($item);
         } catch (\Exception $e) {
             \App::log()->warn($e);
         }
     }
 }
 /**
  * Create report
  *
  * @param $type string
  * @param $params array
  * @return models\ModelAbstract
  */
 public function createReport($type, $params = array())
 {
     $params[ReportFilterFields::REPORT_TYPE] = $type;
     switch ($type) {
         // Supervision
         case ReportModel::PRESENCE_MONTHLY:
         case ReportModel::PRESENCE_DAILY:
         case ReportModel::LOCATION_DAILY:
         case ReportModel::LOCATION_MONTHLY:
         case ReportModel::OPENED_SUP_ALARMS_DAILY:
         case ReportModel::OPENED_SUP_ALARMS_MONTHLY:
         case ReportModel::CLOSED_SUP_ALARMS_DAILY:
         case ReportModel::CLOSED_SUP_ALARMS_MONTHLY:
         case ReportModel::ACCUMULATED_SUMMARY_DAILY:
             $model = $this->createSupervisionReport($params, $type);
             break;
             // Expense and consumption
         // Expense and consumption
         case ReportModel::CONSUMPTION_DAILY:
             $model = $this->createConsumptionReport($params, $type);
             break;
         case ReportModel::EXPENSE_DETAIL_MONTHLY:
         case ReportModel::EXPENSE_DETAIL_DAILY:
         case ReportModel::CONSUMPTION_DETAIL_DAILY:
             $model = $this->createChargesDetailReport($params, $type);
             break;
             // Business
         // Business
         case ReportModel::SUBSCRIPTION_BASE_MONTHLY:
             $model = $this->createSubscriptionBaseReport($params, $type);
             break;
         case ReportModel::SUBSCRIPTION_SNAPSHOT_DAILY:
             $model = $this->createSubscriptionSnapshotReport($params, $type);
             break;
         case ReportModel::KPI_MONTHLY:
             $model = $this->createKpiReport($params, $type);
             break;
         case ReportModel::SMIP_CONSUMPTION_MONTHLY:
             $model = $this->createSmipConsumptionMonthly($params);
             break;
         case ReportModel::SMIP_CONSUMPTION_DAILY:
             $model = $this->createSmipConsumptionDaily($params);
             break;
         case ReportModel::CORRELATION:
             $model = $this->createCorrelationReport();
             break;
         default:
             throw new InvalidArgumentException('Invalid report type');
     }
     \App::audit("Created async report with Id {$model->id}", $model);
     return $model;
 }
 public function close($id, Attendance $attendance)
 {
     /**
      * @var $mapper AlarmMapper
      */
     $mapper = AlarmMapper::getInstance();
     if (!isset($id) && !strlen($id) || !$attendance) {
         throw new InvalidArgumentException("Invalid params");
     }
     $alarm = $mapper->findOneById($id);
     if ($alarm->state == AlarmModel::STATE_CLOSED) {
         throw new ValidateException("Can't close an alarm already closed", ValidationCodes::ALARM_ALREADY_CLOSED);
     }
     $this->validate($attendance, true);
     $result = $mapper->close($id, $attendance);
     \App::audit('Close alarm for alarmId:' . $id, $attendance);
     return $result;
 }
 /**
  * Builds a filter list based on params.
  * @param  array          $params
  * @return App_ListFilter | null
  */
 public function buildFilterList(array $params)
 {
     /**
      * Check if ICC parameter exists and if it's valid
      */
     /* REMOVED according to GLOBALPORTAL-12528 (https://jira.tid.es/browse/GLOBALPORTAL-12528)
        if (!empty($params['icc'])) {
            if (!$this->luhn($params['icc'])) {
                throw new AppEx\InvalidArgumentException('Invalid search query');
            }
        }
        */
     /**
      * Check if IMSI parameter exists and if it's valid
      */
     if (!empty($params['imsi'])) {
         $imsis = explode('<>', $params['imsi']);
         if (count($imsis) == 2) {
             /**
              * Is it a range of IMSIs?
              */
             /* REMOVED according to GLOBALPORTAL-12528 (https://jira.tid.es/browse/GLOBALPORTAL-12528)
                if (preg_match('/^[0-9]{15}$/', trim($imsis[0])) &&
                    preg_match('/^[0-9]{15}$/', trim($imsis[1]))) {
                */
             $params['imsi'] = $imsis[0] . '<>' . $imsis[1];
             /* REMOVED according to GLOBALPORTAL-12528 (https://jira.tid.es/browse/GLOBALPORTAL-12528)
                } else {
                    throw new AppEx\InvalidArgumentException('Invalid search query');
                }
                */
         } else {
             /* REMOVED according to GLOBALPORTAL-12528 (https://jira.tid.es/browse/GLOBALPORTAL-12528)
                if (!preg_match('/^[0-9]{15}$/', trim($params['imsi']))) {
                    throw new AppEx\InvalidArgumentException('Invalid search query');
                }
                */
         }
     }
     $factory = new \App_ListFilter_FilterFactory();
     $factory->setWhiteList(StockFilterFields::getWhiteList());
     $factory->getSortingWhiteList(StockSortingFields::getWhiteList());
     $filterList = $factory->constructFilter($params);
     if (!$filterList->isValid()) {
         $filterList = new \App_ListFilter();
     }
     $filterList->setResourceId(StockFilterFields::getResourceId());
     $filterList->addExtraData('filterType', 'stock');
     \App::log()->debug('Unused filters [' . implode(',', $factory->getUnusedList()) . ']');
     return $filterList;
 }
 /**
  * Validate that report organization is a customer
  *
  * @param array $params
  */
 protected function _validateCustomerOrg(array &$params, $type = null)
 {
     $orgId = \App::getOrgUserLogged()->id;
     switch (Mapper\OrganizationMapper::getTypeByOrgId($orgId)) {
         case Model\Organization\OrgCustomerModel::ORG_TYPE:
             $params[ReportFilterFields::ORGANIZATION] = $orgId;
             break;
         case Model\Organization\OrgServiceProviderModel::ORG_TYPE:
             $this->_validateMandatoryParams($params, array(ReportFilterFields::ORGANIZATION));
             break;
     }
     if (Mapper\OrganizationMapper::getTypeByOrgId($params[ReportFilterFields::ORGANIZATION]) !== Model\Organization\OrgCustomerModel::ORG_TYPE) {
         throw new InvalidArgumentException('Invalid parameter value: ' . ReportFilterFields::ORGANIZATION . '. Supported values are customer-xxxxx');
     }
 }
 public function delete($orgOrId)
 {
     if (!isset($orgOrId) && !strlen($orgOrId)) {
         throw new InvalidArgumentException('function param cannot be null');
     }
     if (!$orgOrId instanceof \Application\Model\OrgModelAbstract) {
         $org = $this->load($orgOrId);
     } else {
         $org = $orgOrId;
     }
     $validator = new \Application\Model\Validate\Organization\CustomerIsErasable();
     if (!$validator->isValid($org)) {
         throw new ValidateException("customer {$orgOrId} is not erasable", array('validationErrors' => $validator->getMessages()));
     }
     $type = $this->getChildrenTypeByOrg($org);
     $filterListOrgService = $this->buildFilterList(array('type' => $type, \Application\Model\Filter\OrgFilterFields::PARENT_ID => $org->getId()));
     if ($org->getType() != OrgAggregatorModel::ORG_TYPE) {
         $list = $this->listAll($type, array('filterList' => $filterListOrgService));
         $items = $list->getItems();
         if (count($items) > 0) {
             throw new InvalidArgumentException('The organization has ChildOrgs and can not be deleted');
         }
     }
     $templateService = TemplateService::getInstance();
     $userService = UserService::getInstance();
     $APPIdService = APIIdService::getInstance();
     $this->deleteOrgElements($org, $templateService);
     $this->deleteOrgElements($org, $userService);
     $this->deleteOrgElements($org, $APPIdService);
     $mapper = $this->getMapperByType($this->getTypeById($org->getId()));
     $result = $mapper->delete($org->getId());
     WatcherService::getInstance()->removeByScope('organization', $org->id);
     \App::audit('The organization with Id ' . $org->getId() . "has been deleted", $org);
     $this->_sendEvent('delete', $org);
     return $result;
 }