Files
WitShop/app/Http/Controllers/Admin/OrderController.php
T
marcoandClaude Sonnet 4.6 248fb21a05
Deploy to Production (witstore.wende.it) / deploy (push) Successful in 7s
Add PayPal, Stripe, CSV-Export; fix .env.production issue
- PayPal: Checkout-Integration via srmklive/paypal, Weiterleitung zu PayPal,
  Bestellung wird nach Capture automatisch auf payment_received gesetzt
- Stripe: Payment Elements im Checkout, Intent-API, Zahlung direkt bestätigt
- CSV-Export: Admin → Bestellungen → CSV exportieren (UTF-8 BOM für Excel)
- PayPal/Stripe nur sichtbar wenn Keys in .env gesetzt
- .env.example um PAYPAL_* und STRIPE_* Keys erweitert

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-21 11:24:02 +02:00

115 lines
4.1 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Mail\PaymentReceivedMail;
use App\Models\Order;
use App\Services\InvoiceService;
use App\Services\OrderService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class OrderController extends Controller
{
public function __construct(
private OrderService $orderService,
private InvoiceService $invoiceService,
) {}
public function index(Request $request)
{
$query = Order::with('addresses')->latest();
if ($request->filled('status')) {
$query->where('status', $request->status);
}
if ($request->filled('payment')) {
$query->where('payment_method', $request->payment);
}
if ($request->filled('q')) {
$query->where('order_number', 'like', '%'.$request->q.'%');
}
$orders = $query->paginate(25);
return view('admin.orders.index', compact('orders'));
}
public function show(Order $order)
{
$order->load('items', 'addresses', 'user');
return view('admin.orders.show', compact('order'));
}
public function updateStatus(Request $request, Order $order)
{
$data = $request->validate([
'status' => 'required|in:' . implode(',', array_keys(Order::$statusLabels)),
'tracking_code' => 'nullable|string|max:255',
]);
$this->orderService->updateStatus($order, $data['status'], $data['tracking_code'] ?? null);
// Bei Zahlungseingang: Rechnung per Mail senden
if ($data['status'] === 'payment_received') {
$pdf = $this->invoiceService->generate($order);
$email = $order->guest_email ?? $order->user?->email;
if ($email) {
Mail::to($email)->send(new PaymentReceivedMail($order, $pdf->output(), $this->invoiceService->filename($order)));
}
}
return back()->with('success', 'Status aktualisiert.');
}
public function sendInvoice(Order $order)
{
$pdf = $this->invoiceService->generate($order);
$email = $order->guest_email ?? $order->user?->email;
if ($email) {
Mail::to($email)->send(new PaymentReceivedMail($order, $pdf->output(), $this->invoiceService->filename($order)));
}
return back()->with('success', 'Rechnung erneut gesendet.');
}
public function downloadInvoice(Order $order)
{
return $this->invoiceService->generate($order)->download($this->invoiceService->filename($order));
}
public function exportCsv()
{
$orders = Order::with(['items','billingAddress'])->orderByDesc('created_at')->get();
$rows = [];
$rows[] = ['Bestellnr.','Datum','Status','Zahlungsart','Kunde','E-Mail','Zwischensumme','Rabatt','Versand','Gesamt','MwSt.'];
foreach ($orders as $order) {
$addr = $order->billingAddress();
$rows[] = [
$order->order_number,
$order->created_at->format('d.m.Y H:i'),
Order::$statusLabels[$order->status] ?? $order->status,
Order::$paymentLabels[$order->payment_method] ?? $order->payment_method,
$addr ? $addr->full_name : ($order->guest_email ?? ''),
$order->guest_email ?? $order->user?->email ?? '',
number_format($order->subtotal, 2, ',', '.'),
number_format($order->discount_amount ?? 0, 2, ',', '.'),
number_format($order->shipping_cost, 2, ',', '.'),
number_format($order->total, 2, ',', '.'),
number_format($order->tax_total, 2, ',', '.'),
];
}
$csv = "\xEF\xBB\xBF"; // UTF-8 BOM für Excel
foreach ($rows as $row) {
$csv .= implode(';', array_map(fn($v) => '"' . str_replace('"', '""', $v) . '"', $row)) . "\n";
}
return response($csv, 200, [
'Content-Type' => 'text/csv; charset=UTF-8',
'Content-Disposition' => 'attachment; filename="bestellungen-' . date('Y-m-d') . '.csv"',
]);
}
}