}
}
+ $grouped = [];
+ foreach ($rows as $idx => $row) {
+ $key = "{$row['store_id']}|{$row['category']}|{$row['subcategory']}|{$row['species']}";
+ $grouped[$key][] = $idx;
+ }
+ foreach ($grouped as $key => $indices) {
+ $sumPercent = 0.0;
+ foreach ($indices as $i) {
+ $sumPercent += $rows[$i]['percent'];
+ }
+ if ($sumPercent < 1.0) {
+ $diff = 1.0 - $sumPercent;
+ $count = count($indices);
+ $add = $diff / $count;
+ foreach ($indices as $i) {
+ $rows[$i]['percent'] = round($rows[$i]['percent'] + $add, 4);
+ }
+ }
+ }
+
return ['weeksData' => $rows];
}