<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
include ('../inc/repfunctions.php');
include ('../inc/cartfunctions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
get_pagesecurity('3','creates','R');     
$_SESSION['cuPages']='req_reqcreate.php';   
$cuPage='req_reqcreate.php';    
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];    
}else{
header('Location:../index.php');
exit;    
}
$mhead='requisition';
$menuh='Requisition';
$phead='reqcre';
$page='Requisition create';
$dtnow = date("Y-m-d h:i:s", time());
?>

<?php 
if(isset($_POST['save_requisition'])){
	//if(!isset($_SESSION['axes_expdata'])){
    //save_msg('w','No Expen data found!!');
	//echo "<script>window.location='dai_expensescreate.php'</script>";
    //exit;    
    //}
    
	if(!isset($_SESSION['axes_reqitem'])){
    save_msg('w','No Requisition item found!!');
	echo "<script>window.location='req_reqcreate.php'</script>";
    exit;    
    }
    
    $reqno = gen_newinvno('tbl_requisition','REQ');
    $apdate = remove_junk(escape($_POST['apdate']));
    $prjid = remove_junk(escape($_POST['prjid']));
    $stfid = remove_junk(escape($_POST['stfid']));
    $epjid = remove_junk(escape($_POST['prjid']));
    if($prjid==''){$prjid='NULL'; $prjno='NULL';}else{$prjid="'".$prjid."'"; $prjno="'".get_fild_data('tbl_project',$epjid,'prjid')."'";}
    $note = remove_junk(escape($_POST['note']));
    $total = total_reqvalue();
    
    $sql="INSERT INTO tbl_requisition (invno,note,amount,apdate,prjid,stfid,status,brid,uid,date) VALUES ('$reqno','$note','$total','$apdate',$prjid,$stfid,1,'$brid','$aid','$dtnow')";    
	$result = mysqli_query($con,$sql) or die(mysqli_error($con));
    $sid=$con->insert_id;
    $efid=mysqli_affected_rows($con);
    
    if($efid>0){
    if(isset($_SESSION['axes_reqitem'])){
    if(is_array($_SESSION['axes_reqitem'])){
    $max=count($_SESSION['axes_reqitem']);
    for($i=0;$i<$max;$i++){
    $reqid=$_SESSION['axes_reqitem'][$i]['reqid'];
    $amount=$_SESSION['axes_reqitem'][$i]['amount'];  
    $mtype=$_SESSION['axes_reqitem'][$i]['mtype'];     
    $qty=$_SESSION['axes_reqitem'][$i]['qty'];
        
    $sql="INSERT INTO tbl_requisitionde (seid,reqid,qty,amount,mtype) VALUES ('$sid','$reqid','$qty','$amount','$mtype')";
    mysqli_query($con,$sql) or die(mysqli_error($con));
        
        
    }}}
    unset($_SESSION['axes_reqdata']);
    unset($_SESSION['axes_reqitem']);    
    $act =remove_junk(escape('Requisition No: '.$reqno));    
    write_activity($aid,'REQ','Requisition has been Added',$act);    
    save_msg('s','Data Successfully Saved!');
    }else{
    save_msg('w','Data Fail to Saved!');    
    }
    echo "<script>window.location='req_reqcreate.php'</script>";  
	}  
?>    
<?php
include('../layout/head.php');
include('../layout/header.php');
include('../layout/side_nav.php');
?>    
<div class="content-wrapper">
<?php include('../layout/page_head.php'); ?>
<!-- Main content -->
<section class="content">

<div class="row">
<div class="col-md-8">
<div class="box box-solid">
<div class="box-header with-border">
<h3 class="box-title">Add Requisition</h3>
</div>
<div class="box-body">
<?php echo display_msg();?>  
<form action="req_reqcreate.php" onsubmit="return validate()" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<div class="col-md-12 popup_details_div">    
<div class="row">    
<center>
<h3 class="page-title">REQUISITION VOUCHER</h3>
</center>
</div>
<div class="row">
<div class="col-md-4">
    <div class="form-group" >
    <div class="input-group">
        <span class="input-group-addon"><b>Project:</b></span>    
        <select class="form-control select2" name="prjid" id="prjid">
            <option value="">-Select-</option>
            <?php
            $sql="SELECT * FROM tbl_project WHERE status IN ('0','1','3') ORDER BY date DESC";    
            $querys=mysqli_query($con,$sql)or die(mysqli_error($con));
            while ($prj=mysqli_fetch_array($querys)){
            ?>
            <option value="<?php echo $prj['id'];?>"><?php echo $prj['prjid'];?></option>
            <?php } ?>
        </select>    
    </div>   
