<?php

	$letter_head = getOwnerCompanyLetterHead($_SESSION['url_get_branch_admin']) ;

	// get all position
	$position 	  = [] ;
	$get_position = $mysqli->query("SELECT a.job_position_id, b.job_position_desc FROM setting_job_position a
					 				LEFT JOIN setting_job_position_translation b ON ( a.job_position_id = b.job_position_id )
								    WHERE a.deleted_at IS NULL AND b.lang = 'en'") ;
	if ( $get_position->num_rows > 0 ){
		while ( $row_position = $get_position->fetch_assoc() ){
			$position[$row_position['job_position_id']] = $row_position['job_position_desc'] ;
		}
	}

	$mysqli_query = $mysqli->query("SELECT a.advance_id, a.advance_paidby, a.advance_amount, a.advance_reason, a.advance_status, a.advance_payment_status, a.advance_payment_date, a.created_at, a.updated_at, b.staff_id, b.staff_name, b.staff_idno, b.staff_accountno, b.staff_icno, b.staff_passportno, b.country_id,b.job_position_id FROM staff_advance a
						 LEFT JOIN staff b ON ( a.staff_id = b.staff_id )
						 WHERE a.deleted_at IS NULL AND a.advance_id IN (".$page.") ");

	$letter_head = $letter_head['header'] ;
	$letter_head = str_replace( '<td><img src="images/logo.png" style="height:80px; width:80px" /></td>', '', $letter_head ) ;
	$letter_head = str_replace( '<td>&nbsp;&nbsp;</td>', '', $letter_head ) ;

	$html = '
	<html>
		<head>
			<style>
				.line-top{
					border-top: 1px solid black;
				}
				.line-bottom{
					border-bottom: 1px solid black;
				}
				hr {
				    border-top:1px dotted #000;
				}
			</style>
		</head>
		<body>' ;

		if ($mysqli_query->num_rows > 0){
			while ($row_page = $mysqli_query->fetch_array(MYSQLI_ASSOC)){
				$staff_idno = ucwords($row_page['staff_idno']) ;

				// set body content
				$html .= '
				<div class="container" style="padding:25px;">
					<table style="border:0; width: 100%; font-size:14px;">
					    <tr>
					        <th style="text-align:left; border:0;"><h2 style="text-align:left;">'.$letter_head.'</h2></th>
					    </tr>
					    <tr>
					    	<th>&nbsp;</th>
					    </tr>
					    <tr>
					        <td style="text-align:center; text-transform: uppercase;"><h3>Advanced Slip</h3></td>
					    </tr>
					</table>
					<table style="border: 0px; width: 100%; font-size:14px;" class="info">
					    <tr>
					        <td style="width:13%;" >Employee ID</td>
					        <td style="width:2%;" >:</td>
					        <td style="width:30%;" >'.$staff_idno.'</td>
					        <td style="width:10%;" >&nbsp;</td>
					        <td style="width:13%;" >Date</td>
					        <td style="width:2%;" >:</td>
					        <td style="width:30%;" >'.( $row_page['advance_payment_date'] != '' ? date('d.m.Y',strtotime(dataFilter($row_page['advance_payment_date']))) : '-' ).'</td>
					    </tr>
					    <tr>
					        <td>Name</td>
					        <td>:</td>
					        <td>'.dataFilter($row_page['staff_name']).'</td>
					        <td></td>
					        <td>Department</td>
					        <td>:</td>
					        <td style="text-transform: capitalize;" >';
						        //department
					        	$d_name  	    = [] ;
								$get_department = $mysqli->query("SELECT a.department_id, b.department_desc FROM staff_department a
						   				     					  LEFT JOIN setting_department_translation b ON ( a.department_id = b.department_id )
											 					  WHERE a.deleted_at IS NULL AND b.lang = 'en' AND staff_id = '".$row_page['staff_id']."'") ;
								if($get_department->num_rows > 0){
									while ( $row_department = $get_department->fetch_assoc() ){
										$d_name[] = $row_department['department_desc'];
									}
								}
								$html .= implode( ', ', $d_name ) ;
					        $html .= '
					        </td>
					    </tr>
					    <tr>
					        <td>Month</td>
					        <td>:</td>
					        <td>'.date('F Y',strtotime(dataFilter($row_page['created_at']))).'</td>
					        <td>&nbsp;</td>
					        <td>Position</td>
					        <td>:</td>
					        <td style="text-transform: capitalize;" >'.dataFilter( $position[$row_page['job_position_id']] ).'</td>
					    </tr>
					    <tr>
					        <td colspan="7" >&nbsp;</td>
					    </tr>
					    <tr>
					        <td colspan="7" style="text-align:center; border:1; font-size:14px; padding:5px; text-transform: uppercase;"><h3>Particulars</h3></td>
					    </tr>
					</table>
					<table style="border:0; width: 100%; font-size:14px;">
					    <tr>
					        <td style="width:15%; text-transform:uppercase;" >Advanced</td>
					        <td style="width:10%;" >&nbsp;</td>
					        <td style="width:15%; text-align:right;" >'.dataFilter($row_page['advance_amount']).'</td>
					        <td style="width:60%;" ></td>
					    </tr>
					    <tr>
					        <td colspan="4" class="line-bottom">&nbsp;</td>
					    </tr>
					    <tr>
					        <td>TOTAL ADVANCE</td>
					        <td>&nbsp;</td>
					        <td style="text-align:right;" >'.dataFilter($row_page['advance_amount']).'</td>
					        <td></td>
					    </tr>
					    <tr>
					        <td colspan="4" class="line-top">&nbsp;</td>
					    </tr>
					    <tr>
					        <td><b>Nett pay</b></td>
					        <td>&nbsp;</td>
					        <td style="text-align:right; border-bottom: 1px double black; border-width:2px;" >'.dataFilter($row_page['advance_amount']).'</td>
					        <td ></td>
					    </tr>
					    <tr>
					        <td colspan="4">&nbsp;</td>
					    </tr>
					    <tr>
					        <td colspan="4">&nbsp;</td>
					    </tr>
					    <tr>
					        <td ><b>Received By:</b></td>
					        <td colspan="2" class="line-bottom">&nbsp;</td>
					        <td></td>
					    </tr>
					</table>
				</div>';
			}
		}

		$html .= '
		</body>
	</html>' ;

	//print pdf 
	include_once 'MPDF/mpdf.php' ;
	$mpdf = new mPDF('utf-8', [ 215, 135 ], '', 'freesans', 0, 0, 0, 0, 0, 0) ;

	$mpdf->useAdobeCJK = true ;
    $mpdf->SetAutoFont(AUTOFONT_ALL) ;

    // Use different Odd/Even headers and footers and mirror margins
    $mpdf->mirrorMargins = 1 ;

    // set mpdf header
    //$mpdf->SetHTMLHeader($header) ;
    //$mpdf->SetHTMLHeader($header,'E') ;

    // set mpdf footer
    $mpdf->SetHTMLFooter($footer) ;
    $mpdf->SetHTMLFooter($footer,'E') ;

    // write in html
    $mpdf->WriteHTML($html) ;

    // set filename
    $page_filename = 'hr-advanced-'.time() ;
    $filename = $page_filename ; // Your Filename whit local date and time
    $filename_save = $filename.'.pdf' ;
    $filename_jpeg = $filename.'.jpg' ;
    $filename_temp = $filename ;

    // check output type
    $page_type_output = ($page_type == 'jpeg' ? 'F' : 'I') ;
    $mpdf->Output($filename_save, $page_type_output) ;

    // check page type
    if ($page_type == 'jpeg'){
        //*******************************************************************
        //******************************************************************* save pdf file to jpeg file
        //*******************************************************************
        $img = new imagick() ;
        //this must be called before reading the image, otherwise has no effect - "-density {$x_resolution}x{$y_resolution}"
        $img->setResolution(300,300) ; //this is important to give good quality output, otherwise text might be unclear
        $img->readImage("{$filename_save}") ; //read the pdf
        $total_page = $img->getNumberImages() ;
        $img->setImageFormat('jpg') ; //set new format
        $img->writeImages($filename_jpeg, true) ; //save image file
        //*******************************************************************
        //******************************************************************* force download jpeg file
        //*******************************************************************
        $file_name = $filename_jpeg ; // grab the requested file's name
        // make sure it's a file before doing anything!
        if ($total_page > 1){
            for ($i = 0; $i < $total_page; $i++){
                $file_name = $filename_temp.'-'.$i.'.jpg' ;
                if(is_file($file_name)) {
                    $files_to_zip[] = $file_name ;      
                }
            }
            create_zip($files_to_zip, $filename_temp.'.zip') ;
            for ($i = 0; $i < $total_page; $i++){ unlink($filename_temp.'-'.$i.'.jpg') ; }
            $file_name = $filename_jpeg = $filename_temp.'.zip' ;
        }
        if(is_file($file_name)) {           
            // required for IE
            if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off') ;    }
            // get the file mime type using the file extension
            switch(strtolower(substr(strrchr($file_name, '.'), 1))) {
                case 'pdf': $mime = 'application/pdf'; break ;
                case 'zip': $mime = 'application/zip'; break ;
                case 'jpeg':
                case 'jpg': $mime = 'image/jpg'; break ;
                default: $mime = 'application/force-download' ;
            }
            header('Pragma: public') ;  // required
            header('Expires: 0') ;      // no cache
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0') ;
            header('Last-Modified: '.gmdate ('D, d M Y H:i:s', filemtime ($file_name)).' GMT') ;
            header('Cache-Control: private',false) ;
            header('Content-Type: '.$mime) ;
            header('Content-Disposition: attachment; filename="'.basename($file_name).'"') ;
            header('Content-Transfer-Encoding: binary') ;
            header('Content-Length: '.filesize($file_name)) ;   // provide file size
            header('Connection: close') ;
            readfile($file_name); // push it out            
        }
        unlink($filename_jpeg) ;
        unlink($filename_save) ;
        echo '<script type="text/javascript">setTimeout("window.close();", 0);</script>' ;
    }

?>