90 lines
2.9 KiB
PHP
90 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\ProductReservationBundle\Util;
|
|
|
|
use Query\Operator;
|
|
|
|
class ProductReservationUtil
|
|
{
|
|
/** System reservation types */
|
|
public const TYPE_RESERVATION = 'reservation';
|
|
|
|
/** Manual reservation types */
|
|
public const TYPE_RETAIL_RESERVE = 'retail_reserve';
|
|
|
|
public static function getReservationTypes(): array
|
|
{
|
|
$types = [
|
|
self::TYPE_RETAIL_RESERVE => 'Maloobchodní rezerva',
|
|
];
|
|
|
|
if (findModule(\Modules::RESERVATIONS)) {
|
|
$types[self::TYPE_RESERVATION] = 'Rezervace';
|
|
}
|
|
|
|
return $types;
|
|
}
|
|
|
|
public function createProductReservation(int $productId, ?int $variationId, float $quantity, string $type): int
|
|
{
|
|
if (!isset(static::getReservationTypes()[$type])) {
|
|
throw new \InvalidArgumentException(
|
|
sprintf('Unknown product reservation type "%s"!', $type)
|
|
);
|
|
}
|
|
|
|
return sqlGetConnection()->transactional(function () use ($productId, $variationId, $quantity, $type) {
|
|
sqlQueryBuilder()
|
|
->insert('product_reservations')
|
|
->directValues(
|
|
[
|
|
'type' => $type,
|
|
'id_product' => $productId,
|
|
'id_variation' => $variationId,
|
|
'quantity' => $quantity,
|
|
]
|
|
)->execute();
|
|
|
|
return (int) sqlInsertId();
|
|
});
|
|
}
|
|
|
|
public function cancelProductReservation(int $productReservationId): bool
|
|
{
|
|
return sqlGetConnection()->transactional(function () use ($productReservationId) {
|
|
$item = sqlQueryBuilder()
|
|
->select('id_product, id_variation, quantity')
|
|
->from('product_reservations')
|
|
->where(Operator::equals(['id' => $productReservationId]))
|
|
->execute()->fetchAssociative();
|
|
|
|
if (!$item) {
|
|
throw new \RuntimeException('Trying to cancel non-existing product reservation!');
|
|
}
|
|
|
|
// prictu sklad zpatky k produktu
|
|
$product = new \Product($item['id_product']);
|
|
$product->storeIn($item['id_variation'], $item['quantity']);
|
|
|
|
// smazu zaznam se skladovou rezervaci
|
|
sqlQueryBuilder()
|
|
->delete('product_reservations')
|
|
->where(Operator::equals(['id' => $productReservationId]))
|
|
->execute();
|
|
|
|
return true;
|
|
});
|
|
}
|
|
|
|
public function getProductReservedQuantity(int $productId, ?int $variationId = null): float
|
|
{
|
|
return (float) sqlQueryBuilder()
|
|
->select('COALESCE(SUM(quantity), 0)')
|
|
->from('product_reservations')
|
|
->where(Operator::equalsNullable(['id_product' => $productId, 'id_variation' => $variationId]))
|
|
->execute()->fetchOne();
|
|
}
|
|
}
|