feat(controller): add MigrasiController for data processing
- Menambahkan MigrasiController untuk memproses data dari file CSV. - Mengimplementasikan metode untuk memproses data Arrangement, Customer, dan Bill Detail. - Menggunakan model TempArrangement, TempCustomer, dan TempBillDetail untuk menyimpan data. - Menangani pengecualian jika file tidak ditemukan atau tidak dapat dibuka.
This commit is contained in:
145
app/Http/Controllers/MigrasiController.php
Normal file
145
app/Http/Controllers/MigrasiController.php
Normal file
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Webstatement\Http\Controllers;
|
||||
|
||||
use Exception;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Modules\Webstatement\Models\TempCustomer;
|
||||
use Modules\Webstatement\Models\TempArrangement;
|
||||
use Modules\Webstatement\Jobs\ProcessCustomerDataJob;
|
||||
use Modules\Webstatement\Models\TempBillDetail;
|
||||
|
||||
class MigrasiController extends Controller
|
||||
{
|
||||
|
||||
public function processArrangementData()
|
||||
{
|
||||
$filePath = storage_path('app/20240901.ST.AA.ARRANGEMENT.csv'); // Adjust this path as needed
|
||||
try {
|
||||
if (!file_exists($filePath)) {
|
||||
throw new Exception("File not found: $filePath");
|
||||
}
|
||||
|
||||
//ProcessCustomerDataJob::dispatch($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 TempArrangement())->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']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user