<?php
session_start();
include 'fpdf.php';
include 'exfpdf.php';
include 'easyTable.php';

include('../includes/dbcon.php');


$userid=$_SESSION['hrm_id'];
 $user_name=$_SESSION['hrm_name'];

if (isset($_GET['inv_no'])) { 
  $inv_no= trim($_GET['inv_no']);
} 



if (isset($_POST['inv_no'])) { 
$inv_no= trim($_POST['inv_no']);

}

//$inv_no=442;

/////!!!!!!!!!!!!!!!!!!  Open inv_header   !!!!!!!!!!!!!!!!!!!!!!!!!!!!
$query=mysqli_query($con,"  SELECT inv_header.*,customer_master.cus_name,stock_locations.loc_name,invoice_types.des FROM inv_header
 LEFT JOIN customer_master ON inv_header.cus_id=customer_master.cus_id
 LEFT JOIN stock_locations ON stock_locations.loc_id=inv_header.loc_id 
  LEFT JOIN invoice_types ON inv_header.invtype_id=invoice_types.invtype_id 
  where inv_id='$inv_no'  ")or die(mysqli_error($con));
$count=mysqli_num_rows($query);

if ($count>0)
{

  while($rowsp=mysqli_fetch_array($query)){     
    //$grnd_no=$rowd['grnd_no'];  
    $inv_date=$rowsp['inv_date'];
    $loc_name=$rowsp['loc_name']; 
    $cus_name=$rowsp['cus_name'];
   // $invd_ref=$rowsp['invd_ref'];
    $note=$rowsp['note'];
    $discount=$rowsp['discount'];
    $des=$rowsp['des'];
  } ////  while($rowsp=mysqli_fetch_array($query)){ 



}


//$pdf=new PDF("P","mm",array(76.2,150));
$pdf = new exFPDF('P','mm',array(76.2,150));
$pdf->AliasNbPages();
$pdf->AddPage(); 
$pdf->SetFont('helvetica','',10);




$table1=new easyTable($pdf, 2);
$table1->easyCell('Invoice', 'font-size:14; font-style:B; font-color:#000000;');
//$table1->easyCell('', 'img:../dist/img/anura_logo1.jpg, w20; align:R;');
$table1->printRow();


 $table1->rowStyle('font-size:10; ');
 $table1->easyCell("<b>Inv: $inv_no");
  
 $table1->printRow();
 

 
 $reqdate=date("Y-m-d",strtotime($inv_date));
 $table1->rowStyle('font-size:9; ','align:L;');
 $table1->easyCell("<b>Date: $reqdate"); 
 $table1->printRow();

 $table1->rowStyle('font-size:9; ','align:L;');
 $table1->easyCell("<b>Loc: $loc_name"); 
 $table1->printRow();

 
 $table1->rowStyle('font-size:9; ','align:L;');
 $table1->easyCell("<b>Cus:</b> $cus_name"); 
 $table1->printRow();

  $table1->rowStyle('font-size:9; ','align:L;');
 $table1->easyCell("<b>Type:</b> $des"); 
 $table1->printRow();

//////////////////////////////////////  DEtail

 $table1->endTable(5);
 $table=new easyTable($pdf, '{20,130,70,40}','align:L{LLRR;border:0; border-color:#ffffff; ');
 $table->rowStyle('align:{LLRR};valign:M;bgcolor:#ffffff; font-color:#000000; font-family:times; font-style:B;');
 $table->easyCell('#');
 $table->easyCell('Item');
 $table->easyCell('Price');
 $table->easyCell('Qty');
  $table->easyCell('Total');
 
 $table->printRow();


                 $sub_total=0;
                                    $inv_discount=0;
                                    $net_total=0;
                                    $total_qty=0;
          $ncount=0;
          $queryd=mysqli_query($con," SELECT * FROM inv_detail
LEFT JOIN item_master ON item_master.item_id=inv_detail.item_id where inv_id='$inv_no' ")or die(mysqli_error($con));
          while($row=mysqli_fetch_array($queryd)){               
              //$rcount = $rcount + 1;
              $item_name=$row['item_name'];
              $qty=$row['qty'];
              $sales_price=$row['sales_price'];
              $item_dis=$row['item_dis'];

                   $tot_sales=($sales_price * $row['qty'])-($sales_price * $row['qty'] *  $item_dis/100);
                 $sub_total= $sub_total + $tot_sales ;

      $total_qty=$total_qty + $row['qty'];
      $ncount=$ncount+1;

          $bgcolor='';
          if($ncount%2)
          {
            $bgcolor='bgcolor:#ffffff;';
          }
  
          $table->rowStyle('valign:M;border:LR;paddingY:2;' . $bgcolor);
          $table->easyCell($ncount,'font-size:6;');
          $table->easyCell($item_name,'font-size:7;');
           $table->easyCell(number_format($sales_price,2),'font-size:7;');
          $table->easyCell($qty,'font-size:7;');  
          
        
          $table->printRow();


          $table->rowStyle('valign:M;border:LR;paddingY:2;' . $bgcolor);
           $table->easyCell('');
           $table->easyCell('Line tot:','font-size:8;');
           $table->easyCell(number_format($tot_sales,2),'font-size:8;');
            $table->easyCell(''); 
           $table->printRow();

    
          }

 if ($discount<>0) {

                                    $inv_discount= $sub_total * $discount/100;
                                    $net_total=$sub_total - $inv_discount;


                                   }
                                   else
                                    { 
                                       $net_total=$sub_total;
                                    }



$table->rowStyle('align:{LLRR};');
// $table->easyCell(' ', 'border:T;colspan:1'); 
 //$table->easyCell(' ', 'border:T;colspan:1');
  $table->easyCell(' ');  
 $table->easyCell('Sub Total','font-size:8;');
 $table->easyCell( number_format($sub_total,2),'font-size:8;','align:R;');  
 $table->easyCell($total_qty,'font-size:8;','align:R;');

 $table->printRow();    

 $table->easyCell(' ');  
 $table->easyCell('Dis','font-size:8;');
 $table->easyCell( number_format($inv_discount,2),'font-size:8;','align:R;');  
 $table->easyCell('','font-size:8;','align:R;');
 $table->printRow();   


 $table->easyCell(' ');  
 $table->easyCell('Total','font-size:8;');
 $table->easyCell( number_format($net_total,2),'font-size:8;','align:R;');  
 $table->easyCell('','font-size:8;','align:R;');
 $table->printRow(); 

 $table->endTable(10);




////////////////////////End of Detail
  $pdf->Cell(0,10,'Page '.$pdf->PageNo().'/{nb}',0,0,'C');
  $pdf->Output();
?>