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

		$clientDetail = array();
		$toDate       = $_REQUEST['todate'];
		$fromDate     = $_REQUEST['fromDate'];
		//////////////////////////////////////Buy : Start //////////////////////////////////
		$selectClient = "SELECT * FROM client
						 WHERE partyId = ".$partyId."
						 AND  cDate BETWEEN '".$toDate."'
						 AND '".$fromDate."'
						 AND  cstatus = 'A'
						   AND saudaId > 0
						 ORDER BY clientId ASC";
		$selectClientRes = mysql_query($selectClient);
		while($clientRow = mysql_fetch_array($selectClientRes))
		{
		  $clientDetail[$k]['clientId']     = $clientRow['clientId'];
		  $clientDetail[$k]['particular']   = $clientRow['particular'];
		  $clientDetail[$k]['cliTransType'] = $clientRow['cliTransType'];
		  $clientDetail[$k]['cDate']        = date('d-m-Y',strtotime($clientRow['cDate']));
		  $clientDetail[$k]['rate']       	= $clientRow['rate'];
		  $clientDetail[$k]['kg']         	= $clientRow['kg'];
		  $clientDetail[$k]['rojmelId']     = $clientRow['rojmelId'];
		  $clientDetail[$k]['sell_type']    = $clientRow['sell_type'];
		  $clientDetail[$k]['acc_type']     = $clientRow['acc_type'];
		  $clientDetail[$k]['totalAmt']   	= round($clientRow['totalAmt'],0);
		  $k++;
		} 
		//////////////////////////////////////Buy : End //////////////////////////////////
	
	
		//////////////////////////////////////Sell : Start //////////////////////////////////
			
			$kk = 0;
			$clientDet = array();
			$selectClient = "SELECT *,acc_type,rojTransType,client.particular FROM client
							LEFT JOIN rojmel ON rojmel.rojmelId = client.rojmelId
							 WHERE client.partyId = ".$partyId."
							 AND  cDate BETWEEN '".$toDate."'
							 AND '".$fromDate."'
							 AND  cstatus = 'A'
							 AND client.rojmelId > 0
							 ORDER BY clientId ASC";
			$selectClientRes = mysql_query($selectClient);
			while($clientRow = mysql_fetch_array($selectClientRes))
			{
			  $clientDet[$kk]['clientId']     = $clientRow['clientId'];
			  $clientDet[$kk]['particular']   = $clientRow['particular'];
			  $clientDet[$kk]['cliTransType'] = $clientRow['cliTransType'];
			  $clientDet[$kk]['is_opening']   = $clientRow['is_opening'];
			  $clientDet[$kk]['cDate']        = date('d-m-Y',strtotime($clientRow['cDate']));
			  $clientDet[$kk]['rate']         = $clientRow['rate'];
			  $clientDet[$kk]['kg']           = $clientRow['kg'];
			  $clientDet[$kk]['rojmelId']     = $clientRow['rojmelId'];
			  $clientDet[$kk]['sell_type']    = $clientRow['sell_type'];
			  $clientDet[$kk]['acc_type']     = $clientRow['acc_type'];
			  $clientDet[$kk]['rojTransType'] = $clientRow['rojTransType'];
			  $clientDet[$kk]['totalAmt']     = round($clientRow['totalAmt'],0);
			  $kk++;
			} 
			
			$selectClient = "SELECT * FROM client
						 WHERE partyId = ".$partyId."
						 AND  cstatus = 'A'
						  AND is_opening = 1
						  AND rojmelId = 0
						 ORDER BY clientId ASC";
			$selectClientRes = mysql_query($selectClient);
			while($clientRow = mysql_fetch_array($selectClientRes))
			{
			  $clientDet[$kk]['clientId']     = '';
			  $clientDet[$kk]['particular']   = $clientRow['particular'];
			  $clientDet[$kk]['cliTransType'] = $clientRow['cliTransType'];
			  $clientDet[$kk]['is_opening']   = $clientRow['is_opening'];
			  $clientDet[$kk]['cDate']        = '';
			  $clientDet[$kk]['rate']         = '';
			  $clientDet[$kk]['kg']           = $clientRow['kg'];
			  $clientDet[$kk]['rojmelId']     = '';
			  $clientDet[$kk]['sell_type']    = '';
			  $clientDet[$kk]['acc_type']     = '';
			  $clientDet[$kk]['totalAmt']     = round($clientRow['totalAmt'],0);
			  $kk++;
			} 
		
		//////////////////////////////////////Sell : End //////////////////////////////////
	}
	
		$z=0;
		$clientArray = array();
		$selectClient = "SELECT partyId,partyName
						   FROM party
						WHERE partyType = 'C'
					   ORDER BY partyName";
		$selectClientRes = mysql_query($selectClient);
		while($clientRow = mysql_fetch_array($selectClientRes))
		{
		  $clientArray['partyId'][$z]   = $clientRow['partyId'];
		  $clientArray['partyName'][$z] = $clientRow['partyName'];
		  $z++;
		}	
		$selectAavak = "SELECT SUM(totalAmt) AS avakAmt,SUM(kg) AS avakkg
		                 FROM client
						WHERE partyId = ".$partyId."
						AND cliTransType = 'A'";
		$selectAavakRes = mysql_query($selectAavak);
		while($aavakRow = mysql_fetch_array($selectAavakRes))
		{
		  $avakAmt         =  $aavakRow['avakAmt'];
		  $avakkg          = $aavakRow['avakkg'];
		}
		
		$selectJavak = "SELECT SUM(totalAmt) AS javakAmt,SUM(kg) AS javakkg
		                 FROM client
						WHERE partyId = ".$partyId."
						AND cliTransType = 'J'";
		$selectJavakRes = mysql_query($selectJavak);
		while($javakRow = mysql_fetch_array($selectJavakRes))
		{
		  $javakAmt   = $javakRow['javakAmt'];
		  $javakkg    = $javakRow['javakkg'];
		}
 
		$finalKg 	 = $avakkg - $javakkg;
		$finalAmount = $avakAmt - $javakAmt;
}
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Client 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>Client Name : <?php echo $partyName; ?></th>
					<th>From <?php echo date('d-m-Y',strtotime($toDate)) ?></th>    
					<th>To <?php echo date('d-m-Y',strtotime($fromDate)) ?></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();
?>