<?php
//call the FPDF library
//require('fpdf181/fpdf.php');
$this->load->library('Fpdf_Master');

//$today = date('Y-m-d'));
//A4 width : 219mm
//default margin : 10mm each side
//writable horizontal : 219-(10*2)=189mm

//$tt =  $user;
//class PDF_sample extends FPDF

$data= '';   

class PDF_sample extends FPDF {
  
  	private $data = array();
  	private $inv = array();
  	private $inv_itm = array();
  	private $inv_pay = array();
  	private $inv_trn = array();
  	private $chq_trn = array();
  	private $chq_col = array();
  	private $tm = array();
  	private $options = array(
  		'inv' => '',
  		'destinationfile' => '',
  		'paper_size'=>'F4',
  		'orientation'=>'P'
  	);
  	
  	function __construct($data = array(), $options = array(),$inv = array(),$inv_itm = array(),$inv_pay= array(),$inv_trn= array(),$chq_trn = array(),$chq_col = array(),$tm = array()) {
    	parent::__construct();
    	$this->data = $data;
    	$this->inv = $inv;
    	$this->inv_itm = $inv_itm;
    	$this->inv_pay = $inv_pay;
    	$this->inv_trn = $inv_trn;
    	$this->chq_trn = $chq_trn;
    	$this->chq_col = $chq_col;
    	$this->options = $options;
    	$this->tm = $tm;
	}
	

	
	function Footer()
{
    // Go to 1.5 cm from bottom
   
    $this->SetY(-15);
    
//	$this->Image('assets/images/meslogo.png',8,10,-300);  //$this->Image('img/cargologo.jpg',15,890,40,40);
//	$this->Image('img/cargologo.jpg',15,540,50,50);
	 
	 //$this->Ln(2);
$this->SetFont('Arial','B',8);
//$this->Cell(5 ,-3,'CREATED USER & DATE : ' .$this->inv[0]->grn_cuser.' / '.$this->inv[0]->grn_cdate,0,0);//end of line
//$this->Cell(5 ,5,$this->inv[0]->grn_euser,0,0);//end of line

   $this->SetFont('Arial','',7);
	 
//	$this->SetX(0); $this->Cell(0, 10, $this->tm[1][0]->pro_fslogans, 0, 0,'C');
//	$this->Ln(2);
//	$this->SetX(0); $this->Cell(0, 12, $this->tm[1][0]->pro_sfslogans, 0, 0,'C');
    // Select Arial italic 8
    $this->SetFont('Arial','',7);
	
    // Print centered page number
    //$this->Cell(0,10,'Page '.$this->PageNo(),0,0,'R');
    $this->Cell(0,10,'Page '.$this->PageNo().' of {nb}',0,0,'R');
     $this->Line(18,283,205,283);
	
}
	
