<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
$_SESSION['cuPages']='prr_receivecre.php';   
$cuPage='prr_receivecre.php';    
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];     
}else{
header('Location:../index.php');
exit;    
}
$mhead='repair';
$menuh='Product Repair';
$phead='prrcv';
$page='Product Receive Create';
$print='print';
$dtnow = date("Y-m-d h:i:s", time());
$today = strftime("%Y-%m-%d", time());
?>
<?php 
if(isset($_POST['save_rcv'])){
    $invno = gen_newinvno('tbl_proreceive','PRR');
    $ddate = remove_junk(escape($_POST['ddate']));
    $rdate = remove_junk(escape($_POST['rdate']));
    $cusid = remove_junk(escape($_POST['cusid'])); 
    
    $serial = remove_junk(escape($_POST['serial'])); 
    $pid = remove_junk(escape($_POST['pid']));
    $mgid = remove_junk(escape($_POST['mgid']));
    $enid = remove_junk(escape($_POST['enid']));
    $qcid = remove_junk(escape($_POST['qcid']));     
    $note = remove_junk(escape($_POST['note']));
          
    $sql="INSERT INTO tbl_proreceive(cusid,invno,mgid,enid,qcid,ddate,rdate,note,uid,date) VALUES ('$cusid','$invno','$mgid','$enid','$qcid','$ddate','$rdate','$note','$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_proitem'])){
            if(is_array($_SESSION['axes_proitem'])){
            $max=count($_SESSION['axes_proitem']);
            for($i=0;$i<$max;$i++){       
            $pid=$_SESSION['axes_proitem'][$i]['pid'];
            $serial=$_SESSION['axes_proitem'][$i]['serial'];
            $qty=$_SESSION['axes_proitem'][$i]['qty'];     
            $sql="INSERT INTO tbl_proreceivedetails (proid,pid,serial,qty) VALUES ('$sid','$pid','$serial','$qty')";
            mysqli_query($con,$sql) or die(mysqli_error($con)); 
                
            }}}
            unset($_SESSION['axes_prodata']);
            unset($_SESSION['axes_proitem']);    

    $act =remove_junk(escape('Product Receive: '.$invno));    
    write_activity($aid,'PRR','Product Receive has been Created',$act);   
    save_msg('s','Data Successfully Saved!');
    }else{
    save_msg('w','Data Fail to Saved!');   
    }
    echo "<script>window.location='prr_receivelist.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 New Receive</h3>
</div>
<div class="box-body">
<?php echo display_msg();?>  
<form action="prr_recievecre.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">
  <div class="col-md-6">   
  <div class="form-group">
    <label>Customer</label>
    <select class="form-control select2" name="cusid" id="cusid">
      <option value="">-Select-</option>
      <?php									
      $querys=mysqli_query($con,"SELECT * FROM tbl_customer ORDER BY name ASC")or die(mysqli_error($con));
      while ($rows=mysqli_fetch_array($querys)){
      ?>
        <option value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>    
      <?php } ?>
    </select>    
  </div>
  </div>
</div>    
   

<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Manager</label>
<select class="form-control select2" name="mgid" id="mgid">
      <option value="">-Select-</option>
      <?php									
      $querys=mysqli_query($con,"SELECT * FROM tbl_employe ORDER BY name ASC")or die(mysqli_error($con));
      while ($rows=mysqli_fetch_array($querys)){
      ?>
        <option value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>    
      <?php } ?>
</select> 
</div>    
</div>
<div class="col-md-6">
<div class="form-group">
<label>Engineer</label>
<select class="form-control select2" name="enid" id="enid">
      <option value="">-Select-</option>
      <?php									
      $querys=mysqli_query($con,"SELECT * FROM tbl_employe ORDER BY name ASC")or die(mysqli_error($con));
      while ($rows=mysqli_fetch_array($querys)){
      ?>
        <option value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
      <?php } ?>
</select> 
</div>    
</div>    
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>QC</label>
<select class="form-control select2" name="qcid" id="qcid">
      <option value="">-Select-</option>
      <?php									
      $querys=mysqli_query($con,"SELECT * FROM tbl_employe ORDER BY name ASC")or die(mysqli_error($con));
      while ($rows=mysqli_fetch_array($querys)){
      ?>
        <option value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>   
      <?php } ?>
