From 82a6349218f681175e5bbf85ef260de53d68e6f5 Mon Sep 17 00:00:00 2001 From: Jepp9350 <2jepp9350@gmail.com> Date: Mon, 24 Mar 2025 14:40:22 +0100 Subject: [PATCH] Add booking confirmation email functionality Implemented a new booking confirmation email template and its integration with email sending functionality. Updated form handling to trigger confirmation email after submission and enhanced error handling on department validation. --- services/nginx/app/classes/email.php | 57 +++++++++++ .../email_template_booking_confirmation.php | 97 +++++++++++++++++++ .../nginx/app/modules/forms/book_wash_f.php | 25 +++++ .../objects/collected_order_invoices_o.php | 1 + .../nginx/app/routes/orderInvoicesRoute.php | 2 +- services/nginx/app/traits/form_t.php | 23 ++++- 6 files changed, 203 insertions(+), 2 deletions(-) create mode 100644 services/nginx/app/modules/email/templates/email_template_booking_confirmation.php diff --git a/services/nginx/app/classes/email.php b/services/nginx/app/classes/email.php index ccc3b5d4..7018f074 100644 --- a/services/nginx/app/classes/email.php +++ b/services/nginx/app/classes/email.php @@ -7,8 +7,10 @@ require_once WD . '/modules/email/templates/email_template_header.php'; require_once WD . '/modules/email/templates/email_template_footer.php'; require_once WD . '/modules/email/templates/email_template_head.php'; require_once WD . '/modules/email/templates/email_template_stripe_invoice.php'; +require_once WD . '/modules/email/templates/email_template_booking_confirmation.php'; use email\email_c; +use email\templates\email_template_booking_confirmation; use email\templates\email_template_footer; use email\templates\email_template_head; use email\templates\email_template_header; @@ -169,4 +171,59 @@ class email implements email_i $html .= self::generateHtmlFooter(); $this->sendEmailMailerSend($to, $recipient_name, 'Betalingslink for bestilling #' . $order_id, '', $html); } + + /** + * Send a booking confirmation email + * @throws MailerSendException + * @throws ClientExceptionInterface + * @throws JsonException + * @throws MailerSendAssertException + */ + public function sendBookingConfirmationEmail( + int $booking_id, + string $company_name, + int $customer_id, + string $contact_email, + string $reference_number, + string $vehicle_registration_truck, + string $vehicle_registration_trailer, + string $wants_wash_certificate, + string $wash_certificate_email, + string $date, + string $department, + string $address, + string $pickup, + string $notes + ): void + { + // Generate HTML email + $html = self::generateHtmlHeader(); + // Add email content + $html .= (new email_template_booking_confirmation( + $booking_id, + $company_name, + $customer_id, + $contact_email, + $reference_number, + $vehicle_registration_truck, + $vehicle_registration_trailer, + $wants_wash_certificate, + $wash_certificate_email, + $date, + $department, + $address, + $pickup, + $notes + ))->generate_html(); + // Add email footer + $html .= self::generateHtmlFooter(); + // Send email + $this->sendEmailMailerSend( + $contact_email, + $company_name, + 'Truck Wash Booking ( ID: ' . $booking_id . ', REF: ' . $reference_number . ' )', + '', + $html + ); + } } \ No newline at end of file diff --git a/services/nginx/app/modules/email/templates/email_template_booking_confirmation.php b/services/nginx/app/modules/email/templates/email_template_booking_confirmation.php new file mode 100644 index 00000000..21f80685 --- /dev/null +++ b/services/nginx/app/modules/email/templates/email_template_booking_confirmation.php @@ -0,0 +1,97 @@ +booking_id = $booking_id; + $this->company_name = $company_name; + $this->customer_id = $customer_id; + $this->contact_email = $contact_email; + $this->reference_number = $reference_number; + $this->vehicle_registration_truck = $vehicle_registration_truck; + $this->vehicle_registration_trailer = $vehicle_registration_trailer; + $this->wants_wash_certificate = $wants_wash_certificate; + $this->wash_certificate_email = $wash_certificate_email; + $this->date = $date; + $this->department = $department; + $this->address = $address; + $this->pickup = $pickup; + $this->notes = $notes; + } + + public function generate_text(): string + { + return ""; + } + + public function generate_html(): string + { + ob_start(); + # Start of the html + ?> + + +

Tak for din booking!

+

Vi bekræfter modtagelsen af din booking med følgende informationer.:

+
+

Virksomhed: company_name ?>

+

Kunde nummer: customer_id ?>

+

Kontakt email: contact_email ?>

+

Referencenummer / kode: reference_number ?>

+

Reg. nr. Trækker: vehicle_registration_truck ?>

+

Reg. nr. Trailer: vehicle_registration_trailer ?>

+

Ønsker vaskecertifikat: wants_wash_certificate ?>

+

Vaskecertifikat email: wash_certificate_email ?>

+

Dato: date ?>

+

Afdeling: department ?>

+

Addresse: address ?>

+

Skal afhentes: pickup ?>

+

Noter: notes ?>

+

Booking ID: booking_id ?>

+
+

Du vil modtage vaskecertifikatet på begge email addresser, såfremt du har ønsket det.

+

Med venlig hilsen, Truck Wash

+ + + getUserByCustomerNumber(self::getSanitizedData('customer_number')); + // Get the department name from the department id + $department = (new departments_o())->selectId(self::getSanitizedData('department_id')); + // Send the booking confirmation email + $email->sendBookingConfirmationEmail( + 123, + $user->getCustomerName($user->customer_number->value()), + self::getCustomerNumber(), + self::getSanitizedData('contact_email'), + self::getSanitizedData('reference'), + strtoupper(self::getSanitizedData('registration_number_tractor')), + strtoupper(self::getSanitizedData('registration_number_trailer')), + self::getSanitizedData('wants_wash_certificate') ? 'Ja' : 'Nej', + self::getSanitizedData('wants_wash_certificate_email'), + self::getSanitizedData('date'), + $department->name->value(), + $department->description->value(), + self::getSanitizedData('wants_pickup') ? 'Ja' : 'Nej', + self::getSanitizedData('notes') + ); // TODO: Implement afterSubmit() method. } diff --git a/services/nginx/app/objects/collected_order_invoices_o.php b/services/nginx/app/objects/collected_order_invoices_o.php index d2a7302f..da4c1583 100644 --- a/services/nginx/app/objects/collected_order_invoices_o.php +++ b/services/nginx/app/objects/collected_order_invoices_o.php @@ -89,6 +89,7 @@ class collected_order_invoices_o extends db 'economic_invoice_booked_id' => null, // This will be overwritten if the external id is set 'total_net_amount' => self::getTotalAmount(), 'user' => (array)(new users_o())->getUserByCustomerNumber($this->customer_number->value())->asArray(), + //'debug' => (new economic())->invoices->draft->get((new economic())->invoices->draft->get_from_external_id($this->getExternalId())), ]; // If the external id is set, get the invoice draft id if (!empty($this->external_id->value())) { diff --git a/services/nginx/app/routes/orderInvoicesRoute.php b/services/nginx/app/routes/orderInvoicesRoute.php index a69eff43..94bca40b 100644 --- a/services/nginx/app/routes/orderInvoicesRoute.php +++ b/services/nginx/app/routes/orderInvoicesRoute.php @@ -56,7 +56,7 @@ class orderInvoicesRoute 'updated_at' => (string)$collected_order_invoice['updated_at'], 'created_at' => (string)$collected_order_invoice['created_at'], 'orders' => $tmp_collected_order_invoices->getOrders(true), - 'total_net_amount' => (float)$tmp_collected_order_invoices->getTotalAmount() + 'total_net_amount' => (float)$tmp_collected_order_invoices->getTotalAmount(), ]; }, )); diff --git a/services/nginx/app/traits/form_t.php b/services/nginx/app/traits/form_t.php index fc5a5ff8..faafa2b5 100644 --- a/services/nginx/app/traits/form_t.php +++ b/services/nginx/app/traits/form_t.php @@ -77,6 +77,13 @@ trait form_t * @var array $form_unsanitized_data */ public array $form_unsanitized_data = []; + /** + * The form submissions object (When the form is submitted) + * @var form_submissions_o $form_submissions_o The form submissions object + * @see form_submissions_o + * @see submit() + */ + public form_submissions_o $form_submissions_o; /** * The form constructor (Runs the setup method) @@ -240,6 +247,8 @@ trait form_t self::getDepartmentId(), self::getCustomerNumber(), ); + // Set the form submissions object + $this->form_submissions_o = $form_submissions_o; } /** @@ -275,9 +284,20 @@ trait form_t * Set the department id * @param int|null $department_id The department id * @return form_t|book_wash_f + * @throws Exception If the department does not exist */ public function setDepartmentId(int|null $department_id): self { + // If the department id is not set, set it to null + if (empty($department_id)) { + $department_id = null; + } + // Check if the department exists + $department = (new departments_o())->selectId($department_id); + if (!$department->exists()) { + throw new Exception('The department does not exist'); + } + // Set the department id $this->department_id = $department_id; return $this; } @@ -298,6 +318,7 @@ trait form_t */ public function setCustomerNumber(int|null $customer_number): self { + // If the customer number is not set, set it to null $this->customer_number = $customer_number; return $this; } @@ -407,7 +428,7 @@ trait form_t * @param int $customer_number The customer number to validate * @return bool True if the customer number is valid, false otherwise */ - public function validateCustomerNumber(int $customer_number): bool + public function validateCustomerNumber(int|string $customer_number): bool { // Check if the customer number is valid // This regex allows for 1 to 10 digits