217 lines
No EOL
8.2 KiB
PHP
217 lines
No EOL
8.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use Request;
|
|
use Response;
|
|
|
|
use HTMLHelper;
|
|
use Carbon\Carbon;
|
|
use App\Models\Lead;
|
|
use App\Models\Status;
|
|
use App\Services\Util;
|
|
use App\Models\Booking;
|
|
use App\Models\LeadMail;
|
|
use App\Models\FewoLodging;
|
|
use App\Models\TravelAgenda;
|
|
use App\Models\TravelCompany;
|
|
use App\Models\ServiceProvider;
|
|
use Illuminate\Validation\Rules\In;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\ServiceProviderEntry;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
use App\Models\TravelUserBookingFewo;
|
|
use App\Exports\ReportCollectionExport;
|
|
|
|
class ReportLeadsController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware(['superadmin', '2fa']);
|
|
}
|
|
|
|
public function leads()
|
|
{
|
|
// $query = Lead::with('customer')->with('sf_guard_user')->with('status')->select('lead.*');
|
|
|
|
$data = [
|
|
'filter_leads_options' => [], //need ??? FewoLodging::get()->pluck('name', 'id'),
|
|
|
|
];
|
|
return view('admin.report.leads', $data);
|
|
}
|
|
|
|
|
|
|
|
//checkLeadsDatatable
|
|
|
|
private function prozessLeadsSearch()
|
|
{
|
|
|
|
$query = Lead::with('customer')->with('sf_guard_user')->with('status')
|
|
->select('lead.*');
|
|
//->where('deleted_at', '=', null);
|
|
|
|
/*
|
|
if(Request::get('filter_option_leads_id') != ""){
|
|
$query->where('...?', '=', Request::get('filter_option_leads_id'));
|
|
}
|
|
*/
|
|
if(Request::get('filter_date_from') != ""){
|
|
$date_from = Carbon::parse(Request::get('filter_date_from'))->format("Y-m-d");
|
|
$query->where("request_date", '>=', $date_from);
|
|
}
|
|
if(Request::get('filter_date_to') != ""){
|
|
$date_to = Carbon::parse(Request::get('filter_date_to'))->format("Y-m-d");
|
|
$query->where("request_date", '<=', $date_to);
|
|
}
|
|
return $query;
|
|
}
|
|
|
|
public function leadsDatatable()
|
|
{
|
|
$query = $this->prozessLeadsSearch();
|
|
|
|
return \DataTables::eloquent($query)
|
|
|
|
->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('travel_country', function (Lead $lead) {
|
|
return $lead->getTravelCountryDestco();
|
|
})
|
|
|
|
->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();
|
|
$badge = $lead_mail->is_answer ? 'badge-default' : 'badge-secondary';
|
|
$badge = !$lead_mail->send ? $badge : 'badge-success';
|
|
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 '.$badge.'">'
|
|
.($lead_mail->send ? '<i class="fa fa-check-circle"></i>' : '<i class="fa fa-times-circle"></i>').' '
|
|
.$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('request_date', 'request_date $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', 'travel_country', 'lead_notice', 'action_delete'])
|
|
->make(true);
|
|
}
|
|
|
|
public function leadsExport(){
|
|
|
|
$query = $this->prozessLeadsSearch();
|
|
$order = explode(",", Request::get('order'));
|
|
|
|
|
|
$orderByNum = [
|
|
0 => 'id',
|
|
1 => 'customer_id',
|
|
2 => 'customer.firstname',
|
|
3 => 'customer.name',
|
|
4 => 'customer.email',
|
|
5 => 'request_date',
|
|
6 => 'travel_country',
|
|
7 => 'sf_guard_user.last_name',
|
|
8 => 'status',
|
|
9 => 'last_lead_email',
|
|
];
|
|
if(isset($order[0])) {
|
|
$column = isset($orderByNum[$order[0]]) ? $orderByNum[$order[0]] : "id";
|
|
$direction = isset($order[1]) ? strtoupper($order[1]) : "ASC";
|
|
$query->orderBy($column, $direction);
|
|
}
|
|
|
|
$filename = "file-".date('Y-m-d-H-i-s');
|
|
$exports = $query->get();
|
|
$columns = [];
|
|
|
|
if(Request::get('export') === "export"){
|
|
$filename = "Buchungen_Anfragen_".date('Y-m-d-H-i-s');
|
|
|
|
$headers = array(
|
|
'LeadID',
|
|
'KundenID',
|
|
'Vorname',
|
|
'Nachname',
|
|
'E-Mail',
|
|
'Anfrage-Datum',
|
|
'Reiseland',
|
|
'Sachbearbeiter',
|
|
'Status',
|
|
'Letzte E-Mail',
|
|
);
|
|
|
|
foreach($exports as $export) {
|
|
$columns[] = array(
|
|
'LeadID' => $export->id,
|
|
'KundenID' => $export->customer_id,
|
|
'Vorname' => $export->customer ? $export->customer->firstname : " ",
|
|
'Nachname' => $export->customer ? $export->customer->name : " ",
|
|
'E-Mail' => $export->customer ? $export->customer->email : " ",
|
|
'Anfrage-Datum' => _format_date($export->request_date),
|
|
'Reiseland' => $export->getTravelCountryDestco(false),
|
|
'Sachbearbeiter' => $export->sf_guard_user->last_name,
|
|
'Status' => $export->status->name,
|
|
'Letzte E-Mail' => $export->lead_mails->count() ? $export->lead_mails_sent_at->last()->sent_at : " ",
|
|
);
|
|
}
|
|
}
|
|
return Excel::download(new ReportCollectionExport($columns, $headers), $filename.'.xls');
|
|
}
|
|
|
|
} |