Newer
Older
/**
* This file contains QUI\ERP\Order\Mail
*/
use function is_array;
use function is_string;
use function json_decode;
use function pathinfo;
use function rename;
use function strtotime;
use function time;
use function trim;
/**
* Class Mail
*
* @package QUI\ERP\Order
*/
class Mail
{
/**
* confirmation mail for the customer
* @throws Exception|\PHPMailer\PHPMailer\Exception
public static function sendOrderConfirmationMail(Order $Order): void
$email = $Customer->getAttribute('email');
if (empty($email)) {
QUI\System\Log::writeRecursive(
QUI::getLocale()->get('quiqqer/order', 'message.order.missing.customer.mail', [
'orderId' => $Order->getUUID(),
'customerId' => $Customer->getUUID()
])
);
return;
}
// create order data
$OrderControl = new QUI\ERP\Order\Controls\Order\Order([
'orderHash' => $Order->getUUID(),
'template' => 'OrderLikeBasket'
$user = $Customer->getName();
if (empty($user)) {
$Address = $Customer->getAddress();
}
// mail
$Mailer = QUI::getMailManager()->getMailer();
$Mailer->addRecipient($email);
if (Settings::getInstance()->get('order', 'sendOrderConfirmationToAdmin')) {
self::addBCCMailAddress($Mailer);
QUI::getLocale()->get(
'quiqqer/order',
'order.confirmation.subject',
self::getOrderLocaleVar($Order, $Customer)
)

Henning Leutz
committed
self::addOrderMailAttachments($Mailer, $Order);

Henning Leutz
committed
$Engine = QUI::getTemplateManager()->getEngine();
$Articles = $Order->getArticles()->toUniqueList();
$Articles->hideHeader();
if (class_exists('QUI\ERP\Shipping\Shipping') && $Order->getShipping()) {
$Shipping = QUI\ERP\Shipping\Shipping::getInstance()->getShippingByObject($Order);
$DeliveryAddress = $Shipping->getAddress();
$DeliveryAddress?->setAttribute(
'template',
dirname(__FILE__) . '/MailTemplates/orderConfirmationAddress.html'
);
$InvoiceAddress = $Order->getInvoiceAddress();
$InvoiceAddress->setAttribute('template', dirname(__FILE__) . '/MailTemplates/orderConfirmationAddress.html');
// comment
$comment = '';
if (QUI::getSession()->get('comment-customer')) {
$comment .= QUI::getSession()->get('comment-customer') . "\n";
}
if (QUI::getSession()->get('comment-message')) {
$comment .= QUI::getSession()->get('comment-message');
}
'DeliveryAddress' => $DeliveryAddress,
'InvoiceAddress' => $InvoiceAddress,
'Payment' => $Order->getPayment(),
'comment' => $comment,
'Articles' => $Articles,
'message' => QUI::getLocale()->get(
'quiqqer/order',
'order.confirmation.body',
self::getOrderLocaleVar($Order, $Customer)
)
]);
$Mailer->setBody(
$Engine->fetch(dirname(__FILE__) . '/MailTemplates/orderConfirmation.html')
/**
* confirmation mail for the admin
*
* @param Order $Order
* @throws Exception|\PHPMailer\PHPMailer\Exception
public static function sendAdminOrderConfirmationMail(Order $Order): void
$Package = QUI::getPackage('quiqqer/order');
$Config = $Package->getConfig();
$email = $Config->getValue('order', 'orderAdminMails');
if (empty($email)) {
$email = QUI::conf('mail', 'admin_mail');
}
if (empty($email)) {
QUI\System\Log::addAlert(
QUI::getLocale()->get('quiqqer/order', 'message.order.missing.admin.mail')
);
return;
}
// create order data
$OrderControl = new QUI\ERP\Order\Controls\Order\Order([
'orderHash' => $Order->getUUID(),
'template' => 'OrderLikeBasket'
$Customer = $Order->getCustomer();
$comments = $Order->getComments()->toArray();
$user = $Customer->getName();
if (empty($user)) {
$Address = $Customer->getAddress();
}
// mail
$Mailer = QUI::getMailManager()->getMailer();
$Mailer->addRecipient($email);
$Mailer->setSubject(
QUI::getLocale()->get(
'quiqqer/order',
'order.confirmation.admin.subject',
self::getOrderLocaleVar($Order, $Customer)
)

Henning Leutz
committed
self::addOrderMailAttachments($Mailer, $Order);
try {
$Engine = QUI::getTemplateManager()->getEngine();
$Order = $OrderControl->getOrder();
$Articles = $Order->getArticles()->toUniqueList();
$Articles->hideHeader();
QUI\System\Log::addAlert(
QUI::getLocale()->get('quiqqer/order', 'exception.order.confirmation.admin', [
'orderId' => $Order->getPrefixedNumber(),
'message' => $Exception->getMessage()
])
);
return;
}
$DeliveryAddress = null;
if (class_exists('QUI\ERP\Shipping\Shipping') && $Order->getShipping()) {
$Shipping = QUI\ERP\Shipping\Shipping::getInstance()->getShippingByObject($Order);
$DeliveryAddress = $Shipping->getAddress();
$DeliveryAddress->setAttribute(
'template',
dirname(__FILE__) . '/MailTemplates/orderConfirmationAddress.html'
$InvoiceAddress = $Order->getInvoiceAddress();
$InvoiceAddress->setAttribute(
'template',
dirname(__FILE__) . '/MailTemplates/orderConfirmationAddress.html'
'DeliveryAddress' => $DeliveryAddress,
'InvoiceAddress' => $InvoiceAddress,
'Payment' => $Order->getPayment(),
'comments' => $comments,
'message' => QUI::getLocale()->get('quiqqer/order', 'order.confirmation.admin.body', [
'orderId' => $Order->getPrefixedNumber(),
'userId' => $Customer->getUUID(),
'username' => $user
])
]);
$Mailer->setBody(
$Engine->fetch(dirname(__FILE__) . '/MailTemplates/orderConfirmationAdmin.html')
);
try {
$Mailer->send();
QUI\System\Log::addAlert(
QUI::getLocale()->get('quiqqer/order', 'exception.order.confirmation.admin', [
'orderId' => $Order->getPrefixedNumber(),
'message' => $Exception->getMessage()
])
);
return;
}
}
/**
* Sends a shipping confirmation to the customer
*
public static function sendOrderShippingConfirmation(AbstractOrder $Order): void
$Address = $Customer->getAddress();
$email = $Customer->getAttribute('email');
$Country = $Customer->getCountry();
if (empty($email)) {
$mailList = $Address->getMailList();
if (isset($mailList[0])) {
$email = $mailList[0];
}
}
if (empty($email)) {
try {
$User = QUI::getUsers()->get($Customer->getUUID());
if ($User->getAttribute('email')) {
$email = $User->getAttribute('email');
}
QUI::getLocale()->get('quiqqer/order', 'exception.shipping.order.no.customer.mail')
);
}
$shippingConfirmation = $Order->getDataEntry('shippingConfirmation');
$shippingTracking = $Order->getDataEntry('shippingTracking');
if (is_string($shippingTracking)) {
$shippingTracking = json_decode($shippingTracking, true);
}
if (!is_array($shippingConfirmation)) {
$shippingConfirmation = [];
}
$shippingConfirmation[] = [
'email' => $email
];
$localeVar = self::getOrderLocaleVar($Order, $Customer);
$localeVar['trackingNumber'] = '';
if (
!empty($shippingTracking)
&& isset($shippingTracking['type'])
&& isset($shippingTracking['number'])
&& class_exists('QUI\ERP\Shipping\Tracking\Tracking')
) {
$localeVar['trackingInfo'] = QUI::getLocale()->get(
'quiqqer/order',
'shipping.order.mail.body.shippingInformation',
[
'trackingLink' => QUI\ERP\Shipping\Tracking\Tracking::getUrl(
$shippingTracking['number'],
$shippingTracking['type'],
$Country
),
'trackingNumber' => $shippingTracking['number']
]
);
$localeVar['trackingNumber'] = $shippingTracking['number'];
$localeVar['trackingLink'] = QUI\ERP\Shipping\Tracking\Tracking::getUrl(
$shippingTracking['number'],
$shippingTracking['type'],
$Country
);
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
}
// mail
$Mailer = QUI::getMailManager()->getMailer();
$Mailer->addRecipient($email);
$Mailer->setSubject(
QUI::getLocale()->get(
'quiqqer/order',
'shipping.order.mail.subject',
$localeVar
)
);
$Mailer->setBody(
QUI::getLocale()->get(
'quiqqer/order',
'shipping.order.mail.body',
$localeVar
)
);
try {
$Mailer->send();
} catch (\Exception $Exception) {
QUI\System\Log::addAlert(
QUI::getLocale()->get('quiqqer/shipping', 'exception.shipping.order.mail', [
'orderId' => $Order->getPrefixedNumber(),
'message' => $Exception->getMessage()
])
);
return;
}
$Order->setData('shippingConfirmation', $shippingConfirmation);
$Order->update(QUI::getUsers()->getSystemUser());
}
/**

Henning Leutz
committed
* Add the order mail attachments to the

Henning Leutz
committed
* like privacy policy, terms and condition and cancellation policy

Henning Leutz
committed
*
* @param QUI\Mail\Mailer $Mail

Henning Leutz
committed
* @param OrderInterface $Order
*/

Henning Leutz
committed
protected static function addOrderMailAttachments(
QUI\Mail\Mailer $Mail,
OrderInterface $Order
// check if html2pdf is installed
if (QUI::getPackageManager()->isInstalled('quiqqer/htmltopdf') === false) {
return;
}
$Package = QUI::getPackage('quiqqer/order');
$Config = $Package->getConfig();
$privacyPolicy = (int)$Config->getValue('mails', 'privacyPolicy');
$termsAndConditions = (int)$Config->getValue('mails', 'termsAndConditions');
$cancellationPolicy = (int)$Config->getValue('mails', 'cancellationPolicy');
$attachments = $Config->getValue('mails', 'attachments');
$Customer = $Order->getCustomer();
if ($privacyPolicy) {
try {

Henning Leutz
committed
$Site = QUI\ERP\Utils\Sites::getPrivacyPolicy();
if ($Site) {
$file = self::generatePdfFromSite($Site);
$Mail->addAttachment($file);
}
}
}
if ($termsAndConditions) {
try {

Henning Leutz
committed
$Site = QUI\ERP\Utils\Sites::getTermsAndConditions();
if ($Site) {
$file = self::generatePdfFromSite($Site);
$Mail->addAttachment($file);
}
}
}

Henning Leutz
committed
if ($cancellationPolicy && !$Customer->isCompany()) {
try {

Henning Leutz
committed
$Site = QUI\ERP\Utils\Sites::getRevocation();
if ($Site) {
$file = self::generatePdfFromSite($Site);
$Mail->addAttachment($file);
}
}
}
if (!empty($attachments)) {
$attachments = explode(',', $attachments);
$Media = QUI::getProjectManager()->getStandard()->getMedia();
foreach ($attachments as $attachment) {
try {
$Item = $Media->get((int)$attachment);
$Mail->addAttachment($Item->getFullPath());
} catch (\Exception $Exception) {
QUI\System\Log::addAlert('Order mail attachment file error :: ' . $Exception->getMessage());
}
/**

Henning Leutz
committed
* @param QUI\Projects\Site $Site
* @return string

Henning Leutz
committed
*
*/
protected static function generatePdfFromSite(QUI\Projects\Site $Site): string
{
$Document = new QUI\HtmlToPdf\Document();

Henning Leutz
committed
//$Document->setHeaderHTML('');
$Document->setContentHTML($Site->getAttribute('content'));

Henning Leutz
committed
//$Document->setFooterHTML('');
// create PDF file

Henning Leutz
committed
$file = $Document->createPDF();
// rename for attachment
$title = $Site->getAttribute('title');
['dirname' => $dirname, 'extension' => $extension] = pathinfo($file);
$newFile = $dirname . '/' . $title . '.' . $extension;

Henning Leutz
committed

Henning Leutz
committed
return $newFile;
}
/**
* Add the order bcc addresses
*
* @param QUI\Mail\Mailer $Mailer
protected static function addBCCMailAddress(QUI\Mail\Mailer $Mailer): void
$Package = QUI::getPackage('quiqqer/order');
$Config = $Package->getConfig();
$orderMails = $Config->getValue('order', 'orderAdminMails');
if (empty($orderMails)) {
$orderMails = QUI::conf('mail', 'admin_mail');
}
if (!empty($orderMails)) {
$Mailer->addBCC($orderMails);
}
}
* @param OrderInterface $Order
* @param QUI\Interfaces\Users\User $Customer
protected static function getOrderLocaleVar(OrderInterface $Order, QUI\Interfaces\Users\User $Customer): array
{
$Address = $Customer->getAddress();
// customer name
$user = $Customer->getName();
if (empty($user)) {
$user = $Address->getName();
}
// email
$email = $Customer->getAttribute('email');
if (empty($email)) {
$mailList = $Address->getMailList();
$email = $mailList[0];
}
}
return [
'orderPrefixedId' => $Order->getPrefixedNumber(),
'hash' => $Order->getAttribute('hash'),
'date' => self::dateFormat($Order->getAttribute('date')),
'systemCompany' => self::getCompanyName(),
'user' => $user,
'name' => $user,
'company' => $Customer->getStandardAddress()->getAttribute('company'),
'companyOrName' => self::getCompanyOrName($Customer),
'address' => $Address->render(),
'email' => $email,
'salutation' => $Address->getAttribute('salutation'),
'firstname' => $Address->getAttribute('firstname'),
'lastname' => $Address->getAttribute('lastname')
];
}
/**
* @param $date
* @return false|string
*/
public static function dateFormat($date): bool | string
{
// date
$localeCode = QUI::getLocale()->getLocalesByLang(
QUI::getLocale()->getCurrent()
);
IntlDateFormatter::SHORT,
IntlDateFormatter::NONE
}
return $Formatter->format($date);
}
/**
* @param QUI\Interfaces\Users\User $Customer
protected static function getCompanyOrName(QUI\Interfaces\Users\User $Customer): string
{
$Address = $Customer->getStandardAddress();
if (!empty($Address->getAttribute('company'))) {
return $Address->getAttribute('company');
}
return $Customer->getName();
}
/**
* Return the company name of the quiqqer system
*
* @return string
*/
protected static function getCompanyName(): string
{
try {
$Conf = QUI::getPackage('quiqqer/erp')->getConfig();
$company = $Conf->get('company', 'name');
} catch (\Exception $Exception) {
QUI\System\Log::writeException($Exception);
return '';
}
if (empty($company)) {
return '';
}
return $company;
}
//endregion