<?php
session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];    
}else{
header('Location:../index.php');
exit;     
}
$dtnow = date("Y-m-d h:i:s", time());
?>



<?php 
if(isset($_POST['addsitem'])){ 
$item=intval($_POST['item']);   
?>
<div class="col-md-12 popup_details_div addservice">
<div class="row">
<div id="server-results"><!-- For server results --></div>    
</div>
<div class="row">
<div class="col-md-12">
<div class="col-md-1"></div>
<div class="col-md-10">
<?php if($item <=0){ ?>     
<div class="form-group">
<label>Name</label>
<input type="text" maxlength="35" value="" name="name" id="name" class="form-control" placeholder="e.g. Mother Board Repair"/>
</div>    
<div class="form-group">
<label>Category</label>    
<select class="form-control select2" name="catid" id="catid" onchange="getAllSubgroup(this.value)">
<option value="">-Select-</option>
<?php									
$querys=mysqli_query($con,"SELECT * FROM tbl_category ORDER BY id 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>
<input type="hidden" name="addseritem" readonly />     
</div>
<div class="form-group">
<label>Sub-Category</label>        
<select class="form-control select2" name="scatid" id="scatid">
<option value="">-Select-</option>
    
</select>    
</div>       
<div class="form-group">
<label>Cost</label>    
<input type="text" maxlength="10" class="form-control" name="cost" id="cost"  onkeypress="return isNumberKey(event)" placeholder="e.g. 500" autocomplete="off">    
</div>
<div class="form-group">
<label>Price</label>    
<input type="text" maxlength="10" class="form-control" name="price" id="price"  onkeypress="return isNumberKey(event)" placeholder="e.g. 500" autocomplete="off">    
</div>
<div class="form-group">
<label>Description</label>
<textarea class="form-control" maxlength="250" rows="5" name="description" placeholder="Description"></textarea>
</div>
<div class="row">
<div class="col-md-4">    
<div class="form-group">
<label>Code</label>    
<input type="text" name="code" maxlength="45" value="<?php echo get_genid('SE','ABA');?>" id="code" class="form-control" placeholder="e.g. Code" readonly/>    
</div>
</div>    
<div class="col-md-8">
<div class="form-group">
<label>Status</label>
<select class="form-control select2" name="status" id="status">
<option value="1">Active</option>
<option value="0">De-Active</option>    
</select>
</div>
</div>     
</div>
<?php 
}else{ 
$sql="SELECT * FROM tbl_seritem WHERE id='".$item."' ORDER BY id ASC LIMIT 1";    
$admin=mysqli_query($con,$sql) or die(mysqli_error($con));    
$adm=mysqli_fetch_array($admin);    
?>
<div class="form-group">
<label>Name</label>
<input type="text" maxlength="35" value="<?php echo $adm['name'];?>" name="name" id="name" class="form-control" placeholder="e.g. Mother Board Repair"/>
<input type="hidden" maxlength="11" value="<?php echo $adm['id'];?>" class="form-control" name="itmid" autocomplete="off" readonly>    
</div>    
<div class="form-group">
<label>Category</label>    
<select class="form-control select2" name="catid" id="catid" onchange="getAllSubgroup(this.value)">
<option value="">-Select-</option>
<?php									
$querys=mysqli_query($con,"SELECT * FROM tbl_category ORDER BY id ASC")or die(mysqli_error($con));
while ($rows=mysqli_fetch_array($querys)){
?>
<?php if($rows['id']==$adm['catid']){ ?>
<option selected value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
<?php }else{?>    
<option value="<?php echo $rows['id'];?>"><?php echo $rows['name'];?></option>
<?php } ?>
<?php } ?>    
</select>
<input type="hidden" name="editseritem" readonly />     
</div>
<div class="form-group">
<label>Sub-Category</label>        
<select class="form-control select2" name="scatid" id="scatid">
<option value="">-Select-</option>
<?php if($adm['catid']!=''){?>
<?php
$sql="SELECT * FROM tbl_subcat WHERE catid='".$adm['catid']."' ORDER BY id ASC";           
$querysc=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($rowsc=mysqli_fetch_array($querysc)){
?>
<?php if($rowsc['id']==$adm['scatid']){ ?>
<option selected value="<?php echo $rowsc['id'];?>"><?php echo $rowsc['name'];?></option>
<?php }else{?>
<option value="<?php echo $rowsc['id'];?>"><?php echo $rowsc['name'];?></option>
<?php } ?>
<?php } ?>    
<?php } ?>     
</select>    
</div>       
<div class="form-group">
<label>Cost</label>    
<input type="text" maxlength="10" class="form-control" value="<?php echo $adm['cost'];?>" name="cost" id="cost"  onkeypress="return isNumberKey(event)" placeholder="e.g. 500" autocomplete="off">    
</div>
<div class="form-group">
<label>Price</label>    
<input type="text" maxlength="10" class="form-control" value="<?php echo $adm['price'];?>" name="price" id="price"  onkeypress="return isNumberKey(event)" placeholder="e.g. 500" autocomplete="off">    
</div>
<div class="form-group">
<label>Description</label>
<textarea class="form-control" maxlength="250" rows="5" name="description" placeholder="Description"><?php echo $adm['description'];?></textarea>
</div>
<div class="row">
<div class="col-md-4">    
<div class="form-group">
<label>Code</label>    
<input type="text" name="code" maxlength="45" value="<?php echo $adm['code'];?>" id="code" class="form-control" placeholder="e.g. Code" readonly/>    
</div>
</div>    
<div class="col-md-8">
<div class="form-group">
<label>Status</label>
<select class="form-control select2" name="status" id="status">
<option <?php if($adm['status']==1){echo 'selected';}?> value="1">Active</option>
<option <?php if($adm['status']==0){echo 'selected';}?> value="0">De-Active</option>    
</select>
</div>
</div>     
</div>    
<?php } ?>   
</div>    
<div class="col-md-1"></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="additem" class="btn btn-flat bg-purple btn-sm " value="<?php if($item <=0){echo 'Save';}else{echo 'Update';} ?>"/>
</div> 
</div>
<script type="text/javascript">
$(".select2").select2();
    
function getAllSubgroup(id){
$.ajax({
url: 'axe_getsub.php',
type: 'post',
data: {subcat : id},
success:function(data) {
$('#scatid').html(data);
}
});
};
    
function chek_error(){
var result = true;
var name = $('#name').val();
var cost = parseFloat($('#cost').val()); 
var price = parseFloat($('#price').val()); 

$('.LV_invalid_field').removeClass('LV_invalid_field');     
$('.LV_validation_message').remove();     

if(name.length<1){
$('#name').addClass('LV_invalid_field');   
$('#name').after("<span class='LV_validation_message LV_invalid'>Can't be empty!</span>").addClass('has-error'); 
result=false;
}else{
$('#name').removeClass('LV_invalid_field');
result=true;    
}    

if(isNaN(cost) != false || cost < 0){
$('#cost').addClass('LV_invalid_field');   
$('#cost').after("<span class='LV_validation_message LV_invalid'>Enter Valid Cost!</span>").addClass('has-error');
result=false;    
}else{
$('#cost').removeClass('LV_invalid_field');
result=true;     
}    

if(isNaN(price) != false || price < 0){
$('#price').addClass('LV_invalid_field');   
$('#price').after("<span class='LV_validation_message LV_invalid'>Enter Valid Price!</span>").addClass('has-error');
result=false;    
}else{
$('#price').removeClass('LV_invalid_field');
result=true;     
}    

if(cost.length<0 || cost.length<0 || !result){
return false;    
}else{
return true;     
}        
}
    
$(document).on('blur', '#name, #cost, #price', function() {
chek_error();    
});    
</script> 

<?php }
if(isset($_POST['addseritem'])){
$name = ucwords(remove_junk(escape($_POST['name'])));
$code = get_genid('SE','ABA');
$catid = remove_junk(escape($_POST['catid']));
if($catid==''){$catid='NULL';}else{$catid="'".$catid."'";}
$scatid = remove_junk(escape($_POST['scatid']));
if($scatid==''){$scatid='NULL';}else{$scatid="'".$scatid."'";}
$cost = remove_junk(escape($_POST['cost']));
$price = remove_junk(escape($_POST['price']));
$status = remove_junk(escape($_POST['status']));   
$description = remove_junk(escape($_POST['description'])); 

if(isset($_POST['name'])){
$sql="SELECT * FROM tbl_seritem WHERE name = '$name'";           
$ducode = mysqli_query($con,$sql);
}
    
if($ducode->num_rows > 0) {
echo json_encode(array(
'status' => 'error',
'message'=> 'Sorry! Service Item Already Exists!!'
));
return;
exit;
}else{ 
$sql="INSERT INTO tbl_seritem (code,name,description,catid,scatid,cost,price,status,brid,uid,date) VALUES ('$code','$name','$description',$catid,$scatid,'$cost','$price','$status','$brid','$aid','$dtnow')";
$result = mysqli_query($con,$sql) or die(mysqli_error($con));
$pid=$con->insert_id;    
$efid=mysqli_affected_rows($con);    
if($efid>0){    
$act =remove_junk(escape('Service name: '.$name));    
write_activity($aid,'SEI','New service has been created',$act);    
echo json_encode(array(
'status' => 'success',
'message'=> 'Data Save Successfully!!'   
));
exit;
}else{
echo json_encode(array(
'status' => 'error',
'message'=> 'Sorry! Data Fail to Saved!!'
));    
} 
}   
}

if(isset($_POST['editseritem'])){
$name = ucwords(remove_junk(escape($_POST['name'])));
$itemid = remove_junk(escape($_POST['itmid']));;
$catid = remove_junk(escape($_POST['catid']));
if($catid==''){$catid='NULL';}else{$catid="'".$catid."'";}
$scatid = remove_junk(escape($_POST['scatid']));
if($scatid==''){$scatid='NULL';}else{$scatid="'".$scatid."'";}
$cost = remove_junk(escape($_POST['cost']));
$price = remove_junk(escape($_POST['price']));
$status = remove_junk(escape($_POST['status']));   
$description = remove_junk(escape($_POST['description'])); 

if(isset($_POST['name'])){
$sql="SELECT * FROM tbl_seritem WHERE name = '$name' AND id!='$itemid'";           
$ducode = mysqli_query($con,$sql);
}
    
if($ducode->num_rows > 0) {
echo json_encode(array(
'status' => 'error',
'message'=> 'Sorry! Service Item Already Exists!!'
));
return;
exit;
}else{ 
$sql="UPDATE tbl_seritem SET name='$name',description='$description',catid=$catid,scatid=$scatid,cost='$cost',price='$price',status='$status' WHERE id='$itemid'";
$result = mysqli_query($con,$sql) or die(mysqli_error($con));
$pid=$con->insert_id;    
$efid=mysqli_affected_rows($con);    
if($efid>0){    
$act =remove_junk(escape('Service name: '.$name));    
write_activity($aid,'SEI','Service has been Updated',$act);    
echo json_encode(array(
'status' => 'success',
'message'=> 'Data Update Successfully!!'   
));
exit;
}else{
echo json_encode(array(
'status' => 'error',
'message'=> 'Sorry! Data Fail to Update!!'
));    
} 
}   
}

function check_deseritem($id){
$flage=0;
global $con;
$sql="SELECT * FROM tbl_sersalesde WHERE sid='$id'";    
$query=mysqli_query($con,$sql)or die(mysqli_error($con));
if($query->num_rows > 0) {
$flage=1;    
}else{
$flage=0;    
}

if($flage>0){
return $flage;
exit;    
}    
return $flage;    
}

if(isset($_POST['delseritem'])){
$id=intval($_POST['item']);
    
if(check_deseritem($id)){
echo json_encode(array(
'status' => 'error',
'message'=> 'Sorry! Service Item Depend On Other Table!!'
));
exit;
return;    
}

$name=get_fild_data('tbl_seritem',$id,'name');
$sql="DELETE FROM tbl_seritem WHERE id='$id'";
$fdel=mysqli_query($con,$sql)or die(mysqli_error($con));    
$efid=mysqli_affected_rows($con);
if($efid>0){
$act =remove_junk(escape('Service item: '.$name));    
write_activity($aid,'SEI','Service item has been deleted',$act);        
echo json_encode(array(
'status' => 'success',
'message'=> 'Data Deleted Successfully!!'   
));
exit;
}else{
echo json_encode(array(
'status' => 'error',
'message'=> 'Sorry! Data Fail to Delete!!'
));    
}       
}
?> 

<?php if(isset($_POST['viewitem'])){
$sql="SELECT * FROM tbl_seritem ORDER BY name ASC";    
$query=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($row=mysqli_fetch_array($query)){
$cat='';    
if($row['catid']!=''){
$cat.='<strong>'.get_fild_data('tbl_category',$row['catid'],'name').'</strong>';    
}
if($row['scatid']!=''){
$cat.='<br>'.get_fild_data('tbl_subcat',$row['scatid'],'name');    
}    
?>
<tr>
<td class="text-center"><?php echo count_id();?></td>
<td><?php echo $row['name'];?></td>      
<td><?php echo $row['code'];?></td>
<td><?php echo $cat;?></td>   
<td><?php echo numtolocal($row['cost'],get_fild_data('tbl_currency','1','symbol'));?></td>
<td><?php echo numtolocal($row['price'],get_fild_data('tbl_currency','1','symbol'));?></td>
<td><?php echo $row['description'];?></td>    
<td><?php if($row['status']>0){echo 'Active';}else{echo 'De-Active';}?></td>    
<td nowrap="">
<a class="btn btn-flat bg-purple details-invoice" href="#" id="ser_<?php echo $row['id']; ?>"><i class="fa fa-edit cat-child"></i></a>    
<a class="btn btn-flat bg-purple" href="#" onclick="remove_item('DL_<?php echo $row['id'];?>')"><i class="fa fa-trash"></i></a>
<form action="sel_sreturncteate.php" id="RE_<?php echo $row['id'];?>" method="post" >
<input type="hidden" name="selret" value="<?php echo $row['id']; ?>" />
</form>
<form action="sel_sinvlist.php" id="DL_<?php echo $row['id'];?>" method="post" >
<input type="hidden" name="delsel" value="<?php echo $row['id']; ?>" />
</form>
</td>    
</tr>    
<?php }} ?>

<?php 
if(isset($_POST['viewitemhis'])){
if($_SESSION['access']=='1'){echo read_activity($aid,'SEI','A');}else{echo read_activity($aid,'SEI','U');}
}?>