예제 #1
0
 function launch()
 {
     global $configArray;
     global $interface;
     global $user;
     // Distributor Filter
     $allDistributors = $this->getAllDistributors();
     $interface->assign('distributorFilter', $allDistributors);
     $selectedDistributorFilter = null;
     if (isset($_REQUEST['distributorFilter'])) {
         $selectedDistributorFilter = $_REQUEST['distributorFilter'];
     }
     $interface->assign('selectedDistributorFilter', $selectedDistributorFilter);
     $distributors = empty($selectedDistributorFilter) ? $allDistributors : $selectedDistributorFilter;
     $interface->assign('distributors', $distributors);
     // Date range filter
     $period = isset($_REQUEST['period']) ? $_REQUEST['period'] : 'week';
     if ($period == 'week') {
         $periodLength = new DateInterval("P1W");
     } elseif ($period == 'day') {
         $periodLength = new DateInterval("P1D");
     } elseif ($period == 'month') {
         $periodLength = new DateInterval("P1M");
     } elseif ($period == 'year') {
         $periodLength = new DateInterval("P1Y");
     }
     $interface->assign('period', $period);
     $endDate = isset($_REQUEST['endDate']) && strlen($_REQUEST['endDate']) > 0 ? DateTime::createFromFormat('m/d/Y', $_REQUEST['endDate']) : new DateTime();
     $interface->assign('endDate', $endDate->format('m/d/Y'));
     if (isset($_REQUEST['startDate']) && strlen($_REQUEST['startDate']) > 0) {
         $startDate = DateTime::createFromFormat('m/d/Y', $_REQUEST['startDate']);
     } else {
         if ($period == 'day') {
             $startDate = new DateTime($endDate->format('m/d/Y') . " - 7 days");
         } elseif ($period == 'week') {
             //Get the sunday after this
             $endDate->setISODate($endDate->format('Y'), $endDate->format("W"), 0);
             $endDate->modify("+7 days");
             $startDate = new DateTime($endDate->format('m/d/Y') . " - 28 days");
         } elseif ($period == 'month') {
             $endDate->modify("+1 month");
             $numDays = $endDate->format("d");
             $endDate->modify(" -{$numDays} days");
             $startDate = new DateTime($endDate->format('m/d/Y') . " - 6 months");
         } elseif ($period == 'year') {
             $endDate->modify("+1 year");
             $numDays = $endDate->format("m");
             $endDate->modify(" -{$numDays} months");
             $numDays = $endDate->format("d");
             $endDate->modify(" -{$numDays} days");
             $startDate = new DateTime($endDate->format('m/d/Y') . " - 2 years");
         }
     }
     $interface->assign('startDate', $startDate->format('m/d/Y'));
     //Set the end date to the end of the day
     $endDate->setTime(24, 0, 0);
     $startDate->setTime(0, 0, 0);
     //Create the periods that are being represented
     $periods = array();
     $periodEnd = clone $endDate;
     while ($periodEnd >= $startDate) {
         array_unshift($periods, clone $periodEnd);
         $periodEnd->sub($periodLength);
     }
     // create a SQL clause to filter by selected distributors
     $distributorRestriction = null;
     if (isset($_REQUEST['distributorFilter'])) {
         $distributorsToShow = array();
         foreach ($_REQUEST['distributorFilter'] as $id) {
             if (is_numeric($id)) {
                 $distributorsToShow[] = $id;
             }
         }
         if (!empty($distributorsToShow)) {
             $distributorRestriction = "distributorId IN (" . implode(",", $distributorsToShow) . ") ";
         }
     }
     //Load data for each period
     $periodDataByDistributor = array();
     $periodDataByStatus = array();
     for ($i = 0; $i < count($periods) - 1; $i++) {
         $periodStart = clone $periods[$i];
         //$periodStart->setTime(0,0,0);
         $periodEnd = clone $periods[$i + 1];
         //$periodStart->setTime(23, 59, 59);
         $periodDataByDistributor[$periodStart->getTimestamp()] = array();
         $periodDataByStatus[$periodStart->getTimestamp()] = array();
         //Determine how many files were imported by distributor
         $packagingDetails = new PackagingDetailsEntry();
         $packagingDetails->selectAdd();
         $packagingDetails->selectAdd('COUNT(id) as numberOfFiles, distributorId');
         $packagingDetails->whereAdd('created >= ' . $periodStart->getTimestamp() . ' AND created < ' . $periodEnd->getTimestamp());
         if ($distributorRestriction) {
             $packagingDetails->whereAdd($distributorRestriction);
         }
         $packagingDetails->groupBy('distributorId');
         $packagingDetails->orderBy('distributorId');
         $packagingDetails->find();
         while ($packagingDetails->fetch()) {
             $periodDataByDistributor[$periodStart->getTimestamp()][$packagingDetails->distributorId] = $packagingDetails->numberOfFiles;
         }
         //Determine how many files were imported by status
         $packagingDetails = new PackagingDetailsEntry();
         $packagingDetails->selectAdd();
         $packagingDetails->selectAdd('COUNT(id) as numberOfFiles, status');
         $packagingDetails->whereAdd('created >= ' . $periodStart->getTimestamp() . ' AND created < ' . $periodEnd->getTimestamp());
         if ($distributorRestriction) {
             $packagingDetails->whereAdd($distributorRestriction);
         }
         $packagingDetails->groupBy('status');
         $packagingDetails->orderBy('status');
         $packagingDetails->find();
         while ($packagingDetails->fetch()) {
             $periodDataByStatus[$periodStart->getTimestamp()][$packagingDetails->status] = $packagingDetails->numberOfFiles;
         }
     }
     $interface->assign('periodDataByDistributor', $periodDataByDistributor);
     $interface->assign('periodDataByStatus', $periodDataByStatus);
     //Get a list of all of the statuses that will be shown
     $statusesUntranslated = $this->getStatuses();
     $statuses = array();
     foreach ($statusesUntranslated as $status) {
         $statuses[$status] = translate($status);
     }
     $interface->assign('statuses', $statuses);
     //Check to see if we are exporting to Excel
     if (isset($_REQUEST['exportToExcel'])) {
         $this->exportToExcel($periodDataByDistributor, $distributors, $periodDataByStatus, $statuses);
     } else {
         //Generate the graphs
         $this->generateGraphByDistributor($periodDataByDistributor, $periods, $distributors);
         $this->generateGraphByStatus($periodDataByStatus, $periods, $statuses);
     }
     $interface->setTemplate('packagingSummary.tpl');
     $interface->setPageTitle('Packaging Summary Report');
     $interface->display('layout.tpl');
 }