Files
kupshop/bundles/KupShop/AgeVerifyBundle/View/PackageOrderView.php
2025-08-02 16:30:27 +02:00

291 lines
10 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\AgeVerifyBundle\View;
use KupShop\AgeVerifyBundle\Exception\AgeVerificationException;
use KupShop\AgeVerifyBundle\Utils\AgeVerifyUtil;
use KupShop\KupShopBundle\Context\ContextManager;
use KupShop\KupShopBundle\Context\UserContext;
use KupShop\KupShopBundle\Exception\RedirectException;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\KupShopBundle\Util\Price\Price;
use KupShop\KupShopBundle\Util\Price\PriceCalculator;
use KupShop\KupShopBundle\Views\View;
use KupShop\KupShopBundle\Wrapper\PriceWrapper;
use KupShop\OrderingBundle\Entity\Purchase\ProductPurchaseItem;
use KupShop\OrderingBundle\Entity\Purchase\PurchaseState;
use KupShop\OrderingBundle\Event\OrderEvent;
use KupShop\OrderingBundle\Util\Purchase\PurchaseUtil;
use Query\Operator;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Contracts\Service\Attribute\Required;
class PackageOrderView extends View
{
protected $template = 'ageverify/packageOrder.tpl';
protected $title = 'Ověření přes zásilkovnu';
#[Required]
public ContextManager $contextManager;
#[Required]
public UserContext $userContext;
#[Required]
public RequestStack $requestStack;
#[Required]
public AgeVerifyUtil $ageVerifyUtil;
#[Required]
public PurchaseUtil $purchaseUtil;
#[Required]
public EventDispatcherInterface $eventDispatcher;
protected array $deliveries = [];
protected array $payments = [];
protected $id_delivery;
protected $id_payment;
protected ?Price $product_price = null;
public function getBreadcrumbs(): ?array
{
return getReturnNavigation(-1, 'USER', [$this->getTitle()]);
}
public function getBodyVariables(): array
{
$vars = parent::getBodyVariables();
$vars['user'] = $this->userContext->getActive();
$mainRequest = $this->requestStack->getMainRequest();
$this->id_delivery = $mainRequest->get('delivery_id') ?? false;
$this->id_payment = $mainRequest->get('payment_id') ?? false;
$vars['deliveries'] = $this->getDeliveries();
$vars['payments'] = $this->getPayments();
if ($this->id_delivery) {
$vars['deliveries'][$this->id_delivery]->storeDeliveryInfo(getVal($this->id_delivery, $mainRequest->get('delivery_data')));
}
$vars['payment_id'] = $this->id_payment;
$vars['delivery_id'] = $this->id_delivery;
return $vars;
}
public function submitForm()
{
$user = $this->userContext->getActive();
$userId = $user->id;
$userVerification = sqlQueryBuilder()->select('*')->from('users_age_verification')->where(Operator::equals(['id_user' => $userId]))
->execute()->fetchAssociative();
$mainRequest = $this->requestStack->getMainRequest();
if ($mainRequest->get('submitOrder') === null) {
return false;
}
$data = $mainRequest->get('data');
$id_delivery = $mainRequest->get('delivery_id');
$id_payment = explode('-', $mainRequest->get('payment_id', ''))[0] ?? false;
if ($data['email'] ?? false) {
$data['name'] ??= $user->invoice['name'] ?? '';
$data['surname'] ??= $user->invoice['surname'] ?? '';
}
$order = null;
$id_product = \Settings::getDefault()['age_verify']['package']['product'];
if ($userVerification === false || (empty($userVerification['id_order']) && $userVerification['legal_age'] != 'Y')) {
$deliveryType = $this->getDeliveryType($id_delivery, $id_payment);
if (!$deliveryType) {
return;
}
$purchaseState = $this->createPurchaseState();
$product = new \Product($id_product);
$product->createFromDB();
$purchaseItem = new ProductPurchaseItem(
$id_product,
null,
1,
PriceCalculator::toPrice($product->getProductPrice())
);
$purchaseItem->setProduct($product);
$purchaseState->addProduct($purchaseItem)
->setDeliveryTypeId($deliveryType->id);
$delivery_data = $mainRequest->get('delivery_data');
$deliveryInfo = [];
if ($delivery_data[$id_delivery] ?? false) {
$deliveryInfo = $purchaseState->getDeliveryType()->getDelivery()->storeDeliveryInfo($delivery_data[$id_delivery]);
}
$purchaseState->setCustomData([
'order' => [
'id_user' => $user->id,
'invoice_name' => $user->invoice['name'] ?? '',
'invoice_surname' => $user->invoice['surname'] ?? '',
'invoice_firm' => $user->invoice['firm'] ?? '',
'invoice_ico' => $user->invoice['ico'] ?? '',
'invoice_dic' => $user->invoice['dic'] ?? '',
'invoice_street' => $user->invoice['street'] ?? '',
'invoice_city' => $user->invoice['city'] ?? '',
'invoice_zip' => $user->invoice['zip'] ?? '',
'invoice_country' => $user->invoice['country'] ?? '',
'invoice_phone' => $user->invoice['phone'] ?? '',
'invoice_email' => $user->invoice['email'] ?? '',
'invoice_state' => $user->invoice['state'] ?? '',
'delivery_name' => $data['name'] ?? '',
'delivery_surname' => $data['surname'] ?? '',
'delivery_street' => $data['street'] ?? '',
'delivery_city' => $data['city'] ?? '',
'delivery_zip' => $data['zip'] ?? '',
'delivery_phone' => $data['phone'] ?? '',
'date_created' => date('Y-m-d H:i:s'),
'date_updated' => date('Y-m-d H:i:s'),
'note_admin' => json_encode(['delivery_data' => $deliveryInfo]),
],
'age_verification' => true,
]);
$order = $this->purchaseUtil->createOrderFromPurchaseState($purchaseState);
$orderEvent = new OrderEvent($order);
$this->eventDispatcher->dispatch($orderEvent, OrderEvent::ORDER_FINISHED);
if (!empty($order)) {
sqlQueryBuilder()->insert('users_age_verification')
->directValues([
'id_user' => $userId,
'verification_type' => 'package',
'id_order' => $order->id,
])
->onDuplicateKeyUpdate(['id_user' => 'id_user', 'id_order'])
->execute();
if ($birthdate = $mainRequest->get('cart_data')['birthdate'] ?? false) {
sqlQueryBuilder()->update('users')->directValues([
'birthdate' => $birthdate,
])->where(Operator::equals(['id' => $userId]))->execute();
}
} else {
throw new AgeVerificationException('Failed to create order.');
}
} elseif (!empty($userVerification['id_order'])) {
addUserMessage(translate('orderAlreadyCreated', 'AgeVerify'));
throw new RedirectException(path('ageVerify'));
}
return $order;
}
public function getDeliveries(): array
{
if (empty($this->deliveries)) {
$deliveryTypes = array_filter(\DeliveryType::getAll(), function ($deliveryType) {
return $deliveryType->getDelivery()->getCustomData()['balikobot']['require_full_age'] ?? false;
});
foreach ($deliveryTypes as $deliveryType) {
$delivery = $deliveryType->getDelivery();
if ($delivery->accept($this->getProductPrice(), false)) {
$this->deliveries[$deliveryType->id_delivery] = $delivery;
}
}
}
return $this->deliveries;
}
public function getPayments(): array
{
if (empty($this->payments)) {
$deliveryTypes = array_filter(\DeliveryType::getAll(true), function ($deliveryType) {
return ($deliveryType->getDelivery()->getCustomData()['balikobot']['require_full_age'] ?? false) && $deliveryType->id_delivery == $this->id_delivery;
});
foreach ($deliveryTypes as $deliveryType) {
$payment = $deliveryType->getPayment();
if ($payment->accept($this->getProductPrice(), false)) {
$this->payments[$deliveryType->id_payment] = [
'class' => $payment,
'name' => $payment->getName(),
'exception' => $payment->exception,
'photo' => $payment->getPhoto(null),
'enabled' => \Payment::isEnabled($payment->class),
];
}
}
}
return $this->payments;
}
public function getDeliveryPrice($id)
{
/** @var Price $price */
$price = $this->getDeliveries()[$id]->getPrice();
return ServiceContainer::getService(PriceWrapper::class)->setObject($price);
}
public function getPaymentPrice($id)
{
foreach (\DeliveryType::getAll() as $deliveryType) {
if ($deliveryType->id_payment == $id) {
$price = $deliveryType->price_payment;
}
}
return ServiceContainer::getService(PriceWrapper::class)->setObject($price);
}
protected function getProductPrice()
{
if (!$this->product_price) {
$product = new \Product();
$product->createFromDB(\Settings::getDefault()['age_verify']['package']['product']);
$this->product_price = $product->getProductPrice();
}
return $this->product_price;
}
protected function getDeliveryType($id_delivery, $id_payment): ?\DeliveryType
{
foreach (\DeliveryType::getAll(false) as $deliveryType) {
if ($deliveryType->id_delivery == $id_delivery && $deliveryType->id_payment == $id_payment) {
$delivery = $deliveryType->getDelivery();
$delivery->accept($this->getProductPrice(), false);
return $deliveryType;
}
}
return null;
}
protected function createPurchaseState(): PurchaseState
{
return new PurchaseState([]);
}
}