<?php
namespace Cogi\CogiLicenseManager\Service;
use Cogi\CogiLicenseManager\CogiLicenseManager;
use Cogi\CogiLicenseManager\Core\LicenseEmail\LicenseEmailEntity;
use Cogi\CogiLicenseManager\Core\LicenseKey\LicenseKeyCollection;
use Cogi\CogiLicenseManager\Core\LicenseKey\LicenseKeyEntity;
use Cogi\CogiLicenseManager\Core\LicenseMultiKeyLog\LicenseMultiKeyLogEntity;
use Cogi\CogiLicenseManager\Core\LicensePool\LicensePoolEntity;
use Cogi\CogiLicenseManager\Core\LicenseProduct\LicenseProductEntity;
use Doctrine\DBAL\Connection;
use Exception;
use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Content\Mail\Service\AbstractMailService;
use Shopware\Core\Content\MailTemplate\MailTemplateEntity;
use Shopware\Core\Content\Media\MediaService;
use Shopware\Core\Framework\Api\Context\SystemSource;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\Framework\Validation\DataBag\DataBag;
use Shopware\Core\System\Salutation\SalutationEntity;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class LicenseManagerService extends AbstractController {
/**
* @var AbstractMailService $mailService
*/
private AbstractMailService $mailService;
/**
* @var EntityRepository $mailTemplateRepository
*/
private EntityRepository $mailTemplateRepository;
/**
* @var EntityRepository $salutationRepository
*/
private EntityRepository $salutationRepository;
/**
* @var SystemConfigService $systemConfigService
*/
private SystemConfigService $systemConfigService;
/**
* @var EntityRepository
*/
protected EntityRepository $licenseProductRepository;
/**
* @var EntityRepository
*/
protected EntityRepository $licenseRepository;
/**
* @var EntityRepository
*/
protected EntityRepository $licensePoolRepository;
/**
* @var EntityRepository $orderRepository
*/
private EntityRepository $orderRepository;
/**
* @var EntityRepository $orderTransactionRepository
*/
private EntityRepository $orderTransactionRepository;
/**
* @var EntityRepository $orderDeliveryRepository
*/
private EntityRepository $orderDeliveryRepository;
/**
* @var EntityRepository
*/
protected EntityRepository $licenseUrlRepository;
/**
* @var EntityRepository
*/
protected EntityRepository $licenseMultiKeyLogRepository;
/**
* @var EntityRepository
*/
protected EntityRepository $productRepository;
/**
* @var array
*/
protected array $config;
/**
* @var MediaService
*/
private MediaService $mediaService;
/**
* @param MediaService $mediaService
* @param AbstractMailService $mailService
* @param EntityRepository $mailTemplateRepository
* @param EntityRepository $salutationRepository
* @param SystemConfigService $systemConfigService
* @param EntityRepository $licenseProductRepository
* @param EntityRepository $licenseRepository
* @param EntityRepository $licensePoolRepository
* @param EntityRepository $orderRepository
* @param EntityRepository $orderTransactionRepository
* @param EntityRepository $orderDeliveryRepository
* @param EntityRepository $licenseUrlRepository
* @param EntityRepository $productRepository
* @param EntityRepository $licenseMultiKeyLogRepository
*/
public function __construct(MediaService $mediaService, AbstractMailService $mailService, EntityRepository $mailTemplateRepository, EntityRepository $salutationRepository, SystemConfigService $systemConfigService, EntityRepository $licenseProductRepository, EntityRepository $licenseRepository, EntityRepository $licensePoolRepository, EntityRepository $orderRepository, EntityRepository $orderTransactionRepository, EntityRepository $orderDeliveryRepository, EntityRepository $licenseUrlRepository, EntityRepository $productRepository, EntityRepository $licenseMultiKeyLogRepository) {
$this->mailService = $mailService;
$this->mailTemplateRepository = $mailTemplateRepository;
$this->salutationRepository = $salutationRepository;
$this->systemConfigService = $systemConfigService;
$this->licenseProductRepository = $licenseProductRepository;
$this->licenseRepository = $licenseRepository;
$this->licensePoolRepository = $licensePoolRepository;
$this->orderRepository = $orderRepository;
$this->orderTransactionRepository = $orderTransactionRepository;
$this->orderDeliveryRepository = $orderDeliveryRepository;
$this->licenseUrlRepository = $licenseUrlRepository;
$this->productRepository = $productRepository;
$this->licenseMultiKeyLogRepository = $licenseMultiKeyLogRepository;
$this->mediaService = $mediaService;
$this->config = $systemConfigService->getDomain('CogiLicenseManager.config');
}
/**
* @param OrderEntity $order
* @param OrderLineItemEntity $lineItem
* @param Context $context
*/
public function processLicenseOrder(OrderEntity $order, OrderLineItemEntity $lineItem, Context $context) {
$criteria = new Criteria();
$criteria->addAssociation('pool');
$criteria->addAssociation('product');
$criteria->setLimit(1);
$criteria->addFilter(new EqualsFilter('productId', $lineItem->getReferencedId()));
/** @var LicenseProductEntity $result */
$result = $this->licenseProductRepository->search($criteria, $context)->getEntities()->first();
$multiKey = $this->getMultiKey($result->getPool()->getId(), $context, $result->getCount() * $lineItem->getQuantity());
if ($multiKey->count() <= 0) {
$licenseList = $this->getFreeLicenses($result->getPool()->getId(), $context, $result->getCount() * $lineItem->getQuantity(), true);
} else {
$licenseList = $multiKey;
}
if (count($licenseList) < $result->getCount() && $multiKey === null) {
try {
$mailTemplate = $this->getMailTemplate(CogiLicenseManager::MAIL_TEMPLATE_ADMIN_EMPTY_POOL);
$licenseEmail = new LicenseEmailEntity();
$licenseEmail->setCustomer($order->getOrderCustomer());
$licenseEmail->setOrder($order);
$licenseEmail->setPool($result->getPool());
$licenseEmail->setLicenseProduct($result);
$this->sendEmptyPoolMail($licenseEmail, $mailTemplate);
} catch (InconsistentCriteriaIdsException $e) {
return;
}
} else if (count($licenseList) <= 0) {
try {
$mailTemplate = $this->getMailTemplate(CogiLicenseManager::MAIL_TEMPLATE_ADMIN_EMPTY_POOL);
$licenseEmail = new LicenseEmailEntity();
$licenseEmail->setCustomer($order->getOrderCustomer());
$licenseEmail->setOrder($order);
$licenseEmail->setPool($result->getPool());
$licenseEmail->setLicenseProduct($result);
$this->sendEmptyPoolMail($licenseEmail, $mailTemplate);
} catch (InconsistentCriteriaIdsException $e) {
return;
}
} else {
$this->reserveLicense($licenseList, $order, $lineItem, $result->getPool()->getId(), $result->getCount() * $lineItem->getQuantity());
$orderCustomFields = $order->getCustomFields() ?: [];
$licenseKeys = [];
if (isset($orderCustomFields['licenseKeys'])) $licenseKeys = $orderCustomFields['licenseKeys'] ?: [];
/** @var LicenseKeyEntity $license */
foreach ($licenseList->getElements() as $license) {
array_push($licenseKeys, $license->getLicense());
}
$orderCustomFields['licenseKeys'] = $licenseKeys;
$order->setCustomFields($orderCustomFields);
$this->orderRepository->update([
[
'id' => $order->getId(),
'customFields' => $orderCustomFields
]
], $context);
try {
$mailTemplate = $this->getMailTemplate(CogiLicenseManager::MAIL_TEMPLATE_ADMIN);
if (isset($mailTemplate)) {
$licenseEmail = new LicenseEmailEntity();
$licenseEmail->setCustomer($order->getOrderCustomer());
$licenseEmail->setOrder($order);
$licenseEmail->setLicenseKeys($licenseList->getElements());
$licenseEmail->setPool($result->getPool());
$licenseEmail->setLicenseList($licenseList);
$licenseEmail->setLicenseProduct($result);
$this->sendAdminMail($licenseEmail, $mailTemplate);
}
} catch (InconsistentCriteriaIdsException $e) {
return;
}
}
}
/**
* @param EntityCollection $licenseList
* @param OrderEntity $order
* @param OrderLineItemEntity $lineItem
* @param string $poolId
* @param int $count
*/
private function reserveLicense(EntityCollection $licenseList, OrderEntity $order, OrderLineItemEntity $lineItem, string $poolId, int $count) {
/** @var LicenseKeyEntity $license */
foreach ($licenseList as $license) {
if (!$license->isMultiKey()) {
$this->licenseRepository->update([
[
'id' => $license->getId(),
'sold' => false,
'delivered' => false,
'reserved' => true,
'orderId' => $order->getId(),
'customerId' => $order->getOrderCustomer()->getCustomer()->getId(),
'productId' => $lineItem->getProductId(),
]
], Context::createDefaultContext());
} else {
$this->licenseMultiKeyLogRepository->create([
[
'id' => Uuid::randomHex(),
'keyId' => $license->getId(),
'count' => $count,
'sold' => false,
'deliverd' => false,
'reserved' => true,
'orderId' => $order->getId(),
'customerId' => $order->getOrderCustomer()->getCustomer()->getId(),
'productId' => $lineItem->getProductId(),
]
], Context::createDefaultContext());
}
}
}
/**
* @param OrderEntity $order
* @param Context $context
* @param bool $isResend
*/
public function processSendKeys(OrderEntity $order, Context $context, bool $isResend = false) {
$keys = $this->getKeysToBeSent($order, $context, $isResend);
if (count($keys) <= 0 && !isset($order->getCustomFields()["licenseHasSendSuccess"])) {
$criteria = new Criteria([$order->getId()]);
$criteria->addAssociation("orderCustomer");
$criteria->addAssociation("orderCustomer.customer");
$criteria->addAssociation("salesChannel");
$criteria->addAssociation("currency");
$criteria->addAssociation("billingAddress");
$criteria->addAssociation("deliveries");
$criteria->addAssociation("lineItems");
$criteria->addAssociation("transactions");
$order = $this->orderRepository->search($criteria, $context)->first();
foreach ($order->getLineItems() as $lineItem) {
if (isset($lineItem->getPayload()['isLicense'])) {
if ($lineItem->getPayload()['isLicense'] !== null) {
if ($lineItem->getPayload()['isLicense']) {
$this->processLicenseOrder($order, $lineItem, $context);
}
}
}
}
$keys = $this->getKeysToBeSent($order, $context, $isResend);
}
if (!$isResend) {
$this->updateOrderState($order, $context);
foreach ($keys as $key) {
if (!isset($key->keyId)) {
$this->licenseRepository->update([
[
'id' => $key->getId(),
'sold' => true,
'delivered' => true,
]
], Context::createDefaultContext());
} else {
$this->licenseMultiKeyLogRepository->update([
[
'id' => $key->getId(),
'sold' => true,
'deliverd' => true,
'reserved' => true
]
], Context::createDefaultContext());
$this->licenseRepository->update([
[
'id' => $key->getKey()->getId(),
'multiKeyUsed' => ($key->getKey()->getMultiKeyUsed() + $key->getCount()),
]
], Context::createDefaultContext());
}
}
}
$keyPoolArr = array();
$keyCounts = [];
foreach ($keys as $key) {
if (!isset($key->keyId)) {
$keyPoolArr[$key->getPool()->getId()][] = $key;
} else {
$keyCounts[$key->keyId] = $key->count;
$multiKey = $key->getKey();
$multiKey->setCustomer($key->getCustomer());
$multiKey->setOrder($key->getOrder());
$multiKey->setProduct($key->getProduct());
$keyPoolArr[$key->getKey()->getPool()->getId()][] = $multiKey;
}
}
foreach ($keyPoolArr as $poolItems) {
$pool = $poolItems[0]->getPool();
$licenseList = new LicenseKeyCollection();
foreach ($poolItems as $license) {
$licenseList->add($license);
}
$licenseEmail = new LicenseEmailEntity();
$licenseEmail->setCustomer($order->getOrderCustomer());
$licenseEmail->setOrder($order);
$licenseEmail->setLicenseKeys($poolItems);
$licenseEmail->setPool($pool);
$licenseEmail->setLicenseList($licenseList);
$licenseEmail->setCount($keyCounts[$poolItems[0]->id] ?? 1);
$licenseEmail->setHidenMailUrl($this->config['CogiLicenseManager.config.openKeyUrl'] ?? null);
if (isset($pool)) $licenseEmail->setUrlList($this->getUrls($pool, $context) ?: null);
if (isset($pool)) $licenseEmail->setMediaList($pool->getMedialist() ?: null);
if (isset($pool)) $licenseEmail->setEmailContent($pool->getEmailcontent() ?: null);
try {
$mailTemplate = $this->getMailTemplate(CogiLicenseManager::MAIL_TEMPLATE_CUSTOMER);
} catch (InconsistentCriteriaIdsException $e) {
return;
}
if (!$isResend) {
try {
$orderCustomFields = $order->getCustomFields() ?: [];
// $orderCustomFields['licenseEmail'] = json_decode(json_encode($licenseEmail));
$orderCustomFields['licenseHasSendSuccess'] = true;
$order->setCustomFields($orderCustomFields);
$this->orderRepository->update([
[
'id' => $order->getId(),
'customFields' => $orderCustomFields
]
], $context);
} catch (Exeption $e) {
}
}
$this->sendEmail($licenseEmail, $mailTemplate);
}
}
/**
* @param OrderEntity $order
* @param Context $context
*/
public function checkState(OrderEntity $order, Context $context) {
$transaction = $this->getTransaction($order);
$paymentState = $transaction->getStateMachineState()->getId();
$hasLicense = false;
foreach ($order->getLineItems() as $lineItem){
if(isset($lineItem->getPayload()['isLicense'])) {
if ($lineItem->getPayload()['isLicense'] !== null) {
if ($lineItem->getPayload()['isLicense']) {
$hasLicense = true;
break;
}
}
}
}
if (!$this->checkForPaymentCanceled($order, $context, $paymentState) && $hasLicense) {
if (isset($this->config['CogiLicenseManager.config.transactionStatus'])) {
if ($this->config['CogiLicenseManager.config.transactionStatus'] === $paymentState) {
$this->processSendKeys($order, $context);
}
}
}
}
/**
* @param OrderEntity $order
* @param Context $context
* @param $paymentState
* @return bool
*/
private function checkForPaymentCanceled(OrderEntity $order, Context $context, $paymentState): bool {
if (is_array($this->config['CogiLicenseManager.config.transactionAbordStatus'])) {
if (in_array($paymentState, $this->config['CogiLicenseManager.config.transactionAbordStatus'])) {
$this->cancelLicense($order, $context);
return true;
}
} else if ($this->config['CogiLicenseManager.config.transactionAbordStatus'] === $paymentState) {
$this->cancelLicense($order, $context);
return true;
}
return false;
}
/**
* @param OrderEntity $order
* @param Context $context
*/
private function cancelLicense(OrderEntity $order, Context $context) {
$keys = $this->getKeysToBeSent($order, $context);
$poolId = null;
$productId = null;
foreach ($keys as $key) {
if (!isset($key->keyId)) {
$this->licenseRepository->update([
[
'id' => $key->getId(),
'sold' => false,
'delivered' => false,
'reserved' => false,
'orderId' => null,
'customerId' => null,
'productId' => null,
]
], Context::createDefaultContext());
if ($productId === null) {
$productId = $key->getProduct()->getId();
}
if ($poolId === null) {
$poolId = $key->getPool();
}
if ($poolId !== $key->getPool() || $productId !== $key->getProduct()->getId()) $this->updateProductStock($productId, $poolId);
} else {
$this->licenseMultiKeyLogRepository->update([
[
'id' => $key->getId(),
'sold' => false,
'deliverd' => false,
'reserved' => false
]
], Context::createDefaultContext());
}
}
}
/**
* @param $salutationId
* @return SalutationEntity|null
*/
private function getSalutation($salutationId): ?SalutationEntity {
$context = new Context(new SystemSource());
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('id', $salutationId));
$criteria->setLimit(1);
/** @var SalutationEntity|null $salutation */
$salutation = $this->salutationRepository->search($criteria, $context)->first();
return $salutation;
}
/**
* Gibt das bennötigte EMail Template wieder
* @param string $technicalName
* @return MailTemplateEntity|null
*/
private function getMailTemplate(string $technicalName): ?MailTemplateEntity {
$context = new Context(new SystemSource());
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('mailTemplateType.technicalName', $technicalName));
$criteria->setLimit(1);
/** @var MailTemplateEntity|null $mailTemplate */
$mailTemplate = $this->mailTemplateRepository->search($criteria, $context)->first();
return $mailTemplate;
}
/**
* @param string $poolId
* @param Context $context
* @param int $count
* @param bool $noMultiKey
* @return EntityCollection
*/
public function getFreeLicenses(string $poolId, Context $context, int $count, bool $noMultiKey = false): EntityCollection {
$criteria = new Criteria();
$criteria->addAssociation('pool');
if ($count > 0) {
$criteria->setLimit($count);
} else {
$criteria->setLimit(500);
}
$criteria->addFilter(new EqualsFilter('poolId', $poolId));
$criteria->addFilter(new EqualsFilter('active', true));
$criteria->addFilter(new EqualsFilter('delivered', false));
$criteria->addFilter(new EqualsFilter('sold', false));
$criteria->addFilter(new EqualsFilter('reserved', false));
if ($noMultiKey) $criteria->addFilter(new EqualsFilter('isMultiKey', false));
return $this->licenseRepository->search($criteria, $context)->getEntities();
}
/**
* @param string $poolId
* @param Context $context
* @param int $count
* @return LicenseKeyCollection
*/
public function getMultiKey(string $poolId, Context $context, int $count): LicenseKeyCollection {
$criteria = new Criteria();
$criteria->addAssociation('pool');
$criteria->setLimit(500);
$criteria->addFilter(new EqualsFilter('poolId', $poolId));
$criteria->addFilter(new EqualsFilter('active', true));
$criteria->addFilter(new EqualsFilter('delivered', false));
$criteria->addFilter(new EqualsFilter('sold', false));
$criteria->addFilter(new EqualsFilter('reserved', false));
$criteria->addFilter(new EqualsFilter('isMultiKey', true));
$keys = $this->licenseRepository->search($criteria, $context)->getEntities();
$list = new LicenseKeyCollection ();
/** @var LicenseKeyEntity $key */
foreach ($keys as $key) {
if ($key->isMultiKey()) {
$criteria = new Criteria();
$criteria->setLimit(500);
$criteria->addFilter(new EqualsFilter('reserved', true));
$criteria->addFilter(new EqualsFilter('sold', false));
$criteria->addFilter(new EqualsFilter('deliverd', false));
$criteria->addFilter(new EqualsFilter('keyId', $key->id));
$reserved = $this->licenseMultiKeyLogRepository->search($criteria, $context)->getEntities()->count();
if (($key->getMultiKeyLimit() - $key->getMultiKeyUsed() - $reserved) < $count) {
continue;
} else if (($key->getMultiKeyLimit() - $key->getMultiKeyUsed() - $reserved) >= $count) {
$list->add($key);
return $list;
}
}
}
return $list;
}
/**
* @param string $id
* @param Context $context
* @return int
*/
public function getReservedCountMultiKey(string $id, Context $context): int {
$count = 0;
$criteria = new Criteria();
$criteria->setLimit(500);
$criteria->addFilter(new EqualsFilter('reserved', true));
$criteria->addFilter(new EqualsFilter('sold', false));
$criteria->addFilter(new EqualsFilter('deliverd', false));
$criteria->addFilter(new EqualsFilter('keyId', $id));
/** @var LicenseMultiKeyLogEntity $key */
foreach ($this->licenseMultiKeyLogRepository->search($criteria, $context)->getEntities() as $key) {
$count += $key->getCount();
}
return $count;
}
/**
* @param OrderEntity $order
* @param Context $context
* @param bool $isResend
* @return EntityCollection
*/
private function getKeysToBeSent(OrderEntity $order, Context $context, bool $isResend = false): EntityCollection {
$criteria = new Criteria();
$criteria->setLimit(500);
$criteria->addAssociation('pool.medialist');
$criteria->addAssociation('product');
$criteria->addAssociation('order');
$criteria->addAssociation('order.billingAddress');
$criteria->addAssociation('order.deliveries');
$criteria->addAssociation('order.lineItems');
$criteria->addAssociation('order.transactions');
$criteria->addAssociation('order.currency');
$criteria->addAssociation('order.language');
$criteria->addAssociation('order.addresses');
$criteria->addAssociation('customer');
$criteria->addAssociation('customer.salutation');
$criteria->addAssociation('customer.addresses');
$criteria->addAssociation('customer.language');
$criteria->addFilter(new EqualsFilter('orderId', $order->getId()));
$criteria->addFilter(new EqualsFilter('active', true));
if (!$isResend) {
$criteria->addFilter(new EqualsFilter('delivered', false));
}
$criteria->addFilter(new EqualsFilter('reserved', true));
$criteria->addSorting(new FieldSorting("createdAt", "ASC"));
$list = $this->licenseRepository->search($criteria, $context)->getEntities();
if ($list->count() <= 0) {
$criteria = new Criteria();
$criteria->setLimit(500);
$criteria->addAssociation('key');
$criteria->addAssociation('key.pool');
$criteria->addAssociation('key.pool.medialist');
$criteria->addAssociation('product');
$criteria->addAssociation('order');
$criteria->addAssociation('order.billingAddress');
$criteria->addAssociation('order.deliveries');
$criteria->addAssociation('order.lineItems');
$criteria->addAssociation('order.transactions');
$criteria->addAssociation('order.currency');
$criteria->addAssociation('order.language');
$criteria->addAssociation('order.addresses');
$criteria->addAssociation('customer');
$criteria->addAssociation('customer.salutation');
$criteria->addAssociation('customer.addresses');
$criteria->addAssociation('customer.language');
$criteria->addFilter(new EqualsFilter('orderId', $order->getId()));
if (!$isResend) {
$criteria->addFilter(new EqualsFilter('deliverd', false));
}
$criteria->addFilter(new EqualsFilter('reserved', true));
$criteria->addSorting(new FieldSorting("createdAt", "ASC"));
$list = $this->licenseMultiKeyLogRepository->search($criteria, $context)->getEntities();
}
return $list;
}
/**
* @param LicensePoolEntity $pool
* @param Context $context
* @return EntityCollection
*/
private function getUrls(LicensePoolEntity $pool, Context $context): EntityCollection {
$criteria = new Criteria();
$criteria->setLimit(500);
$criteria->addFilter(new EqualsFilter('poolId', $pool->getId()));
return $this->licenseUrlRepository->search($criteria, $context)->getEntities();
}
/**
* @param OrderEntity $order
* @param Context $context
*/
private function updateOrderState(OrderEntity $order, Context $context) {
$containsNoDigitalProduct = false;
/** @var OrderLineItemEntity $lineItem */
foreach ($order->getLineItems() as $lineItem) {
if ($lineItem->getType() === "product") {
if (isset($lineItem->getPayload()['isLicense'])) {
if ($lineItem->getPayload()['isLicense'] !== null) {
if (!$lineItem->getPayload()['isLicense']) {
$containsNoDigitalProduct = true;
}
} else {
$containsNoDigitalProduct = true;
}
} else {
$containsNoDigitalProduct = true;
}
}
}
try {
if ($this->config['CogiLicenseManager.config.deliveryStatus'] != null && !$containsNoDigitalProduct) {
$criteria = new Criteria();
$criteria->setLimit(1);
$criteria->addFilter(new EqualsFilter('orderId', $order->getId()));
/** @var OrderDeliveryEntity $result */
$result = $this->orderDeliveryRepository->search($criteria, $context)->getEntities()->first();
if (isset($result)) {
$this->orderDeliveryRepository->update([
[
'id' => $result->getId(),
'stateId' => $this->config['CogiLicenseManager.config.deliveryStatus'],
]
], Context::createDefaultContext());
}
if ($this->config['CogiLicenseManager.config.orderStatus'] != null) {
$this->orderRepository->update([
[
'id' => $order->getId(),
'stateId' => $this->config['CogiLicenseManager.config.orderStatus'],
]
], Context::createDefaultContext());
}
} else if ($this->config['CogiLicenseManager.config.deliveryStatusPartial'] != null && $containsNoDigitalProduct) {
$criteria = new Criteria();
$criteria->setLimit(1);
$criteria->addFilter(new EqualsFilter('orderId', $order->getId()));
/** @var OrderDeliveryEntity $result */
$result = $this->orderDeliveryRepository->search($criteria, $context)->getEntities()->first();
if (isset($result)) {
$this->orderDeliveryRepository->update([
[
'id' => $result->getId(),
'stateId' => $this->config['CogiLicenseManager.config.deliveryStatusPartial'],
]
], Context::createDefaultContext());
}
}
} catch (Exception $e) {
}
}
/**
* Returns transaction.
*
* @param OrderEntity $order
*
* @return OrderTransactionEntity|null
*
* @throws InconsistentCriteriaIdsException
*/
private function getTransaction(OrderEntity $order): ?OrderTransactionEntity {
$context = new Context(new SystemSource());
$criteria = new Criteria();
$criteria->setLimit(500);
$criteria->addFilter(new EqualsFilter('orderId', $order->getId()));
return $this->orderTransactionRepository->search($criteria, $context)->first();
}
/**
* @param string $productId
* @param string $poolId
* @return float|int
* @throws \Doctrine\DBAL\Exception
*/
public function updateProductStock(string $productId, string $poolId) {
// if(!$this->config['CogiLicenseManager.config.stockupdate']) {
// return -1;
// }
$context = new Context(new SystemSource());
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('poolId', $poolId));
$criteria->addFilter(new EqualsFilter('active', true));
$criteria->addFilter(new EqualsFilter('delivered', false));
$criteria->addFilter(new EqualsFilter('sold', false));
$criteria->addFilter(new EqualsFilter('reserved', false));
/** @var LicenseKeyCollection $licenses */
$licenses = $this->licenseRepository->search($criteria, $context)->getEntities();
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('productId', $productId));
/** @var LicenseProductEntity $product */
$product = $this->licenseProductRepository->search($criteria, $context)->getEntities()->first();
$count = $product->getCount();
$stock = 0;
/** @var LicenseKeyEntity $license */
foreach ($licenses as $license) {
if ($license->isMultiKey()) {
$i = $license->getMultiKeyLimit() - $license->getMultiKeyUsed();
if ($i > 0) {
$stock += $i;
}
} else {
$stock++;
}
}
if ($stock > 1) {
if (($stock / $count) < 1) {
$stock = 0;
} else {
$i = ($stock / $count);
$stock = round($i, 0, PHP_ROUND_HALF_DOWN);
if ($stock < 1) $stock = 1;
}
}
// $this->productRepository->update([
// [
// 'id' => $productId,
// 'stock' => intval($stock),
// 'availableStock' => intval($stock),
// 'max_purchase' => intval($stock),
// ]
// ], Context::createDefaultContext());
/** @var Connection $connection */
$connection = $this->container->get(Connection::class);
$connection->executeStatement("UPDATE `product` SET `stock` = :stock, `available_stock` = :stock, `max_purchase` = :stock WHERE `id` = UNHEX(:id);", [
'id' => $productId,
'stock' => intval($stock)
]);
return $stock;
}
/**
* Email an den Kunden senden
* @param LicenseEmailEntity $licenseEmail
* @param MailTemplateEntity $mailTemplate
*/
private function sendEmail(LicenseEmailEntity $licenseEmail, MailTemplateEntity $mailTemplate) {
$recipients = [
$licenseEmail->getCustomer()->getEmail() => $licenseEmail->getCustomer()->getFirstName() . ' ' . $licenseEmail->getCustomer()->getLastName()
];
$context = new Context(new SystemSource());
$attachments = [];
$data = new DataBag();
$data->set('recipients', $recipients);
$data->set('senderName', $mailTemplate->getSenderName());
$data->set('salesChannelId', $licenseEmail->getOrder()->getSalesChannelId());
$data->set('contentHtml', $mailTemplate->getContentHtml());
$data->set('contentPlain', $mailTemplate->getContentPlain());
$data->set('subject', $mailTemplate->getSubject());
if ($licenseEmail->getMediaList() !== null) {
foreach ($licenseEmail->getMediaList()->getElements() as $mailTemplateMedia) {
$attachments[] = $this->mediaService->getAttachment($mailTemplateMedia, $context);
}
}
$data->set('binAttachments', $attachments);
$this->mailService->send($data->all(), $context, [
'licenseEmail' => $licenseEmail,
]);
}
/**
* @param LicenseEmailEntity $licenseEmail
* @param MailTemplateEntity $mailTemplate
*/
private function sendAdminMail(LicenseEmailEntity $licenseEmail, MailTemplateEntity $mailTemplate) {
if (isset($this->config['CogiLicenseManager.config.storeMail'])) {
if (isset($this->config['CogiLicenseManager.config.sendAdminMail'])) {
if ($this->config['CogiLicenseManager.config.sendAdminMail']) {
$recipients = [
$this->config['CogiLicenseManager.config.storeMail'] => 'SHOP'
];
$context = new Context(new SystemSource());
$attachments = [];
$data = new DataBag();
$data->set('recipients', $recipients);
$data->set('senderName', $mailTemplate->getSenderName());
$data->set('salesChannelId', $licenseEmail->getOrder()->getSalesChannelId());
$data->set('contentHtml', $mailTemplate->getContentHtml());
$data->set('contentPlain', $mailTemplate->getContentPlain());
$data->set('subject', $mailTemplate->getSubject());
if ($licenseEmail->getMediaList() !== null) {
foreach ($licenseEmail->getMediaList()->getElements() as $mailTemplateMedia) {
$attachments[] = $this->mediaService->getAttachment($mailTemplateMedia, $context);
}
}
$data->set('binAttachments', $attachments);
$this->mailService->send($data->all(), $context, [
'licenseEmail' => $licenseEmail
]);
}
}
}
}
/**
* @param LicenseEmailEntity $licenseEmail
* @param MailTemplateEntity $mailTemplate
*/
private function sendEmptyPoolMail(LicenseEmailEntity $licenseEmail, MailTemplateEntity $mailTemplate) {
if (isset($this->config['CogiLicenseManager.config.storeMail'])) {
if (isset($this->config['CogiLicenseManager.config.sendMailKeyEmpty'])) {
if ($this->config['CogiLicenseManager.config.sendMailKeyEmpty']) {
$recipients = [
$this->config['CogiLicenseManager.config.storeMail'] => 'SHOP'
];
$context = new Context(new SystemSource());
$attachments = [];
$data = new DataBag();
$data->set('recipients', $recipients);
$data->set('senderName', $mailTemplate->getSenderName());
$data->set('salesChannelId', $licenseEmail->getOrder()->getSalesChannelId());
$data->set('contentHtml', $mailTemplate->getContentHtml());
$data->set('contentPlain', $mailTemplate->getContentPlain());
$data->set('subject', $mailTemplate->getSubject());
if ($licenseEmail->getMediaList() !== null) {
foreach ($licenseEmail->getMediaList()->getElements() as $mailTemplateMedia) {
$attachments[] = $this->mediaService->getAttachment($mailTemplateMedia, $context);
}
}
$data->set('binAttachments', $attachments);
$this->mailService->send($data->all(), $context, [
'licenseEmail' => $licenseEmail
]);
}
}
}
}
}