Skip to content
Code-Schnipsel Gruppen Projekte

feat: new versioning

Zusammengeführt Henning Leutz schlägt vor, dev in next zu mergen.
7 Dateien
+ 145
124
Änderungen vergleichen
  • Nebeneinander
  • In der Reihe
Dateien
7
+ 101
90
@@ -2,10 +2,21 @@
namespace QUI\ERP\Coupons;
use DateInterval;
use DateTime;
use Exception;
use PDO;
use QUI;
use QUI\ERP\Discount\Handler as DiscountHandler;
use QUI\Utils\Grid;
use QUI\Utils\Security\Orthos;
use QUI\ERP\Discount\Handler as DiscountHandler;
use function current;
use function explode;
use function implode;
use function is_null;
use function json_encode;
use function preg_replace;
/**
* Class Handler
@@ -34,7 +45,7 @@ class Handler
*
* @var CouponCode[]
*/
protected static $couponCodes = [];
protected static array $couponCodes = [];
/**
* Get CouponCode
@@ -43,7 +54,7 @@ class Handler
* @return CouponCode
* @throws CouponCodeException
*/
public static function getCouponCode($id)
public static function getCouponCode(int $id): CouponCode
{
if (isset(self::$couponCodes[$id])) {
return self::$couponCodes[$id];
@@ -61,18 +72,19 @@ class Handler
* @return CouponCode
*
* @throws CouponCodeException
* @throws QUI\Database\Exception
*/
public static function getCouponCodeByCode($code)
public static function getCouponCodeByCode(string $code): CouponCode
{
$result = QUI::getDataBase()->fetch([
'select' => [
'id'
],
'from' => self::getTable(),
'where' => [
'from' => self::getTable(),
'where' => [
'code' => $code
],
'limit' => 1
'limit' => 1
]);
if (empty($result)) {
@@ -95,9 +107,9 @@ class Handler
* @param array $settings (optional) - If omitted a random default CouponCode is generated
* @return CouponCode
*
* @throws \Exception
* @throws Exception
*/
public static function createCouponCode($discountIds, $settings = [])
public static function createCouponCode(array $discountIds, array $settings = []): CouponCode
{
$DiscountHandler = DiscountHandler::getInstance();
@@ -112,19 +124,19 @@ class Handler
foreach ($discountIds as $discountId) {
try {
$DiscountHandler->getChild($discountId);
} catch (\Exception $Exception) {
} catch (Exception $Exception) {
throw new CouponCodeException([
'quiqqer/coupons',
'exception.Handler.discount_error',
[
'discountId' => $discountId,
'error' => $Exception->getMessage()
'error' => $Exception->getMessage()
]
]);
}
}
$Now = new \DateTime();
$Now = new DateTime();
if (!empty($settings['code'])) {
if (self::existsCode($settings['code'])) {
@@ -149,24 +161,24 @@ class Handler
}
$couponCode = [
'title' => empty($settings['title']) ? null : $settings['title'],
'createDate' => $Now->format('Y-m-d H:i:s'),
'code' => $code,
'maxUsages' => $maxUsages,
'discountIds' => \json_encode($discountIds)
'title' => empty($settings['title']) ? null : $settings['title'],
'createDate' => $Now->format('Y-m-d H:i:s'),
'code' => $code,
'maxUsages' => $maxUsages,
'discountIds' => json_encode($discountIds)
];
if (!empty($settings['validUntilDate'])) {
$ValidUntil = new \DateTime($settings['validUntilDate']);
$ValidUntil = new DateTime($settings['validUntilDate']);
$couponCode['validUntilDate'] = $ValidUntil->format('Y-m-d H:i:s');
}
if (!empty($settings['userIds'])) {
$couponCode['userIds'] = \json_encode(\explode(",", $settings['userIds']));
$couponCode['userIds'] = json_encode(explode(",", $settings['userIds']));
}
if (!empty($settings['groupIds'])) {
$couponCode['groupIds'] = \json_encode(\explode(",", $settings['groupIds']));
$couponCode['groupIds'] = json_encode(explode(",", $settings['groupIds']));
}
QUI::getDataBase()->insert(
@@ -185,9 +197,9 @@ class Handler
* @param array $settings (optional) - If omitted a random default CouponCode is generated
* @return CouponCode
*
* @throws \Exception
* @throws Exception
*/
public static function editCouponCode($id, $discountIds, $settings = [])
public static function editCouponCode(int $id, array $discountIds, array $settings = []): CouponCode
{
QUI\Permissions\Permission::checkPermission(self::PERMISSION_EDIT);
@@ -208,23 +220,25 @@ class Handler
foreach ($discountIds as $discountId) {
try {
$DiscountHandler->getChild($discountId);
} catch (\Exception $Exception) {
} catch (Exception $Exception) {
throw new CouponCodeException([
'quiqqer/coupons',
'exception.Handler.discount_error',
[
'discountId' => $discountId,
'error' => $Exception->getMessage()
'error' => $Exception->getMessage()
]
]);
}
}
$Now = new \DateTime();
$Now = new DateTime();
if (!empty($settings['code'])) {
if ($CouponCode->getCode() !== $settings['code']
&& self::existsCode($settings['code'])) {
if (
$CouponCode->getCode() !== $settings['code']
&& self::existsCode($settings['code'])
) {
throw new CouponCodeException([
'quiqqer/coupons',
'exception.Handler.code_already_exists',
@@ -246,28 +260,28 @@ class Handler
}
$couponCode = [
'title' => empty($settings['title']) ? null : $settings['title'],
'createDate' => $Now->format('Y-m-d H:i:s'),
'code' => $code,
'maxUsages' => $maxUsages,
'discountIds' => \json_encode($discountIds)
'title' => empty($settings['title']) ? null : $settings['title'],
'createDate' => $Now->format('Y-m-d H:i:s'),
'code' => $code,
'maxUsages' => $maxUsages,
'discountIds' => json_encode($discountIds)
];
if (!empty($settings['validUntilDate'])) {
$ValidUntil = new \DateTime($settings['validUntilDate']);
$ValidUntil = new DateTime($settings['validUntilDate']);
$couponCode['validUntilDate'] = $ValidUntil->format('Y-m-d H:i:s');
} else {
$couponCode['validUntilDate'] = null;
}
if (!empty($settings['userIds'])) {
$couponCode['userIds'] = \json_encode(\explode(",", $settings['userIds']));
$couponCode['userIds'] = json_encode(explode(",", $settings['userIds']));
} else {
$couponCode['userIds'] = null;
}
if (!empty($settings['groupIds'])) {
$couponCode['groupIds'] = \json_encode(\explode(",", $settings['groupIds']));
$couponCode['groupIds'] = json_encode(explode(",", $settings['groupIds']));
} else {
$couponCode['groupIds'] = null;
}
@@ -289,11 +303,11 @@ class Handler
* @return CouponCode[]|int
* @throws CouponCodeException
*/
public static function search($searchParams, $countOnly = false)
public static function search(array $searchParams, bool $countOnly = false)
{
$couponCodes = [];
$Grid = new Grid($searchParams);
$gridParams = $Grid->parseDBParams($searchParams);
$Grid = new Grid($searchParams);
$gridParams = $Grid->parseDBParams($searchParams);
$binds = [];
$where = [];
@@ -304,7 +318,7 @@ class Handler
$sql = "SELECT id";
}
$sql .= " FROM `".self::getTable()."`";
$sql .= " FROM `" . self::getTable() . "`";
if (!empty($searchParams['search'])) {
$searchColumns = [
@@ -316,49 +330,46 @@ class Handler
$whereOr = [];
foreach ($searchColumns as $searchColumn) {
$whereOr[] = '`'.$searchColumn.'` LIKE :search';
$whereOr[] = '`' . $searchColumn . '` LIKE :search';
}
if (!empty($whereOr)) {
$where[] = '('.\implode(' OR ', $whereOr).')';
$where[] = '(' . implode(' OR ', $whereOr) . ')';
$binds['search'] = [
'value' => '%'.$searchParams['search'].'%',
'type' => \PDO::PARAM_STR
'value' => '%' . $searchParams['search'] . '%',
'type' => PDO::PARAM_STR
];
}
}
// build WHERE query string
if (!empty($where)) {
$sql .= " WHERE ".\implode(" AND ", $where);
$sql .= " WHERE " . implode(" AND ", $where);
}
// ORDER
if (!empty($searchParams['sortOn'])
) {
if (!empty($searchParams['sortOn'])) {
$sortOn = Orthos::clear($searchParams['sortOn']);
$order = "ORDER BY ".$sortOn;
$order = "ORDER BY " . $sortOn;
if (isset($searchParams['sortBy']) &&
!empty($searchParams['sortBy'])
) {
$order .= " ".Orthos::clear($searchParams['sortBy']);
if (!empty($searchParams['sortBy'])) {
$order .= " " . Orthos::clear($searchParams['sortBy']);
} else {
$order .= " ASC";
}
$sql .= " ".$order;
$sql .= " " . $order;
} else {
$sql .= " ORDER BY id DESC";
}
// LIMIT
if (!empty($gridParams['limit']) && !$countOnly) {
$sql .= " LIMIT ".$gridParams['limit'];
$sql .= " LIMIT " . $gridParams['limit'];
} else {
if (!$countOnly) {
$sql .= " LIMIT ".(int)20;
$sql .= " LIMIT " . 20;
}
}
@@ -366,22 +377,22 @@ class Handler
// bind search values
foreach ($binds as $var => $bind) {
$Stmt->bindValue(':'.$var, $bind['value'], $bind['type']);
$Stmt->bindValue(':' . $var, $bind['value'], $bind['type']);
}
try {
$Stmt->execute();
$result = $Stmt->fetchAll(\PDO::FETCH_ASSOC);
} catch (\Exception $Exception) {
$result = $Stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $Exception) {
QUI\System\Log::addError(
self::class.' :: search() -> '.$Exception->getMessage()
self::class . ' :: search() -> ' . $Exception->getMessage()
);
return [];
}
if ($countOnly) {
return (int)\current(\current($result));
return (int)current(current($result));
}
foreach ($result as $row) {
@@ -392,20 +403,22 @@ class Handler
}
/**
* Check if an invite code already eixsts
* Check if a CouponCode exists by its code
*
* @param string $code
* @return bool
* @param string $code The code of the CouponCode
* @return bool Returns true if the CouponCode exists, false otherwise
*
* @throws QUI\Database\Exception
*/
public static function existsCode($code)
public static function existsCode(string $code): bool
{
$result = QUI::getDataBase()->fetch([
'select' => 'id',
'from' => self::getTable(),
'where' => [
'from' => self::getTable(),
'where' => [
'code' => $code
],
'limit' => 1
'limit' => 1
]);
return !empty($result);
@@ -419,7 +432,7 @@ class Handler
public static function getRegistrationSite()
{
try {
$Conf = QUI::getPackage('quiqqer/coupons')->getConfig();
$Conf = QUI::getPackage('quiqqer/coupons')->getConfig();
$regSite = $Conf->get('settings', 'registrationSite');
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
@@ -433,7 +446,7 @@ class Handler
try {
return QUI\Projects\Site\Utils::getSiteByLink($regSite);
} catch (\Exception $Exception) {
} catch (Exception $Exception) {
return false;
}
}
@@ -441,28 +454,27 @@ class Handler
/**
* Deletes all CouponCodes that are expired
*
* @param int $days (optional) - Delete expired Codes that are older than X days [default: delete all]
* @param int|null $days (optional) - Delete expired Codes that are older than X days [default: delete all]
* @return void
*
* @throws \Exception
* @throws Exception
*/
public static function deleteExpiredCouponCodes($days = null)
public static function deleteExpiredCouponCodes(int $days = null)
{
$Now = new \DateTime();
$Now = new DateTime();
$where = [
'validUntilDate' => [
'type' => '<=',
'type' => '<=',
'value' => $Now->format('Y-m-d H:i:s')
]
];
if (!\is_null($days)) {
$days = (int)$days;
$OldDate = new \DateTime();
$OldDate->sub(new \DateInterval('P'.$days.'D'));
if (!is_null($days)) {
$OldDate = new DateTime();
$OldDate->sub(new DateInterval('P' . $days . 'D'));
$where['validUntilDate'] = [
'type' => '<=',
'type' => '<=',
'value' => $OldDate->format('Y-m-d H:i:s')
];
}
@@ -476,27 +488,26 @@ class Handler
/**
* Deletes all CouponCodes that have been redeemed
*
* @param int $days (optional) - Delete redeemed Codes that are older than X days [default: delete all]
* @param int|null $days (optional) - Delete redeemed Codes that are older than X days [default: delete all]
* @return void
*
* @throws \Exception
* @throws Exception
*/
public static function deleteRedeemedCouponCodes($days = null)
public static function deleteRedeemedCouponCodes(int $days = null)
{
$where = [
'useDate' => [
'type' => 'NOT',
'type' => 'NOT',
'value' => null
]
];
if (!\is_null($days)) {
$days = (int)$days;
$OldDate = new \DateTime();
$OldDate->sub(new \DateInterval('P'.$days.'D'));
if (!is_null($days)) {
$OldDate = new DateTime();
$OldDate->sub(new DateInterval('P' . $days . 'D'));
$where['useDate'] = [
'type' => '<=',
'type' => '<=',
'value' => $OldDate->format('Y-m-d H:i:s')
];
}
@@ -513,9 +524,9 @@ class Handler
* @param string $code
* @return string
*/
public static function sanitizeCode($code)
public static function sanitizeCode(string $code): string
{
return \preg_replace('#[^A-Za-z0-9\.\-_\*&$% ]#i', '', $code);
return preg_replace('#[^A-Za-z0-9\.\-_\*&$% ]#i', '', $code);
}
/**
@@ -523,7 +534,7 @@ class Handler
*
* @return string
*/
public static function getTable()
public static function getTable(): string
{
return QUI::getDBTableName('quiqqer_coupons');
}