ReportLib.php 5.37 KB
<?php namespace App\Jobs;

use Auth;
use Request;
use Config;
use App\Models\User;
use DB;
use Log;
use Hash;
use App\Jobs\KHRMSLib;
use \PDO;
use \PHPExcel_IOFactory;
use \PHPExcel_Cell;

class ReportLib
{

	function pripareReportData() {
		$wakka = new KHRMSLib();
		$dashboarduser=Auth::user();
		$conn_exception = '';

		$i=1;

		// $loggedInUser=DB::table('users')->where('username','=',$dashboarduser->username)->get();
		// $loggedInUserData = json_decode($loggedInUser[0]->data);
		// $ipArray = ($loggedInUserData->childuserservers != '') ? explode(',',$loggedInUserData->childuserservers) : array();	
		// if (($akey = array_search(env('DB_HOST'), $ipArray)) !== false) {
  //   		unset($ipArray[$akey]);
		// }
		//array_push($ipArray, env('DB_HOST'));
		

		//$ipArray=['10.2.105.141'=>'Mumbai','10.4.105.141'=>'Chennai','10.14.130.50'=>'Delhi'];
		$ipArray=['10.4.105.141'=>'Chennai'];
		

		//$ipArray=['192.168.3.252'=>'server1','192.168.3.246'=>'server2'];

		$reportarray = array();
		$reportarray1 = array();
		foreach ($ipArray as $ip => $server) {
			try {
				// Config::set("database.connections.mysql.read.host", $ip);
				// Config::set("database.connections.mysql.database", $server);
				//DB::purge('mysql');
				DB::connection($server);
				// echo $ip;
				// echo $server;
				// echo Config::get("database.connections.$server.read.host");
				// echo Config::get("database.connections.$server.database");
				
				if($dashboarduser->usertype != 'Admin')
				{
					$accessData['is_admin'] = false;
					$allusers=DB::connection($server)->table('users')->where(function ($query)  use($dashboarduser) {
							$query->where('username','=',$dashboarduser->username)
							->orwhere('supervisor','=',$dashboarduser->username)
							->orWhere('lteam2','=',$dashboarduser->username)
							->orWhere('lteam','=',$dashboarduser->username);
					})->where('status', '=', 'Active')->get();

					$clients = array(); $didlines = array();
					if($dashboarduser->exten!="")$didlines[]=$dashboarduser->exten;
					$oclientlst=$wakka->clientsReadAccess();
					if(!empty($oclientlst)) {
						foreach($oclientlst as $tclnt) {
							if($tclnt!="")
							{
								$roclientstr[]="$tclnt";
							}
						}
					}
					$accessData['clients'] = $clients;
					$accessData['didlines'] = $didlines;
				}
				else {
					$accessData['is_admin'] = true;
					$allusers = DB::connection($server)->table('users')->where('status', '=', 'Active')->get();
				}

				foreach($allusers as $tuser) {
					$accessData['users'][]=$tuser->id;
					$accessData['usersData'][$tuser->id] = array('username'=>$tuser->username, 
						'fullname'=>$tuser->fullname);
				}
				//print_r(generateReportData($ip, $accessData, $reportarray, $i));
				$reportarray = generateReportData($ip, $accessData, $reportarray, $i,$server);
				DB::disconnect($server);
			} catch (\Exception $e) {
				//echo $e;
			    $conn_exception .= "Could not connect to the database on $ip.<br />";
			}
		}
		$return_array['conn_exception'] = $conn_exception;
		$return_array['reportarray'] = $reportarray;
		return $return_array;
	}

	function downloadExcelFromArray($reporthead, $reportdata, $filename = 'logexcel.xls')
	{
		include_once(app_path().'/lib/phpexcel/PHPExcel.php');
		
		$inputFileType = "Excel5";
		$objReader = PHPExcel_IOFactory::createReader($inputFileType);
		$objPHPExcel = $objReader->load("assets/extras/blank.xls");
		$baseRow = 2;


		$highestColumn = sizeof($reporthead);
		for ($head = 0; $head < $highestColumn; $head++){
			$colstr=PHPExcel_Cell::stringFromColumnIndex($head);
			$objPHPExcel->getActiveSheet()->setCellValue($colstr."1", $reporthead[$head]);
		}

		foreach($reportdata as $row)
		{
			$baseRowValue = $baseRow++;
			$col = 0;
			
			for ($head = 0; $head < $highestColumn; $head++){
				$colstr=PHPExcel_Cell::stringFromColumnIndex($head);
				$objPHPExcel->getActiveSheet()->setCellValue($colstr.$baseRowValue, $row[$reporthead[$head]]);
			}
		}

		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="'.$filename.'"');
		header('Cache-Control: max-age=0');

		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $inputFileType);
		$objWriter->save('php://output');
		
		return ;
	}

	function downloadExcelFromArray1($reporthead, $reportdata, $filename = 'logexcel.xls')
	{
		include_once(app_path().'/lib/phpexcel/PHPExcel.php');
		
		$inputFileType = "Excel5";
		$objReader = PHPExcel_IOFactory::createReader($inputFileType);
		$objPHPExcel = $objReader->load("assets/extras/blank.xls");
		$baseRow = 2;


		$highestColumn = sizeof($reporthead);
		for ($head = 0; $head < $highestColumn; $head++){
			$colstr=PHPExcel_Cell::stringFromColumnIndex($head);
			$objPHPExcel->getActiveSheet()->setCellValue($colstr."1", $reporthead[$head]);
		}

		foreach($reportdata as $date => $dateArray)
		{
			
			foreach ($dateArray as $server => $row) {
				$baseRowValue = $baseRow++;
			$col = 0;
				for ($head = 0; $head < $highestColumn; $head++){
					$colstr=PHPExcel_Cell::stringFromColumnIndex($head);
					$objPHPExcel->getActiveSheet()->setCellValue($colstr.$baseRowValue, $row[$reporthead[$head]]);
				}
			}
		}


		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="'.$filename.'"');
		header('Cache-Control: max-age=0');

		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $inputFileType);
		$objWriter->save('php://output');
		
		return ;
	}

}
?>