Files
kupshop/bundles/KupShop/GTMBundle/Ecommerce/Purchase.php
2025-08-02 16:30:27 +02:00

233 lines
8.5 KiB
PHP

<?php
namespace KupShop\GTMBundle\Ecommerce;
use KupShop\GTMBundle\Utils\DataLoaders\PurchaseState;
use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\OrderingBundle\Util\Order\DeliveryInfo;
use KupShop\OrderingBundle\Util\Order\OrderInfo;
class Purchase extends AbstractEcommerce
{
protected $currencyContext;
/**
* @var LanguageContext
*/
protected $languageContext;
/**
* @var DeliveryInfo
*/
private $deliveryInfo;
/**
* @var OrderInfo
*/
private $orderInfo;
/**
* @var PurchaseState
*/
protected $purchaseStateLoader;
/**
* Specific PageData.
*
* @throws \Exception
*/
public function getData(&$dataContainer)
{
$dbcfg = \Settings::getDefault();
$ec = new \stdClass();
$order = $this->pageData['body']['order'];
if (!empty($order) && $order instanceof \Order && !$order->getData('conversion_sent')) {
$orderVAT = $this->priceComputer->getPrice($order->total_price_array['value_vat']);
$deliveryType = $order->getDeliveryType();
$delivery = $deliveryType->getDelivery()?->getClassName() ?? '';
$payment = $deliveryType->getPayment()?->getClassName() ?? '';
$affiliation = join(' | ', [$delivery, $payment, $this->languageContext->getActiveId()]);
$discounts = $this->purchaseStateLoader->getDiscounts($order->getPurchaseState(true));
$this->addAdditionalFields($dataContainer, $order);
$dataContainer->event = 'orderSuccess';
if (!empty($order->invoice_name)) {
$dataContainer->user = $dataContainer->user ?? new \stdClass();
$dataContainer->user->name = $order->invoice_name;
$dataContainer->user->surname = $order->invoice_surname;
$dataContainer->user->email = $order->invoice_email;
$dataContainer->user->phone = $order->invoice_phone;
$dataContainer->user->address = [
'street' => $order->invoice_street,
'city' => $order->invoice_city,
'region' => $order->invoice_state,
'postalCode' => $order->invoice_zip,
'country' => $order->invoice_country,
];
}
$purchase = new \stdClass();
$purchase->id = $order->order_no;
$purchase->affiliation = $affiliation;
$purchase->paymentType = $payment;
$purchase->payment = $this->purchaseStateLoader->getPaymentObject($order);
$purchase->deliveryType = $delivery;
$currency = $this->priceComputer->getOutputCurrency();
$purchase->currency = $currency->getId();
$shippingPrice = $this->getDeliveryPrice($order);
$shippWithVat = $shippingPrice['value_with_vat'] ?? \DecimalConstants::zero();
$shippWithoutVat = $shippingPrice['value_without_vat'] ?? \DecimalConstants::zero();
$purchase->shipping = $this->priceComputer->getPrice($shippingPrice ?? toDecimal(0));
$purchase->shippingWithVat = $this->priceComputer->getPrice($shippWithVat);
$purchase->shippingWithOutVat = $this->priceComputer->getPrice($shippWithoutVat);
if (($dbcfg->analytics['google_tag_manager']['withoutDelivery'] ?? 'N') == 'Y') {
// Tohle je kvuli tomu, ze podle nastaveni nevime jestli ma byt bez nebo s dph, proto nechame rozhodnout funkce uvnitr a pak to akorat proste odectu
$tp = toDecimal($this->priceComputer->getPrice($order->getTotalPrice()));
$shipp = toDecimal($purchase->shipping);
$purchase->total = $tp->sub($shipp);
$purchase->totalWithVat = $this->priceComputer->getPrice($order->getTotalPrice()->getPriceWithVat()->sub($shippWithVat));
$purchase->totalWithoutVat = $this->priceComputer->getPrice($order->getTotalPrice()->getPriceWithoutVat()->sub($shippWithoutVat));
} else {
$purchase->total = $this->priceComputer->getPrice($order->getTotalPrice());
$purchase->totalWithVat = $this->priceComputer->getPrice($order->getTotalPrice()->getPriceWithVat());
$purchase->totalWithoutVat = $this->priceComputer->getPrice($order->getTotalPrice()->getPriceWithoutVat());
}
$purchase->totalWithoutVatAndShipping = $this->priceComputer->getPrice($order->getTotalPrice()->getPriceWithoutVat()->sub(toDecimal($shippingPrice['value_without_vat'] ?? 0)));
$purchase->vat = $orderVAT;
$purchase->flags = array_filter(array_keys($order->getFlags()));
if ($coupons = OrderInfo::getUsedCoupons($order)) {
$coupons = join(',', $coupons);
}
$purchase->coupons = $coupons ?: '';
$purchase->heurekaDisagree = $order->getData('heurekaDisagree');
$purchase->discounts = $discounts;
$purchase->products = [];
$order->productList->fetchVariations(true);
$order->productList->fetchSections();
$order->productList->fetchProducers();
$order->productList->fetchMainImages(1);
foreach ($order->fetchItems() as $item) {
if ($item['id_product']) {
$p = $this->productLoader->getData($item['product'], $this->view);
$p->quantity = $item['pieces'];
if ($p->categoryCurrent == '@breadcrumbs') {
$p->categoryCurrent = [];
}
$prices = $this->purchaseStateLoader->getPrices($item);
$p->price = $prices['price'];
$p->priceWithVat = $prices['priceWithVat'];
$p->priceWithoutVat = $prices['priceWithoutVat'];
$p->priceVat = $prices['priceVat'];
$purchase->products[] = $p;
}
}
$purchase->charges = $this->purchaseStateLoader->getCharges($order);
$this->appendAfilliateCodes($purchase);
$dataContainer->purchase = $purchase;
if (!$dataContainer->user) {
$dataContainer->user = new \stdClass();
}
$dataContainer->user->email = $order->invoice_email;
$userStats = $this->fetchUsersStatsOrderData($order);
$dataContainer->user->lastOrderDate = $userStats['last_date'] ?? null;
$dataContainer->user->firstOrderDate = $userStats['first_date'] ?? null;
$dataContainer->user->countOrders = $userStats['count'] ?? null;
$dataContainer->user->shaEmail = hash('sha256', $order->invoice_email.($dbcfg->analytics['google_tag_manager']['sha_salt'] ?? false ?: ''));
}
}
/**
* @required
*/
public function setLanguageContext(LanguageContext $languageContext): void
{
$this->languageContext = $languageContext;
}
/**
* @return \Decimal
*/
public function getDeliveryPrice(\Order $order)
{
$delivery_item = $this->deliveryInfo->getDeliveryItem($order);
return $delivery_item ? $order->fetchItems()[$delivery_item]['total_price'] : null;
}
/**
* @required
*/
public function setPurchaseStateLoader(PurchaseState $purchaseStateLoader): void
{
$this->purchaseStateLoader = $purchaseStateLoader;
}
protected function appendAfilliateCodes(\stdClass &$purchase)
{
$request = $this->getRequest();
if (!$request) {
return;
}
$cjevent = $request->cookies->get('cjevent') ?: $request->getSession()->get('cjevent');
if ($cjevent) {
$purchase->cjevent = $cjevent;
}
$AP_tracker_TID = $request->cookies->get('AP_tracker_TID') ?: $request->getSession()->get('AP_tracker_TID');
if ($AP_tracker_TID) {
$purchase->AP_tracker_TID = $AP_tracker_TID;
}
}
/**
* @required
*/
public function setCurrencyContext(CurrencyContext $currencyContext): void
{
$this->currencyContext = $currencyContext;
}
/**
* @required
*/
public function setDeliveryInfo(DeliveryInfo $deliveryInfo): void
{
$this->deliveryInfo = $deliveryInfo;
}
/**
* @required
*/
public function setOrderInfo(OrderInfo $orderInfo): void
{
$this->orderInfo = $orderInfo;
}
}