| 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/app/classes/ |
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 app\classes;
/**
* Description of BasicUser
*
* @author Maroš
*/
class BasicUser extends SVUser {
CONST UPLOAD_DIR = "uploads/private/uploads/users/";
public function __construct($email, $idAsEmail = false, $allow_deleted = false, $allow_disabled = false, $types = array()) {
parent::__construct("users", $email, $idAsEmail ? "id" : "email", $allow_deleted, $allow_disabled, $types);
$this->uniqueColumn = array("email");
}
/**
* @return \app\classes\BasicUser
*/
public static function getLoggedUser($types = array()) {
if (self::$loggedUser === NULL || (count($types) > 0 && !in_array(self::$loggedUser->_get("type"), $types))) {
if (isset($_SESSION["basicUser"])) {
self::$loggedUser = new BasicUser($_SESSION["basicUser"], false, false, false, $types);
return self::$loggedUser;
}
try {
if (!isset(getallheaders()["Authorization"])) {
throw new \Exception(\app\constants\ResponseCodes::getMessageForCode(\app\constants\ResponseCodes::UNAUTHORIZED), \app\constants\ResponseCodes::UNAUTHORIZED);
}
$token = getallheaders()["Authorization"];
$tokenParse = explode("|", $token);
if (count($tokenParse) !== 2) {
throw new \Exception(\app\constants\ResponseCodes::INVALID_AUTH_TOKEN, \app\constants\ResponseCodes::INVALID_AUTH_TOKEN);
}
$user = new \app\classes\BasicUser($tokenParse[0]);
if ($user->authorize($token) === false) {
throw new \Exception(\app\constants\ResponseCodes::INVALID_AUTH_TOKEN, \app\constants\ResponseCodes::INVALID_AUTH_TOKEN);
}
self::$loggedUser = $user;
} catch (\Exception $ex) {
throw new \Exception(\app\constants\ResponseCodes::INVALID_AUTH_TOKEN, \app\constants\ResponseCodes::INVALID_AUTH_TOKEN);
}
throw new \Exception(\app\constants\ResponseCodes::getMessageForCode(\app\constants\ResponseCodes::UNAUTHORIZED), \app\constants\ResponseCodes::UNAUTHORIZED);
}
return self::$loggedUser;
}
/**
* @return \app\classes\AdminUser
*/
public static function newUser($data) {
$userData["state"] = $data["state"];
$userData["first_name"] = $data["first_name"];
$userData["last_name"] = $data["last_name"];
$userData["personalno"] = $data["personalno"];
$userData["email"] = trim($data["email"]);
$userData["phone"] = $data["phone"];
$userData["type"] = $data["type"];
$userData["color"] = $data["color"];
$userData["contract"] = $data["contract"];
$userData["registration_ip"] = get_ip_address();
$userData["folder"] = getUniqueId();
$userData["password"] = generateRandomString(64);
$userData["secret"] = generateRandomString(40);
$db = PDODatabase::getInstance();
$db->addRecord("users", $userData);
return new BasicUser($userData["email"], false, false, true);
}
public function login($password) {
$loginResult = parent::login($password);
if ($loginResult !== false) {
$_SESSION["basicUser"] = $this->_get("email");
$_SESSION["email"] = $this->_get("email");
$_SESSION["type"] = $this->_get("type");
if ($this->_get("type") === \app\constants\UserType::ADMIN) {
$_SESSION["adminUser"] = $this->_get("email");
}
}
return $loginResult;
}
public function getUploadPath($www = false) {
$nonwww = DOCUMENT_ROOT . self::UPLOAD_DIR . $this->_get("folder") . "/";
if (!file_exists($nonwww)) {
mkdir($nonwww);
}
return $www ? "/" . self::UPLOAD_DIR . $this->_get("folder") . "/" : $nonwww;
}
public function pridejNabidku(string $datumCas, int $pocetKs, int $provize, int $znackaKvality, string $feedback) {
$this->db->addRecord("pdso_offers", [
"users_id" => $this->_get("id"),
"state" => \app\constants\State::ACTIVE,
"created" => date("Y-m-d H:i:s", strtotime($datumCas)),
"created_by" => "admin@ttjs.cz",
"last_update" => date("Y-m-d H:i:s", strtotime($datumCas)),
"last_update_by" => "admin@ttjs.cz",
"items_count" => $pocetKs,
"approx_commission" => $provize,
"rating" => $znackaKvality,
"feedback" => $feedback,
"customer_notified" => 1,
], [], "id", false);
}
public function prodejKus(string $datumCas, string $datumPredaja, int $cena, int $proviziaPercent) {
$cnf = new \modules\ttjs\code\TtjsConfig();
$currency = $cnf->getConfig(\modules\ttjs\code\TtjsConfig::CURRENCY);
$taxRate = $cnf->getConfig(\modules\ttjs\code\TtjsConfig::TAX_RATE) / 100;
$cost = $cena;
$percentage = $proviziaPercent;
$commissionRate = $percentage / 100;
$commssion = ($cost - $cost * $taxRate) * $commissionRate;
$this->db->addRecord("sold_items", [
"users_id" => $this->_get("id"),
"state" => \app\constants\State::ACTIVE,
"created" => date("Y-m-d H:i:s", strtotime($datumCas)),
"created_by" => "admin@ttjs.cz",
"last_update" => date("Y-m-d H:i:s", strtotime($datumCas)),
"last_update_by" => "admin@ttjs.cz",
"cost" => $cost,
"commission" => \modules\ttjs\code\Currency::getRoundedVal($commssion, $currency, false, false, "."),
"sell_date" => date("Y-m-d", strtotime($datumPredaja))
], [], "id", false);
}
public function vyplatProvizi(string $datumCas, int $suma) {
$cnf = new \modules\ttjs\code\TtjsConfig();
$vsPrefix = $cnf->getConfig(\modules\ttjs\code\TtjsConfig::PAYMENTS_VS_PREFIX);
$app = \App::getInstance();
$t = Translator::getInstance();
$months = \modules\ttjs\code\Stocks::getMonths();
$month = $t->_get($months[date("m", strtotime("-1 month", strtotime($datumCas)))]);
$year = $t->_get(date("Y", strtotime("-1 month", strtotime($datumCas))));
$note = $t->_get("Provízia za") . " " . $month . " " . $year . " (" . $app->getConfig(\app\constants\Config::PAGE_NAME) . ")";
$this->db->addRecord("paid_commissions", [
"users_id" => $this->_get("id"),
"state" => \app\constants\State::ACTIVE,
"created" => date("Y-m-d H:i:s", strtotime($datumCas)),
"created_by" => "admin@ttjs.cz",
"last_update" => date("Y-m-d H:i:s", strtotime($datumCas)),
"last_update_by" => "admin@ttjs.cz",
"amount" => $suma,
"iban" => $this->_get("iban"),
"vs" => $vsPrefix . str_pad($this->_get("personalno"), 5, 0, STR_PAD_LEFT),
"note" => $note,
], [], "id", false);
}
public function updateStockHistory() {
$db = PDODatabase::getInstance();
$db->sqlCommand("DELETE FROM stocks WHERE snapshot!=:snapshot AND users_id=:users_id", [
"snapshot" => 0,
"users_id" => $this->_get("id")
]);
$dates = $this->getLastDaysOfMonths($this->getDefaultStock()->_get("created"), date("Y-m-d"));
$defaultStockSnapshot = intval(date('Ym', strtotime($this->getDefaultStock()->_get("created") . ' -1 months')));
foreach ($dates as $date) {
$thisMonthSnapshot = intval(date("Ym", strtotime($date)));
$prevMonthSnapshot = intval(date('Ym', strtotime($date) - 60 * 60 * 24 * 40));
$prev2MonthSnapshot = intval(date('Ym', strtotime($date) - 60 * 60 * 24 * 70));
if ($prevMonthSnapshot <= $defaultStockSnapshot) {
continue;
}
$prev2MonthSnapshotId = $db->getValue("SELECT id FROM stocks WHERE "
. "users_id=:users_id AND snapshot=:snapshot AND state=:ACTIVE", array(
"users_id" => $this->_get("id"),
"snapshot" => $prev2MonthSnapshot,
"ACTIVE" => \app\constants\State::ACTIVE
));
if ($prev2MonthSnapshotId === null) {
$prev2MonthSnapshotStock = $this->getDefaultStock();
} else {
$prev2MonthSnapshotStock = new \modules\ttjs\code\Stock($prev2MonthSnapshotId);
}
$received = $db->getValue("SELECT IFNULL(SUM(items_count), 0) FROM pdso_offers WHERE "
. "users_id=:users_id AND EXTRACT(YEAR_MONTH FROM `created`)=:snapshot AND state=:ACTIVE", array(
"users_id" => $this->_get("id"),
"snapshot" => $prevMonthSnapshot,
"ACTIVE" => \app\constants\State::ACTIVE
));
$deprecated = $db->getValue("SELECT IFNULL(SUM(items_count), 0) FROM deprecated_items WHERE "
. "users_id=:users_id AND EXTRACT(YEAR_MONTH FROM `deprecation_date`)=:snapshot AND state=:ACTIVE", array(
"users_id" => $this->_get("id"),
"snapshot" => $prevMonthSnapshot,
"ACTIVE" => \app\constants\State::ACTIVE
));
$sold = $db->getValue("SELECT IFNULL(COUNT(*), 0) FROM sold_items WHERE "
. "users_id=:users_id AND EXTRACT(YEAR_MONTH FROM `sell_date`)=:snapshot AND state=:ACTIVE", array(
"users_id" => $this->_get("id"),
"snapshot" => $prevMonthSnapshot,
"ACTIVE" => \app\constants\State::ACTIVE
));
$commission = $db->getValue("SELECT IFNULL(SUM(commission), 0) FROM sold_items WHERE "
. "users_id=:users_id AND EXTRACT(YEAR_MONTH FROM `sell_date`)=:snapshot AND state=:ACTIVE", array(
"users_id" => $this->_get("id"),
"snapshot" => $prevMonthSnapshot,
"ACTIVE" => \app\constants\State::ACTIVE
));
$paidCommission = $db->getValue("SELECT IFNULL(SUM(amount), 0) FROM paid_commissions WHERE "
. "users_id=:users_id AND EXTRACT(YEAR_MONTH FROM `created`)=:snapshot AND state=:ACTIVE", array(
"users_id" => $this->_get("id"),
"snapshot" => $prevMonthSnapshot,
"ACTIVE" => \app\constants\State::ACTIVE
));
$snapshot = array(
"snapshot" => $prevMonthSnapshot,
"items_count" => $prev2MonthSnapshotStock->_get("items_count") + $received - $deprecated - $sold,
"total_commission" => $prev2MonthSnapshotStock->_get("total_commission") + $commission,
"unpaid_commission" => $prev2MonthSnapshotStock->_get("total_commission") + $commission - $paidCommission,
"users_id" => $this->_get("id"),
"state" => \app\constants\State::ACTIVE,
"created" => date("Y-m-d", strtotime($date)) . " 23:40:01",
"created_by" => "cronjob",
"last_update" => date("Y-m-d", strtotime($date)) . " 23:40:01",
"last_update_by" => "cronjob",
);
//var_dump($snapshot);
try {
$db->addRecord("stocks", $snapshot, [], "", false);
} catch (\Exception $ex) {
//snapshot already exists
}
}
}
function getLastDaysOfMonths($startDate, $endDate) {
$start = new \DateTime($startDate);
$end = new \DateTime($endDate);
// Normalize start to the first day of the month
$start->modify('first day of this month');
$interval = new \DateInterval('P1M');
$period = new \DatePeriod($start, $interval, $end);
$lastDays = [];
foreach ($period as $date) {
// Calculate last day of the current iteration month
$lastDayOfMonth = \DateTime::createFromFormat('Y-m-d', $date->format('Y-m-t'));
// Only include if last day is within the range
if ($lastDayOfMonth <= $end) {
$lastDays[] = $lastDayOfMonth->format('Y-m-d');
}
}
return $lastDays;
}
}