
<?php
session_start();
error_reporting(0);
@ini_set('display_errors', 0);
$id_user=$_SESSION['id'];
$branch=$_SESSION['branch_id'];
$business=$_SESSION['business_id'];
?>

<!DOCTYPE html>
<html>

<head>

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <?php
    include("../includes/add_licence.php");
    include("../includes/header-links.php");
$permissions = mysqli_fetch_array(mysqli_query($con,"SELECT * FROM  
  permissions,users WHERE  permissions.role_id = users.role 
  AND users.id='$id_user' AND users.business_id ='$business' AND users.branch_id ='$branch'"));

if($permissions['nav_settings_branch'] != 1){
?>
<script>window.location="../main/dashboard.php"</script>
<?php

}
?>

<link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="../bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
    <link href="../assets/styles.css" rel="stylesheet" media="screen">
    <link href="../assets/DT_bootstrap.css" rel="stylesheet" media="screen">
    <link rel="stylesheet" href="../data-table-css/bootstrap-table.css">
<link rel="stylesheet" href="../data-table-css/bootstrap-editable.css">
    <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="vendors/flot/excanvas.min.js"></script><![endif]-->
    <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
        <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <script src="../vendors/modernizr-2.6.2-respond-1.1.0.min.js"></script>
</head>

<body>
    <script type="text/javascript">
function FormatCurrency(ctrl) {
              //Check if arrow keys are pressed - we want to allow navigation around textbox using arrow keys
              if (event.keyCode == 37 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40) {
                  return;
              }

              var val = ctrl.value;

              val = val.replace(/,/g, "")
              ctrl.value = "";
              val += '';
              x = val.split('.');
              x1 = x[0];
              x2 = x.length > 1 ? '.' + x[1] : '';

              var rgx = /(\d+)(\d{3})/;

              while (rgx.test(x1)) {
                  x1 = x1.replace(rgx, '$1' + ',' + '$2');
              }

              ctrl.value = x1 + x2;
          }

          function CheckNumeric() {
              return event.keyCode >= 48 && event.keyCode <= 57 || event.keyCode == 46;
          }
</script>
    <?php include '../includes/header.php'; ?>
    <div class="container-fluid">
        <div class="row-fluid">
            <?php include '../includes/sidebar.php'; ?>
            <!--/span-->
            <div class="span10" id="content">

                 <div class="row-fluid">
                    <!-- block -->
                    <div class="block">
                        <div class="navbar navbar-inner block-header">
                            <div class="muted pull-left">Business Licence Notificstions</div>
                            <?php
                                                           // Create connection
if ($business!=0) {
 $sqlnotify = mysqli_query($con,"SELECT *,(select business_name from business where business_id=licences.business_id) as business FROM licences where business_id='$business'");
}else{
$sqlnotify = mysqli_query($con,"SELECT *,(select business_name from business where business_id=licences.business_id) as business FROM licences ");
}


//$result = mysqli_query($con, $sql);

if (mysqli_num_rows($sqlnotify) > 0) {
  while($rownotify = mysqli_fetch_assoc($sqlnotify)) {
    $notbusiness= $rownotify['business_id']; 
  
  $bsname=$rownotify['business'];
    $annual_bill=$rownotify['annual_bill']; 
    // $row['startdate']; 
    $enddate=$rownotify['enddate']; 
    $exp=strtotime($rownotify['enddate']);
    $thistoday=strtotime(date('Y-m-d'));
    $range=($exp-$thistoday)/(86400);
   $message="
            Dear $bsname,  
            Your current subscrption for Poscream Point Of Sales System will expire in $range days.<br>
             You need a Subscription Payment of  $annual_bill before your Licence expires.<br>
             Please Pay in time to avoid inconviniences<br>
             Thank You <br>
             NUGSOFT 


             ";
//              $contact='256777674441';

//     $user='vincent';
//     $password='matsiko.nugsoft';
//     $reciever=$contact;
//     $from='Poscream Point Of Sales System';
//     $message=$message;

//     $data = array(
//         'password' => $password,
//         'user' => $user,
//         'sender'  => $from,
//         'reciever'  => $reciever,
//         'message'  => $message
// );
    

      $date=date('Y-m-d');
    if ($range<=60 ) {
    if ($range>=31 ) {
      // echo $range;

      $checknotify=mysqli_query($con,"SELECT * from licence_notifications where business_id='$notbusiness' && remaining_days>=31 && remaining_days<=60");
       }else if ((int)$range>=16 ) {
        $checknotify=mysqli_query($con,"SELECT * from licence_notifications where business_id='$notbusiness' && remaining_days>=16 && remaining_days<=30");

        }else if ($range>=5 ) {
        $checknotify=mysqli_query($con,"SELECT * from licence_notifications where business_id='$notbusiness' && remaining_days>=6 && remaining_days<=15");
        }else if ($range>=5) {
          $checknotify=mysqli_query($con,"SELECT * from licence_notifications where business_id='$notbusiness' && remaining_days>=1 && remaining_days<=5");
        }else if ($range<=0) {
          mysqli_query($con,"UPDATE business SET status=0 where business_id='$notbusiness' ");
          $checknotify='';
        }

         if (mysqli_num_rows($checknotify) <= 0  && $range>0) {

          $notif=mysqli_query($con,"INSERT INTO licence_notifications(date,enddate,business_id,annual_bill,message,remaining_days) values('$date','$enddate','$notbusiness','$annual_bill','$message','$range')");
          if (!$notif) {
            echo "Error".mysqli_error($con);
          }else{
            echo "done";
          }
          
         }
    }else{
      // echo $range."br>";
    }
                                          
}
}
                            ?>
                        </div>
                        <div class="block-content collapse in">
                            <div class="span12">
                               
                               
                                    <!--Model Ends here -->
                              
                               <?php
include '../includes/connection.php';
                               // Create connection
if ($business!=0) {
 $sql = mysqli_query($con,"SELECT *,(select business_name from business where business_id=licence_notifications.business_id) as business FROM licence_notifications where business_id='$business'");
}else{
$sql = mysqli_query($con,"SELECT *,(select business_name from business where business_id=licence_notifications.business_id) as business FROM licence_notifications  ");
}

//$result = mysqli_query($con, $sql);

if (mysqli_num_rows($sql) > 0) {
// output data of each row



                               ?>
                           
                                <div id="toolbar">
                                    <select class="form-control">
                                            <option value="">Export Basic</option>
                                            <option value="all">Export All</option>
                                            <option value="selected">Export Selected</option>
                                        </select>
                                </div>
                                 
                                <table  id="table" data-toggle="table" data-pagination="true" data-search="true" data-show-columns="true" data-show-pagination-switch="true" data-show-refresh="false" data-key-events="true" data-show-toggle="false" data-resizable="false" data-cookie="true"
                                    data-cookie-id-table="saveId" data-show-export="true" data-click-to-select="true" data-toolbar="#toolbar" data-export-options='{"fileName": "Testing_filename"}'>
                                    <thead>
                                        <tr>
                                            <th>No</th>
                                            <th> Date</th>
                                            <th> Business</th>
                                            <th> Annual Payment</th>
                                            <th>Endate</th>
                                            <th>Message</th>
                                            
                                            <th>Remaining Days</th>
                                            <th>Recorded on</th>

                                        </tr>
                                    </thead>
                                    <tbody>
                                    
                                    <?php
                                    $no = 0;
                                    while($row = mysqli_fetch_assoc($sql)) {
                                        $no++;
                                            ?>
                                    <tr class="gradeU">
                                            <td><?php echo $no; ?></td>
                                            <td><?php echo $row['date']; ?></td>
                                            <td><?php echo $row['business']; ?></td>
                                            <td><?php echo number_format($row['annual_bill']); ?></td>
                                            <td><?php echo $row['enddate']; ?></td>
                                            <td><?php echo $row['message']; ?></td>
                                            <td><?php
                                            $exp=strtotime($row['enddate']);
                                              $thistoday=strtotime(date('Y-m-d'));
                                             echo $range=($exp-$thistoday)/(86400)." days";
                                          
                                              ?></td>
                                            <td><?php echo $row['recorded_on']; ?></td>

                                              
                                                
                                           
                                           

                                            
                                        </tr>
                                        
                                        <?php


}
} else {
echo "<h4 style='color:red;'>0 results</h4>";
}




?>


                                    
                                        
                                    </tbody>
                                </table>
                                
                            </div>
                        </div>
                    </div>
                    <!-- /block -->
                </div>
            </div>
        </div>
        
        <hr>
        <?php include '../includes/footer.php'; ?>
    </div>
    <!--/.fluid-container-->

    <!--==========Footer js======-->
    <?php require_once("../includes/footerjs.php");?>
    <!--==========Footer js======-->
<script type = "text/javascript">
function confirmationDelete(anchor){
    var conf = confirm("Are you sure you want to delete this Branch?");
    if(conf){
        window.location = anchor.attr("href");
    }
} 
function confirmationSuspend(anchor){
    var conf1 = confirm("Are you sure you want to Deactivate this Branch?");
    if(conf1){
        window.location = anchor.attr("href");
    }
}  
function confirmationReturn(anchor){
    var conf2 = confirm("Are you sure you want to Activate this Branch?");
    if(conf2){
        window.location = anchor.attr("href");
    }
}      
</script>
<script src="../data-table-js/bootstrap-table.js"></script>
<script src="../data-table-js/tableExport.js"></script>
<script src="../data-table-js/data-table-active.js"></script>
<script src="../data-table-js/bootstrap-table-editable.js"></script>
<script src="../data-table-js/bootstrap-editable.js"></script>
<script src="../data-table-js/bootstrap-table-resizable.js"></script>
<script src="../data-table-js/colResizable-1.5.source.js"></script>
<script src="../data-table-js/bootstrap-table-export.js"></script>
</body>


</html>

