<?PHP
ob_start();
session_start();
include 'connect.php';
if(isset($_SESSION['adminemail']))
{
$user=$_SESSION['adminemail'];

$gtp=mysqli_query($conn,"select * from admin where username='$user'");
$gtp1=mysqli_fetch_assoc($gtp);

include 'top.php';
?>


<div id="page-wrapper" >
          <div id="page-inner">
<div class="main-content">
				<div class="main-content-inner">
					<div class="breadcrumbs ace-save-state" id="breadcrumbs">
						<ul class="breadcrumb">
							<li>
								<i class="ace-icon fa fa-home home-icon"></i>
								<a href="#">Home</a>
							</li>
							<li class="active">Payment</li>
						</ul><!-- /.breadcrumb -->
					</div>

					<div class="page-content">
			

						<div class="page-header">
                        
<?php

$aca=mysqli_query($conn,"select * from financial where user='$user' order by ID desc");
$aca1=mysqli_fetch_assoc($aca);

$financial=$aca1['financial'];

?>                        
                        
							<table style="width:100%;">
                        
                        <tr><td style="width:50%;">
                        
							<h1>
								Payment
							</h1>
                            
                            </td>
                            
                            <td style="width:50%; text-align:right; padding-right:15px; font-size:15px;">
                            
                            <i class="fa fa-calendar"></i>&nbsp; <?php echo $financial; ?>
                            
                            </td></tr></table>
                            
						</div><!-- /.page-header -->
<br />

						<div class="row">
							<div class="col-xs-12">
                            
								<!-- PAGE CONTENT BEGINS -->
                                
<script src="assets/js/jquery.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css">
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script>
  <script>
    $(function() {
      // Initialize the datepicker
      $("#datepicker").datepicker({
        format: "mm/dd/yyyy",
        autoclose: true,
        todayHighlight: true
      });

      // Adjust the positioning of the datepicker dropdown
      $("#datepicker").on("show", function(e) {
        var inputOffset = $(this).offset();
        var inputHeight = $(this).outerHeight();
        var datepickerDropdown = $(".datepicker-dropdown");

        datepickerDropdown.css({
          top: inputOffset.top + inputHeight + 10,
          left: inputOffset.left
        });
      });
    });
  </script>
<script>

  $(".bank").live('change',function()
		  {
		  var bank=$(this).val();
		  var dataString = 'bank='+ bank;
		  //alert(dataString);
		  $.ajax
		  ({
		  type: "GET",
		  url: "getacc.php",
		  data: dataString,
		  cache: false,
		  success: function(resp)
		  {
		  //alert(resp);
		  //var company=$(resp).filter('#company').text();
		  //alert(company);
		  $('.account').html($('#account',resp).html());
		  }
		  });
		  });
		  
</script>


<script>
	
jQuery(function($){
		   
	// simple jQuery validation script
	$('#work').submit(function(){
		
		var valid = true;
		var errormsg = 'This field is required!';
		var errorcn = 'error';
		
		$('.' + errorcn, this).remove();			
		
		$('#required', this).each(function(){
			var parent = $(this).parent();
			if( $(this).val() == '' ){
				var msg = $(this).attr('title');
				msg = (msg != '') ? msg : errormsg;
				$('<span style="font-size:13px;color:#FF0000; padding-left:3px;" class="'+ errorcn +'">'+ msg +'</span>')
					.appendTo(parent)
					.fadeIn('fast')
					.click(function(){ $(this).remove(); });
				valid = false;
			};
		});
		
		return valid;
		
		$('button:submit').prop('disabled', true);
		
	});
	
});	

</script>
								
<?php
	
if(isset($_GET['id']))
{
	
$id=$_GET['id'];

if(isset($_POST['submit']))
{

$id=$_POST['id'];
$date=$_POST['date'];
$supplier=$_POST['supplier'];
$voucherno=$_POST['voucherno'];
$description=$_POST['description'];
$amount=$_POST['amount'];
$type=$_POST['paymentmode'];
$account=$_POST['account'];
$chequeno=$_POST['chequeno'];
$chequedate=$_POST['chequedate'];
$upimobileno=$_POST['upimobileno'];

if($supplier)
{

$date=date('Y-m-d',strtotime($date));

if($chequedate!='')
{
	$cdate=date('Y-m-d',strtotime($chequedate));
}

$up=mysqli_query($conn,"update payment set date='$date',supplier='$supplier',description='$description',amount='$amount',paymentmode='$type',account='$account',chequeno='$chequeno',chequedate='$cdate',upimobileno='$upimobileno' where ID='$id'");

if($up)
{

header("location:viewpayment.php?s=1");

}

}

}

else
{

$cus=mysqli_query($conn,"select * from payment where ID='".$id."'");
$cus1=mysqli_fetch_assoc($cus);

$b=mysqli_query($conn,"select * from bank where ID='$cus1[account]'");
$b1=mysqli_fetch_assoc($b);

if($cus1['chequedate']!='0000-00-00')
{
	$cdate=date('d-m-Y',strtotime($cus1['chequedate']));
}

echo '<form action="viewpayment.php?id='.$id.'" method="post" id="work">

<input type="hidden" name="id" value="'.$id.'" />

<center><table width="70%" id="page" style="line-height:35px;">

<tr><td id="page">Date <span class="red"></span></td><td width="2%"></td> <td id="page"><input class="form-control" style="width:30ch; margin-bottom:20px;" type="text" name="date" autocomplete="off" class="datepicker" id="datepicker" title="Enter Date" value="'.date('d-m-Y',strtotime($cus1['date'])).'" autocomplete="off" /required></td></tr>

<tr><td id="page">Supplier <span class="red"></span></td><td width="2%"></td> <td id="page"><select class="form-control" style="width:30ch; margin-bottom:20px;" name="supplier" style="width:220px;" id="required" title="Select Supplier"><option value="">Select Supplier</option>';

$cs=mysqli_query($conn,"select * from supplier order by name asc");
while($cs1=mysqli_fetch_assoc($cs))
{
$nm="<option value='".$cs1['ID']."'>".$cs1['name']."</option>";
$nm=str_replace("<option value='".$cus1['supplier']."'","<option value='".$cus1['supplier']."' selected", $nm);
echo $nm;
}

echo'</select></td></tr>

<tr><td id="page">Description <span class="red"></span></td><td width="2%"></td> <td id="page"><textarea class="form-control" style="width:30ch;height:15ch; margin-bottom:20px;" name="description" style="width:220px; height:70px;" id="required" title="Enter Description">'.$cus1['description'].'</textarea></td></tr>

<tr><td id="page" style="width:180px;">Amount <span class="red"></span></td><td width="2%"></td> <td id="page"><input class="form-control" style="width:30ch; margin-bottom:20px;"size="25" type="text" name="amount" autocomplete="off" id="required" title="Enter Amount" value="'.$cus1['amount'].'" autocomplete="off" /></td></tr>

</table>';

?>

<script type="text/javascript">
function showstuff(element){
    document.getElementById("Cash").style.display = element=="Cash"?"block":"none";
    document.getElementById("Bank").style.display = element=="Bank"?"block":"none";
	document.getElementById("gpay").style.display = element=="gpay"?"block":"none";
}
</script>

<table width="70%" id="page">

<tr><td id="page" style="width:180px;">Payment Mode <span class="red"></span></td><td width="2%"></td> <td id="page"><select class="form-control" style="width:30ch; margin-bottom:20px;" name="paymentmode" onchange="showstuff(this.value)" style="width:220px;" id="required" title="Select Payment Mode" class="paymentmode"><option value="">Select Payment Mode</option>
<option value="Cash" <?php if($cus1['paymentmode']=='Cash'){ echo"selected='selected'"; } ?>>Cash</option>  
<option value="Bank" <?php if($cus1['paymentmode']=='Bank'){ echo"selected='selected'"; } ?>>Bank</option>
<option value="gpay" <?php if($cus1['paymentmode']=='gpay'){ echo"selected='selected'"; } ?>>G-Pay</option>
</select></td></tr>

</table>

<DIV ID="Cash" style=" <?php if($cus1['paymentmode']=='Cash') 
{echo 'display:block;';} else{echo 'display:none;';}?> margin:0px; float:left;"></DIV>


<DIV ID="gpay" style=" <?php if($cus1['paymentmode']=='gpay') 
{echo 'display:block;';} else{echo 'display:none;';}?> margin:auto; width:70%;">

<table style="width:100%; margin-top:5px; line-height:35px;">

<tr><td style="width:180px;">Mobile No.</td><td style="padding:0 0px 0 3px;"></td><td><input class="form-control" style="width:30ch;margin-left:10px; margin-bottom:20px;" type="text" name="upimobileno" value="<?php echo $cus1['upimobileno']; ?>" /></td></tr>

</table>

</DIV>


<center><DIV ID="Bank" style=" <?php if($cus1['paymentmode']=='Bank') 
{echo 'display:block;';} else{echo 'display:none;';}?> margin:auto; width:70%;">
<table style="width:100%; line-height:35px;">

<tr><td id="page" width="180">Bank name <font color="red"></font> </td><td style="padding-right:13px;"></td><td id="page">

<select style="width: 30ch; margin-bottom:10px;padding-left:10px; height:34px;border-radius: 5px;border: 1px solid #c0c0c0;" name="bank" class="bank" style="width:220px;"><option value="">Select Bank</option>

<?php

$bn=mysqli_query($conn,"select * from bank group by bank");
while($bn1=mysqli_fetch_assoc($bn))
{

$land="<option value='".$bn1['bank']."'>".$bn1['bank']."</option>";
$land=str_replace("<option value='".$b1['bank']."'","<option value='".$b1['bank']."' selected",$land);
echo $land;

}

?>

</select>

</td></tr>

<tr><td id="page" width="180">Account name <font color="red"></font> </td><td width="11"></td><td id="page">

<select style="width: 30ch; margin-bottom:10px;padding-left:10px; height:34px;border-radius: 5px;border: 1px solid #c0c0c0;" name="account" class="account" style="width:220px;"><option value="">Select Account Name</option>

<?php

$bn=mysqli_query($conn,"select * from bank where bank='$b1[bank]' order by ID asc");
while($bn1=mysqli_fetch_assoc($bn))
{



$l="<option value='".$bn1['ID']."'>".$bn1['accountname']."</option>";
$l=str_replace("<option value='".$cus1['account']."'","<option value='".$cus1['account']."' selected",$l);
echo $l;

}

?>

</select>

</td></tr>


<tr><td id="page" width="180">Cheque No </td><td width="11"></td><td id="page"><input style="width: 30ch; margin-bottom:10px; height:34px;border-radius: 5px;border: 1px solid #c0c0c0;padding-left:10px;" type="text" name="chequeno" value="<?php echo $cus1['chequeno']; ?>" autocomplete="off" /></td></tr>

<tr><td id="page" width="180">Cheque Date </td><td width="11"></td><td id="page"><input style="width: 30ch; margin-bottom:10px;padding-left:10px; height:34px;border-radius: 5px;border: 1px solid #c0c0c0;" type="text" name="chequedate" class="popupdatepicker" value="<?php echo $cdate; ?>" autocomplete="off" /></td></tr>


</table>
</DIV></center>

<?php

echo'<br><center><button id="save" type="submit" class="btn btn-sm btn-primary" name="submit" value="Add">Save</button></center>

</form>';

}

}

else
	{
	
	if (isset($_GET['s'])) 
	{
	 $status=$_GET['s'];
		 if($status=='1')
		 {
		 echo '<div class="alert alert-success">
				<a class="close" data-dismiss="alert">x</a>
					Values Edited Successfully !
			  </div>';
		}
		elseif($status=='5')
		 {
		 echo '<div class="alert alert-success">
				<a class="close" data-dismiss="alert">x</a>
					Values Deleted Successfully !
			  </div>';
		}
	}



echo'<table style="width:99%; margin:auto;">
                                        
                                        <tr><td style="width:20%;">
                                        <a href="payment.php"><button class="btn btn-minier btn-info">+ Make Payment</button></a>
                                        </td>
                                        
                                        <td style="width:50%;"></td>
                                        
                                        <td style="width:30%;">
                                        <div class="clearfix" style="padding-top:5px;">
											<div class="pull-right tableTools-container"></div>
										</div>
                                        </td>
                                        
                                        </tr>
                                        
                                        </table>';
									
echo'<table id="dynamic-table" class="table table-striped table-bordered table-hover">';

echo"<thead><tr><th width='7%'>S.No</th><th style='width:12%;'>Date</th><th width='25%'>Supplier</th><th width='20%'>Amount (Rs.)</th><th style='width:8%;'>Edit</th></tr></thead>
<tbody>";

$select="select * from payment where date between '$aca1[fromdate]' and '$aca1[todate]' order by ID asc";
$select1=mysqli_query($conn,$select);
$sno=0;
while($value=mysqli_fetch_assoc($select1))
{
$sno=$sno+1;

$cus=mysqli_query($conn,"select * from supplier where ID='$value[supplier]'");
$cus1=mysqli_fetch_assoc($cus);
		
echo"<tr><td>".$sno."</td><td>".date('d-m-Y',strtotime($value['date']))."</td><td>$cus1[name]</td><td style='text-align:right;'>$value[amount]</td><td>

													   <div class='hidden-sm hidden-xs btn-group'>
													   
															<a href='viewpayment1.php?id=".$value['ID']."' title='View'>
																<i class='ace-icon fa fa-search-plus bigger-100 blue'></i></a> &nbsp;
																
															<a href='viewpayment.php?id=".$value['ID']."' title='Edit'>
																<i class='ace-icon fa fa-pencil bigger-100 green'></i></a> &nbsp;
																
															<a href='del.php?id=".$value['ID']."&desc=payment' title='Delete'>
																<i class='ace-icon fa fa-trash-o bigger-100 red'></i></a>
															
														 </div>

</td></tr>";
}

echo"</tbody></table>";

	}
	

?>                                
                               
								

								<!-- PAGE CONTENT ENDS -->
							</div><!-- /.col -->
						</div><!-- /.row -->
					

</div><!-- /.page-content -->
				</div>
			</div><!-- /.main-content -->
			</div><!-- /.main-content -->
			</div><!-- /.main-content -->


<?php
include 'bottom.php';
}
else
{

 header("location:login.php");
}

ob_flush();


?>