From 33acf73b5ee7524d8c0f24978a0c78f3e9b87dac Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sat, 12 Jul 2025 14:03:12 +0530 Subject: [PATCH] Added invoice dashboard module get api --- .../Controllers/ModuleFilterController.php | 2 +- .../ModuleInvoiceDataController.php | 91 ++++ .../ModuleInvoiceTypeController.php | 497 ++++++++++++++++++ .../Controllers/ModulePlantLineController.php | 11 +- .../ModuleProductionLineStopController.php | 171 ++++++ routes/api.php | 6 + 6 files changed, 774 insertions(+), 4 deletions(-) create mode 100644 app/Http/Controllers/ModuleInvoiceDataController.php create mode 100644 app/Http/Controllers/ModuleInvoiceTypeController.php create mode 100644 app/Http/Controllers/ModuleProductionLineStopController.php diff --git a/app/Http/Controllers/ModuleFilterController.php b/app/Http/Controllers/ModuleFilterController.php index 59ec175..e387ddb 100644 --- a/app/Http/Controllers/ModuleFilterController.php +++ b/app/Http/Controllers/ModuleFilterController.php @@ -41,7 +41,7 @@ class ModuleFilterController extends Controller ], 404); } // If 'line-name' is 'FG Lines', return actual FG Line names - if (strtolower(trim($lineName)) === 'fg lines') { + if (strtolower(trim($lineName)) == 'fg lines') { $fgLines = \App\Models\Line::where('type', 'FG Line') ->orderBy('created_at', 'asc') ->pluck('name'); diff --git a/app/Http/Controllers/ModuleInvoiceDataController.php b/app/Http/Controllers/ModuleInvoiceDataController.php new file mode 100644 index 0000000..c5399b4 --- /dev/null +++ b/app/Http/Controllers/ModuleInvoiceDataController.php @@ -0,0 +1,91 @@ +header('Authorization'); + $expectedToken = $expectedUser . ':' . $expectedPw; + + if ("Bearer " . $expectedToken != $header_auth) + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid authorization token!' + ], 403); + } + + $moduleName = $request->header('invoice-type-list'); + + if (empty($moduleName)) + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invoice type can't be empty!" + ], 404); + } + + $headerValue = $request->header('invoice-type-list'); + + if ($headerValue != 'Invoice Type List') { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid value for 'invoice-type-list' header!" + ], 404); + } + + $invoiceTypes = ['All Invoice', 'Serial Invoice', 'Individual Invoice', 'Bundle Invoice']; + + return response()->json([ + 'status_code' => 'SUCCESS', + 'status_description' => $invoiceTypes + ], 200); + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(string $id) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, string $id) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(string $id) + { + // + } +} diff --git a/app/Http/Controllers/ModuleInvoiceTypeController.php b/app/Http/Controllers/ModuleInvoiceTypeController.php new file mode 100644 index 0000000..52699da --- /dev/null +++ b/app/Http/Controllers/ModuleInvoiceTypeController.php @@ -0,0 +1,497 @@ +header('Authorization'); + // $expectedToken = $expectedUser . ':' . $expectedPw; + + // if ("Bearer " . $expectedToken != $header_auth) + // { + // return response()->json([ + // 'status_code' => 'ERROR', + // 'status_description' => 'Invalid authorization token!' + // ], 403); + // } + + // $moduleName = $request->header('invoice-name'); + + // if (empty($moduleName)) + // { + // return response()->json([ + // 'status_code' => 'ERROR', + // 'status_description' => "Invoice name can't be empty!" + // ], 404); + // } + + // $plantName = $request->header('plant-name'); + // $selectedInvoice = strtolower(trim($request->header('invoice-name'))); + // $activeFilter = strtolower(trim($request->header('filter-name'))); + + // $plant = Plant::where('name', $plantName)->first(); + + // if (!$plant) { + // return response()->json([ + // 'status_code' => 'ERROR', + // 'status_description' => "Plant '{$plantName}' not found!" + // ], 404); + // } + + // $selectedPlant = $plant->id; + + // if (empty($selectedInvoice) || empty($selectedPlant) || empty($activeFilter)) { + // return response()->json([ + // 'status_code' => 'ERROR', + // 'status_description' => 'Missing required headers: invoice-name, plant-name, or filter-name' + // ], 404); + // } + + // $validFilters = ['Today', 'Yesterday', 'This Week', 'This Month']; + // $filterHeader = $request->header('filter-name'); + + // if (!in_array($filterHeader, $validFilters)) { + // return response()->json([ + // 'status_code' => 'ERROR', + // 'status_description' => "Invalid filter-name value! Accepted values are: " . implode(', ', $validFilters) + // ], 404); + // } + + // $validInvoiceFilters = ['All Invoice', 'Serial Invoice', 'Individual Invoice', 'Bundle Invoice']; + // $filterInvoice = $request->header('invoice-name'); + + // if (!in_array($filterInvoice, $validInvoiceFilters)) { + // return response()->json([ + // 'status_code' => 'ERROR', + // 'status_description' => "Invalid invoice-name value! Accepted values are: " . implode(', ', $validInvoiceFilters) + // ], 404); + // } + + // if ($activeFilter == 'yesterday') { + // $startDate = now()->subDay()->setTime(8, 0, 0); + // $endDate = now()->setTime(8, 0, 0); + // } elseif ($activeFilter == 'this week') { + // $startDate = now()->startOfWeek()->setTime(8, 0, 0); + // $endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0); + // } elseif ($activeFilter == 'this month') { + // $startDate = now()->startOfMonth()->setTime(8, 0, 0); + // $endDate = now()->endOfMonth()->addDay()->setTime(8, 0, 0); + // } else { + // $startDate = now()->setTime(8, 0, 0); + // $endDate = now()->copy()->addDay()->setTime(8, 0, 0); + // } + + // $labels = []; + // $datasets = []; + + // if ($activeFilter == 'yesterday' || $activeFilter == 'today') + // { + + // // === Imported Invoices === + // $importedQuery = InvoiceValidation::where('plant_id', $selectedPlant) + // ->whereBetween('created_at', [$startDate, $endDate]); + + // if ($selectedInvoice == 'individual invoice') { + // $importedQuery->where('quantity', 1); + // } elseif ($selectedInvoice == 'serial invoice') { + // $importedQuery->whereNull('quantity'); + // } elseif ($selectedInvoice == 'bundle invoice') { + // $importedQuery->where('quantity', '>', 1); + // } + + // $importedCount = $importedQuery->distinct('invoice_number')->count('invoice_number'); + + + + // // === Completed Invoices === + // $completedQuery = InvoiceValidation::select('invoice_number') + // ->where('plant_id', $selectedPlant) + // ->whereBetween('updated_at', [$startDate, $endDate]); + + // if ($selectedInvoice == 'individual invoice') { + // $completedQuery->where('quantity', 1) + // ->groupBy('invoice_number') + // ->havingRaw("COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"); + // } elseif ($selectedInvoice == 'serial invoice') { + // $completedQuery->whereNull('quantity') + // ->groupBy('invoice_number') + // ->havingRaw("COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)"); + // } elseif ($selectedInvoice == 'bundle invoice') { + // $completedQuery->where('quantity', '>', 1) + // ->groupBy('invoice_number') + // ->havingRaw("COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"); + // } + + // $completedCount = $completedQuery->count(); + + // $labels = ['Imported Invoice', 'Completed Invoice']; + // $datasets = [[ + // 'label' => 'Invoices', + // 'data' => [$importedCount, $completedCount], + // ]]; + + + // } + + // elseif ($activeFilter == 'this week') { + // $labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; + // $imported = array_fill(0, 7, 0); + // $completed = array_fill(0, 7, 0); + + // for ($i = 0; $i < 7; $i++) { + // $dayStart = now()->startOfWeek()->addDays($i)->setTime(8, 0, 0); + // $dayEnd = $dayStart->copy()->addDay()->setTime(8, 0, 0); + + // $importedQuery = InvoiceValidation::where('plant_id', $selectedPlant) + // ->whereBetween('created_at', [$dayStart, $dayEnd]); + + // $completedQuery = InvoiceValidation::select('invoice_number') + // ->where('plant_id', $selectedPlant) + // ->whereBetween('updated_at', [$dayStart, $dayEnd]); + + // if ($selectedInvoice == 'individual invoice') { + // $importedQuery->where('quantity', 1); + // $completedQuery->where('quantity', 1) + // ->groupBy('invoice_number') + // ->havingRaw("COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"); + // } elseif ($selectedInvoice == 'serial invoice') { + // $importedQuery->whereNull('quantity'); + // $completedQuery->whereNull('quantity') + // ->groupBy('invoice_number') + // ->havingRaw("COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)"); + // } elseif ($selectedInvoice == 'bundle invoice') { + // $importedQuery->where('quantity', '>', 1); + // $completedQuery->where('quantity', '>', 1) + // ->groupBy('invoice_number') + // ->havingRaw("COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"); + // } + + // $imported[$i] = $importedQuery->distinct('invoice_number')->count('invoice_number'); + // $completed[$i] = $completedQuery->count(); + // } + + // $datasets = [ + // ['label' => 'Imported Invoices', 'data' => $imported], + // ['label' => 'Completed Invoices', 'data' => $completed], + // ]; + // } + + // elseif ($activeFilter == 'this month') { + // $startOfMonth = now()->startOfMonth()->setTime(8, 0, 0); + // $endOfMonth = now()->endOfMonth()->addDay()->setTime(8, 0, 0); + + // $labels = []; + // $imported = []; + // $completed = []; + + // $weekStart = $startOfMonth->copy(); + // while ($weekStart < $endOfMonth) { + // $weekEnd = $weekStart->copy()->addDays(7); + // $label = $weekStart->format('M') . ' (' . $weekStart->format('j') . '-' . $weekEnd->subDay()->format('j') . ')'; + // $labels[] = $label; + + // $importedQuery = InvoiceValidation::where('plant_id', $selectedPlant) + // ->whereBetween('created_at', [$weekStart, $weekEnd]); + + // $completedQuery = InvoiceValidation::select('invoice_number') + // ->where('plant_id', $selectedPlant) + // ->whereBetween('updated_at', [$weekStart, $weekEnd]); + + // if ($selectedInvoice == 'individual invoice') { + // $importedQuery->where('quantity', 1); + // $completedQuery->where('quantity', 1) + // ->groupBy('invoice_number') + // ->havingRaw("COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"); + // } elseif ($selectedInvoice == 'serial invoice') { + // $importedQuery->whereNull('quantity'); + // $completedQuery->whereNull('quantity') + // ->groupBy('invoice_number') + // ->havingRaw("COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)"); + // } elseif ($selectedInvoice == 'bundle invoice') { + // $importedQuery->where('quantity', '>', 1); + // $completedQuery->where('quantity', '>', 1) + // ->groupBy('invoice_number') + // ->havingRaw("COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"); + // } + + // $imported[] = $importedQuery->distinct('invoice_number')->count('invoice_number'); + // $completed[] = $completedQuery->count(); + + // $weekStart = $weekEnd; + // } + + // $datasets = [ + // ['label' => 'Imported Invoices', 'data' => $imported], + // ['label' => 'Completed Invoices', 'data' => $completed], + // ]; + // } + + // // Clean zero values to null + // foreach ($datasets as &$dataset) { + // $dataset['data'] = array_map(function ($value) { + // return ($value === null) ? 0 : $value; + // }, $dataset['data']); + // } + + + // return response()->json([ + // 'status_code' => 'SUCCESS', + // 'labels' => $labels, + // 'datasets' => $datasets, + // ], 200); + // } + + /** + * Store a newly created resource in storage. + */ + public function get_invoiceCountData(Request $request) + { + $expectedUser = env('API_AUTH_USER'); + $expectedPw = env('API_AUTH_PW'); + $header_auth = $request->header('Authorization'); + $expectedToken = $expectedUser . ':' . $expectedPw; + + if ("Bearer " . $expectedToken != $header_auth) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid authorization token!' + ], 403); + } + + $plantName = $request->header('plant-name'); + $filterHeader = $request->header('filter-name'); + $invoiceHeader = $request->header('invoice-name'); + + if (empty($plantName) || empty($invoiceHeader) || empty($filterHeader)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Missing required headers: plant-name, invoice-name, or filter-name" + ], 404); + } + + $validFilters = ['Today', 'Yesterday', 'This Week', 'This Month']; + if (!in_array($filterHeader, $validFilters)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid filter-name value! Accepted values are: " . implode(', ', $validFilters) + ], 404); + } + + $validInvoiceFilters = ['All Invoice', 'Serial Invoice', 'Individual Invoice', 'Bundle Invoice']; + if (!in_array($invoiceHeader, $validInvoiceFilters)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid invoice-name value! Accepted values are: " . implode(', ', $validInvoiceFilters) + ], 404); + } + + $plant = Plant::where('name', $plantName)->first(); + if (!$plant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant '{$plantName}' not found!" + ], 404); + } + + $selectedPlant = $plant->id; + $activeFilter = strtolower(trim($filterHeader)); + $selectedInvoice = strtolower(trim($invoiceHeader)); + + // Set time range + if ($activeFilter == 'yesterday') { + $startDate = now()->subDay()->setTime(8, 0, 0); + $endDate = now()->setTime(8, 0, 0); + } elseif ($activeFilter == 'this week') { + $startDate = now()->startOfWeek()->setTime(8, 0, 0); + $endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0); + } elseif ($activeFilter == 'this month') { + $startDate = now()->startOfMonth()->setTime(8, 0, 0); + $endDate = now()->endOfMonth()->addDay()->setTime(8, 0, 0); + } else { + $startDate = now()->setTime(8, 0, 0); + $endDate = now()->copy()->addDay()->setTime(8, 0, 0); + } + + $labels = []; + $datasets = []; + + // Map types for All Invoice + $invoiceTypes = $selectedInvoice == 'all invoice' + ? [ + 'Individual Invoice' => fn($q) => $q->where('quantity', 1), + 'Serial Invoice' => fn($q) => $q->whereNull('quantity'), + 'Bundle Invoice' => fn($q) => $q->where('quantity', '>', 1) + ] + : [ + ucfirst($selectedInvoice) => match ($selectedInvoice) { + 'individual invoice' => fn($q) => $q->where('quantity', 1), + 'serial invoice' => fn($q) => $q->whereNull('quantity'), + 'bundle invoice' => fn($q) => $q->where('quantity', '>', 1), + } + ]; + + foreach ($invoiceTypes as $labelPrefix => $queryModifier) { + if ($activeFilter == 'yesterday' || $activeFilter == 'today') { + $importedQuery = InvoiceValidation::where('plant_id', $selectedPlant) + ->whereBetween('created_at', [$startDate, $endDate]); + + $queryModifier($importedQuery); + $importedCount = $importedQuery->distinct('invoice_number')->count('invoice_number'); + + $completedQuery = InvoiceValidation::select('invoice_number') + ->where('plant_id', $selectedPlant) + ->whereBetween('updated_at', [$startDate, $endDate]); + + if ($selectedInvoice == 'serial invoice' || $labelPrefix == 'Serial Invoice') { + $queryModifier($completedQuery) + ->groupBy('invoice_number') + ->havingRaw("COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)"); + } else { + $queryModifier($completedQuery) + ->groupBy('invoice_number') + ->havingRaw("COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"); + } + + $completedCount = $completedQuery->count(); + + $labels = ['Imported Invoice', 'Completed Invoice']; + $datasets[] = [ + 'label' => "$labelPrefix", + 'data' => [$importedCount, $completedCount], + ]; + } + + elseif ($activeFilter == 'this week') { + $labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']; + $imported = array_fill(0, 7, 0); + $completed = array_fill(0, 7, 0); + + for ($i = 0; $i < 7; $i++) { + $dayStart = now()->startOfWeek()->addDays($i)->setTime(8, 0, 0); + $dayEnd = $dayStart->copy()->addDay()->setTime(8, 0, 0); + + $importedQuery = InvoiceValidation::where('plant_id', $selectedPlant) + ->whereBetween('created_at', [$dayStart, $dayEnd]); + $queryModifier($importedQuery); + $imported[$i] = $importedQuery->distinct('invoice_number')->count('invoice_number'); + + $completedQuery = InvoiceValidation::select('invoice_number') + ->where('plant_id', $selectedPlant) + ->whereBetween('updated_at', [$dayStart, $dayEnd]); + + if ($selectedInvoice == 'serial invoice' || $labelPrefix == 'Serial Invoice') { + $queryModifier($completedQuery) + ->groupBy('invoice_number') + ->havingRaw("COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)"); + } else { + $queryModifier($completedQuery) + ->groupBy('invoice_number') + ->havingRaw("COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"); + } + + $completed[$i] = $completedQuery->count(); + } + + $datasets[] = ['label' => "$labelPrefix Imported", 'data' => $imported]; + $datasets[] = ['label' => "$labelPrefix Completed", 'data' => $completed]; + } + + elseif ($activeFilter == 'this month') { + $startOfMonth = now()->startOfMonth()->setTime(8, 0, 0); + $endOfMonth = now()->endOfMonth()->addDay()->setTime(8, 0, 0); + $labels = []; + $imported = []; + $completed = []; + + $weekStart = $startOfMonth->copy(); + while ($weekStart < $endOfMonth) { + $weekEnd = $weekStart->copy()->addDays(7); + $label = $weekStart->format('M') . ' (' . $weekStart->format('j') . '-' . $weekEnd->subDay()->format('j') . ')'; + $labels[] = $label; + + $importedQuery = InvoiceValidation::where('plant_id', $selectedPlant) + ->whereBetween('created_at', [$weekStart, $weekEnd]); + $queryModifier($importedQuery); + $imported[] = $importedQuery->distinct('invoice_number')->count('invoice_number'); + + $completedQuery = InvoiceValidation::select('invoice_number') + ->where('plant_id', $selectedPlant) + ->whereBetween('updated_at', [$weekStart, $weekEnd]); + + if ($selectedInvoice == 'serial invoice' || $labelPrefix == 'Serial Invoice') { + $queryModifier($completedQuery) + ->groupBy('invoice_number') + ->havingRaw("COUNT(*) = SUM(CASE WHEN scanned_status = 'Scanned' THEN 1 ELSE 0 END)"); + } else { + $queryModifier($completedQuery) + ->groupBy('invoice_number') + ->havingRaw("COUNT(*) = SUM(CASE WHEN serial_number IS NOT NULL AND serial_number != '' THEN 1 ELSE 0 END)"); + } + + $completed[] = $completedQuery->count(); + $weekStart = $weekEnd; + } + + $datasets[] = ['label' => "$labelPrefix Imported", 'data' => $imported]; + $datasets[] = ['label' => "$labelPrefix Completed", 'data' => $completed]; + } + } + + // Ensure 0 instead of null + foreach ($datasets as &$dataset) { + $dataset['data'] = array_map(fn($val) => $val === null ? 0 : $val, $dataset['data']); + } + + return response()->json([ + 'status_code' => 'SUCCESS', + 'labels' => $labels, + 'datasets' => $datasets, + ]); + } + + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(string $id) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, string $id) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(string $id) + { + // + } +} diff --git a/app/Http/Controllers/ModulePlantLineController.php b/app/Http/Controllers/ModulePlantLineController.php index 2bc80d4..4608a08 100644 --- a/app/Http/Controllers/ModulePlantLineController.php +++ b/app/Http/Controllers/ModulePlantLineController.php @@ -93,7 +93,14 @@ class ModulePlantLineController extends Controller if (empty($plantName)) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Plant Name can't be empty!" + 'status_description' => "Plant name can't be empty!" + ], 404); + } + + if (empty($chartName)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Chart name can't be empty!" ], 404); } @@ -106,8 +113,6 @@ class ModulePlantLineController extends Controller ], 404); } - - // Load only lines NOT equal to FG Line $nonFgLines = Line::where('plant_id', $plant->id) ->where('type', '!=', 'FG Line') diff --git a/app/Http/Controllers/ModuleProductionLineStopController.php b/app/Http/Controllers/ModuleProductionLineStopController.php new file mode 100644 index 0000000..28154dc --- /dev/null +++ b/app/Http/Controllers/ModuleProductionLineStopController.php @@ -0,0 +1,171 @@ +header('Authorization'); + $expectedToken = $expectedUser . ':' . $expectedPw; + + if ("Bearer " . $expectedToken != $header_auth) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid authorization token!' + ], 403); + } + + $requiredHeaders = ['module-name', 'chart-name', 'plant-name', 'line-name', 'filter-name']; + + $missingHeaders = []; + foreach ($requiredHeaders as $header) { + if (empty($request->header($header))) { + $missingHeaders[] = $header; + } + } + + if (!empty($missingHeaders)) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Missing required headers: ' . implode(', ', $missingHeaders) + ], 404); + } + + $plantName = $request->header('plant-name'); + $lineName = $request->header('line-name'); + $filterName = strtolower(trim($request->header('filter-name'))); + + $plant = Plant::where('name', $plantName)->first(); + if (!$plant) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Plant '{$plantName}' not found!" + ], 404); + } + + $line = Line::where('name', $lineName)->where('plant_id', $plant->id)->first(); + if (!$line) { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Line '{$lineName}' not found for plant '{$plantName}'" + ], 404); + } + + // Set time range + if ($filterName == 'yesterday') { + $startDate = now()->subDay()->setTime(8, 0, 0); + $endDate = now()->setTime(8, 0, 0); + } elseif ($filterName == 'this week') { + $startDate = now()->startOfWeek()->setTime(8, 0, 0); + $endDate = now()->endOfWeek()->addDay()->setTime(8, 0, 0); + } elseif ($filterName == 'this month') { + $startDate = now()->startOfMonth(); + $endDate = now()->endOfMonth(); + } else { + $startDate = now()->setTime(8, 0, 0); + $endDate = now()->copy()->addDay()->setTime(8, 0, 0); + } + + $query = DB::table('production_line_stops') + ->join('line_stops as ls', 'production_line_stops.linestop_id', '=', 'ls.id') + ->join('lines', 'production_line_stops.line_id', '=', 'lines.id') + ->join('plants', 'production_line_stops.plant_id', '=', 'plants.id') + ->select( + 'ls.code', + 'ls.reason', + DB::raw("SUM(production_line_stops.stop_hour) as total_stop_hours"), + DB::raw("SUM(production_line_stops.stop_min) as total_stop_minutes") + ) + ->where('plants.id', $plant->id) + ->where('lines.id', $line->id) + ->whereBetween('production_line_stops.created_at', [$startDate, $endDate]) + ->groupBy('ls.code', 'ls.reason') + ->get(); + + if ($query->isEmpty()) { + return response()->json([ + 'status_code' => 'SUCCESS', + 'labels' => ['No Data'], + 'data' => [1], + ], 200); + } + + $labels = []; + $data = []; + $totalStopMinutes = 0; + + foreach ($query as $row) { + $stopHours = $row->total_stop_hours; + $stopMinutes = $row->total_stop_minutes; + $visualTotal = $stopHours + ($stopMinutes / 100); + + $labels[] = "{$row->code} - {$row->reason}"; + $data[] = $visualTotal; + + $totalStopMinutes += ($stopHours * 60) + $stopMinutes; + } + + // Calculate available runtime + $remainingMinutes = 1440 - $totalStopMinutes; + $runtimeHours = floor($remainingMinutes / 60); + $runtimeMinutes = $remainingMinutes % 60; + $runtimeVisual = $runtimeHours + ($runtimeMinutes / 100); + + $labels[] = 'Available Runtime'; + $data[] = $runtimeVisual; + + return response()->json([ + 'status_code' => 'SUCCESS', + 'labels' => $labels, + 'data' => $data, + ], 200); + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(string $id) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, string $id) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(string $id) + { + // + } +} diff --git a/routes/api.php b/routes/api.php index 1d14048..1309f38 100644 --- a/routes/api.php +++ b/routes/api.php @@ -7,8 +7,10 @@ use App\Http\Controllers\ModuleFGLineController; use App\Http\Controllers\ModuleFilterController; use App\Http\Controllers\ModuleFilterDataController; use App\Http\Controllers\ModuleInvoiceDataController; +use App\Http\Controllers\ModuleInvoiceTypeController; use App\Http\Controllers\ModulePlantController; use App\Http\Controllers\ModulePlantLineController; +use App\Http\Controllers\ModuleProductionLineStopController; use App\Http\Controllers\ModuleProductionOrderDataController; use App\Http\Controllers\ObdController; use App\Http\Controllers\PalletController; @@ -89,7 +91,11 @@ Route::get('get/module-filter-value/data', [ModuleFilterDataController::class, ' Route::get('get/module-production-order/data', [ModuleProductionOrderDataController::class, 'get_moduleProductionOrder']); +Route::get('get/module-production-linestop/data', [ModuleProductionLineStopController::class, 'get_moduleProductionLineStop']); + //Invoice Dashboard Controller Route::get('get/module-invoice-type/data', [ModuleInvoiceDataController::class, 'get_invoiceData']); +Route::get('get/module-invoice-count/data', [ModuleInvoiceTypeController::class, 'get_invoiceCountData']); +