114 lines
3.5 KiB
PHP
114 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace KupShop\QuantityDiscountBundle\Util;
|
|
|
|
use KupShop\KupShopBundle\Context\CurrencyContext;
|
|
use KupShop\KupShopBundle\Util\Contexts;
|
|
use Query\Operator;
|
|
|
|
class QuantityDiscountUtil
|
|
{
|
|
public const DISCOUNT_TYPE_PERC = 'perc';
|
|
/** @deprecated Use currency id (CZK, EUR...) instead of `amount` keyword */
|
|
public const DISCOUNT_TYPE_AMOUNT = 'amount';
|
|
|
|
public function getDiscountTypes(): array
|
|
{
|
|
$types = [self::DISCOUNT_TYPE_PERC => '%'];
|
|
|
|
$currencyContext = Contexts::get(CurrencyContext::class);
|
|
|
|
foreach ($currencyContext->getAll() as $currency) {
|
|
$types[$currency->getId()] = $currency->getId();
|
|
}
|
|
|
|
return $types;
|
|
}
|
|
|
|
public function addQuantityDiscount(int $productId, ?int $variationId, int $pieces, \Decimal $discount, ?int $groupId = null, ?string $type = self::DISCOUNT_TYPE_PERC): bool
|
|
{
|
|
// backward compatibility: remove me when TYPE_AMOUNT is not used
|
|
if ($type === self::DISCOUNT_TYPE_AMOUNT) {
|
|
$type = Contexts::get(CurrencyContext::class)->getDefaultId();
|
|
}
|
|
|
|
$id = sqlQueryBuilder()
|
|
->select('id')
|
|
->from('products_quantity_discounts')
|
|
->where(
|
|
Operator::equalsNullable(
|
|
[
|
|
'id_group' => $groupId,
|
|
'id_product' => $productId,
|
|
'id_variation' => $variationId,
|
|
'pieces' => $pieces,
|
|
'discount_type' => $type,
|
|
]
|
|
)
|
|
)->execute()->fetchOne();
|
|
|
|
if ($id) {
|
|
sqlQueryBuilder()
|
|
->update('products_quantity_discounts')
|
|
->directValues(
|
|
[
|
|
'discount' => $discount,
|
|
]
|
|
)
|
|
->where(Operator::equals(['id' => $id]))
|
|
->execute();
|
|
} else {
|
|
sqlQueryBuilder()
|
|
->insert('products_quantity_discounts')
|
|
->directValues(
|
|
[
|
|
'id_group' => $groupId,
|
|
'id_product' => $productId,
|
|
'id_variation' => $variationId,
|
|
'pieces' => $pieces,
|
|
'discount' => $discount,
|
|
'discount_type' => $type,
|
|
]
|
|
)->execute();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function deleteQuantityDiscount(int $productId, ?int $variationId, int $pieces, ?int $groupId = null): void
|
|
{
|
|
sqlQueryBuilder()
|
|
->delete('products_quantity_discounts')
|
|
->where(
|
|
Operator::equalsNullable(
|
|
[
|
|
'id_group' => $groupId,
|
|
'id_product' => $productId,
|
|
'id_variation' => $variationId,
|
|
'pieces' => $pieces,
|
|
]
|
|
)
|
|
)->execute();
|
|
}
|
|
|
|
public function getGroups(): array
|
|
{
|
|
// default group
|
|
$groups = [
|
|
0 => $this->getDefaultGroupName(),
|
|
];
|
|
|
|
// additional groups
|
|
if ($additionalGroups = findModule(\Modules::QUANTITY_DISCOUNT, 'groups', [])) {
|
|
$groups = array_merge($groups, $additionalGroups);
|
|
}
|
|
|
|
return $groups;
|
|
}
|
|
|
|
public function getDefaultGroupName(): string
|
|
{
|
|
return 'E-Shop';
|
|
}
|
|
}
|