<?php
$pgMod="diposit";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);


if($_POST)
{
	//depositor
	$_SESSION['form']=$_POST;
	$flgEr=FALSE; 
	if(!isset($_POST['mode']) || trim($_POST['mode'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please select bank.");
	}if(!isset($_POST['depositor']) || trim($_POST['depositor'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter Depositor Name.");
	}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")
	{
		$data = array(	
					 'rid'      =>  0,
					 'bankname'      =>  $_POST["mode"],
					 'payment_mode'     =>  $_POST["pmode"],
					 'name'     =>  $_POST["depositor"],
					 'cheque_no'=>  $_POST["chque"],
					 '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'   =>  $_POST["detail"],
					 );
		$flgIn = $db->insertAry("banktransaction",$data);
		if(!is_null($flgIn))
		{
			$_SESSION['msg']='Saved Successfully';
			unset($_SESSION['form']);
			sms($LinksDetails["DirectorNumber"],"Dear sir, Income Amount of Rs. ".$_POST["amount"]." from ".$db->getVal("select bank from bank where school_id='".$_SESSION["admin"]["school_id"]."' and id=".$_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($flgEr!=TRUE && $pgAct=="edit")
	{
		$data = array(	

					 'bankname'     =>  $_POST["mode"],
					 'payment_mode' =>  $_POST["pmode"],
					 'name'         =>  $_POST["depositor"],
					 'cheque_no'    =>  $_POST["chque"],
					 'amount'       =>  $_POST["amount"],
					 'udate'        =>  $_POST["idate"],
                     "added_by"     =>  $_SESSION["admin"]["uid"],
					 'detail'       =>  $_POST["detail"],
					 );

		$flgIn = $db->updateAry("banktransaction",$data, "where id='".$_GET['id']."' ");
		if(!is_null($flgIn))
		{
			$_SESSION['msg']='Saved Successfully';
			unset($_SESSION['form']);
			sms($LinksDetails["DirectorNumber"],"Dear sir, Income Amount of Rs. ".$_POST["amount"]." from ".$db->getVal("select bank from bank where school_id='".$_SESSION["admin"]["school_id"]."' and id=".$_POST["mode"])." is Successfully Withdraw 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());
		}
	}
    
}

if($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$res=$db->delete("banktransaction","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
}
?>
<?php
if($pgAct=="edit" && $_GET["id"]!="")
{   
	$aryForm=$db->getRow("SELECT * FROM banktransaction 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">Bank Name</label>
							<div class="controls">
								<select name="mode" class="span12" id="class">
                                    <option value="">Bank</option>
                                    <?php 
                                        $class=$db->getRows("select * from bank  where school_id='".$_SESSION["admin"]["school_id"]."'");
                                        foreach($class as $cla){
                                            ?>
                                            <option <?php if($cla["id"]==$aryForm["bankname"])echo 'selected';?> value="<?php echo $cla["id"] ?>">
                                                <?php echo $cla["bank"] ?>(<?php echo $cla["account"] ?>)
                                            </option>
                                            <?php
                                        }
                                    ?>
                                </select>
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="depositor">Depositor Name</label>
							<div class="controls">
								<textarea type="text" id="depositor" name="depositor" class="input-block-level"><?php if($_POST){echo $_POST["depositor"];} else{echo $aryForm['name'];} ?></textarea>
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="detail">Deposit Detail</label>
							<div class="controls">
								<textarea type="text" id="detail" name="detail" class="input-block-level"><?php if($_POST){echo $_POST["detail"];} else{echo $aryForm['detail'];} ?></textarea>
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="idate">Deposit Date</label>
							<div class="controls">
								<input type="text" id="idate" name="idate" class="input-block-level datepicker" value="<?php if($_POST){echo $_POST["idate"];} elseif (!empty($aryForm["udate"])) echo $aryForm["udate"]; else echo date('Y-m-d H:i:s'); ?>">
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="amount">Deposit Amount</label>
							<div class="controls">
								<input type="number" id="amount" name="amount" class="input-block-level" value="<?php if($_POST){echo $_POST["amount"];} else{echo $aryForm['amount'];} ?>">
							</div>
						</div>
                        <div class="control-group">
                            <label class="control-label" for="netsalery">Deposit Mode</label>
                            <div class="controls">
                                    <label class="radio"><span><input onchange="hide_underof('1')" checked="checked" type="radio" name="pmode" value="Cash" <?php if($aryForm['payment_mode']=="Cash")echo 'checked'; ?>></span> Cash</label>
                                    <label class="radio"><span><input onchange="hide_underof('2')" type="radio" name="pmode" value="POS"  <?php if($aryForm['payment_mode']=="POS")echo 'checked'; ?>></span> POS</label>
                                    <label class="radio"><span><input onchange="hide_underof('3')" type="radio" name="pmode" value="Online"  <?php if($aryForm['payment_mode']=="Online")echo 'checked'; ?>></span> Online</label>
                                    <label class="radio"><span><input onchange="hide_underof('4')" type="radio" name="pmode" value="Cheque/DD"  <?php if($aryForm['payment_mode']=="Cheque/DD")echo 'checked'; ?>></span> Cheque/DD</label>
                            </div>
                        </div>
                        <div id="paymode" <?php if($pgAct=="add"){ echo 'style="display:none"';}elseif($aryForm["payment_mode"]!="Cheque/DD"){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 $aryForm["cheque_no"];?>" > 
                                </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>
	
	
	<?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>
      <div style="float:right; margin-top:5px">
                	<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>
    <div class="well no-padding">
      <?php
	$aryPgAct=array("src"=>"Account", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("select b.id,b.bank,c.id,c.cheque_no, c.payment_mode,c.bankname,c.amount,c.udate,c.detail,c.type,c.school_id from banktransaction as c inner join bank as b on c.bankname=b.id where c.school_id='".$_SESSION["admin"]["school_id"]."' and type='deposit'");
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
      <table class="table" id="example">
        <thead>
          <tr>
            <th>Sr.No.</th>
            <th>Bank Name</th>
            <th>Detail</th>
            <th>Amount</th>
			<th>Deposit Mode</th>
			<th>Deposit Date</th>
			<th>Action</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach($aryList as $clist){ $i++; 
            $aryPgAct["id"]=$clist['id'];
            
            if($clist['payment_mode'] == "Cheque/DD"){
		        $payment_mode = "$clist[payment_mode] - $clist[cheque_no]"; 
		    } else {
		        $payment_mode = "$clist[payment_mode]";
		    }
            ?>
          <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
            <td><?php echo $i; ?></td>
            <td><?php  echo $clist["bank"] ?></td>          
            <td><?php echo $clist["detail"] ?></td>
           
			 <td><?php echo $clist["amount"] ?></td>
			  <td><?php echo $payment_mode; ?></td>
			   <td><?php echo $clist["udate"] ?></td>
		    <td><?php $aryPgAct['action']="edit"; ?>
		    <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>">
              <button type="button" class="btn btn-info">Edit</button>
              </a>
                <?php $aryPgAct['action']="delete"; ?>
              <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>">
              <button type="button" class="btn btn-danger">Delete</button>
              </a></td>
          </tr>
          <?php } ?>
        </tbody>
        <tfoot>
          <tr>
            <th></th>
            <th></th>
            <th></th>
            <th></th>
            <th colspan="2"></th>
          </tr>
        </tfoot>
      </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
}?>	
	
<!-- / Forms -->
<!-- / Forms -->
<script>
function hide_underof(valu)
{
	if(valu=='1' || valu=='2' || valu=='3'){
		$('#paymode').hide('slow');
		$("#chque").val('');$("#backname").val('');
	}else 
	if(valu=='4'){
		$('#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>