Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subscriptions #126

Open
ewooonk opened this issue Sep 4, 2017 · 1 comment
Open

subscriptions #126

ewooonk opened this issue Sep 4, 2017 · 1 comment

Comments

@ewooonk
Copy link

ewooonk commented Sep 4, 2017

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.

@ewooonk
Copy link
Author

ewooonk commented Sep 9, 2017

  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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant