<?php
$pgMod="addincome";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);
if($_POST)
{
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	if(!isset($_POST['mode']) || trim($_POST['mode'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter Income mode.");
	}elseif(!isset($_POST['detail']) || trim($_POST['detail'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter Detail.");
	}elseif(!isset($_POST['amount']) || trim($_POST['amount'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter Amount.");
	}elseif(!isset($_POST['pmode']) || trim($_POST['pmode'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter Payment mode.");
	}
	if($flgEr!=TRUE && $pgAct=="add")
	{
		$detail=$_POST["detail"]." ".$_POST["mode"];
		if($_POST["chque"]!="")$detail.="<hr/>Cheque No:".$_POST["chque"];
		if($_POST["chque"]!="")$detail.="<br/>Bank Name:".$_POST["bankname"];
		$data = array(	
					 'rid'=>0,
					 'tid'=>0,
					 'tfor'=>$_POST["mode"],
					 'type'=>"deposit",
					 'amount'=>$_POST["amount"],
					'session'=>$_SESSION["admin"]["session"],
					 'status'=>"1",
					 'udate'=>$_POST["idate"],
					 "school_id"=>$_SESSION["admin"]["school_id"],"added_by"=>$_SESSION["admin"]["uid"],
					 'detail'=>$detail);
		$flgIn = $db->insertAry(TRANSECTION,$data);
		if(!is_null($flgIn))
		{
			$_SESSION['msg']='Saved Successfully';
			unset($_SESSION['form']);
			//sms($LinksDetails["DirectorNumber"],"Dear sir, Income Amount of Rs. ".$_POST["amount"]." from ".$_POST["mode"]." is Successfully Deposited in ".$LinksDetails["site_name"]." account","Income_Expense");
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Account", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
			
		}
	}
}
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$res=$db->delete(TRANSECTION,"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; }
	else { array_push($alert_err,$db->getErMsg()); }
	redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Account", "page_id"=>$pgMod)));
} 
if(isset($_SESSION['msg']) && $_SESSION['msg']!="")
{
	?>
	<div class="alert alert-success">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-thumbs-up-alt"></i> <b>Notice</b><?php echo $_SESSION['msg'] ?></i>
	</div>
	<?php
	unset($_SESSION["msg"]);
}
if(is_array($alert_err) && count($alert_err)>0)
{
	?>
	<div class="alert alert-error">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-warning-sign"></i> <b>Notice</b> <?php echo $alert_err[0] ?></i>
	</div>
	<?php
}
if($pgAct=="edit" && $_GET["id"]!="")
{   
	$aryForm=$db->getRow("SELECT * FROM ".TRANSECTION." WHERE school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
}
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''))
{
?>
<!-- Forms --> 
	<div class="row-fluid">
        <form method="post" action="">
			<!-- Forms: Box -->
			<div class="span12">

				<!-- Forms: Top Bar -->
				<div class="top-bar">
					<h3><i class="icon-cog"></i> Create a Income Transection</h3>
				</div>
				<!-- / Forms: Top Bar -->

				<!-- Forms: Content --> 
				<div class="well no-padding">

						<div class="control-group">
							<label class="control-label" for="mode">Income mode</label>
							<div class="controls">
								<select name="mode" class="span12" id="class">
                                    <option value="">mode</option>
                                    <?php 
                                        $class=$db->getRows("select * from mode where school_id='".$_SESSION["admin"]["school_id"]."' and type='Income'");
                                        foreach($class as $cla){
                                            ?>
                                            <option <?php if($cla["mode"]==$aryForm["mode"])echo 'selected';?> value="<?php echo $cla["mode"] ?>">
                                                <?php echo $cla["mode"] ?>
                                            </option>
                                            <?php
                                        }
                                    ?>
                                </select>
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="detail">Income Detail</label>
							<div class="controls">
								<textarea type="text" id="detail" name="detail" class="input-block-level"><?php if($_POST)echo $_POST["detail"]; ?></textarea>
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="idate">Income Date</label>
							<div class="controls">
								<input type="text" id="idate" name="idate" class="datepicker input-block-level" value="<?php if($_POST)echo $_POST["idate"]; ?>">
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="amount">Income Amount</label>
							<div class="controls">
								<input type="text" id="amount" name="amount" class="input-block-level" value="<?php if($_POST)echo $_POST["amount"]; ?>">
							</div>
						</div>
                        <div class="control-group">
                            <label class="control-label" for="netsalery">Pay Type</label>
                            <div class="controls">
                                    <label class="radio"><span><input onchange="hide_underof('1')" checked="checked" type="radio" name="pmode" value="cash"></span> Cash</label>
                                    <label class="radio"><span><input onchange="hide_underof('2')" type="radio" name="pmode" value="cheque" ></span> Cheque/DD</label> 
                            </div>
                        </div>
                        <div id="paymode" <?php echo 'style="display:none"'; ?>>
                            <div class="control-group">
                                <label class="control-label" for="chque">Cheque/DD No</label>
                                <div class="controls">
                                        <input type="text" id="chque" name="chque" class="input-block-level" value="<?php  echo $salDetail["cheque"];?>" > 
                                </div>
                            </div>
                             <div class="control-group">
                                <label class="control-label" for="bankname">Bank Name</label>
                                <div class="controls">
                                        <input type="text" id="bankname" name="bankname" class="input-block-level" value="<?php  echo $salDetail["bankname"];?>" > 
                                </div>
                            </div>
                        </div>
						<div class="form-actions">
							<button type="submit" class="btn btn-primary"> Save</button>
							<button type="button" class="btn"> Cancel</button>
						</div>
                        
				</div>
				<!-- / Forms: Content -->

			</div>
			<!-- / Forms: Box -->

        </form>
	</div>
<!-- / Forms -->
<!-- / Forms -->
<?php
}else{

	?>
	<div class="row-fluid"><div class="span12"><div class="top-bar">
    <h3><i class="icon-eye-open"></i> <?php echo ucwords($pgAct." ".$pgMod); ?></h3>
    <button style="float:right; margin-top:5px" onclick="window.location='./?src=Account&page_id=<?php echo $pgMod; ?>&action=add'" class="btn" type="button">Add <?php echo $pgMod; ?></button>
    </div>
    <div class="well no-padding">
	<?php
	$aryPgAct=array("src"=>"Account", "page_id"=>$pgMod,"action"=>"delete");
// 	$aryList=$db->getRows("select idno from student where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and status=1 and issue_TC=0 order by name");
    $allmodes = $db->getRows("select * from mode where type='Income' ");
	if(is_array($allmodes) && count($allmodes)>0)
	{
		?>
    
        <!--<input type="hidden" id="tabletype" value="deposite_data1" />-->
        <!--<table class="table" id="ajaxtable">-->
                <table class="table" id="example">
            <thead>
                <tr>
                    <!--<th>Serial No</th>-->
                   <th>Type</th>
                    <th>Amount</th>
                    <th>detail</th>
                    <th>Date</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                        foreach($allmodes as $mode){
                            $records = $db->getRows("select * from transaction where tfor='$mode[mode]' and type='deposit' and school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' ");
                                foreach($records as $row){ 
                    			 $aryPgAct["id"] = $row['id'];
                                ?>
                                    <tr>
                                        <td><?php echo $row["tfor"]; ?></td>
                                        <td><?php echo $row["amount"]; ?></td>
                                        <td><?php echo $row["detail"]; ?></td>
                                        <td><?php echo $row["udate"]; ?></td>
                                        <td><a href="javascript:;" onclick="del('<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct) ?>')" class="confirmation"><button type="button" class="btn btn-danger">Delete</button></a></td>
                                    </tr>
                                    <?php
                                }   
                        }
                        ?>
            </tbody>
        </table>
		<?php
	}
	else{
		?>
        <div class="alert alert-info">
          <a class="close" data-dismiss="alert">×</a>
          <i class="icon icon-warning-sign"></i> <b>Notice</b> Record not found </i>
        </div>
        <?php
	}
	?>
	</div></div></div>
	<?php
}
?>

<script type="text/javascript">
$(document).ready(function(e) {
    var oTable = $('#ajaxtable').dataTable( {
		"bProcessing": true,
		'sPaginationType': 'full_numbers',
		"sAjaxSource": 'ajax.php?type='+$('#tabletype').val(),
		'iDisplayLength': 5,
		"oLanguage": {
			"sSearch": "Search by any columns:"
		}
	});
});
function fetchByClass(){
	var name=$("#class").val();
	var section=$("#section").val();
	if(name!=""){
		var url='ajax.php?class='+name+'&type='+$('#tabletype').val();
		if(section!=""){
			url+='&section='+section
		}
		 oTable = $('#ajaxtable').dataTable( {
			"bProcessing": true,
			'sPaginationType': 'full_numbers',
			"bServerSide": false,
			"bDestroy":true,
			"sAjaxSource": url,
			'iDisplayLength': 5,
			"oLanguage": {
				"sSearch": "Search by any columns:"
			}
		});
	}else{
		var oTable = $('#ajaxtable').dataTable( {
			"bProcessing": true,
			'sPaginationType': 'full_numbers',
			"bServerSide": false,
			"bDestroy":true,
			"sAjaxSource": 'ajax.php?type='+$('#tabletype').val(),
			'iDisplayLength': 5,
			"oLanguage": {
				"sSearch": "Search by any columns:"
			}
		});
	}
}
</script>
<script>
function hide_underof(valu)
{
	if(valu=='1'){
		$('#paymode').hide('slow');
		$("#chque").val('');$("#backname").val('');
	}else 
	if(valu=='2'){
		$('#paymode').show('slow');
	}
}
$(function() {
	$(".datepicker").each(function(index, element) {
        var d = new Date(); // for now
		datetext=d.getHours()+": "+d.getMinutes()+": "+d.getSeconds();
		$( this ).datepicker({
		  changeMonth: true,
		  changeYear: true,
		  showOn: "button",
		  buttonImage: "js/images/calendar.gif",
		  buttonImageOnly: true,
		  buttonText: "Select date",
		  dateFormat: 'yy-mm-dd ',
		  onSelect: function(datetext){
			var d = new Date(); // for now
			datetext=datetext+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
			$(this).val(datetext);
		  }
		});
    });
});
</script>