<?php

App::import('Vendor','tcpdf/config/lang/eng');
App::import('Vendor','tcpdf/tcpdf');


class TestsreportsPDF  extends TCPDF {

	var $xEmployee = '';
	var $xheadertext  = 'PDF created using CakePHP and TCPDF'; 
	var $xClient = '';

	var $xDate = '';

	var $xDspId = '';

    //Page header
    public function Header() {
        // Logo
        $image_file = K_PATH_IMAGES.'logo.png';
//		$this->Cell(0, 0, $image_file, 1, 1, 'C', 0, '', 1);
        $this->Image($image_file, 15, 10, 95, 23, 'PNG', '', 'M', 2, 300, 'C', false, false, 0, false, false, false);
        // Set font
        $this->SetFont('helvetica', 'B', 20);
    }

    // Page footer
    public function Footer() {
        // Position at 15 mm from bottom
        $this->SetY(-20);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // Page number
		//Cell ($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M')
		//$this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, 1, 'C', 0, '', 0, false, 'T', 'M');
        //$this->Cell(0, 10, $this->xAddressTestmetrix, 'T', 0, 'C', 0, '', 0, false, 'T', 'M');
    }
	
    function HeadingLetter()
    {		 
		$this->SetFontSize(10);		
		$this->Ln(9);	
		// writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=0, $reseth=true, $align='', $autopadding=true)
		$this->writeHTMLCell('', '', '', 40, $this->xrightReportTitle0, 0, 1, 0, true, 'R', true);	
		$this->writeHTMLCell('', '', '', 55, $this->xrightReportTitle1, 0, 1, 0, true, 'C', true);
		$this->writeHTMLCell('', '', '', 65, $this->xrightReportTitle2, 0, 1, 0, true, 'L', true);	
#		$this->writeHTMLCell(40, '', '', '', $this->xrightInvoiceTitle2, 0, 0, 0, true, 'L', true);	
    }		
	
/*	
	function SubjectLetter()
	{	
		// writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=0, $reseth=true, $align='', $autopadding=true)	
		$this->writeHTMLCell(130, '', '', 80, $this->xSubjectTitle1, 0, 0, 0, true, 'L', true);
		// write the second column
		$this->writeHTMLCell(50, '', '', '', $this->xSubjectTitle2, 0, 1, 0, true, 'L', true);	
		$this->Ln(2);	
	}	
*/
	
