From 11a47c110448ae73f4136ed91d0c4d7b63bff729 Mon Sep 17 00:00:00 2001
From: Henning Leutz <leutz@pcsg.de>
Date: Mon, 18 May 2020 09:05:14 +0200
Subject: [PATCH] feat: pcsg/kunden/froilein-adrett-template#31 - order mail
 attachments

---
 locale.xml                 |  24 +++++++++
 settings.xml               |  43 ++++++++++++++++
 src/QUI/ERP/Order/Mail.php | 100 +++++++++++++++++++++++++++++++++++++
 3 files changed, 167 insertions(+)

diff --git a/locale.xml b/locale.xml
index f79b4da2..582595d2 100644
--- a/locale.xml
+++ b/locale.xml
@@ -579,6 +579,30 @@
             <de><![CDATA[Vorgehen bei gescheiterter Zahlung]]></de>
             <en><![CDATA[Procedure for failed payment]]></en>
         </locale>
+        <locale name="order.settings.mails.attachments">
+            <de><![CDATA[Automatische Mailanhänge]]></de>
+            <en><![CDATA[Automatic mail attachments]]></en>
+        </locale>
+        <locale name="order.settings.mails.attachments.description">
+            <de><![CDATA[
+            Die ausgewählten Seiten werden automatisch als Mailanhänge an jede Bestellmail mit versendet.
+            ]]></de>
+            <en><![CDATA[
+            The selected pages are automatically attached to each order mail as mail attachments.
+            ]]></en>
+        </locale>
+        <locale name="order.settings.mails.termsAndConditions">
+            <de><![CDATA[AGB]]></de>
+            <en><![CDATA[Terms and Condistions]]></en>
+        </locale>
+        <locale name="order.settings.mails.privacyPolicy">
+            <de><![CDATA[Datenschutz]]></de>
+            <en><![CDATA[Privacy Policy]]></en>
+        </locale>
+        <locale name="order.settings.mails.cancellationPolicy">
+            <de><![CDATA[Widerrufsrecht]]></de>
+            <en><![CDATA[Cancellation Policy]]></en>
+        </locale>
 
         <locale name="order.settings.failedPaymentProcedure,execute">
             <de><![CDATA[Bestellung ausführen]]></de>
diff --git a/settings.xml b/settings.xml
index 9e1ad2a6..1747f557 100644
--- a/settings.xml
+++ b/settings.xml
@@ -34,6 +34,21 @@
                 </conf>
             </section>
 
+            <section name="mails">
+                <!-- Datenschutz -->
+                <conf name="privacyPolicy">
+                    <type><![CDATA[string]]></type>
+                </conf>
+                <!-- AGB -->
+                <conf name="termsAndConditions">
+                    <type><![CDATA[string]]></type>
+                </conf>
+                <!-- Cancellation policy -->
+                <conf name="cancellationPolicy">
+                    <type><![CDATA[string]]></type>
+                </conf>
+            </section>
+
             <section name="orderStatus">
                 <conf name="standard">
                     <type><![CDATA[integer]]></type>
@@ -152,6 +167,34 @@
                         </input>
                     </settings>
 
+                    <settings>
+                        <title>
+                            <locale group="quiqqer/order" var="order.settings.mails.attachments"/>
+                        </title>
+                        <text>
+                            <locale group="quiqqer/order"
+                                    var="order.settings.mails.attachments.description"
+                            />
+                        </text>
+
+                        <input type="checkbox" conf="mails.termsAndConditions">
+                            <text>
+                                <locale group="quiqqer/order" var="order.settings.mails.termsAndConditions"/>
+                            </text>
+                        </input>
+                        <input type="checkbox" conf="mails.privacyPolicy">
+                            <text>
+                                <locale group="quiqqer/order" var="order.settings.mails.privacyPolicy"/>
+                            </text>
+                        </input>
+                        <input type="checkbox" conf="mails.cancellationPolicy">
+                            <text>
+                                <locale group="quiqqer/order" var="order.settings.mails.cancellationPolicy"/>
+                            </text>
+                        </input>
+
+                    </settings>
+
                     <settings title="orderStatusSettings" name="orderStatusSettings">
                         <title>
                             <locale group="quiqqer/order" var="order.status.settings.title"/>