	public function rptDetailData () {
		
		//
		$border = 0;
		$this->AddPage();
		$this->SetAutoPageBreak(False);
		$this->SetAutoPageBreak(true, 15);
		$this->AliasNbPages();
		$left = 18;
		$left1 = 5;
		//$left = 15;
		
		
	$this->SetLeftMargin($left);


   
$this->SetFont('Arial','B',12);	

$this->Cell(0 ,5,$this->tm[1]->pro_name ,0,1,'C');//end of line
$this->SetFont('Arial','',10);	
$this->Cell(0 ,5,$this->tm[1]->pro_add,0,1,'C');//end of line
	$this->SetFont('Arial','B',10);	
	if(isset($this->inv[0]->user_fname)){
	$this->Cell(0 ,5,'DAILY SALES REPORT - '.$this->tm[0]. ' To '.$this->tm[2] .' | Sales Person - '.$this->inv[0]->user_fname.' '.$this->inv[0]->user_lname.' | Depot - '.$this->inv[0]->dep_name,0,1,'C');//end of line 
	}else{
	    	$this->Cell(0 ,5,'DAILY SALES REPORT - '.$this->tm[0]. ' To '.$this->tm[2] .' | Sales Person - ALL | Depot - ',0,1,'C');//end of line 

	}
	//$this->SetFont('Arial','',10);	
	//$this->Cell(0 ,5,'Sales Person - '.$this->inv[0]->user_fname.' '.$this->inv[0]->user_lname.' | Depot - '.$this->inv[0]->dep_name,0,1,'C');//end of line
	
	//include
	 //$this->Ln(2);
     //$this->Cell(77);
     //$this->SetFont('Arial','B',10);
    //$this->Cell(111,10,'INVOICE',0,0,'L');
	//set font to arial, regular, 12pt

//bill end
//$this->Cell(1 ,4,$sup,0,0);//end of line
//$this->Cell(130 ,5,'[Street Address]',0,0);
//$this->Cell(59 ,4,'',0,1);//end of line


//$podt = date("d-M-Y", strtotime($this->inv[0]->ret_cdate));
//$this->SetFont('Arial','',8);
//$this->Cell(130 ,4,'',0,0);
//$this->Cell(33 ,4,'DATE',0,0);
//$this->Cell(34 ,4,': '.strtoupper($podt),0,1);//end of line



//if($this->inv[0]->grn_poid!=""){$po=$this->inv[0]->grn_poid;}else{$po="N/A";}
//$this->Cell(130 ,4, $repad ,0,0);
//$this->Cell(33 ,4,'RETURN NOTE No.',0,0);
//$this->Cell(34 ,4,': '.$this->inv[0]->ret_code.$this->inv[0]->ret_id,0,1);//end of line

//$this->Cell(130 ,4, strtoupper($repad1)  ,0,0);
//$this->Cell(33 ,4,'GRN NUMBER.',0,0);
///$this->Cell(34 ,4,': '.$this->inv[0]->grn_code.$this->inv[0]->grn_id,0,1);//end of line

//$this->Cell(130 ,4, strtoupper($repad2) ,0,0);
//$this->Cell(33 ,4,'GRN DATE.',0,0);
//$this->Cell(34 ,4,': '.strtoupper(date("d-M-Y", strtotime($this->inv[0]->grn_cdate))),0,1);//end of line

//if(isset($this->inv[0]->cnty_name)){
// $this->Cell(130 ,4,strtoupper($this->inv[0]->cnty_name)  ,0,0);   
//}
//$this->Cell(33 ,4,'',0,0);
//$this->Cell(34 ,4,': '.$po,0,1);//end of line

//$this->Cell(130 ,5,strtoupper($this->inv[0]->cnty_name)  ,0,0);

//$this->Cell(33 ,4,'MES REF.No.',0,0);
//$this->Cell(40 ,4,': ',0,1);//end of line


$this->Cell(130 ,4,'' ,0,0);
$this->SetFont('Arial','',8);
$this->Cell(33 ,4,'',0,0);
//$this->Cell(40 ,4,': ',0,1);//end of line


	
	//end include

//set font to arial, regular, 12pt
$this->SetFont('Arial','B',10);



//bill end
//$this->Cell(1 ,4,$sup,0,0);//end of line
//$this->Cell(130 ,5,'[Street Address]',0,0);
$this->Cell(59 ,4,'',0,1);//end of line


//$repad = strtoupper(stripslashes($this->inv[0]->sup_addres));
//$repad = iconv('UTF-8', 'windows-1252', $repad);

//$repad1 = strtoupper(stripslashes($this->inv[0]->sup_add1));
//$repad1 = iconv('UTF-8', 'windows-1252', $repad1);

//$repad2 = strtoupper(stripslashes($this->inv[0]->sup_add2));
//$repad2 = iconv('UTF-8', 'windows-1252', $repad2);


//make a dummy empty cell as a vertical spacer
//$this->Cell(40 ,10,'',0,1);//end of line

//$this->Ln(4);
		


		$INVi=$paycnt=0;
        $total=0;
        $itmcount=0;
        $invtotal=0;
        $invpaytotal=0;
        $creditsalecnt=0;
        
        
        
        foreach ($this->inv_itm as $post)   { 
            //if($post->serv_name!=""){$itm_name = $post->serv_name;
            //}else{$itm_name = $post->brand_name." ".$post->itm_name.$post->serv_name."\n".$post->part_no."\n".$post->model_name;}
            
       
          
          $INVi= $INVi+1;
          if(($post[7] - $post[9])!= 0.00){
              $paycnt = $paycnt+1;
          }
          
           if(($post[7] - $post[9]) == 0.00){
              $creditsalecnt = $creditsalecnt+1;
          }
          
      $invtotal = $invtotal + $post[7] ;
      $invpaytotal = $invpaytotal + $post[9] ;
      
       // $total = number_format(($totalold),2,".",",");
          //  $itmcount = $itmcount+$post[4];
        }
        
        $creditsale = $invpaytotal;
        $blance = $invtotal - $invpaytotal;
        //$blance = $invtotal - $creditsale;
        
        
      //summary
      //$this->SetFont('Arial','B',9);
//$this->Cell(92 ,5,'',0,0);
//$this->Cell(73 ,5,'',0,0);
//$this->Cell(28 ,5,'',1,0);
//$this->Cell(22 ,5,number_format($invtotal,2),1,1,'R');//end of line  
        


		$payi=$payo=0;
		$expayi=0;
		$payhqi=0;
        $totalpay=0;
        $itmcount=0;
        $totachqlpay=0;
        $totachqlpayi=0;
        $cashpay=0;
        $cdeposit =0;
        $cdepositi =0;
        $expey=0;
        $csh=0;
        $cshi=0;
        
        
        
        foreach ($this->inv_pay as $post)   { 
            //if($post->serv_name!=""){$itm_name = $post->serv_name;
            //}else{$itm_name = $post->brand_name." ".$post->itm_name.$post->serv_name."\n".$post->part_no."\n".$post->model_name;}
            
           
            
      

          
      /*    $h = 4;
		$left = 40;
		$top = 10;	
		#tableheader
		//$this->SetFillColor(200,200,200);	
		$left = $this->GetX();
		$this->SetFont('Arial','',8);
	//	$this->Cell(6,$h,'',1,0,'C',true);
		$this->SetX($left += 0); $this->Cell(187, $h,$post[5], 1, 1, 'C',true); */
       
          
          $payi= $payi+1;
      $totalpay = $totalpay + $post[4] ;
      	if($post[7]==1){
			    
			    $totachqlpay = $totachqlpay + $post[4] ;
			    $payhqi= $payhqi+1;
			}
			
			 if($post[3]=="Bank Deposit"){
			     
			      $cdeposit = $cdeposit + $post[4] ;
			      $cdepositi = $cdepositi + 1;
			 }
			 
			  if($post[3]=="Cash"){
			     
			      $csh = $csh + $post[4] ;
			      
			      $cshi= $cshi+1;
			 }
			 
		    if($post[3]=="Payment Excess" || $post[3]=="Return Excess" || $post[3]=="Old Return"){
			     
			      $expey = $expey + $post[4] ;
			      $expayi = $expayi +1;
			      $payo= $payo+1;
			 }
			    
			    $cashpay = $totalpay - ($totachqlpay);
			   
		
       // $total = number_format(($totalold),2,".",",");
          //  $itmcount = $itmcount+$post[4];
        }
        


		$i=0;
        $total=0;
        $itmcount=0;
        
        
        
        foreach ($this->inv_trn as $post)   { 
            //if($post->serv_name!=""){$itm_name = $post->serv_name;
            //}else{$itm_name = $post->brand_name." ".$post->itm_name.$post->serv_name."\n".$post->part_no."\n".$post->model_name;}
            

          
          $i= $i+1;
      //$total = $total + $post[4] ;
       // $total = number_format(($totalold),2,".",",");
          //  $itmcount = $itmcount+$post[4];
        }
        
        
        
   
        
        // cheq return
        
                        $this->Ln(2);


		$i=0;
        $total=0;
        $itmcount=0;
        
        //print_r($this->chq_trn);
        foreach ($this->chq_trn as $post)   { 
          ////  echo $post;
            //if($post->serv_name!=""){$itm_name = $post->serv_name;
            //}else{$itm_name = $post->brand_name." ".$post->itm_name.$post->serv_name."\n".$post->part_no."\n".$post->model_name;}
            
    
          
          $i= $i+1;
      //$total = $total + $post[4] ;
       // $total = number_format(($totalold),2,".",",");
          //  $itmcount = $itmcount+$post[4];
        }
        
        
        //cheque collection
        
                               // $this->Ln(2);
     //$this->Cell(77);


		$stli=0;
        $totalsetl=0;
        $itmcount=0;
        
        
        
        foreach ($this->chq_col as $post)   { 
            //if($post->serv_name!=""){$itm_name = $post->serv_name;
            //}else{$itm_name = $post->brand_name." ".$post->itm_name.$post->serv_name."\n".$post->part_no."\n".$post->model_name;}
            

          
            $stli= $stli+1;
            $totalsetl = $totalsetl + $post[3] ;
       // $total = number_format(($totalold),2,".",",");
          //  $itmcount = $itmcount+$post[4];
        }
        
        
        
        $this->Ln(5);
        
        $this->SetFont('Arial','B',12);	
	$this->Cell(0 ,0,'Summary',0,1,'C');//end of line
	 $this->Ln(15);
	 $this->SetFont('Arial','',10);	
//summary
$this->Cell(40 ,5,'',0,0);
$this->Cell(65 ,5,'TOTAL SALES',0,0);
$this->Cell(15 ,5,$INVi,1,0,'R');
$this->Cell(28 ,5,number_format($invtotal,2),1,1,'R');//end of line



$this->Cell(40 ,5,'',0,0);
$this->Cell(65 ,5,'CREDIT SALE',0,0);
$this->Cell(15 ,5,$paycnt,1,0,'R');
//$this->Cell(28 ,5,number_format($creditsale,2),1,1,'R');//end of line
$this->Cell(28 ,5,number_format($blance,2),1,1,'R');//end of line

$this->Cell(40 ,5,'',0,0);
$this->Cell(65 ,5,'BALANCE',0,0);
$this->Cell(15 ,5,$creditsalecnt,1,0,'R');
//$this->Cell(28 ,5,number_format($blance,2),1,1,'R');//end of line
$this->Cell(28 ,5,number_format($creditsale,2),1,1,'R');//end of line

$this->Cell(40 ,5,'',0,0);
$this->Cell(65 ,5,'CREDIT SETTLE',0,0);
$this->Cell(15 ,5,$payi-$payo,1,0,'R');
//$this->Cell(28 ,5,number_format(($totalpay-($blance + $expey)),2),1,1,'R');//end of line
$this->Cell(28 ,5,number_format($totalpay - ($invpaytotal+$expey),2),1,1,'R');//end of line

$this->Cell(40 ,5,'',0,0);
$this->Cell(65 ,5,'ACTUAL SALE',0,0);
$this->Cell(15 ,5,$payi-$payo,1,0,'R');
$actual_p = $creditsale+($totalpay-$expey);
$this->Cell(28 ,5,number_format($totalpay - $expey,2),1,1,'R');//end of line

$this->Cell(40 ,5,'',0,0);
$this->Cell(65 ,5,'TOTAL CHEQUE',0,0);
$this->Cell(15 ,5,$payhqi,1,0,'R');
$this->Cell(28 ,5,number_format($totachqlpay,2),1,1,'R');//end of line

$this->Cell(40 ,5,'',0,0);
$this->Cell(65 ,5,'CASH',0,0);
$this->Cell(15 ,5,$cshi,1,0,'R');
$this->Cell(28 ,5,number_format($csh,2),1,1,'R');//end of line

$this->Cell(40 ,5,'',0,0);
$this->Cell(65 ,5,'CASH DEPOSIT',0,0);
$this->Cell(15 ,5,$cdepositi,1,0,'R');
$this->Cell(28 ,5,number_format($cdeposit,2),1,1,'R');//end of line



$this->Cell(40 ,5,'',0,0);
$this->Cell(65 ,5,'RET CHEQUE COLLECTION',0,0);
$this->Cell(15 ,5,$stli,1,0,'R');
$this->Cell(28 ,5,number_format($totalsetl,2),1,1,'R');//end of line

$this->Cell(40 ,5,'',0,0);
$this->Cell(65 ,5,'EXCESS PAYMENT & OLD RETURN',0,0);
$this->Cell(15 ,5,$expayi,1,0,'R');
$this->Cell(28 ,5,number_format($expey,2),1,1,'R');//end of line



//$dd = ($this->inv[0]->po_discount * 100)/$total;
//$dispre = number_format((float)$dd, 2, '.', '');

$this->Cell(92 ,5,'',0,0);
//$this->Cell(38 ,5,'DISCOUNT '.$dispre.'%',0,0);
//$this->Cell(28 ,5,$this->inv[0]->curr_symble,1,0);
//$this->Cell(28 ,5,number_format($this->inv[0]->po_discount,2),1,1,'R');//end of line
//$this->Cell(32 ,5,$post->discount.'%'.number_format($post->dis_price,2),1,1,'R');//end of line
//$divd = ($total-($this->inv[0]->po_discount));

//$this->Cell(92 ,5,'',0,0);
//$this->Cell(38 ,5,'NET TOTAL',0,0);
//$this->Cell(28 ,5,$this->inv[0]->curr_symble,1,0);
//$this->Cell(28 ,5,number_format($total-$this->inv[0]->po_discount,2),1,1,'R');//end of line
//$this->Cell(32 ,5,$post->discount.'%'.number_format($post->dis_price,2),1,1,'R');//end of line

//$divd = ($total-($this->inv[0]->po_discount));

//$taxspre = ($this->inv[0]->po_tax * $divd)/100 ;






//$this->Cell(92 ,5,'',0,0);
//$this->Cell(38 ,5,'TOTAL',0,0);
//$this->Cell(28 ,5,$this->inv[0]->curr_symble,1,0);
//$this->Cell(28 ,5,number_format($this->inv[0]->po_final_tot,2),1,1,'R');//end of line

//$this->Ln(2);
//$this->SetFont('Arial','B',8);
//$this->Cell(12 ,5,'ITEMS: ',0,0);//end of line
//$this->Cell(130 ,5,'[Street Address]',0,0);
//$this->SetFont('Arial','',8);
//$this->Cell(10 ,5,$i,0,0);//end of line
//$this->SetFont('Arial','B',8);
//$this->Cell(19 ,5,'QUANTITY: ',0,0);//end of line
//$this->SetFont('Arial','',8);
//$this->Cell(10 ,5,$total,0,0);//end of line
//$this->SetFont('Arial','B',8);
//$this->Cell(19 ,5,'TOTAL VALUE: ',0,0);//end of line
//$this->SetFont('Arial','',8);
//$this->Cell(30 ,5,$total,0,1);//end of line
//$this->AliasNbPages();
//$this->AddPage();

//$this->Ln(2);
//$this->SetFont('Arial','B',8);
//$this->Cell(30 ,5,'RETURN REASON :',0,0);
//$this->SetFont('Arial','',8);
//$this->Cell(150 ,5,$this->inv[0]->ret_reason,0,1);
//end of line

//$this->Ln(3);
//$this->SetFont('Arial','',8);

//$this->Cell(150 ,5,'---------------------',0,0);//end of line
//$this->Cell(130 ,5,'[Street Address]',0,0);
//$this->Cell(30 ,5,'----------------------',0,1);//end of line
//$this->SetFont('Arial','B',8);
//$this->Cell(50 ,5,'TERMS & CONDITIONS :',0,1);//end of line
//$this->SetFont('Arial','',10);
//$this->Cell(150 ,5,"",0,0);//end of line



//$this->inv[0]->po_tc;
$this->SetFont('Arial','',9);
$this->Ln(15);
//$this->Cell(10 ,5,'',0,0);//end of line
$this->Cell(80 ,5,'-------------------------',0,0);//end of line
$this->Cell(70 ,5,'-------------------------',0,0);//end of line
$this->Cell(40 ,5,'-------------------------',0,1);//end of line

//$this->Cell(80 ,2,'',0,0);//end of line

$this->Cell(80 ,2,'     Prepared by',0,0);//end of line
$this->Cell(70 ,2,'     Checked by',0,0);//end of line
$this->Cell(40 ,2,'    Authorized By',0,1);//end of line


//$this->Ln(5);
$this->SetFont('Arial','',8);


	}

