Newer
Older
<?php
namespace QUI\ERP\Coupons;
use QUI;
use QUI\Permissions\Permission;
/**
*
* @var int
*/
protected $id;
/**
* Actual code
*
* @var string
*/
protected $code;
/**
*
* @var \DateTime
*/
protected $ValidUntilDate = null;
/**
throw new CouponCodeException([
'quiqqer/coupons',
'exception.CouponCode.not_found',
[
$this->id = (int)$data['id'];
$this->code = $data['code'];
$this->title = $data['title'];
if (!empty($data['usages'])) {
$this->usages = json_decode($data['usages'], true);
if (!empty($data['userIds'])) {
$this->userIds = json_decode($data['userIds'], true);
}
if (!empty($data['groupIds'])) {
$this->groupIds = json_decode($data['groupIds'], true);
$this->CreateDate = new \DateTime($data['createDate']);
if (!empty($data['validUntilDate'])) {
$this->ValidUntilDate = new \DateTime($data['validUntilDate']);
}
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getCode()
{
return $this->code;
}
/**
* @return \DateTime
*/
public function getCreateDate()
{
return $this->CreateDate;
}
/**
}
/**
* @return \DateTime|null
*/
public function getValidUntilDate()
{
return $this->ValidUntilDate;
}
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Redeems this CouponCode
*
* Hint: This may invalidate the code for future use
*
* @param QUI\Users\User $User - The user that redeems the CouponCode [if omitted use Session User]
* @return void
* @throws CouponCodeException
if (is_null($User)) {
$User = QUI::getUserBySession();
}
$this->checkRedemption($User);
$Now = new \DateTime();
$this->usages[] = [
'userId' => $User->getId(),
'date' => $Now->format('Y-m-d H:i:s')
];
QUI::getDataBase()->update(
Handler::getTable(),
[
'usages' => json_encode($this->usages)
],
[
'id' => $this->id
]
);
$this->checkValidity();
* @param QUI\Users\User $User - If omitted, use session user
* @throws CouponCodeException - Thrown if not redeemable by the given User
if (!$this->isValid()) {
throw new CouponCodeException([
'quiqqer/coupons',
'exception.CouponCode.no_longer_valid'
]);
if (!empty($this->userIds)) {
if (in_array($User->getId(), $this->userIds)) {
if (!$this->reusable && $this->hasUserRedeemed($User)) {
throw new CouponCodeException([
'quiqqer/coupons',
'exception.CouponCode.already_used'
]);
}
} else {
throw new CouponCodeException([
'quiqqer/coupons',
'exception.CouponCode.user_not_allowed'
]);
}
foreach ($this->groupIds as $groupId) {
if ($User->isInGroup($groupId)) {
$userInGroup = true;
break;
}
}
if ($userInGroup) {
if (!$this->reusable && $this->hasUserRedeemed($User)) {
throw new CouponCodeException([
'quiqqer/coupons',
'exception.CouponCode.already_used'
]);
}
} else {
throw new CouponCodeException([
'quiqqer/coupons',
'exception.CouponCode.user_not_allowed_group'
]);
}
}
* @param QUI\Users\User $User - If omitted, use session user
try {
$this->checkRedemption($User);
} catch (CouponCodeException $Exception) {
return false;
}
return true;
foreach ($this->usages as $usage) {
if ($usage['userId'] === $userId) {
return true;
}
*
* @return void
*/
public function delete()
{
Permission::checkPermission(Handler::PERMISSION_DELETE);
QUI::getDataBase()->delete(
Handler::getTable(),
*
* @return array
*/
public function toArray()
{
'userIds' => $this->userIds,
'groupIds' => $this->groupIds,
'createDate' => $this->getCreateDate()->format('Y-m-d H:i:s'),
'validUntilDate' => false,
'title' => $this->getTitle() ?: false,
'isValid' => $this->isValid(),
'isReusable' => $this->reusable
];
if ($ValidUntilDate) {
$data['validUntilDate'] = $ValidUntilDate->format('Y-m-d H:i:s');
/**
* Checks if this CouponCode is still valid
*
* @return void
*/
protected function checkValidity()
{
// Check if the expiration date has been reached
if (!empty($this->ValidUntilDate)) {
$Now = new \DateTime();
if ($Now > $this->ValidUntilDate) {
$this->valid = false;
return;
}
}
// If the CouponCode is restricted to certain users -> Check if all those
// users have already redeemed the code
if (!empty($this->userIds)) {
$usedByAllUsers = true;
foreach ($this->userIds as $userId) {
foreach ($this->usages as $usage) {
if ($userId == $usage['userId']) {
continue 2;
}
}
$usedByAllUsers = false;
break;
}
if ($usedByAllUsers) {
$this->valid = false;
return;
}
}