<?php

include '../includes/connection.php';

$sqlnotify = mysqli_query($con,"SELECT *,(select business_name from business where business_id=licences.business_id) as business FROM licences ");



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="Poscream Point Of Sales System <br>
            Dear $bsname,  
            Your current subscrption 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 


             ";

      
      $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 ($range>0) {
         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>";
    }
                                          
}
}
                            ?>