DONE: add flexible json update saham, yuran, and pelaburan ui
This commit is contained in:
@@ -12,7 +12,7 @@ class UpdateVoterPelaburanFromJson extends Command
|
||||
{--election_id= : Override election_id (default: latest in voter table)}
|
||||
{--dry-run : Show what would change without updating}';
|
||||
|
||||
protected $description = 'Update voter pelaburan (dividen) from list-pelabur.json for latest election only';
|
||||
protected $description = 'Update voter dividen_pelaburan (from JSON pelaburan) and accumulated_amount from list-pelabur.json for latest election only';
|
||||
|
||||
public function handle()
|
||||
{
|
||||
@@ -51,8 +51,9 @@ class UpdateVoterPelaburanFromJson extends Command
|
||||
continue;
|
||||
}
|
||||
|
||||
// JSON key "pelaburan" is the dividend amount → voter.dividen_pelaburan
|
||||
$map[$noAnggota] = [
|
||||
'pelaburan' => $this->cleanNumber($row['pelaburan'] ?? 0),
|
||||
'dividen_pelaburan' => $this->cleanNumber($row['pelaburan'] ?? 0),
|
||||
'accumulated_amount' => $this->cleanNumber($row['accumulated_amount'] ?? 0),
|
||||
];
|
||||
}
|
||||
@@ -67,6 +68,7 @@ class UpdateVoterPelaburanFromJson extends Command
|
||||
$this->info("Target election_id: {$electionId}");
|
||||
$this->info("JSON members loaded: " . count($map));
|
||||
$this->info($dryRun ? "Mode: DRY RUN (no DB updates)" : "Mode: UPDATE");
|
||||
$this->info("Mapping: JSON.pelaburan → voter.dividen_pelaburan, JSON.accumulated_amount → voter.accumulated_amount");
|
||||
|
||||
$totalInElection = (int) DB::table('voter')->where('election_id', $electionId)->count();
|
||||
$this->info("Voters in election: {$totalInElection}");
|
||||
@@ -80,7 +82,7 @@ class UpdateVoterPelaburanFromJson extends Command
|
||||
$bar->start();
|
||||
|
||||
DB::table('voter')
|
||||
->select(['id', 'no_anggota', 'pelaburan', 'accumulated_amount'])
|
||||
->select(['id', 'no_anggota', 'dividen_pelaburan', 'accumulated_amount'])
|
||||
->where('election_id', $electionId)
|
||||
->orderBy('id')
|
||||
->chunkById(500, function ($chunk) use ($map, $dryRun, &$updated, &$matched, &$missingInJson, &$unchanged, $bar) {
|
||||
@@ -96,11 +98,11 @@ class UpdateVoterPelaburanFromJson extends Command
|
||||
$matched++;
|
||||
$jsonRow = $map[$noAnggota];
|
||||
|
||||
$oldPelaburan = $voter->pelaburan !== null ? (float) $voter->pelaburan : 0.0;
|
||||
$oldDividen = $voter->dividen_pelaburan !== null ? (float) $voter->dividen_pelaburan : 0.0;
|
||||
$oldAccumulated = $voter->accumulated_amount !== null ? (float) $voter->accumulated_amount : 0.0;
|
||||
|
||||
if (
|
||||
abs($oldPelaburan - $jsonRow['pelaburan']) < 0.00001
|
||||
abs($oldDividen - $jsonRow['dividen_pelaburan']) < 0.00001
|
||||
&& abs($oldAccumulated - $jsonRow['accumulated_amount']) < 0.00001
|
||||
) {
|
||||
$unchanged++;
|
||||
@@ -112,7 +114,7 @@ class UpdateVoterPelaburanFromJson extends Command
|
||||
DB::table('voter')
|
||||
->where('id', $voter->id)
|
||||
->update([
|
||||
'pelaburan' => $jsonRow['pelaburan'],
|
||||
'dividen_pelaburan' => $jsonRow['dividen_pelaburan'],
|
||||
'accumulated_amount' => $jsonRow['accumulated_amount'],
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
@@ -145,7 +147,7 @@ class UpdateVoterPelaburanFromJson extends Command
|
||||
|
||||
private function cleanNumber($value): float
|
||||
{
|
||||
if ($value === null) {
|
||||
if ($value === null || $value === '') {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user