File: D:/HostingSpaces/farmfun/reserveren.farmfun.be/app/Komma/Export/Kms/ExportController.php
<?php
namespace App\Komma\Export\Kms;
use App\Komma\Export\Exports\OrdersExport;
use App\Komma\Export\Exports\ReservationsExport;
use App\Komma\Orders\Models\Order;
use App\Komma\Reservations\Models\Reservation;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Maatwebsite\Excel\Facades\Excel;
class ExportController extends Controller
{
protected $slug = 'export';
public function index() {
return \View::make('kms/section.export', [
'slug' => $this->slug,
'siteSlug' => null,
]);
}
public function create(Request $request) {
$startDate = $request->input('startDate_date');
$endDate = $request->input('endDate_date');
$startDate_format = (new Carbon($startDate))->startOfDay();
$endDate_format = (new Carbon($endDate))->endOfDay();
$type = $request->input('type');
switch ($type) {
case 'orders':
$export = new OrdersExport(
Order::whereBetween('created_at', [
$startDate_format->format(Carbon::DEFAULT_TO_STRING_FORMAT),
$endDate_format->format(Carbon::DEFAULT_TO_STRING_FORMAT),
])
->with('lines.product.translation', 'lines.location')
->orderBy('created_at')
->get()
);
break;
case 'reservations':
$export = new ReservationsExport(
Reservation::whereBetween('date', [
$startDate_format->format(Carbon::DEFAULT_TO_STRING_FORMAT),
$endDate_format->format(Carbon::DEFAULT_TO_STRING_FORMAT),
])
->with('items', 'items.product.translation', 'items.location')
->orderBy('date')
->get()
);
break;
default:
throw new \UnexpectedValueException('Unknown handling for export type: '.$type);
}
ob_end_clean();
ob_start();
return Excel::download($export, $startDate_format->format('Ymd').'-'.$endDate_format->format('Ymd').'-'.$type.'.xlsx');
}
}