mein-sterntours/app/Repositories/CustomerMailRepository.php
2020-03-26 09:48:19 +01:00

203 lines
No EOL
8.2 KiB
PHP

<?php
namespace App\Repositories;
use App\Mail\MailSendFeWoService;
use App\Mail\MailSendInfo;
use App\Models\Booking;
use App\Models\CustomerFile;
use App\Models\CustomerMail;
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){
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) {
$message = $this->prepareContent($booking, $data['message']);
$subject = $this->prepareContent($booking, $data['subject']);
$reply_id = isset($data['customer_mail_id']) ? $data['customer_mail_id'] : NULL;
$customer_mail = $this->store($booking, $subject, $message, $booking->customer->email, false, $reply_id);
$this->sendMail($customer_mail, $customer_files);
foreach ($customer_files as $file) {
$file->customer_id = $booking->customer_id;
$file->customer_mail_id = $customer_mail->id;
$file->save();
}
}
}
}
}
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) {
$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['subject'], $data['message'], $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->save();
}
}
}
}
public function store($booking, $subject, $message, $mail_from, $is_answer = false, $reply_id = NULL, $sent_at=false){
$customer_mail = CustomerMail::create([
'booking_id' => $booking->id,
'customer_id' => $booking->customer_id,
'lead_id' => $booking->lead_id,
'is_answer' => $is_answer,
'reply_id' => $reply_id,
'email' => $mail_from,
'subject' => $subject,
'message' => $message,
'sent_at' => $sent_at ? $sent_at : now(),
]);
return $customer_mail;
}
private function sendMail($customer_mail, $customer_files){
try{
Mail::to($customer_mail->email)->send(new MailSendInfo($customer_mail->subject, $customer_mail->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 prepareContent($booking, $content){
$first_name = $booking->customer->firstname;
$last_name = $booking->customer->name;
$country = $booking->travel_country_id ? $booking->travel_country->name : "-";
$program = $booking->travelagenda_id ? $booking->travel_agenda->name : "-";
$salutation = $booking->customer->salutation->name;
$dear = $booking->customer->salutation_id == 1 ? 'geehrter' : 'geehrte';
$search = ['#geehrte/r#', '#Anrede#', '#Vorname#', '#Nachname#', '#Reiseland#', '#Programm#'];
$replace = [$dear, $salutation, $first_name, $last_name, $country, $program, $salutation];
$content = str_replace($search, $replace, $content);
return $content;
}
public static function loadModal($data)
{
$value = new Collection();
$value->title = "";
$value->subtitle = "";
$value->url = "";
if ($data['action'] === "new-customer-mail") {
if (isset($data['booking_id'])) {
$value->id = $data['booking_id'];
$value->customers = $data['customers'];
$value->subject = "";
$value->message = "Sehr #geehrte/r# #Anrede# #Vorname# #Nachname#,\n\nText ....";
$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: ".$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'];
return view("customer.mail.modal-new-mail", compact('data', 'value'))->render();
}
}
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";
return view("customer.mail.modal-show-mail", compact('data', 'value', 'customer_mail'))->render();
}
}
if ($data['action'] === "reply-customer-mail") {
if (isset($data['booking_id']) && $booking = Booking::find($data['booking_id'])) {
$value->id = $data['booking_id'];
$value->booking = $booking;
$value->message = "";
$value->subject = "";
$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: ".$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'];
return view("customer.mail.modal-new-mail", compact('data', 'value'))->render();
}
}
}
}