mein-sterntours/app/Repositories/CustomerMailRepository.php
2026-04-22 16:01:27 +02:00

411 lines
No EOL
19 KiB
PHP

<?php
namespace App\Repositories;
use App\Mail\MailSendInfo;
use App\Models\Airline;
use App\Models\Booking;
use App\Models\CMSContent;
use App\Models\CustomerFile;
use App\Models\CustomerMail;
use App\Models\EmailTemplate;
use App\Services\Placeholder;
use App\Services\Util;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Mail;
class CustomerMailRepository extends BaseRepository {
public function __construct(CustomerMail $model)
{
$this->model = $model;
}
public function update($data)
{
return $this->model;
}
public function sendAndStore($data){
//send or draft
//$data['action']
if(isset($data['send_mail_to']) && is_array($data['send_mail_to'])) {
//has Attachments
$customer_files = [];
if(isset($data['message_attachment_id']) && is_array($data['message_attachment_id'])){
foreach ($data['message_attachment_id'] as $message_attachment_id){
if($CustomerFile = CustomerFile::find($message_attachment_id)){
$customer_files[] = $CustomerFile;
}
}
}
foreach ($data['send_mail_to'] as $booking_id => $on) {
$booking = Booking::find($booking_id);
if ($booking->customer) {
$data['draft'] = (isset($data['action']) && $data['action'] === 'draft' ? true : false);
if(!$data['draft']){
$data['message'] = $this->prepareContent($booking, $data['message']);
$data['subject'] = $this->prepareContent($booking, $data['subject']);
}
$reply_id = isset($data['customer_mail_id']) ? $data['customer_mail_id'] : NULL;
$email = isset($data['send_mail_to_mail'][$booking_id]) ? $data['send_mail_to_mail'][$booking_id] : null;
$customer_mail = $this->store($booking, $data, $email, false, $reply_id);
foreach ($customer_files as $file) {
$file->customer_id = $booking->customer_id;
$file->customer_mail_id = $customer_mail->id;
$file->identifier = 'mail';
$file->save();
}
if(isset($data['action']) && $data['action'] === 'send'){ //not at draft
$this->sendMail($customer_mail);
}
}
}
}
}
public function forwardMail($customer_mail, $data){
//send or draft
//$data['action']
if(isset($data['customer_mail_forward_email']) && !empty($data['customer_mail_forward_email'])) {
$to_mails = [];
if(strpos($data['customer_mail_forward_email'], ',')){
$to_mails = array_map('trim', explode(',', $data['customer_mail_forward_email']));
}else{
$to_mails[] = $data['customer_mail_forward_email'];
}
$customer_files = $customer_mail->customer_files;
$full_message = $this->prepareMessageFull($customer_mail);
try {
//
Mail::to($to_mails)
->bcc($customer_mail->bcc ?: [])
->send(new MailSendInfo($customer_mail->subject, $full_message, $customer_files));
}
catch(\Exception $e){
// Never reached
$forward = array();
$forward[now()->format("d.m.Y H:i:s")] = ['fail'=> true, 'to'=> $data['customer_mail_forward_email'], 'error'=>$e->getMessage()];
$customer_mail->setForwardMessage($forward);
return false;
}
$forward = array();
$forward[now()->format("d.m.Y H:i:s")] = ['sent'=> true, 'to'=> $data['customer_mail_forward_email']];
$customer_mail->setForwardMessage($forward);
return true;
}
}
public function replyStore($data){
if(isset($data['booking_id']) && $booking = Booking::find($data['booking_id'])) {
//has Attachments
$customer_files = [];
if(isset($data['message_attachment_id']) && is_array($data['message_attachment_id'])){
foreach ($data['message_attachment_id'] as $message_attachment_id){
if($CustomerFile = CustomerFile::find($message_attachment_id)){
$customer_files[] = $CustomerFile;
}
}
}
if ($booking->customer) {
$data['draft'] = (isset($data['action']) && $data['action'] === 'draft' ? true : false);
$mail_from = isset($data['mail_from']) ? $data['mail_from'] : $booking->customer->email;
$sent_at = isset($data['sent_at']) ? \Carbon::parse(str_replace("- ", "", $data['sent_at'])) : now();
$reply_id = isset($data['customer_mail_id']) ? $data['customer_mail_id'] : NULL;
$customer_mail = $this->store($booking, $data, $mail_from, true, $reply_id, $sent_at);
foreach ($customer_files as $file) {
$file->customer_id = $booking->customer_id;
$file->customer_mail_id = $customer_mail->id;
$file->identifier = 'mail';
$file->save();
}
}
}
}
public function store($booking, $data, $mail_from, $is_answer = false, $reply_id = NULL, $sent_at=false){
if(isset($data['save_customer_mail_id'])){
$customer_mail = CustomerMail::find($data['save_customer_mail_id']);
$customer_mail->fill([
'booking_id' => $booking->id,
'customer_id' => $booking->customer_id,
// customer_mails.lead_id-Spalte bleibt unverändert; Wert kommt aus booking.inquiry_id
'lead_id' => $booking->inquiry_id,
'is_answer' => $is_answer,
'reply_id' => $reply_id,
'email' => $mail_from,
'recipient' => isset($data['recipient']) ? Util::_explodeLines($data['recipient']) : null,
'cc' => isset($data['cc']) ? Util::_explodeLines($data['cc']) : null,
'bcc' => isset($data['bcc']) ? Util::_explodeLines($data['bcc']) : null,
'subject' => $data['subject'],
'message' => $data['message'],
'dir' => isset($data['dir']) ? $data['dir'] : 0,
'subdir' => isset($data['subdir']) ? $data['subdir'] : 0,
'draft' => $data['draft'],
'sent_at' => $sent_at ? $sent_at : now(),
])->save();
}else{
$customer_mail = CustomerMail::create([
'booking_id' => $booking->id,
'customer_id' => $booking->customer_id,
// customer_mails.lead_id-Spalte bleibt unverändert; Wert kommt aus booking.inquiry_id
'lead_id' => $booking->inquiry_id,
'is_answer' => $is_answer,
'reply_id' => $reply_id,
'email' => $mail_from,
'recipient' => isset($data['recipient']) ? Util::_explodeLines($data['recipient']) : null,
'cc' => isset($data['cc']) ? Util::_explodeLines($data['cc']) : null,
'bcc' => isset($data['bcc']) ? Util::_explodeLines($data['bcc']) : null,
'subject' => $data['subject'],
'message' => $data['message'],
'dir' => isset($data['dir']) ? $data['dir'] : 0,
'subdir' => isset($data['subdir']) ? $data['subdir'] : 0,
'draft' => $data['draft'],
'sent_at' => $sent_at ? $sent_at : now(),
]);
}
return $customer_mail;
}
private function sendMail($customer_mail){
$to_mails = [];
if(strpos($customer_mail->email, ',')){
$to_mails = array_map('trim', explode(',', $customer_mail->email));
}else{
$to_mails[] = $customer_mail->email;
}
if($customer_mail->recipient){
$to_mails = array_merge($to_mails, $customer_mail->recipient);
}
$customer_files = $customer_mail->customer_files;
$full_message = $this->prepareMessageFull($customer_mail);
try {
//
Mail::to($to_mails)
->cc($customer_mail->cc ?: [])
->bcc($customer_mail->bcc ?: [])
->send(new MailSendInfo($customer_mail->subject, $full_message, $customer_files));
}
catch(\Exception $e){
// Never reached
$customer_mail->fail = true;
$customer_mail->error = $e->getMessage();
$customer_mail->save();
return false;
}
$customer_mail->send = true;
$customer_mail->sent_at = now();
$customer_mail->save();
return true;
}
private function prepareMessageFull($customer_mail, $deep = 0){
$ret = "";
if($deep === 0){
$ret .= $customer_mail->message;
}else{
$ret .= "--------------------------------\n";
$ret .= $customer_mail->is_answer ? "Antwort von: " : "Gesendet an: ";
$ret .= "&lt;".$customer_mail->email."&gt; ".$customer_mail->sent_at."\n";
$ret .= "<strong>".$customer_mail->subject."</strong>\n";
$ret .= $customer_mail->message;
}
if($customer_mail->customer_mail){
$ret .= $this->prepareMessageFull($customer_mail->customer_mail, $deep+1);
}
return $ret;
}
private function prepareContent($booking, $content){
$content = Placeholder::replaceBooking($booking, $content);
return $content;
}
private static function prepareContactMails($value){
if(isset($value->customers)){ //&& $value->customer_mail_dir !== NULL
$first_mail = "";
if($value->customer_mail_dir < 10){ // && $value->customer_mail_subdir > 0
$customer_mail_dir = \App\Services\Booking::getCustomerMailDir($value->customer_mail_dir);
$contact_emails = \App\Services\Booking::getCustomerMailEmails($customer_mail_dir, $value->customer_mail_subdir);
if($value->customer_mail_dir == 0){
$value->recipient = Util::_implodeLines($contact_emails);
return $value;
}else{
if($contact_emails && count($contact_emails) > 0) {
$first_mail = array_shift($contact_emails);
if (count($contact_emails) > 0) {
$value->recipient = Util::_implodeLines($contact_emails);
}
}
}
}
foreach ($value->customers as $key=>$val){
$val['email'] = $first_mail;
$value->customers[$key] = $val;
}
}else{
if(isset($value->replay_email)){
if($value->customer_mail_dir < 10){ // && $value->lead_mail_subdir > 0
$customer_mail_dir = \App\Services\Booking::getCustomerMailDir($value->customer_mail_dir);
$contact_emails = \App\Services\Booking::getCustomerMailEmails($customer_mail_dir, $value->customer_mail_subdir);
if($contact_emails && count($contact_emails) > 0) {
$value->replay_email = array_shift($contact_emails);
}
}
}
}
return $value;
}
public static function loadModal($data)
{
$value = new Collection();
$value->title = "";
$value->subtitle = "";
$value->url = "";
$value->recipient = "";
$value->show_move_dirs = true;
$value->cc = "";
$value->bcc = "";
$value->lead_title_id = "";
$value->filter_email_templates_directories = EmailTemplate::join('email_template_dirs', 'email_template_dir_id', '=', 'email_template_dirs.id')->get()->pluck('name', 'id')->unique()->toArray();
/*Ansicht*/
if ($data['action'] === "show-customer-mail") {
if (isset($data['customer_mail_id']) && $customer_mail = CustomerMail::find($data['customer_mail_id'])) {
$value->url = $data['url'];
$value->title = "E-Mail Ansicht";
if(isset($data['preview']) && $data['preview']){
$value->show_move_dirs = false;
}
return view("customer.mail.modal-show-mail", compact('data', 'value', 'customer_mail'))->render();
}
}
/* neue Mail */
if ($data['action'] === "edit-customer-mail") {
$value->id = $data['id']; //
$customer_mail = CustomerMail::find($value->id);
$booking = $customer_mail->booking;
$value->customer_files = $customer_mail->customer_files;
$value->save_customer_mail_id = $customer_mail->id;
$value->draft = true;
$value->id = $customer_mail->booking_id;
$value->booking = $booking;
$value->show = 'single';
$value->lead_title_id = " - (".$value->booking->inquiry_id.")";
$tmp = [];
$tmp['email'] = $customer_mail->email ? $customer_mail->email : "";
$tmp['name'] = $booking->customer ? $booking->customer->firstname." ".$booking->customer->name." | " : "- | ";
$tmp['name'] .= $booking->travel_country_id ? $booking->travel_country->name." | " : "- | ";
$tmp['name'] .= $booking->travelagenda_id ? $booking->travel_agenda->name."" : "-";
$data['customers'][$booking->id] = $tmp;
$value->customers = $data['customers'];
$value->subject = $customer_mail->subject;
$value->message = $customer_mail->message;
$value->recipient = Util::_implodeLines($customer_mail->recipient);
$value->cc = Util::_implodeLines($customer_mail->cc);
$value->bcc = Util::_implodeLines($customer_mail->bcc);
$value->title = "E-Mail- Nachricht an Kunden senden";
$value->subtitle = "Dem Kunden wird eine E-Mail zugesendet.";
if($customer_mail->reply_id){
$value->title = "E-Mail Antwort an Kunden senden";
$value->subtitle = "Dem Kunden wird eine E-Mail zugesendet, die im System als Antwort gespeichert wird.";
$value->customer_mail = $customer_mail->customer_mail;
}
$value->s_placeholder = "Betreff der E-Mail";
$value->m_placeholder = "Nachricht der E-Mail";
$value->url = $data['url'];
$value->customer_mail_dir = $customer_mail->dir ? $customer_mail->dir : 0;
$value->customer_mail_subdir = $customer_mail->subdir ? $customer_mail->subdir : 0;
return view("customer.mail.modal-new-mail", compact('data', 'value'))->render();
}
/* neue Mail */
if ($data['action'] === "new-customer-mail") {
$value->id = "";
$value->draft = false;
$value->lead_title_id = "-";
//singel
if (isset($data['booking_id']) && $booking = Booking::find($data['booking_id'])) {
$value->id = $data['booking_id'];
$value->booking = $booking;
$value->show = 'single';
$value->draft = true;
$value->lead_title_id = " - (".$value->booking->inquiry_id.")";
}else{
//multi
$value->show = 'multi';
}
$value->customers = $data['customers'];
$value->subject = $value->lead_title_id;
$value->message = CMSContent::getContentBySlug('mailvorlage');
$value->s_placeholder = "Betreff der E-Mail";
$value->m_placeholder = "Nachricht der E-Mail";
if(isset($data['customer_mail_id']) && $customer_mail = CustomerMail::find($data['customer_mail_id'])){
$value->subject = "Re: ".Util::_first_replace($customer_mail->subject);
$value->customer_mail = $customer_mail;
}
$value->title = "E-Mail- Nachricht an Kunden senden";
$value->subtitle = "Dem Kunden wird eine E-Mail zugesendet.";
if($data['id'] === 'reply-send'){
$value->title = "E-Mail Antwort an Kunden senden";
$value->subtitle = "Dem Kunden wird eine E-Mail zugesendet, die im System als Antwort gespeichert wird.";
}
$value->url = $data['url'];
$value->customer_mail_dir = isset($data['customer_mail_dir']) ? $data['customer_mail_dir'] : 0;
$value->customer_mail_subdir = isset($data['customer_mail_subdir']) ? $data['customer_mail_subdir'] : 0;
$value = self::prepareContactMails($value);
return view("customer.mail.modal-new-mail", compact('data', 'value'))->render();
}
/*Antwort speichern*/
if ($data['action'] === "reply-customer-mail") {
if (isset($data['booking_id']) && $booking = Booking::find($data['booking_id'])) {
$value->id = $data['booking_id'];
$value->draft = false;
$value->booking = $booking;
$value->message = "";
$value->subject = " - (".$value->booking->inquiry_id.")";
$value->lead_title_id = " - (".$value->booking->inquiry_id.")";
$value->s_placeholder = "Betreff des Kunden";
$value->m_placeholder = "Nachricht des Kunden";
if(isset($data['customer_mail_id']) && $customer_mail = CustomerMail::find($data['customer_mail_id'])){
$value->subject = "Re: ".Util::_first_replace($customer_mail->subject);
$value->customer_mail = $customer_mail;
}
$value->title = "E-Mail Antwort speichern";
$value->subtitle = "Die E-Mail wird im System gespeichert.";
if($data['id'] === 'reply-save'){
$value->subtitle = "Die E-Mail wird im System als Antwort gespeichert.";
}
$value->url = $data['url'];
$value->show = 'reply';
$value->customer_mail_dir = isset($data['customer_mail_dir']) ? $data['customer_mail_dir'] : 0;
$value->customer_mail_subdir = isset($data['customer_mail_subdir']) ? $data['customer_mail_subdir'] : 0;
$value->replay_email = $value->booking->customer->email;
$value = self::prepareContactMails($value);
return view("customer.mail.modal-new-mail", compact('data', 'value'))->render();
}
}
}
}