public function generateFile()
 {
     echo "MANIKGARH Generator starts...\n";
     $CCDS = ChannelConfigurationDataStore::getInstance();
     $FDS = FolderDataStore::getInstance();
     $WQDS = WQDDataDataStore::getInstance();
     $folders = $FDS->FindByLocationSeqs("11");
     echo "<br>--------TotalFolders:" . count($folders);
     foreach ($folders as $folder) {
         $folSeq = $folder->getSeq();
         //$latestDataRows = $WQDS->getChannelsLatestInfo($folSeq);
         echo "FolderSeq:" . $folSeq . " FolderName:" . $folder->getFolderName() . "<br>\n";
         echo "<br>\n ToDateFromLatestData " . $latestDataRows['dated'];
         $channelConfigs = $CCDS->FindByFolder($folSeq);
         $toDateStr = date('Y/m/d H:00:00');
         $fromDateStr = date('Y/m/d H:00:00', strtotime('-1 hour'));
         //$toDateStr = "2014/02/15 18:00:00";
         //            $fromDateStr = "2014/02/15 17:00:00";
         $toDate = new DateTime($toDateStr);
         $fromDate = new DateTime($fromDateStr);
         echo "<br>From:" . $fromDateStr . "<br>\n";
         echo "To:" . $toDateStr . "<br>\n";
         $dataArray = $WQDS->getAllDataByFol($fromDateStr, $toDateStr, $folSeq);
         $channelNoArr = self::getChannelNumberNameArray(self::$channelsList, $channelConfigs);
         $addDatesData = array();
         foreach ($dataArray as $data) {
             $channelData = array();
             $dated = $data['wqdfiledatadated'];
             $chValues = array();
             $chStatuses = array();
             foreach ($channelNoArr as $key => $value) {
                 $vl = (double) $data["ch" . $key . "value"];
                 if ($vl < 0) {
                     $vl = "n.o";
                 }
                 array_push($chValues, $vl);
                 array_push($chStatuses, (double) $data["ch" . $key . "status"]);
             }
             $allDatesData[strtotime($dated)]['channelValue'] = $chValues;
             $allDatesData[strtotime($dated)]['channelStatuses'] = $chStatuses;
         }
         $channelArr = array();
         foreach ($channelNoArr as $chName) {
             array_push($channelArr, $chName);
         }
         $dataSlices = DateUtils::getDateSlicesByInterval($fromDate, $toDate, "15min");
         $dataRows = $WQDS->getAverageDataByDataArrayDataSlices("15min", $allDatesData, $dataSlices, $channelArr, "normal", array());
         $repStr = "";
         if (count($dataRows) > 1) {
             $repStr = "";
             foreach ($dataRows as $dateStr => $row) {
                 $chNameValueArr = array();
                 foreach ($row as $rowId => $rowVal) {
                     $chNameValueArr[$channelArr[$rowId]] = $rowVal;
                 }
                 $dated = new DateTime($dateStr);
                 $repStr .= $dated->format("Y-m-d H:i:s");
                 $channelsList = self::$channelsList;
                 foreach ($channelsList as $chName) {
                     if ($chNameValueArr[$chName] != null) {
                         $repStr .= "," . $chNameValueArr[$chName];
                     } else {
                         $repStr .= ",NA";
                     }
                 }
                 $repStr .= "\r\n";
             }
             echo $repStr;
             $currDate = new DateTime();
             $fileName = "MHCHDMANIKGARHC011_" . $dated->format("YmdHis") . ".csv";
             $repFileName = "/home/envirote/public_html/Generated/Manikgarh/" . $folSeq . "/" . $fileName;
             //$repFileName = $fileName;
             if (!file_exists(dirname($repFileName))) {
                 mkdir(dirname($repFileName), 0777, true);
             }
             $fh = fopen($repFileName, 'w') or die("can't open file");
             fwrite($fh, $repStr);
             fclose($fh);
         }
         unset($channelArr);
         unset($latestDataRows);
         unset($channelConfigs);
         unset($channelNoArr);
         unset($dataSlices);
         unset($dataRows);
         unset($chNameValueArr);
         unset($allDatesData);
         unset($channelsList);
     }
     //for loop ends
 }
 public function getMultiStationReport($GET)
 {
     $infoType = $GET['infoTypeRadio'];
     $exportType = $GET['exportTypeRadio'];
     $valueType = $GET['valueTypeRadio'];
     $folSeq = $GET['stationsSelect'];
     $fromDateStr = $GET['fromDate'];
     $toDateStr = $GET['toDate'];
     $timeBase = $GET['timeBase'];
     $timeBaseQuick = $GET['timeBaseQuick'];
     $quickReportType = $GET['quickReportType'];
     if ($quickReportType != "null") {
         $timeBase = $timeBaseQuick;
     }
     if ($valueType != "normal") {
         $timeBase = "5min";
     }
     $isValidated = $GET['isValidated'];
     $isAverage = $GET['isAverage'];
     $folSeqArray = array();
     //holds all folderseqs
     $folChannelsArray = array();
     //holds selected channels by foldSeq as key
     foreach ($GET as $key => $value) {
         if (strpos($key, "channelNos_") !== false) {
             $folSeq = (double) substr($key, 11);
             array_push($folSeqArray, $folSeq);
             $folChannelsArray[$folSeq] = $value;
         }
     }
     //a few validation checks
     if (count($folChannelsArray) == 0) {
         return null;
     }
     if ($quickReportType == "null" && (false === strtotime($fromDateStr) || false === strtotime($toDateStr))) {
         return null;
     }
     //get folderNames from DB
     $FDS = FolderDataStore::getInstance();
     $foldersObjArray = $FDS->FindBySeqs($folSeqArray);
     //calculating from to dates from provided $_GET
     $fromToDates = DateUtils::getDatesArrayForStationReports($quickReportType, $fromDateStr, $toDateStr);
     //calculating Date Slices now from DateUtils
     $fromDate = new DateTime($fromToDates['fromDate']);
     $toDate = new DateTime($fromToDates['toDate']);
     $folChannelsNamesArray = array();
     //holds selected channels by foldSeq as key
     $allChannelsArray = array();
     $channelUnitArray = array();
     foreach ($folChannelsArray as $folSeq => $folChannelChannelsArr) {
         $folName = $foldersObjArray[$folSeq]->getFolderName();
         $CCDS = ChannelConfigurationDataStore::getInstance();
         $ChannelsInfo = $CCDS->FindByFolderAndChannelNos($folSeq, $folChannelChannelsArr);
         $channelNamesArr = array();
         foreach ($ChannelsInfo as $chInfo) {
             if ($valueType != "normal") {
                 if (!self::isZeroSpanChannel($chInfo->getChannelName())) {
                     $chNo = $chInfo->getChannelNumber();
                     $chArr = $folChannelsArray[$folSeq];
                     if (($key = array_search($chNo, $chArr)) !== false) {
                         unset($folChannelsArray[$folSeq][$key]);
                         $folChannelsArray[$folSeq] = array_values($folChannelsArray[$folSeq]);
                     }
                     continue;
                 }
             }
             $chUnit = $chInfo->getChannelUnit();
             if (ConvertorUtils::getPrescribedUnit($chInfo->getChannelName()) != null) {
                 $chUnit = ConvertorUtils::getPrescribedUnit($chInfo->getChannelName());
             }
             $chUnit = mb_check_encoding($chUnit, 'UTF-8') ? $chUnit : utf8_encode($chUnit);
             $chStation = $chInfo->getChannelStation();
             if (!empty($chStation)) {
                 $chStation = " - " . $chStation;
             }
             $chStr = $folName . '<br>' . $chInfo->getChannelName() . $chStation . '<br>' . $chUnit;
             array_push($allChannelsArray, $chStr);
             array_push($channelUnitArray, $chUnit);
             array_push($channelNamesArr, $chInfo->getChannelName());
         }
         $folChannelsNamesArray[$folSeq] = $channelNamesArr;
     }
     //Created an array with folSeq and seelcted channels for it
     $folChannelDataArray = array();
     foreach ($folSeqArray as $folSeq) {
         $channelNoArray = $folChannelsArray[$folSeq];
         $EDS = ExemptionDataStore::getInstance();
         $exemptions = $EDS->FindExemptionsByFolderDatesChannels($folSeq, $fromToDates['fromDate'], $fromToDates['toDate'], implode(', ', $channelNoArray));
         //Stack or AQMS
         $stationType = $foldersObjArray[$folSeq]->getStationType();
         $dataJson = self::getDataJSON($fromToDates['fromDate'], $fromToDates['toDate'], $folSeq, "5min", $channelNoArray, $periodType, $exemptions, $stationType);
         //$avgData = self::getAverageData($fromToDates['fromDate'],$fromToDates['toDate'],$folSeq,$channelNoArray);
         $folChannelDataArray[$folSeq] = $dataJson['data'];
     }
     //Created an array with folSeq -> date->chanelvalues
     $CMD = CommentsDataStore::getInstance();
     $commentsObjs = $CMD->FindCommentsMasterByFolChannel($folSeqArray[0], $folChannelsArray[$folSeqArray[0]]);
     $dataSlices = null;
     //if its stack type we are supporting single station/folder data only
     $stationType = $foldersObjArray[$folSeqArray[0]]->getStationType();
     if ($stationType == 'stack' || $stationType == 'effluent') {
         //if($isAverage == "on"){
         //$dateSlices = DateUtils::getDateSlicesByInterval($fromDate,$toDate,$timeBase);
         //}else{
         if ($timeBase == "instant") {
             $dateSlices = DateUtils::getDateSlicesForData($folChannelDataArray[$folSeqArray[0]], $isAverage, $timeBase);
         } else {
             $dateSlices = DateUtils::getDateSlicesByInterval($fromDate, $toDate, $timeBase);
         }
         //}
     } else {
         $dateSlices = DateUtils::getDateSlicesByInterval($fromDate, $toDate, $timeBase);
     }
     //$dateSlices = $dateSlicesValid;
     $dataFinal = self::getFinalDataByValueType($timeBase, $dateSlices, $folSeqArray, $folChannelDataArray, $folChannelsNamesArray, $valueType, $isAverage, $stationType);
     if ($isValidated == "on") {
         $dateSlicesValidByChannelNumber = DateUtils::getDateSlicesByComments($commentsObjs, $timeBase, $folChannelsArray[$folSeqArray[0]]);
         $dataFinal = self::getValidatedDataOnly($dataFinal, $dateSlicesValidByChannelNumber);
     }
     if ($valueType != "normal") {
         $dataFinal = self::getZeroSpanValues($dataFinal);
     }
     $channelDataJSON = array();
     $channelDataJSON['channels'] = $allChannelsArray;
     //all channels with key as chno and val as chname
     $channelDataJSON['data'] = $dataFinal;
     //dateFinal is date as key and array of channel values as value
     $finalDataJSON = self::processDataJsonForGridGraph($channelDataJSON, $infoType);
     $finalDataJSON["units"] = $channelUnitArray;
     if ($infoType == "export") {
         ExportUtils::exportMultiStationGridReport($finalDataJSON, $exportType);
         return null;
     } else {
         if (count($channelDataJSON['channels']) == 1) {
             $chName = $channelNamesArr[0];
             $presLimit = PrescribedLimitsUtils::getPrescribedLimit($chName);
             if ($presLimit != null) {
                 $finalDataJSON['presLimit'] = $presLimit;
             }
         }
         return self::getAvgMinMaxValues($finalDataJSON);
     }
 }
 public function generateFile()
 {
     echo "APPCB Generator starts...\n";
     $CCDS = ChannelConfigurationDataStore::getInstance();
     $FDS = FolderDataStore::getInstance();
     $WQDS = WQDDataDataStore::getInstance();
     $folders = $FDS->FindByLocationSeqs("7,10");
     echo "<br>--------TotalFolders:" . count($folders);
     foreach ($folders as $folder) {
         $stationName = $folder->getFolderName();
         $stationNumber = substr($stationName, 7);
         $districtCode = "kham";
         $industryCode = "ITCPBCM";
         //if($folder->getSeq() == 20){
         if ($folder->getLocationSeq() == 10) {
             $stationNumber = 1;
             if ($folder->getSeq() == 26) {
                 $stationNumber = 2;
             } else {
                 if ($folder->getSeq() == 27) {
                     $stationNumber = 3;
                 } else {
                     if ($folder->getSeq() == 28) {
                         $stationNumber = 4;
                     } else {
                         if ($folder->getSeq() == 29) {
                             $stationNumber = 5;
                         }
                     }
                 }
             }
             $districtCode = "hyde";
             $industryCode = "APPCB";
         }
         $folSeq = $folder->getSeq();
         $latestDataRows = $WQDS->getChannelsLatestInfo($folSeq);
         echo "FolderSeq:" . $folSeq . " FolderName:" . $folder->getFolderName() . "<br>\n";
         //echo ("<br>\nTotal Rows found:". count($latestDataRows));
         echo "<br>\n ToDateFromLatestData " . $latestDataRows['dated'];
         if (count($latestDataRows) > 0) {
             $channelConfigs = $CCDS->FindByFolder($folSeq);
             $toDateStr = $latestDataRows['dated'];
             $toDate = new DateTime($toDateStr);
             $fromDate = new DateTime($toDateStr);
             $fromDate->setTime(0, 0, 0);
             $fromDateStr = DateUtils::getSQLDateFromDateObj($fromDate);
             echo "<br>From:" . $fromDateStr . "<br>\n";
             echo "To:" . $toDateStr . "<br>\n";
             $dataArray = $WQDS->getAllDataByFol($fromDateStr, $toDateStr, $folSeq);
             $channelNoArr = self::getChannelNumberNameArray(self::$channelsList, $channelConfigs);
             $addDatesData = array();
             foreach ($dataArray as $data) {
                 $channelData = array();
                 $dated = $data['wqdfiledatadated'];
                 $chValues = array();
                 $chStatuses = array();
                 foreach ($channelNoArr as $key => $value) {
                     $vl = (double) $data["ch" . $key . "value"];
                     if ($vl < 0) {
                         $vl = "n.o";
                     }
                     array_push($chValues, $vl);
                     array_push($chStatuses, (double) $data["ch" . $key . "status"]);
                 }
                 $allDatesData[strtotime($dated)]['channelValue'] = $chValues;
                 $allDatesData[strtotime($dated)]['channelStatuses'] = $chStatuses;
             }
             $channelArr = array();
             foreach ($channelNoArr as $chName) {
                 array_push($channelArr, $chName);
             }
             $dataSlices = DateUtils::getDateSlicesByInterval($fromDate, $toDate, "1hour");
             $dataRows = $WQDS->getAverageDataByDataArrayDataSlices("1hour", $allDatesData, $dataSlices, $channelArr, "normal", array());
         } else {
             echo " \n<br>Null FromDate";
         }
         if (count($dataRows) > 1) {
             $repStr = "";
             foreach ($dataRows as $dateStr => $row) {
                 $chNameValueArr = array();
                 foreach ($row as $rowId => $rowVal) {
                     $chNameValueArr[$channelArr[$rowId]] = $rowVal;
                 }
                 $repStr .= $districtCode;
                 $repStr .= "," . $industryCode;
                 $repStr .= "," . $stationNumber . ",";
                 $dated = new DateTime($dateStr);
                 $repStr .= $dated->format("m/d/Y h:i:s A");
                 $channelsList = self::$channelsList;
                 foreach ($channelsList as $chName) {
                     if ($chNameValueArr[$chName] != null) {
                         $repStr .= "," . $chNameValueArr[$chName];
                     } else {
                         $repStr .= ",NA";
                     }
                 }
                 $repStr .= "\r\n";
             }
             echo $repStr;
             //$fileName = "khmmitcl".$folder->getFolderName(). date("Ymd") .".txt";
             //$fileName = "air". date("Ymd") .".txt";
             $fileName = "air" . $fromDate->format("Ymd") . ".txt";
             $repFileName = "/home/envirote/public_html/Generated/APPCB/" . $folSeq . "/" . $fileName;
             //$repFileName ="d:". $folSeq ."/" . $fileName;
             if (!file_exists(dirname($repFileName))) {
                 mkdir(dirname($repFileName), 0777, true);
             }
             $fh = fopen($repFileName, 'w') or die("can't open file");
             fwrite($fh, $repStr);
             fclose($fh);
         }
         unset($channelArr);
         unset($latestDataRows);
         unset($channelConfigs);
         unset($channelNoArr);
         unset($dataSlices);
         unset($dataRows);
         unset($chNameValueArr);
         unset($allDatesData);
         unset($channelsList);
     }
     //for loop ends
 }