MIF_E31221305/TA_API/app/Http/Controllers/Api/TailorDashboardController.php

63 lines
2.1 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Models\Booking;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
class TailorDashboardController extends BaseController
{
/**
* Get tailor dashboard data
*/
public function index()
{
try {
$tailor_id = Auth::id();
$now = Carbon::now();
// Get incoming bookings (status 'pending')
$incomingBookings = Booking::where('tailor_id', $tailor_id)
->where('status', 'pending')
->with(['customer:id,name', 'customer.ratings'])
->orderBy('created_at', 'desc')
->get();
// Calculate current month's earnings
$currentMonthEarnings = Booking::where('tailor_id', $tailor_id)
->where('status', 'selesai')
->where('payment_status', 'paid')
->whereYear('updated_at', $now->year)
->whereMonth('updated_at', $now->month)
->sum('total_price');
// Calculate total earnings
$totalEarnings = Booking::where('tailor_id', $tailor_id)
->where('status', 'selesai')
->where('payment_status', 'paid')
->sum('total_price');
// Get average rating
$averageRating = DB::table('tailor_ratings')
->where('tailor_id', $tailor_id)
->avg('rating');
return $this->sendResponse([
'incoming_bookings' => $incomingBookings,
'current_month_earnings' => $currentMonthEarnings,
'total_earnings' => $totalEarnings,
'average_rating' => round($averageRating ?? 0, 1),
'total_completed_orders' => Booking::where('tailor_id', $tailor_id)
->where('status', 'selesai')
->count()
], 'Data dashboard berhasil diambil');
} catch (\Exception $e) {
return $this->sendError('Error.', ['error' => 'Terjadi kesalahan saat mengambil data dashboard'], 500);
}
}
}