<script src="../js/jquery-1.9.1.js"></script>
<?php 
//echo "<pre>";print_r($_GET);exit;
if($_POST){
	$ptypess="busfee";
	$data=$db->getVal("select id from receipt where id=".$_GET["id"]." and type='".$_GET["type"]."'"); 
	if($data!=""){
		echo "record already exit";
	}else{
		$_POST["session"]=$_SESSION["admin"]["session"];
		$_POST["school_id"]=$_SESSION["admin"]["school_id"];
	$_POST["added_by"]=$_SESSION["admin"]["uid"];
		$res=$db->insertAry("pocket_receipt",$_POST);
		if($res>0)
		redirect();//URL_ROOT."print/?page_id=recepit&id=".$_GET["id"]."&rid=".$_POST["id"]);
		else 
		echo $db->getErMsg();
	}
}
?>
<style>
.page {	width: 687px; height: 275px; border: 1px dashed #333; padding: 5px;}
.headPrint { border-bottom: 1px solid #000; }
.headPrint img { float: left; margin-right: 14px; }
.txt{border-bottom:1px dashed #00F; border-left:0; border-right:0; border-top:0;}
.detail {border-bottom: 1px dashed #00F;min-width: 214px;border-left: 0;border-right: 0;border-top: 0;display: inline-block;height: 15px;padding: 2px 2px 2px 5px;color: #00f;font-size: 16px;text-transform: capitalize;}
.am {border: 0;width: 229px;padding: 3px 0;border-bottom: 1px solid;}
.amt {border: 1px solid;padding: 7px;border-radius: 7px;}
.recept {font-weight: bold;font-size: 22px;color: #FC0D0D;}
.btn_receipt{background: firebrick;color: white;height: 26px;width: 53px;border: 2px inset snow;border-radius: 10px;}
</style>
<?php 
$detail=$db->getRow("select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["idno"]."'"); 
$rdetil=$db->getRow("select * from receipt where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and class='".$_GET["class"]."' and type='".$_GET["type"]."' and install='".$_GET["install"]."' and idno='".$_GET["idno"]."' and paytype='".$_GET["paytype"]."'");
if(count($rdetil)>1)$_REQUEST=$rdetil;
$typess=$_GET["paytype"];
if($_GET["paytype"]=="busfee")$typess="busfee";
if($_GET["paytype"]=="classfee")$typess="fee";
if($_GET["paytype"]=="hostelfee")$typess="hostelfee";
elseif($_GET["paytype"]=="extraotherfinefee" || $_GET["paytype"]=="otherfee" || $_GET["paytype"]=="otherfinefee" || $_GET["paytype"]=="classfinefee" || $_GET["paytype"]=="busfinefee" || $_GET["paytype"]=="hostelfinefee")$typess=$_GET["detail"];

$LastQ=$db->getVal("select max(id) from transaction where rid='".$_GET["idno"]."' and school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and detail like 'Enquiryfee%'");
$CurrentQ=$db->getVal("select max(id) from reff_receipt where idno='".$_GET["idno"]."' and school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and type='".$typess."' and amount='".$_GET["amount"]."'");
?>
<form method="post" action="" name="test" style="background: salmon;">
<div  id="test">
<div class="page" id="page">
<div class="headPrint"><table width="100%">
        <tr>
          <td width="10%"><img src="<?php echo $LinksDetails["logo"] ?>" height="60px"></td>
          <td><h1 style="margin:0; padding:0;"><?php echo ucfirst($LinksDetails["site_name"]) ?></h1>
            <?php echo ucfirst(nl2br($LinksDetails["contact_content"])) ?></td>
        </tr>
      </table></div>

<table width="100%" border="0">
  <tr>
    <td>Recept No. <span class="recept"><?php //echo count($rdetil); 
	$id=(int)$db->getVal("select max(id) from receipt where school_id='".$_SESSION["admin"]["school_id"]."' and type='".$_GET["type"]."'")+1;
	//echo $db->getLastQuery();
	if(count($rdetil)>0)echo $rdetil["id"];else echo $id; ?></span>
    <input type="hidden" name="id" value="<?php if(count($rdetil)>0)echo $rdetil["id"];else echo $id; ?>">
    <input type="hidden" name="idno" value="<?php echo $_GET["idno"]; ?>">
    <input type="hidden" name="class" value="<?php echo $_GET["class"]; ?>">
    <input type="hidden" name="type" value="<?php echo $_GET["type"]; ?>">
    <input type="hidden" name="install" value="<?php echo $_GET["install"]; ?>">
    <input type="hidden" name="paytype" value="<?php echo $_GET["paytype"]; ?>">
    <input type="hidden" name="rrid" value="<?php echo $LastQ ?>" />
    </td>
    <td>Date : <input class="txt" type="text" readonly="readonly" name="udate" id="udate" value="<?php echo $_GET["udate"] ?>" required></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>Student Name : <span class="detail"><?php echo $detail["name"] ?></span></td>
    <td>Father Name : <span class="detail"><?php echo $detail["fname"] ?></span></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>Class : <span class="detail"><?php echo $detail["class"] ?></span></td>
    <td>Medium : <span class="detail"><?php echo $detail["medium"] ?></span></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>Contact No : <span class="detail"><?php echo $detail["mobile"] ?></span></td>
    <td>Detail : 
      <input class="txt" type="text" name="detail" readonly="readonly" id="detail" value="<?php echo $_GET["detail"]; ?>" required></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td class="amt">Amount : <input readonly="readonly" type="text" style="font-weight:bold;" onBlur="getValueOf(this)" name="amount" value="" required class="am" id="rs">
      </td>
    <td> In word : <span id="word" class="detail"></span>
    <input id="wordtxt" class="txt" type="hidden" name="word" value="" required>
    </td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>      </td>
    <td>Signature
    <?php if(!isset($_POST["amount"]) && count($rdetil)>0)echo "<br />Duplicate Copy";else "ye";?>
    </td>
    <td>&nbsp;</td>
  </tr>
</table>
</div>
</div>
<?php if(count($rdetil)>1){
?><input type="button" value="Print" class="btn_receipt" onclick="printDiv('test')" style="display:none" />
<input type="button" value="Print" class="btn_receipt" onclick="window.print()" /><?php
}else{
	
	?>
<input type="submit" id="saveprintme" class="btn_receipt" value="Save" style="display:none" />    
<input type="button" class="btn_receipt" onclick="checkreceptNo('<?php echo $id; ?>')" value="Save" />
<?php 
	if($LastQ==$CurrentQ) 
	{
		echo '<input type="button" class="btn_receipt" value="Delete" onclick="deleteMe()" />';
	}elseif($_GET["paytype"]!="classfee" && $_GET["paytype"]!="busfee"){
		 echo '<input type="button" class="btn_receipt" value="Delete" onclick="deleteMe()" />';
 	}
 } ?>

</form>
<script>
$(document).ready(function(e) {
	$("#rs").val('<?php echo $_GET["amount"]; ?>');
	var strs=$('#rs');
	getValueOf(strs);
});
var th = ['', 'thousand', 'million', 'billion', 'trillion'];
var dg = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
var tn = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
var tw = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
function toWords(s) {
    s = s.toString();
    s = s.replace(/[\, ]/g, '');
    if (s != parseFloat(s)) return 'not a number';
    var x = s.indexOf('.');
    if (x == -1) x = s.length;
    if (x > 15) return 'too big';
    var n = s.split('');
    var str = '';
    var sk = 0;
    for (var i = 0; i < x; i++) {
        if ((x - i) % 3 == 2) {
            if (n[i] == '1') {
                str += tn[Number(n[i + 1])] + ' ';
                i++;
                sk = 1;
            } else if (n[i] != 0) {
                str += tw[n[i] - 2] + ' ';
                sk = 1;
            }
        } else if (n[i] != 0) {
            str += dg[n[i]] + ' ';
            if ((x - i) % 3 == 0) str += 'hundred ';
            sk = 1;
        }
        if ((x - i) % 3 == 1) {
            if (sk) str += th[(x - i - 1) / 3] + ' ';
            sk = 0;
        }
    }
    if (x != s.length) {
        var y = s.length;
        str += 'point ';
        for (var i = x + 1; i < y; i++) str += dg[n[i]] + ' ';
    }
    return str.replace(/\s+/g, ' ');

}


function getValueOf	(idn){
	var id=$(idn).val();
	var wordn=toWords(id);
	$('#word').html(wordn);
	$('#wordtxt').val(wordn);
}
function printDiv(divID) {
	var styles="<style>.page {	width: 687px; height: 263px; border: 1px dashed #333; padding: 5px;}.headPrint { border-bottom: 1px solid #000; }.headPrint img { float: left; margin-right: 14px; }.txt{border-bottom:1px dashed #00F; border-left:0; border-right:0; border-top:0;}.detail {border-bottom: 1px dashed #00F;min-width: 214px;border-left: 0;border-right: 0;border-top: 0;display: inline-block;height: 15px;padding: 2px 2px 2px 5px;color: #00f;font-size: 16px;text-transform: capitalize;}.am {border: 0;width: 229px;padding: 3px 0;border-bottom: 1px solid;}.amt {border: 1px solid;padding: 7px;}.recept {font-weight: bold;font-size: 22px;color: #FC0D0D;}</style>";
	var divElements = document.getElementById(divID).innerHTML;
	var newbody="<html><head><title></title>"+styles+"</head><body>" +
	  divElements + "</body></html>";
    var thePopup = window.open( '', "Print", "menubar=0,location=0,height=700,width=700" );
	
	//alert(newbody); 
    thePopup.document.write(newbody);
    thePopup.print();
	//thePopup.onfocus=function(){ thePopup.close();}	
}
function deleteMe(){
	
	if(confirm("are you sure you want to delete?")){
		//?page_id=recepit&idno=3&udate=15-10-2014&amount=1000&type=fee&paytype=classfee&detail=School%20fee&install=2&class=2nd
		$.post( "<?php echo URL_ROOT ?>ajax.php", { 
				<?php if(isset($_GET["refid"]) && $_GET["refid"]!=""){ ?>refid: '<?php echo $_GET["refid"] ?>',<?php } ?>
				type: "DeletepocketInstallment" ,
				idno: '<?php echo $_GET["idno"] ?>',
				udate: '<?php echo $_GET["udate"] ?>',
				amount: '<?php echo $_GET["amount"] ?>',
				paytype: '<?php echo $_GET["paytype"] ?>',
				detail: '<?php echo $_GET["detail"] ?>',
				install: '<?php echo $_GET["install"] ?>',
				class: '<?php echo $_GET["class"] ?>',
				id: '<?php echo $id ?>',
				ip: '<?php echo $_GET["ip"] ?>'
			} , function( data ) {
				//alert(data);return false;
			data=data.trim();
			if(data=="")
				alert("This fee cannot be deleted")
			else window.parent.location.reload();
				// window.parent.location='<?php echo URL_ROOT; ?>index.php?src=Student&page_id=pocketmoney&action=add&id=<?php echo $_GET["idno"] ?>';
			/*if(data!=1)
				alert(data);
			else*/
				
		});
	}
}
function checkreceptNo(id){
	$.post( "<?php echo URL_ROOT ?>ajax.php", { 
			type: "checkreceptNo" ,
			paytype: '<?php echo $_GET["type"] ?>',
			id: id
		} , function( data ) {
		data=data.trim();
		if(data==""){
			$("#saveprintme").click();
		}
		else{
			alert("receipt id already exit");
		}
	});
}
</script>