CoolCMS API

VoucherFormTrait

Add/edit form of a voucher: code (generated by a client-side button), validity, limits and per-currency thresholds and fixed discounts ({@see VoucherPrice}).

IMPORTANT: currentNumber is not a form field. The entity carries it (ExternalReadonly); the trait never reads it from $values nor calls setCurrentNumber(), and the value loaded from the database is sent back unchanged. The template shows it as info only.

IMPORTANT: VoucherPrice is not a time series. The primary key is (voucher, currencyId), so the row of a currency is overwritten (upsert), not appended. A currency with no value filled (min, max and fixed discount all empty) gets no row, so the voucher is not valid in that currency; the owned orphanRemoval collection deletes the existing row, which is intended (see Voucher::getPriceForCurrency()).

Table of Contents

Constants

VOUCHER_DT_FORMAT  : mixed = 'Y-m-d\TH:i'
Value format of `<input type=datetime-local>`.

Methods

createComponentEditForm()  : Form
addVoucherBaseFields()  : void
addVoucherPriceFields()  : void
Adds one row (cart min/max and fixed discount) per currency; the axis is fixed by {@see \App\UI\Admin\Eshop\Presenters\VoucherPresenter::$voucherCurrencies}.
buildVoucherEntity()  : Voucher
buildVoucherPrices()  : ArrayCollection<int, VoucherPrice>
Upserts the price/threshold rows per currency (not a time series, see the trait docblock).
decimalOrNull()  : float|null
Converts an entity value to a `float` for prefilling the form.
loadVoucherSelectData()  : void
Loads the form axes (system currencies) once per request; `CurrencyManager::findAll()` is deliberately not filtered to active ones.
saveVoucherForm()  : bool
setVoucherFormDefaults()  : void

Constants

VOUCHER_DT_FORMAT

Value format of `<input type=datetime-local>`.

private mixed VOUCHER_DT_FORMAT = 'Y-m-d\TH:i'

Methods

addVoucherPriceFields()

Adds one row (cart min/max and fixed discount) per currency; the axis is fixed by {@see \App\UI\Admin\Eshop\Presenters\VoucherPresenter::$voucherCurrencies}.

private addVoucherPriceFields(Form $form) : void
Parameters
$form : Form

buildVoucherEntity()

private buildVoucherEntity(ArrayHash<string|int, mixed> $values) : Voucher
Parameters
$values : ArrayHash<string|int, mixed>
Return values
Voucher

buildVoucherPrices()

Upserts the price/threshold rows per currency (not a time series, see the trait docblock).

private buildVoucherPrices(ArrayHash<string|int, mixed> $values, Voucher $voucher) : ArrayCollection<int, VoucherPrice>

A currency with no input is left out of the collection; the owned orphanRemoval then deletes its existing row, which detaches the currency from the voucher.

Parameters
$values : ArrayHash<string|int, mixed>
$voucher : Voucher
Return values
ArrayCollection<int, VoucherPrice>

decimalOrNull()

Converts an entity value to a `float` for prefilling the form.

private decimalOrNull(string|null $value) : float|null

NOTE: this is not a duplicate of Decimal::fromInput(); that helper parses user input (comma, thousands spaces) into a numeric-string, while this goes the other way, from an entity value to a float for the prefill.

IMPORTANT: never convert 0.0 to null. A currency row whose only filled value is minPrice = 0 would show three empty fields on edit, and a re-save without changes would silently delete it through orphanRemoval, so the voucher would stop being valid in that currency. A zero in the input is the lesser evil.

Parameters
$value : string|null
Return values
float|null

loadVoucherSelectData()

Loads the form axes (system currencies) once per request; `CurrencyManager::findAll()` is deliberately not filtered to active ones.

private loadVoucherSelectData() : void

saveVoucherForm()

private saveVoucherForm(ArrayHash<string|int, mixed> $values[, Form|null $form = null ]) : bool
Parameters
$values : ArrayHash<string|int, mixed>
$form : Form|null = null
Return values
bool
On this page

Search results