add hourly_rate and fixed_rate to timesheet entries (#302)

This commit is contained in:
Kevin Papst
2018-09-05 15:27:39 +02:00
committed by GitHub
parent a636faa4ab
commit 6163f33abf
15 changed files with 219 additions and 9 deletions

View File

@@ -1,2 +1,3 @@
unreleased=true
future-release=0.4
exclude-labels=duplicate,question,invalid,wontfix

View File

@@ -32,7 +32,7 @@ which leads to problems between Composer and Symfony Flex, resulting in an error
This can be fixed by updating Composer and Flex before executing the Kimai update:
```
sudo composer self-update
sudo -u www-data composer update symfony/flex --no-plugins
sudo -u www-data composer update symfony/flex --no-plugins --no-scripts
```
Then the full update can be executed as usual:

10
composer.lock generated
View File

@@ -3599,16 +3599,16 @@
},
{
"name": "symfony/flex",
"version": "v1.1.0",
"version": "v1.1.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/flex.git",
"reference": "d6f5fed47ddad2eb25d5cc19316692203a2898eb"
"reference": "9fb60f232af0764d58002e7872acb43a74506d25"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/flex/zipball/d6f5fed47ddad2eb25d5cc19316692203a2898eb",
"reference": "d6f5fed47ddad2eb25d5cc19316692203a2898eb",
"url": "https://api.github.com/repos/symfony/flex/zipball/9fb60f232af0764d58002e7872acb43a74506d25",
"reference": "9fb60f232af0764d58002e7872acb43a74506d25",
"shasum": ""
},
"require": {
@@ -3642,7 +3642,7 @@
}
],
"description": "Composer plugin for Symfony",
"time": "2018-08-21T07:51:18+00:00"
"time": "2018-09-03T08:17:12+00:00"
},
{
"name": "symfony/form",

View File

@@ -89,9 +89,26 @@ class Timesheet
* @var float
*
* @ORM\Column(name="rate", type="decimal", precision=10, scale=2, nullable=false)
* @Assert\GreaterThanOrEqual(0)
*/
private $rate = 0.00;
/**
* @var float
*
* @ORM\Column(name="fixed_rate", type="decimal", precision=10, scale=2, nullable=true)
* @Assert\GreaterThanOrEqual(0)
*/
private $fixedRate = null;
/**
* @var float
*
* @ORM\Column(name="hourly_rate", type="decimal", precision=10, scale=2, nullable=true)
* @Assert\GreaterThanOrEqual(0)
*/
private $hourlyRate = null;
/**
* Get entry id
*
@@ -259,6 +276,42 @@ class Timesheet
return $this->rate;
}
/**
* @return float
*/
public function getFixedRate(): ?float
{
return $this->fixedRate;
}
/**
* @param float $fixedRate
* @return Timesheet
*/
public function setFixedRate(?float $fixedRate)
{
$this->fixedRate = $fixedRate;
return $this;
}
/**
* @return float
*/
public function getHourlyRate(): ?float
{
return $this->hourlyRate;
}
/**
* @param float $hourlyRate
* @return Timesheet
*/
public function setHourlyRate(?float $hourlyRate)
{
$this->hourlyRate = $hourlyRate;
return $this;
}
/**
* @param ExecutionContextInterface $context
* @param mixed $payload

View File

@@ -16,6 +16,7 @@ use App\Form\Type\UserType;
use App\Repository\ActivityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
@@ -74,6 +75,14 @@ class TimesheetEditForm extends AbstractType
'label' => 'label.description',
'required' => false,
])
->add('fixedRate', NumberType::class, [
'label' => 'label.fixed_rate',
'required' => false,
])
->add('hourlyRate', NumberType::class, [
'label' => 'label.hourly_rate',
'required' => false,
])
;
if ($options['include_user']) {

View File

@@ -74,7 +74,7 @@ final class Version20180730044139 extends AbstractMigration
}
$timesheet = $this->getTableName('timesheet');
$user = $this->getTableName('user');
$user = $this->getTableName('users');
if ($platform === 'sqlite') {
$this->addSql('DROP INDEX IDX_4F60C6B18D93D649');

View File

@@ -54,7 +54,7 @@ final class Version20180805183527 extends AbstractMigration
$this->abortIf(true, 'Unsupported database platform: ' . $platform);
}
$user = $this->getTableName('user');
$user = $this->getTableName('users');
if ($platform === 'sqlite') {
$this->addSql('DROP INDEX UNIQ_B9AC5BCE92FC23A8');

View File

@@ -0,0 +1,67 @@
<?php declare(strict_types=1);
namespace DoctrineMigrations;
use App\Doctrine\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* Adding hourly_rate and fixed_rate to timesheet table
*/
final class Version20180903202256 extends AbstractMigration
{
public function up(Schema $schema) : void
{
$platform = $this->getPlatform();
if (!in_array($platform, ['sqlite', 'mysql'])) {
$this->abortIf(true, 'Unsupported database platform: ' . $platform);
}
$timesheet = $this->getTableName('timesheet');
$user = $this->getTableName('users');
$activity = $this->getTableName('activities');
if ($platform === 'sqlite') {
$this->addSql('CREATE TEMPORARY TABLE __temp__' . $timesheet . ' AS SELECT id, user, activity_id, start_time, end_time, duration, description, rate FROM ' . $timesheet);
$this->addSql('DROP TABLE ' . $timesheet);
$this->addSql('CREATE TABLE ' . $timesheet . ' (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, user INTEGER DEFAULT NULL, activity_id INTEGER DEFAULT NULL, start_time DATETIME NOT NULL, end_time DATETIME DEFAULT NULL, duration INTEGER DEFAULT NULL, description CLOB DEFAULT NULL COLLATE BINARY, rate NUMERIC(10, 2) NOT NULL, fixed_rate NUMERIC(10, 2) DEFAULT NULL, hourly_rate NUMERIC(10, 2) DEFAULT NULL, CONSTRAINT FK_4F60C6B18D93D649 FOREIGN KEY (user) REFERENCES ' . $user . ' (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE, CONSTRAINT FK_4F60C6B181C06096 FOREIGN KEY (activity_id) REFERENCES ' . $activity . ' (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('INSERT INTO ' . $timesheet . ' (id, user, activity_id, start_time, end_time, duration, description, rate, fixed_rate, hourly_rate) SELECT id, user, activity_id, start_time, end_time, duration, description, rate, null, null FROM __temp__' . $timesheet);
$this->addSql('DROP TABLE __temp__' . $timesheet);
$this->addSql('CREATE INDEX IDX_4F60C6B181C06096 ON ' . $timesheet . ' (activity_id)');
$this->addSql('CREATE INDEX IDX_4F60C6B18D93D649 ON ' . $timesheet . ' (user)');
} else {
$this->addSql('ALTER TABLE ' . $timesheet . ' DROP FOREIGN KEY FK_4F60C6B18D93D649');
$this->addSql('ALTER TABLE ' . $timesheet . ' ADD fixed_rate NUMERIC(10, 2) DEFAULT NULL, ADD hourly_rate NUMERIC(10, 2) DEFAULT NULL');
$this->addSql('ALTER TABLE ' . $timesheet . ' ADD CONSTRAINT FK_4F60C6B18D93D649 FOREIGN KEY (user) REFERENCES ' . $user . ' (id) ON DELETE CASCADE');
}
}
public function down(Schema $schema) : void
{
$platform = $this->getPlatform();
if (!in_array($platform, ['sqlite', 'mysql'])) {
$this->abortIf(true, 'Unsupported database platform: ' . $platform);
}
$timesheet = $this->getTableName('timesheet');
$user = $this->getTableName('users');
if ($platform === 'sqlite') {
$this->addSql('DROP INDEX IDX_4F60C6B18D93D649');
$this->addSql('DROP INDEX IDX_4F60C6B181C06096');
$this->addSql('CREATE TEMPORARY TABLE __temp__' . $timesheet . ' AS SELECT id, user, activity_id, start_time, end_time, duration, description, rate FROM ' . $timesheet);
$this->addSql('DROP TABLE ' . $timesheet);
$this->addSql('CREATE TABLE ' . $timesheet . ' (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, user INTEGER DEFAULT NULL, activity_id INTEGER DEFAULT NULL, start_time DATETIME NOT NULL, end_time DATETIME DEFAULT NULL, duration INTEGER DEFAULT NULL, description CLOB DEFAULT NULL, rate NUMERIC(10, 2) NOT NULL)');
$this->addSql('INSERT INTO ' . $timesheet . ' (id, user, activity_id, start_time, end_time, duration, description, rate) SELECT id, user, activity_id, start_time, end_time, duration, description, rate FROM __temp__' . $timesheet);
$this->addSql('DROP TABLE __temp__' . $timesheet);
$this->addSql('CREATE INDEX IDX_4F60C6B18D93D649 ON ' . $timesheet . ' (user)');
$this->addSql('CREATE INDEX IDX_4F60C6B181C06096 ON ' . $timesheet . ' (activity_id)');
} else {
$this->addSql('ALTER TABLE ' . $timesheet . ' DROP FOREIGN KEY FK_4F60C6B18D93D649');
$this->addSql('ALTER TABLE ' . $timesheet . ' DROP fixed_rate, DROP hourly_rate');
$this->addSql('ALTER TABLE ' . $timesheet . ' ADD CONSTRAINT FK_4F60C6B18D93D649 FOREIGN KEY (user) REFERENCES ' . $user . ' (id)');
}
}
}

View File

@@ -41,6 +41,11 @@ class RateCalculator implements CalculatorInterface
return;
}
if (null !== $record->getFixedRate()) {
$record->setRate($record->getFixedRate());
return;
}
$rate = $this->calculateRate($record);
$factor = $this->getRateFactor($record);
@@ -80,7 +85,11 @@ class RateCalculator implements CalculatorInterface
*/
protected function calculateRate(Timesheet $record)
{
$hourlyRate = (float) $record->getUser()->getPreferenceValue(UserPreference::HOURLY_RATE, 0);
if (null !== $record->getHourlyRate()) {
$hourlyRate = $record->getHourlyRate();
} else {
$hourlyRate = (float)$record->getUser()->getPreferenceValue(UserPreference::HOURLY_RATE, 0);
}
return (float) $hourlyRate * ($record->getDuration() / 3600);
}

View File

@@ -20,6 +20,26 @@ use App\Entity\User;
*/
class TimesheetTest extends AbstractEntityTest
{
public function testDefaultValues()
{
$sut = new Timesheet();
$this->assertNull($sut->getId());
$this->assertNull($sut->getBegin());
$this->assertNull($sut->getEnd());
$this->assertSame(0, $sut->getDuration());
$this->assertNull($sut->getUser());
$this->assertNull($sut->getActivity());
$this->assertNull($sut->getDescription());
$this->assertSame(0.00, $sut->getRate());
$this->assertNull($sut->getFixedRate());
$this->assertNull($sut->getHourlyRate());
$this->assertInstanceOf(Timesheet::class, $sut->setFixedRate(13.47));
$this->assertEquals(13.47, $sut->getFixedRate());
$this->assertInstanceOf(Timesheet::class, $sut->setHourlyRate(99));
$this->assertEquals(99, $sut->getHourlyRate());
}
protected function getEntity()
{
$customer = new Customer();

View File

@@ -22,6 +22,32 @@ class RateCalculatorTest extends TestCase
{
public const HOURLY_RATE = 75;
public function testCalculateWithHourlyRate()
{
$record = new Timesheet();
$record->setEnd(new \DateTime());
$record->setDuration(1800);
$record->setHourlyRate(100);
$sut = new RateCalculator([]);
$sut->calculate($record);
$this->assertEquals(50, $record->getRate());
}
public function testCalculateWithFixedRate()
{
$record = new Timesheet();
$record->setEnd(new \DateTime());
$record->setDuration(1800);
$record->setFixedRate(10);
// make sure that fixed rate is always applied, even if hourly rate is set
$record->setHourlyRate(99);
$sut = new RateCalculator([]);
$sut->calculate($record);
$this->assertEquals(10, $record->getRate());
}
protected function getTestUser()
{
$pref = new UserPreference();
@@ -38,6 +64,9 @@ class RateCalculatorTest extends TestCase
{
$record = new Timesheet();
$record->setBegin(new \DateTime());
$record->setDuration(1800);
$record->setFixedRate(100);
$record->setHourlyRate(100);
$this->assertEquals(0, $record->getRate());
$sut = new RateCalculator([]);

View File

@@ -319,6 +319,10 @@
<source>modal.columns.description</source>
<target>Beim Speichern werden die nicht ausgewählten Spalten ausgeblendet und diese Einstellungen in einem Browser Cookie hinterlegt. Sollten Sie Ihre Cookies löschen, werden die Einstellungen rückgängig gemacht.</target>
</trans-unit>
<trans-unit id="label.fixed_rate">
<source>label.fixed_rate</source>
<target>Festbetrag</target>
</trans-unit>
<!--
User profile

View File

@@ -319,6 +319,10 @@
<source>modal.columns.description</source>
<target>Upon saving the un-checked columns will be hidden and this setting will be stored in a browser cookie. If you delete your cookies, the settings will be reversed.</target>
</trans-unit>
<trans-unit id="label.fixed_rate">
<source>label.fixed_rate</source>
<target>Fixed rate</target>
</trans-unit>
<!--
User profile

Binary file not shown.

View File

@@ -17,3 +17,17 @@ The `duration` field supports entering data in the following formats:
| Seconds | {seconds} | | `3600` = 1 Hour / `8820` = 2 Hours, 27 Minutes |
Please note: if time rounding is activated (which is the default behaviour), then your entered seconds might be removed after submitting the form.
## Rate calculation
The rate of a timesheet entry can be calculated from several settings.
It can have two values dedicated to the entry itself:
- if a fixed rate is set, the rate of a record is set to this exact value no matter how long the duration is
- if a hourly rate is set, it will be used to calculate the record rate by using it multiplied with the records duration
- each of the above can be set to 0, to set the records rate to 0
If none of the above was set the users rate will be used to calculate the records rate.
Please see also the configuration chapter about [hourly rates for timesheet records](configurations.md) to see how you
can apply configurable multiplying factors based on day and time.