Send pdf invoce with confirmation

Change-Id: Ie5b7b20043d11879f758af6536b55961314188eb
diff --git a/User.php b/User.php
index 5e8487e..91cfa21 100644
--- a/User.php
+++ b/User.php
@@ -3,6 +3,7 @@
 
 class User
 {
+    public $id;
     public $username;
     public $first_name;
     public $last_name;
@@ -23,10 +24,14 @@
     public $participation_confirmed_at;
     public bool $student;
     public bool $conference_dinner;
+    public $registered_at;
+    public bool $earlybird_registration;
     public float $total_due;
+    public $invoice;
 
     function __construct()
     {
+        $this->id = "";
         $this->username = "";
         $this->first_name = "";
         $this->last_name = "";
@@ -47,7 +52,10 @@
         $this->participation_confirmed_at = "";
         $this->student = false;
         $this->conference_dinner = false;
+        $this->registered_at = date("Y-m-d H:i:s") . "UTC";
+        $this->earlybird_registration = false;
         $this->total_due = 0.0;
+        $this->invoice = "";
     }
 
     function __construct1(object $data)
@@ -97,6 +105,7 @@
     function to_string()
     {
         $tmp_user = $this;
+        $tmp_user->invoice = "";
         $tmp_user->password = "***";
         $tmp_user->user_hash = "***";
         return  json_encode($tmp_user);
@@ -113,7 +122,7 @@
     function to_table() {
         $table = "<table  style='border: 1px solid #e1e4e8; border-collapse: collapse; width: 100%;'>";
         foreach ($this as $key => $value) {
-            if ($key != "password" && $key != "user_hash" && !empty($value)) {
+            if ($key != "password" && $key != "user_hash" && !empty($value) && $key != "invoice") {
                 if (is_bool($value)) {
                     if ($value) $value = "yes";
                     else $value = "no";
diff --git a/composer.json b/composer.json
index cbe00da..826dc18 100644
--- a/composer.json
+++ b/composer.json
@@ -1,6 +1,13 @@
 {
+    "repositories": [
+        {
+            "type": "vcs",
+            "url": "https://github.com/morckx/pdf-invoice"
+        }
+    ],
     "require": {
         "gregwar/captcha": "1.*",
-        "monolog/monolog": "3.*"
+        "monolog/monolog": "3.*",
+        "konekt/pdf-invoice": "master-dev"
     }
-}
+}
\ No newline at end of file
diff --git a/db_backend.php b/db_backend.php
index e1be86e..6876558 100644
--- a/db_backend.php
+++ b/db_backend.php
@@ -16,7 +16,7 @@
         global $DB, $DB_USER, $DB_PASS;
         $this->log = $log;
         try {
-            $this->pdo = new PDO($DB, $DB_USER, $DB_PASS);
+            $this->pdo = new PDO($DB, $DB_USER, $DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") );
             if($log)
                 $log->info("Connected to database $DB");
         } catch (PDOException $e) {
@@ -34,6 +34,7 @@
                 $a[$key] = $value;
             }
         }
+        $a["participation_confirmed_at"] = date("Y-m-d H:i:s UTC");
         $keys = array_keys($a);
         $sql = "INSERT INTO person (" . implode(", ", $keys) . ") \n";
         $sql .= "VALUES ( :" . implode(", :", $keys) . ")";
@@ -42,7 +43,9 @@
         foreach ($a as $value)
             $q->bindParam($i++, $value);
         $this->log->debug($sql);
-        return $q->execute($a);
+        $res = $q->execute($a);
+        $user->id = $this->pdo->lastInsertId();
+        return $res;
     }
 
     function getColumnNames($table)
diff --git a/index.php b/index.php
index 5ce87e6..47712cd 100755
--- a/index.php
+++ b/index.php
@@ -8,6 +8,7 @@
 include_once 'db_backend.php';
 include_once 'validators.php';
 include_once 'User.php';
+include_once 'invoice.php';
 require __DIR__ . '/vendor/autoload.php';
 use Monolog\Level;
 use Monolog\Logger as Logger;
@@ -50,7 +51,7 @@
         "subject" => $WELCOME_TEMPLATE->subject,
         "text"    => replace_all_user_variables($WELCOME_TEMPLATE->text, $user, $url),
         "html"    => replace_all_user_variables($WELCOME_TEMPLATE->html, $user, $url)
-    ]);
+    ], $user);
 }
 
 function send_confirmation_email(string $mail, object $smtp, string $url, User $user)
