<? include "top.php"; ?>
<!DOCTYPE html>
<html>
<head>
	<?php include('include/head.php'); ?>
	<title>Add Expense</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'){
		if($psave==1){
		$trn_ac=$_POST['trn_ac'];
		$getac=f(q("SELECT * FROM tbl_ledger WHERE lid='$trn_ac'"));
		$trn_amt=$_POST['trn_amt'];
		$trn_date=$_POST['trn_date'];
		$trn_memo=$_POST['trn_memo'];
		$trn_type="EXPENSE AC";
		$trn_grate=$_POST['trn_grate'];
		$trn_igst=$_POST['trn_igst'];
		$csadded=$_SESSION['murgibhai'];
		if(floatval($trn_grate)>0){

			$trn_gst=1;

		}else{

			$trn_gst=0;

		}
		$trn_gamt=$_POST['trn_gamt'];


		$res=mysqli_query($con,"INSERT INTO tbl_trans SET trn_ac='$trn_ac',trn_name='$getac[lname]',trn_amt='$trn_amt',trn_date='$trn_date',trn_memo='$trn_memo',trn_type='$trn_type',trn_gst='$trn_gst',trn_grate='$trn_grate',trn_gamt='$trn_gamt',trn_igst='$trn_igst',trn_user='$csadded'");
	//	echo "INSERT INTO tbl_trans SET trn_ac='$trn_ac',trn_name='$getac[lname]',trn_amt='$trn_amt',trn_date='$trn_date',trn_memo='$trn_memo',trn_type='$trn_type',trn_gst='$trn_gst',trn_grate='$trn_grate',trn_gamt='$trn_gamt',trn_igst='$trn_igst'";
		//echo "INSERT INTO tbl_trans SET trn_ac='$trn_ac',trn_name='$getac[lname]',trn_amt='$trn_amt',trn_date='$trn_date',trn_memo='$trn_memo',trn_type='$trn_type'";


		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>

				<? }

		}else{
			?>
			<script type="text/javascript">
			Swal.fire('You are not Authorized to perform this Action','','error')
			 </script>
			<?

		}

	}elseif($action=='update'){
		
		if($pupdate==1){
		
		$cid=$_REQUEST['cid'];

		$trn_ac=$_POST['trn_ac'];
		$getac=f(q("SELECT * FROM tbl_ledger WHERE lid='$trn_ac'"));
		$trn_amt=$_POST['trn_amt'];
		$trn_date=$_POST['trn_date'];
		$trn_memo=$_POST['trn_memo'];
		$trn_type="EXPENSE AC";
		$trn_grate=$_POST['trn_grate'];
		$trn_igst=$_POST['trn_igst'];
		if(floatval($trn_grate)>0){

			$trn_gst=1;

		}
		$trn_gamt=$_POST['trn_gamt'];

				$res=mysqli_query($con,"UPDATE tbl_trans SET trn_ac='$trn_ac',trn_name='$getac[lname]',trn_amt='$trn_amt',trn_date='$trn_date',trn_memo='$trn_memo',trn_type='$trn_type',trn_gst='$trn_gst',trn_grate='$trn_grate',trn_gamt='$trn_gamt',trn_igst='$trn_igst' WHERE trnid='$cid'");

if($res){?>

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

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

				<? }
		}else{
			?>
			<script type="text/javascript">
			Swal.fire('You are not Authorized to perform this Action','','error')
			 </script>
			<?

		}

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



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

		if($pdelete==1){
		$cid=$_REQUEST['cid'];

		$res=mysqli_query($con,"DELETE FROM tbl_trans WHERE trnid='$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>

				<? }
		}else{
			?>
			<script type="text/javascript">
			Swal.fire('You are not Authorized to perform this Action','','error')
			 </script>
			<?

		}

	}

	


	?>
	<style type="text/css">
		.form-control:focus{
        border: 1px solid red;

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

    }
	</style>
	<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>Add Expenses</h4>
							</div>
							<nav aria-label="breadcrumb" role="navigation">
								<ol class="breadcrumb">
									<li class="breadcrumb-item"><a href="index.php">Home</a></li>
									<li class="breadcrumb-item active" aria-current="page">Add Expenses</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>
				<script type="text/javascript">
							
							function calgst(gval){

								if(parseFloat(gval)>0){


									var tamt=document.getElementById('trn_amt').value;
									if(parseFloat(tamt)>0){

											var grate=parseFloat(100)+parseFloat(gval);
											var c=parseFloat(tamt)*(parseFloat(gval)/parseFloat(grate));
											document.getElementById('trn_gamt').value=c.toFixed(2);

									}


								}else{

									document.getElementById('trn_gamt').value='0';
									//alert('0');

									}

							}
						</script>
				<!-- Default Basic Forms Start -->
				<div class="pd-20 bg-white border-radius-4 box-shadow mb-30">
					<small style="color: red;"><?=$msg?></small>
					<form action="<?=$_SERVER['PHP_SELF']?>?action=<? if($action=='edit'){?>update&cid=<?=$cid?><? }else{?>save<?} ?>" method="POST">
					
					<div class="clearfix" style="min-height: 30px;"></div>
					
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Expense Head</label>
							<div class="col-sm-12 col-md-10">
								<select class="custom-select2 form-control" name="trn_ac" onchange="calgst('0')">
									<option value="0">CHOOSE</option>
								<?
									$getle=mysqli_query($con,"SELECT * FROM tbl_ledger ORDER BY lname");
									while($arrl=mysqli_fetch_array($getle)){?>

										<option value="<?=$arrl['lid']?>" <? if($edit['trn_ac']==$arrl['lid']){?> selected <? } ?>><?=$arrl['lname']?></option>



								<?	}

								?>
							</select>
							</div>
						</div>
						
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Date</label>
							<div class="col-sm-12 col-md-4">
								<input type="text" name="trn_date" id="paydt" class="form-control" value="<?php if($action=='edit'){ echo $edit['trn_date']; }else{ echo date('Y-m-d'); } ?>" <?php if($_SESSION['lype']!='1'){?> readonly <?php } ?> >
							</div>
							<label class="col-sm-12 col-md-4 col-form-label text-right">Other State Payment</label>
							<div class="col-sm-12 col-md-2">
								<select name="trn_igst" class="form-control"  id="trn_igst">
									<option value="0" <? if($edit['trn_igst']=='0'){?> selected <? } ?>>No</option>
									<option value="1"  <? if($edit['trn_igst']=='1'){?> selected <? } ?>>Yes</option>
									
								</select>
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Payment Amount</label>
							<div class="col-sm-12 col-md-2">
								<input type="text" name="trn_amt" class="form-control" id="trn_amt"  value="<?=$edit['trn_amt']?>">
							</div>
							<label class="col-sm-12 col-md-2 col-form-label text-right" style="font-size: 14px;">GST Rate</label>
							<div class="col-sm-12 col-md-2">
								<select name="trn_grate" class="form-control"  id="trn_grate" onchange="calgst(this.value)">
									<option value="0" <? if($edit['trn_grate']=='0'){?>selected <? } ?>>0%</option>
									<option value="5" <? if($edit['trn_grate']=='5'){?>selected <? } ?>>5%</option>
									<option value="12" <? if($edit['trn_grate']=='12'){?>selected <? } ?>>12%</option>
									<option value="18" <? if($edit['trn_grate']=='18'){?>selected <? } ?>>18%</option>
									<option value="28" <? if($edit['trn_grate']=='28'){?>selected <? } ?>>28%</option>
								</select>
							</div>
							<label class="col-sm-12 col-md-2 col-form-label text-right">GST Amt</label>
							<div class="col-sm-12 col-md-2">
								<input type="text" name="trn_gamt" class="form-control"  id="trn_gamt"  value="<?=$edit['trn_gamt']?>">
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-12 col-md-2 col-form-label">Details</label>
							<div class="col-sm-12 col-md-10">
								<textarea name="trn_memo" class="form-control"><?=$edit['trn_memo']?></textarea>
							</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> -->
						</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>
					</form>
					<div class="collapse collapse-box" id="basic-form1" >
						
					</div>
				</div>
				<!-- <div class="pd-20 bg-white border-radius-4 box-shadow mb-30">
					<div class="clearfix mb-20">
						<div class="pull-left">
							<h5 class="text-blue">Expense List</h5>
						</div>
					</div>
					<div class="row">
						<table class="stripe hover multiple-select-row data-table-export nowrap">
							<thead>
								<tr>
									<th class="table-plus datatable-nosort">Date</th>
									<th>Name</th>
									<th>Amount</th>
									
									<th>Action</th>	
								</tr>
							</thead>
							<tbody>
								<?
									$getc=mysqli_query($con,"SELECT * FROM tbl_trans WHERE trn_type='EXPENSE AC' ORDER BY trn_date ASC");
									while($arr=mysqli_fetch_array($getc)){
								?>
								<tr>
									<td class="table-plus"><?=$arr['trn_date']?></td>
									<td><?=$arr['trn_name']?></td>
									<td><?=$arr['trn_amt']?></td>
									<td>
									<a href="<?=$_SERVER['PHP_SELF']?>?action=edit&cid=<?=$arr['trnid']?>">EDIT</a><br>
									<a href="<?=$_SERVER['PHP_SELF']?>?action=delete&cid=<?=$arr['trnid']?>" onclick="return confirm('Are you sure you want to delete this item?');">DELETE</a><br>
									

									</td>
								</tr>
								<? } ?>	
							</tbody>
						</table>
					</div>
				</div> -->
				

			</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(){
			$('.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 src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $( "#strdt" ).datepicker({
      changeMonth: true,
      changeYear: true
    });
    //$( "#strdt" ).datepicker( "option", "dateFormat", "yy-mm-dd" );

    $( "#enddt" ).datepicker({
      changeMonth: true,
      changeYear: true
    });
	<?php if($_SESSION['lype']=='1'){?> $( "#paydt" ).datepicker({
      changeMonth: true,
      changeYear: true
    });
    $( "#paydt" ).datepicker( "option", "dateFormat", "yy-mm-dd" );
	$('#paydt').datepicker('setDate', 'today'); <?php } ?>
    //$( "#enddt" ).datepicker( "option", "dateFormat", "yy-mm-dd" );
  } );
</script>
</body>
</html>