/**
  * Get an specific report by its Id
  */
 public function getAction()
 {
     $output = $this->getRequest()->getParam('output', 'json');
     $type = $this->getRequest()->getParam('id');
     // Custom download report name
     if ($type !== ReportModel::KPI_MONTHLY) {
         throw new InvalidArgumentException("Only Report KPI/ARPU available.");
     }
     // Check report permissions by type
     $dumpReport = new ReportModel();
     $dumpReport->setType($type);
     $this->_helper->allowed('read', $dumpReport);
     if ($output !== 'json') {
         throw new InvalidArgumentException("Only JSON output available.");
     }
     $helper = $this->_helper->output('Stream_Json');
     // Load report
     $params = $this->getRequest()->getParams();
     $params['type'] = $type;
     $this->view->data = \Application\Service\ReportService::getInstance()->load($type, $params);
     if (is_null($this->view->data)) {
         throw new \Application\Exceptions\NotFoundException("Report not available");
     }
     if (isset($params['requiredFields'])) {
         $requiredFields = explode(':', $params['requiredFields']);
         $this->view->setAllowedFields($requiredFields);
     }
     if (isset($params['skipEmptyItems']) && $params['skipEmptyItems']) {
         $this->view->setSkipEmptyItems(true);
     }
     $helper->addHeaders();
     $this->getResponse()->sendHeaders();
     $this->view->render('');
     $this->_helper->forceExit();
 }
 /**
  * Get an specific report by its Id
  */
 public function getAction()
 {
     $params = $this->getRequest()->getQuery();
     $params['id'] = $this->getRequest()->getParam('id');
     $params['reportType'] = $params['id'];
     // Mapping report name (a mistake on spec)
     if ($params['id'] === 'charges_detail_monthly') {
         $params['id'] = ReportModel::EXPENSE_DETAIL_MONTHLY;
         $params['reportType'] = ReportModel::EXPENSE_DETAIL_MONTHLY;
     } else {
         if ($params['id'] === 'charges_detail_daily') {
             $params['id'] = ReportModel::EXPENSE_DETAIL_DAILY;
             $params['reportType'] = ReportModel::EXPENSE_DETAIL_DAILY;
         }
     }
     // Check report permissions by type
     $dumpReport = new ReportModel();
     $dumpReport->setType($params['id']);
     try {
         $this->_helper->allowed('read', $dumpReport);
     } catch (ForbiddenException $ex) {
         \App::log()->crit($ex);
         if ($dumpReport->getResourceId() == 'report') {
             \App::log()->crit("Invalid report type: " . $params['id']);
         }
         throw new NotAllowedException('List report Operation is not allowed: Customer is not allowed');
     }
     // Check report params
     $this->_reportSrv->validateParams($params['id'], $params);
     // Check report organization
     if (isset($params['orgId'])) {
         try {
             $org = OrgService::getInstance()->load($params['orgId']);
         } catch (Exception $ex) {
             throw new \Application\Exceptions\InvalidArgumentException("Invalid parameter value: " . ReportFilterFields::ORGANIZATION . ". Supported values are customer-xxxxx");
         }
         if (empty($org)) {
             throw new NotFoundException("Resource " . ReportFilterFields::ORGANIZATION . " does not exists");
         }
         try {
             $this->_helper->allowed('read', $org);
         } catch (ForbiddenException $ex) {
             throw new NotAllowedException('List report Operation is not allowed: Customer is not allowed');
         }
     }
     // Check report exists
     if (!$this->_reportSrv->validateReportExists($params)) {
         throw new \Application\Exceptions\NotFoundException("Resource report does not exist");
     }
     // Prepare report download
     $downloadToken = new DownloadTokenModel();
     $downloadToken->params = $params;
     $downloadToken->controller = 'report';
     $downloadToken->action = 'get';
     $this->_downloadTokenSrv->create($downloadToken);
     $this->view->resultURL = $downloadToken->url;
 }
 /**
  * @expectedException \Application\Exceptions\NotAllowedException
  * @expectedExceptionMessage Create report Operation is not allowed: Too many reports created. The limit is 20 generated reports
  */
 public function testCreate()
 {
     $reportModel = $this->getMock('Application\\Model\\Stream\\StreamResultModel', array());
     $type = ReportModel::ACCUMULATED_SUMMARY_DAILY;
     $dumpReport = new ReportModel();
     $dumpReport->setType($type);
     $reportModel->expects($this->any())->method('count')->will($this->returnValue(20));
     $this->_reportService->setListAll($reportModel);
     $this->_reportService->create($dumpReport);
 }
 /**
  * Get an specific report by its Id
  */
 public function getAction()
 {
     $type = $this->_downloadToken->getParam('id');
     // Check report permissions by type
     $dumpReport = new ReportModel();
     $dumpReport->setType($type);
     $this->_helper->allowed('read', $dumpReport);
     // Custom download report name
     switch ($type) {
         case ReportModel::KPI_MONTHLY:
             $fileType = 'arpu';
             break;
         default:
             $fileType = $type;
     }
     $helper = $this->_helper->output('Stream_Csv');
     $helper->setFilename('report-' . $fileType . '.csv');
     // Load report
     $params = $this->_downloadToken->getParams();
     $params['type'] = $type;
     $this->view->data = $this->_reportSrv->load($type, $params);
     if (is_null($this->view->data)) {
         throw new \Application\Exceptions\NotFoundException("Report not available");
     }
     if (is_array($this->view->data)) {
         $this->view->data = array_shift($this->view->data);
     }
     // Render it?
     if (is_string($this->view->data)) {
         $extension = pathinfo($this->view->data, PATHINFO_EXTENSION);
         $this->_helper->sendFile($this->view->data, null, 'report-' . $fileType . '.' . $extension);
         unset($this->view->data);
         $this->_helper->viewRenderer->setNoRender(true);
         return;
     }
     $headers = $this->_reportSrv->getHeaders($type, $params);
     $dumpReport->setHeaders($headers);
     $this->_helper->filterNotAllowedCols("read_field", $dumpReport);
     $this->view->setHeaders($dumpReport->headers);
     $this->view->setFileHeaders($this->_reportSrv->getFileHeaders($type));
     $this->view->setFileFooters($this->_reportSrv->getFileFooters($type));
     $this->view->setFilters($this->_reportSrv->getViewFilters($type));
     $helper->addHeaders();
     $this->getResponse()->sendHeaders();
     $this->view->render('');
     $this->_helper->forceExit();
 }
 public function postAction()
 {
     $reqData = $this->_helper->requestData();
     $reqKeys = array_keys($reqData);
     if (count($reqKeys) != 1) {
         throw new InvalidArgumentException("Report type is required as a main key");
     }
     $type = $reqKeys[0];
     $params = $reqData[$type];
     // Check report permissions by type
     $dumpReport = new ReportModel();
     $dumpReport->setType($type);
     if ($type !== ReportModel::AUDIT_LOG) {
         $this->_helper->allowed('create', $dumpReport);
     } else {
         $dumpAudit = new \Application\Model\AuditLogModel();
         $this->_helper->allowed('create', $dumpAudit);
     }
     // Custom download report name
     switch ($type) {
         case ReportModel::KPI_MONTHLY:
             $fileType = 'arpu';
             break;
         default:
             $fileType = $type;
     }
     if ($type !== ReportModel::AUDIT_LOG) {
         try {
             $headers = ReportAsyncService::getInstance()->getHeaders($type, $params);
             $dumpReport->setHeaders($headers);
             $this->_helper->filterNotAllowedCols("read_field", $dumpReport);
             $columns = array_keys($dumpReport->headers);
             if (isset($params['requiredFields'])) {
                 $columns = array_intersect($columns, $params['requiredFields']);
             }
             $params['columns'] = $columns;
         } catch (InvalidArgumentException $e) {
             \App::log()->info("Report {$type} does not have defined headers");
         }
     }
     $params['fileType'] = $fileType;
     $dumpReport->params = $params;
     //validate organization exists
     $validateOrgExist = new App_Validate_Ericsson_OrganizationExistsById();
     if (isset($params['orgId'])) {
         $result = $validateOrgExist->isValid($params['orgId']);
         if (!$result) {
             throw new InvalidArgumentException("Invalid parameter value: orgId. Supported values are orgId belonging to the current organization");
         }
     }
     try {
         $this->_service->create($dumpReport);
     } catch (Application\Exceptions\NotFoundException $e) {
         if ($e->getMessage() == "Resource supervisionGroup does not exists") {
             throw new InvalidArgumentException("Invalid parameter value supervisionGroup");
         }
         throw $e;
     }
     $url = $this->getFrontController()->getRouter()->assemble(array('controller' => $this->getRequest()->getControllerName(), 'action' => $this->getRequest()->getActionName(), 'id' => $dumpReport->getId()));
     $this->getResponse()->setHeader('Location', $url);
     $this->getResponse()->setHttpResponseCode(202);
 }
 /**
  * Validate date params
  *
  * @param array  $params
  * @param string $reportType
  * @param int    $daysLimit  (optional)
  */
 protected function _validateReportDates(array &$params, $reportType, $daysLimit = null)
 {
     // Date format (YYYY-MM or YYYY-MM-DD)
     if (ReportModel::isMonthly($reportType)) {
         $format = 'Y-m';
     } else {
         if (ReportModel::isDaily($reportType)) {
             $format = 'Y-m-d';
         }
     }
     foreach (array('date', 'start', 'end') as $date) {
         if (isset($params[$date]) && !\DateTime::createFromFormat($format, $params[$date])) {
             throw new InvalidArgumentException("Invalid parameter value: {$date}. Supported values are using {$format} format");
         }
     }
     // BillingCycleStartDay for EXPENSE_DETAIL_MONTHLY REPORTS
     if ($reportType === ReportModel::EXPENSE_DETAIL_MONTHLY && isset($params['billingcyclestartday'])) {
         $billingCycleStartDay = $params['date'] . '-' . $params['billingcyclestartday'];
         if (!\DateTime::createFromFormat('Y-m-d', $billingCycleStartDay)) {
             throw new InvalidArgumentException("Invalid parameter value: {$date}. Supported values are using {$format} format");
         }
         $params['datebillingcyclestart'] = $billingCycleStartDay;
     }
     // Date interval (start < end)
     if (isset($params['start']) && isset($params['end'])) {
         $d1 = new \DateTime($params['start']);
         $d2 = new \DateTime($params['end']);
         $interval = $d1->diff($d2);
         if ($interval->invert) {
             throw new InvalidArgumentException('Invalid parameter value: end. Supported values are greater than start');
         }
         if ($daysLimit && $interval->days > $daysLimit) {
             throw new InvalidArgumentException("Invalid parameter value: end. Supported values are periods between 1 and {$daysLimit} days");
         }
     }
 }
 public function postAction()
 {
     $params = $this->_helper->requestData();
     if (!isset($params['type'])) {
         throw new InvalidArgumentException("Report type is required");
     }
     $type = $params['type'];
     // Check report permissions by type
     $dumpReport = new ReportModel();
     $dumpReport->setType($type);
     // Custom download report name
     switch ($type) {
         case ReportModel::KPI_MONTHLY:
             $fileType = 'arpu';
             break;
         default:
             $fileType = $type;
     }
     if ($type !== ReportModel::AUDIT_LOG) {
         try {
             $headers = ReportAsyncService::getInstance()->getHeaders($type, $params);
             $dumpReport->setHeaders($headers);
             $this->_helper->filterNotAllowedCols("read_field", $dumpReport);
             $columns = array_keys($dumpReport->headers);
             if (isset($params['requiredFields'])) {
                 $columns = array_intersect($columns, $params['requiredFields']);
             }
             $params['columns'] = $columns;
         } catch (InvalidArgumentException $e) {
             \App::log()->info("Report {$type} does not have defined headers");
         }
     }
     $params['fileType'] = $fileType;
     $dumpReport->params = $params;
     if ($type !== ReportModel::AUDIT_LOG) {
         $this->_helper->allowed('create', $dumpReport);
     } else {
         $dumpAudit = new \Application\Model\AuditLogModel();
         $this->_helper->allowed('create', $dumpAudit);
     }
     $this->view->watcher = $this->_service->create($dumpReport);
     if (is_null($this->view->watcher)) {
         throw new NotFoundException("Report not available");
     }
     $this->view->watcher->reload();
     $this->_helper->filterNotAllowedFields('read_field', $this->view->watcher);
 }
 public function testIsDailyReturnsTrueIfReportTypeContainsDailyString()
 {
     $this->assertTrue(ReportModel::isDaily('expense_detail_daily'));
 }