<?php
//user.php
include('db_con.php');
  include('function.php');
if (isset($_SESSION['user_id'])) {
    $user_type = $_SESSION['user_type'];
    $user_id = $_SESSION['user_id'];
    $user_name = $_SESSION['user_name'];
    $branch_id = $_SESSION['branch_id'];
    $branch_name = $_SESSION['branch_name'];
    $store_id = $_SESSION['store_id'];
    $store_name = $_SESSION['store_name'];
}else{
  header('Location: login');
  die();
}
//if(!isset($_SESSION["type"]))
//{
//  header('location:login');
//}
//if($_SESSION["type"] != 'master')
//{
//  header("location:index.php");
//}
$sql = "SELECT doc_status FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'purchase'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $purchase = $statement->fetchColumn();
            if($purchase=='OFF'){header('Location: dashboard');}
$sql = "SELECT doc_status FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'purchase_edit'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $purchase_edit = $statement->fetchColumn();
            $sql = "SELECT doc_status FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'purchase_new'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $purchase_new = $statement->fetchColumn();
              $sql = "SELECT doc_status FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'purchase_print'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $purchase_print = $statement->fetchColumn();
             $sql = "SELECT doc_status FROM user_rights WHERE user_id = '".$user_id."' and doc_name = 'purchase_delete'";
            $statement = $connect->prepare($sql);
            $statement->execute(); // no need to add `$sql` here, you can take that out
            $purchase_delete = $statement->fetchColumn();
?>
<!DOCTYPE html>
<html>
<head>
  <title><?php  if(basename(__FILE__, '.php'=='purchase.php')){echo 'Purchase Invoices';}?></title>
  <?php include('include/head.php'); ?>
  
</head>
<body>
  <?php include('include/header.php'); ?>
  <?php include('include/sidebar.php'); ?>
<div class="main-container">
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0 text-dark"></h1>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="#">Payables</a></li>
              <li class="breadcrumb-item active">Purchase Invoices </li>
            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->
    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <!-- Small boxes (Stat box) -->
     <span id="alert_action"></span>
    <div class="row">
        <div class="col-12">
          <div class="card">
            <div class="card-header">
            <div class="row">
                          <div class="col-lg-10 col-md-10 col-sm-8 col-xs-6">
                              <h3 class="panel-title">Purchase Invoices</h3>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-4 col-xs-6" align="right">
                              <a href="purchase_new?add=1" class="btn btn-primary"><i class="fa fa-plus"></i> New Purchase</a>
                          
                          </div>
                        </div>
            </div>
            <!-- /.card-header -->
            <div class="card-body">
              
                        <table width="100%" id="_dataTable" class="table table-bordered table-striped table-hover">
                          <thead>
                  <tr>
                    <th width="3%">ID</th>                    
                    <th width="15%">Date</th>
                     <th width="10%">Inv#</th>
                    <th width="25%">Vendor</th>
                    
                    <th width="17%">Created</th>
                  
                    
                    <th width="15%">Amount</th>
                  
                                   
                    <?php if($purchase_edit=='ON')
                    {
                      echo ' <th width="5%">Edit</th>';
                    }   ?>
                     <?php if($purchase_print=='ON')
                    {
                      echo ' <th width="5%">Print</th>';
                    }  ?> 
                     <?php if($purchase_delete=='ON')
                    {
                      echo ' <th width="5%">Delete</th>';
                    }   ?>
                
                   
                
                
                
                      
                  
                    
                   
                  </tr>
                </thead>
                        </table>
                         </div>
              </div>
          </div>
      </div>
             
        
        
      </div><!-- /.container-fluid -->
    </section>
    <!-- /.content -->
 
  <!-- /.content-wrapper -->
 
    </div>
    </div>
  <?php include('include/script.php'); ?>
  
</body>
</html>
        <div id="dataModal" class="modal fade">
          <div class="modal-dialog">
            <form method="post" id="data_form">
              <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
              <h4 class="modal-title"><i class="fa fa-pencil"></i> Purchase Expense Details</h4>
              </div>
              <div class="modal-body">
                
                <div class="form-group">
              <label>Description</label>
              <input type="text" name="description" id="description" class="form-control" required />
            </div>
             <div class="form-group">
              <label>BL</label>
              <input type="text" name="bl" id="bl" class="form-control" required />
            </div>
            <div class="form-group">
            <div align="right">
                    <button type="button" name="add_row" id="add_row" class="btn btn-success btn-xs">+</button>
                  </div>
             <table width="100%" id="item-table" class="table table-bordered">
                        </table>
            </div>
            <table width="100%" class="table table-bordered">
             <tr>
                <td width="50%" align="left"><b>Total : </td><td width="50%"><b><span id="tot_amount"></span></b></td>
               
                </tr>
                </table>
                
            
              </div>
              <div class="modal-footer">
                 <input type="hidden" name="p_id" id="p_id" />
                 <input type="hidden" name="total_item" id="total_item" />
                <input type="hidden" name="btn_action" id="btn_action" />
                <input type="submit" name="action" id="action" class="btn btn-info" value="Save" />
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              </div>
            </div>
            </form>
          </div>
        </div>
        
<script>
$(document).ready(function(){
  
  
  var _dataTable = $('#_dataTable').DataTable({
    "processing": true,
    "serverSide": true,
    "order": [],
    "ajax":{
      url:"purchase_fetch.php",
      type:"POST"
    },
    "columnDefs":[
      {
        "target":[2,3,4],
        "orderable":false
      }
    ],
    "language": {
          "info": "_START_-_END_ of _TOTAL_ entries",
          searchPlaceholder: "Search"
        },
        "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
        
        "pageLength": 25
  });
  $(document).on('click', '.delete', function(){
    var p_id = $(this).attr("id");
    
    var btn_action = "delete";
    if(confirm("Are you sure you want delete it permanently?"))
    {
      $.ajax({
        url:"purchase_action.php",
        method:"POST",
        data:{p_id:p_id, btn_action:btn_action},
        success:function(data)
        {
          $('#alert_action').fadeIn().html('<div class="alert alert-info">'+data+'</div>');
          _dataTable.ajax.reload();
        }
      })
    }
    else
    {
      return false;
    }
  });
  
        
        
        
       
        
  
 
  
});
    
</script>
<?php
//include('footer.php');
?>
