<?php 
	date_default_timezone_set('UTC');
	//require('fpdf/fpdf.php');
    require_once("config/configDB.php");
    require_once('tcpdf/config/tcpdf_config.php');
    require_once("tcpdf/tcpdf.php");

    $tripfromDate ='';
    if($_GET['startDate']) {
        $invoicedate_temp   = new DateTime(stripslashes($_GET['startDate']));
        $tripfromDate 		= $invoicedate_temp->format('Y-m-d');
    }
    $triptoDate ='';
    if($_GET['endDate']) {
        $invoicedate_temp1   = new DateTime(stripslashes($_GET['endDate']));
	    $triptoDate 		= $invoicedate_temp1->format('Y-m-d');
    }
    $triplabourname     = is_numeric($_GET['ename']) ? $_GET['ename'] : 0;

//echo "SELECT id, ename   FROM employee_master WHERE id='$triplabourname' ";
    $sqlEmpMasterQuery = mysqli_query($conn,"SELECT id, ename   FROM employee_master WHERE id='$triplabourname' ");
    $rowtripdrivername    = mysqli_fetch_array($sqlEmpMasterQuery,MYSQLI_ASSOC);
    $tripdrivername_Fillter      = $rowtripdrivername['ename'];

    $sql = "SELECT lm.ename as ename,sum(lad.shifttype) as shifttype,sum(lad.extraworkinghrs) as extraworkinghrs,  lad.enameid
    FROM employee_attendance_dtl as lad 
    LEFT JOIN employee_master as lm ON lm.id=lad.enameid where 1=1  ";
	//echo $sql;

    if($tripfromDate !=''  && $triptoDate !='' && $triplabourname ==0) {
        $sql.=" and lad.invoicedate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'";

    } else if($tripfromDate !=''  && $triptoDate !='' && $triplabourname >0) {
        $sql.=" and lad.invoicedate BETWEEN '".$tripfromDate."' AND '".$triptoDate."' and lad.enameid='".$triplabourname."' ";
    } else if($tripfromDate ==''  && $triptoDate =='' && $triplabourname >0) {
        $sql.=" and lad.enameid='".$triplabourname."' ";
    } 

    $sql.="  GROUP BY lad.enameid ";	

    

    $query=mysqli_query($conn, $sql);

    $data_step4     = mysqli_fetch_array($query,MYSQLI_ASSOC);    
    
    $shifttype      = $data_step4['shifttype'];
	 $extraworkinghrs      = $data_step4['extraworkinghrs'];
     

    $sql22 = "SELECT lm.ename as ename,(lad.advance_collection_amount) as collectadvanceamt , lad.employee_id
    FROM employee_advance_collection as lad 
    LEFT JOIN employee_master as lm ON lm.id=lad.employee_id where lad.employee_id='".$triplabourname."'  ";

    if($tripfromDate !=''  && $triptoDate !='') {
        $sql22.=" and lad.advance_date='".$tripfromDate."' and   lad.advance_date='".$triptoDate."'";
    } 

    $sql22.="  GROUP BY lad.employee_id  ORDER BY lad.id DESC LIMIT 1";	
	//echo $sql22;
    
    $query=mysqli_query($conn, $sql22);

    $data_step22     = mysqli_fetch_array($query,MYSQLI_ASSOC);    

    $collectadvanceamt  = $data_step22['collectadvanceamt'];

    $sql_advsalary = "SELECT lm.ename as ename,sum(lad.advancesalary) as advancesalary 
            FROM employee_attendance_dtl as lad 
            LEFT JOIN employee_master as lm ON lm.id=lad.enameid 
            where lad.enameid='$triplabourname'  ";

    $sql_advsalary.="  GROUP BY lad.enameid ";	
    $query_advsalary    = mysqli_query($conn, $sql_advsalary);
    $data_advsalary     = mysqli_fetch_array($query_advsalary,MYSQLI_ASSOC); 
    $advancesalary      = $data_advsalary['advancesalary'];

    //Collect total Advance Amount
    $sql_advcollect = "SELECT lm.ename as ename,sum(lad.advance_collection_amount) as collectadvanceamt 
        FROM employee_advance_collection as lad 
        LEFT JOIN employee_master as lm ON lm.id=lad.employee_id 
        where lad.employee_id='$triplabourname'  ";

