Newer
Older
<?php
namespace QUI\ERP\Coupons;
use QUI;
use QUI\Utils\Grid;
use QUI\Utils\Security\Orthos;
use QUI\ERP\Discount\Handler as DiscountHandler;
/**
* Class Handler
*
* Main CouponCode Code handler
*/
class Handler
{
/**
* Permissions
*/
const PERMISSION_VIEW = 'quiqqer.couponcode.view';
const PERMISSION_EDIT = 'quiqqer.couponcode.edit';
/**
* Valur for `maxUsage`
*/
const MAX_USAGE_ONCE_PER_USER = 'oncePerUser';
const MAX_USAGE_ONCE = 'once';
const MAX_USAGE_UNLIMITED = 'unlimited';
if (isset(self::$couponCodes[$id])) {
return self::$couponCodes[$id];
* @param array $discountIds - IDs of the discounts that are linked to this CouponCode
* @param array $settings (optional) - If omitted a random default CouponCode is generated
public static function createCouponCode($discountIds, $settings = [])
$DiscountHandler = DiscountHandler::getInstance();
if (empty($discountIds)) {
throw new CouponCodeException([
'quiqqer/coupons',
'exception.Handler.no_discounts_linked'
]);
}
// check if all given discounts exist
foreach ($discountIds as $discountId) {
try {
$DiscountHandler->getChild($discountId);
} catch (\Exception $Exception) {
throw new CouponCodeException([
'quiqqer/coupons',
'exception.Handler.discount_error',
[
'discountId' => $discountId,
'error' => $Exception->getMessage()
]
]);
}
}
if (!empty($settings['code'])) {
if (self::existsCode($settings['code'])) {
throw new CouponCodeException([
'quiqqer/coupons',
'exception.Handler.code_already_exists',
[
if (empty($settings['maxUsages'])) {
$maxUsages = self::MAX_USAGE_ONCE_PER_USER;
} else {
$maxUsages = $settings['maxUsages'];
}
'title' => empty($settings['title']) ? null : $settings['title'],
'createDate' => $Now->format('Y-m-d H:i:s'),
'code' => $code,
if (!empty($settings['validUntilDate'])) {
$ValidUntil = new \DateTime($settings['validUntilDate']);
$couponCode['validUntilDate'] = $ValidUntil->format('Y-m-d H:i:s');
$couponCode['userIds'] = json_encode(explode(",", $settings['userIds']));
$couponCode['groupIds'] = json_encode(explode(",", $settings['groupIds']));
}
QUI::getDataBase()->insert(
self::getTable(),
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
* Edit a CouponCode
*
* @param int $id - CouponCode ID
* @param array $discountIds - IDs of the discounts that are linked to this CouponCode
* @param array $settings (optional) - If omitted a random default CouponCode is generated
* @return CouponCode
*
* @throws \Exception
*/
public static function editCouponCode($id, $discountIds, $settings = [])
{
QUI\Permissions\Permission::checkPermission(self::PERMISSION_EDIT);
// check if CouponCode exists
$CouponCode = self::getCouponCode($id);
// Check settings
$DiscountHandler = DiscountHandler::getInstance();
if (empty($discountIds)) {
throw new CouponCodeException([
'quiqqer/coupons',
'exception.Handler.no_discounts_linked'
]);
}
// check if all given discounts exist
foreach ($discountIds as $discountId) {
try {
$DiscountHandler->getChild($discountId);
} catch (\Exception $Exception) {
throw new CouponCodeException([
'quiqqer/coupons',
'exception.Handler.discount_error',
[
'discountId' => $discountId,
'error' => $Exception->getMessage()
]
]);
}
}
$Now = new \DateTime();
if (!empty($settings['code'])) {
if ($CouponCode->getCode() !== $settings['code']
&& self::existsCode($settings['code'])) {
throw new CouponCodeException([
'quiqqer/coupons',
'exception.Handler.code_already_exists',
[
'code' => $settings['code']
]
]);
}
} else {
$code = CodeGenerator::generate();
}
if (empty($settings['maxUsages'])) {
$maxUsages = self::MAX_USAGE_ONCE_PER_USER;
} else {
$maxUsages = $settings['maxUsages'];
}
$couponCode = [
'title' => empty($settings['title']) ? null : $settings['title'],
'createDate' => $Now->format('Y-m-d H:i:s'),
'code' => $code,
'discountIds' => json_encode($discountIds)
];
if (!empty($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']));
} else {
$couponCode['userIds'] = null;
}
if (!empty($settings['groupIds'])) {
$couponCode['groupIds'] = json_encode(explode(",", $settings['groupIds']));
} else {
$couponCode['groupIds'] = null;
}
QUI::getDataBase()->update(
self::getTable(),
$couponCode,
[
'id' => $id
]
);
return self::getCouponCode($id);
}
*
* @param array $searchParams
* @param bool $countOnly (optional) - get result count only [default: false]
*/
public static function search($searchParams, $countOnly = false)
{
$Grid = new Grid($searchParams);
$gridParams = $Grid->parseDBParams($searchParams);
if ($countOnly) {
$sql = "SELECT COUNT(*)";
} else {
$sql = "SELECT id";
}
$binds['search'] = [
'value' => '%'.$searchParams['search'].'%',
}
}
// build WHERE query string
if (!empty($where)) {
}
// ORDER
if (!empty($searchParams['sortOn'])
) {
$sortOn = Orthos::clear($searchParams['sortOn']);
if (isset($searchParams['sortBy']) &&
!empty($searchParams['sortBy'])
) {
} else {
$sql .= " ORDER BY id DESC";
}
// LIMIT
if (!empty($gridParams['limit'])
&& !$countOnly
) {
}
}
$Stmt = QUI::getPDO()->prepare($sql);
// bind search values
foreach ($binds as $var => $bind) {
}
try {
$Stmt->execute();
$result = $Stmt->fetchAll(\PDO::FETCH_ASSOC);
} catch (\Exception $Exception) {
QUI\System\Log::addError(
}
if ($countOnly) {
return (int)current(current($result));
}
foreach ($result as $row) {
}
/**
* Check if an invite code already eixsts
*
* @param string $code
* @return bool
*/
public static function existsCode($code)
{
return !empty($result);
}
/**
* Get Registration site
*
* @return QUI\Projects\Site|false
*/
public static function getRegistrationSite()
{
try {
$Conf = QUI::getPackage('quiqqer/coupons')->getConfig();
$regSite = $Conf->get('settings', 'registrationSite');
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
return false;
}
if (empty($regSite)) {
return false;
}
try {
return QUI\Projects\Site\Utils::getSiteByLink($regSite);
} catch (\Exception $Exception) {
return false;
}
}
/**
*
* @param int $days (optional) - Delete expired Codes that are older than X days [default: delete all]
* @return void
*
* @throws \Exception
*/
public static function deleteExpiredCouponCodes($days = null)
if (!is_null($days)) {
$days = (int)$days;
$OldDate = new \DateTime();
'type' => '<=',
'value' => $OldDate->format('Y-m-d H:i:s')
}
QUI::getDataBase()->delete(
self::getTable(),
$where
);
}
/**
*
* @param int $days (optional) - Delete redeemed Codes that are older than X days [default: delete all]
* @return void
*
* @throws \Exception
*/
public static function deleteRedeemedCouponCodes($days = null)
if (!is_null($days)) {
$days = (int)$days;
$OldDate = new \DateTime();
'type' => '<=',
'value' => $OldDate->format('Y-m-d H:i:s')
}
QUI::getDataBase()->delete(
self::getTable(),
$where
);
}
/**
* Sanitize coupon code and allow only certain characters
*
* @param string $code
* @return string
*/
public static function sanitizeCode($code)
{
return preg_replace('#[^A-Za-z0-9\.\-_\*&$% ]#i', '', $code);
}
*
* @return string
*/
public static function getTable()
{