<?php
require_once 'config/config.php';
$db = getDbInstance();
$charges = $db->get("charges");
foreach($charges as $charge) {
  if ($charge['total'] > 0) { // Check if total is greater than zero
    $add = round((time() - $charge['last_recharge']) / (86400/$charge['total']));
    $charge['charges'] = $charge['charges'] + $add;
    if ($charge['charges'] > $charge['total']) {
      $charge['charges'] = $charge['total'];
    }

    $db = getDbInstance();
    $db->where('amember', $charge['amember']);
    $db->update('charges', array(
      "charges" => $charge['charges'],
      "last_recharge" => time()
    ));
    echo $charge['amember']." adding ".$add."<br>\n";
  } else {
    echo $charge['amember']." has total 0, skipping<br>\n";
  }
}
