<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
$_SESSION['cuPages']='pur_porderlist.php';   
$cuPage='pur_porderlist.php';    
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];
$uty=$_SESSION['utype'];    
}else{
header('Location:../index.php');
exit;    
}
$mhead='purchase';
if(get_fild_data('tbl_setting','1','sval')==0){$menuh = 'Purchase';}else{$menuh = 'ক্রয় করুন';}
$phead='porlist';
if(get_fild_data('tbl_setting','1','sval')==0){$page = 'Purchase Order';}else{$page = 'ক্রয় আদেশ';}
$print='print';
$dtnow = date("Y-m-d h:i:s", time());
$today = strftime("%Y-%m-%d", time());
?>
<?php

if(isset($_POST['tfdate'])){
$fdate=$_POST['tfdate'];	
}else{
$fdate=date('Y-m-d', strtotime('-15 days', time()));	
}
if(isset($_POST['ttdate'])){
$tdate=$_POST['ttdate'];	
}else{
$tdate=$today;	
}

function check_porinvdel($invno){
global $con;
$flage=0;

$sql="SELECT * FROM tbl_purchase WHERE refinv='$invno'";    
$query=mysqli_query($con,$sql)or die(mysqli_error($con));
if($query->num_rows > 0) {
$flage=1;    
}else{
$flage=0;    
}    
return $flage;   
}

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

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

$invno=$dsel['invno'];    
    
$sql="UPDATE tbl_purorder SET ap='Y' 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('Order invoice: '.$invno));
$bact =remove_junk(escape('ক্রয় আদেশ নং: '.$invno));    
write_activity($aid,'POR','Purchase Order has been approved',$act,'ক্রয় আদেশ অনুমোদন করেছেন',$bact);        
save_msg('s','Purchase Order Successfully Approved!!!');
}else{
save_msg('w','Purchase Order Fail to Approved!!!');    
}
echo "<script>window.location='pur_porderlist.php'</script>";        
}

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

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

$invno=$dsel['invno'];    
    
$sql="UPDATE tbl_purorder SET ap='D' 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('Order invoice: '.$invno));
$bact =remove_junk(escape('ক্রয় আদেশ নং: '.$invno));    
write_activity($aid,'POR','Purchase Order has been deny',$act,'ক্রয় আদেশ প্রত্যাখ্যান করেছেন',$bact);        
save_msg('s','Purchase Order Successfully Denyed!!!');
}else{
save_msg('w','Purchase Order Fail to Deny!!!');    
}
echo "<script>window.location='pur_porderlist.php'</script>";        
}

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

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

$invno=$dsel['invno'];    
    
if(check_porinvdel($invno)){
save_msg('w','Purchase Order Depend on others data!!!');
echo "<script>window.location='pur_porderlist.php'</script>";
return;    
}
    
$sql="DELETE FROM tbl_purorderde WHERE invno='$invno'";    
mysqli_query($con,$sql)or die(mysqli_error($con));
    
$sql="DELETE FROM tbl_purorder 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('Order invoice: '.$invno));
$bact =remove_junk(escape('ক্রয় আদেশ নং: '.$invno));    
write_activity($aid,'POR','Purchase Order has been deleted',$act,'ক্রয় আদেশ মুছে ফেলছেন',$bact);        
save_msg('s','Purchase Order Successfully Deleted!!!');
}else{
save_msg('w','Purchase Order Fail to Delete!!!');    
}
echo "<script>window.location='pur_porderlist.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-9">
<div class="box box-solid">
<div class="box-header with-border">
<h3 class="box-title"><?php if(get_fild_data('tbl_setting','1','sval')==0){echo 'Purchase Record';}else{echo 'ক্রয় আদেশ তালিকা';}?></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; text-align:center;">SN</th>   
<th><?php if(get_fild_data('tbl_setting','1','sval')==0){echo 'Date';}else{echo 'তারিখ';}?></th>
<th><?php if(get_fild_data('tbl_setting','1','sval')==0){echo 'Supplier';}else{echo 'সরবরাহকারী';}?></th>
<th><?php if(get_fild_data('tbl_setting','1','sval')==0){echo 'Order No';}else{echo 'আদেশ নং';}?></th>
<th><?php if(get_fild_data('tbl_setting','1','sval')==0){echo 'Total';}else{echo 'মোট';}?></th>
<th style="text-align:center;"><?php if(get_fild_data('tbl_setting','1','sval')==0){echo 'isInvoice';}else{echo 'ক্রয় হয়েছে';}?></th>    
<th><?php if(get_fild_data('tbl_setting','1','sval')==0){echo 'Note';}else{echo 'মন্তব্য';}?></th> 
<th style="width:40px; text-align:center;"><?php if(get_fild_data('tbl_setting','1','sval')==0){echo 'Approve';}else{echo 'অনুমোদিত';}?></th>     
<th style="width:40px; text-align:center;"><?php if(get_fild_data('tbl_setting','1','sval')==0){echo 'Action';}else{echo 'অ্যাকশন';}?></th>    
</tr>
</thead>    
<tbody>
<?php
$sql="SELECT * FROM tbl_purorder WHERE apdate BETWEEN '$fdate' AND '$tdate' AND brid='$brid' ORDER BY apdate DESC,id DESC";    
$query=mysqli_query($con,$sql)or die(mysqli_error($con));
while ($row=mysqli_fetch_array($query)){
$invno=$row['invno'];   
?>
<tr>
<td class="text-center"><?php echo count_id();?></td>
<td><?php echo date("d M Y", strtotime($row['apdate']));?></td>      
<td>
<?php 
if($row['type']=='SU'){
echo show_addres(get_fild_data('tbl_supplier',$row['supid'],'name'),get_fild_data('tbl_supplier',$row['supid'],'cnumber'),get_fild_data('tbl_supplier',$row['supid'],'cemail'),'',get_fild_data('tbl_supplier',$row['supid'],'code'),$row['type'].'_'.$row['supid']);
}else{
echo show_addres(get_fild_data('tbl_customer',$row['supid'],'name'),get_fild_data('tbl_customer',$row['supid'],'cnumber'),get_fild_data('tbl_customer',$row['supid'],'cemail'),'',get_fild_data('tbl_customer',$row['supid'],'code'),$row['type'].'_'.$row['supid']);
}
?></td>
<td><?php echo $row['invno'];?></td>    
<td><?php echo numtolocal($row['total'],get_fild_data('tbl_currency','1','symbol'));?></td>
<td style="text-align:center;"><?php if(strlen(get_fild_data('tbl_purchase','','refinv',"refinv='$invno'"))>2){if(get_fild_data('tbl_setting','1','sval')==0){echo 'Yes';}else{echo 'হ্যাঁ';}}else{if(get_fild_data('tbl_setting','1','sval')==0){echo 'No';}else{echo 'না';}}; ?></td>
<td><?php echo $row['note'];?></td>
<td style="text-align:center;"><?php if($row['ap']=='Y'){if(get_fild_data('tbl_setting','1','sval')==0){echo 'Yes';}else{echo 'হ্যাঁ';}}elseif($row['ap']=='D'){if(get_fild_data('tbl_setting','1','sval')==0){echo 'Deny';}else{echo 'প্রত্যাখ্যান';}}else{if($today<=$row['expdate']){if(get_fild_data('tbl_setting','1','sval')==0){echo 'No';}else{echo 'না';}}else{if(get_fild_data('tbl_setting','1','sval')==0){echo 'Expired';}else{echo 'মেয়াদ উত্তীর্ণ';}}} ?></td>    
<td nowrap="">
<a class="btn btn-flat bg-purple details-invoice" href="#" id="inv_<?php echo $row['id'].'_'.$row['supid']; ?>"><i class="fa fa-eye cat-child"></i></a>
<?php  if($row['ap']=='Y'){ ?>
<?php if(strlen(get_fild_data('tbl_purchase','','refinv',"refinv='$invno'"))>2){}else{?>       
<a class="btn btn-flat bg-purple" href="#" onclick="take_action('CN_<?php echo $row['id'];?>')"><i class="fa fa-exchange"></i></a>
<?php }} ?>    
<?php if(!check_porinvdel($invno)){ ?>             
<a class="btn btn-flat bg-purple" href="#" onclick="remove_item('DL_<?php echo $row['id'];?>')"><i class="fa fa-trash"></i></a>
<?php } ?>    
<form action="pur_pinvcteate.php" id="CN_<?php echo $row['id'];?>" method="post" >
<input type="hidden" name="conpor" value="<?php echo $row['id']; ?>" />
</form>    
<form action="pur_porderlist.php" id="AP_<?php echo $row['id'];?>" method="post" >
<input type="hidden" name="appord" value="<?php echo $row['id']; ?>" />
</form>
<form action="pur_porderlist.php" id="DN_<?php echo $row['id'];?>" method="post" >
<input type="hidden" name="appden" value="<?php echo $row['id']; ?>" />
</form>    
<form action="pur_porderlist.php" id="DL_<?php echo $row['id'];?>" method="post" >
<input type="hidden" name="delord" 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="pur_pordercteate.php" class="btn btn-flat bg-purple"><?php if(get_fild_data('tbl_setting','1','sval')==0){echo 'Create Order';}else{echo 'নতুন ক্রয় আদেশ';}?></a>
</div>
</div>    
</div>    
</div>
</div>
</div>
<div class="col-md-3">
<div class="row">
<div class="col-md-12">
<div class="box box-solid">
<div class="box-header">
<!-- tools box -->
<div class="pull-right box-tools">
<a class=" pull-right" data-widget="collapse" style="margin-right: 5px;">
<i class="fa fa-minus"></i></a>
</div>
<!-- /. tools -->
<i class="fa fa-filter" aria-hidden="true"></i>    
<h3 class="box-title">Date Range Filter</h3>
</div>
<!-- /.box-header -->
<div class="box-body" >

<form action="#" 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-12">
<div class="col-md-1"></div>
<div class="col-md-10">    

<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Date From</label>
<div class="input-group date datetimepicker">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
<input type="text" name="tfdate" value="<?php if(isset($_POST['tfdate'])){echo $_POST['tfdate']; }else{echo date('Y-m-d', strtotime('-15 days', time()));} ?>"  class="form-control" placeholder="Date From" readonly="true">
</div>
</div>        
</div>
<div class="col-md-6">
<div class="form-group" >
<label>Date To</label>
<div class="input-group date datetimepicker">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
<input type="text" name="ttdate" value="<?php if(isset($_POST['ttdate'])){echo $_POST['ttdate']; }else{echo $today;} ?>"  class="form-control" placeholder="Date To" readonly="true">
</div>    
</div>    
</div>    
</div>     
    
</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="submit" name="date_filter" id="submit" class="btn btn-flat bg-purple btn-sm " value="Submit"/>
</div> 
</div>     
</form>    
    
</div>
</div>
</div>

</div>    
<div class="row">
<div class="col-md-12">
<div class="box box-solid">
<div class="box-header">
<h3 class="box-title"><?php if(get_fild_data('tbl_setting','1','sval')==0){echo 'History';}else{echo 'ইতিহাস';}?></h3>
</div>
<!-- /.box-header -->
<div class="box-body" >
<?php if($_SESSION['access']=='1'){echo read_activity($aid,'POR','A');}else{echo read_activity($aid,'POR','U');}?>
</div>
</div>
</div>
</div>
</div>
</div>
 
<?php include('../layout/quick.php');?>    
</section>
<!-- /.main content -->
<?php include('../layout/print.php'); ?>
<?php include('../layout/details.php'); ?>       
</div>
<?php include('../layout/footer.php'); ?>
<!-- page script -->
<script type="text/javascript">
$(document).ready(function() {
$('#datarec').DataTable({stateSave: true});
});
    
function take_action(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: 'pur_vieworlist.php',
method: "POST",
data:{ 
invid: id[1],supid: id[2]
},
success: function(data){
$('#listitem').html(data);
pagelist();    
}
});    
    
