<?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']) && !isset($_SESSION['s_userType'])) {
    $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
    header("Location:index.php");
} else {
    $partyId = isset($_REQUEST['partyId']) && $_REQUEST['partyId'] != '' ? $_REQUEST['partyId'] : 0;
	$clientDetail = array();
	$toDate       = $_REQUEST['toDate'];
	$fromDate     = $_REQUEST['fromDate'];
	
	if($partyId > 0)
	{
		$selectParty = "SELECT partyName FROM party 
						WHERE partyId = ".$partyId;
		$selectPartyRes = mysql_query($selectParty);
		if($parRow = mysql_fetch_array($selectPartyRes))
		{
			$partyName = $parRow['partyName'];
		}
		
		$k = 0;
		$selectClient = "SELECT *,partyName FROM client
						LEFT JOIN party ON party.partyId = client.climcxid
						 WHERE client.partyId = ".$partyId."
						 AND  cDate BETWEEN '".$toDate."'
						 AND '".$fromDate."'
						 AND  cstatus = 'A'
						 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]['diff']     	= $clientRow['diff'];
		  $clientDetail[$k]['diffAmt']    	= $clientRow['diffAmt'];
		  $clientDetail[$k]['total']      	= $clientRow['total'];
		  $clientDetail[$k]['kg']         	= $clientRow['kg'];
		  $clientDetail[$k]['partyName']   	= $clientRow['partyName'];
		  $clientDetail[$k]['checkStatus']  = $clientRow['checkStatus'];
		  $clientDetail[$k]['totalAmt']   	= round($clientRow['totalAmt'],3);
		  $k++;
		}
	}
	
	$selectAmount = "SELECT SUM(kg) AS jKg,SUM(totalAmt) AS jtotalAmt
						   FROM client
						  WHERE cliTransType = 'J'
					AND partyId = ".$partyId;
		$selectAmountRes = mysql_query($selectAmount);
		if($jrow = mysql_fetch_assoc($selectAmountRes))
		{
		  $jKg          = $jrow['jKg'];
		  $jtotalAmt    = $jrow['jtotalAmt'];
		  
		}

		$selectAavkAmount = "SELECT SUM(kg) AS aKg,SUM(totalAmt) AS atotalAmt
						   FROM client
						  WHERE cliTransType = 'A'
					AND partyId = ".$partyId;
		$selectAavkAmountRes = mysql_query($selectAavkAmount);
		if($arow = mysql_fetch_assoc($selectAavkAmountRes))
		{
		  $aKg          = $arow['aKg'];
		  $atotalAmt    = $arow['atotalAmt'];  
		}
		
		$selectJavakAmount = "SELECT SUM(kg) AS jKg,SUM(totalAmt) AS jtotalAmt
						   FROM client
						  WHERE cliTransType = 'J'
					AND partyId = ".$partyId;
		$selectJavakAmountRes = mysql_query($selectJavakAmount);
		if($jrow = mysql_fetch_assoc($selectJavakAmountRes))
		{
		  $jKg          = $jrow['jKg'];
		  $jtotalAmt    = $jrow['jtotalAmt'];  
		}
		
		$selectOpening = "SELECT SUM(openingAmt) AS openingAmt,SUM(openingFine) AS openingFine
						   FROM client
					WHERE partyId = ".$partyId;
		$selectOpeningRes = mysql_query($selectOpening);
		if($oprow = mysql_fetch_assoc($selectOpeningRes))
		{
		  $opFine          = $oprow['openingFine'];
		  $openingAmt      = $oprow['openingAmt'];  
		}
		 
		 
		$finalKg     = $aKg - $jKg + $opFine; 

		$finalAmount = $atotalAmt - $jtotalAmt + $openingAmt;
 

 
}
ob_start();
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Items Print</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta http-equiv="Content-Language" content="hi">
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <!-- 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">
        <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:15px;">
            
            <thead>
                <tr> 
					<th style="width: 20%; text-align: left;">Party : <?php echo $partyName; ?></th>
                    <th style="width: 20%; text-align: left;">From Date : <?php echo  date('d-m-Y',strtotime($toDate)); ?></th>
                    <th style="width: 20%; text-align: left;">To Date : <?php echo  date('d-m-Y',strtotime($fromDate)); ?></th>
                </tr>
            </thead>
        </table>
        <table cellspacing="0" style="width: 100%; font-size:15px;" border="1" align="center">
            <thead>
                <tr>
                    <th style="text-align: left;">Type</th>
                    <th style="text-align: left;">Particular</th>											
                    <th style="text-align: left;">Date</th>											
                    <th style="text-align: left;">Rate</th>	
                    <th style="text-align: left;">Rate Diff</th>	
                    <th style="text-align: left;">Diff Amt</th>	
                    <th style="text-align: left;">Total</th>	
                    <th style="text-align: left;">K.G.</th>	
                    <th style="text-align: left;">Total Amount</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach($clientDetail as $item_row){ ?>
                    <tr>
						<?php if($item_row['cliTransType'] == 'J') { ?>
							<td>Javak</td>
						<?php } else { ?>
							<td>Aavak</td>
						<?php } ?>
                        <td><?php echo $item_row['particular']; ?></td>
                        <td><?php echo $item_row['cDate']; ?></td>
                        <td><?php echo $item_row['rate']; ?></td>
                        <td><?php echo $item_row['diff']; ?></td>
                        <td><?php echo $item_row['diffAmt']; ?></td>
                        <td><?php echo $item_row['total']; ?></td>
                        <td><?php echo  number_format ((float) $item_row['kg'],3,'.',''); ?></td>
                        <td><?php echo  number_format ((float) $item_row['totalAmt'],0,'.',''); ?></td>
                    </tr>
                <?php } ?>
            </tbody>
				
        </table>
		<br>
		<table cellspacing="0" style="width: 30%; font-size:20px;" border="1" align="center">
					<thead>
						<tr>
							<th style="text-align: left;">Final Kg : <?php echo  number_format ((float) $finalKgl,3,'.',''); ?></th>
						</tr>
						<tr>
							<th style="text-align: left;">Final Amount : <?php echo  number_format ((float) $finalAmount,0,'.',''); ?></th>
						</tr>
					</thead>
				</table>
    </body>
</html>
<?php
    $html_content = ob_get_clean();
    $mpdf->WriteHTML($html_content);
    $mpdf->Output("Client Ledger.pdf", 'I');
    exit();
?>
