Exemple #1
0
 public function pay($code, $userId)
 {
     if (preg_match("/^\\d{" . $this->codeLength . "}\$/", $code)) {
         $usersTable = new table('user');
         $cardmatch = $this->table->load("WHERE activated=0 AND code='{$code}'");
         $usermatch = $usersTable->load("WHERE id={$userId}");
         c(" WHERE activated=1 AND code='{$code}'");
         c($userId);
         if (count($usermatch) == 1 && count($cardmatch) == 1) {
             c(42);
             $card = $cardmatch[0];
             $user = $usermatch[0];
             $paySum = intval($card['value']);
             $usersTable->edit(array('id' => $userId, 'cash' => intval($user['cash']) + $paySum));
             $this->table->edit(array('id' => $card['id'], 'activated' => 1, 'activationdate' => date("Y-m-d H:i:s"), 'user' => $userId));
             return $paySum;
         } else {
             l('payment', 'badscratchcard', NULL, $userId, NULL, array());
         }
         return false;
     }
 }
Exemple #2
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);
        }
    }
}
Exemple #3
0
     foreach ($data as $key => $value) {
         if ($key !== 'id' && !checkPermission($sessionId, array('table', $target, 'edit', $key))) {
             unset($data[$key]);
         }
     }
 }
 // Extract cash difference from user and proceed as moneflow
 if ($target == 'user' && ($action == 'dbedit' || $action == 'dbadd') && isset($data['cash'])) {
     unset($data['cash']);
 }
 switch ($action) {
     case 'dbadd':
         $id = $table->add($data);
         break;
     case 'dbedit':
         $id = $table->edit($data);
         break;
     case 'dbremove':
         $id = $table->delete($data);
         break;
 }
 if ($target == 'user' && ($action == 'dbedit' || $action == 'dbadd') && isset($_POST['cash']) && checkPermission($sessionId, array('table', $target, 'edit', 'cash'))) {
     if ($action === 'dbedit') {
         $user = $table->loadById($id);
         $sum = money($_POST['cash']) - money($user['cash']);
     } else {
         $sum = money($_POST['cash']);
     }
     if ($sum) {
         $moneyflowTable = new table('moneyflow');
         $moneyflowTable->add(array("user" => $id, "sum" => $sum, "detailsname" => "adminpay", "detailsid" => $sessionId));