AMS_Backend/app/Helpers/promo_helper.php
2025-11-06 13:41:06 +08:00

82 lines
2.3 KiB
PHP

<?php
if(!function_exists('revertPromoSetting')){
function revertPromoSetting(array $promoSetting): array
{
$output = [
'minimumSpend' => null,
'minimumQuantity' => null,
'everyQuantity' => null,
'itemCategory1' => null,
'itemCategoryID1' => [],
'discountAmount' => null,
'discountType' => null,
'getNumber' => null,
'itemCategory2' => null,
'itemCategoryID2' => [],
'promoType' => null,
];
// --- MinimumSpend ---
$min = $promoSetting['MinimumSpend'] ?? [];
$type = $min['type'] ?? 'none';
$amountType = $min['amount_type'] ?? '';
$amount = $min['amount'] ?? 0;
if ($type !== 'none') {
$output['itemCategory1'] = $type;
$output['itemCategoryID1'] = $min['filter'] ?? [];
if ($amountType === 'amount') {
$output['minimumSpend'] = $amount;
} elseif ($amountType === 'quantity') {
$output['minimumQuantity'] = $amount;
} elseif ($amountType === 'every_quantity') {
$output['everyQuantity'] = $amount;
}
}
// --- Promo ---
$promo = $promoSetting['Promo'] ?? [];
$promoType = $promo['promo_type'] ?? '';
$output['promoType'] = $promoType;
$output['itemCategory2'] = $promo['filter_type'] ?? null;
if ($promoType === 'free_item') {
$output['itemCategoryID2'] = $promo['free_item'] ?? [];
$output['getNumber'] = $promo['amount'] ?? null;
} else {
$output['itemCategoryID2'] = $promo['filter'] ?? [];
$output['discountType'] = $promo['discount_type'] ?? null;
// Determine amount source
if ($promo['discount_type'] === 'amount' || $promo['discount_type'] === 'percentage') {
$output['discountAmount'] = $promo['amount'] ?? null;
} else {
$output['getNumber'] = $promo['amount'] ?? null;
}
}
return $output;
}
}
if(!function_exists('calculatePromoPrice')){
function calculatePromoPrice($price, $amount, $type){
if ($price < 0 || $amount < 0) {
return 0;
}
if($type == 'amount'){
return $amount;
} else if ($type == 'percentage'){
return $price * $amount / 100;
} else {
return 0;
}
}
}
?>