<? include "top.php"; ?>
<!DOCTYPE html>
<html>
<head>
	<?php include('include/head.php'); ?>
	<title>Ready Bird Sales</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>
</head>
<body>
	<?php include('include/header.php'); ?>
	<?php include('include/sidebar.php'); 

	$action=$_REQUEST['action'];
	if($action=='save'){
		$rddate=$_POST['rddate'];
		$rdbill=$_POST['rdbill'];
		$rdvech=$_POST['rdvech'];
		$rdlot=$_POST['rdlot'];
		$rddc=$_POST['rddc'];
		$rdcustid=$_POST['rdcustid'];
		$getc=f(q("SELECT * FROM tbl_customer WHERE custid='$rdcustid'"));
		$rdcname=$getc['custname'];
		$rdac="SALES AC";
		$csadded=$_SESSION['murgibhai'];
		
		$rdfid=$_POST['rdfid'];
		$getf=f(q("SELECT * FROM tbl_farmer WHERE faid='$rdfid'"));
		$rdfname=$getf['faname'];
		$rdlftid=$_POST['rdlftid'];
		$getl=f(q("SELECT * FROM tbl_liftman WHERE lfid='$rdlftid'"));
		$rdlftname=$getl['lfname'];
		$rdentry=$_POST['rdentry'];
		$rdprod=$_POST['rdprod'];
		$getp=f(q("SELECT * FROM tbl_product WHERE maid='$rdprod'"));
		$rdpname=$getp['maname'];
		$rdquan=$_POST['rdquan'];
		$rdquality=$_POST['rdquality'];



		$rdsrate=$_POST['rdsrate'];
		$rdhight=$_POST['rdhight'];
		$rdpaper=$_POST['rdpaper'];
		$rdwt=$_POST['rdwt'];
		$rdcarry=$_POST['rdcarry'];
		$rdavr=$_POST['rdavr'];
		$rdbillamt=$_POST['rdbillamt'];
		$rduser=$csadded;
		//$rduser=$_POST['rduser'];

		$getqual=f(q("SELECT * FROM tbl_ptype WHERE ptname='$rdquality'"));
		if($getqual['ptapp']=='yes'){

			$comms=$getqual['ptcomm'];

			$rdcomm=floatval($rdwt)*floatval($comms);

		}else{

			$rdcomm=0;
		}


		

			
				$res=mysqli_query($con,"INSERT INTO `tbl_readymade` SET `rddate`='$rddate',`rdbill`='$rdbill',`rdvech`='$rdvech',`rdlot`='$rdlot',`rddc`='$rddc',`rdcustid`='$rdcustid',`rdcname`='$rdcname',`rdfid`='$rdfid',`rdfname`='$rdfname',`rdlftid`='$rdlftid',`rdlftname`='$rdlftname',`rdentry`='$rdentry',`rdprod`='$rdprod',`rdpname`='$rdpname',`rdquan`='$rdquan',`rdquality`='$rdquality',`rdsrate`='$rdsrate',`rdhight`='$rdhight',`rdpaper`='$rdpaper',`rdwt`='$rdwt',`rdcarry`='$rdcarry',`rdavr`='$rdavr',`rdbillamt`='$rdbillamt',`rduser`='$rduser',rdac='$rdac',rdcomm='$rdcomm'");

				//echo "INSERT INTO tbl_cash_sales SET csdate='$csdate',csfarmer='$csfarmer',csliftman='$csliftman',csunit='$csunit',csbill='$csbill',cstotal='$cstotal',csac='$csac',csadded='$csadded'";

				if($res){?>

					 <script type="text/javascript">
            Swal.fire('Data Saved Successfully','','success')
             </script>
				<?}else{?>

					<script type="text/javascript">
            Swal.fire('Error Saving Data','','error')
             </script>

				<?}

	}elseif($action=='update'){
		$rddate=$_POST['rddate'];
		$rdbill=$_POST['rdbill'];
		$rdvech=$_POST['rdvech'];
		$rdlot=$_POST['rdlot'];
		$rddc=$_POST['rddc'];
		$rdcustid=$_POST['rdcustid'];
		$getc=f(q("SELECT * FROM tbl_customer WHERE custid='$rdcustid'"));
		$rdcname=$getc['custname'];
		$rdac="SALES AC";
		$csadded=$_SESSION['murgibhai'];
		
		$rdfid=$_POST['rdfid'];
		$getf=f(q("SELECT * FROM tbl_farmer WHERE faid='$rdfid'"));
		$rdfname=$getf['faname'];
		$rdlftid=$_POST['rdlftid'];
		$getl=f(q("SELECT * FROM tbl_liftman WHERE lfid='$rdlftid'"));
		$rdlftname=$getl['lfname'];
		$rdentry=$_POST['rdentry'];
		$rdprod=$_POST['rdprod'];
		$getp=f(q("SELECT * FROM tbl_product WHERE maid='$rdprod'"));
		$rdpname=$getp['maname'];
		$rdquan=$_POST['rdquan'];
		$rdquality=$_POST['rdquality'];
		$rdsrate=$_POST['rdsrate'];
		$rdhight=$_POST['rdhight'];
		$rdpaper=$_POST['rdpaper'];
		$rdwt=$_POST['rdwt'];
		$rdcarry=$_POST['rdcarry'];
		$rdavr=$_POST['rdavr'];
		$rdbillamt=$_POST['rdbillamt'];
		$rduser=$csadded;
		$cid=$_REQUEST['cid'];

		$getqual=f(q("SELECT * FROM tbl_ptype WHERE ptname='$rdquality'"));
		if($getqual['ptapp']=='yes'){

			$comms=$getqual['ptcomm'];

			$rdcomm=floatval($rdwt)*floatval($comms);

		}else{

			$rdcomm=0;
		}


	$res=mysqli_query($con,"UPDATE `tbl_readymade` SET `rddate`='$rddate',`rdbill`='$rdbill',`rdvech`='$rdvech',`rdlot`='$rdlot',`rddc`='$rddc',`rdcustid`='$rdcustid',`rdcname`='$rdcname',`rdfid`='$rdfid',`rdfname`='$rdfname',`rdlftid`='$rdlftid',`rdlftname`='$rdlftname',`rdentry`='$rdentry',`rdprod`='$rdprod',`rdpname`='$rdpname',`rdquan`='$rdquan',`rdquality`='$rdquality',`rdsrate`='$rdsrate',`rdhight`='$rdhight',`rdpaper`='$rdpaper',`rdwt`='$rdwt',`rdcarry`='$rdcarry',`rdavr`='$rdavr',`rdbillamt`='$rdbillamt',`rduser`='$rduser',rdac='$rdac',rdcomm='$rdcomm' WHERE rdid='$cid'");

		
	if($res){?>

					 <script type="text/javascript">
            Swal.fire('Data Updated Successfully','','success')
             </script>
				<?}else{?>

					<script type="text/javascript">
            Swal.fire('Error Updating Data','','error')
             </script>

				<?}



	}elseif($action=='edit'){
		$cid=$_REQUEST['cid'];
		$edit=f(q("SELECT * FROM tbl_readymade WHERE rdid='$cid'"));



	}elseif($action=='delete'){

		$cid=$_REQUEST['cid'];

		$res=mysqli_query($con,"DELETE FROM tbl_readymade WHERE rdid='$cid'");
		
		if($res){?>

					 <script type="text/javascript">
            Swal.fire('Data Deleted Successfully','','success')
             </script>
				<?}else{?>

					<script type="text/javascript">
            Swal.fire('Error Deleting Data','','error')
             </script>

				<?}

	}

	


	?>
	<style type="text/css">
		.msmall{

			width: 50px;
			font-size: 12px;
		}
		.smallf{

			font-size: 12px;
		}
		tr{
			width: 100%;
		}
		td{
			width: 20%;
		}
		input[type="text"]:focus { 
    
    border: 2px solid red;   
    }
    select:focus{

         border: 2px solid red;  
    }
    textarea:focus{
        border: 2px solid red; 
    }

    input[type="checkbox"]:focus { 
    
    border: 1px solid red;   
    }
    .form-control:focus{
        border: 1px solid red;

    }
    .btn:focus{
        border: 1px solid red;

    }
	</style>
	<script type="text/javascript">
		function addtotal(){

			var a=document.getElementById('chquan').value;
			var b=document.getElementById('chrates').value;
			
			//document.getElementById('chtotal').value=c;
			var d=document.getElementById('weight').value;
			var c=parseFloat(d)*parseFloat(b);
			var e=document.getElementById('carry').value;
			var f=parseFloat(d)*parseFloat(e);
			var g=parseFloat(c)+parseFloat(f);
			var h=parseFloat(d)/parseFloat(a);
			document.getElementById('avrbdw').value=h.toFixed(2);
			document.getElementById('billamt').value=g.toFixed(2);




		}
	</script>
	<? include "ajax.php"; ?>
	<div class="main-container">
		<div class="pd-ltr-20 customscroll customscroll-10-p height-100-p xs-pd-20-10">
			<div class="min-height-200px">
				<div class="page-header">
					<div class="row">
						<div class="col-md-6 col-sm-12">
							<div class="title">
								<h4>Ready Bird Sales</h4>
							</div>
							<nav aria-label="breadcrumb" role="navigation">
								<ol class="breadcrumb">
									<li class="breadcrumb-item"><a href="rbsales-list.php">VIEW LIST</a></li>
									<li class="breadcrumb-item active" aria-current="page"><a href="ready-cust-payment.php">ADD PAYMENT</a></li>
								</ol>
							</nav>
						</div>
						<div class="col-md-6 col-sm-12 text-right">
							<!-- <div class="dropdown">
								<a class="btn btn-secondary dropdown-toggle" href="#" role="button" data-toggle="dropdown">
									January 2018
								</a>
								<div class="dropdown-menu dropdown-menu-right">
									<a class="dropdown-item" href="#">Export List</a>
									<a class="dropdown-item" href="#">Policies</a>
									<a class="dropdown-item" href="#">View Assets</a>
								</div>
							</div> -->
						</div>
					</div>
				</div>
				<form action="<?=$_SERVER['PHP_SELF']?>?action=<? if($action=='edit'){?>update&cid=<?=$cid?><? }else{?>save<?} ?>" method="POST">
				<div class="pd-20 bg-white border-radius-4 box-shadow mb-30">

					<div class="row">
						<div class="col-lg-6">
							
							<div class="form-group row">
								<label class="col-sm-12 col-md-2">Date</label>
								<div class="col-sm-12 col-md-10">
									<input type="date" class="form-control" name="rddate" id="csdate" value="<? if($action=='edit'){?><?=$edit['rddate']?><? }else{ ?><? echo date('Y-m-d');?><? } ?>">
								</div>
							</div>
							<div class="form-group row">
								<label class="col-sm-12 col-md-2">Bill</label>
								<div class="col-sm-12 col-md-10">
									<input type="text" class="form-control" name="rdbill" id="csbill" value="<?=$edit['rdbill']?>">
								</div>
							</div>
							<div class="form-group row">
								<label class="col-sm-12 col-md-2">Vehicle No</label>
								<div class="col-sm-12 col-md-10">
									<input type="text" class="form-control" name="rdvech" id="csvno" value="<?=$edit['rdvech']?>">
								</div>
							</div>
							<div class="form-group row">
								<label class="col-sm-12 col-md-2">Lot No</label>
								<div class="col-sm-12 col-md-10">
									<input type="text" class="form-control" name="rdlot" id="lotno" value="<?=$edit['rdlot']?>">
								</div>
							</div>
							<div class="form-group row">
								<label class="col-sm-12 col-md-2">DC No</label>
								<div class="col-sm-12 col-md-10">
									<input type="text" class="form-control" name="rddc" id="lotno" value="<?=$edit['rddc']?>">
								</div>
							</div>

						</div>
						<div class="col-lg-6">
							
							<div class="form-group row">
								<label class="col-sm-12 col-md-2">Customer</label>
								<div class="col-sm-12 col-md-10">
									<select name="rdcustid" class="form-control">
										<option value="0">Choose</option>
										<?
											$getuni=mysqli_query($con,"SELECT * FROM tbl_customer");
											while($arr=mysqli_fetch_array($getuni)){
										?>
										<option value="<?=$arr['custid']?>" <? if($edit['rdcustid']==$arr['custid']){?> selected <? } ?>><?=$arr['custname']?></option>

									<? } ?>

									</select>
								</div>
							</div>
							<div class="form-group row">
								<label class="col-sm-12 col-md-2">Farmer</label>
								<div class="col-sm-12 col-md-10">
									<select name="rdfid" class="form-control">
										<option value="0">Choose</option>
										<?
											$getuni=mysqli_query($con,"SELECT * FROM tbl_farmer");
											while($arr=mysqli_fetch_array($getuni)){
										?>
										<option value="<?=$arr['faid']?>" <? if($edit['rdfid']==$arr['faid']){?> selected <? } ?>><?=$arr['faname']?></option>

									<? } ?>

									</select>
								</div>
							</div>
							<div class="form-group row">
								<label class="col-sm-12 col-md-2">Liftman</label>
								<div class="col-sm-12 col-md-10">
									<select name="rdlftid" class="form-control">
										<option value="0">Choose</option>
										<?
											$getuni=mysqli_query($con,"SELECT * FROM tbl_liftman");
											while($arr=mysqli_fetch_array($getuni)){
										?>
										<option value="<?=$arr['lfid']?>" <? if($edit['rdlftid']==$arr['lfid']){?> selected <? } ?>><?=$arr['lfname']?></option>

									<? } ?>

									</select>
								</div>
							</div>
							<div class="form-group row">
								<label class="col-sm-12 col-md-2">Entry By</label>
								<div class="col-sm-12 col-md-10">
									<input type="text" class="form-control" name="rdentry" id="entryby" value="<?=$edit['rddc']?>">
								</div>
							</div>

						</div>

					</div>

				</div>
				<!-- Default Basic Forms Start -->
				<div class="pd-20 bg-white border-radius-4 box-shadow mb-30">
					<h4>Add Products</h4><br><br>
					<div class="row">
						<div class="col-lg-6">
							<input type="hidden" name="chrow" id="chrow" value="0">
							<div class="form-group row">
								<label class="col-sm-12 col-md-2">Product</label>
								<div class="col-sm-12 col-md-10">
									<select class="form-control" name="rdprod" id="chchicks" onchange="getprods(this.value)">
										<option value="0">Choose</option>
									<?
										$getq=mysqli_query($con,"SELECT * FROM tbl_product");
										while($arr=mysqli_fetch_array($getq)){
											

											
									?>
									<option value="<?=$arr['maid']?>" <? if($edit['rdprod']==$arr['maid']){?> selected <? } ?>><?=$arr['maname']?></option>

									<? } ?>
								</select>
								<input type="hidden" id="prodname" value="">
								</div>
							</div>
							<div class="form-group row">
								<label class="col-sm-12 col-md-2">Quality</label>
								<div class="col-sm-12 col-md-10">
									<select class="form-control" name="rdquality" id="chtype">0
										<option value="0">Choose</option>
									<?
										$getq=mysqli_query($con,"SELECT * FROM tbl_ptype");
										while($arr=mysqli_fetch_array($getq)){
											

											
									?>
									<option value="<?=$arr['ptname']?>" <? if($edit['rdquality']==$arr['ptname']){?> selected <? } ?>><?=$arr['ptname']?></option>

									<? } ?>
								</select>
								<input type="hidden" id="prodname" value="">
								</div>
							</div>
							<div class="form-group row">
								<label class="col-sm-12 col-md-2">Quantity (piece)</label>
								<div class="col-sm-12 col-md-10">
									<input type="number" class="form-control" name="rdquan" id="chquan" value="<? if($action=='edit'){?><?=$edit['rdquan']?><?}else{?>0<? } ?>">
								</div>
							</div>
							<div class="form-group row ">
								<label class="col-sm-12 col-md-2">Sell Rate</label>
								<div class="col-sm-12 col-md-10">
									<input type="number" class="form-control" name="rdsrate" id="chrates" step=0.01 value="<? if($action=='edit'){?><?=$edit['rdsrate']?><?}else{?>0<? } ?>">
								</div>
							</div>
							<!-- <div class="form-group row ">
								<label class="col-sm-12 col-md-2">Total</label>
								<div class="col-sm-12 col-md-10">
									<input type="text" class="form-control" name="chtotal" id="chtotal" value="0">
								</div>
							</div> -->
							<!-- <div class="form-group row">
								<div class="col-sm-12 col-md-12 text-right">
								<button class="btn btn-warning" type="button" onclick="addtablerow()"> Add >></button>
								</div>
							</div> -->



						</div>
						<div class="col-lg-6">
							<div class="form-group row ">
								<label class="col-sm-12 col-md-3">Hightech rate</label>
								<div class="col-sm-12 col-md-9">
									<input type="number" class="form-control" name="rdhight" id="hightech" step=0.01  value="<? if($action=='edit'){?><?=$edit['rdhight']?><?}else{?>0<? } ?>">
								</div>
							</div>
							<div class="form-group row ">
								<label class="col-sm-12 col-md-3">Paper rate</label>
								<div class="col-sm-12 col-md-9">
									<input type="number" class="form-control" name="rdpaper" id="paper" step=0.01  value="<? if($action=='edit'){?><?=$edit['rdpaper']?><?}else{?>0<? } ?>">
								</div>
							</div>
							<div class="form-group row ">
								<label class="col-sm-12 col-md-3">Weight</label>
								<div class="col-sm-12 col-md-9">
									<input type="number" class="form-control" name="rdwt" id="weight" step=0.01  value="<? if($action=='edit'){?><?=$edit['rdwt']?><?}else{?>0<? } ?>" onchange="addtotal()" onblur="addtotal()">
								</div>
							</div>
							<div class="form-group row ">
								<label class="col-sm-12 col-md-3">Carrying Cost / Kg</label>
								<div class="col-sm-12 col-md-9">
									<input type="number" class="form-control" name="rdcarry" id="carry" step=0.01  value="<? if($action=='edit'){?><?=$edit['rdcarry']?><?}else{?>0<? } ?>" onchange="addtotal()" onblur="addtotal()">
								</div>
							</div>
							<div class="form-group row ">
								<label class="col-sm-12 col-md-3">AVR-BDW</label>
								<div class="col-sm-12 col-md-9">
									<input type="number" class="form-control" name="rdavr" id="avrbdw" step=0.01  value="<? if($action=='edit'){?><?=$edit['rdavr']?><?}else{?>0<? } ?>" >
								</div>
							</div>
							<div class="form-group row ">
								<label class="col-sm-12 col-md-6 text-right">Bill Amount</label>
								<div class="col-sm-12 col-md-6">
									<input type="text" class="form-control" name="rdbillamt" id="billamt"  value="<? if($action=='edit'){?><?=$edit['rdbillamt']?><?}else{?>0<? } ?>">
								</div>
							</div>


						</div>



					</div>
					<small style="color: red;"><?=$msg?></small>
					
					
					<div class="clearfix" style="min-height: 30px;"></div>
					
						
						<!-- <div class="form-group row">
							
							<div class="col-sm-12 col-md-10">
								<label class="weight-600">Commission Applicable</label>
									<div class="custom-control custom-radio mb-5">
										<input type="radio" id="customRadio1" name="maptype" class="custom-control-input" value="no" <? if($edit['maptype']=='no'){?>checked="true"<? } ?>>
										<label class="custom-control-label" for="customRadio1">No</label>
									</div>
									<div class="custom-control custom-radio mb-5">
									<input type="radio" id="customRadio2" name="maptype" class="custom-control-input" value="yes" <? if($edit['maptype']=='no'){?>checked="true"<? } ?>>
										<label class="custom-control-label" for="customRadio2">Yes</label>
									</div>
							</div>
						</div> -->
						
						<div class="clearfix">
						<div class="pull-left">
							<!-- <h4 class="text-blue">Default Basic Forms</h4>
							<p class="mb-30 font-14">All bootstrap element classies</p> -->
							<a href="rbsales-list.php" class="btn btn-secondary btn-sm">View List</a>
						</div>
						<div class="pull-right">
							<button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-save"></i> Save</button>
							<!-- <a href="#basic-form1" class="btn btn-primary btn-sm scroll-click" rel="content-y"  data-toggle="collapse" role="button"><i class="fa fa-code"></i> Source Code</a> -->
						</div>
					</div>
					
					<div class="collapse collapse-box" id="basic-form1" >
						
					</div>
				</div>
				</form>
				
				

			</div>
			<?php include('include/footer.php'); ?>
		</div>
	</div>
	<?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(){ document.getElementById('csdate').focus();
			$('.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: [
				'copy', '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>