<?php
define('FPDF_FONTPATH', 'font/');
require('./font/fpdf.php');
include "include/config.inc.php";
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
	
  if($_REQUEST['partyId'] > 0)
  {
	$selectClientAavk = "SELECT SUM(vkg) AS aKg,SUM(vTotalAmt) AS aTotalAmt
	                      FROM vepari
					   WHERE veTransType = 'A'
				    AND partyId = ".$_REQUEST['partyId'];
	$selectClientAavkRes = mysql_query($selectClientAavk);
	if($cavakRow = mysql_fetch_assoc($selectClientAavkRes))
	{
     	  $aKg          = $cavakRow['aKg'];
	  $aTotalAmt    = $cavakRow['aTotalAmt'];  
	}
    
	$selectClientJavak = "SELECT SUM(vkg) AS javakKg,SUM(vTotalAmt) AS javaktotalAmt
	                   FROM vepari
					  WHERE veTransType = 'J'
				 AND partyId = ".$_REQUEST['partyId'];
	$selectClientJavakRes = mysql_query($selectClientJavak);
	if($cjavakRow = mysql_fetch_assoc($selectClientJavakRes))
	{
      $javakKg          = $cjavakRow['javakKg'];
	  $javaktotalAmt    = $cjavakRow['javaktotalAmt'];  
	}
	
	
	if($javakKg > 0)
	{
	  $finalKg     = $javakKg  - $aKg;
	}
	else
	{
	$finalKg     = $aKg - $javakKg;
	}
	 $finalAmount = $javaktotalAmt - $aTotalAmt;
	 	

   $select = "SELECT partyName FROM vepari
			  JOIN party ON party.partyId = vepari.partyId
			  WHERE vepari.partyId = ".$_REQUEST['partyId'];
   $res = mysql_query($select);
   if($row = mysql_fetch_array($res))
   {
	 $partyName = $row['partyName'];
   }
  }	
  $pdf=new FPDF('P','mm','A4');   //Create new pdf file
  $pdf->Open();     //Open file
  $pdf->SetAutoPageBreak(false);  //Disable automatic page break
  $pdf->AddPage();  //Add first page
  $pdf->SetFont('Arial','B',10);
  $pdf->SetXY(50,5);
  
  $pdf->SetFont('Arial','B',10);  
  $pdf->SetXY(10,5);
  $pdf->Cell(90,7,'Party Name :  '.$partyName,0);

  $pdf->SetXY(10,20);
  $pdf->Cell(90,7,'Particular',0);
  $pdf->SetXY(50,20);
  $pdf->Cell(90,7,'Date',0);
  $pdf->SetXY(70,20);
  $pdf->Cell(90,7,'Rate',0);
  $pdf->SetXY(90,20);
  $pdf->Cell(90,7,'Diff',0);
  $pdf->SetXY(110,20);
  $pdf->Cell(90,7,'Diff Amt',0);
  $pdf->SetXY(130,20);
  $pdf->Cell(90,7,'Total',0);
  $pdf->SetXY(150,20);
  $pdf->Cell(90,7,'Kg',0);
  $pdf->SetXY(170,20);
  $pdf->Cell(90,7,'Total Amt',0);
  
  $o=0;
  $quatationDetail = "SELECT *
                        FROM  vepari
					WHERE partyId = ".$_REQUEST['partyId'];
  $quatationDetailRes = mysql_query($quatationDetail);
  $itemY = 25;
  while($detailRow = mysql_fetch_array($quatationDetailRes))
  {
	$pdf->SetFont('Arial','',10);  
    $pdf->SetXY(10,$itemY);
    $pdf->Cell(90,7,$detailRow['vParti'],0);
	$pdf->SetXY(45,$itemY);
    $pdf->Cell(90,7,$detailRow['vDate'],0);
	$pdf->SetXY(70,$itemY);
    $pdf->Cell(90,7,$detailRow['vRate'],0);
	$pdf->SetXY(90,$itemY);
    $pdf->Cell(90,7,$detailRow['vDiff'],0);
	$pdf->SetXY(110,$itemY);
    $pdf->Cell(90,7,$detailRow['vDiffAmt'],0);
	$pdf->SetXY(130,$itemY);
    $pdf->Cell(90,7,$detailRow['vTotal'],0);
	$pdf->SetXY(150,$itemY);
    $pdf->Cell(90,7,$detailRow['vkg'],0);
	$pdf->SetXY(170,$itemY);
    $pdf->Cell(90,7,$detailRow['vTotalAmt'],0);
    $pdf->Ln();
    $itemY += 5;
    $o++;
  }
  $pdf->SetFont('Arial','B',10);  
  $pdf->SetXY(65,$itemY);
  $pdf->Cell(90,7,'K.g :  '.$finalKg,0);
  $pdf->SetXY(95,$itemY);
  $pdf->Cell(90,7,'Amount :  '.$finalAmount,0);
  $pdf->Ln();
  $pdf->Output();
}
?>