<?php
include("../db.php");
$ch=$_REQUEST["type"];
if($ch=="saleryDetail"){
	$facDetail=$db->getRow("select * from staff where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["id"]."'");
	$salDetail=$db->getRow("select * from paysalery where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_REQUEST["id"]."' and uyear='".$_REQUEST["year"]."' and udate='".$_REQUEST["month"]."'");
	$salleryRecord=json_decode($salDetail["detail"]);
	//print_r($salleryRecord);echo $db->getErMsg()."<hr>".$db->getLastQuery();
	if(is_array($salleryRecord) && count($salleryRecord)>0){
		?><script>$(document).ready(function(){$("#paynowBTN").hide()})</script><?php
		//echo "<pre>";print_r($salleryRecord);echo "<pre>";
		foreach($salleryRecord as $sal){
			$symbol=$db->getVal("select opration from saleryStructure where school_id='".$_SESSION["admin"]["school_id"]."' and name='".$sal->key."'");
			?>
			<input type="hidden" name="key[]" value="<?php echo $sal->key ?>" />
            <div class="control-group">
                <label class="control-label" for="sturnture"><?php echo $sal->key ?></label>
                <div class="controls">
                    <input required type="text" onkeyup="getTotal()" rel="<?php echo $symbol; ?>" name="value[]" readonly class="input-block-level sal" value="<?php echo $sal->value ?>" >
                </div>
            </div>
			<?php
		}
	}else{
		?><script>$(document).ready(function(){$("#paynowBTN").show()})</script><?php
		?> <div class="control-group">
                <label class="control-label" for="salery">Basic salery</label>
                <div class="controls">
                    <input readonly type="text" id="salery" onkeyup="getTotal()" name="value[]" class="input-block-level sal" value="<?php echo $facDetail["salery"] ?>" >
                    <input type="hidden" name="key[]" value="Basic salery" />
                </div>
            </div><?php
		$saleryStructure=$db->getRows("select * from saleryStructure where school_id='".$_SESSION["admin"]["school_id"]."' and status=1");
		if(is_array($saleryStructure) && count($saleryStructure)>0){
			foreach($saleryStructure as $s){
				
				?>
				<input type="hidden" name="key[]" value="<?php echo $s['name'] ?>" />
				<div class="control-group">
					<label class="control-label" for="sturnture"><?php echo $s['name'] ?></label>
					<div class="controls">
						<input required type="text" name="value[]" onkeyup="getTotal()" id="sturnture<?php echo $s['id'] ?>" rel="<?php echo $s['opration'] ?>" class="input-block-level sal" value="0" >
					</div>
				</div>
				<?php
			}
		}else{
			?><div class="alert alert-info">
			  <a class="close" data-dismiss="alert">×</a>
			  <i class="icon-info"></i> Please create salery structure first
			</div><?php
		}
	}
	?>
	<div class="control-group">
		<label class="control-label" for="netsalery">Net Salery</label>
		<div class="controls">
			<input type="text" id="netsalery" name="netsalery" class="input-block-level" readonly value="0" >
		</div>
	</div>
	<div class="control-group">
		<label class="control-label" for="netsalery">Pay Type</label>
		<div class="controls">
				<label class="radio"><span><input <?php if($salDetail["paytype"]!="cheque") echo "checked"; ?>  onChange="hide_underof('1')" type="radio" name="paytype" value="cash"></span> Cash</label>
				<label class="radio"><span><input <?php if($salDetail["paytype"]=="cheque") echo "checked"; ?> onChange="hide_underof('2')" type="radio" name="paytype" value="cheque" ></span> Cheque/DD</label> 
		</div>
	</div>
	<div id="paymode" <?php if($salDetail["paytype"]=="cheque")echo '';else 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>
	<?php
}

elseif($ch=="findteacher"){
		$where="";
	if(isset($_REQUEST["section"]) && $_REQUEST["section"]!="")
			$where=" AND section='".$_REQUEST["section"]."' ";
	$subjects=$db->getRows("select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["cls"]."' and session='".$_SESSION["admin"]["session"]."' $where order by name");
echo	$ss="select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and class='".$_REQUEST["cls"]."' and session='".$_SESSION["admin"]["session"]."' $where order by name";
	?><select name="idno" id="idno"><?php
	if(is_array($subjects) && count($subjects)>0){
		?><option value="">All Student</option><?php
		foreach($subjects as $cla){
			?>
			<option value="<?php echo $cla["idno"] ?>">
				<?php echo $cla["name"] ?>
			</option>
			<?php
		}
	}
	?></select><?php
}
elseif($ch=="CalenderDetail"){
	
	$mn=date('m',strtotime($_REQUEST["year"]."-".$_REQUEST["month"]."-1"));$y=0;
	$y=$_REQUEST["year"];
	$maxDays=date('t',strtotime($y."-".$mn."-1"));
	for($i=1; $i<= $maxDays;$i++){ $day=date('D',strtotime($y."-".$mn."-".$i));
		$class="success";
		$chkHoli=$db->getVal("select status from holiday where school_id='".$_SESSION["admin"]["school_id"]."' and date='".$y."-".$mn."-".$i."'");
		$chkAtt=$db->getVal("select status from attandenc where school_id='".$_SESSION["admin"]["school_id"]."' and date='".$y."-".$mn."-".$i."' and type='1' and idno='".$_REQUEST["id"]."'");
		if(strtolower($day)=="sun") $class="danger";
		elseif($chkHoli=="1") $class="danger";
		elseif($chkAtt=="0") $class="default";
		
		?><span title="<?php echo $day; ?>" class="btn btn-<?php echo $class; ?>" rel="tooltip" style="width:20px; height:20px; margin:3px;padding: 2px;">
		&nbsp;&nbsp;<?php echo $i; ?>&nbsp;&nbsp;
		</span><?php if($i%7==0)echo "<br/>";
	}
}

elseif($ch=="delete_photo"){
		$aryForm=$db->getRow("SELECT * FROM staff WHERE school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_POST["id"]."'");
$path ="../media/photo/";
	$name=microtime()."-ms.jpg";
	file_put_contents("media/photo/".$name, file_get_contents($_POST["imgsrc"]));
	$file=$aryForm["photo"];
	
	unlink($path . "/" . $file) or die("Failed to <strong class='highlight'>delete</strong> file");
	$name="";
   $db->updateAry("staff",array("photo"=>$name),"where idno=".$_POST["id"]);
}

elseif($ch=="paybybranch"){
	$data=array("faculty_id"    =>  $_REQUEST["faculty_id"],
				"totalDays"     =>  $_REQUEST["totalDays"],
				"SchoolSalary"  =>  $_REQUEST["SchoolSalary"],
				"OneDayPayment" =>  $_REQUEST["OneDayPayment"],
				"Diffrence"     =>  $_REQUEST["Diffrence"],
				"DayAbsent"     =>  $_REQUEST["DayAbsent"],
				"CLAmount"      =>  $_REQUEST["CLAmount"],
				"AdvancePayment"=>  $_REQUEST["AdvancePayment"],
				"GrossTotal"    =>  $_REQUEST["GrossTotal"],
				"dmonth"        =>  $_REQUEST["dmonth"],
				"dyear"         =>  $_REQUEST["dyear"],
				"school_id"     =>  $_SESSION["admin"]["school_id"],
				"added_by"      =>  $_SESSION["admin"]["added_by"],
				"session"       =>  $_SESSION["admin"]["session"],
				
				"medical"           =>  $_REQUEST["medical"],
				"transport"         =>  $_REQUEST["transport"],
				"pfemployer"        =>  $_REQUEST["pfemployer"],
				"gradepay"          =>  $_REQUEST["gradepay"],
				"dearnessallowance" =>  $_REQUEST["dearnessallowance"],
				"specialallowance"  =>  $_REQUEST["specialallowance"],
				"hrallowance"       =>  $_REQUEST["hrallowance"],

                "uniform"           =>  $_REQUEST["uniform"],
                "tds"               =>  $_REQUEST["tds"],
                "pf"                =>  $_REQUEST["pf"],
                "esi"               =>  $_REQUEST["esi"],
                "houserent"         =>  $_REQUEST["houserent"],
                "electricitybill"   =>  $_REQUEST["electricitybill"],
			);
	$flgIn  =   $db->insertAry("paybybranch",$data);
	echo $db->getErMsg();

	$tid    =   withdraw($_REQUEST["GrossTotal"],$_REQUEST["faculty_id"],$flgIn,"salery","Give Faculty salery");
	echo $db->getErMsg();

	$db->updateAry("paybybranch",array("tid"=>$tid["did"]),"where id=".$flgIn);
	echo $db->getErMsg();
}

elseif($ch=="deletepaybybranch"){
	$flgIn=$db->delete("paybybranch","where id=".$_REQUEST["payid"]);
	$flgIn=$db->delete(TRANSECTION,"where id=".$_REQUEST["tid"]);
}
?>