Spaces:
Sleeping
Sleeping
File size: 2,278 Bytes
10dc6f2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | <?php
namespace App\Http\Controllers;
use App\Models\Order;
use App\Services\OrderService;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\StreamedResponse;
class OrdersController extends Controller
{
public function index(Request $request)
{
$query = Order::where('user_id', auth()->id())->with('event');
if ($request->filled('status')) {
$query->where('status', $request->status);
}
$orders = $query->latest()->paginate(10);
return view('orders.index', compact('orders'));
}
public function show(Order $order)
{
abort_unless($order->user_id === auth()->id(), 403);
$order->load('event', 'items.ticketTier', 'attendees');
return view('orders.show', compact('order'));
}
public function cancel(Order $order, OrderService $orderService)
{
abort_unless($order->user_id === auth()->id(), 403);
if (!$order->isPaid()) {
return back()->with('error', 'Hanya order yang sudah dibayar yang bisa dibatalkan.');
}
$success = $orderService->cancelOrder($order);
if (!$success) {
return back()->with('error', 'Tidak bisa membatalkan order. Batas waktu refund sudah lewat (minimal 24 jam sebelum event).');
}
return back()->with('success', 'Order berhasil dibatalkan dan akan direfund.');
}
public function exportCsv(): StreamedResponse
{
$orders = Order::where('user_id', auth()->id())
->with('event')
->latest()
->get();
return response()->streamDownload(function () use ($orders) {
$handle = fopen('php://output', 'w');
fputcsv($handle, ['Order Code', 'Event', 'Total', 'Status', 'Tanggal']);
foreach ($orders as $order) {
fputcsv($handle, [
$order->order_code,
$order->event->title,
'Rp ' . number_format($order->total, 0, ',', '.'),
$order->status,
$order->created_at->format('d/m/Y H:i'),
]);
}
fclose($handle);
}, 'orders-' . now()->format('Ymd') . '.csv', ['Content-Type' => 'text/csv']);
}
}
|