@@ -315,7 +316,7 @@
                         $pending = redis_get("pending");
                         if ($pending) {
                             $maillist = $pending->mails;
-                            if (in_array($user->email, $maillist)) {
+                            if (is_array($maillist) && in_array($user->email, $maillist)) {
                                 unset($maillist[array_search($user->email, $maillist)]);
                                 redis_set("pending", (object)["mails" => $maillist], $MAIL_CONFIRMATION_AWAIT_DELAY);
                             }
diff --git a/invoice.php b/invoice.php
index d1de840..53bba3d 100644
--- a/invoice.php
+++ b/invoice.php
@@ -1,41 +1,76 @@
 <?php
 
-require '../vendor/autoload.php';
+include_once 'User.php';
+use \User as User;
+
+require 'vendor/autoload.php';
 
 use Konekt\PdfInvoice\InvoicePrinter;
 
-include '../src/InvoicePrinter.php';
-$invoice = new InvoicePrinter("A4", "€", "en");
-$invoice->lang['product'] = "Item";
-/* Header Settings */
-$invoice->setLogo('images/iclc-logo.png');
-$invoice->setColor('#4bb044');
-$invoice->setType('Invoice');
-$invoice->setReference('55033645');
-$invoice->setDate(date('Y-m-d', time()));
-$invoice->setFrom(['', 'Leibniz-Institute for the German Language', 'R5 6-13', '68181 Mannheim', 'Germany']);
-$invoice->setTo(['', 'Marc Kupietz', 'U4 4', '68161 Mannheim', 'Germany']);
+#include_once '../src/InvoicePrinter.php';
+include 'config.php';
 
-$invoice->setDue(date('Y-m-d', strtotime('+2 weeks')));
-// $invoice->hide_tofrom();
-/* Adding Items in table */
-$invoice->addItem('ICLC-10 Early Bird Standard Registration', '10ᵗʰ International Contrastive Linguistics Conference, 18-21 July in Mannheim (Germany)', 1, false, 280, false, 280);
-$invoice->addItem('Conference Dinner', 'The conference dinner will take place on 20 July.', 1, false, 60, false, 60);
-/* Add totals */
-$invoice->addTotal('Total', 340);
-$invoice->addTotal('Total due', 340, true);
+function create_invoice(User $user)
+{
+    global $SERVICE_ACRONYM, $SERVICE_NAME, $SERVICE_LOGO, $REGULAR_CONFERENCE_FEE, $EARLYBIRD_CONFERENCE_FEE, $STUDENT_DISCOUNT, $CONFERENCE_DINNER;
 
-$invoice->addTitle("Payment Details");
+    $invoice = new InvoicePrinter("A4", "€", "en");
+    $invoice->lang['product'] = "Item";
+    /* Header Settings */
+    $invoice->setLogo(str_replace(".svg", ".png", $SERVICE_LOGO));
+    $invoice->setColor('#4bb044');
+    $invoice->setType('Invoice');
+    $invoice->setReference('ICLC-10-' . $user->id);
+    $invoice->setDate(date('Y-m-d', time()));
+    $invoice->setFrom(['', 'Leibniz-Institute for the German Language', 'R5 6-13', '68181 Mannheim', 'Germany']);
+    $invoice->setTo([$user->first_name . ' ' . $user->last_name, $user->organization, $user->street, $user->zip . ' ' . $user->city, $user->country]);
+    $invoice->setDue(date('Y-m-d', strtotime('+2 weeks')));
+    // $invoice->hide_tofrom();
+    /* Adding Items in table */
+    if ($user->earlybird_registration) {
+        $invoice->addItem( $SERVICE_ACRONYM . ' Early Bird Registration', $SERVICE_NAME, 1, false, $EARLYBIRD_CONFERENCE_FEE, false, $EARLYBIRD_CONFERENCE_FEE);
+    } else
+        $invoice->addItem( $SERVICE_ACRONYM . ' Registration', $SERVICE_NAME, 1, false, $REGULAR_CONFERENCE_FEE, false, $REGULAR_CONFERENCE_FEE);
+    if($user->student) $invoice->addItem('Student Discount', 'Student discount', 1, false, "", $STUDENT_DISCOUNT, -$STUDENT_DISCOUNT);
+    if($user->conference_dinner)
+        $invoice->addItem('Conference Dinner', 'The conference dinner will take place on 20 July.', 1, false, $CONFERENCE_DINNER, false, $CONFERENCE_DINNER);
+    /* Add totals */
+    $invoice->addTotal('Total', $user->total_due);
+    // $invoice->addTotal('Discount', $STUDENT_DISCOUNT);
+    $invoice->addTotal('Total due', $user->total_due, true);
 
-$invoice->addParagraph("Please make payment by Bank Transfer, quoting the above REFERENCE and INVOICE NUMBER as shown above. Bank details are given below:");
+    $invoice->addTitle("Payment Details");
 
-$invoice->addParagraph("Leibniz-Institute for the German Language
+    $invoice->addParagraph("Please transfer the total amount to the following account: ");
+
+    $invoice->addParagraph("Leibniz-Institute for the German Language
 IBAN: DE70 6708 0050 0694 9411 00
 BIC: DRESDEFF670
 Commerzbank Mannheim
-  
-VAT ID: DE 143845359
+
+Reference: ICLC-10-" . $user->id . "
 ");
-$invoice->setFooterNote("xx");
-/* Render */
-$invoice->render('example2.pdf', 'I'); /* I => Display on browser, D => Force Download, F => local path save, S => return document path */
+    //$invoice->setFooterNote("xx");
+
+    /* Render */
+    $path = tempnam(sys_get_temp_dir(), 'prefix');
+    $ret = $invoice->render('example4.pdf', 'S'); /* I => Display on browser, D => Force Download, F => local path save, S => return document path */
+    return $ret;
+}
+
+/*
+$user = new User();
+$user->id = 107;
+$user->first_name = "John";
+$user->last_name = "Урсула";
+$user->organization = "Урсула ACME Inc.";
+$user->street = "123 Main St.";
+$user->zip = "12345";
+$user->city = "Mannheim";
+$user->country = "Germany";
+$user->total_due = 260.0;
+$user->student = true;
+$user->conference_dinner = true;
+create_invoice($user);
+*/
+?>
\ No newline at end of file
diff --git a/mail.php b/mail.php
index c0f3251..3a0440f 100755
--- a/mail.php
+++ b/mail.php
@@ -2,8 +2,10 @@
 require_once "Mail.php";
 include('Mail/mime.php');
 include 'config.php';
+include_once 'User.php';
+use \User as User;
 
-function send_mail(string $email, object $smtp, object $message) {
+function send_mail(string $email, object $smtp, object $message, User $user = null) {
     include 'config.php';
 
     $crlf = "\r\n";
@@ -41,6 +43,9 @@
     // Setting the body of the email
     $mime->setTXTBody($message->text);
     $mime->setHTMLBody($message->html);
+    if ($user && $user->invoice) {
+        $mime->addAttachment ( $user->invoice , 'application/pdf' , $SERVICE_ACRONYM."-invoice.pdf" , false, "base64" );
+    }
 
     $body = $mime->get();
     $headers = $mime->headers($headers);
diff --git a/templates/mail_confirmed.htm b/templates/mail_confirmed.htm
index c01da31..6057d6e 100644
--- a/templates/mail_confirmed.htm
+++ b/templates/mail_confirmed.htm
@@ -1,5 +1,10 @@
 <div class="alert alert-success" role="alert">
     <p>Your email address was confirmed! Thank you for registering for
-        <?php echo $SERVICE_ACRONYM; if(!empty($REDIRECT_TO) ) { echo "</p><p>You are being redirected..."; } ?>
+        <?php 
+        echo $SERVICE_ACRONYM ."!";
+        if ($CONFERENCE_REGISTRATION) {
+            echo " A confirmation email including the invoice and payment instructions has been sent to you.";
+        }
+        if(!empty($REDIRECT_TO) ) { echo "</p><p>You are being redirected..."; } ?>
     </p>
 </div>
diff --git a/templates/register.htm b/templates/register.htm
index 3ce7a33..29d35b9 100644
--- a/templates/register.htm
+++ b/templates/register.htm
@@ -481,7 +481,7 @@
     <?php endif; ?>
      <?php if( $CONFERENCE_REGISTRATION ): ?>
      <div class="form-outline col-md-7">
-        <label class="form-label fw-bold" for="student">Author Status</label>
+        <label class="form-label fw-bold" for="author">Author Status</label>
         <div class="form-group form-check">
             <input type="checkbox" class="form-check-input" name="author" id="author"
                 <?php echo isset($_POST['author']) ? 'checked' : '' ?>
@@ -493,7 +493,7 @@
     </div>
 
     <div class="form-outline col-md-5">
-        <label class="form-label fw-bold" for="student">Paper ID</label>
+        <label class="form-label fw-bold" for="paper_id">Paper ID</label>
         <input type="text" class="form-control fs-4" name="accepted_paper_id" id="paper_id"
             <?php echo isset($_POST['author']) ? 'placeholder="0000"' : 'disabled' ?>
             value="<?php echo isset($_POST['paper_id']) ? htmlspecialchars($_POST['paper_id']) : '' ?>" />
@@ -511,10 +511,14 @@
                 value="participation_confirmed">
             <label class="form-check-label fs-4" for="participation_confirmed">I confirm that I will be attending the <?php echo $SERVICE_ACRONYM; ?> conference. 
                 <?php if(new DateTimeImmutable("now") <= new DateTimeImmutable($EARLYBIRD_DEADLINE." 23:59:59 HST")) {
-                    echo "The early bird conference fee of <strong>€ <span id='conference_fee'>". $EARLYBIRD_CONFERENCE_FEE."</span></strong> is still valid until ".$EARLYBIRD_DEADLINE.".
+                        echo "The early bird conference fee of <strong>€ <span id='conference_fee'>". $EARLYBIRD_CONFERENCE_FEE."</span></strong> is still valid until ".$EARLYBIRD_DEADLINE.".
                      (afterwards the fee fee will be € <span id='conference_fee'>". $REGULAR_CONFERENCE_FEE.")";
+                        echo '<input type="hidden" name="conference_fee" value="' . $EARLYBIRD_CONFERENCE_FEE . '">';
+                        echo '<input type="hidden" name="earlybird_registration" value="earlybird_registration">';
                     } else {
                         echo "The conference fee is <strong>€ <span id='conference_fee'>". $REGULAR_CONFERENCE_FEE."</span></strong>.";
+                        echo '<input type="hidden" name="conference_fee" value="' . $REGULAR_CONFERENCE_FEE . '">';
+                        echo '<input type="hidden" name="earlybird_registration" value="">';
                     } ?>
             </label>
             <div class="invalid-feedback">
@@ -531,7 +535,7 @@
                 <?php echo isset($_POST['student']) ? 'checked' : '' ?>
                 oninput="update_total_due()"
                 value="student">
-            <label class="form-check-label fs-4" for="student">I am student and will therefore benefit from student discount of <strong>€ <span id="student_discount"><?php echo $STUDENT_DISCOUNT; ?></span></strong>.
+            <label class="form-check-label fs-4" for="student">I am student and will therefore benefit from student discount of <strong>€&nbsp;<span id="student_discount"><?php echo $STUDENT_DISCOUNT; ?></span></strong>.
             </label>
         </div>
     </div>
@@ -543,7 +547,7 @@
                 <?php echo isset($_POST['conference_dinner']) ? 'checked' : '' ?>
                 oninput="update_total_due()"
                 value="conference_dinner">
-            <label class="form-check-label fs-4" for="conference_dinner">I will be attending the conference dinner on 20 July at 8pm at the Rheinterrassen for the price of <strong>€ <span id="conference_dinner_price"><?php echo $CONFERENCE_DINNER; ?></span></strong>.
+            <label class="form-check-label fs-4" for="conference_dinner">I will be attending the conference dinner on 20 July at 8pm at the Rheinterrassen for the price of <strong>€&#160;<span id="conference_dinner_price"><?php echo $CONFERENCE_DINNER; ?></span></strong>.
             </label>
         </div>
     </div>
@@ -553,7 +557,7 @@
               <div class="input-group-text fs-3">€</div>
               <input lang="en" name="total_due" id="total_due" class="form-control text-end fw-bold fs-3" readonly>
             </div>
-            <div id="totalHelp" class="form-text fs-4">Only bank transfer is available as a payment method. After submitting the form, you will be asked to confirm your e-mail address. After you have confirmed it, you will receive an invoice with all bank details.</div>
+            <div id="totalHelp" class="form-text fs-4">Please note that bank transfer is the only payment method. After submitting this form, you will be asked to confirm your email address. After that you will receive an invoice with all bank details.</div>
     </div>
 
     <?php endif; ?>