Ejemplo n.º 1
0
function payment($mode, $id = false)
{
    global $db;
    global $mysqlTimeDateFormat;
    /*
        $mode
          0   payment: new cash value will be written, 'upd' action on routers
          1   shownotification: new cash value will not be writtent, 'shownotify' on routers
          2   clearnotifications: 'clearnotify' on all routers
    */
    if ($mode !== 2) {
        if ($res = $db->query("SELECT id, price FROM " . DB_TABLE_PREFIX . "tariff")->fetchAll()) {
            $price = array();
            foreach ($res as $row) {
                $price[$row['id']] = $row['price'];
            }
        }
        $typeOfCalculation = configgetvalue('system', 'cash', NULL, 'typeOfCalculation');
        $creditMonths = configgetvalue('system', 'cash', NULL, 'creditMonths');
        $routerAction = $mode ? "shownotification" : "update";
        $usersTable = new table('user');
        $usersRes = $usersTable->load($id ? "WHERE id={$id}" : "");
        foreach ($usersRes as $row) {
            if (!isset($price[$row['tariff']])) {
                continue;
            }
            $tariffId = $row['tariff'];
            $userId = $row['id'];
            // Calculate amounts
            $cash = money($row['cash']);
            $sum = -getCashToPay($userId, $row, $price[$tariffId]);
            $fullMonthSum = -getCashToPay($userId, $row, $price[$tariffId], true);
            $newCash = $cash + $sum;
            $minimumCash = $fullMonthSum * intval($creditMonths);
            $currentOrder = getCurrentTariff($userId);
            if ($mode == 0) {
                if ($row['disabled'] == '0' && (!$currentOrder || $currentOrder['temp'] == 1) && ($newCash >= $minimumCash || $row['credit'] == '1')) {
                    //Add info to payments table
                    $orderTable = new table('order');
                    $moneyFlowTable = new table('moneyflow');
                    $currentDate = new DateTime('midnight');
                    $startDate = new DateTime('first day of this month midnight');
                    $endDate = new DateTime('first day of next month midnight');
                    $endDate->modify("-1 sec");
                    $withdrawalDay = configgetvalue('system', 'cash', NULL, 'withdrawalDay');
                    $withdrawalDay -= 1;
                    if ($withdrawalDay) {
                        $startDate->modify("+{$withdrawalDay} day");
                        $endDate->modify("+{$withdrawalDay} day");
                    }
                    if ($currentDate < $startDate) {
                        $startDate->modify("-1 month");
                        $endDate->modify("-1 month");
                    }
                    if ($currentOrder) {
                        $orderTable->edit(array('id' => $currentOrder['id'], 'canceled' => 1, 'enddate' => $currentDate->format($mysqlTimeDateFormat)));
                    }
                    $orderId = $orderTable->add(array('user' => $userId, 'detailsname' => 'tariff', 'detailsid' => $tariffId, 'startdate' => $currentDate->format($mysqlTimeDateFormat), 'enddate' => $endDate->format($mysqlTimeDateFormat)));
                    $moneyFlowTable->add(array('user' => $userId, 'detailsname' => 'order', 'detailsid' => $orderId, 'sum' => money($sum)));
                } else {
                    controllerRouterQueue($row['router'], "update", $row['id']);
                }
            } else {
                if ($mode == 1) {
                    if ($row['disabled'] == '0' && ($newCash < $minimumCash && $row['credit'] == '0')) {
                        controllerRouterQueue($row['router'], "shownotification", $row['id']);
                    }
                }
            }
        }
    } else {
        $routerTable = new table('router');
        $routerRes = $routerTable->load($id ? "WHERE id={$id}" : "");
        foreach ($routerRes as $row) {
            controllerRouterQueue($row['id'], 'clearnotification', $id);
        }
    }
}
Ejemplo n.º 2
0
 function calculateTariffPrice()
 {
     global $mysqlTimeDateFormat;
     return getCashToPay($this->getId());
 }
Ejemplo n.º 3
0
     $response->header = array(array('ip', 'ip'), array('mac', 'mac'));
     $mac = controllerRouter($_POST['router'], "getmac", $_POST['ip']);
     if ($mac) {
         $response->data[] = array($_POST['ip'], $mac);
     }
     $response->success = true;
     break;
 case 'getstatistics':
     require_once '../include/statistics.php';
     $statistics = new PaymentStatistics();
     $response = $statistics->getStatistics();
     break;
 case 'getcashtopay':
     $userId = $_GET['id'];
     $cashToPay = getCashToPay($userId);
     $fullCashToPay = getCashToPay($userId, false, false, true);
     $response->data = array("partial" => $cashToPay, "full" => $fullCashToPay);
     break;
 case 'routeronline':
     $response->header = array(array('ip', 'ip'), array('router', 'id'));
     $response->data = array();
     $routerTable = new table('router');
     $routers = $routerTable->load();
     foreach ($routers as $key => $value) {
         $id = $value['id'];
         $onlineUsers = controllerRouter($id, 'getonline');
         if (is_array($onlineUsers)) {
             foreach ($onlineUsers as $ip) {
                 $response->data[$ip] = array($ip, $id);
             }
         }