diff --git a/app/Http/Controllers/MigrasiController.php b/app/Http/Controllers/MigrasiController.php new file mode 100644 index 0000000..e52d05a --- /dev/null +++ b/app/Http/Controllers/MigrasiController.php @@ -0,0 +1,145 @@ +getFillable(); + + while (($row = fgetcsv($handle, 0, ";")) !== false) { + unset($row[0]); // Remove the first empty column if present + if (count($headers) === count($row)) { + $data = array_combine($headers, $row); + TempArrangement::updateOrCreate( + ['arrangement_id' => $data['arrangement_id']], // key to find existing record + $data // data to update or create + ); + } + } + fclose($handle); + } else { + throw new Exception("Unable to open file: {$filePath}"); + } + + return response()->json(['message' => 'Data Arrangement processing job has been successfully']); + } catch (Exception $e) { + return response()->json(['error' => $e->getMessage()], 500); + } + } + + public function processCustomerData() + { + $filePath = storage_path('app/20240901.ST.CUSTOMER.csv'); // Adjust this path as needed + try { + if (!file_exists($filePath)) { + throw new Exception("File not found: $filePath"); + } + + set_time_limit(24 * 60 * 60); + + if (!file_exists($filePath)) { + throw new Exception("File not found: {$filePath}"); + } + + $handle = fopen($filePath, "r"); + + if ($handle !== false) { + $headers = (new TempCustomer())->getFillable(); + while (($row = fgetcsv($handle, 0, "~")) !== false) { + unset($row[0]); // Remove the first empty column if present + if (count($headers) === count($row)) { + $data = array_combine($headers, $row); + TempCustomer::updateOrCreate( + ['customer_code' => $data['customer_code']], // key to find existing record + $data // data to update or create + ); + } + } + fclose($handle); + } else { + throw new Exception("Unable to open file: {$filePath}"); + } + + return response()->json(['message' => 'Data Customer processing job has been successfully']); + } catch (Exception $e) { + return response()->json(['error' => $e->getMessage()], 500); + } + } + + public function processBillDetailData() + { + $filePath = storage_path('app/20240901.ST.AA.BILL.DETAILS.csv'); // Adjust this path as needed + try { + if (!file_exists($filePath)) { + throw new Exception("File not found: $filePath"); + } + + set_time_limit(24 * 60 * 60); + + if (!file_exists($filePath)) { + throw new Exception("File not found: {$filePath}"); + } + + $handle = fopen($filePath, "r"); + + if ($handle !== false) { + $headers = (new TempBillDetail())->getFillable(); + while (($row = fgetcsv($handle, 0, ";")) !== false) { + if (count($headers) === count($row)) { + $data = array_combine($headers, $row); + + try { + TempBillDetail::create( + $data // data to update or create + ); + } catch (Exception $e) { + echo json_encode($e->getMessage()); + } + } + } + fclose($handle); + } else { + throw new Exception("Unable to open file: {$filePath}"); + } + + return response()->json(['message' => 'Data Bill Details processing job has been successfully']); + } catch (Exception $e) { + return response()->json(['error' => $e->getMessage()], 500); + } + } + + public function index() + { + $this->processCustomerData(); + $this->processArrangementData(); + $this->processBillDetailData(); + return response()->json(['message' => 'Data processing job has been successfully']); + } +}