<?php
include("../db.php");
$ch=$_REQUEST["type"];

    if($ch=="paybybranch"){
        $idno = $_REQUEST['idno'];
        $allitems = $_REQUEST['items'];
        $maxid = $db->getVal("select max(rid) from inventory_receipt");
        $rid = $maxid + 1;
        foreach($allitems as $item){
        $data = array(  "rid"=>$rid,
        				"idno"=>$idno,
        				"inventory_id"=>$item["id"],
        				"price"=>$item["price"],
        				"num_item"=>$item["qty"],
        			    "total_amount"=>$item["amount"],
        			    "discount"=>$item["discount"],
        				"school_id"=>$_SESSION["admin"]["school_id"],
        				"added_by"=>$_SESSION["admin"]["uid"],
        				"session"=>$_SESSION["admin"]["session"],
        				"payment_mode" => "Cash"
        				);
        	$flgIn=$db->insertAry("inventory_receipt",$data);
        // 	echo $db->getErMsg();
        }
        if($flgIn > 0){
            echo $rid;
        }
    }

    if($ch=="facultyinventory"){
        $idno = $_REQUEST['idno'];
        $allitems = $_REQUEST['items'];
        $maxid = $db->getVal("select max(rid) from faculty_inventory_receipt");
        $rid = $maxid + 1;
        foreach($allitems as $item){
        $data = array(  "rid"=>$rid,
        				"idno"=>$idno,
        				"inventory_id"=>$item["id"],
        				"price"=>$item["price"],
        				"num_item"=>$item["qty"],
        			    "total_amount"=>$item["amount"],
        			    "discount"=>$item["discount"],
        				"school_id"=>$_SESSION["admin"]["school_id"],
        				"added_by"=>1,
        				"session"=>$_SESSION["admin"]["session"],
        				"payment_mode" => "Cash"
        				);
        	$flgIn=$db->insertAry("faculty_inventory_receipt",$data);
        // 	echo $db->getErMsg();
        }
        if($flgIn > 0){
            echo $rid;
        }
    }

    if($ch == "get_inventory"){
        ?>
    		<select id="mySelect1" name="name" style="width:100%;" onchange="getBudget(this.value)">
                <!--<option value="">--Select--</option>-->
                <?php $allSubjects = $db->getRows("select * from inventory_names where head_name='$_POST[head]' and school_id='".$_SESSION["admin"]["school_id"]."' and status='1' ");
                foreach($allSubjects as $sub){
                    ?>
                    <option value="<?php echo $sub['name']; ?>" ><?php echo $sub['name']; ?></option>
                    <?php
                    }
                ?>
            </select>
        <?php
    }
    
    if($ch == "getList"){
        $where = "";
        if($_POST['name']!=""){
            $where = " head='$_POST[name]' and ";
        }

    	$aryList=$db->getRows("select * from inventory where $where school_id='".$_SESSION["admin"]["school_id"]."'  order by name");
        ?>
            <input type="hidden" value="<?php echo count($aryList); ?>" id="allitem" />
            <table class="table">
                <thead>
					<tr>
						<th>Serial No</th>
						<th>Inventory Name</th>
						<th>Inventory Quantity</th>
						<th>Price</th>
						<th>Booking Inventory </th>
                        <th>Total Amount</th>
                        <th>Discount</th>
                        <th>Tick <br>
                        <input  onchange="checkAll()" class="check" type="checkbox" name="check[]" value="" id="checkboxa" />                        </th>
					</tr>
				</thead>
				<tbody>
						<?php foreach($aryList as $clist){ $i++;

						?>
						<tr>
				    	<td><?php echo $i; ?></td>
						<td><?php echo $clist["name"] ?>
    						<input type="hidden" id="item_name<?php echo $i; ?>" value="<?php echo $clist["name"] ?>" />
						</td>
					    <td>
					        <?php $sold = $db->getVal("select sum(num_item) from  inventory_receipt where inventory_id='".$clist['id']."' ");
    					          $sold1 = $db->getVal("select sum(num_item) from  faculty_inventory_receipt where inventory_id='".$clist['id']."' ");
                                  echo $rem = $clist["quantity"] - $sold - $sold1;
                                 
                             ?>
				        </td>
					    <td>
					        <input class="py" type="hidden"  data-id="<?php echo $i; ?>" id="price<?php echo $i; ?>" value="<?php echo $clist["price"] ?>" /> <?php echo $clist["price"] ?>
					    </td>

				     	<td width="15%">
				     	    <!--<input class="py" type="text" name="advance_payment[]" data-id="<?php echo $clist['id'] ?>" id="itom<?php echo $clist['id'] ?>"  value="1" />-->
				     	    <input class="py" type="text" name="advance_payment[]" data-id="<?php echo $i; ?>" id="itom<?php echo $i; ?>"  value="1" onkeyup="showpreview(<?php echo $i; ?>)" />
			     	    </td>
 
                        <td>
                            <!--<input class="py" name="grasstotal[]" type="text" data-id="<?php echo $clist['id'] ?>" id="GrossTotal<?php echo $clist['id'] ?>"  value="<?php echo $clist["price"]*1; ?>" />-->
                            <input class="py" name="grasstotal[]" type="text" data-id="<?php echo $i; ?>" id="GrossTotal<?php echo $i; ?>"  value="<?php echo $clist["price"]*1; ?>" readonly />
                        </td>

                        <td>
                            <input class="py" type="text" name="discount[]" style="width:100px;" id="discount<?php echo $i; ?>" onkeyup="showpreview(<?php echo $i; ?>)" />
                        </td>

                        <td>
                            <input class="checkboxes" type="checkbox" name="sid[]" value="<?php echo $clist["id"];?>" id="item_<?php echo $i; ?>" onclick="showpreview(<?php echo $i; ?>)" />
                        </td>
                         <!--<td class=""><input class="checkboxes" type="checkbox" name="sid[]" value="<?php echo $clist["id"];?>" id="cat<?php echo $clist["id"]; ?>" /></td>-->
                         <!--<td><button class="btn btn-default" onclick="paynow('<?php echo $clist['id'] ?>')" data-id="<?php echo $clist['id'] ?>" type="button" id="btn<?php echo $clist['id'] ?>">Pay</button></td>-->

                   </tr>
					<?php	}
						?>
                    <tr class='notfound'>
                     <td colspan='4'>No record found</td>
                   </tr>
                    </tbody>
                 </table>

        
        <?php
    }

    if($ch == "stockreport"){
        $where = "";
        if($_POST['name']!=""){
            $where = " where head='$_POST[name]' ";
        }

    // 	$aryList=$db->getRows("select * from inventory where $where school_id='".$_SESSION["admin"]["school_id"]."'  order by name");
    	$allitems = $db->getRows("select * from inventory $where");
        ?>
            <table class="table">
                <thead>
                    <tr>
                        <th>S. No.</th>
                        <th>Item</th>
                        <th>Location</th>
                        <th>Price per qty</th>
                        <th>Quantity</th>
                        <th>Remaining</th>
                        <th>Amount</th>
                    </tr>
                </thead>
				<tbody>
                            <?php $i = 1; $total = 0;
                                foreach($allitems as $item){
                                ?>
                                <tr>
                                    <td><?php echo $i; ?></td>
                                    <td><?php echo $item['name']; ?></td>
                                    <td><?php echo $item['location']; ?></td>
                                    <td><?php echo $item['price']; ?></td>
                                    <td><?php echo $item['quantity']; ?></td>
                                    <td>
                                        <?php $sold = $db->getVal("select sum(num_item) from  inventory_receipt where inventory_id='".$item['id']."' ");
                                        $sold1 = $db->getVal("select sum(num_item) from  faculty_inventory_receipt where inventory_id='".$item['id']."' ");
                                        echo $rem = $item["quantity"] - $sold - $sold1;
                                        ?>
                                    </td>
                                    <td>
                                        <?php echo $item['price']*$rem;
                                        $total+= $item['price']*$rem;
                                        ?>
                                    </td>
                                </tr>
                                <?php $i++;
                            } ?>
                        </tbody>
                        <tfoot>
                            <tr>
                                <td colspan="6">Total</td>
                                <td>
                                    <?php echo $total; ?>
                                </td>
                            </tr>
                        </tfoot>
                 </table>

        
        <?php
    }

// if($ch=="paybybranch"){
// 	$data=array("inventory_id"=>$_REQUEST["inventory_id"],
// 				"idno"=>$_REQUEST["idno"],
// 				"price"=>$_REQUEST["price"],
// 				"num_item"=>$_REQUEST["itom"],
// 			    "total_amount"=>$_REQUEST["Grossamount"],
// 				"school_id"=>$_SESSION["admin"]["school_id"],
// 				"added_by"=>1,
// 				"session"=>$_SESSION["admin"]["session"]
// 				);
// 				print_r($data);
// 	$flgIn=$db->insertAry("inventory_receipt",$data);
// 	echo $db->getErMsg();

// }
?>