186 lines
4.3 KiB
PHP
186 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Carbon\CarbonPeriod;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class GetReportOnlinePaymentByDay extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'report:online';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Command description';
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
// Function to generate date strings
|
|
function generateDateStrings($startDate, $endDate) {
|
|
$period = CarbonPeriod::create($startDate, $endDate);
|
|
$dateStrings = [];
|
|
|
|
foreach ($period as $date) {
|
|
$dateStrings[] = $date->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);
|
|
|
|
|
|
}
|
|
}
|