<?php include("../db.php");?>
<script>
    var headPrint='<div class="headPrint"><img src="<?php echo $LinksDetails["logo"] ?>" height="60px"><h1><?php echo ucfirst(trim($LinksDetails["site_name"])) ?></h1></div>';
	var root='<?php echo URL_ROOT ?>';
</script>
<script type="text/javascript" src="<?php echo URL_ROOT ?>js/jquery-1.9.1.js"></script>
<link href='<?php echo URL_ROOT ?>assets/css/print.css' rel='stylesheet'>
<script type="text/javascript" src="<?php echo URL_ROOT ?>js/custom.js"></script>
<?php 
 
$aryForm=$db->getRow("SELECT * FROM student WHERE  school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET['id']."'");

// $isAlreadyEntry=$db->getVal("select id from inventory_receipt where idno='".$_GET["id"]."' and inventory_id in ('".$_GET["idno"]."')  and session='".$_SESSION["admin"]["session"]."' and school_id='".$_SESSION["admin"]["school_id"]."'");

$isAlreadyEntry = $rid = $id = $_REQUEST['rid'];

if($_POST){
	echo '1';
	print_r($_POST);

		$_POST["school_id"]=$_SESSION["admin"]["school_id"];
		$_POST["added_by"]=$_SESSION["admin"]["uid"];

		$db->insertAry("inventory_receipt",$_POST);
}

$allitems = $db->getRows("select * from inventory_receipt where idno='".$_REQUEST['id']."' and rid='".$_REQUEST['rid']."' ");

?><style>
.recept {
font-weight: bold;
font-size: 20px;
color: #FC0D0D;
}
.headprint .logo{
	margin: -5px 10px 0px 0px !important;
}

#pm1 {
    display:none;
}
#txnid {
    display:none;
}
#bankname {
    display:none;
}
#tdate {
    display:none;
}
</style>

<form method="post" action="" id='printMe'>

<div style="display:flex;">
<div style="width:50%; border: 4px solid black;background: #C1CCD8;">
<div class="headPrint" style="background: #C1CCD8; border-bottom: 4px solid black;"> 
<table width="100%">
        <tr>
          <!--<td width="10%"><img style="top:3px;position:relative;" src="<?php echo $LinksDetails["logo"] ?>" class="logo" height="50px" width="70px"></td>-->
           <td  style="text-align:center"><h5 style="margin:0; padding:0;"><?php echo ucfirst($LinksDetails["site_name"]) ?></h5>
            <span style="text-align:center;font-size:13px;"><?php echo ucfirst(nl2br($LinksDetails["contact_content"])); ?></span><br>
            <!--<span style="text-align:center;font-size:13px;">For Student</span>-->
			</td>
        </tr>
      </table>
  </div>
