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

@@ -9,7 +9,7 @@
namespace App\Doctrine;
use App\Entity\UserPreference;
use App\Timesheet\CalculatorInterface;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Event\LifecycleEventArgs;
@@ -20,6 +20,27 @@ use App\Entity\Timesheet;
*/
class TimesheetSubscriber implements EventSubscriber
{
/**
* @var CalculatorInterface[]
*/
protected $calculator;
/**
* TimesheetSubscriber constructor.
* @param iterable $calculators
*/
public function __construct(iterable $calculators)
{
foreach ($calculators as $calculator) {
if (!($calculator instanceof CalculatorInterface)) {
throw new \InvalidArgumentException(
'Invalid TimesheetCalculator implementation given. Expected CalculatorInterface but received ' .
get_class($calculator)
);
}
}
$this->calculator = $calculators;
}
/**
* @return array
@@ -55,26 +76,12 @@ class TimesheetSubscriber implements EventSubscriber
{
$entity = $args->getObject();
if ($entity instanceof Timesheet) {
if ($entity->getEnd() !== null) {
$duration = $entity->getEnd()->getTimestamp() - $entity->getBegin()->getTimestamp();
$entity->setDuration($duration);
if (!($entity instanceof Timesheet)) {
return;
}
// TODO allow to set hourly rate on activity, project and customer and prefer these
$rate = $this->calculateRate($entity);
$entity->setRate($rate);
}
foreach ($this->calculator as $calculator) {
$calculator->calculate($entity);
}
}
/**
* @param Timesheet $entity
* @return float
*/
protected function calculateRate(Timesheet $entity)
{
$hourlyRate = (float) $entity->getUser()->getPreferenceValue(UserPreference::HOURLY_RATE, 0);
return (float) $hourlyRate * ($entity->getDuration() / 3600);
}
}