$sql_advcollect.="  GROUP BY lad.employee_id ";	
$query_advcollect    = mysqli_query($conn, $sql_advcollect);
$data_advcollect     = mysqli_fetch_array($query_advcollect,MYSQLI_ASSOC); 
$collectTotaladvanceamt      = $data_advcollect['collectadvanceamt'];


    //-- Get data from Master Labours table
    $shiftworkamt   ='';
    $extrahoursamt  ='';
    if($triplabourname >0) {

        $sql_step3  = mysqli_query($conn,"SELECT ename, id,extrahoursamt ,shiftworkamt FROM employee_master WHERE id='".$triplabourname."' ");
		//echo "SELECT ename, id,extrahoursamt ,shiftworkamt FROM employee_master WHERE id='".$triplabourname."' ";

        $data_step3     = mysqli_fetch_array($sql_step3,MYSQLI_ASSOC);
        $shiftworkamt   = $data_step3['shiftworkamt'];
        $extrahoursamt  = $data_step3['extrahoursamt'];
    
    } 
    
    $sqlDate = "SELECT lm.ename as ename,lad.invoicedate,sum(lad.shifttype) as shifttype,sum(lad.extraworkinghrs) as extraworkinghrs,lad.enameid 
    FROM employee_attendance_dtl as lad 
    LEFT JOIN employee_master as lm ON lm.id=lad.enameid where 1=1  ";

    if($tripfromDate !=''  && $triptoDate !='' && $triplabourname ==0) {
        $sqlDate.=" and lad.invoicedate BETWEEN '".$tripfromDate."' AND '".$triptoDate."'";

    } else if($tripfromDate !=''  && $triptoDate !='' && $triplabourname >0) {
        $sqlDate.=" and lad.invoicedate BETWEEN '".$tripfromDate."' AND '".$triptoDate."' and lad.enameid='".$triplabourname."' ";
    } else if($tripfromDate ==''  && $triptoDate =='' && $triplabourname >0) {
        $sqlDate.=" and lad.enameid='".$triplabourname."' ";
    } 

    $sqlDate.="  GROUP BY lad.enameid,lad.invoicedate  ";	

    $query_date=mysqli_query($conn, $sqlDate);

    // $sql_advsalaryDatewise = "SELECT lm.ename as ename,lad.enameid,lad.invoicedate ,lad.advancesalary as advancesalary
    // FROM employee_attendance_dtl as lad 
    // LEFT JOIN employee_master as lm ON lm.id=lad.enameid 
    // where lad.enameid='".$triplabourname."' and lad.advancesalary>0 ";

    // $sql_advsalaryDatewise.="  GROUP BY lad.enameid,lad.invoicedate  ";	

    // $query_advsalaryDatewise=mysqli_query($conn, $sql_advsalaryDatewise);
    

    class MYPDF extends TCPDF {

        //Page header
        public function Header() {
            // Logo
            //$image_file = K_PATH_IMAGES.'logo_example.jpg';
            //$this->Image($image_file, 10, 10, 15, '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);
            // Set font
            $this->SetY(12);
            $this->SetFont('helvetica', 'B', 24);
            // Title
            $this->Cell(0, 25, PDF_HEADER_TITLE, 0, false, 'C', 0, '', 0, false, 'M', 'M');
            $style3 = array('width' => 0.2, 'cap' => 'round', 'join' => 'round', 'dash' => 0, 'color' => array(0, 0, 0));
            //$this->Line(0, 22, 210, 22, $style3);
        }
    
        // Page footer
        public function Footer() {
            // Position at 15 mm from bottom
            $this->SetY(-15);
            // Set font
            $this->SetFont('helvetica', 'I', 8);
            // Page number
            $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'R', 0, '', 0, false, 'T', 'M');
        }
    }

    $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('thinkofmind.com');
$pdf->SetTitle('Labour Report');
$pdf->SetSubject('Labour Report');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
//$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
	require_once(dirname(__FILE__).'/lang/eng.php');
	$pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// set font
$pdf->SetFont('helvetica', '', 20);

// add a page
$pdf->AddPage();
// Line
//$pdf->Line(5, 10, 5, 30, $style2);


// set some text to print
$txt = <<<EOD
Employee Salary Slip

EOD;

// print a block of text using Write()
$pdf->Write(0, $txt, '', 0, 'C', true, 0, false, false, 0);

$pdf->SetFont('helvetica', '', 10);

$pdf->Write(2, '', '', 0, 'C', true, 0, false, false, 0);

$tbl_step1 = <<<EOD
<table cellspacing="0" cellpadding="1" >
    <tr>
        <td>Labour Name : $tripdrivername_Fillter</td>
    </tr>
</table>           

EOD;

