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';
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',
[
} else {
$code = CodeGenerator::generate();
}
$couponCode = [
'title' => empty($settings['title']) ? null : $settings['title'],
'createDate' => $Now->format('Y-m-d H:i:s'),
'code' => $code,
'isReusable' => empty($settings['reusable']) ? 0 : 1,
if (!empty($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($settings['userIds']);
if (!empty($settings['groupIds'])) {
$couponCode['groupIds'] = json_encode($settings['groupIds']);
}
QUI::getDataBase()->insert(
self::getTable(),
167
168
169
170
171
172
173
174
175
176
177
178
179
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
/**
* 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();
}
$couponCode = [
'title' => empty($settings['title']) ? null : $settings['title'],
'createDate' => $Now->format('Y-m-d H:i:s'),
'code' => $code,
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
'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($settings['userIds']);
} else {
$couponCode['userIds'] = null;
}
if (!empty($settings['groupIds'])) {
$couponCode['groupIds'] = json_encode($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()
{