<?php
declare(strict_types=1);
namespace App\Service;
use PHPUnit\Exception;
use Pimcore\Log\ApplicationLogger;
use Pimcore\Model\DataObject\DealerLocation;
use Pimcore\Model\DataObject\Inquiry;
use Pimcore\Mail;
use Pimcore\Model\DataObject\Objectbrick\Data\VehicleBaseData;
use Pimcore\Model\DataObject\Vehicle;
use Spatie\ArrayToXml\ArrayToXml;
use Symfony\Component\Uid\Uuid;
class InquiryService
{
/**
* @var ApplicationLogger
*/
private ApplicationLogger $logger;
/**
* @var Vehicle|null
*/
private ?Vehicle $vehicle;
/**
* @var Inquiry
*/
private Inquiry $inquiry;
/**
* @var Array
*/
private array $inquiryFile;
/**
* @var Array
*/
private array $message;
public function __construct(ApplicationLogger $logger)
{
$this->logger = $logger;
$this->vehicle = null;
$this->inquiryFile = [];
$this->messageString[] = 'Neue Anfrage über die Homepage';
}
/**
* @return void
* @throws \Exception
*/
public function sendCRMInquiryMailNotification()
{
$this->logger->debug('Created Inquiry');
// create generic xml
$this->inquiryFile['generalInformation'] = [
'channel' => 'HOMEPAGE',
'urgency' => 'NORMAL',
'subject' => $this->inquiry->getMessage(),
];
// ???? $this->inquiryFile['leadid'] = $leadId ?? Uuid::v4();
$this->inquiryFile['contact'] = [
'status' => '',
'type' => $this->inquiry->getDealerdeskType() ?? 'CASE',
'salutation' => $this->inquiry->getSalutation() ?? '',
'fullName' => $this->inquiry->getFirstname() . ' ' . $this->inquiry->getLastname(),
'givenName' => $this->inquiry->getFirstname(),
'familyName' => $this->inquiry->getLastname(),
'address1' => '',
'address2' => '',
'zip' => '',
'city' => '',
'country' => '',
'state' => $this->inquiry->getSourceContext() ?? '',
'phone' => $this->inquiry->getPhone() ?? '',
'email' => $this->inquiry->getEmail() ?? '',
];
// - is vehicle included
if ($this->getVehicle() instanceof Vehicle) {
// - attach vehicle information to generic message
/* @var VehicleBaseData $vehicleBaseData */
$vehicleBaseData = $this->getVehicle()->getStammdaten()->getVehicleBaseData();
// - attach vehicle information to xml
$this->inquiryFile['requestedVehicle'] = [
'internalId' => $vehicleBaseData->getInterneNummer(),
'link' => 'https://timmermanns.de' . '/fahrzeugboerse/fahrzeuge?i=' . $vehicleBaseData->getInterneNummer(),
'make' => $vehicleBaseData->getMarke(),
'model' => $vehicleBaseData->getModellbasis(),
'modelDescription' => $vehicleBaseData->getModell(),
'mileage' => $vehicleBaseData->getKilometer(),
'price' => $this->getVehicle()->getKosten()->getCosts()->getPreis(),
'firstRegistration' => $vehicleBaseData->getEz(),
'power' => round($this->getVehicle()->getTechnischeDaten()->getTechnicalData()->getLeistung() * 1.35962, 0), // convert from kW to PS
'exteriorColor' => $this->getVehicle()->getAussen()->getExterieur()->getFarbe(),
];
}
// set mail recipient
$mailRecipient = $this->getInquiryMailRecipient();
$mail = new Mail();
$mail->to($mailRecipient);
$mail->subject("Neue Anfrage über die Webseite");
$mail->text($this->composeMessage());
$mail->attach($this->getInquiryXML(), 'dealerdeskLead.xml', 'text/xml');
$mail->sendWithoutRendering();
}
/**
* @return Vehicle|null
* @throws \Exception
*/
protected function getVehicle()
{
if (!$this->vehicle instanceof Vehicle) {
$this->logger->debug('Vehicle not yet set ... ');
try {
if ($this->inquiry->getInternalVehicleId() !== null) {
$this->vehicle = Vehicle::getList()->filterByInternalVehicleId($this->inquiry->getInternalVehicleId())->current();
}
} catch (Exception $exception) {
$this->logger->error($exception->getMessage());
$this->vehicle = null;
}
}
return $this->vehicle;
}
/**
* @return string|null
* @throws \Exception
*/
protected function getInquiryMailRecipient(): ?string
{
$parentFolder = $this->inquiry->getParent();
if ($parentFolder instanceof \Pimcore\Model\DataObject\Folder) {
$sourceContextKey = $this->inquiry->getSourceContext();
if ($sourceContextKey) {
$recipientFromProperty = $parentFolder->getProperty($sourceContextKey);
if ($recipientFromProperty) {
return $recipientFromProperty;
}
}
}
try {
$vehicle = $this->getVehicle();
if ($vehicle instanceof Vehicle) {
$dealerLocation = DealerLocation::getList()->filterByLocationId(
$vehicle->getStammdaten()->getVehicleBaseData()->getFilialId()
)->current();
if ($dealerLocation && $dealerLocation->getAutocrmCarInquiryEmail()) {
return $dealerLocation->getAutocrmCarInquiryEmail();
}
}
} catch (Exception $exception) {
$this->logger->error($exception->getMessage());
}
$inquiryLocation = $this->inquiry->getLocation();
try {
$dealerLocation = DealerLocation::getList()->filterByName($inquiryLocation)->current();
if ($dealerLocation && $dealerLocation->getAutocrmGenericInquiryEmail()) {
return $dealerLocation->getAutocrmGenericInquiryEmail();
}
} catch (Exception $exception) {
$this->logger->error($exception->getMessage());
}
return 'homepage-neuss@timmermanns.dealerdesk.de';
}
/**
* @return bool|string
* @throws \Exception
*/
protected function getInquiryXML()
{
return ArrayToXml::convert($this->inquiryFile, 'dealerdeskLead', true, 'utf-8');
}
/**
* @return string
* @throws \Exception
*/
protected function composeMessage(): string
{
$messageString = '';
$this->message[] = $this->getInquiryXML();
foreach ($this->message as $line) {
$messageString .= $line . "\n";
}
return $messageString;
}
/**
* @return Inquiry
*/
public function getInquiry(): Inquiry
{
return $this->inquiry;
}
/**
* @param Inquiry $inquiry
*/
public function setInquiry(Inquiry $inquiry): void
{
$this->inquiry = $inquiry;
}
}