added rounding rules and hourly rates factor #112 (#128)

* added unit tests and documentation #112
* fixed DashboardController
* added CONTRIBUTION guidelines
* fixed license year
This commit is contained in:
Kevin Papst
2018-02-07 12:51:00 +01:00
committed by GitHub
parent 65790f1fb7
commit 2e60e14132
22 changed files with 925 additions and 76 deletions

View File

@@ -0,0 +1,139 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Timesheet\Calculator;
use App\Entity\Timesheet;
use App\Timesheet\CalculatorInterface;
/**
* 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
{
/**
* @var array
*/
protected $roundings;
/**
* DurationCalculator constructor.
* @param array $roundings
*/
public function __construct(array $roundings)
{
$this->roundings = $roundings;
}
/**
* @param Timesheet $record
*/
public function calculate(Timesheet $record)
{
if ($record->getEnd() === null) {
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)) {
$this->roundBegin($record, $rounding['begin']);
$this->roundEnd($record, $rounding['end']);
$this->applyDuration($record);
$this->roundDuration($record, $rounding['duration']);
}
}
}
/**
* @param Timesheet $record
* @param int $minutes
*/
protected function roundBegin(Timesheet $record, $minutes)
{
if ($minutes <= 0) {
return;
}
$timestamp = $record->getBegin()->getTimestamp();
$seconds = $minutes * 60;
$diff = $timestamp % $seconds;
if ($diff === 0) {
return;
}
$record->getBegin()->setTimestamp($timestamp - $diff);
}
/**
* @param Timesheet $record
* @param int $minutes
*/
protected function roundEnd(Timesheet $record, $minutes)
{
if ($minutes <= 0) {
return;
}
$timestamp = $record->getEnd()->getTimestamp();
$seconds = $minutes * 60;
$diff = $timestamp % $seconds;
if ($diff === 0) {
return;
}
$record->getEnd()->setTimestamp($timestamp - $diff + $seconds);
}
/**
* @param Timesheet $record
* @param int $minutes
*/
protected function roundDuration(Timesheet $record, $minutes)
{
if ($minutes <= 0) {
return;
}
$timestamp = $record->getDuration();
$seconds = $minutes * 60;
$diff = $timestamp % $seconds;
if ($diff === 0) {
return;
}
$record->setDuration($timestamp - $diff + $seconds);
}
}