VoucherGenerator
in package
Generates the compensation voucher when an order moves to a status with `OrderStatus::$generateVoucher=1` (currently done).
Called from OrderTransitionManager fail-soft: a missing level or a database error never blocks the transition, it only returns a warning.
Value and validity are data-driven, not hardcoded: VoucherManager::resolveLevel() picks the level
by currency and order.roundedTotalPriceWithVat. A level is either/or: percents > 0 creates a
percent voucher (hasPercents=true), a filled fixedAmount creates a fixed-amount voucher
(hasPercents=false, amount in VoucherPrice::$price in the order currency, same as manually
created vouchers, VoucherFormTrait::buildVoucherPrices()).
IMPORTANT: a VoucherPrice row for the order currency (no cart limits) is created in both cases;
without it VoucherRedemptionService::applyVouchers() rejects the voucher in that currency as invalid.
The voucher is always single-use (maxNumber=1) and valid for duration days from generation.
When no level exists, generation is rejected with voucherLevelNotFound; no fallback value is invented.
Tags
Table of Contents
Constants
- CODE_RANDOM_BYTES : int = 5
- MAX_CODE_ATTEMPTS : int = 5
- VOUCHERS_TABLE : string = 'cms_mod_eshop_vouchers'
Properties
- $em : EntityManagerInterface
- $voucherManager : VoucherManager
Methods
- __construct() : mixed
- generateForOrder() : Result
- generateUniqueCode() : string|null
- Retry loop against the unique `code` index; a collision of a random 10-character hex is practically impossible.
Constants
CODE_RANDOM_BYTES
private
int
CODE_RANDOM_BYTES
= 5
MAX_CODE_ATTEMPTS
private
int
MAX_CODE_ATTEMPTS
= 5
VOUCHERS_TABLE
private
string
VOUCHERS_TABLE
= 'cms_mod_eshop_vouchers'
Properties
$em read-only
private
EntityManagerInterface
$em
$voucherManager read-only
private
VoucherManager
$voucherManager
Methods
__construct()
public
__construct(VoucherManager $voucherManager, EntityManagerInterface $em) : mixed
Parameters
- $voucherManager : VoucherManager
- $em : EntityManagerInterface
generateForOrder()
public
generateForOrder(TOrderGraph $order) : Result
Parameters
- $order : TOrderGraph
Return values
Result —success data: ['voucher' => Voucher, 'level' => array{voucherLevelId: int, durationDays: int, minPrice: string, maxPrice: string, percents: int, fixedAmount: ?string}];
failure errorCode: voucherLevelNotFound (no level for the currency/amount, fail-soft,
not a technical error) · voucherCodeGenerationFailed · a save failure of VoucherManager.
generateUniqueCode()
Retry loop against the unique `code` index; a collision of a random 10-character hex is practically impossible.
private
generateUniqueCode() : string|null