<table width="100%" style='background: #C1CBD8;font-size:12px;'>
    <tr>
        <td width="20%"><strong>R.No</strong></td><td><span class="recept"><?php echo $_REQUEST["rid"]; ?></span></td>
        <!--<td width="20%"><strong>Date</strong></td><td><?php echo date("d-m-Y",strtotime($_REQUEST["udate"])); ?></td>-->
		
		<td width="20%"><strong>Date</strong></td><td><?php echo date('d-m-Y', strtotime($allitems[0]['issue_date'])); ?></td>
    </tr>
    <tr>
        <td><strong>Name</strong></td><td><?php echo $aryForm["name"]; ?></td>
        
       <td><strong>Class</strong></td><td><?php echo $aryForm["class"]; ?></td>
    </tr>
    <tr>
        <td><strong>Father's</strong></td><td><?php echo $aryForm["fname"]; ?></td>
        <td><strong>Issued By</strong></td><td><?php 
		$addb=$_SESSION["admin"]["uid"];
		if(isset($_REQUEST["receiptid"]))$addb=$db->getVal("select added_by from inventory_receipt where id=".$_REQUEST["receiptid"]);
		echo $db->getVal("select name from users where id=".$addb);
		//echo $db->getLastQuery();
		 ?></td>
        <!--<td><strong>Mother's</strong></td><td><?php echo $aryForm["mname"]; ?></td>-->
    </tr>
    <!--<tr>-->
        
    <!--    <td><strong>Bank</strong></td><td><?php echo $_REQUEST[bank]?> </td>        -->
    <!--    <td><strong>Cheque No</strong></td><td><?php echo $_REQUEST[ch_no]?> </td>-->
    <!--</tr>-->
    <tr>
        <td><strong>Adm/No</strong></td><td><?php echo $aryForm["admissionno"]; ?></td>
        <td><strong>Contact</strong></td><td><?php echo $aryForm["mobile"]; ?></td>
    </tr>

    <tr>
          <?php 
		if($isAlreadyEntry!=''){
		$cheque_number=$db->getVal("SELECT txnid FROM inventory_receipt WHERE  rid= '$isAlreadyEntry'"); 
		
		$bankname=$db->getVal("SELECT bankname FROM inventory_receipt WHERE  rid= '$isAlreadyEntry'"); 
		}
		else { 
		    $cheque_number=$db->getVal("SELECT txnid FROM inventory_receipt WHERE  rid= '$id'");
		    $bankname=$db->getVal("SELECT bankname FROM inventory_receipt WHERE  rid= '$id'"); 
		}

     ?>
    <td><strong>Cheque No/Tr. No.</strong></td>
    
    <td>
        <span id="txnid"><?php echo $cheque_number; ?></span>
        <input id="txnidtext" type="text" name="txnid" value="<?php echo $cheque_number; ?>" style="background-color:white !important; height: 23px; width:90px; font-size:15px !important;">
    </td>
     <td><strong>Bank Name</strong></td>
    
    <td>
        <span id="bankname"><?php echo $bankname; ?></span>
        <input id="banknametext" type="text" name="bankname" value="<?php echo $bankname; ?>" style="background-color:white !important; height: 23px; width:90px; font-size:15px !important;">
    </td>
    </tr>
    <tr>
        <td><strong>Transaction Date</strong></td>
        <?php
            if($isAlreadyEntry!=''){
                $tdate=$db->getVal("select tdate from inventory_receipt where rid='$isAlreadyEntry' ");
            }
            else {
                $tdate=$db->getVal("select tdate from inventory_receipt where rid='$id' ");
            }
        ?>
        <td>
            <span id="tdate"><?php echo $tdate; ?></span>
            <input id="tdatetext" type="text" name="tdate" value="<?php echo $tdate; ?>" style="background-color:white !important; height: 25px; width:90px; font-size:15px !important;">
        </td>

        <td><strong>Mode</strong></td>
        <td>
	    	<?php 
    	    	if($isAlreadyEntry!=''){
            		$payment_mode=$db->getRow("SELECT payment_mode FROM inventory_receipt WHERE  rid= '$isAlreadyEntry'");
        		}
		        else { $payment_mode=$db->getRow("SELECT payment_mode FROM inventory_receipt WHERE  rid= '$id'"); }
		    ?>
    		<span id='pm1'><?=  $payment_mode['payment_mode']?></span>
		     <select id='status' name='payment_mode' onchange='change_pm()'>
		          <option <?php if($payment_mode['payment_mode'] == 'Cash') echo "selected";?> >Cash</option>
       	   	      <option <?php if($payment_mode['payment_mode'] == 'Cheque') echo "selected";?>>Cheque</option>
       	   	      <option <?php if($payment_mode['payment_mode'] == 'NEFT') echo "selected";?>>NEFT</option>
       	   	      <option <?php if($payment_mode['payment_mode'] == 'UPI') echo "selected";?>>UPI</option>
       	   	      <option <?php if($payment_mode['payment_mode'] == 'Bank Deposit') echo "selected";?>>Bank Deposit</option>
       	   	      <option <?php if($payment_mode['payment_mode'] == 'POS') echo "selected";?>>POS</option>
       	   	      <option <?php if($payment_mode['payment_mode'] == 'Debit Card') echo "selected";?>>Debit Card</option>
       	   	      <option <?php if($payment_mode['payment_mode'] == 'Credit Card') echo "selected";?>>Credit Card</option>
       	   	      <option <?php if($payment_mode['payment_mode'] == 'Online Payment') echo "selected";?>>Online Payment</option>
       	   	      <option <?php if($payment_mode['payment_mode'] == 'Mobile Wallet') echo "selected";?>>Mobile Wallet</option>
       	   	      <option <?php if($payment_mode['payment_mode'] == 'POS-2') echo "selected";?>>POS-2</option>
       	   	      <option <?php if($payment_mode['payment_mode'] == 'QR') echo "selected";?>>QR</option>
       	   	      <option <?php if($payment_mode['payment_mode'] == 'Swipe') echo "selected";?>>Swipe</option>
       	   	      
			</select>
        </td>


    </tr>

	<tr>
    <td colspan="4">
    
<?php
echo "<table width='100%' border='1'>
<tr>
	<th>Sr. No</th><th>Inventory Head</th><th>Qty.</th><th>Rate</th><th>Amount</th>
