diff --git a/app/Console/Commands/GetReportOnlineByDay.php b/app/Console/Commands/GetReportOnlineByDay.php new file mode 100644 index 0000000..8cc6136 --- /dev/null +++ b/app/Console/Commands/GetReportOnlineByDay.php @@ -0,0 +1,185 @@ +format('Y-m-d'); + } + + dump(json_encode($dateStrings)); + return $dateStrings; + } + + $kodcaw = [ + //'011234', + //'WCY', + 'WB', + ]; + + function generateReportDayBayaranOnline($kodcaw, $date){ + + + $radpenser = 'bayaran_online'; + + $pendser = Http::get(config('api.url'). '/api/SenaraiPendSer/range/' . $kodcaw, ['mula' => $date,'hingga' => $date,'jenis' => $radpenser])->json(); + + $serahan = array_map(function($x){ + return $x['serahan']; + }, $pendser); + + $serahan_total = array_reduce($serahan, function($accumulator, $currentItem){ + $value = $accumulator + floatval($currentItem); + + // Round the value to 2 decimal places + $roundedValue = round($value, 2); + + // Format the value to ensure it has 2 decimal places + $formattedValue = number_format($roundedValue, 2, '.', ''); + + return $formattedValue; + }, 0); + + $bayaran_online = json_encode($serahan_total); + return $serahan_total; + } + + + function generateReportDayTotalDuitMasuk($kodcaw, $date){ + + $pendser = Http::get(config('api.url'). '/api/ansuran/laporan/' . $kodcaw, ['tarikh' => $date])->json(); + + $serahan_online = array_filter($pendser, function($x){ + return $x['teller'] == 'Online'; + }); + + $serahan = array_map(function($x){ + return $x['duit_masuk']; + }, $serahan_online); + + $duit_masuk_total = array_reduce($serahan, function($accumulator, $currentItem){ + $value = $accumulator + floatval($currentItem); + + // Round the value to 2 decimal places + $roundedValue = round($value, 2); + + // Format the value to ensure it has 2 decimal places + $formattedValue = number_format($roundedValue, 2, '.', ''); + + return $formattedValue; + }, 0); + + $total = json_encode($duit_masuk_total); + return $total; + } + + $startDate = '2024-5-29'; + $endDate = '2024-6-26'; + + $dates = generateDateStrings($startDate, $endDate); + + $reports_bayaran_online = array_map(function($caw) use($dates){ + return array_map(function($date) use($caw){ + return generateReportDayBayaranOnline($caw, $date); + }, $dates); + }, $kodcaw); + + $this->info(json_encode($reports_bayaran_online)); + + $reports_ansuran_online = array_map(function($caw) use($dates){ + return array_map(function($date) use($caw){ + return generateReportDayTotalDuitMasuk($caw, $date); + }, $dates); + }, $kodcaw); + + + function merge_by_index($array1, $array2, $date){ + $resultArray = []; + + for ($i = 0; $i < sizeOf($date); $i++) { + $resultArray[] = [ + 'serahan_online' => $array1[0][$i], + 'ansuran_online' => $array2[0][$i], + 'balance' => floatval($array1[0][$i]) - floatval($array1[0][$i])]; + } + + return $resultArray; + + } + + + function exports_to_csv($kodcaw, $data) + { + // Open a file in write mode + $filename = $kodcaw . 'output.csv'; + $file = fopen($filename, 'w'); + + // Initialize a single array to hold all the data + $singleRow = []; + + // Iterate over the data array and append each value to the single array + foreach ($data as $row) { + foreach ($row as $value) { + $singleRow[] = $value; + } + } + + $csvString = implode(',', $singleRow); + + // Store the CSV string as a file in the storage folder + Storage::put('public/' . $filename, $csvString); + + // Close the file + fclose($file); + + echo "CSV file created successfully!"; + } + + $total_reports = merge_by_index($reports_bayaran_online, $reports_ansuran_online, $dates); + exports_to_csv('Wakaf_Bharu_', $total_reports); + + + } +}