I’d really appreciate the help here… I have an array of rates, e.g.
[
[
'rate_id' => 1,
'after' => 0,
'units' => 0,
'rate' => 0
],
[
'rate_id' => 2,
'after' => 8,
'units' => 0,
'rate' => 12
],
[
'rate_id' => 3,
'after' => 12,
'units' => 0,
'rate' => 15
]
]
]
And I need to take an inputted total time
e.g. say 15.5 hours for 1 day and loop through the rates and split the 15 hours into the respective array key, so the first array item should contain the first 8 hours, then 4 in the second one, then 3.5 in the last.
The above array is the data for a rate sheet, so each “after” is the when the overtime rate kicks in.
Please any help I would really appreciate, as I’m struggling to get this working accurately with decimal places.
So far I have tried doing a php do/while loop, here’s my code from my laravel app so far.
$totalTime = $item->units;
$rates = $item->timesheet->rates()
->orderBy('after', 'desc')
->get()
->keyBy('after')
->map(function($item) {
return [
'units' => 0,
'rate_id' => $item['id']
];
})->toArray();
do {
foreach ($rates as $after => $rate) {
if ($totalTime >= $after) {
$rates[$after]['units'] = $rates[$after]['units'] + 1;
break;
}
}
$totalTime -= 1;
} while ($totalTime >= 1);
$rates = collect($rates)->filter(function ($item, $key) {
return $item['units'] > 0;
});
This works I think so far, but it doesn’t work if the $totalTime
is a decimal place, rounds it up/down to the nearest decimal because we are only decrementing by 1, instead of by 0.1, but if I decrement it by 0.1 it also doesn’t work properly as when we get below 0 it starts doing 0.9, 0.8, 0.7 etc.
2
So this is incremental thing. We will for
from the start, filling the hours while reducing amount from $totalTime
$arr = [
[
'rate_id' => 1,
'after' => 0,
'units' => 0,
'rate' => 0
],
[
'rate_id' => 2,
'after' => 8,
'units' => 0,
'rate' => 12
],
[
'rate_id' => 3,
'after' => 12,
'units' => 0,
'rate' => 15
]
];
$totalTime = 15.5;
for ($i = 0; $i < count($arr) - 1; $i++) {
$shift = $arr[$i + 1]['after'] - $arr[$i]['after'];
$worked = min($totalTime, $shift);
$arr[$i]['units'] = $worked;
$totalTime -= $worked;
}
if (count($arr)) {
$arr[count($arr) - 1]['units'] = $totalTime;
}
print_r($arr);
// output:
Array
(
[0] => Array
(
[rate_id] => 1
[after] => 0
[units] => 8
[rate] => 0
)
[1] => Array
(
[rate_id] => 2
[after] => 8
[units] => 4
[rate] => 12
)
[2] => Array
(
[rate_id] => 3
[after] => 12
[units] => 3.5
[rate] => 15
)
)
1