<? include "top.php"; ?>
<!DOCTYPE html>
<html>
<head>
	<?php include('include/head.php'); 

	$action=$_REQUEST['action'];
	if($action=='show'){


	
	$stdate=$_POST['stdate'];
	$enddate=$_POST['enddate'];
	$farmid=$_POST['rdfid'];

	if($farmid=='0'){

		header("Locaation:repcashsales.php");
	}else{
		$getf=f(q("SELECT * FROM tbl_customer WHERE custid='$farmid'"));
		$rdfname=$getf['custname'];
		$sql="SELECT * FROM tbl_readymade WHERE rdcustid='$farmid' AND rddate BETWEEN '$stdate' AND '$enddate' order by rdid ASC";
		$title="Readybird Sales Report of ".$rdfname." from ".$stdate." and ".$enddate;
	}


}else{

	header("Locaation:repcashsales.php");
}



	?>
	<title><?=$title?></title>
	<link rel="stylesheet" type="text/css" href="https://sarmac.b-cdn.net/plugins/datatables/media/css/jquery.dataTables.css">
	<link rel="stylesheet" type="text/css" href="https://sarmac.b-cdn.net/plugins/datatables/media/css/dataTables.bootstrap4.css">
	<link rel="stylesheet" type="text/css" href="https://sarmac.b-cdn.net/plugins/datatables/media/css/responsive.dataTables.css">
	<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
	<style type="text/css">
		th{

    	font-size: 12px !important;
    }
    td{

    	font-size: 10px !important;
    }
	</style>
</head>
<body>