	public function printPDF () {
				
	
		
	    $this->SetAutoPageBreak(false);
	    $this->AliasNbPages();
	    $this->SetFont("helvetica", "B", 10);
	    //$this->AddPage();
		
		
		
		
		
	    $this->rptDetailData();
			    
	    $this->Output();
  	}
  	
  	private $widths;
	private $aligns;

	function SetWidths($w)
	{
		//Set the array of column widths
		$this->widths=$w;
	}

	function SetAligns($a)
	{
		//Set the array of column alignments
		$this->aligns=$a;
	}

	function Row($data)
	{
		//Calculate the height of the row
		$nb=0;
		for($i=0;$i<count($data);$i++)
			$nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
		$h=4*$nb;
		//Issue a page break first if needed
		$this->CheckPageBreak($h);
		//Draw the cells of the row
		for($i=0;$i<count($data);$i++)
		{
			$w=$this->widths[$i];
			$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
			//Save the current position
			$x=$this->GetX();
			$y=$this->GetY();
			//Draw the border
			$this->Rect($x,$y,$w,$h);
			//Print the text
			$this->MultiCell($w,4,$data[$i],0,$a);
			//Put the position to the right of the cell
			$this->SetXY($x+$w,$y);
		}
		//Go to the next line
		$this->Ln($h);
	}