</tr>";
 
$i=0;$total_amount=0; $discount = 0;
        foreach($allitems as $it){$i++;
            
            $name= $db->getVal("select name from inventory where id='$it[inventory_id]'");
        	echo "<tr><th>$i</th><th>$name</th><th>$it[num_item]</th><th>$it[price]</th><th>$it[total_amount]</th></tr>";
    
            $total_amount+=$it['total_amount'];
            $discount+=$it['discount'];
        }

echo "<tr><th></th><th colspan='3'>Total  Amount</th><th>".$total_amount."</th></tr>";
echo "<tr><th></th><th colspan='3'>Total  Discount</th><th>".$discount."</th></tr>";
	
echo "</table><hr/><div class='footerPrint'><span>Signature...............................</span></div>";


	echo '<input id="myPrntbtn" style="background: firebrick;color: white;height: 26px;width: 53px;border: 2px inset snow;border-radius: 10px;" type="button"  onclick="printDivNoHeader(\'printMe\')" value="Print">';

?>

<input id="updatebtn" style="background: firebrick;color: white;height: 26px;width: 53px;border: 2px inset snow;border-radius: 10px;" type="button"  onclick="update()" value="Update">

<style type="text/css">
@media print {
    #myPrntbtn {
        display :  none;
    }
    #updatebtn{
        display: none;
    }
    
    #status {
        display:none;
    }
    
    #pm1 {
        display:block;
    }

    #txnidtext {
        display:none;
    }    
    #txnid{
        display:block;
    }
    
    #banknametext {
        display:none;
    }    
    #bankname{
        display:block;
    }

    #tdatetext {
        display:none;
    }    
    #tdate{
        display:block;
    }
    
}

</style>

	</td></tr>
</table>
<span style="color:red;text-align:center;font-size:14px;"><?php echo "[" ?>Note : If any correction,Please consult to fee counter.<?php echo "]" ?></span>
</div>


<div style="width:50%; border: 4px solid black;background: #C1CCD8;margin:0px 5px;">
<div class="headPrint" style="background: #C1CCD8; border-bottom: 4px solid black;"> 
<table width="100%">
        <tr>
          <!--<td width="10%"><img style="top:3px;position:relative;" src="<?php echo $LinksDetails["logo"] ?>" class="logo" height="50px" width="70px"></td>-->
           <td  style="text-align:center"><h5 style="margin:0; padding:0;"><?php echo ucfirst($LinksDetails["site_name"]) ?></h5>
            <span style="text-align:center;font-size:13px;"><?php echo ucfirst(nl2br($LinksDetails["contact_content"])); ?></span><br>
            <!--<span style="text-align:center;font-size:13px;">For Stock Room</span>-->
			</td>
        </tr>
      </table>
  </div>
