| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Http\Controllers\Controller;
- use App\Models\Expense;
- use App\Models\Laundry;
- use App\Models\Mutation;
- use App\Models\Product;
- use App\Models\Transaction;
- use App\Services\ExpenseService;
- use App\Services\MutationService;
- use App\Services\TransactionService;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Inertia\Inertia;
-
- class DashboardController extends Controller
- {
- /**
- * Handle the incoming request.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Inertia\Response
- */
- public function __invoke(Request $request)
- {
- $transactions = Transaction::whereDate('created_at', date('Y-m-d'))->get();
-
- $expenses = Expense::whereDate('created_at', date('Y-m-d'))->get();
-
- $laundries = Laundry::get();
-
- $products = Product::get();
-
- if (request()->user()->outlet_id !== null) {
- request()->merge(['outlet' => request()->user()->outlet_id]);
- }
- $transactionDiscount = Transaction::filter(request()->only('outlet'))
- ->whereMonth('created_at', date('m'))
- ->whereNotIn('discount', [0])
- ->get();
-
- $transactionChart = Transaction::get()
- ->groupBy([
- fn($transaction) => Carbon::parse($transaction->getRawOriginal('created_at'))->format('Y'),
- fn($transaction) => Carbon::parse($transaction->getRawOriginal('created_at'))->format('M'),
- ]);
-
- $mutationChart = Mutation::whereYear('created_at', date('Y'))
- ->get()
- ->groupBy([
- fn($mutation) => $mutation->type,
- fn($mutation) => Carbon::parse($mutation->getRawOriginal('created_at'))->format('M'),
- ]);
-
- $transactionOutletChart = Transaction::whereYear('created_at', date('Y'))
- ->whereMonth('created_at', date('m'))
- ->get()
- ->groupBy([
- fn($transaction) => Carbon::parse($transaction->getRawOriginal('created_at'))->format('M'),
- fn($transaction) => $transaction->outlet->name,
- ]);
-
- $topTransactionChart = Transaction::get()
- ->groupBy('customer_number');
-
- return inertia('home/Index', [
- 'cardStatistics' => [
- [
- 'title' => __('words.transaction'),
- 'icon' => 'pi pi-shopping-cart',
- 'amount' => $transactions->count(),
- 'amountLabel' => __('words.today'),
- 'value' => (new TransactionService)->totalPriceGroupAsString($transactions),
- ],
- [
- 'title' => __('words.expense'),
- 'icon' => 'pi pi-wallet',
- 'amount' => $expenses->count(),
- 'amountLabel' => __('words.today'),
- 'value' => (new ExpenseService)->totalPriceAsString($expenses),
- ],
- [
- 'title' => __('words.discount_given'),
- 'icon' => 'pi pi-percentage',
- 'amount' => $transactionDiscount->count(),
- 'amountLabel' => __('words.this_month'),
- 'value' => (new TransactionService)->totalDiscountGivenGroupAsString($transactionDiscount),
- ],
- [
- 'title' => __('words.laundry_type'),
- 'icon' => 'pi pi-table',
- 'amount' => $laundries->count(),
- 'amountLabel' => __('words.total'),
- ],
- [
- 'title' => __('words.product_type'),
- 'icon' => 'pi pi-table',
- 'amount' => $products->count(),
- 'amountLabel' => __('words.total'),
- ],
- ],
- 'chartTransactionStatistics' => [
- 'transaction' => [
- 'title' => __('words.transaction_statistic'),
- 'description' => __('words.per_year') . ' ' . now()->subYear(1)->format('Y') . '-' . date('Y'),
- 'data' => (new TransactionService)->statisticData($transactionChart, -2),
- ],
- 'transactionMutation' => [
- 'title' => __('words.mutation_statistic'),
- 'description' => __('words.per_year') . ' ' . date('Y'),
- 'data' => (new MutationService)->statisticData($mutationChart, -2),
- ],
- ],
- 'chartOutletStatistic' => [
- 'title' => __('words.transaction_outlet_statistic'),
- 'description' => Carbon::parse(date('Y-m-d'))->translatedFormat('F, Y'),
- 'data' => (new TransactionService)->statisticData($transactionOutletChart)->first(),
- ],
- 'chartTopTransactionStatistic' => [
- 'title' => __('words.top_customer'),
- 'description' => __('words.top_number_customer', ['number' => 5]),
- 'data' => (new TransactionService)->topTransaction($topTransactionChart),
- ],
- ]);
- }
- }
|