391 lines
No EOL
18 KiB
PHP
391 lines
No EOL
18 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
|
use App\Mail\MailSendInfo;
|
|
use App\Models\CMSContent;
|
|
use App\Models\CustomerFewoFile;
|
|
use App\Models\CustomerFewoMail;
|
|
use App\Models\EmailTemplate;
|
|
use App\Models\TravelUserBookingFewo;
|
|
use App\Services\Placeholder;
|
|
use App\Services\Util;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Support\Facades\Mail;
|
|
|
|
|
|
class CustomerFewoMailRepository extends BaseRepository {
|
|
|
|
|
|
public function __construct(CustomerFewoMail $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 = CustomerFewoFile::find($message_attachment_id)){
|
|
$customer_files[] = $CustomerFile;
|
|
}
|
|
}
|
|
}
|
|
foreach ($data['send_mail_to'] as $booking_fewo_id => $on) {
|
|
$booking_fewo = TravelUserBookingFewo::find($booking_fewo_id);
|
|
if ($booking_fewo->travel_user) {
|
|
$data['draft'] = (isset($data['action']) && $data['action'] === 'draft' ? true : false);
|
|
if(!$data['draft']){
|
|
$data['message'] = $this->prepareContent($booking_fewo, $data['message']);
|
|
$data['subject'] = $this->prepareContent($booking_fewo, $data['subject']);
|
|
}
|
|
$reply_id = isset($data['customer_mail_id']) ? $data['customer_mail_id'] : NULL;
|
|
$email = isset($data['send_mail_to_mail'][$booking_fewo_id]) ? $data['send_mail_to_mail'][$booking_fewo_id] : $booking_fewo->travel_user->email;
|
|
$customer_mail = $this->store($booking_fewo, $data, $email, false, $reply_id);
|
|
foreach ($customer_files as $file) {
|
|
$file->travel_user_id = $booking_fewo->travel_user_id;
|
|
$file->customer_fewo_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['travel_user_booking_fewo_id']) && $booking_fewo = TravelUserBookingFewo::find($data['travel_user_booking_fewo_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 = CustomerFewoFile::find($message_attachment_id)){
|
|
$customer_files[] = $CustomerFile;
|
|
}
|
|
}
|
|
}
|
|
if ($booking_fewo->travel_user) {
|
|
$data['draft'] = (isset($data['action']) && $data['action'] === 'draft' ? true : false);
|
|
$mail_from = isset($data['mail_from']) ? $data['mail_from'] : $booking_fewo->travel_user->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_fewo, $data, $mail_from, true, $reply_id, $sent_at);
|
|
foreach ($customer_files as $file) {
|
|
$file->travel_user_id = $booking_fewo->travel_user_id;
|
|
$file->customer_fewo_mail_id = $customer_mail->id;
|
|
$file->identifier = 'mail';
|
|
$file->save();
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public function store($booking_fewo, $data, $mail_from, $is_answer = false, $reply_id = NULL, $sent_at=false){
|
|
|
|
if(isset($data['save_customer_mail_id'])){
|
|
$customer_mail = CustomerFewoMail::find($data['save_customer_mail_id']);
|
|
$customer_mail->fill([
|
|
'travel_user_booking_fewo_id' => $booking_fewo->id,
|
|
'travel_user_id' => $booking_fewo->travel_user_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 = CustomerFewoMail::create([
|
|
'travel_user_booking_fewo_id' => $booking_fewo->id,
|
|
'travel_user_id' => $booking_fewo->travel_user_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 .= "<".$customer_mail->email."> ".$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_fewo, $content){
|
|
$content = Placeholder::replaceBookingFewo($booking_fewo, $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\BookingFewo::getCustomerMailDir($value->customer_mail_dir);
|
|
$contact_emails = \App\Services\BookingFewo::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;
|
|
}
|
|
|
|
}
|
|
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 = CustomerFewoMail::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("travel.user.booking.mail.modal-show-mail", compact('data', 'value', 'customer_mail'))->render();
|
|
}
|
|
}
|
|
/* neue Mail */
|
|
if ($data['action'] === "edit-customer-mail") {
|
|
$value->id = $data['id']; //
|
|
$customer_mail = CustomerFewoMail::find($value->id);
|
|
$booking = $customer_mail->travel_user_booking_fewo;
|
|
$value->customer_files = $customer_mail->customer_files;
|
|
$value->save_customer_mail_id = $customer_mail->id;
|
|
$value->draft = true;
|
|
$value->id = $customer_mail->travel_user_booking_fewo_id;
|
|
$value->booking = $booking;
|
|
$value->show = 'single';
|
|
$value->lead_title_id = " - (".$value->booking->invoice_number.")";
|
|
|
|
$tmp = [];
|
|
$tmp['email'] = $booking->travel_user ? $booking->travel_user->email : "";
|
|
$tmp['name'] = $booking->travel_user ? $booking->travel_user->first_name . " " . $booking->travel_user->last_name . " | " : "- | ";
|
|
$tmp['name'] .= $booking->fewo_lodging_id ? $booking->fewo_lodging->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("travel.user.booking.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['travel_user_booking_fewo_id']) && $booking_fewo = TravelUserBookingFewo::find($data['travel_user_booking_fewo_id'])) {
|
|
$value->id = $data['travel_user_booking_fewo_id'];
|
|
$value->booking = $booking_fewo;
|
|
$value->show = 'single';
|
|
$value->draft = true;
|
|
$value->lead_title_id = " - (".$value->booking->invoice_number.")";
|
|
|
|
}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 = CustomerFewoMail::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("travel.user.booking.mail.modal-new-mail", compact('data', 'value'))->render();
|
|
|
|
}
|
|
/*Antwort speichern*/
|
|
if ($data['action'] === "reply-customer-mail") {
|
|
if (isset($data['travel_user_booking_fewo_id']) && $booking_fewo = TravelUserBookingFewo::find($data['travel_user_booking_fewo_id'])) {
|
|
$value->id = $data['travel_user_booking_fewo_id'];
|
|
$value->draft = false;
|
|
$value->booking = $booking_fewo;
|
|
$value->message = "";
|
|
$value->subject = " - (".$value->booking->invoice_number.")";
|
|
$value->lead_title_id = " - (".$value->booking->invoice_number.")";
|
|
$value->s_placeholder = "Betreff des Kunden";
|
|
$value->m_placeholder = "Nachricht des Kunden";
|
|
if(isset($data['customer_mail_id']) && $customer_mail = CustomerFewoMail::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 = self::prepareContactMails($value);
|
|
return view("travel.user.booking.mail.modal-new-mail", compact('data', 'value'))->render();
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
} |