rounding rules via admin screen, round begin when starting record (#1229)
This commit is contained in:
@@ -11,26 +11,19 @@ namespace App\Timesheet\Calculator;
|
||||
|
||||
use App\Entity\Timesheet;
|
||||
use App\Timesheet\CalculatorInterface;
|
||||
use App\Timesheet\Rounding\RoundingInterface;
|
||||
use App\Timesheet\RoundingService;
|
||||
|
||||
/**
|
||||
* Implementation to calculate the durations for a timesheet record.
|
||||
*
|
||||
* This calculator takes the configuration %kimai.timesheet.rounding% as argument,
|
||||
* so its rounding behaviour can be customized.
|
||||
*/
|
||||
class DurationCalculator implements CalculatorInterface
|
||||
final class DurationCalculator implements CalculatorInterface
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
* @var RoundingService
|
||||
*/
|
||||
protected $roundings;
|
||||
private $roundings;
|
||||
|
||||
/**
|
||||
* DurationCalculator constructor.
|
||||
* @param array $roundings
|
||||
*/
|
||||
public function __construct(array $roundings)
|
||||
public function __construct(RoundingService $roundings)
|
||||
{
|
||||
$this->roundings = $roundings;
|
||||
}
|
||||
@@ -44,37 +37,9 @@ class DurationCalculator implements CalculatorInterface
|
||||
return;
|
||||
}
|
||||
|
||||
$this->applyDuration($record);
|
||||
$this->applyRoundings($record);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Timesheet $record
|
||||
*/
|
||||
protected function applyDuration(Timesheet $record)
|
||||
{
|
||||
$duration = $record->getEnd()->getTimestamp() - $record->getBegin()->getTimestamp();
|
||||
$record->setDuration($duration);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Timesheet $record
|
||||
*/
|
||||
protected function applyRoundings(Timesheet $record)
|
||||
{
|
||||
foreach ($this->roundings as $rounding) {
|
||||
$weekday = $record->getEnd()->format('l');
|
||||
$days = array_map('strtolower', $rounding['days']);
|
||||
|
||||
if (in_array(strtolower($weekday), $days)) {
|
||||
$class = 'App\\Timesheet\\Rounding\\' . ucfirst($rounding['mode']) . 'Rounding';
|
||||
/* @var $rounder RoundingInterface */
|
||||
$rounder = new $class();
|
||||
$rounder->roundBegin($record, $rounding['begin']);
|
||||
$rounder->roundEnd($record, $rounding['end']);
|
||||
$this->applyDuration($record);
|
||||
$rounder->roundDuration($record, $rounding['duration']);
|
||||
}
|
||||
}
|
||||
$this->roundings->applyRoundings($record);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user