    function ItemTable($header,$items) 
	{		
		// header spacing
		$this->Cell(0,5); 
		$this->Ln();
			
        //Colors, line width and bold font
        $this->SetFillColor(201,255,255);
        $this->SetTextColor(0,0,0);
        $this->SetDrawColor(255,255,255);
        $this->SetLineWidth(.3);
        //$this->SetFont('','B');
		$this->SetFont('helvetica', 'B', 8);
        //Table Header
		//array('Qty','Description','Price','Total');
		//array('#',$infoCustomerInvoice['Customer']['abbrv'].' Number','Team','Requester','Employee','Service','Sent by','Status','Price'
        //$w=array(40,85,30,35);
		$w=array(55,22,40,40,31,31,17,22,20);
		
        for($i=0;$i<count($header);$i++)
		//Cell ($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M')
        //$this->Cell($w[$i],20,$header[$i],1,0,'C',true, '',0);
		// writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=0, $reseth=true, $align='', $autopadding=true)
		$this->writeHTMLCell($w[$i], '', '', '', $header[$i], 1, 0, 1, true, 'C', true);	
		
        $this->Ln();
				
        //Color and font restoration
        $this->SetFillColor(248,248,248);
        $this->SetTextColor(0);
        //$this->SetFont(9);
		$this->SetFont('helvetica', '', 8.5);

		//$this->SetCellPadding(3);
		$lineHeights = array('');

        $fill=0;
		//echo pr($items);
		$a = 1;

		$customerId = 0;
		$numRows = 0;
		
		$completed = 0; $completedInit = 0; $completedFollow = 0;		
		$Sumcompleted = 0; $SumcompletedInit = 0; $SumcompletedFollow = 0;		
		$canceledUser = 0; $canceledUserInit = 0; $canceledUserFollow = 0;		
		$SumcanceledUser = 0; $SumcanceledUserInit = 0; $SumcanceledUserFollow = 0;				
		$canceledAdmin = 0; $canceledAdminInit = 0; $canceledAdminFollow = 0;
		$SumcanceledAdmin = 0; $SumcanceledAdminInit = 0; $SumcanceledAdminFollow = 0;		
		$autoCanceled1 = 0;  $autoCanceled1Init = 0; $autoCanceled1Follow = 0;
		$autoCanceled2 = 0;  $autoCanceled2Init = 0; $autoCanceled2Follow = 0;		
		$SumautoCanceled1 = 0;  $SumautoCanceled1Init = 0; $SumautoCanceled1Follow = 0;
		$SumautoCanceled2 = 0; $SumautoCanceled2Init = 0; $SumautoCanceled2Follow = 0;
		$pendComplQuest1 = 0; $pendComplQuest1Init = 0; $pendComplQuest1Follow = 0; 
		$pendComplQuest2 = 0; $pendComplQuest2Init = 0; $pendComplQuest2Follow = 0;
		$SumpendComplQuest1 = 0; $SumpendComplQuest1Init = 0; $SumpendComplQuest1Follow = 0;
		$SumpendComplQuest2 = 0; $SumpendComplQuest2Init = 0; $SumpendComplQuest2Follow = 0;		
		$SumcanceledEmp = 0; $SumcanceledEmpInit = 0; $SumcanceledEmpFollow = 0;
		$canceledEmp = 0; $canceledEmpInit = 0; $canceledEmpFollow = 0;
		
		$customerName = '';
		$testsfrec = array();
		$numRows = count($items);
		
        foreach($items as $row) {			
			if ($a == 1) {
				$customerId = $row['Customer']['id']; 		
			} 			
			//echo pr($row);
			// same customer
			if ($customerId == $row['Customer']['id']) {
				if($row['Status']['id'] == 13) {
					$completed = $row[0]['nbrTest'];
					if($row[0]['test_serie'] == 1){
						$completedInit = $row[0]['nbrTest']; 
					} elseif($row[0]['test_serie'] == 9) {
						$completedFollow = $row[0]['nbrTest'];
					}
				} elseif($row['Status']['id'] == 7) {
					$canceledUser = $row[0]['nbrTest'];
					if($row[0]['test_serie'] == 1){
						$canceledUserInit = $row[0]['nbrTest']; 
					} elseif($row[0]['test_serie'] == 9) {
						$canceledUserFollow = $row[0]['nbrTest'];
					}	
				} elseif($row['Status']['id'] == 14) {
					$autoCanceled1 = $row[0]['nbrTest'];
					if($row[0]['test_serie'] == 1){
						$autoCanceled1Init = $row[0]['nbrTest']; 
					} elseif($row[0]['test_serie'] == 9) {
						$autoCanceled1Follow = $row[0]['nbrTest'];
					}
				} elseif($row['Status']['id'] == 15) {
					$canceledAdmin = $row[0]['nbrTest'];
					if($row[0]['test_serie'] == 1){
						$canceledAdminInit = $row[0]['nbrTest']; 
					} elseif($row[0]['test_serie'] == 9) {
						$canceledAdminFollow = $row[0]['nbrTest'];
					}
				} elseif($row['Status']['id'] == 10) {
					$pendComplQuest1 = $row[0]['nbrTest'];
					if($row[0]['test_serie'] == 1){
						$pendComplQuest1Init = $row[0]['nbrTest']; 
					} elseif($row[0]['test_serie'] == 9) {
						$pendComplQuest1Follow = $row[0]['nbrTest'];
					}
				} elseif($row['Status']['id'] == 11) {
					$pendComplQuest2 = $row[0]['nbrTest'];
					if($row[0]['test_serie'] == 1){
						$pendComplQuest2Init = $row[0]['nbrTest']; 
					} elseif($row[0]['test_serie'] == 9) {
						$pendComplQuest2Follow = $row[0]['nbrTest'];
					}					
				} elseif($row['Status']['id'] == 16) {
					$canceledEmp = $row[0]['nbrTest'];
					if($row[0]['test_serie'] == 1){
						$canceledEmpInit = $row[0]['nbrTest']; 
					} elseif($row[0]['test_serie'] == 9) {
						$canceledEmpFollow = $row[0]['nbrTest'];
					}										
				} elseif($row['Status']['id'] == 18) {
					$autoCanceled2 = $row[0]['nbrTest'];
					if($row[0]['test_serie'] == 1){
						$autoCanceled2Init = $row[0]['nbrTest']; 
					} elseif($row[0]['test_serie'] == 9) {
						$autoCanceled2Follow = $row[0]['nbrTest'];
					}									
				}
				$customerName = $row['Customer']['name'];
			} else {
				$testsfrec = array_merge($testsfrec, array(array('cust' => $customerName, 
					'completed' => $completed, 'completedInit' => $completedInit, 'completedFollow' => $completedFollow,  
					'pendComplQuest1' => $pendComplQuest1, 'pendComplQuest1Init' => $pendComplQuest1Init, 'pendComplQuest1Follow' => $pendComplQuest1Follow,
					'pendComplQuest2' => $pendComplQuest2, 'pendComplQuest2Init' => $pendComplQuest2Init, 'pendComplQuest2Follow' => $pendComplQuest2Follow, 
					'autoCanceled1' => $autoCanceled1, 'autoCanceled1Init' => $autoCanceled1Init, 'autoCanceled1Follow' => $autoCanceled1Follow,
					'autoCanceled2' => $autoCanceled2, 'autoCanceled2Init' => $autoCanceled2Init, 'autoCanceled2Follow' => $autoCanceled2Follow, 
					'canceledUser' => $canceledUser, 'canceledUserInit' => $canceledUserInit, 'canceledUserFollow' => $canceledUserFollow, 
					'canceledEmp' => $canceledEmp, 'canceledEmpInit' => $canceledEmpInit, 'canceledEmpFollow' => $canceledEmpFollow,
					'canceledAdmin' => $canceledAdmin, 'canceledAdminInit' => $canceledAdminInit, 'canceledAdminFollow' => $canceledAdminFollow)));
					
				$customerId = $row['Customer']['id'];
				$completed = 0; $completedInit = 0; $completedFollow = 0;
				$canceledUser = 0; $canceledUserInit = 0; $canceledUserFollow = 0;
				$canceledAdmin = 0; $canceledAdminInit = 0; $canceledAdminFollow = 0;
				$autoCanceled1 = 0; $autoCanceled1Init = 0; $autoCanceled1Follow = 0;
				$pendComplQuest1 = 0; $pendComplQuest1Init = 0; $pendComplQuest1Follow = 0;
				$pendComplQuest2 = 0; $pendComplQuest2Init = 0; $pendComplQuest2Follow = 0;
				$canceledEmp = 0; $canceledEmpInit = 0; $canceledEmpFollow = 0;
				$autoCanceled2 = 0; $autoCanceled2Init = 0; $autoCanceled2Follow = 0;
					
				if($row['Status']['id'] == 13) {
					$completed = $row[0]['nbrTest'];
					if($row[0]['test_serie'] == 1){
						$completedInit = $row[0]['nbrTest'];
					} elseif($row[0]['test_serie'] == 9) {
						$completedFollow = $row[0]['nbrTest'];
					}
				} elseif($row['Status']['id'] == 7) {
					$canceledUser = $row[0]['nbrTest'];
					if($row[0]['test_serie'] == 1){
						$canceledUserInit = $row[0]['nbrTest'];
					} elseif($row[0]['test_serie'] == 9) {
						$canceledUserFollow = $row[0]['nbrTest'];
					}					
				} elseif($row['Status']['id'] == 14) {
					$autoCanceled1 = $row[0]['nbrTest'];
					if($row[0]['test_serie'] == 1){
						$autoCanceled1Init = $row[0]['nbrTest'];
					} elseif($row[0]['test_serie'] == 9) {
						$autoCanceled1Follow = $row[0]['nbrTest'];
					}
				} elseif($row['Status']['id'] == 15) {
					$canceledAdmin = $row[0]['nbrTest'];
					if($row[0]['test_serie'] == 1){
						$canceledAdminInit = $row[0]['nbrTest'];
					} elseif($row[0]['test_serie'] == 9) {
						$canceledAdminFollow = $row[0]['nbrTest'];
					}					
				} elseif($row['Status']['id'] == 10) {
					$pendComplQuest1 = $row[0]['nbrTest'];
					if($row[0]['test_serie'] == 1){
						$pendComplQuest1Init = $row[0]['nbrTest'];
					} elseif($row[0]['test_serie'] == 9) {
						$pendComplQuest1Follow = $row[0]['nbrTest'];
					}										
				} elseif($row['Status']['id'] == 11) {
					$pendComplQuest2 = $row[0]['nbrTest'];
					if($row[0]['test_serie'] == 1){
						$pendComplQuest2Init = $row[0]['nbrTest'];
					} elseif($row[0]['test_serie'] == 9) {
						$pendComplQuest2Follow = $row[0]['nbrTest'];
					}										
				} elseif($row['Status']['id'] == 16) {
					$canceledEmp = $row[0]['nbrTest'];
					if($row[0]['test_serie'] == 1){
						$canceledEmpInit = $row[0]['nbrTest'];
					} elseif($row[0]['test_serie'] == 9) {
						$canceledEmpFollow = $row[0]['nbrTest'];
					}										
				} elseif($row['Status']['id'] == 18) {
					$autoCanceled2 = $row[0]['nbrTest'];
					if($row[0]['test_serie'] == 1){
						$autoCanceled2Init = $row[0]['nbrTest'];
					} elseif($row[0]['test_serie'] == 9) {
						$autoCanceled2Follow = $row[0]['nbrTest'];
					}									
				}				
				
				$customerName = $row['Customer']['name'];			
			}
			if ($a == $numRows) {
				$testsfrec = array_merge($testsfrec, array(array('cust' => $customerName, 
					'completed' => $completed, 'completedInit' => $completedInit, 'completedFollow' => $completedFollow,
					'pendComplQuest1' => $pendComplQuest1, 'pendComplQuest1Init' => $pendComplQuest1Init, 'pendComplQuest1Follow' => $pendComplQuest1Follow, 
					'pendComplQuest2' => $pendComplQuest2, 'pendComplQuest2Init' => $pendComplQuest2Init, 'pendComplQuest2Follow' => $pendComplQuest2Follow, 
					'autoCanceled1' => $autoCanceled1, 'autoCanceled1Init' => $autoCanceled1Init, 'autoCanceled1Follow' => $autoCanceled1Follow,
					'autoCanceled2' => $autoCanceled2, 'autoCanceled2Init' => $autoCanceled2Init, 'autoCanceled2Follow' => $autoCanceled2Follow, 
					'canceledUser' => $canceledUser, 'canceledUserInit' => $canceledUserInit, 'canceledUserFollow' => $canceledUserFollow,
					'canceledEmp' => $canceledEmp, 'canceledEmpInit' => $canceledEmpInit, 'canceledEmpFollow' => $canceledEmpFollow,
					'canceledAdmin' => $canceledAdmin, 'canceledAdminInit' => $canceledAdminInit, 'canceledAdminFollow' => $canceledAdminFollow)));
			} 
			$a++;
        }	
		
        foreach($testsfrec as $xs) {
		// MultiCell($w, $h, $txt, $border=0, $align='J', $fill=0, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0)		

			$this->MultiCell($w[0], '',$xs['cust'], 0, 'R', $fill, 0, '' ,'', true);
					$lineHeights[] = $this->getLastH();
			$this->MultiCell($w[1],'', $xs['completedInit'].' | '.$xs['completedFollow'], 0, 'C', $fill, 0, '' ,'', true);
					$Sumcompleted = $Sumcompleted + $xs['completedInit'] + $xs['completedFollow'];
					$lineHeights[] = $this->getLastH();
			$this->MultiCell($w[2], '', $xs['pendComplQuest1Init'].' | '.$xs['pendComplQuest1Follow'] , 0, 'C', $fill, 0, '' ,'', true);
					$SumpendComplQuest1 = $SumpendComplQuest1 + $xs['pendComplQuest1Init'] + $xs['pendComplQuest1Follow'];
					$lineHeights[] = $this->getLastH();						
			$this->MultiCell($w[3], '', $xs['pendComplQuest2Init'].' | '.$xs['pendComplQuest2Follow'] , 0, 'C', $fill, 0, '' ,'', true);
					$SumpendComplQuest2 = $SumpendComplQuest2 + $xs['pendComplQuest2Init'] + $xs['pendComplQuest2Follow'];
					$lineHeights[] = $this->getLastH();											
			$this->MultiCell($w[4], '', $xs['autoCanceled1Init'].' | '.$xs['autoCanceled1Follow'], 0, 'C', $fill, 0, '' ,'', true);
					$SumautoCanceled1 = $SumautoCanceled1 + $xs['autoCanceled1Init'] + $xs['autoCanceled1Follow'];
					$lineHeights[] = $this->getLastH();						
			$this->MultiCell($w[5], '', $xs['autoCanceled2Init'].' | '.$xs['autoCanceled2Follow'], 0, 'C', $fill, 0, '' ,'', true);
					$SumautoCanceled2 = $SumautoCanceled2 + $xs['autoCanceled2Init'] + $xs['autoCanceled2Follow'];
					$lineHeights[] = $this->getLastH();						
			$this->MultiCell($w[6],'', $xs['canceledUserInit'].' | '.$xs['canceledUserFollow'], 0, 'C', $fill, 0, '' ,'', true);
					$SumcanceledUser = $SumcanceledUser + $xs['canceledUserInit'] + $xs['canceledUserFollow'];
					$lineHeights[] = $this->getLastH();		
			$this->MultiCell($w[7], '', $xs['canceledEmpInit'].' | '.$xs['canceledEmpFollow'], 0, 'C', $fill, 0, '' ,'', true);					
					$SumcanceledEmp = $SumcanceledEmp + $xs['canceledEmpInit'] + $xs['canceledEmpFollow'];
					$lineHeights[] = $this->getLastH();
			$this->MultiCell($w[8], '', $xs['canceledAdminInit'].' | '.$xs['canceledAdminFollow'], 0, 'C', $fill, 0, '' ,'', true);					
					$SumcanceledAdmin = $SumcanceledAdmin + $xs['canceledAdminInit'] + $xs['canceledAdminFollow'];
					$lineHeights[] = $this->getLastH();					
            $this->Ln(max($lineHeights));
						$lineHeights = array('');
            $fill=!$fill;
			$a++;
        }
		$this->Ln(4);
		$this->MultiCell($w[0], '', 'Total', 0, 'C', 0, 0, '' ,'', true);
		$this->MultiCell($w[1], '', $Sumcompleted, 'T', 'C', 0, 0, '' ,'', true);
		$this->MultiCell($w[2], '', $SumpendComplQuest1, 'T', 'C', 0, 0, '' ,'', true);
		$this->MultiCell($w[3], '', $SumpendComplQuest2, 'T', 'C', 0, 0, '' ,'', true);
		$this->MultiCell($w[4], '', $SumautoCanceled1, 'T', 'C', 0, 0, '' ,'', true);
		$this->MultiCell($w[5], '', $SumautoCanceled2, 'T', 'C', 0, 0, '' ,'', true);
		$this->MultiCell($w[6], '', $SumcanceledUser, 'T', 'C', 0, 0, '' ,'', true);
		$this->MultiCell($w[7], '', $SumcanceledEmp, 'T', 'C', 0, 0, '' ,'', true);
		$this->MultiCell($w[8], '', $SumcanceledAdmin, 'T', 'C', 0, 0, '' ,'', true);		
    }	


    function AmountSection()
    {		 
		$this->SetFontSize(9);		
		$this->Ln(5);	
		// writeHTMLCell($w, $h, $x, $y, $html='', $border=0, $ln=0, $fill=0, $reseth=true, $align='', $autopadding=true)
		$this->writeHTMLCell(130, '', '', '', '', 0, 0, 0, true, 'L', true);	
		$this->writeHTMLCell(50, '', '', '', $this->xsubTotalTexte, 0, 0, 0, true, 'L', true);	
		$this->writeHTMLCell(15, '', '', '', $this->xsubTotal, 0, 1, 0, true, 'R', true);
    }	
		
}
?>