$.ajax({
url: 'pur_vieworinv.php',
method: "POST",
data:{ 
print: id[1]
},
success: function(data){
$('#invhold').html(data);
sethead();    
}
});
    
$('.right-side').toggle('slide', { direction: 'right' }, 300);    

e.preventDefault();
});

$(".detailsrec").click(function (e) {
id_arr = $(this).attr('id');
id = id_arr.split("_");
if(id[1]==0)return;

$.ajax({
url: 'axe_details.php',
method: "POST",
data:{ 
cusdet: 1,cusid: id[1],type:id[0]
},
success: function(data){
$('#profile').html(data);    
}
});    
    
$.ajax({
url: 'axe_details.php',
method: "POST",
data:{ 
tradet: 1,cusid: id[1],type:id[0]
},
success: function(data){
$('#details').html(data);   
}
});    
    
$('.right-side-details').toggle('slide', { direction: 'right' }, 300);   
e.preventDefault();    
});    
    
$(document).on('click', '#closedet', function() {
$('#profile').html('');
$('#details').html('');    
if($('.right-side-details').is(':visible')) {
$('.right-side-details').toggle('slide', { direction: 'right' }, 300);
}    
});    
    
$(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', '.invpiv', function() {
id_arr = $(this).attr('id');
id = id_arr.split("_");
var ids = id[1];
$.ajax({
url: 'pur_vieworinv.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>    