</div>   
<div class="form-group" >
    <div class="input-group">
        <span class="input-group-addon"><b>Stuff Name:</b></span>    
        <select class="form-control select2" name="stfid" id="stfid">
            <option value="">-Select-</option>
            <?php
            $sql="SELECT * FROM tbl_employe ORDER BY date DESC";    
            $querys=mysqli_query($con,$sql)or die(mysqli_error($con));
            while ($stf=mysqli_fetch_array($querys)){
            ?>
            <option value="<?php echo $stf['id'];?>"><?php echo $stf['name'];?></option>
            <?php } ?>
        </select>    
    </div>   
</div>   
</div> 
<div class="col-md-4"></div>
<div class="col-md-4">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><b>Requisition No:</b></span>
<input type="text" class="form-control" maxlength="15" name="invno" id="invno" value="<?php if(isset($_SESSION['axes_reqdata']['invno'])){$_SESSION['axes_reqdata']['invno']=gen_newinvno('tbl_requisition','REQ');echo $_SESSION['axes_reqdata']['invno'];}else{echo gen_newinvno('tbl_requisition','REQ');}?>" placeholder="e.g. AXE121119101" autocomplete="off">
</div>
</div>
<div class="form-group" >
<div class="input-group">
<span class="input-group-addon"><b>Date:</b></span>
<input type="text" class="form-control datetimepicker" name="apdate" id="apdate" value="<?php if(isset($_SESSION['axes_expdata']['date'])){echo $_SESSION['axes_expdata']['date'];}else{ echo date('Y-m-d');}?>" placeholder="Date:" autocomplete="off" readonly>
</div>
</div> 

</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group" >
<label>Expense/Product Name</label>
<div class="input-group">
<select class="form-control select2" name="reqid" id="reqid">
<option>Select</option>
<optgroup label="Expense Head">
<?php
$sql="SELECT * FROM tbl_acledger WHERE grid IN (8,9) AND sgrid IN (20,21) ORDER BY id ASC";    
$querys=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<option value="EH_<?php echo $rows['id'];?>"><?php echo $rows['code'].' - '.$rows['name'];?></option>
<?php } ?>
</optgroup>
<optgroup label="Contractor">
<?php
$sql="SELECT * FROM tbl_contractor ORDER BY id ASC";    
$querys=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<option value="CO_<?php echo $rows['id'];?>"><?php echo $rows['code'].' - '.$rows['name'];?></option>
<?php } ?>
</optgroup>
<optgroup label="Product">
<?php
$sql="SELECT * FROM tbl_item ORDER BY id ASC";    
$querys=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<option value="PO_<?php echo $rows['id'];?>"><?php echo $rows['code'].' - '.$rows['name'];?></option>
<?php } ?>
</optgroup>
</select>
<span class="input-group-addon"><a id="aexpi"><span class="fa fa-plus"></span></a></span>     
</div>   
</div>
</div>
<div class="col-md-3">
<div class="form-group" >
<label>Qty</label>
<input type="text" maxlength="25" class="form-control" name="qty" id="qty" value='1' onkeypress="return isNumberKey(event)" placeholder="e.g. 10" autocomplete="off">    
</div>    
</div>
<div class="col-md-2">
<div class="form-group">
<label>Amount</label>    
<input type="text" maxlength="6" class="form-control" name="amount" id="amount"  onkeypress="return isNumberKey(event)" placeholder="e.g. 500" autocomplete="off">    
</div> 
</div>
<div class="col-md-2">
<div class="form-group">
    <label>Type</label>    
    <select class="form-control select2" name="mtype" id="mtype">
    <option value="0">-Select-</option>
    <?php
    $sql="SELECT id,name FROM tbl_prjtype ORDER BY date DESC";    
    $quer=mysqli_query($con,$sql)or die(mysqli_error($con));
    while ($typ=mysqli_fetch_array($quer)){
    ?>
    <option value="<?php echo $typ['id'];?>"><?php echo $typ['name'];?></option>
    <?php } ?>
    </select>     
</div> 
</div> 
<div class="col-md-1">
<div class="form-group">
<label>&nbsp;</label>    
<input type="button" id="addreq" class="btn btn-flat bg-red" value="Add"/>    
</div>
</div>    
</div>
<div class="row">
<div class="col-md-12">
<table class="table table-bordered table-striped">
<thead>    
<th style="width:40px; text-align:center">SN</th>
<th>Expenses/Product</th>
<th>Type</th>
<th>Qty</th>
<th>Amount</th>
<th style="width:40px; text-align:center"><a class="empty" style="cursor: pointer;"><i class="fa fa-trash"></i></a></th>    
</thead>
<tbody id="itemdata">

</tbody>
<tfoot id="itemfoot">

</tfoot>    
</table> 
</div>    
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Note</label>
<textarea class="form-control" name="note" id="note" maxlength="250" rows="3" placeholder="e.g. Note"><?php if(isset($_SESSION['axes_expdata']['note'])){echo $_SESSION['axes_expdata']['note'];}?></textarea>
</div>    
</div>   
</div>    
</div>    
    
