356 lines
No EOL
16 KiB
PHP
356 lines
No EOL
16 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
|
use App\Mail\MailSendInfo;
|
|
use App\Models\Airline;
|
|
use App\Models\Booking;
|
|
use App\Models\CustomerFile;
|
|
use App\Models\CustomerMail;
|
|
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] : $booking->customer->email;
|
|
$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->save();
|
|
}
|
|
|
|
if(isset($data['action']) && $data['action'] === 'send'){ //not at draft
|
|
$this->sendMail($customer_mail);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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->save();
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public function store($booking, $data, $mail_from, $is_answer = false, $reply_id = NULL, $sent_at=false){
|
|
|
|
$data['travel_country_id'] = isset($data['travel_country_id']) && $data['travel_country_id']>0 ? $data['travel_country_id'] : NULL;
|
|
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,
|
|
'lead_id' => $booking->lead_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,
|
|
'travel_country_id' => $data['travel_country_id'],
|
|
'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,
|
|
'lead_id' => $booking->lead_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,
|
|
'travel_country_id' => $data['travel_country_id'],
|
|
'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;
|
|
try {
|
|
//
|
|
Mail::to($to_mails)
|
|
->cc($customer_mail->cc ?: [])
|
|
->bcc($customer_mail->bcc ?: [])
|
|
->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;
|
|
$start_date = $booking->getStartDateFormat();
|
|
$end_date = $booking->getEndDateFormat();
|
|
$booking_date = $booking->getBookingDateFormat();
|
|
$airline = $booking->airline ? $booking->airline->name_full : '-';
|
|
|
|
$dear = $booking->customer->salutation_id == 1 ? 'geehrter' : 'geehrte';
|
|
$search = ['#geehrte/r#', '#Anrede#', '#Vorname#', '#Nachname#', '#Reiseland#', '#Programm#', '#Anreisedatum#', '#Abreisedatum#', '#Buchungsdatum#', '#Airline#'];
|
|
$replace = [$dear, $salutation, $first_name, $last_name, $country, $program, $start_date, $end_date, $booking_date, $airline];
|
|
$content = str_replace($search, $replace, $content);
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
private static function prepareContactMails($value){
|
|
|
|
if(isset($value->customers) && $value->customer_mail_dir > 0){
|
|
$first_mail = "";
|
|
if($value->customer_mail_dir == 1 && $value->customer_mail_country > 0){
|
|
//Agentur / Land
|
|
$travel_country = \App\Models\Sym\TravelCountry::find($value->customer_mail_country);
|
|
if($travel_country && $travel_country->contact_emails && count($travel_country->contact_emails) > 0){
|
|
$contact_emails = $travel_country->contact_emails;
|
|
$first_mail = array_shift($contact_emails);
|
|
if(count($contact_emails) > 0){
|
|
$value->recipient = Util::_implodeLines($contact_emails);
|
|
}
|
|
}
|
|
}
|
|
if($value->customer_mail_dir == 2){
|
|
//Airline
|
|
$airline = Airline::whereName('Xemail')->first();
|
|
if($airline && $airline->contact_emails && count($airline->contact_emails) > 0){
|
|
$contact_emails = $airline->contact_emails;
|
|
$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->cc = "";
|
|
$value->bcc = "";
|
|
/*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";
|
|
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';
|
|
|
|
$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->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_country = $customer_mail->travel_country_id ? $customer_mail->travel_country_id : 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;
|
|
//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;
|
|
}else{
|
|
//multi
|
|
$value->show = 'multi';
|
|
}
|
|
$value->customers = $data['customers'];
|
|
$value->subject = "";
|
|
$value->message = "<p>Sehr #geehrte/r# #Anrede# #Vorname# #Nachname#,</p><p>Text ...</p>";
|
|
$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_country = isset($data['customer_mail_country']) ? $data['customer_mail_country'] : 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->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_country = isset($data['customer_mail_country']) ? $data['customer_mail_country'] : 0;
|
|
|
|
$value = self::prepareContactMails($value);
|
|
return view("customer.mail.modal-new-mail", compact('data', 'value'))->render();
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
} |