Skip to content
Code-Schnipsel Gruppen Projekte

feat!: quiqqer v2

Zusammengeführt Henning Leutz schlägt vor, feat-quiqqer-v2 in dev zu mergen.
5 Dateien
+ 31
0
Änderungen vergleichen
  • Nebeneinander
  • In der Reihe
Dateien
5
@@ -6,7 +6,10 @@ use DateTime;
use Exception;
use QUI;
use QUI\ERP\Discount\Handler as DiscountHandler;
use QUI\ERP\Order\AbstractOrder;
use QUI\ERP\Order\OrderInterface;
use QUI\ExceptionStack;
use QUI\Interfaces\Users\User;
use QUI\Permissions\Permission;
use function array_key_first;
@@ -15,6 +18,7 @@ use function current;
use function in_array;
use function is_array;
use function is_null;
use function is_numeric;
use function json_decode;
use function json_encode;
use function method_exists;
@@ -36,35 +40,35 @@ class CouponCode
*
* @var string
*/
protected $code;
protected mixed $code;
/**
* IDs of users that this CouponCode is restricted to
*
* @var int[]
*/
protected $userIds = [];
protected mixed $userIds = [];
/**
* IDs of groups that this CouponCode is restricted to
*
* @var int[]
*/
protected $groupIds = [];
protected mixed $groupIds = [];
/**
* IDs of all linked discounts
*
* @var int[]
* @var array
*/
protected $discountIds = [];
protected array $discountIds = [];
/**
* List of usages of this CouponCode
*
* @var array
*/
protected $usages = [];
protected mixed $usages = [];
/**
* Creation Date
@@ -83,9 +87,9 @@ class CouponCode
/**
* CouponCode title
*
* @var string
* @var string|null
*/
protected $title = null;
protected ?string $title = null;
/**
* Flag - Is the CouponCode valid?
@@ -99,7 +103,7 @@ class CouponCode
*
* @var string
*/
protected $maxUsages = Handler::MAX_USAGE_ONCE_PER_USER;
protected string $maxUsages = Handler::MAX_USAGE_ONCE_PER_USER;
/**
* CouponCode constructor.
@@ -118,7 +122,7 @@ class CouponCode
'id' => $id
]
]);
} catch (QUI\DataBase\Exception $Exception) {
} catch (QUI\Database\Exception $Exception) {
QUI\System\Log::addError($Exception->getMessage());
throw new CouponCodeException([
@@ -154,10 +158,30 @@ class CouponCode
$this->userIds = json_decode($data['userIds'], true);
}
// migrate user ids
foreach ($this->userIds as $k => $userId) {
if (is_numeric($userId)) {
try {
$this->userIds[$k] = QUI::getUsers()->get($userId)->getUUID();
} catch (QUI\Exception) {
}
}
}
if (!empty($data['groupIds'])) {
$this->groupIds = json_decode($data['groupIds'], true);
}
// migrate user ids
foreach ($this->groupIds as $k => $groupId) {
if (is_numeric($groupId)) {
try {
$this->groupIds[$k] = QUI::getGroups()->get($groupId)->getUUID();
} catch (QUI\Exception) {
}
}
}
if (!empty($data['maxUsages'])) {
$this->maxUsages = $data['maxUsages'];
}
@@ -219,7 +243,7 @@ class CouponCode
}
/**
* @return string
* @return string|null
*/
public function getTitle(): ?string
{
@@ -276,13 +300,14 @@ class CouponCode
*
* Hint: This may invalidate the code for future use
*
* @param QUI\Interfaces\Users\User $User - The user that redeems the CouponCode [if omitted use Session User]
* @param QUI\ERP\Order\Order|null $Order (optional) - Link redemption to a specific Order
* @param User|null $User - The user that redeems the CouponCode [if omitted use Session User]
* @param AbstractOrder|null $Order (optional) - Link redemption to a specific Order
* @return void
* @throws CouponCodeException
* @throws QUI\Exception
* @throws QUI\Database\Exception
* @throws ExceptionStack
*/
public function redeem(QUI\Interfaces\Users\User $User = null, QUI\ERP\Order\Order $Order = null)
public function redeem(QUI\Interfaces\Users\User $User = null, AbstractOrder $Order = null): void
{
if (is_null($User)) {
$User = QUI::getUserBySession();
@@ -293,13 +318,13 @@ class CouponCode
$Now = new DateTime();
$usage = [
'userId' => $User->getId(),
'userId' => $User->getUUID(),
'date' => $Now->format('Y-m-d H:i:s'),
'orderPrefixedId' => false
];
if ($Order instanceof QUI\ERP\Order\Order) {
$usage['orderPrefixedId'] = $Order->getPrefixedId();
$usage['orderPrefixedId'] = $Order->getPrefixedNumber();
}
$this->usages[] = $usage;
@@ -328,7 +353,7 @@ class CouponCode
* @return void
* @throws CouponCodeException - Thrown if not redeemable by the given User
*/
public function checkRedemption(QUI\Interfaces\Users\User $User)
public function checkRedemption(QUI\Interfaces\Users\User $User): void
{
if (!$this->isValid()) {
throw new CouponCodeException([
@@ -396,7 +421,7 @@ class CouponCode
// Restriction to QUIQQER user(s)
if (!empty($this->userIds)) {
if (in_array($User->getId(), $this->userIds)) {
if (in_array($User->getUUID(), $this->userIds)) {
if (
$this->maxUsages !== Handler::MAX_USAGE_UNLIMITED
&& $this->hasUserRedeemed($User)
@@ -450,7 +475,7 @@ class CouponCode
* @param OrderInterface|null $Order
* @throws CouponCodeException
*/
public function checkOrderRedemption(?OrderInterface $Order)
public function checkOrderRedemption(?OrderInterface $Order): void
{
if ($Order === null) {
return;
@@ -496,7 +521,7 @@ class CouponCode
/**
* Check if the given User can redeem this CouponCode
*
* @param QUI\Interfaces\Users\User $User - If omitted, use session user
* @param User|null $User - If omitted, use session user
* @param OrderInterface|null $Order
* @return bool
*/
@@ -504,7 +529,7 @@ class CouponCode
{
try {
$this->checkRedemption($User);
} catch (CouponCodeException $Exception) {
} catch (CouponCodeException) {
return false;
}
@@ -539,7 +564,7 @@ class CouponCode
*/
public function hasUserRedeemed(QUI\Interfaces\Users\User $User): bool
{
$userId = $User->getId();
$userId = $User->getUUID();
foreach ($this->usages as $usage) {
if ($usage['userId'] === $userId) {
@@ -556,7 +581,7 @@ class CouponCode
* @return void
* @throws QUI\Permissions\Exception
*/
public function delete()
public function delete(): void
{
Permission::checkPermission(Handler::PERMISSION_DELETE);
@@ -565,11 +590,11 @@ class CouponCode
Handler::getTable(),
['id' => $this->id]
);
} catch (QUI\DataBase\Exception $Exception) {
} catch (QUI\Database\Exception $Exception) {
QUI\System\Log::addError($Exception->getMessage());
}
// If hidden discount are connected to this coupon -> delete them aswell
// If hidden discount are connected to this coupon -> delete them as well
foreach ($this->getDiscounts() as $Discount) {
if (!empty($Discount->getAttribute('hidden'))) {
try {
@@ -616,7 +641,7 @@ class CouponCode
*
* @return void
*/
protected function checkValidity()
protected function checkValidity(): void
{
// Check if the expiration date has been reached
if (!empty($this->ValidUntilDate)) {
@@ -665,7 +690,7 @@ class CouponCode
* @param QUI\ERP\Order\OrderInProcess $Order
* @throws QUI\Exception
*/
public function addToOrder(QUI\ERP\Order\OrderInProcess $Order)
public function addToOrder(QUI\ERP\Order\OrderInProcess $Order): void
{
$coupons = $Order->getDataEntry('quiqqer-coupons');
@@ -691,7 +716,7 @@ class CouponCode
/* @var $Coupon CouponCode */
try {
$Coupon = Handler::getCouponCodeByCode($coupon);
} catch (Exception $Exception) {
} catch (Exception) {
continue;
}
@@ -721,7 +746,7 @@ class CouponCode
// @todo wenn fest preis (zb 10$), dann eigener produkt typ hinzufügen
$articles[] = new QUI\ERP\Accounting\Articles\Text([
'id' => '-',
'id' => -1,
'articleNo' => $Coupon->getCode(),
'title' => $PriceFactor->getTitle(),
'description' => '',