<div class="clearfix" ></div>
<div class="col-md-12 nopadding widgets_area"></div>    
<div class="row"style="margin-top: 15px" >
<div class="col-md-8"></div>
<div class="col-md-4 text-right" >
<input type="button" id="reqreset" class="btn btn-flat bg-red btn-sm " value="Reset"/>    
<input type="submit" name="save_requisition" id="submit" class="btn btn-flat bg-purple btn-sm" value="Save"/> <a href="dai_expenlist.php" class="btn btn-flat bg-gray  ">Close</a>
</div> 
</div>     
</form>    
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
<div class="col-md-12">
<div class="box box-solid">
<div class="box-header">
<h3 class="box-title">History </h3>
</div>
<!-- /.box-header -->
<div class="box-body" >
<?php if($_SESSION['access']=='1'){echo read_activity($aid,'REQ','A');}else{echo read_activity($aid,'REQ','U');}?>
</div>
</div>
</div>
</div>
</div>
</div>

<?php include('../layout/quick.php');?>    
</section>
<!-- /.main content -->
<?php include('../layout/rside.php'); ?>    
</div>

<?php 
include('../layout/footer.php');
?>
<!-- page script -->
<script type="text/javascript">
$(document).ready(function (){ 
var apdate = new LiveValidation('apdate');
apdate.add(Validate.Presence);
});
    
ReadData();
function ReadData(){
$.ajax({
url: "req_reqview.php",
method: "POST",
data:{ 
body:1
},
success: function(data){
$('#itemdata').html(data);
}
})

$.ajax({
url: "req_reqview.php",
method: "POST",
data:{ 
foot:1
},
success: function(data){
$('#itemfoot').html(data);
}
})    
};    

function ReadFoot(){
$.ajax({
url: "req_reqview.php",
method: "POST",
data:{ 
foot:1
},
success: function(data){
$('#itemfoot').html(data);
}
})	
}
 
$(document).on('blur change', '#apdate', function() {
apdate=$(this).val();
$.ajax({
url: 'axe_cart.php',
method: "POST",
data:{ 
reqdate: apdate
},
dataType: 'json',
success: function(data){
$('#apdate').val(data[0]);
}
});     
});    

$(document).on('blur', '#note', function() {
note = $(this).val();
$.ajax({
url: 'axe_cart.php',
method: "POST",
data:{ 
reqnote: note
},
dataType: 'json',
success: function(data){
$('#note').val(data[0]);
}
});     
});    

$(document).on('click','.empty',function() {
$.ajax({
url: 'axe_cart.php',
method: "POST",
data:{ 
reqdata: 1
},
success: function(data){
ReadData();    
}
});    
});    

$(document).on('click', '.remove', function () {
var id = $(this).attr('id');
$.ajax({
url: 'axe_cart.php',
method: "POST",
data:{ 
reqremove: id
},
success: function(data){
ReadData();
}
});    
});    

$(document).on('click','#reqreset',function() {
$.ajax({
url: 'axe_cart.php',
method: "POST",
data:{ 
reqclear: 1
},
success: function(data){
$("#note").val("");   
ReadData();    
}
});    
});     
    
$(document).on('click', '#addreq', function() {
var reqid = $('#reqid').val();    
var amount = parseFloat($('#amount').val());
var qty = $('#qty').val();    
toastr.options = {'positionClass': 'toast-top-center'};    

if(reqid == '-Select-' || reqid == ''){
toastr.info('Please Select Requisition!');
return;
}    
    
if(isNaN(amount) != false || amount <= 0){
toastr.info('Please Enter Amount!');
return;
}
if(isNaN(qty) != false || qty <= 0){
toastr.info('Please Enter Quantity!');
return;
}

$.ajax({
url: 'axe_cart.php',
type: 'post',
data: {reqid:reqid, amount:amount, qty:qty},
success:function(response){
$("#reqid").val("").trigger("change");    
$('#qty').val("");
$('#amount').val("");    
ReadData();
}
});    
    
});
    
$(document).on('click', '#aexpi', function() {
$.ajax({
url: "axe_additem.php",
method: "POST",
data:{addexpi:1},
success: function(data){
$('#addsitem').html(data);
$('.right-side-add').toggle('slide', { direction: 'right' }, 300);     
}
})       
});
    
$(document).on('click', '#addcash', function() {
$.ajax({
url: "axe_additem.php",
method: "POST",
data:{addcash:1},
success: function(data){
$('#addsitem').html(data);
$('.right-side-add').toggle('slide', { direction: 'right' }, 300);     
}
})       
});    

$(document).on('click', '#closepop', function() {    
if($('.right-side-add').is(':visible')) { $('.right-side-add').toggle('slide', { direction: 'right' }, 300); }
$('#addsitem').html('');    
});    
</script>    
<!-- /page script -->
</html>    