294 lines
11 KiB
PHP
Executable file
294 lines
11 KiB
PHP
Executable file
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Carbon;
|
|
use Request;
|
|
use App\Models\Lead;
|
|
use App\Models\LeadFile;
|
|
use App\Models\LeadMail;
|
|
use App\Models\LeadNotice;
|
|
use App\Models\StatusHistory;
|
|
use App\Models\LeadParticipant;
|
|
use App\Repositories\LeadRepository;
|
|
use App\Repositories\CustomerRepository;
|
|
use App\Repositories\LeadFileRepository;
|
|
|
|
class LeadController extends Controller
|
|
{
|
|
|
|
protected $leadRepo;
|
|
protected $custRepo;
|
|
|
|
public function __construct(LeadRepository $leadRepo, CustomerRepository $custRepo)
|
|
{
|
|
$this->middleware('admin');
|
|
$this->leadRepo = $leadRepo;
|
|
$this->custRepo = $custRepo;
|
|
|
|
}
|
|
|
|
public function index($step = false)
|
|
{
|
|
$data = [
|
|
'step' => $step
|
|
];
|
|
return view('lead.index', $data);
|
|
}
|
|
|
|
public function detail($id)
|
|
{
|
|
if($id === "new") {
|
|
$lead = new Lead();
|
|
$id = 'new';
|
|
|
|
}else{
|
|
$lead = Lead::findOrFail($id);
|
|
$lead->getPassolutionPDF(true);
|
|
$id = $lead->id;
|
|
}
|
|
$data = [
|
|
'lead' => $lead,
|
|
'id' => $id,
|
|
'show_modal_quill_preview' => true,
|
|
];
|
|
return view('lead.detail', $data);
|
|
|
|
}
|
|
|
|
public function store($id)
|
|
{
|
|
$data = Request::all();
|
|
|
|
if($data['action'] === 'saveCustomer'){
|
|
$customer = $this->custRepo->updateCustomerFromLead($id, $data);
|
|
\Session()->flash('alert-save', '1');
|
|
return redirect(route('lead_detail', [$id]).'#collapseLeadCustomer');
|
|
}
|
|
|
|
if($data['action'] === 'saveLead'){
|
|
$lead = $this->leadRepo->updateLead($id, $data);
|
|
\Session()->flash('alert-save', '1');
|
|
return redirect(route('lead_detail', [$id]).'#collapseLeadDetail');
|
|
}
|
|
|
|
if($data['action'] === 'saveStatus'){
|
|
$lead = $this->leadRepo->updateLeadStatus($id, $data);
|
|
\Session()->flash('alert-save', '1');
|
|
return redirect(route('lead_detail', [$id]).'#collapseLeadStatus');
|
|
}
|
|
|
|
if($data['action'] === 'save_notice'){
|
|
$lead = $this->leadRepo->updateNotice($id, $data);
|
|
\Session()->flash('alert-save', '1');
|
|
return redirect(route('lead_detail', [$lead->id])."#collapseLeadNotice");
|
|
}
|
|
if($data['action'] === 'edit_notice'){
|
|
$lead = $this->leadRepo->updateNotice($id, $data);
|
|
\Session()->flash('alert-save', '1');
|
|
return redirect(route('lead_detail', [$lead->id])."#collapseLeadNotice");
|
|
}
|
|
if($data['action'] === 'update_lead_participant'){
|
|
$lead = $this->leadRepo->updateLeadParticipant($id, $data);
|
|
\Session()->flash('alert-save', '1');
|
|
return redirect(route('lead_detail', [$lead->id])."#collapseBookingParticipant");
|
|
}
|
|
|
|
|
|
return back();
|
|
}
|
|
|
|
public function action($action, $id=false){
|
|
|
|
if(!$lead = Lead::find($id)){
|
|
abort(404);
|
|
}
|
|
if($action === 'lead_participant_add'){
|
|
LeadParticipant::create([
|
|
'lead_id' => $lead->id,
|
|
'nationality_id' => 1,
|
|
'participant_salutation_id' => 1,
|
|
|
|
]);
|
|
\Session()->flash('alert-success', __('Neuen Teilnehmer hinzugefügt'));
|
|
return redirect(route('lead_detail', [$lead->id])."#collapseLeadParticipant");
|
|
}
|
|
}
|
|
|
|
|
|
public function getAjaxRequests(){
|
|
|
|
$data = Request::all();
|
|
if(Request::ajax()) {
|
|
if(isset($data['action']) && $data['action'] === "get_popover_lead_notice"){
|
|
$lead = Lead::findOrFail($data['lead_id']);
|
|
$ret = "";
|
|
|
|
if($lead->lead_notices->count()){
|
|
$lead_notice = $lead->lead_notices->first();
|
|
return $lead_notice->getSmallerMessage(500);
|
|
}
|
|
if($ret === ""){
|
|
return 'keine Notiz';
|
|
}
|
|
return $ret;
|
|
}
|
|
if(isset($data['action']) && $data['action'] === "get_popover_lead_last_email"){
|
|
$lead = Lead::findOrFail($data['lead_id']);
|
|
$ret = "";
|
|
if($lead->lead_mails->count()){
|
|
$lead_mail = $lead->lead_mails_sent_at->last();
|
|
return "<h6>".$lead_mail->subject."</h6>".$lead_mail->message;
|
|
}
|
|
if($ret === ""){
|
|
return 'keine E-Mail';
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public function loadModal(){
|
|
$data = Request::all();
|
|
$ret = "";
|
|
if(Request::ajax()) {
|
|
if($data['action'] === "edit_notice") {
|
|
$value = LeadNotice::findOrFail($data['id']);
|
|
$ret = view("lead.edit_notice_modal", compact('data', 'value'))->render();
|
|
}
|
|
}
|
|
return response()->json(['response' => $data, 'html'=>$ret]);
|
|
}
|
|
|
|
public function delete($id, $del=false){
|
|
|
|
if($del === 'lead') {
|
|
$lead = Lead::findOrFail($id);
|
|
//Files
|
|
$leadFiles = LeadFile::where('lead_id', $lead->id)->get();
|
|
foreach ($leadFiles as $leadFile) {
|
|
$fileRepo = new LeadFileRepository($leadFile);
|
|
$fileRepo->_set('disk', 'lead');
|
|
$fileRepo->delete();
|
|
$leadFile->delete();
|
|
}
|
|
//history
|
|
$leadHistories = StatusHistory::where('lead_id', $lead->id)->get();
|
|
foreach ($leadHistories as $leadHistory) {
|
|
$leadHistory->delete();
|
|
}
|
|
//Mails Files CASCADE
|
|
$lead->delete();
|
|
\Session()->flash('alert-success', __('Anfrage gelöscht'));
|
|
}
|
|
if($del === 'passolution_file'){
|
|
$lead = Lead::findOrFail($id);
|
|
$lead->resyncPassolutionPDF();
|
|
\Session()->flash('alert-success', 'Passolution erneuert');
|
|
return redirect(route('lead_detail', [$lead->id]).'#collapseLeadFiles');
|
|
|
|
}
|
|
if($del === 'lead_notice'){
|
|
$leadNotice = LeadNotice::findOrFail($id);
|
|
$lead = $leadNotice->lead;
|
|
$leadNotice->delete();
|
|
\Session()->flash('alert-success', 'Notiz gelöscht');
|
|
return redirect(route('lead_detail', [$lead->id]).'#collapseLeadNotice');
|
|
}
|
|
|
|
if($del === 'lead_files'){
|
|
$leadFile = LeadFile::findOrFail($id);
|
|
$lead = $leadFile->lead;
|
|
$fileRepo = new LeadFileRepository($leadFile);
|
|
$fileRepo->_set('disk', 'lead');
|
|
$fileRepo->delete();
|
|
$leadFile->delete();
|
|
\Session()->flash('alert-success', 'Datei gelöscht');
|
|
return redirect(route('lead_detail', [$lead->id]).'#collapseLeadFiles');
|
|
}
|
|
return redirect(route('leads'));
|
|
}
|
|
|
|
public function getLeads()
|
|
{
|
|
$query = Lead::with('customer')->with('sf_guard_user')->with('status')->select('lead.*');
|
|
|
|
return \DataTables::eloquent($query)
|
|
->addColumn('action_edit', function (Lead $lead) {
|
|
return '<a href="'.route('lead_detail', [$lead->id]).'" class="btn icon-btn btn-sm btn-primary"><span class="fa fa-edit"></span></a>';
|
|
})
|
|
->addColumn('id', function (Lead $lead) {
|
|
return '<a data-order="'.$lead->id.'" href="'.route('lead_detail', [$lead->id]).'" data-id="'.$lead->id.'">'.$lead->id.'</a>';
|
|
})
|
|
->addColumn('customer_id', function (Lead $lead) {
|
|
return '<a data-order="'.$lead->customer_id.'" href="'.route('customer_detail', [$lead->customer_id]).'" data-id="'.$lead->customer_id.'">'.$lead->customer_id.'</a>';
|
|
})
|
|
->addColumn('request_date', function (Lead $lead) {
|
|
return Carbon::parse($lead->request_date)->format(\Util::formatDateDB());
|
|
})
|
|
->addColumn('status', function (Lead $lead) {
|
|
return $lead->getStatusBadge();
|
|
})
|
|
->addColumn('lead_notice', function (Lead $lead) {
|
|
return $lead->lead_notices->count() ? '<span data-order="1" class="badge badge-pill badge-success" data-lead_id="'.$lead->id.'" data-action="get_popover_lead_notice" data-placement="top" data-toggle="popover" title="letzte Notiz"><i class="fa fa-check"></i></span>' :
|
|
'<span data-order="0" class="badge badge-pill badge-danger"><i class="fa fa-times"></i></span>';
|
|
})
|
|
->addColumn('last_lead_email', function (Lead $lead) {
|
|
|
|
if($lead->lead_mails->count()){
|
|
$lead_mail = $lead->lead_mails_sent_at->last();
|
|
return '<a data-order="'.$lead_mail->getSentAtRaw().'" href="#" data-toggle="modal"
|
|
data-target="#modals-load-content"
|
|
data-id="show-mail"
|
|
data-url="mail"
|
|
data-preview="true"
|
|
data-lead_id="'.$lead->id.'"
|
|
data-lead_mail_id="'.$lead_mail->id.'"
|
|
data-action="show-lead-mail"
|
|
data-redirect="back"
|
|
data-route="'.route('lead_mail_modal_load').'">
|
|
<span class="badge '.($lead_mail->is_answer ? 'badge-default' : 'badge-secondary').'">'.$lead_mail->sent_at.'</span>
|
|
</a>';
|
|
}
|
|
return '<span data-order="">-</span>';
|
|
})
|
|
->addColumn('action_delete', function (Lead $lead) {
|
|
return '<a href="' . route('lead_delete', [$lead->id, 'lead']) . '" class="btn icon-btn btn-sm btn-danger" onclick="return confirm(\''.__('Really delete entry?').'\');"><span class="fa fa-trash"></span></a>';
|
|
})
|
|
->orderColumn('id', 'id $1')
|
|
->orderColumn('customer_id', 'customer_id $1')
|
|
->orderColumn('status', 'status_id $1')
|
|
|
|
->orderColumn('last_lead_email', function ($query, $order) {
|
|
|
|
$query->whereHas('lead_mails',
|
|
function ($q) use ($order) {
|
|
// $q->select('sent_at')->where('sent_at', DB::raw("(select max('sent_at') customer_mails)")); //)
|
|
})->orderBy(
|
|
LeadMail::select('sent_at')
|
|
->whereColumn('lead_id', 'lead.id')
|
|
->orderBy('sent_at', 'DESC')
|
|
->limit(1)
|
|
, $order);
|
|
})
|
|
|
|
->filterColumn('id', function($query, $keyword) {
|
|
if($keyword != ""){
|
|
$query->where('id', 'LIKE', '%'.$keyword.'%');
|
|
}
|
|
})
|
|
->filterColumn('customer_id', function($query, $keyword) {
|
|
if($keyword != ""){
|
|
$query->where('customer_id', 'LIKE', '%'.$keyword.'%');
|
|
}
|
|
})
|
|
->rawColumns(['action_edit', 'customer_id', 'sf_guard_user_id', 'id', 'status', 'last_lead_email', 'lead_notice', 'action_delete'])
|
|
->make(true);
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|