diff --git a/app/Http/Controllers/GeneralController.php b/app/Http/Controllers/GeneralController.php index aa9b742..fcad45e 100644 --- a/app/Http/Controllers/GeneralController.php +++ b/app/Http/Controllers/GeneralController.php @@ -37,13 +37,30 @@ class GeneralController extends Controller $endDate = Carbon::parse($request->end_date)->format('m/d/Y'); } + $targets = []; + + $originTarget = (Setting::where('key', 'target')->value('value') ?? 90000); + $target = $originTarget / 30; + + $c = []; + $order = 'asc'; + if ($request->order != '') { + $order = $request->order; + } $charts = Sale::selectRaw('SUM(total) as stotal, date') ->whereBetween('date', [$startDate, $endDate]) - ->orderBy('date', 'asc') + ->orderBy('date', $order) ->groupBy('date') ->get(); - $originTarget = (Setting::where('key', 'target')->value('value') ?? 90000); - $target = $originTarget / 30; + + $date = Carbon::parse($startDate); + while ($date <= Carbon::parse($endDate)) { + $total = $charts->where('date', $date->format('m/d/Y'))->value('stotal') ?? 0; + $c[] = ['stotal' => $total, 'date' => $date->format('m/d/Y')]; + $date = $date->addDay(); + + $targets[] = $target; + } $dounat = SaleItem::selectRaw('product_id, category_id, SUM(quantity) as qty') ->with('product.category') @@ -57,30 +74,52 @@ class GeneralController extends Controller $favoriteProducts = SaleItem::selectRaw('product_id, sum(quantity) as qty') ->with('product') ->join('sales', 'sales.id', '=', 'sale_items.sale_id') + ->join('products', 'products.id', '=', 'sale_items.product_id') ->whereBetween('sales.date', [now()->startOfMonth()->format('m/d/Y'), now()->endOfMonth()->format('m/d/Y')]) - ->orderBy('qty', 'desc') - ->groupBy('product_id') - ->get(); + ->orderBy('qty', $order) + ->groupBy('product_id'); + + if ($request->p_q != '') { + $favoriteProducts->where(function ($query) use ($request) { + $query->where('products.name', 'like', "%$request->p_q%") + ->orWhere('products.code', 'like', "%$request->p_q%"); + }); + } + + $favoriteProducts = $favoriteProducts->get(); $transactionCustomers = Sale::selectRaw('customer_id, sum(total) as stotal') + ->join('customers', 'customers.id', '=', 'sales.customer_id') ->where('date', now()->format('m/d/Y')) ->groupBy('customer_id') - ->orderBy('stotal') - ->with('customer') - ->get(); + ->orderBy('stotal', $order) + ->with('customer'); + + if ($request->c_q != '') { + $transactionCustomers->where(function ($query) use ($request) { + $query->where('customers.name', 'like', "%$request->c_q%"); + }); + } + + $transactionCustomers = $transactionCustomers->get(); return inertia('Dashboard', [ 'total_sale_today' => $totalSaleToday, 'total_item_today' => $totalItem, 'total_item_price_today' => $totalItemPrice, 'total_customer' => $totalCustomer, - 'sale_days' => $charts, + 'sale_days' => $c, + '_startDate' => $startDate, + '_endDate' => $endDate, + '_order' => $order, + '_c_q' => $request->c_q, + '_p_q' => $request->p_q, 'favorite_categories' => $dounat, 'list_favorite_product' => $favoriteProducts, 'list_customer' => $transactionCustomers, 'month' => now()->locale('id')->translatedFormat('F'), 'total_sale_month' => $totalSaleMonth, - 'targets' => [$target, $target, $target, $target, $target, $target, $target, $target], + 'targets' => $targets, 'target' => $originTarget, ]); } diff --git a/resources/js/Pages/Dashboard.jsx b/resources/js/Pages/Dashboard.jsx index a52e090..70f6cb5 100644 --- a/resources/js/Pages/Dashboard.jsx +++ b/resources/js/Pages/Dashboard.jsx @@ -1,6 +1,6 @@ -import React from 'react' +import React, { useEffect, useState } from 'react' import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout' -import { Head } from '@inertiajs/react' +import { Head, router } from '@inertiajs/react' import { formatIDR } from '@/utils' import { Chart as ChartJS, @@ -13,7 +13,11 @@ import { Legend, } from 'chart.js' import { Bar, Doughnut } from 'react-chartjs-2' +import { BiSortAlt2 } from 'react-icons/bi' import moment from 'moment' +import FormInputDateRanger from '@/Components/FormInputDateRange' +import { usePrevious } from 'react-use' +import SearchInput from '@/Components/SearchInput' ChartJS.register( CategoryScale, LinearScale, @@ -26,11 +30,14 @@ ChartJS.register( export default function Dashboard(props) { const { - total_sale_today, total_item_today, total_item_price_today, - total_customer, sale_days, + _startDate, + _endDate, + _order, + _p_q, + _c_q, list_favorite_product, list_customer, favorite_categories, @@ -40,6 +47,14 @@ export default function Dashboard(props) { target, } = props + const [dates, setDates] = useState({ + startDate: _startDate, + endDate: _endDate, + }) + const [order, setOrder] = useState(_order) + const [p_q, setPq] = useState(_p_q) + const [c_q, setCq] = useState(_c_q) + const options = { responsive: true, scales: { @@ -130,6 +145,32 @@ export default function Dashboard(props) { }, } + const preValue = usePrevious({ + dates, + order, + c_q, + p_q, + }) + + useEffect(() => { + if (preValue) { + router.get( + route(route().current()), + { + start_date: dates.startDate, + end_date: dates.endDate, + order, + c_q, + p_q, + }, + { + replace: true, + preserveState: true, + } + ) + } + }, [dates, order, c_q, p_q]) + return (
-
- Penjualan 7 Hari Terakhir +
+
+ Penjualan 7 Hari Terakhir +
+
+
+ +
+
+ + setDates(dates) + } + /> +
+
{/* list produk paling laris dengan jumlah penjualan */}
-
Produk Terlaris
+
+
Produk Terlaris
+
+
+ setOrder( + order === 'asc' ? 'desc' : 'asc' + ) + } + > + +
+ setPq(e.target.value)} + /> +
+
@@ -247,7 +321,27 @@ export default function Dashboard(props) { {/* list customer yang bertransaksi dengan total transaksi */}
-
Pelanggan Hari Ini
+
+
+ Pelanggan Hari Ini +
+
+
+ setOrder( + order === 'asc' ? 'desc' : 'asc' + ) + } + > + +
+ setCq(e.target.value)} + /> +
+