<?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 $tm = array();
  	private $options = array(
  		'inv' => '',
  		'destinationfile' => '',
  		'paper_size'=>'F4',
  		'orientation'=>'P'
  	);
  	
  	function __construct($data = array(), $options = array(),$inv = array(),$inv_itm = array(),$tm = array()) {
    	parent::__construct();
    	$this->data = $data;
    	$this->inv = $inv;
    	$this->inv_itm = $inv_itm;
    	$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 = 5;
		$left1 = 5;
		//$left = 15;
		
		
	$this->SetLeftMargin($left);


   
//  $this->SetFont('Arial','B',12);	
//	$this->Cell(140 ,5,'ITEM SUMMERY- '.$this->inv['ref'].' FROM '.$this->inv['fdate'].' TO '.$this->inv['todate'],0,1,'R');//end of line
	
		$this->SetFont('Arial','B',12);	

$this->Cell(0 ,5,'Neha Products' ,0,1,'C');//end of line
$this->SetFont('Arial','',10);	
	//$this->Cell(0 ,5,'Araliyakale Farm, Loluwagoda, 11204, Sri Lanka.',0,1,'C');//end of line
	$this->SetFont('Arial','B',10);	
	$this->Cell(0 ,5,'ITEM OVERVIEW REPORT - From '.$this->inv['fdate'].' To '.$this->inv['todate'].'  Store - '.$this->inv['depo_name']  ,0,1,'C');//end of line 
	$this->SetFont('Arial','',10);


$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);


$this->Ln(4);
//	$this->Cell(0 ,5, $this->inv_itm[0][0].' | '.$this->inv_itm[0][1]  ,0,1,'L');//end of line 
		
      //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  
 
 
 $inv_bal=array(); 
 foreach ($this->inv_itm as $postt)   {   
       
    if(isset($postt[0][1])){
         $this->Ln(6);
     //$this->Cell(77);
    $this->SetFont('Arial','B',10);
        $prod = $postt[0][1];
        
        $this->Cell(111,5,$prod." - Rs ".$postt[0][12],0,0,'L',0);
     $this->Ln(5);
		
		$h = 5;
		$left = 40;
		$top = 10;	
		#tableheader
		$this->SetFillColor(200,200,200);	
		$left = $this->GetX();
		$this->SetFont("Arial", "B", 8);
		$this->Cell(15, $h, 'TYPE', 1, 0, 'C',true);
		$this->SetX($left += 15); $this->Cell(20, $h, 'ID', 1, 0, 'C',true);
		$this->SetX($left += 20); $this->Cell(20, $h, 'LAST STOCK', 1, 0, 'C',true);
		$this->SetX($left += 20); $this->Cell(12, $h, 'OUT', 1, 0, 'C',true);
		$this->SetX($left += 12); $this->Cell(12, $h, 'IN', 1, 0, 'C',true);
		$this->SetX($left += 12); $this->Cell(20, $h, 'NOW STOCK', 1, 0, 'C',true);
		$this->SetX($left += 20); $this->Cell(34, $h, 'DATE', 1, 0, 'C',true);
		//$this->SetX($left += 34); $this->Cell(20, $h, 'STOCK', 1, 0, 'C',true);
		$this->SetX($left += 34); $this->Cell(18, $h, 'USER', 1, 0, 'C',true); 
		$this->SetX($left += 18); $this->Cell(50, $h, 'REASON', 1, 1, 'C',true);
		
		$this->SetFont('Arial','',9);
		$this->SetWidths(array(15,20,20,12,12,20,34,18,50));
		$this->SetAligns(array('L','L','R','R','R','R','R','R','L')); 
		//$this->SetFontStyle(array('B','B','B','B','B','B','B','B'));
		$no = 1; $this->SetFillColor(255);

		$i=0; $in =0; $out =0; 
        $total= $total_qty = $total_val = $total_rtn = $total_rtn_val = $total_rtn_value = $total_amnt = 0;
        $itmcount=0;
        if(isset($postt[0][6])){
            $start = $postt[0][6];
       $nstart = $postt[0][6];
        }else{
            $start = 0;
       $nstart = 0;
        }
       
        foreach ($postt as $post)   { 
            $start = $start-$post[9]+$post[10];
            if($post[9]<1){$p9 = "";}else{$p9 = $post[9];}
            if($post[10]<1){$p10 = "";}else{$p10=$post[10];}
            
            $this->Row(

            array(
            
			$post[2],
			$post[3],
			$nstart,
			$p9,
			$p10,
			$start,
			$post[5],
			$post[8],
			$post[7]
			
          ));
      
      $nstart = $nstart-$post[9]+$post[10];
          $in = $in +$post[9];
          $out = $out +$post[10];
          $i= $i+1;
          
      
    }
    $inv_bal[] = array($prod,$start);    
    $this->SetFont('Arial','B',10);
   // $this->Cell(55 ,5,"",0,0);
   $this->Cell(0 ,5,"In - ".$out." = ".number_format($out*$post[12],2),0,1,'L');
   // $this->Cell(0 ,5,"Out - ".$in." = ".number_format($in*$post[12],2)." / In - ".$out." = ".number_format($out*$post[12],2),0,1,'L');
    //$this->Cell(12 ,5,$out,0,1,'R'); 
   // $this->Cell(55 ,5,"",0,0);
   // $this->Cell(12 ,5,$in*$post[12],0,'R');
   // $this->Cell(12 ,5,$out*$post[12],0,1,'R');
    
    
    }else{$prod="";}
   
 

}
        
 //return Items
 
 $this->Ln(4);
     //$this->Cell(77);
    
//$this->Cell(105 ,5,'',0,0);

//$this->Cell(2 ,5,'',0,0);
//$this->Cell(15 ,5,'TOTAL',0,0);
//$this->Cell(28 ,5,"Rs",1,0);
//$this->Cell(25 ,5,number_format($total,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->SetFont('Arial','B',11);
$this->Cell(25 ,5,"Stock Balance",0,1);
 $this->Ln(2);
$this->SetFont('Arial','',10);


for($i=0; $i<count($inv_bal); $i++){
    if($inv_bal[$i][1]>0){
         $this->Cell(45 ,5,$inv_bal[$i][0],0,0);
    $this->Cell(15 ,5,"",0,0);
    $this->Cell(25 ,5,$inv_bal[$i][1],0,1,'R');
    }
    
    
}




//$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(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(70 ,2,'     Requested 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,$tm);
//$tabel->printPDF();
$tabel->printPDF();
?>