<?php
include("include/config.inc.php");
include_once("./mpdf/mpdf.php");
$mpdf = new mPDF('c', 'A4-L',0,'',$mgl=8,$mgr=8,$mgt=8,$mgb=8);
$mpdf->falseBoldWeight = 8;
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
  $partyId = isset($_REQUEST['partyId']) ? $_REQUEST['partyId'] : 0;
  
	if($partyId > 0)
	{
		$k = 0;
		$partyName = "";
		$clientArray = array();
		$selectClient = "SELECT partyId,partyName FROM party WHERE partyId=".$partyId." LIMIT 1";
		$selectClientRes = mysql_query($selectClient);
		$clientRow = mysql_fetch_array($selectClientRes);	
		$partyName = $clientRow['partyName'];

		$k = 0;
		$clientDetail = array();
		$selectClient = "SELECT * FROM vepari
						 WHERE vepari.partyId = ".$partyId."
						 AND vstatus = 'A'		
						 AND saudaId > 0					 
						ORDER BY vepariId ASC";
		$selectClientRes = mysql_query($selectClient);
		while($clientRow = mysql_fetch_array($selectClientRes))
		{ 
		  $clientDetail[$k]['vepariId']     = $clientRow['vepariId'];
		  $clientDetail[$k]['vRate']        = $clientRow['vRate'];
		  $clientDetail[$k]['particular']   = $clientRow['particular'];
		  $clientDetail[$k]['veTransType']  = $clientRow['veTransType'];
		  $clientDetail[$k]['vDate']        = date('d-m-Y',strtotime($clientRow['vDate']));
		  $clientDetail[$k]['rate']    	    = $clientRow['rate'];
		  $clientDetail[$k]['kg']      		= $clientRow['kg'];
		  $clientDetail[$k]['totalAmt']     = $clientRow['totalAmt'];
		  $clientDetail[$k]['sell_type']    = $clientRow['sell_type'];
		  $k++;
		}
		
		//////////////////////////////////////Buy : End //////////////////////////////////
	
	
		//////////////////////////////////////Sell : Start //////////////////////////////////
			
			$kk = 0;
		$clientDet = array();
		$selectClient = "SELECT *,acc_type FROM vepari
						LEFT JOIN rojmel ON rojmel.rojmelId = vepari.rojmelId
						 WHERE vepari.partyId = ".$partyId."
						 AND  vstatus = 'A'
						 AND vepari.rojmelId > 0
						 ORDER BY vepariId ASC";
		$selectClientRes = mysql_query($selectClient);
		while($clientRow = mysql_fetch_array($selectClientRes))
		{
		  $clientDet[$kk]['vepariId']     = $clientRow['vepariId'];
		  $clientDet[$kk]['particular']   = $clientRow['particular'];
		  $clientDet[$kk]['veTransType']  = $clientRow['veTransType'];
		  $clientDet[$kk]['vDate']        = date('d-m-Y',strtotime($clientRow['vDate']));
		  $clientDet[$kk]['rate']         = $clientRow['rate'];
		  $clientDet[$kk]['kg']           = $clientRow['kg'];
		  $clientDet[$kk]['rojmelId']     = $clientRow['rojmelId'];
		  $clientDet[$kk]['sell_type']    = $clientRow['sell_type'];
		  $clientDet[$kk]['totalAmt']     = round($clientRow['totalAmt'],3);
		  $kk++;
		}

			
			$selectClient = "SELECT * FROM vepari
						 WHERE partyId = ".$partyId."
					 AND  vstatus = 'A'
					  AND is_opening = 1
					  AND rojmelId = 0
					 ORDER BY vepariId ASC";
		$selectClientRes = mysql_query($selectClient);
		while($clientRow = mysql_fetch_array($selectClientRes))
		{
		  $clientDet[$kk]['vepariId']     = '';
		  $clientDet[$kk]['particular']   = $clientRow['particular'];
		  $clientDet[$kk]['veTransType']  = $clientRow['veTransType'];
		  $clientDet[$kk]['cDate']        = '';
		  $clientDet[$kk]['vDate']        = '';
		  $clientDet[$kk]['rate']         = '';
		  $clientDet[$kk]['is_opening']   = $clientRow['is_opening'];
		  $clientDet[$kk]['kg']           = $clientRow['kg'];
		  $clientDet[$kk]['rojmelId']     = '';
		  $clientDet[$kk]['sell_type']    = '';
		  $clientDet[$kk]['totalAmt']     = round($clientRow['totalAmt'],0);
		  $kk++;
		} 
		
		//////////////////////////////////////Sell : End //////////////////////////////////
	
	$selectClientAavk = "SELECT SUM(kg) AS aKg,SUM(totalAmt) AS aTotalAmt
							  FROM vepari
						   WHERE veTransType = 'A'
						AND partyId = ".$partyId;
		$selectClientAavkRes = mysql_query($selectClientAavk);
		if($cavakRow = mysql_fetch_assoc($selectClientAavkRes))
		{
		  $aKg          = $cavakRow['aKg'];
		  $aTotalAmt    = $cavakRow['aTotalAmt'];  
		}
    
		$selectClientJavak = "SELECT SUM(kg) AS javakKg,SUM(totalAmt) AS javaktotalAmt
						   FROM vepari
						  WHERE veTransType = 'J'
					AND partyId = ".$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 = $aTotalAmt - $javaktotalAmt;
	}
}
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Vepari List</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <!-- Bootstrap 3.3.6 -->
    <link rel="stylesheet" href="./css/bootstrap.min.css">
    <style>
    td,
    th {
        padding: 5px;
        white-space: nowrap;
    }

    td {
        border-bottom-style: none;
        border-top-style: none;
    }

    .center {
        text-align: center;
    }

    .right {
        text-align: right;
    }

    h2 {
        font-family: "Impact", Charcoal, sans-serif;
    }

    .border1 {
        border: 1;
    }

    .divRight {
        text-align: right;
    }

    .text_bold {
        font-weight: bold;
    }

    .text_center {
        text-align: center;
    }

    .text_left {
        text-align: left;
    }

    .text_right {
        text-align: right;
    }

    .no-border-top {
        border-top: 0;
    }

    .no-border-bottom {
        border-bottom: 0 !important;
    }

    .no-border-left {
        border-left: 0;
    }

    .no-border-right {
        border-right: 0;
    }

    .no-border {
        border: 0;
    }

    .border-right {
        border-right: 1px solid black;
        border-left: 0;
        border-top: 0;
        border-bottom: 0;
    }

    .text-bold {
        font-weight: 800 !important;
    }

    .text-nowrap {
        white-space: nowrap !important;
    }
    </style>
