<?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
{
    $to       = $_REQUEST['to'];
	$from     = $_REQUEST['from'];
 
	$a = 0;
	$rojmelArray = array();
	$selectRojmel = "SELECT *,partyName FROM sauda
					 LEFT  JOIN party ON party.partyId = sauda.partyId
					  WHERE rojTransType = 'J'
					  AND saudaDate between '".$to."' AND '".$from."'";
	$selectRojmelRes = mysql_query($selectRojmel);
	while($rojmelRow = mysql_fetch_array($selectRojmelRes))
	{
	  $rojmelArray[$a]['saudaId']        = $rojmelRow['saudaId'];
	  $rojmelArray[$a]['saudaDate']      = date('d-m-Y',strtotime($rojmelRow['saudaDate']));
	  $rojmelArray[$a]['rojTransType']   = $rojmelRow['rojTransType'];
	  $rojmelArray[$a]['partyName']      = $rojmelRow['partyName'];
	  $rojmelArray[$a]['particular']     = $rojmelRow['particular'];
	  $rojmelArray[$a]['fine']           = $rojmelRow['fine'];
	  $rojmelArray[$a]['amount']         = $rojmelRow['amount'];
	  $rojmelArray[$a]['acc_type']       = $rojmelRow['acc_type'];
	  $rojmelArray[$a]['rate']   	     = $rojmelRow['rate'];
	  $rojmelArray[$a]['cash_type']      = $rojmelRow['cash_type'];
	  $rojmelArray[$a]['mcxcheckStatus'] = $rojmelRow['mcxcheckStatus'];
	  $a++;
	}
	
	$k = 0;
	$javakArray = array();
	$selectJavak = "SELECT *,partyName FROM sauda
					LEFT JOIN party ON party.partyId = sauda.partyId
					  WHERE rojTransType = 'A'
					   AND saudaDate between '".$to."' AND '".$from."'";
	$selectJavakRes = mysql_query($selectJavak);
	while($javakRow = mysql_fetch_array($selectJavakRes))
	{
	  $javakArray[$k]['saudaId']         = $javakRow['saudaId'];
	  $javakArray[$k]['saudaDate']       = date('d-m-Y',strtotime($javakRow['saudaDate']));
	  $javakArray[$k]['rojTransType']    = $javakRow['rojTransType'];
	  $javakArray[$k]['partyName']       = $javakRow['partyName'];
	  $javakArray[$k]['particular']      = $javakRow['particular'];
	  $javakArray[$k]['fine']            = $javakRow['fine'];
	  $javakArray[$k]['amount']          = $javakRow['amount'];
	  $javakArray[$k]['acc_type']        = $javakRow['acc_type'];
	  $javakArray[$k]['cash_type']       = $javakRow['cash_type'];
	  $javakArray[$k]['rate']            = $javakRow['rate'];
	  $javakArray[$k]['mcxcheckStatus']  = $javakRow['mcxcheckStatus'];
	  $k++;
	}	
	
	///////////////////////////////////////Rtgs : Start ////////////////////////
	$selectRojmel = "SELECT SUM(fine) AS afine,SUM(amount) AS aamount FROM sauda
						WHERE rojTransType = 'A'
						AND cash_type = 'R'
						AND  saudaDate between '".$to."' AND '".$from."'";
	$selectRojmelRes = mysql_query($selectRojmel);
	if($row = mysql_fetch_array($selectRojmelRes))
	{
	  $aFine  =  $row['afine'];
	  $aAmount = $row['aamount'];
	}
	
	$selectRojmel = "SELECT SUM(fine) AS jfine,SUM(amount) AS jamount FROM sauda
						WHERE rojTransType = 'J'
						AND cash_type = 'R'
						AND  saudaDate between '".$to."' AND '".$from."'";
	$selectRojmelRes = mysql_query($selectRojmel);
	if($row = mysql_fetch_array($selectRojmelRes))
	{
	  $jFine   = $row['jfine'];
	  $jAmount = $row['jamount'];
	}
	
	$rtgsfine = $aFine - $jFine;
	$rtgsAmt = $aAmount - $jAmount;
	
	///////////////////////////////////////Rtgs : End ////////////////////////
	
	
	
	//////////////////////////////////////Cash : Start ////////////////////////
	$selectRojmelCash = "SELECT SUM(fine) AS cAavakFine,SUM(amount) AS cAavkamount FROM sauda
						WHERE rojTransType = 'A'
						AND cash_type = 'C'
					AND  saudaDate between '".$to."' AND '".$from."'";
	$selectRojmelCashRes = mysql_query($selectRojmelCash);
	if($rowCash = mysql_fetch_array($selectRojmelCashRes))
	{
	  $cAavakFine  =  $rowCash['cAavakFine'];
	  $cAavkamount = $rowCash['cAavkamount'];
	}
	
	$selectRojmelJavak = "SELECT SUM(fine) AS cJavakFine,SUM(amount) AS cJavkamount FROM sauda
						WHERE rojTransType = 'J'
						AND cash_type = 'C'
					AND  saudaDate between '".$to."' AND '".$from."'";
	$selectRojmelJavakRes = mysql_query($selectRojmelJavak);
	if($row = mysql_fetch_array($selectRojmelJavakRes))
	{
	  $cJavakFine   = $row['cJavakFine'];
	  $cJavkamount  = $row['cJavkamount'];
	}
	
	//////////////////////////////////////Cash : End ////////////////////////
	
	$Cashfine = $cAavakFine - $cJavakFine;
	$CashAmt  = $cAavkamount - $cJavkamount;
	
	$netFine   = $Cashfine + $rtgsfine;
	$netAmount = $rtgsAmt +  $CashAmt;
	
}
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 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 valign="top">Sauda Type</td>
							<td valign="top">Party Name</td>
							<td valign="top">Date</td>
							<td valign="top">Particular</td>
							<td valign="top">Cash Type</td>
							<td valign="top">Rate</td>
							<td valign="top">Fine</td>
							<td valign="top">Amount</td>
			
						</tr>
						<?php
						foreach($rojmelArray as $sec => $cd_row) {
						?>
						<tr>							
							<?php if($cd_row['acc_type'] == 'D') { ?>
								<td>Direct</td>
					 		<?php } else { ?>
								<td>Khata</td>
							<?php } ?>
							<td><?php echo $cd_row['partyName'] ?></td>
							<td><?php echo $cd_row['saudaDate'] ?></td>
							<td><?php echo $cd_row['particular'] ?></td>
							<?php if($cd_row['cash_type'] == 'C') { ?>
								<td>Cash</td>
					 		<?php } else { ?>
								<td>Rtgs</td>
							<?php } ?>
							<td><?php echo number_format((float)$cd_row['rate'], 0, '.', '') ?></td>
							<td><?php echo number_format((float)$cd_row['fine'], 3, '.', '') ?></td>
							<td><?php echo number_format((float)$cd_row['amount'], 0, '.', '') ?></td>
						</tr>
					  <?php
						}
						?>
					</table>
				</td>
				<td>
					<table cellspacing="14">
						<tr><td colspan="5" align="center">Sell</td></tr>
						<tr>
							<td valign="top">Sauda Type</td>
							<td valign="top">Party Name</td>
							<td valign="top">Date</td>
							<td valign="top">Particular</td>
							<td valign="top">Cash Type</td>
							<td valign="top">Rate</td>
							<td valign="top">Fine</td>
							<td valign="top">Amount</td>
						</tr>
						<?php
						foreach($javakArray as $secnew => $cd_row) {
						?>
						<tr>							
							<?php if($cd_row['acc_type'] == 'D') { ?>
								<td>Direct</td>
					 		<?php } else { ?>
								<td>Khata</td>
							<?php } ?>
							<td><?php echo $cd_row['partyName'] ?></td>
							<td><?php echo $cd_row['saudaDate'] ?></td>
							<td><?php echo $cd_row['particular'] ?></td>
							<?php if($cd_row['cash_type'] == 'C') { ?>
								<td>Cash</td>
					 		<?php } else { ?>
								<td>Rtgs</td>
							<?php } ?>
							<td><?php echo number_format((float)$cd_row['rate'], 0, '.', '') ?></td>
							<td><?php echo number_format((float)$cd_row['fine'], 3, '.', '') ?></td>
							<td><?php echo number_format((float)$cd_row['amount'], 0, '.', '') ?></td>
						</tr>
					  <?php
						}
						?>
					</table>
				</td>
			</tr>
			</table>
			<br>
			<table align="center" border="">
				<tr>
					<td><h3>Rtgs Fine : &nbsp;&nbsp; 	<?php echo number_format((float)$rtgsfine, 3, '.', '') ?></h3></td>
					<td><h3>Rtgs Amount : &nbsp;&nbsp; 	<?php echo number_format((float)$rtgsAmt, 0, '.', '') ?></h3></td>
					<td><h3>Cash Fine : &nbsp;&nbsp; <?php echo number_format((float)$Cashfine, 3, '.', '') ?></h3></td>
					<td><h3>Cash Amount : &nbsp;&nbsp; <?php echo number_format((float)$CashAmt, 0, '.', '') ?></h3></td>
				</tr>
				<tr>
					<td><h3>Fine : &nbsp;&nbsp; 	<?php echo number_format((float)$netFine, 3, '.', '') ?></h3></td>
					<td><h3>Amount : &nbsp;&nbsp; 	<?php echo number_format((float)$netAmount, 0, '.', '') ?></h3></td>
				</tr>
			</table>
</body>
</html>
<?php
$html_content = ob_get_clean();
$mpdf->WriteHTML($html_content);
$mpdf->Output("Sales Party Ledger.pdf", 'I');
exit();
?>