Files
kupshop/bundles/KupShop/SalesBundle/Email/SaleEmail.php
2025-08-02 16:30:27 +02:00

166 lines
5.1 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
namespace KupShop\SalesBundle\Email;
use KupShop\BonusProgramBundle\Utils\BonusProvider;
use KupShop\KupShopBundle\Context\ContextManager;
use KupShop\KupShopBundle\Context\CurrencyContext;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\KupShopBundle\Context\UserContext;
use KupShop\KupShopBundle\Email\BaseEmail;
use KupShop\KupShopBundle\Email\EmailGroupTypeEnum;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\SalesBundle\Entity\Sale;
use KupShop\SalesBundle\Util\SalesUtil;
use KupShop\SellerBundle\Utils\SellerUtil;
use Symfony\Contracts\Service\Attribute\Required;
class SaleEmail extends BaseEmail
{
protected static $name = 'Prodejky';
protected static $type = 'SALE_EMAIL';
protected static $group = EmailGroupTypeEnum::SALE;
protected $subject = 'Prodejka {KOD}';
private ?Sale $sale = null;
#[Required]
public SellerUtil $sellerUtil;
public function setSale(Sale $sale): self
{
$this->sale = $sale;
return $this;
}
public function getSale(): ?Sale
{
return $this->sale;
}
public function getEmailTemplate($languageID = null): array
{
if (!$languageID && $this->sale) {
$languageID = $this->sale->languageId;
}
return parent::getEmailTemplate($languageID);
}
public function getBasicTemplate($languageID = null)
{
if (!$languageID && $this->sale) {
$languageID = $this->sale->languageId;
}
return parent::getBasicTemplate($languageID);
}
public static function getPlaceholders()
{
$placeholders = [
'EMAIL_UZIVATELE' => ['text' => 'Email uživatele'],
'ZPUSOB_DORUCENI' => ['text' => 'Způsob doručení'],
'PRODEJNA_EMAIL' => ['text' => 'E-mail prodejny'],
'PRODEJNA_TELEFON' => ['text' => 'Telefon prodejny'],
'PRODEJNA_NAZEV' => ['text' => 'Název prodejny'],
'PRODEJNA_ADRESA' => ['text' => 'Adresa prodejny'],
];
return [self::$type => $placeholders] + (parent::getPlaceholders() ?? []);
}
public function replacePlaceholdersItem($placeholder)
{
$result = parent::replacePlaceholdersItem($placeholder);
if (!$result) {
switch ($placeholder) {
case 'KOD_PRODEJKY':
return $this->sale->code;
case 'CENA':
return printPrice($this->sale->totalPrice, ['currency' => $this->sale->currency, 'ceil' => false, 'decimal' => 'dynamic']);
case 'BODY_CELKEM':
$user = $this->getSale()->getUser();
if (!$user) {
return '0';
}
return (string) ServiceContainer::getService(BonusProvider::class)->getActivePointsAmount($user);
case 'EMAIL_UZIVATELE':
return $this->sale->getUser()?->email;
case 'ZPUSOB_DORUCENI':
return $this->sale->getDeliveryType()?->name;
case 'PRODEJNA_EMAIL':
return $this->getSeller()['email'] ?? '';
case 'PRODEJNA_TELEFON':
return $this->getSeller()['phone'] ?? '';
case 'PRODEJNA_NAZEV':
return $this->getSeller()['title'] ?? '';
case 'PRODEJNA_ADRESA':
$seller = $this->getSeller();
return implode('<br>', [($seller['street'] ?? '').' '.($seller['number'] ?? ''), ($seller['psc'] ?? '').' '.($seller['city'] ?? '')]);
}
}
return $result;
}
public function testEmail(): array
{
if (!isset($this->order)) {
$this->setTestEntity();
}
$contextManager = ServiceContainer::getService(ContextManager::class);
try {
return $contextManager->activateContexts(
[
LanguageContext::class => $this->sale->languageId,
CurrencyContext::class => $this->sale->currency,
UserContext::class => $this->sale->userId,
],
fn () => parent::testEmail()
);
} catch (\Throwable $e) {
throw new \InvalidArgumentException('E-mail nelze otestovat, protože neexistuje žádná objednávka');
}
}
private function setTestEntity(?int $id = null): void
{
$salesUtil = ServiceContainer::getService(SalesUtil::class);
$sale = $salesUtil->getSale($id ?: sqlQueryBuilder()->select('MAX(id)')->from('sales')->execute()->fetchOne());
$this->setSale($sale);
}
private function getSeller(): ?array
{
if (empty($this->sale?->sellerId) || !($seller = $this->sellerUtil->getSeller((int) $this->sale?->sellerId))) {
return null;
}
return $seller;
}
public function __wakeup()
{
parent::__wakeup();
$this->sellerUtil = ServiceContainer::getService(SellerUtil::class);
}
public function __sleep()
{
return array_diff(parent::__sleep(), ['sellerUtil']);
}
}