| Server IP : 37.9.174.138 / Your IP : 216.73.216.117 Web Server : Apache System : Linux vps6.backend.sk 6.12.100+deb13-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.100-1 (2026-07-30) x86_64 User : ftpuser ( 1001) PHP Version : 8.4.24 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/ttjs.cz/dev/modules/ttjs/code/ |
Upload File : |
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace modules\ttjs\code;
use app\classes\BasicUser;
use app\classes\PDODatabase;
use app\classes\PDOCollection;
use app\constants\State;
/**
* Description of RewardPoints
*
* @author pegas
*/
class RewardPoints extends \app\classes\PDOElement {
PRIVATE CONST EXPIRATION_MONTH = 12;
public function __construct($id) {
parent::__construct($id, "reward_points", array("id" => $id), "id", false, false, true);
}
/**
* @return RewardPoints
*/
public static function getRewardPoints(BasicUser $user) {
$db = PDODatabase::getInstance();
$id = $db->getValue("SELECT MAX(id) FROM reward_points WHERE users_id=:users_id AND state=:ACTIVE", [
"users_id" => $user->_get("id"),
"ACTIVE" => State::ACTIVE
]);
if ($id === null) {
$db->addRecord("reward_points", [
"description" => "Default balance",
"points" => 0,
"points_balance" => 0,
"lifetime_points" => 0,
"users_id" => $user->_get("id")
]);
$id = $db->getLastInsertId();
}
return new RewardPoints($id);
}
public static function credit(float $paidAmount, BasicUser $user, $description = "Credit") {
$db = PDODatabase::getInstance();
$points = self::getRewardPoints($user);
$db->addRecord("reward_points", [
"type" => RewardPointsType::CREDIT,
"description" => $description,
"points" => $paidAmount / 100,
"unspended" => $paidAmount / 100,
"expires" => self::getLastDayOfNextYearMonth(self::EXPIRATION_MONTH),
"points_balance" => $points->_get("points_balance") + $paidAmount / 100,
"lifetime_points" => $points->_get("lifetime_points") + $paidAmount / 100,
"users_id" => $user->_get("id")
]);
}
public static function debit(float $points, BasicUser $user, $description = "Debit") {
$db = PDODatabase::getInstance();
$rewardPoints = self::getRewardPoints($user);
if ($rewardPoints->getBalance() < $points) {
throw new \Exception("Insufficient points balance!");
}
try {
$db->startTransaction();
$db->addRecord("reward_points", [
"type" => RewardPointsType::DEBIT,
"description" => $description,
"points" => $points,
"points_balance" => $rewardPoints->_get("points_balance") - $points,
"lifetime_points" => $rewardPoints->_get("lifetime_points"),
"users_id" => $user->_get("id")
]);
$balances = new PDOCollection("reward_points", "id", ""
. "SELECT * FROM reward_points WHERE "
. "users_id=:users_id AND type=:CREDIT AND unspended > 0 AND expires >= CURRENT_DATE() AND state=:ACTIVE "
. "ORDER BY id ASC",
[
"users_id" => $user->_get("id"),
"CREDIT" => RewardPointsType::CREDIT,
"ACTIVE" => State::ACTIVE
]
);
foreach ($balances->toArray() AS $el) {
$item = new RewardPoints($el["id"]);
if ($item->_get("unspended") >= $points) {
$item->update(["unspended" => $item->_get("unspended") - $points]);
break;
}
$points = $points - $item->_get("unspended");
$item->update(["unspended" => 0]);
}
$db->commit();
} catch (\Exception $ex) {
$db->rollback();
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
}
}
public static function revertDebit(float $points, BasicUser $user) {
$db = PDODatabase::getInstance();
try {
$db->startTransaction();
$balances = new PDOCollection("reward_points", "id", ""
. "SELECT * FROM reward_points WHERE "
. "users_id=:users_id AND type=:CREDIT AND unspended != points AND state=:ACTIVE "
. "ORDER BY id DESC",
[
"users_id" => $user->_get("id"),
"CREDIT" => RewardPointsType::CREDIT,
"ACTIVE" => State::ACTIVE
]
);
foreach ($balances->toArray() AS $el) {
$item = new RewardPoints($el["id"]);
$possibleRevertBalance = $item->_get("points") - $item->_get("unspended");
if ($possibleRevertBalance >= $points) {
$item->update(["unspended" => $item->_get("unspended") + $points]);
break;
}
$points = $points - $possibleRevertBalance;
$item->update(["unspended" => $item->_get("unspended") + $possibleRevertBalance]);
}
$db->commit();
} catch (\Exception $ex) {
$db->rollback();
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
}
}
private static function getLastDayOfNextYearMonth($month) {
$year = date('Y') + 1; // Get next year
$date = new \DateTime("$year-$month-01"); // First day of the given month in next year
$date->modify('last day of this month'); // Move to the last day of the month
return $date->format('Y-m-d'); // Format as YYYY-MM-DD
}
public function getBalance($decimalSeparator = ".") {
if ($decimalSeparator === ".") {
return floatval($this->_get("points_balance"));
}
return number_format($this->_get("points_balance"), 2, $decimalSeparator, " ");
}
public function getLifetimeBalance($decimalSeparator = ".") {
if ($decimalSeparator === ".") {
return floatval($this->_get("lifetime_points"));
}
return number_format($this->_get("lifetime_points"), 2, $decimalSeparator, " ");
}
public function delete() {
if ($this->db->getValue("SELECT id FROM reward_points WHERE users_id=:users_id AND id>:id AND state=:ACTIVE", [
"users_id" => $this->_get("users_id"),
"id" => $this->_get("id"),
"ACTIVE" => State::ACTIVE
]) !== null) {
throw new \Exception("Only the last record can be deleted.");
}
if ($this->_get("type") === RewardPointsType::DEBIT) {
self::revertDebit($this->_get("points"), new BasicUser($this->_get("users_id"), true));
}
parent::delete();
}
/**
* @return PDOCollection
*/
public static function getExpirationInfo(BasicUser $user) {
$collection = new PDOCollection("reward_points", "id", ""
. "SELECT id, SUM(unspended) AS unspended, expires FROM reward_points WHERE "
. "users_id=:users_id AND expires > DATE(NOW()) AND state=:ACTIVE "
. "GROUP BY expires "
. "ORDER BY expires ASC", [
"users_id" => $user->_get("id"),
"ACTIVE" => State::ACTIVE
]);
return $collection;
}
public static function getExpirationMessage($points, $date) {
$t = \app\classes\Translator::getInstance();
$points = floatval(str_replace(',', '.', $points));
if ($points - intval($points) > 0) {
return floor($points) . " " . $t->_get("bodov vyprší") . " <b>" . date("d.m.Y", strtotime($date)) . "</b>";
}
switch (intval($points)) {
case 1:
return floor($points) . " " . $t->_get("bod vyprší") . " <b>" . date("d.m.Y", strtotime($date)) . "</b>";
case 2:
case 3:
case 4:
return floor($points) . " " . $t->_get("body vypršia") . " <b>" . date("d.m.Y", strtotime($date)) . "</b>";
default:
return floor($points) . " " . $t->_get("bodov vyprší") . " <b>" . date("d.m.Y", strtotime($date)) . "</b>";
}
}
public function getFormattedPointsBalance($floor = false) {
$points = $this->getBalance();
if ($floor) {
$points = floor($points);
}
if ($points - intval($points) > 0) {
return number_format($points, 2, ",", " ");
}
return number_format($points, 0, ",", " ");
}
public static function getLoayalityCardCodeEncrypted(BasicUser $user) {
$pno = str_pad($user->_get("personalno"), 8, 0, STR_PAD_LEFT);
$sec = sha1(SECRET . $pno . SECRET);
$sec = substr($sec, 0, 4) . substr($sec, strlen($sec) - 4);
$sec = str_split($sec);
$pno = str_split($pno);
return $sec[6] . $pno[4] . $sec[7] . $sec[8] . $pno[3] . $sec[1] . $sec[5] . $pno[2] . $pno[6] . $sec[4] . $pno[7] . $pno[1] . $pno[8] . $sec[3] . $sec[2] . $pno[5];
}
public static function getLoayalityCardCode(BasicUser $user) {
return str_pad($user->_get("personalno"), 8, 0, STR_PAD_LEFT);
}
public static function getPIN(BasicUser $user) {
$map = [
'a' => 8, 'b' => 2, 'c' => 4, 'd' => 1, 'e' => 7, 'f' => 3, 'g' => 6,
'h' => 0, 'i' => 9, 'j' => 2, 'k' => 1, 'l' => 4, 'm' => 5, 'n' => 3,
'o' => 6, 'p' => 0, 'q' => 8, 'r' => 7, 's' => 2, 't' => 9, 'u' => 4,
'v' => 1, 'w' => 6, 'x' => 3, 'y' => 7, 'z' => 5,
'A' => 0, 'B' => 1, 'C' => 8, 'D' => 3, 'E' => 5, 'F' => 2, 'G' => 9,
'H' => 6, 'I' => 4, 'J' => 7, 'K' => 2, 'L' => 0, 'M' => 3, 'N' => 1,
'O' => 9, 'P' => 6, 'Q' => 5, 'R' => 8, 'S' => 4, 'T' => 7, 'U' => 0,
'V' => 2, 'W' => 3, 'X' => 5, 'Y' => 9, 'Z' => 1
];
$converted = '';
$input = substr($user->_get("secret"), 0, 4);
$chars = str_split($input);
foreach ($chars as $char) {
if (isset($map[$char])) {
$converted .= $map[$char];
} else {
$converted .= $char;
}
}
return $converted;
}
}