<?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']);

}

///// !!!!!!!!!!!!!!!  company Master -------------------------


$querycom=mysqli_query($con,"  SELECT * FROM company_master  ")or die(mysqli_error($con));
  while($rowc=mysqli_fetch_array($querycom)){   
       $cost_name=$rowc['cost_name'];
       $address=$rowc['address'];
       $telephone=$rowc['telephone'];
       $email=$rowc['email'];
       $web=$rowc['web'];
  }

////////////////////////////////////////////////////////////////////





/////!!!!!!!!!!!!!!!!!!  Open inv_header   !!!!!!!!!!!!!!!!!!!!!!!!!!!!
$query=mysqli_query($con,"  SELECT inv_header.*,customer_master.cus_name,stock_locations.loc_name,invoice_types.des,root_master.r_name,employee_master.emp_name 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 
    LEFT JOIN root_master ON inv_header.r_id=root_master.r_id
     LEFT JOIN employee_master ON inv_header.emp_id=employee_master.emp_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'];
    $r_name=$rowsp['r_name'];
     $emp_name=$rowsp['emp_name'];
  } ////  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->SetLeftMargin(4);
$pdf->SetRightMargin(4);
$pdf->AddPage(); 
//$pdf->SetFont('helvetica','',10);
$pdf->SetFont('times', '', 10);


$pdf->SetY(5);
// Center the company name
//$pdf->SetX(($pdf->GetPageWidth() - $pdf->GetStringWidth($cost_name)) / 2);

$companyNameWidth = $pdf->GetPageWidth();
$pdf->SetX(0);
$pdf->MultiCell($companyNameWidth, 5, $cost_name, 0, 'C');

$pdf->Ln(0.0001);

$contactInfoX = ($pdf->GetPageWidth() - $pdf->GetStringWidth($address)) / 2;

// Print the address
$pdf->SetX(0);
$pdf->MultiCell($companyNameWidth, 5, $address, 0, 'C');
//$pdf->Ln(); // Move to the next line
$pdf->Ln(0.0001);

// Print the telephone
$pdf->SetX(0);
$pdf->MultiCell($companyNameWidth, 5, "Tel: $telephone", 0, 'C');
//$pdf->Ln(); // Move to the next line
$pdf->Ln(0.0001);

// Print the email
$pdf->SetX(0);
$pdf->MultiCell($companyNameWidth, 5, "Email: $email", 0, 'C');
//$pdf->Ln(); // Move to the next line
$pdf->Ln(0.0001);

// Print the web
$pdf->SetX(0);
$pdf->MultiCell($companyNameWidth, 5, "Web: $web", 0, 'C');

// Move to the next line after printing the contact information
//$pdf->Ln();
$pdf->Ln(0.0010);

$pdf->SetX(0);
$pdf->MultiCell($companyNameWidth, 8, "Sales Invoice", 0, 'C');

// Move to the next line after printing the contact information
//$pdf->Ln();
$pdf->Ln(0.0001);



//////////////// End of Company Name ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~






$pdf->SetFont('courier', '',7);


$pdf->SetX(0);
$table1=new easyTable($pdf, '{35,10,70}','align:L{LLL;border:1; border-color:#ffffff; ');
//$table1->easyCell('Sales Invoice', 'font-size:10; font-style:B; font-color:#000000;align:C;');
//$table1->easyCell('', 'img:../dist/img/anura_logo1.jpg, w20; align:R;');
//$table1->printRow();
$pdf->SetFont('courier', '', 7);


 //$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;');
 $table1->easyCell('Invoice No');
 $table1->easyCell(':');
 $table1->easyCell($inv_no);
 $table1->printRow(); 

 $reqdate=date("Y-m-d",strtotime($inv_date));
 $table1->easyCell('Date');
 $table1->easyCell(':');
 $table1->easyCell($reqdate);
 $table1->printRow(); 

 $table1->easyCell('Customer');
 $table1->easyCell(':');
 $pdf->SetFont('courier', '', 7);
 $table1->easyCell($cus_name);
 $table1->printRow(); 

  $table1->easyCell('Route');
 $table1->easyCell(':');
 $pdf->SetFont('courier', '', 7);
 $table1->easyCell($r_name);
 $table1->printRow();

  $table1->easyCell('Sales Rep');
 $table1->easyCell(':');
 $pdf->SetFont('courier', '', 7);
 $table1->easyCell($emp_name);
 $table1->printRow();
 //emp_name

//$pdf->SetY($pdf->GetY() + 2); // Adjust the Y position as needed
$pdf->SetX(5); // Adjust the X position as needed
$pdf->Cell(5, 5, '------------------------------------------', 0, 1, 'L');

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

 $table1->endTable(1);
 $table=new easyTable($pdf, '{130,70,40,95}','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('Product');
 $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:1;' . $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->easyCell(number_format($tot_sales,2),'font-size:8;');
        
          $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;
                                    }

$pdf->SetX(5); // Adjust the X position as needed
$pdf->Cell(5, 5, '------------------------------------------', 0, 1, 'L');

 if ($discount<>0) {
$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(' ');  
 $table->easyCell($total_qty,'font-size:8;','align:R;');
  $table->easyCell( number_format($sub_total,2),'font-size:8;','align:R;');
 $table->printRow();    


 $pdf->SetX(5); // Adjust the X position as needed
$pdf->Cell(5, 5, '------------------------------------------', 0, 1, 'L');

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



$pdf->SetX(5); // Adjust the X position as needed
$pdf->Cell(5, 5, '------------------------------------------', 0, 1, 'L');
}


 //$table->rowStyle('align:{LLRR};valign:M;bgcolor:#ffffff; font-color:#000000; font-family:times; font-style:B;');
  $table->rowStyle('align:{LRRR};valign:M;bgcolor:#ffffff; font-color:#000000;  font-style:B;');
 $table->easyCell('Net Total','font-size:8;');
 $table->easyCell(' '); 
 $table->easyCell(' '); 
 $table->easyCell( number_format($net_total,2),'font-size:8;','align:R;','font-style:B;');  
 $table->easyCell('','font-size:8;','align:R;');
 $table->printRow(); 

 $table->endTable(2);


//$pdf->SetX(2); // Adjust the X position as needed
//$pdf->Cell(5, 5, 'Sales Count : ' , 0, 1, 'L');
$pdf->MultiCell($companyNameWidth, 2, "Sales Count: $ncount", 0, 'L');
$pdf->Ln(2);

$pdf->SetX(4); // Adjust the X position as needed
$pdf->Cell(5, 5, 'Goods Received By : -----------------------', 0, 1, 'L');
$pdf->SetX(4); // Adjust the X position as needed
$pdf->Cell(5, 5, 'Customer          : -----------------------', 0, 1, 'L');

//$rcount
$pdf->Cell(5, 5, ' ', 0, 1, 'L');
$pdf->Cell(5, 5, '----------- END ------------', 0, 1, 'L');



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