cyklokoalicia/OpenSourceBikeShare

subscriptions

ewooonk opened this issue · 1 comments

Checks if the user has a subscription, then allows the user to ride the first X minutes of each ride for free.

Will post code soon.

  1. Add the column sub float(1,0) to the credit table.

  2. Add the following function to common.php

function getusersub($userid)
{
   global $db,$credit;
   if (iscreditenabled()==FALSE) return; // if credit system disabled, exit
   $result=$db->query("SELECT sub FROM credit WHERE userId=$userid");
   $row=$result->fetch_assoc();
   $usersub=$row["sub"];
   return $usersub;
}`
  1. Replace the 'changecreditrendrental' function in common.php
function changecreditendrental($bike,$userid)
{
   global $db,$watches,$credit;
   if (iscreditenabled()==FALSE) return; // if credit system disabled, exit
   $usercredit=getusercredit($userid);
   $result=$db->query("SELECT time FROM history WHERE bikeNum=$bike AND userId=$userid AND (action='RENT' OR action='FORCERENT') ORDER BY time DESC LIMIT 1");
   if ($result->num_rows==1)
      {
      $row=$result->fetch_assoc();
      $starttime=strtotime($row["time"]);
      $endtime=time();
      $timediff=$endtime-$starttime;
      $creditchange=0;
      $changelog="";
      $usersub=getusersub($userid);
      $freetime=0;
      if ($usersub==1) $freetime=$watches["freetime"];
      if ($timediff<$freetime*60)
         {
         $creditchange=$creditchange;
         $changelog.="overfree-".$credit["rent"].";";
         }
      if ($freetime==0) $freetime=2; // for further calculations
      if ($credit["pricecycle"] AND $timediff>$freetime*60) // after first paid period, i.e. freetime*2; if pricecycle enabled
         {
         $temptimediff=$timediff-($freetime*60);
         if ($credit["pricecycle"]==1) // flat price per cycle
            {
            $cycles=ceil($temptimediff/($watches["flatpricecycle"]*60));
            $creditchange=$creditchange+($credit["rent"]*$cycles);
            $changelog.="flat-".$credit["rent"]*$cycles.";";
            }
         elseif ($credit["pricecycle"]==2) // double price per cycle
            {
            $cycles=ceil($temptimediff/($watches["doublepricecycle"]*60));
            $tempcreditrent=$credit["rent"];
            for ($i=1;$i<=$cycles;$i++)
               {
               $multiplier=$i;
               if ($multiplier>$watches["doublepricecyclecap"])
                  {
                  $multiplier=$watches["doublepricecyclecap"];
                  }
               // exception for rent=1, otherwise square won't work:
               if ($tempcreditrent==1) $tempcreditrent=2;
               $creditchange=$creditchange+pow($tempcreditrent,$multiplier);
               $changelog.="double-".pow($tempcreditrent,$multiplier).";";
               }
            }
         }
      if ($timediff>$watches["longrental"]*3600)
         {
         $creditchange=$creditchange+$credit["longrental"];
         $changelog.="longrent-".$credit["longrental"].";";
         }
      $usercredit=$usercredit-$creditchange;
      $result=$db->query("UPDATE credit SET credit=$usercredit WHERE userId=$userid");
      $result=$db->query("INSERT INTO history SET userId=$userid,bikeNum=$bike,action='CREDITCHANGE',parameter='".$creditchange."|".$changelog."'");
      $result=$db->query("INSERT INTO history SET userId=$userid,bikeNum=$bike,action='CREDIT',parameter=$usercredit");
      return $creditchange;
      }
}

That's it.