<?php
include 'connect/cms-config.php' ;
include 'requires/function.php' ;
include 'requires/session.php' ;

$slip_id                = $_GET['slip_id'];
$query_slip             = $mysqli->query("SELECT a.*, b.staff_idno, b.staff_name, b.staff_image, b.branch_id, b.staff_icno, b.job_position_id, b.staff_date_joined FROM salary_slip a 
                                          JOIN staff b ON (a.staff_id = b.staff_id) 
                                          WHERE a.slip_id = '$slip_id' LIMIT 1") ;
$result_slip            = mysqli_fetch_assoc($query_slip);

$branch_id              = $result_slip['branch_id'];
$query_branch           = $mysqli->query("SELECT * FROM branch WHERE branch_id = '$branch_id' LIMIT 1");
if ( $query_branch->num_rows > 0 ){
    $result_branch      = mysqli_fetch_assoc($query_branch) ;
    $letter_head        = html_entity_decode(htmlspecialchars_decode($result_branch['branch_content']));
}

$staff_id               = $result_slip['staff_id'] ;

$job_position_id        = $result_slip['job_position_id'] ;
$mysqli_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' AND a.job_position_id = '".$job_position_id."' LIMIT 1") ;
$job_position_desc      = '' ;
if ( $mysqli_position->num_rows > 0 ){
    $row_position       = $mysqli_position->fetch_assoc() ;
    $job_position_desc  = $row_position['job_position_desc'] ;
}

$total_earning          = $result_slip['basic_salary'] + $result_slip['allowance'] + $result_slip['commission'];
$total_deduction        = $result_slip['staff_epf'] + $result_slip['staff_socso'] + $result_slip['staff_eis'] + $result_slip['staff_pcb'] + $result_slip['staff_zakat'] + $result_slip['deduction'] ;


// left table
$left_table    = [] ;
$left_table[]  = [
    'key'   => 'WAGES',
    'value' => number_format((float)$result_slip['basic_salary'], 2, '.', '')
] ;
if ( $result_slip['allowance'] > 0 ){
    $left_table[]  = [
        'key'   => 'ALLOWANCE',
        'value' => number_format((float)$result_slip['allowance'], 2, '.', '')
    ] ;
}
if ( $result_slip['commission'] > 0 ){
    $left_table[]  = [
        'key'   => 'COMMISSION',
        'value' => number_format((float)$result_slip['commission'], 2, '.', '')
    ] ;
}

// right table
$right_table = [] ;
if( $result_slip['staff_epf'] > 0 ){
    $right_table[] = [
        'key'   => 'EPF (EMPLOYEE)',
        'value' => number_format((float)$result_slip['staff_epf'], 2, '.', '')
    ] ;
}
if( $result_slip['staff_socso'] > 0 ){
    $right_table[] = [
        'key'   => 'SOCSO (EMPLOYEE)',
        'value' => number_format((float)$result_slip['staff_socso'], 2, '.', '')
    ] ;
}
if( $result_slip['staff_eis'] > 0 ){
    $right_table[] = [
        'key'   => 'EIS (EMPLOYEE)',
        'value' => number_format((float)$result_slip['staff_eis'], 2, '.', '')
    ] ;
}
if( $result_slip['staff_pcb'] > 0 ){
    $right_table[] = [
        'key'   => 'PCB',
        'value' => number_format((float)$result_slip['staff_pcb'], 2, '.', '')
    ] ;
}
if( $result_slip['staff_zakat'] > 0 ){
    $right_table[] = [
        'key'   => 'MUSLIM ZAKAT FUND',
        'value' => number_format((float)$result_slip['staff_zakat'], 2, '.', '')
    ] ;
}
if( $result_slip['deduction'] > 0 ){
    $right_table[] = [
        'key'   => 'DEDUCTION',
        'value' => number_format((float)$result_slip['deduction'], 2, '.', '')
    ] ;
}

// bottom table
$bottom_table = [] ;
if( $result_slip['employer_epf'] > 0 ){
    $bottom_table[] = [
        'key'   => 'EPF',
        'value' => number_format((float)$result_slip['employer_epf'], 2, '.', '')
    ] ;
}
if( $result_slip['employer_socso'] > 0 ){
    $bottom_table[] = [
        'key'   => 'SOCSO',
        'value' => number_format((float)$result_slip['employer_socso'], 2, '.', '')
    ] ;
}
if( $result_slip['employer_eis'] > 0 ){
    $bottom_table[] = [
        'key'   => 'EIS',
        'value' => number_format((float)$result_slip['employer_eis'], 2, '.', '')
    ] ;
}



$html .= '
'.$letter_head.'
<table style="width:600px; font-size:16px;">
    <tr>
        <td colspan="7" style="padding:30px 0; text-align:center; font-size:25px;">
            <strong>PAYSLIP</strong>
        </td>
    </tr>
    <tr>
        <td style="width:100px;">NAME</td>
        <td style="width:10px">:</td>
        <td style="width:180px; border-bottom:1px solid black;">'.$result_slip['staff_name'].'</td>
        <td style="width:20px;">&nbsp;</td>
        <td style="width:100px;">DATE</td>
        <td style="width:10px">:</td>
        <td style="width:180px; border-bottom:1px solid black;">'.strtoupper(date('07-m-y', strtotime($result_slip['month'].' +1 month'))).' </td>
    </tr>
    <tr>
        <td>ID</td>
        <td>:</td>
        <td style="border-bottom:1px solid black;">'.$result_slip['staff_idno'].'</td>
        <td>&nbsp;</td>
        <td>IC NO</td>
        <td>:</td>
        <td style="border-bottom:1px solid black;">'.$result_slip['staff_icno'].'</td>
    </tr>
    <tr>
        <td>POSITION</td>
        <td>:</td>
        <td style="border-bottom:1px solid black;">'.$job_position_desc.'</td>
        <td>&nbsp;</td>
        <td>MONTH</td>
        <td>:</td>
        <td style="border-bottom:1px solid black;">'.strtoupper(date('M-y', strtotime($result_slip['month']))).'</td>
    </tr>
</table>

<table class="calTable" style="width:600px; font-size:14px;">
    <thead>
        <tr class="title">
            <th style="width:150px" class="titleleft" >GROSS EARNING</th>
            <th style="width:150px; text-align:right;">Amount(RM) </th>
            <th style="width:150px;" class="leftLine titleleft">DEDUCTIONS</th>
            <th style="width:150px; text-align:right;">Amount(RM) </th>
        </tr>
    </thead>
    <tbody>
        <tr>    
            <td>[LEFT01_TITLE]</td>
            <td class="amount">[LEFT01_VALUE]</td>
            <td class="leftLine">[RIGHT01_TITLE]</td>
            <td class="amount">[RIGHT01_VALUE]</td>
        </tr>
        <tr>    
            <td>[LEFT02_TITLE]</td>
            <td class="amount">[LEFT02_VALUE]</td>
            <td class="leftLine">[RIGHT02_TITLE]</td>
            <td class="amount">[RIGHT02_VALUE]</td>
        </tr>
        <tr>    
            <td>[LEFT03_TITLE]</td>
            <td class="amount">[LEFT03_VALUE]</td>
            <td class="leftLine">[RIGHT03_TITLE]</td>
            <td class="amount">[RIGHT03_VALUE]</td>
        </tr>
        <tr>    
            <td>[LEFT04_TITLE]</td>
            <td class="amount">[LEFT04_VALUE]</td>
            <td class="leftLine">[RIGHT04_TITLE]</td>
            <td class="amount">[RIGHT04_VALUE]</td>
        </tr>
        <tr>    
            <td>[LEFT05_TITLE]</td>
            <td class="amount">[LEFT05_VALUE]</td>
            <td class="leftLine">[RIGHT05_TITLE]</td>
            <td class="amount">[RIGHT05_VALUE]</td>
        </tr>
        <tr>    
            <td>[LEFT06_TITLE]</td>
            <td class="amount">[LEFT06_VALUE]</td>
            <td class="leftLine">[RIGHT06_TITLE]</td>
            <td class="amount">[RIGHT06_VALUE]</td>
        </tr>
        <tr>
            <td colspan="2"> &nbsp;</td>
            <td colspan="2" class="leftLine">&nbsp;</td>
        </tr>
        
        <tr class="title">
            <td >TOTAL EARNING /<br> INCOME</td>
            <td class="amount" >'.number_format((float)$total_earning, 2, '.', '').'</td>
            <td class="leftLine">TOTAL DEDUCTIONS </td>
            <td class="amount" >'.number_format((float)$total_deduction, 2, '.', '').'</td>
        </tr>
        <tr >
            <td colspan="2">&nbsp;</td>
            <td colspan="2" class="subtitle">EMPLOYER CONTRIBUTIONS</td>
        </tr>
        <tr>    
            <td>NET PAY</td>
            <td class="amount">'.number_format( (float)$result_slip['total'], 2, '.', '').'</td>
            <td class="leftLine">[BOTTOM01_TITLE]</td>
            <td class="amount">[BOTTOM01_VALUE]</td>
        </tr>
        <tr>    
            <td colspan="2"></td>
            <td class="leftLine">[BOTTOM02_TITLE]</td>
            <td class="amount">[BOTTOM02_VALUE]</td>
        </tr>
        <tr>    
            <td colspan="2"></td>
            <td class="leftLine">[BOTTOM03_TITLE]</td>
            <td class="amount">[BOTTOM03_VALUE]</td>
        </tr>
    </tbody>
</table>

<table style="width:600px; font-size:16px;">
    <tr>
        <td style="width:600px;">&nbsp;</td>
    </tr>
    <tr>
        <td>REMARK : '.( dataFilter( $result_slip['remark'] ) ? dataFilter( $result_slip['remark'] ) : '-' ) .'</td>
    </tr>
</table>' ;

$footer   = '
<table style="width:600px; font-size:12px;">
    <tr>
        <td>This is computer generated document. No signature is required.</td>
    </tr>
</table>' ;


// reset
$count_left   = 1 ;
$count_right  = 1 ;
$count_bottom = 1 ;
for ( $a = 0 ; $a <= 5 ; $a++ ){

    if ( count($left_table) > 0 ){
        $html = str_replace( '[LEFT0'.$count_left.'_TITLE]', $left_table[0]['key'], $html ) ;
        $html = str_replace( '[LEFT0'.$count_left.'_VALUE]', $left_table[0]['value'], $html ) ;
        $count_left++ ;

        array_shift( $left_table ) ;
    }else{
        $html = str_replace( '[LEFT0'.$count_left.'_TITLE]', '&nbsp;', $html ) ;
        $html = str_replace( '[LEFT0'.$count_left.'_VALUE]', '&nbsp;', $html ) ;
        $count_left++ ;
    }

    if ( count($right_table) > 0 ){
        $html = str_replace( '[RIGHT0'.$count_right.'_TITLE]', $right_table[0]['key'], $html ) ;
        $html = str_replace( '[RIGHT0'.$count_right.'_VALUE]', $right_table[0]['value'], $html ) ;
        $count_right++ ;

        array_shift( $right_table ) ;
    }else{
        $html = str_replace( '[RIGHT0'.$count_right.'_TITLE]', '&nbsp;', $html ) ;
        $html = str_replace( '[RIGHT0'.$count_right.'_VALUE]', '&nbsp;', $html ) ;
        $count_right++ ;
    }

    if ( count($bottom_table) > 0 ){
        $html = str_replace( '[BOTTOM0'.$count_bottom.'_TITLE]', $bottom_table[0]['key'], $html ) ;
        $html = str_replace( '[BOTTOM0'.$count_bottom.'_VALUE]', $bottom_table[0]['value'], $html ) ;
        $count_bottom++ ;

        array_shift( $bottom_table ) ;
    }else{
        $html = str_replace( '[BOTTOM0'.$count_bottom.'_TITLE]', '&nbsp;', $html ) ;
        $html = str_replace( '[BOTTOM0'.$count_bottom.'_VALUE]', '&nbsp;', $html ) ;
        $count_bottom++ ;
    }
}


// page header
// $header = '<head><link rel="stylesheet" type="text/css" href="template.css"></head>' ;

// keep footer html content in variable
include_once 'MPDF/mpdf.php' ;

$mpdf = new mPDF('utf-8', 'A5', '', 'freesans', 10, 10, 7, 5, 5, 5) ;
ini_set("memory_limit", "9999M");

// 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') ;

//css
$stylesheet = file_get_contents('css/salary_pdf.css'); // external css
$mpdf->WriteHTML($stylesheet, 1);

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

    // set filename
$filename = $print_filename; // Your Filename whit local date and time
$filename_save = $filename.'.pdf' ;
$filename_jpeg = $filename.'.jpg' ;
$filename_temp = $filename ;

// turns all headers/footers off from new page onwards
$mpdf->useAdobeCJK = true;

// check output type
$page_type = ($_GET['page_type']) ;
$page_type_output = ($page_type == 'jpeg' ? 'F' : 'I') ;

//$mpdf->SetAutoFont(AUTOFONT_ALL);
$mpdf->Output($filename_save, $page_type_output);