</select> 
</div>    
</div> 

<div class="col-md-4">   
<div class="form-group">
<label>Estimate Delivery</label>
<input type="text" class="form-control datetimepicker" name="ddate" id="ddate" value="<?php echo date('Y-m-d', strtotime('+7 days',strtotime($today)));?>" placeholder="Estimate Delivery" autocomplete="off" readonly>
</div>
</div>     
<div class="col-md-4">
<div class="form-group">
<label>Received Date</label>
<input type="text" class="form-control datetimepicker" name="rdate" id="rdate" value="<?php echo $today;?>" placeholder="Received Date" autocomplete="off" readonly>
</div>    
</div>  
</div>

  

<div class="row">

<div class="col-md-4">
<div class="form-group">
<label>Product</label>
<select class="form-control select2" name="pid" id="pid">
<option value="">-Select-</option>
<?php									
$queryc=mysqli_query($con,"SELECT * FROM tbl_item WHERE status='1' ORDER BY id ASC")or die(mysqli_error($con));
while ($row=mysqli_fetch_array($queryc)){
?>
<option value="<?php echo $row['id'];?>"><?php echo $row['code'].' - '.$row['name'];?></option>
<?php } ?>
</select>    
</div>
</div>

<div class="col-md-4">
<div class="form-group">
<label>Serial</label>
<input type="text" maxlength="64" class="form-control" name="serial"  id="serial" value="" placeholder="e.g 788455SDS">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Qty</label>
<input type="text" name="qty" id="qty" maxlength="12" value="" onkeypress="return isNumberKey(event)" class="form-control" placeholder="e.g 2,10,00,000"  />
</div>    
</div>
<div class="col-md-1">
<div class="form-group">
<label>&nbsp;</label>    
<input type="button" id="addpro" 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>Product</th>
<th>Serial</th>
<th>Qty</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>Receive Note</label>
<textarea class="form-control" name="note" id="note" maxlength="250" rows="5" placeholder="Received Note"></textarea> 
<input type="hidden" maxlength="15" class="form-control" name="rcvsave" value="1" readonly>       
</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="submit" name="save_rcv" id="submit" class="btn btn-flat bg-purple btn-sm " value="Save"/> <a href="prj_projectlist.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['utype']=='1'){echo read_activity($aid,'PRJ','A');}else{echo read_activity($aid,'PRJ','U');}?>
</div>
</div>
</div>
</div>
</div>
</div>
    
    
<?php include('../layout/quick.php');?>    
</section>
<!-- /.main content -->    
</div>
<?php
include('../layout/footer.php');
?>
<!-- page script -->
<script type="text/javascript">
$(document).ready(function (){ 
var cusid = new LiveValidation('cusid');
cusid.add(Validate.Presence);
var mgid = new LiveValidation('mgid');
mgid.add(Validate.Presence);    
var enid = new LiveValidation('enid');
enid.add(Validate.Presence);  
});
    

</script>    
<!-- page script -->
<script type="text/javascript">
$(document).ready(function (){ 
var rdate = new LiveValidation('rdate');
rdate.add(Validate.Presence);
});
    

    
ReadData();
function ReadData(){
$.ajax({
url: "prr_recieveview.php",
method: "POST",
data:{ 
body:1
},
success: function(data){
$('#itemdata').html(data);
}
})

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

function ReadFoot(){
$.ajax({
url: "prr_recieveview.php",
method: "POST",
data:{ 
foot:1
},
success: function(data){
$('#itemfoot').html(data);
}
})	
}
    
$(document).on('click', '#addpro', function(e) {
pid = $('#pid').val();

serial = $('#serial').val();
qty = $('#qty').val();
toastr.options = {'positionClass': 'toast-top-center'};

if(pid == '-Select-' || pid == ''){
toastr.info('Please Select Product!');
return;
};    
     

$.ajax({
url: 'axe_cart.php',
type: 'post',
data: {getprorecive:1,pid:pid,serial:serial,qty:qty},
success:function(data) {
$("#pid").val("").trigger("change");    
$('#serial').val("");
$('#qty').val("");
ReadData();
}
});    
    
e.preventDefault();     
});
    
   
  

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

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

    
</script>    
<!-- /page script -->
</html>    