// print a block of text using Write()
$pdf->writeHTML($tbl_step1, true, false, false, false, '');
//$pdf->Write(1, '', '', 0, 'C', true, 0, false, false, 0);

$tbl_step2 = <<<EOD
<table cellspacing="0" cellpadding="1" >
    <tr>
        <td>From : $tripfromDate</td>
        <td>To : $triptoDate</td>
    </tr>
</table>           

EOD;

// print a block of text using Write()
$pdf->writeHTML($tbl_step2, true, false, false, false, '');


$tbl_step3 = <<<EOD
<table cellspacing="0" cellpadding="1" >
    <tr>
        <td>Shift Amount : $shiftworkamt</td>
        <td>Extra Working Amount : $extrahoursamt</td>
        
    </tr>
</table>           

EOD;

// print a block of text using Write()
$pdf->writeHTML($tbl_step3, true, false, false, false, '');

$tbl_step31 = <<<EOD
<table cellspacing="0" cellpadding="1" >
    <tr>
        <td>Collect Advance Amount : $collectadvanceamt</td>        
        <td>Total Collect Advance  : $collectTotaladvanceamt</td>        
        
    </tr>
</table>           
EOD;
//$pdf->writeHTML($tbl_step31, true, false, false, false, '');

$pdf->SetFont('helvetica', '', 8);

$tbl = <<<EOD
<h3>Labour Working Details:-</h3>
<table cellspacing="0" cellpadding="1" border="1">
    <tr>
        <th>S.No</th>
        <th>Date </th>                    
        <th>Total Worked shift </th>
		<th>Total Extra Hour </th>
        
    </tr>
    


EOD;
$pdf->SetFont('helvetica', '', 8);
$sno=0;
while($row = mysqli_fetch_array($query_date))
    {
        $edit_id  = $row["enameid"];
    //$nestedData[] = $row["ename"];

    
    $invoicedate   = new DateTime($row["invoicedate"]);

   // $nestedData[] = $invoicedate->format('d-m-Y');
    
        $sno++;
        $invoicedate_arr  =   $row["invoicedate"];
        $invoicedate_arr =$invoicedate->format('d-m-Y');
        $shifttype_arr    =   $row["shifttype"];
		$extrahour_arr    =   $row["extraworkinghrs"];
        

$tbl.= <<<EOD
    <tr>
        <td>$sno</td>
        <td>$invoicedate_arr</td>
        <td>$shifttype_arr</td>
        <td>$extrahour_arr</td>
    </tr>
EOD;
    }
$tbl.= <<<EOD
    </table>
EOD;
$pdf->writeHTML($tbl, true, false, false, false, '');



$tbl_step4 = <<<EOD
<table cellspacing="0" cellpadding="1" >
    <tr>
        <td></td>
        <td></td>
        <td>Total Shift : $shifttype  ($shifttype  * $shiftworkamt)</td>
        
    </tr>
	
	<tr>
        <td></td>
        <td></td>
        <td>Total Extra Hour : $extraworkinghrs  ($extraworkinghrs  * $extrahoursamt)</td>
        
    </tr>
	
</table>           

EOD;

// print a block of text using Write()
$pdf->writeHTML($tbl_step4, true, false, false, false, '');

$pdf->SetFont('helvetica', 'B', 12);
$total_salary = number_format((($shifttype*$shiftworkamt)+($extraworkinghrs  * $extrahoursamt)),2,'.','');

$tbl_step5 = <<<EOD
<table cellspacing="0" cellpadding="1" >
    <tr>
        <td></td>

        <td> Total Amount : $total_salary  </td>
        
    </tr>
</table>           

EOD;

// print a block of text using Write()
$pdf->writeHTML($tbl_step5, true, false, false, false, '');

/*$tbl_step6 = <<<EOD
<table cellspacing="0" cellpadding="1" >
    <tr>
        <td></td>
  
        <td>Collect Advance Amount : $collectadvanceamt </td>
        
    </tr>
</table>           
EOD;*/
//$pdf->writeHTML($tbl_step6, true, false, false, false, '');

$salaryamttemp = $total_salary-$collectadvanceamt; 
/*$tbl_step7 = <<<EOD
<table cellspacing="0" cellpadding="1" >
    <tr>
        <td></td>

        <td>Salary  : $salaryamttemp </td>
        
    </tr>
</table>           
EOD;
$pdf->writeHTML($tbl_step7, true, false, false, false, '');*/


//Close and output PDF document
$pdf->Output('example_048.pdf', 'I');
   
?>