<table width="100%" style='background: #C1CBD8;font-size:12px;'>
    <tr>
        <td width="20%"><strong>R.No</strong></td><td><span class="recept"><?php echo $_REQUEST["rid"]; ?></span></td>
        <!--<td width="20%"><strong>Date</strong></td><td><?php echo date("d-m-Y",strtotime($_REQUEST["udate"])); ?></td>-->
		
		<td width="20%"><strong>Date</strong></td><td><?php echo date('d-m-Y', strtotime($allitems[0]['issue_date'])); ?></td>
    </tr>
    <tr>
        <td><strong>Name</strong></td><td><?php echo $aryForm["name"]; ?></td>
        
			<td><strong>Class</strong></td><td><?php echo $aryForm["class"]; ?></td>
               
    </tr>
    <tr>
        <td><strong>Father's</strong></td><td><?php echo $aryForm["fname"]; ?></td>
        <td><strong>Issued By</strong></td><td><?php 
		$addb=$_SESSION["admin"]["uid"];
		if(isset($_REQUEST["receiptid"]))$addb=$db->getVal("select added_by from inventory_receipt where id=".$_REQUEST["receiptid"]);
		echo $db->getVal("select name from users where id=".$addb);
		//echo $db->getLastQuery();
		 ?></td>
        <!--<td><strong>Mother's</strong></td><td><?php echo $aryForm["mname"]; ?></td>-->
    </tr>
    <!--<tr>-->
        
    <!--    <td><strong>Bank</strong></td><td><?php echo $_REQUEST[bank]?> </td>        <td><strong>Cheque No</strong></td><td><?php echo $_REQUEST[ch_no]?> </td>-->
    <!--</tr>-->
    <tr>
        <td><strong>Adm/No</strong></td><td><?php echo $aryForm["admissionno"]; ?></td>
        <td><strong>Contact</strong></td><td><?php echo $aryForm["mobile"]; ?></td>
    </tr>
    
    <tr>
        <?php 
		if($isAlreadyEntry!=''){
		$cheque_number=$db->getRow("SELECT txnid FROM inventory_receipt WHERE  rid= '$isAlreadyEntry'"); 
		
		$bankname=$db->getRow("SELECT bankname FROM inventory_receipt WHERE  rid= '$isAlreadyEntry'"); 
		}
		else { 
		    $cheque_number=$db->getRow("SELECT txnid FROM inventory_receipt WHERE  rid= '$id'");
		    $bankname=$db->getRow("SELECT bankname FROM inventory_receipt WHERE  rid= '$id'"); 
		}
		?>      
          <td><strong>Cheque No/Tr. No.</strong></td>
         <td><?php echo $cheque_number['txnid']; ?></td>
           <td><strong>Bank Name</strong></td>
         <td><?php echo $bankname['bankname'] ?></td>
   
    </tr>
    <tr>
        <td><strong>Transaction Date</strong></td>
        <?php
            if($isAlreadyEntry!=''){
                $tdate=$db->getVal("select tdate from inventory_receipt where rid='$isAlreadyEntry' ");
            }
            else {
                $tdate=$db->getVal("select tdate from inventory_receipt where rid='$id' ");
            }
            
            if($tdate != ''){
        ?>
            <td><?php echo $tdate;
             ?></td>
            <?php 
        } ?>

        <td><strong>Mode</strong></td>
        <td>
		<?php
		if($isAlreadyEntry!=''){
		$payment_mode=$db->getRow("SELECT payment_mode FROM inventory_receipt WHERE  rid= '$isAlreadyEntry'"); 
		}
		else { $payment_mode=$db->getRow("SELECT payment_mode FROM inventory_receipt WHERE  rid= '$id'"); }
		?>
		<span id='pm' ><?=  $payment_mode['payment_mode']?></span>
        </td>
    </tr>
    
    
	<tr>
    <td colspan="4">
    
<?php
echo "<table width='100%' border='1'>
<tr>
	<th>Sr. No</th><th>Inventory Head</th><th>Qty.</th><th>Rate</th><th>Amount</th>
</tr>";

    $i=0;$total_amount=0;
        foreach($allitems as $it){$i++;

            $name= $db->getVal("select name from inventory where id='$it[inventory_id]'");
        	echo "<tr><th>$i</th><th>$name</th><th>$it[num_item]</th><th>$it[price]</th><th>$it[total_amount]</th></tr>";
    
            $total_amount+=$it['total_amount'];
        }

	

	echo "<tr><th></th><th colspan='3'>Total  Amount</th><th>".$total_amount."</th></tr>";
    echo "<tr><th></th><th colspan='3'>Total  Discount</th><th>".$discount."</th></tr>";
	

echo "</table><hr/><div class='footerPrint'><span>Signature...............................</span></div>";

// 	echo '<input style="background: firebrick;color: white;height: 26px;width: 53px;border: 2px inset snow;border-radius: 10px;" type="button"  onclick="printDivNoHeader(\'printMe\')" value="Print">';

?>
	</td></tr>
</table>
<span style="color:red;text-align:center;font-size:14px;"><?php echo "[" ?>Note : If any correction,Please consult to fee counter.<?php echo "]" ?></span>
</div>



</div>
</form>









<script>
function checkreceptNo(id){
	$.post( "<?php echo URL_ROOT ?>ajax.php", { 
			type: "checkreceptNo" ,
			paytype: '<?php echo $_REQUEST["type"] ?>',
			id: id
		} , function( data ) {
		data=data.trim();
		if(data==""){
			$("#saveprintme").click();
		}
		else{
			alert("receipt id already exit");
		}
	});
}

function change_pm()
{
	var v= $('#status').val();
	$('#pm').text(v);
	$('#pm1').text(v);
}

function update(){

    var mode = $("#status").val();
    var txnid = $("#txnidtext").val();
    var bankname = $("#banknametext").val();
    var tdate = $("#tdatetext").val();

    $.post( "<?php echo URL_ROOT ?>print/ajax.php", {
			type: "updateinventoryreceipt",
			rid: "<?php echo $rid; ?>",
			idno: "<?php echo $_GET['id'] ?>",
			mode: mode,
			txnid: txnid,
			bankname: bankname,
			tdate: tdate
		} , function( data ) {
            // alert(data);
            location.reload();
	});    

}
</script>