diff --git a/src/QUI/ERP/Order/Mail.php b/src/QUI/ERP/Order/Mail.php
index 571b6268..d521e26d 100644
--- a/src/QUI/ERP/Order/Mail.php
+++ b/src/QUI/ERP/Order/Mail.php
@@ -7,6 +7,7 @@
 namespace QUI\ERP\Order;
 
 use QUI;
+use QUI\Projects\Site\Utils as SiteUtils;
 
 /**
  * Class Mail
@@ -178,6 +179,9 @@ public static function sendAdminOrderConfirmationMail(Order $Order)
             ])
         );
 
+        self::addOrderMailAttachments($Mailer);
+
+
         try {
             $Engine = QUI::getTemplateManager()->getEngine();
             $Order  = $OrderControl->getOrder();
@@ -249,4 +253,100 @@ public static function sendAdminOrderConfirmationMail(Order $Order)
             return;
         }
     }
+
+    /**
+     * @param $Mail
+     */
+    protected static function addOrderMailAttachments(QUI\Mail\Mailer $Mail)
+    {
+        // check if html2pdf is installed
+        if (QUI::getPackageManager()->isInstalled('quiqqer/htmltopdf') === false) {
+            return;
+        }
+
+        $Package  = QUI::getPackage('quiqqer/order');
+        $Config   = $Package->getConfig();
+        $language = QUI::getLocale()->getCurrent();
+
+        try {
+            $DefaultProject = QUI::getProjectManager()->getStandard();
+            $Project        = QUI::getProject($DefaultProject->getName(), $language);
+        } catch (QUI\Exception $Exception) {
+            return;
+        }
+
+        $privacyPolicy      = (int)$Config->getValue('mails', 'privacyPolicy');
+        $termsAndConditions = (int)$Config->getValue('mails', 'termsAndConditions');
+        $cancellationPolicy = (int)$Config->getValue('mails', 'cancellationPolicy');
+
+        if ($privacyPolicy) {
+            try {
+                $sites = $Project->getSites([
+                    'where' => [
+                        'type' => 'quiqqer/sitetypes:types/privacypolicy'
+                    ],
+                    'limit' => 1
+                ]);
+
+                if (isset($sites[0])) {
+                    $Site = $sites[0];
+                    $file = self::generatePdfFromSite($Site);
+                    $Mail->addAttachment($file);
+                }
+            } catch (QUI\Exception $Exception) {
+            }
+        }
+
+        if ($termsAndConditions) {
+            try {
+                $sites = $Project->getSites([
+                    'where' => [
+                        'type' => 'quiqqer/sitetypes:types/generalTermsAndConditions'
+                    ],
+                    'limit' => 1
+                ]);
+
+                if (isset($sites[0])) {
+                    $Site = $sites[0];
+                    $file = self::generatePdfFromSite($Site);
+                    $Mail->addAttachment($file);
+                }
+            } catch (QUI\Exception $Exception) {
+            }
+        }
+
+        if ($cancellationPolicy) {
+            try {
+                $sites = $Project->getSites([
+                    'where' => [
+                        'type' => 'quiqqer/order-cancellation-policy:types/privacypolicy'
+                    ],
+                    'limit' => 1
+                ]);
+
+                if (isset($sites[0])) {
+                    $Site = $sites[0];
+                    $file = self::generatePdfFromSite($Site);
+                    $Mail->addAttachment($file);
+                }
+            } catch (QUI\Exception $Exception) {
+            }
+        }
+    }
+
+    /**
+     * @return string
+     * @throws QUI\Exception
+     */
+    protected static function generatePdfFromSite(QUI\Projects\Site $Site)
+    {
+        $Document = new QUI\HtmlToPdf\Document();
+
+        //$Document->setHeaderHTML('<div class="header-test"><p>I am a header</p></div>');
+        $Document->setContentHTML($Site->getAttribute('content'));
+        //$Document->setFooterHTML('<div class="footer-test">I am a footer</div>');
+
+        // create PDF file
+        return $Document->createPDF();
+    }
 }
-- 
GitLab