</head>
<body>
	<table  class="border1" style="width: 100%; font-size:13px;">
    	<thead>
    	<tr> 
				  <th>Vepari Name : <?php echo $partyName; ?></th>
		  </tr>
    	</thead>
    	</table>
   		<table cellspacing="0"  style="width: 100%; font-size:13px;" border="1" align="center">
			<tr>
				<td>
					<table cellspacing="14">
						<tr><td colspan="7" align="center">Buy</td></tr>
						<tr>
							<td>Acc Type</td>
							<td>Sell Type</td>
							<td>Particular</td>
							<td>Date</td>
							<td>Rate</td>
							<td>K.G.</td>
							<td>Total Amount</td>
						</tr>
						<?php
						foreach($clientDetail as $sec => $cd_row) {
						?>
						<tr>							
							<?php if($cd_row['sell_type'] == 'D') { ?>
								<td>Direct</td>
					 		<?php } else { ?>
								<td>Khata</td>
							<?php } ?>

							<?php if($cd_row['sell_type'] == 'J') { ?>
						   	<td>Sell</td>
						 	<?php } else { ?>
						   	<td>Buy</td>
						 	<?php } ?>
							<td><?php echo $cd_row['particular'] ?></td>
							<td><?php echo $cd_row['cDate'] ?></td>
							<td><?php echo $cd_row['rate'] ?></td>
							<td><?php echo number_format((float)$cd_row['kg'], 3, '.', '') ?></td>
							<td><?php echo $cd_row['totalAmt'] ?></td>
						</tr>
					  <?php
						}
						?>
					</table>
				</td>
				<td>
					<table cellspacing="14">
						<tr><td colspan="5" align="center">Rojmel Entry</td></tr>
						<tr>
							<td>Type</td>
							<td>Particular</td>
							<td>Date</td>
							<td>K.G.</td>
							<td>Total Amount</td>
						</tr>
						<?php
						foreach($clientDet as $secnew => $cd_row) {
						?>
						<tr>
							<?php if($cd_row['cliTransType'] == 'J' && $cd_row['is_opening'] == '1') { ?>
								<td>Baki</td>
							<?php } else if($cd_row['cliTransType'] == 'A' && $cd_row['is_opening'] == '1')  { ?>
								<td>Jama</td>
							<?php } else if($cd_row['cliTransType'] == 'A' && $cd_row['is_opening'] == '0')  { ?>
							    <td>Aavak</td>
							<?php } else { ?>
							    <td>Javak</td>
							<?php } ?>
							<td><?php echo $cd_row['particular'] ?></td>
							<td><?php echo $cd_row['cDate'] ?></td>
							<td><?php echo number_format((float)$cd_row['kg'], 3, '.', '') ?></td>
							<td><?php echo $cd_row['totalAmt'] ?></td>
						</tr>
					  <?php
						}
						?>
					</table>
				</td>
			</tr>
			</table>
			<br>
			<table class="border1" align="center" border="">
			<tr>
				<?php if($finalkg < 0) { ?>
			      <td class="border1" colspan="3" align="right" style="color:red;"><h3>Final Kg : &nbsp;&nbsp; 	<?php echo number_format((float)$finalKg, 3, '.', '') ?></h3></td>
				<?php } else { ?>
				  <td class="border1" colspan="3" align="right" style="color:green;"><h3>Final Kg : &nbsp;&nbsp; <?php echo number_format((float)$finalKg, 3, '.', '') ?></h3></td>
				<?php } ?>
			</tr>
			<tr>
				<?php if($finalkg < 0) { ?>
		      <td class="border1" colspan="3" align="right" style="color:red;"><h3>Final Amount : &nbsp;&nbsp; <?php echo round($finalAmount) ?></h3></td>
				<?php } else { ?>
				  <td class="border1" colspan="3" align="right" style="color:green;"><h3>Final Amount : &nbsp;&nbsp; <?php echo round($finalAmount) ?></h3></td>
				<?php } ?>
			  </tr>
			</table>
</body>
</html>
<?php
$html_content = ob_get_clean();
$mpdf->WriteHTML($html_content);
$mpdf->Output("Client List.pdf", 'I');
exit();
?>