<table class="stripe hover multiple-select-row data-table-export nowrap">
							<thead>
								<tr>

									<th class="table-plus datatable-nosort">Entry By</th>
									<th class="datatable-nosort">Date</th>
									<th class="datatable-nosort">Liftman</th>
									<th class="datatable-nosort">Vehicle No</th>
									<th class="datatable-nosort">Farmer</th>
									<th class="datatable-nosort">Lot No</th>
									<th class="datatable-nosort">DC No</th>
									
									<th class="datatable-nosort">Particulars</th>
									<th class="datatable-nosort">Quality</th>
									<th class="datatable-nosort">Piece</th>	
									<th class="datatable-nosort">Weight</th>
									<th class="datatable-nosort">Sell Rt</th>
									<th class="datatable-nosort">Hightech Rt</th>
									<th class="datatable-nosort">Paper rt</th>
									<th class="datatable-nosort">Carry cost/kg</th>
									<th class="datatable-nosort">AVR-BDW</th>
									<th class="datatable-nosort">Bill Amount</th>
									<th class="datatable-nosort">Money rcvno</th>
									<th class="datatable-nosort">Diposit Amt</th>
									<th class="datatable-nosort">Balance</th>
									<th class="datatable-nosort">Commission</th>
									<th class="datatable-nosort">Net Commission</th>
								</tr>
							</thead>
							<tbody>
								<?
									$balance=floatval($getf['custclose']);

									$gettc=f(q("SELECT SUM(rdbillamt) as sales FROM tbl_readymade where rdac='SALES AC' AND rddate BETWEEN '2010-01-01' AND '$stdate'"));
									$gettd=f(q("SELECT SUM(rdbillamt) as deposit FROM tbl_readymade where rdac='DEPOSIT AC' AND rddate BETWEEN '2010-01-01' AND '$stdate'"));

									$diffr=floatval($gettc['sales'])-floatval($gettd['deposit']);
									$balance=floatval($balance)+floatval($diffr);

								?>
								<tr>
									<td class="table-plus"><?=$getuname['rdentry']?></td>
									
									
									<td ></td>
									<td>Opening balance</td>
									
									<td ></td>
									<td ></td>	
									<td ></td>
									<td ></td>
									<td ></td>
									<td ></td>
									<td ></td>
									<td ></td>
									<td ></td>
									<td ></td>
									<td ></td>
									<td ></td>
									<td ></td>
									<td ></td>

									
									<td ></td>
									<td ></td>
									<td ><?=$balance?></td>
									<td >0</td>
									<td>0</td>
								</tr>
								<?
									



									$getc=mysqli_query($con,$sql);
									$i=0;
									while($arr=mysqli_fetch_array($getc)){
										$getuname=f(q("SELECT * FROM tbl_login WHERE l_uid='$arr[csadded]'"));
										$getf=f(q("SELECT * FROM tbl_farmer WHERE faid='$arr[csfarmer]'"));
										$getl=f(q("SELECT * FROM tbl_liftman WHERE lfid='$arr[csliftman]'"));
										$getu=f(q("SELECT * FROM tbl_unit WHERE unid='$arr[csunit]'"));

										$gets=f(q("SELECT * FROM tbl_csale_det WHERE csdsid='$arr[csid]' AND csdtype='chicks'"));
										$proids=$gets['csdprodid'];
										$getprod=f(q("SELECT * FROM tbl_manu WHERE maid=(SELECT mamanu FROM tbl_product WHERE maid='$proids')"));

										$getps=f(q("SELECT * FROM tbl_csale_det WHERE csdsid='$arr[csid]' AND csdprodname LIKE'%PS%'"));

										$getbs=f(q("SELECT * FROM tbl_csale_det WHERE csdsid='$arr[csid]' AND csdprodname LIKE'%BS%'"));

										$getbf=f(q("SELECT * FROM tbl_csale_det WHERE csdsid='$arr[csid]' AND csdprodname LIKE'%BF%'"));

										$getbm=f(q("SELECT SUM(csdtotal) as medtot FROM tbl_csale_det WHERE csdsid='$arr[csid]' AND csdtype='meds'"));

										if($arr['rdac']=='SALES AC'){ 
											$totalcr=floatval($totalcr)+floatval($arr['rdbillamt']);
											$balance=floatval($balance)-floatval($arr['rdbillamt']);
										}
										if($arr['rdac']=='DEPOSIT AC'){ 

											$totaldr=floatval($totaldr)+floatval($arr['rdbillamt']);
											$balance=floatval($balance)+floatval($arr['rdbillamt']);
										}

										//$balance=(floatval($totalcr)-floatval($totaldr));

										$netcomm=floatval($netcomm)+floatval($arr['rdcomm']);

								?>
								<tr>
									<td class="table-plus"><?=$getuname['rdentry']?></td>
									
									
									<td ><?=$arr['rddate']?></td>
									<!-- <td><?=$arr['csac']?></td> -->
									<td ><?=$arr['rdlftname']?></td>
									<td ><?=$arr['rdvech']?></td>
									<td ><?=$arr['rdfname']?></td>	
									<td ><?=$arr['rdlot']?></td>
									<td ><?=$arr['rddc']?></td>
									<td ><?=$arr['rdac']?></td>
									<td ><?=$arr['rdquality']?></td>
									<td ><?=$arr['rdquan']?></td>
									<td ><?=$arr['rdwt']?></td>
									<td ><?=$arr['rdsrate']?></td>
									<td ><?=$arr['rdhight']?></td>
									<td ><?=$arr['rdpaper']?></td>
									<td ><?=$arr['rdcarry']?></td>
									<td ><?=$arr['rdavr']?></td>
									<td ><? if($arr['rdac']=='SALES AC'){ ?><?=$arr['rdbillamt']?><?}else{?>0<?}?></td>

									
									<td ><? if($arr['rdac']=='DEPOSIT AC'){ ?><?=$arr['rdbill']?><?}else{?>0<?}?></td>
									<td ><? if($arr['rdac']=='DEPOSIT AC'){ ?><?=$arr['rdbillamt']?><?}else{?>0<?}?></td>
									<td ><?=$balance?></td>
									<td ><?=$arr['rdcomm']?></td>
									<td><?=$netcomm?></td>
								</tr>
								<? } ?>	
							</tbody>
						</table>


	<?php include('include/script.php'); ?>

	<script src="src/plugins/datatables/media/js/jquery.dataTables.min.js"></script>
	<script src="src/plugins/datatables/media/js/dataTables.bootstrap4.js"></script>
	<script src="src/plugins/datatables/media/js/dataTables.responsive.js"></script>
	<script src="src/plugins/datatables/media/js/responsive.bootstrap4.js"></script>
	<!-- buttons for Export datatable -->
	<script src="src/plugins/datatables/media/js/button/dataTables.buttons.js"></script>
	<script src="src/plugins/datatables/media/js/button/buttons.bootstrap4.js"></script>
	<script src="src/plugins/datatables/media/js/button/buttons.print.js"></script>
	<script src="src/plugins/datatables/media/js/button/buttons.html5.js"></script>
	<script src="src/plugins/datatables/media/js/button/buttons.flash.js"></script>
	<script src="src/plugins/datatables/media/js/button/pdfmake.min.js"></script>
	<script src="src/plugins/datatables/media/js/button/vfs_fonts.js"></script>
	<script>
		$('document').ready(function(){
			$('.data-table').DataTable({
				scrollCollapse: true,
				autoWidth: false,
				responsive: true,
				columnDefs: [{
					targets: "datatable-nosort",
					orderable: false,
				}],
				"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
				"language": {
					"info": "_START_-_END_ of _TOTAL_ entries",
					searchPlaceholder: "Search"
				},
			});
			$('.data-table-export').DataTable({
				scrollCollapse: true,
				autoWidth: false,
				responsive: true,
				columnDefs: [{
					targets: "datatable-nosort",
					orderable: false,
				}],
				"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
				"language": {
					"info": "_START_-_END_ of _TOTAL_ entries",
					searchPlaceholder: "Search"
				},
				dom: 'Bfrtip',
				buttons: [
				'csv', 'pdf', 'print'
				]
			});
			var table = $('.select-row').DataTable();
			$('.select-row tbody').on('click', 'tr', function () {
				if ($(this).hasClass('selected')) {
					$(this).removeClass('selected');
				}
				else {
					table.$('tr.selected').removeClass('selected');
					$(this).addClass('selected');
				}
			});
			var multipletable = $('.multiple-select-row').DataTable();
			$('.multiple-select-row tbody').on('click', 'tr', function () {
				$(this).toggleClass('selected');
			});
		});
	</script>
	<script type="text/javascript">
		
	</script>
</body>
</html>