mein-sterntours/app/Http/Controllers/Admin/ReportBookingController.php
Kevin Adametz 881fc84207 08 2024
2024-08-05 11:58:09 +02:00

396 lines
18 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use Request;
use Carbon\Carbon;
use App\Models\Status;
use App\Services\Util;
use App\Models\Booking;
use App\Models\TravelCompany;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\ReportCollectionExport;
class ReportBookingController extends Controller
{
public function __construct()
{
$this->middleware(['superadmin', '2fa']);
}
public function bookings()
{
$data = [
'filter_lead_status' => Status::get()->pluck('name', 'id')->toArray(),
'filter_travel_companies' => TravelCompany::where('active', true)->get()->pluck('name', 'id')->toArray(),
];
return view('admin.report.bookings', $data);
}
public function checkBookings()
{
$data = [
'filter_lead_status' => Status::get()->pluck('name', 'id')->toArray(),
'filter_travel_companies' => TravelCompany::where('active', true)->get()->pluck('name', 'id')->toArray(),
];
return view('admin.report.check_bookings', $data);
}
//checkBookingsDatatable
private function prozessBookingSearch()
{
$query = Booking::with( 'customer', 'lead', 'booking_strono','service_provider_entries', 'service_provider_entries.service_provider')->select('booking.*');
if(Request::get('filter_db_lead_status_id') != ""){
$query->whereHas('lead', function ($q) {
$q->whereIn('status_id', Request::get('filter_db_lead_status_id'));
});
}
if(Request::get('filter_lead_status_id') != ""){
$query->whereHas('lead', function ($q) {
$q->whereIn('status_id', Request::get('filter_lead_status_id'));
});
}
if(Request::get('filter_travel_company_id') != ""){
$query->whereIn("travel_company_id", Request::get('filter_travel_company_id'));
}
if(Request::get('filter_travel_date_from') != ""){
$travel_date_from = Carbon::parse(Request::get('filter_travel_date_from'))->format("Y-m-d");
$query->where("start_date", '>=', $travel_date_from);
}
if(Request::get('filter_travel_date_to') != ""){
$travel_date_to = Carbon::parse(Request::get('filter_travel_date_to'))->format("Y-m-d");
$query->where("start_date", '<=', $travel_date_to);
}
if(Request::get('filter_booking_date_from') != ""){
$filter_booking_date_from = Carbon::parse(Request::get('filter_booking_date_from'))->format("Y-m-d");
$query->where("booking_date", '>=', $filter_booking_date_from);
}
if(Request::get('filter_booking_date_to') != ""){
$filter_booking_date_from = Carbon::parse(Request::get('filter_booking_date_to'))->format("Y-m-d");
$query->where("booking_date", '<=', $filter_booking_date_from);
}
return $query;
}
public function bookingsDatatable()
{
$query = $this->prozessBookingSearch();
return \DataTables::eloquent($query)
->with('price_total_sum', function() use ($query) {
$price1 = with(clone $query)->whereNull('canceled')->sum('price');
$price2 = with(clone $query)->whereNotNull('canceled')->sum('price_canceled');
return Util::_number_format($price1+$price2);
})
->with('price_total_total_sum', function() use ($query) {
return Util::_number_format($query->sum('price_total'));
})
->with('proceed_total_sum', function() use ($query) {
if($query->count() > 200){
return 'max 200 ';
}
$all = $query->get();
$proceeds = 0;
foreach ($all as $v){
$proceeds += $v->proceeds(true);
}
return Util::_number_format($proceeds);
})
->addColumn('id', function (Booking $booking) {
return '<a data-order="' . $booking->id . '" href="' . route('booking_detail', [$booking->id]) . '" data-id="' . $booking->id . '">' . $booking->id . '</a>';
})
->addColumn('customer.fullName', function (Booking $booking) {
return $booking->customer->fullName();
})
->addColumn('price', function (Booking $booking) {
return $booking->booking_strono ? $booking->price_canceled : $booking->price;
})
->addColumn('proceeds', function (Booking $booking) {
return $booking->proceeds();
})
->addColumn('start_date', function (Booking $booking) {
return $booking->getStartDateFormat();
})
->addColumn('end_date', function (Booking $booking) {
return $booking->getEndDateFormat();
})
->addColumn('booking_date', function (Booking $booking) {
return $booking->getBookingDateFormat();
})
->addColumn('booking_strono_date', function (Booking $booking) {
return $booking->booking_strono ? $booking->booking_strono->storno_date->format("d.m.Y") : "";
})
->addColumn('service_provider.names', function (Booking $booking) {
$ret = "";
if($booking->service_provider_entries){
foreach ($booking->service_provider_entries as $service_provider_entry){
$ret .= '<span class="ui-stars">'.$service_provider_entry->service_provider->name." | ";
$ret .= $service_provider_entry->getAmountFinalEur()." | ";
$ret .= $service_provider_entry->is_cleared ? '<i class="fa fa-check text-success"></i>' : '<i class="fa fa-times text-danger"></i>';
$ret .= "</span><br>";
}
}
return $ret === "" ? "-" : $ret;
})
->addColumn('lead.status_id', function (Booking $booking) {
if($booking->lead && $booking->lead->status_id){
$color = $booking->lead->status->color;
$icon = "";
if($booking->lead->status_id == 14 && $booking->lead->is_rebook){
$color = '#94ae59';
$icon = '<i class="fa fa-check-circle"></i> ';
}
if($booking->lead->status_id == 14 && !$booking->lead->is_rebook){
$icon = '<i class="fa fa-times-circle"></i> ';
}
return '<span data-order="'.$booking->lead->status_id.'"><span class="badge badge-dark" style="background-color: '.$color.'">'.$icon.$booking->lead->status->name.'</span></span>';
}
return '<span data-order="0">-</span>';
})
->filterColumn('customer.fullName', function ($query, $keyword) {
if ($keyword != "") {
$query->whereHas('customer', function ($q) use ($keyword) {
$q->where("name", 'LIKE', '%' . $keyword . '%')
->orWhere('firstname', 'LIKE', '%' . $keyword . '%');
});
}
})
->orderColumn('id', 'id $1')
->orderColumn('start_date', 'start_date $1')
->orderColumn('end_date', 'end_date $1')
->orderColumn('price', 'price $1')
->orderColumn('booking_date', 'booking_date $1')
->orderColumn('customer.fullName', 'customer.firstname $1')
->orderColumn('customer.firstname', 'customer.firstname $1')
->orderColumn('customer.name', 'customer.name $1')
//->orderColumn('lead.status_id', 'lead.status_id $1')
//->orderColumn('is_cleared', 'is_cleared $1')
->rawColumns(['id', 'lead.status_id', 'service_provider.names'])
->make(true);
}
public function bookingsExport(){
$query = $this->prozessBookingSearch();
$order = explode(",", Request::get('order'));
$orderByNum = [
0 => "id",
2 => "merlin_order_number",
3 => "price",
4 => "price_total",
7 => "start_date",
8 => "end_date",
9 => "booking_date",
];
if(isset($order[0])) {
$column = isset($orderByNum[$order[0]]) ? $orderByNum[$order[0]] : "start_date";
$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_".date('Y-m-d-H-i-s');
$headers = array(
'BuchungsID',
'Status',
'Storno',
'MyJack Nr.',
'Organisation',
'Reisepreis',
'Erlös',
'Kunde',
'Reisedatum',
'bis',
'Buchungsdatum',
'Reiseland',
'Reiseprogramm',
'Reiseteilnehmer',
'Leistungsträger',
'Zahlung',
'Zahlungsdatum',
'Rechnungsnummer',
'abgeschlossen',
);
$total_price = 0;
$total_price_total = 0;
$total_proceeds = 0;
$total_amount_final = 0;
foreach($exports as $export) {
$storno_date = $export->booking_strono ? $export->booking_strono->storno_date->format("d.m.Y") : "";
if($export->service_provider_entries->count()){
$new = true;
foreach ($export->service_provider_entries as $service_provider_entry){
if($new){
$total_price += $export->isCanceled() ? $export->getPriceCanceledRaw() : $export->getPriceRaw();
$total_price_total += $export->getPriceTotalRaw();
$total_proceeds += $export->proceeds(true);
}
$total_amount_final += $service_provider_entry->getAmountFinalEurRaw();
$columns[] = array(
'BuchungsID' => $new ? $export->id : "",
'Status' => $new ? $export->lead->status->name : "",
'Storno' => $new ? $storno_date : "",
'MyJack Nr.' => $new ? $export->merlin_order_number : "",
'Organisation' => $new ? ($export->isCanceled() ? $export->price_canceled : $export->price) : "",
'Reisepreis' => $new ? $export->price_total : "",
'Erlös' => $new ? $export->proceeds() : "",
'Kunde' => $new ? $export->customer->fullName() : "",
'Reisedatum' => $new ? $export->getStartDateFormat() : "",
'bis' => $new ? $export->getEndDateFormat() : "",
'Buchungsdatum' => $new ? $export->getBookingDateFormat() : "",
'Reiseland' => $new && $export->travel_country ? $export->travel_country->name : "",
'Reiseprogramm' => $new && $export->travel_agenda ? $export->travel_agenda->name : "",
'Reiseteilnehmer' => $new ? $export->pax : "",
'Leistungsträger' => $service_provider_entry->service_provider->name,
'Zahlung' => $service_provider_entry->getAmountFinalEur(),
'Zahlungsdatum' => $service_provider_entry->getPaymentDateFormat(),
'Rechnungsnummer' => $service_provider_entry->invoice_number,
'abgeschlossen' => $service_provider_entry->is_cleared ? 'J' : 'N',
);
$new = false;
}
}else{
//$total_price += $export->booking->isCanceled() ? $export->booking->getPriceCanceledRaw() : $export->booking->getPriceRaw();
$total_price += $export->isCanceled() ? $export->getPriceCanceledRaw() : $export->getPriceRaw();
$columns[] = array(
'BuchungsID' => $export->id,
'Status' => $export->lead->status->name,
'Storno' => $storno_date,
'MyJack Nr.' => $export->merlin_order_number,
'Organisation' => $export->price,
'Organisation' => $export->isCanceled() ? $export->price_canceled : $export->price,
'Reisepreis' => $export->price_total,
'Erlös' => $export->proceeds(),
'Kunde' => $export->customer->fullName(),
'Reisedatum' => $export->getStartDateFormat(),
'bis' => $export->getEndDateFormat(),
'Buchungsdatum' => $export->getBookingDateFormat(),
'Reiseland' => $export->travel_country ? $export->travel_country->name : "",
'Reiseprogramm' => $export->travel_agenda ? $export->travel_agenda->name : "",
'Reiseteilnehmer' => $export->pax,
'Leistungsträger' => "",
'Zahlung' => "",
'Zahlungsdatum' => "",
'Rechnungsnummer' => "",
'abgeschlossen' => "",
);
}
}
$columns[] = array(
'BuchungsID' => "Total",
'Status' => "",
'Storno' => "",
'MyJack Nr.' => "",
'Organisation' => Util::_number_format($total_price),
'Reisepreis' => Util::_number_format($total_price_total),
'Erlös' => Util::_number_format($total_proceeds),
'Kunde' => "",
'Reisedatum' => "",
'bis' => "",
'Buchungsdatum' => "",
'Reiseland' => "",
'Reiseprogramm' => "",
'Reiseteilnehmer' => "",
'Leistungsträger' => "",
'Zahlung' => Util::_number_format($total_amount_final),
'Zahlungsdatum' => "",
'Rechnungsnummer' => "",
'abgeschlossen' => "",
);
}
return Excel::download(new ReportCollectionExport($columns, $headers), $filename.'.xls');
}
public function checkBookingsDatatable()
{
$query = $this->prozessBookingSearch();
return \DataTables::eloquent($query)
->addColumn('id', function (Booking $booking) {
return '<a data-order="' . $booking->id . '" href="' . route('booking_detail', [$booking->id]) . '" data-id="' . $booking->id . '">' . $booking->id . '</a>';
})
->addColumn('old_crm', function (Booking $booking) {
return '<a data-order="'.$booking->id.'" href="'.make_old_url('booking/'.$booking->id.'/edit').'" data-id="'.$booking->id.'">'.$booking->id.'</a>';
})
->addColumn('id', function (Booking $booking) {
return '<a data-order="' . $booking->id . '" href="' . route('booking_detail', [$booking->id]) . '" data-id="' . $booking->id . '">' . $booking->id . '</a>';
})
->addColumn('price', function (Booking $booking) {
return $booking->price;
})
->addColumn('service_total', function (Booking $booking) {
return $booking->getServiceTotal();
})
->addColumn('price_total', function (Booking $booking) {
return $booking->price_total;
})
->addColumn('check_total', function (Booking $booking) {
$check = $booking->getPriceRaw() + $booking->getServiceTotal(true);
return ($booking->getPriceTotalRaw() != $check) ? '<span class="badge badge-danger">'.Util::_number_format($check).'</span>' : "";
})
->addColumn('price_canceled', function (Booking $booking) {
return $booking->isCanceled() ? $booking->price_canceled : "";
})
->addColumn('start_date', function (Booking $booking) {
return $booking->getStartDateFormat();
})
->addColumn('end_date', function (Booking $booking) {
return $booking->getEndDateFormat();
})
->addColumn('booking_date', function (Booking $booking) {
return $booking->getBookingDateFormat();
})
->addColumn('lead.status_id', function (Booking $booking) {
if($booking->lead && $booking->lead->status_id){
$color = $booking->lead->status->color;
$icon = "";
if($booking->lead->status_id == 14 && $booking->lead->is_rebook){
$color = '#94ae59';
$icon = '<i class="fa fa-check-circle"></i> ';
}
if($booking->lead->status_id == 14 && !$booking->lead->is_rebook){
$icon = '<i class="fa fa-times-circle"></i> ';
}
return '<span data-order="'.$booking->lead->status_id.'"><span class="badge badge-dark" style="background-color: '.$color.'">'.$icon.$booking->lead->status->name.'</span></span>';
}
return '<span data-order="0">-</span>';
})
->orderColumn('id', 'id $1')
->orderColumn('old_crm', 'old_crm $1')
->orderColumn('start_date', 'start_date $1')
->orderColumn('end_date', 'end_date $1')
->orderColumn('price', 'price $1')
->orderColumn('booking_date', 'booking_date $1')
->orderColumn('customer.firstname', 'customer.firstname $1')
->orderColumn('customer.name', 'customer.name $1')
//->orderColumn('lead.status_id', 'lead.status_id $1')
//->orderColumn('is_cleared', 'is_cleared $1')
->rawColumns(['id', 'old_crm', 'check_total', 'lead.status_id'])
->make(true);
}
}