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);
}
}

View File

@@ -0,0 +1,86 @@
<?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\Entity\UserPreference;
use App\Timesheet\CalculatorInterface;
/**
* Implementation to calculate the rate for a timesheet record.
*/
class RateCalculator implements CalculatorInterface
{
/**
* @var array
*/
protected $rates;
/**
* RateCalculator constructor.
* @param array $rates
*/
public function __construct(array $rates)
{
$this->rates = $rates;
}
/**
* @param Timesheet $record
*/
public function calculate(Timesheet $record)
{
if ($record->getEnd() === null) {
return;
}
$rate = $this->calculateRate($record);
$factor = $this->getRateFactor($record);
$record->setRate($rate * $factor);
}
/**
* @param Timesheet $record
* @return float
*/
protected function getRateFactor(Timesheet $record)
{
$factor = 0;
foreach ($this->rates as $rateFactor) {
$weekday = $record->getEnd()->format('l');
$days = array_map('strtolower', $rateFactor['days']);
if (in_array(strtolower($weekday), $days)) {
if ($rateFactor['factor'] <= 0) {
throw new \InvalidArgumentException(
'A rate factor smaller or equals 0 is not allowed, given: ' . $rateFactor['factor']
);
}
$factor += $rateFactor['factor'];
}
}
if ($factor <= 0) {
$factor = 1;
}
return $factor;
}
/**
* @param Timesheet $record
* @return float
*/
protected function calculateRate(Timesheet $record)
{
$hourlyRate = (float) $record->getUser()->getPreferenceValue(UserPreference::HOURLY_RATE, 0);
return (float) $hourlyRate * ($record->getDuration() / 3600);
}
}

View File

@@ -0,0 +1,28 @@
<?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;
use App\Entity\Timesheet;
/**
* A calculator is called before a Timesheet entity will be updated.
* These classes will normally be used when calculating duration or rates.
*/
interface CalculatorInterface
{
/**
* All necessary changes need to be applied on the given $record.
* The methods return value will not be evaluated.
*
* @param Timesheet $record
* @return void
*/
public function calculate(Timesheet $record);
}