	function CheckPageBreak($h)
	{
		//If the height h would cause an overflow, add a new page immediately
		if($this->GetY()+$h>$this->PageBreakTrigger)
			
			$this->AddPage($this->CurOrientation);
	
	}

	function NbLines($w,$txt)
	{
		//Computes the number of lines a MultiCell of width w will take
		$cw=&$this->CurrentFont['cw'];
		if($w==0)
			$w=$this->w-$this->rMargin-$this->x;
		$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
		$s=str_replace("\r",'',$txt);
		$nb=strlen($s);
		if($nb>0 and $s[$nb-1]=="\n")
			$nb--;
		$sep=-1;
		$i=0;
		$j=0;
		$l=0;
		$nl=1;
		while($i<$nb)
		{
			$c=$s[$i];
			if($c=="\n")
			{
				$i++;
				$sep=-1;
				$j=$i;
				$l=0;
				$nl++;
				continue;
			}
			if($c==' ')
				$sep=$i;
			$l+=$cw[$c];
			if($l>$wmax)
			{
				if($sep==-1)
				{
					if($i==$j)
						$i++;
				}
				else
					$i=$sep+1;
				$sep=-1;
				$j=$i;
				$l=0;
				$nl++;
			}
			else
				$i++;
		}
		return $nl;
	}
} //end of class


$options = array(
	'inv' => $inv, //nama file penyimpanan, kosongkan jika output ke browser
	'destinationfile' => '', //I=inline browser (default), F=local file, D=download
	'paper_size'=>'F4',	//paper size: F4, A3, A4, A5, Letter, Legal
	'orientation'=>'P' //orientation: P=portrait, L=landscape

);

$tabel = new PDF_sample($data,$options,$inv,$inv_itm,$inv_pay,$inv_trn,$chq_trn,$chq_col,$tm);
//$tabel->printPDF();
$tabel->printPDF();
?>