<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
get_pagesecurity('14','views','R');    
$_SESSION['cuPages']='sel_exchangelist.php';   
$cuPage='sel_exchangelist.php';    
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];    
}else{
header('Location:../index.php');
exit;    
}
$mhead='sales';
$menuh='Sales';
$phead='exlist';
$page='Exchange List';

$print='print';
$dtnow = date("Y-m-d h:i:s", time());
?>
<?php
include('../layout/head.php');
include('../layout/header.php');
include('../layout/side_nav.php');
?>
<?php 




function check_desales($invno){
$flage=0;
global $con;
  
$sql="SELECT * FROM tbl_traproduct WHERE invno='$invno'";    
$rquery=mysqli_query($con,$sql)or die(mysqli_error($con));
if($rquery->num_rows > 0) {
$flage=1;    
}else{
$flage=0;    
}

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

return $flage;    
}






if(isset($_POST['delitm'])){
$id=$_POST['delitm'];


$sql="SELECT * FROM tbl_exchange WHERE id='$id' LIMIT 1";
$dsales=mysqli_query($con,$sql) or die(mysqli_error($con));    
$dsel=mysqli_fetch_array($dsales);     

$invno=$dsel['invno'];  



$selno=$dsel['selno']; 

if(check_desales($selno)){
save_msg('w','Exchange Depend on others data!!!Delete first related Sales');
echo "<script>window.location='sel_sinvlist.php'</script>";
return;    
}  


$purno=$dsel['purno']; 

if(check_desales($invno)){
save_msg('w','Exchange Depend on others data!!!Delete first related Sales');
echo "<script>window.location='sel_sinvlist.php'</script>";
return;    
}    



$sql="DELETE FROM tbl_exchange WHERE id='$id'";

$query=mysqli_query($con,$sql)or die(mysqli_error($con));
$efid=mysqli_affected_rows($con);     
if($efid>0){
$act =remove_junk(escape('Exchange Invoice: '.$invno));    
write_activity($aid,'EXC','Exchange has been deleted',$act);        
save_msg('s','Exchange Successfully Deleted!!!');
}else{
save_msg('w','Exchange Fail to Delete!!!');    
}    
echo "<script>window.location='sel_exchangelist.php'</script>";
}
?>
<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">Product List</h3>
</div>
<div class="box-body">
<?php echo display_msg();?>    
<div class="col-md-12 table-responsive">
<table class="table table-bordered table-striped" id="datarec">
<thead>
<tr>
<th style="width:40px;">SN</th>   
<th>Invoice</th>
<th>Name</th>
<th>Old Phone</th>
<th>New Phone</th>    
<th>Date</th>
<th>S.No</th>
<th>P.No</th>
<th style="width:40px; text-align:center;">Action</th>    
</tr>
</thead>    
<tbody>
<?php
$sql="SELECT * FROM tbl_exchange ORDER BY id ASC";    
$query=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($row=mysqli_fetch_array($query)){
$id=$row['id'];
?>
<tr>
<td class="center"><?php echo count_id();?></td>
<td><?php echo $row['invno'];?></td>
<td><?php if($row['cus_id']!=''){echo get_fild_data('tbl_customer',$row['cus_id'],'name');}?></td>
<td><?php if($row['cuspid']!=''){echo get_fild_data('tbl_item',$row['cuspid'],'name');}?></td>
<td><?php if($row['selpid']!=''){echo get_fild_data('tbl_item',$row['selpid'],'name');}?></td>
<td><?php echo $row['apdate'];?></td>
<td><?php echo $row['selno'];?></td>
<td><?php echo $row['purno'];?></td>
<td nowrap="">
    
<a class="btn btn-flat bg-purple details-invoice" href="#" id="inv_<?php echo $row['id']; ?>"><i class="fa fa-eye 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_exchangelist.php" id="DL_<?php echo $row['id'];?>" method="post" >
<input type="hidden" name="delitm" value="<?php echo $row['id']; ?>" />
</form>
</td>    
</tr>    
<?php } ?>    
</tbody>   
</table>
</div>
<div class="clearfix" ></div>  
<div class="row"style="margin-top: 15px" >
<div class="col-md-12 table-responsive">    
<div class="col-md-8"></div>
<div class="col-md-4 text-right" >
<a href="sel_exchangelist.php" class="btn btn-flat bg-purple">Add Exchange</a>
</div>
</div>    
</div>    
</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,'EXC','A');}else{echo read_activity($aid,'EXC','U');}?>
</div>
</div>
</div>
</div>
</div>
</div> 

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


<?php 
include('../layout/footer.php');
?>
<!-- page script -->
<script src='../plugins/chart/Chart.js'></script>
<script type="text/javascript">
$(document).ready(function() {
$('#datarec').DataTable({stateSave: true});
} );
function edit_item(id) {
document.getElementById(id).submit(); 
}
function remove_item(id) {
bootbox.confirm({
title: "Confirm",
message: "Are you sure to delete ?",
buttons: {
confirm: {
label: 'Yes',
className: 'btn-flat bg-purple'
},
cancel: {
label: 'Cancel',
className: 'btn-flat bg-gray'
}
},
callback: function (result) {
if (result == true) {
document.getElementById(id).submit();
}
}
});

}
    
$(".details-invoice").click(function (e) {
    id_arr = $(this).attr('id');
    id = id_arr.split("_");

    $.ajax({
        url: 'sel_exchangeviewlist.php',
        method: "POST",
        data:{ 
        invid: id[1]
        },
        success: function(data){
        $('#listitem').html(data);
        pagelist();    
        }
    });    
    
    $.ajax({
        url: 'sel_exchangeview.php',
        method: "POST",
        data:{ 
        print: id[1]
        },
        success: function(data){
        $('#invhold').html(data);
        sethead();    
        }
    });
    
    $('.right-side').toggle('slide', { direction: 'right' }, 300);    
    
    e.preventDefault();
});

$(document).on('click', '#closepop', function() {
    if($('.right-side').is(':visible')) { $('.right-side').toggle('slide', { direction: 'right' }, 300); }   
});    

$('#listitem').on('click','li', function(){
   $(this).addClass('active').siblings().removeClass('active');
});
    
$(document).on('click', '.invrecv', function() {
    id_arr = $(this).attr('id');
    id = id_arr.split("_");
    var ids = id[1];
    $.ajax({
        url: 'sel_exchangeview.php',
        method: "POST",
        data:{ 
            print: ids
        },
        beforeSend: function() {
            $("#invhold").fadeOut('slow');
        },    
        success: function(data){    
            $('#invhold').html(data);
            $("#invhold").fadeIn('slow');    
            sethead();    
        }
    });     
});    
</script>    
<!-- /page script -->
</html>    