Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
new calculation mode and system configuration (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpapst committed May 21, 2022
1 parent b37c0db commit 098ed2a
Show file tree
Hide file tree
Showing 14 changed files with 316 additions and 57 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/linting.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['7.2', '7.3', '7.4']
php: ['7.4', '8.0', '8.1']

name: Linting - PHP ${{ matrix.php }}
steps:
Expand All @@ -20,6 +20,6 @@ jobs:
coverage: none
extensions: intl
- run: composer install --no-progress
- run: composer validate
- run: composer validate --strict --no-check-version
- run: composer codestyle-check
- run: composer phpstan
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.gitignore
.idea
.php_cs.cache
.disabled
/vendor/
.php-cs-fixer.cache
composer.lock
vendor/
29 changes: 23 additions & 6 deletions .php_cs.dist → .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php

$fileHeaderComment = <<<COMMENT
This file is part of the RecalculateRatesBundle for Kimai 2.
This file is part of the RecalculateRatesBundle for Kimai.
All rights reserved by Kevin Papst (www.keleo.de).
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
COMMENT;

return PhpCsFixer\Config::create()
$fixer = new PhpCsFixer\Config();
$fixer
->setRiskyAllowed(true)
->setRules([
'encoding' => true,
Expand All @@ -20,7 +21,7 @@
'function_declaration' => true,
'indentation_type' => true,
'line_ending' => true,
'lowercase_constants' => true,
'constant_case' => ['case' => 'lower'],
'lowercase_keywords' => true,
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
'header_comment' => ['header' => $fileHeaderComment, 'separate' => 'both'],
Expand All @@ -47,7 +48,7 @@
'statements' => ['return'],
],
'cast_spaces' => true,
'class_attributes_separation' => ['elements' => ['method']],
'class_attributes_separation' => ['elements' => ['method' => 'one']],
'concat_space' => ['spacing' => 'one'],
'declare_equal_normalize' => true,
'function_typehint_space' => true,
Expand Down Expand Up @@ -101,9 +102,10 @@
],
'phpdoc_annotation_without_dot' => true,
'phpdoc_indent' => true,
'phpdoc_inline_tag' => true,
'phpdoc_inline_tag_normalizer' => true,
'phpdoc_no_access' => true,
'phpdoc_no_alias_tag' => true,
'phpdoc_no_empty_return' => false,
'phpdoc_no_package' => true,
'phpdoc_no_useless_inheritdoc' => true,
'phpdoc_return_self_reference' => true,
Expand All @@ -130,7 +132,7 @@
'standardize_increment' => true,
'standardize_not_equals' => true,
'ternary_operator_spaces' => true,
'trailing_comma_in_multiline_array' => false,
'trailing_comma_in_multiline' => false,
'trim_array_spaces' => true,
'unary_operator_spaces' => true,
'whitespace_after_comma_in_array' => true,
Expand All @@ -141,6 +143,18 @@
'method',
'property',
]],
'native_function_invocation' => [
'include' => [
'@compiler_optimized'
],
'scope' => 'namespaced'
],
'native_function_type_declaration_casing' => true,
'no_alias_functions' => [
'sets' => [
'@internal'
]
],
])
->setFinder(
PhpCsFixer\Finder::create()
Expand All @@ -149,7 +163,10 @@
])->exclude([
__DIR__ . '/Resources/',
__DIR__ . '/vendor/',
__DIR__ . '/.github/',
])
)
->setFormat('checkstyle')
;

return $fixer;
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 1.0

Compatible with Kimai 1.20.1

- Added "default" mode to recalculate ONLY if one of the fields is changed: customer, project, activity, user, price
- Select recalculate mode: off, default, always

## 0.2

- Added GitHub actions for basic CI
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/RecalculateRatesExtension.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the RecalculateRatesBundle for Kimai 2.
* This file is part of the RecalculateRatesBundle for Kimai.
* All rights reserved by Kevin Papst (www.keleo.de).
*
* For the full copyright and license information, please view the LICENSE
Expand Down
52 changes: 52 additions & 0 deletions EventSubscriber/SystemConfigurationSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/*
* This file is part of the RecalculateRatesBundle for Kimai.
* All rights reserved by Kevin Papst (www.keleo.de).
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KimaiPlugin\RecalculateRatesBundle\EventSubscriber;

use App\Event\SystemConfigurationEvent;
use App\Form\Model\Configuration;
use App\Form\Model\SystemConfiguration;
use KimaiPlugin\RecalculateRatesBundle\Form\RecalculateModeType;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

final class SystemConfigurationSubscriber implements EventSubscriberInterface
{
/**
* @return array<string, array<int, string|int>>
*/
public static function getSubscribedEvents(): array
{
return [
SystemConfigurationEvent::class => ['onSystemConfiguration', 100],
];
}

public function onSystemConfiguration(SystemConfigurationEvent $event): void
{
$newConfiguration = (new Configuration())
->setName('timesheet.recalculate.mode')
->setLabel('recalculate.mode')
->setRequired(true)
->setTranslationDomain('system-configuration')
->setType(RecalculateModeType::class);

foreach ($event->getConfigurations() as $configuration) {
if ($configuration->getSection() === 'timesheet') {
$configuration->addConfiguration($newConfiguration);

return;
}
}

$event->addConfiguration(
(new SystemConfiguration('recalculate'))->setConfiguration([$newConfiguration])
);
}
}
43 changes: 43 additions & 0 deletions Form/RecalculateModeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the RecalculateRatesBundle for Kimai.
* All rights reserved by Kevin Papst (www.keleo.de).
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KimaiPlugin\RecalculateRatesBundle\Form;

use App\Constants;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class RecalculateModeType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver): void
{
$modes = ['recalculate.mode.off' => null];

/* @phpstan-ignore-next-line */
if (\defined('App\Constants::VERSION_ID') && Constants::VERSION_ID > 12000) {
$modes['recalculate.mode.default'] = 'default';
}

$modes['recalculate.mode.always'] = 'always';

$resolver->setDefaults([
'required' => true,
'multiple' => false,
'label' => 'kiosk.login_type',
'choices' => $modes,
]);
}

public function getParent(): string
{
return ChoiceType::class;
}
}
57 changes: 38 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,54 @@
# RecalculateRatesBundle
# Recalculate-Rates plugin for Kimai

A Kimai 2 plugin, which forces a recalculation of the hourly and fixed rates for timesheet records on every update.
A Kimai plugin, which forces a recalculation of the hourly and fixed rates for timesheet records on certain updates.

By default, Kimai will use the hourly/fixed rate which was initially found.
There are two possible modes in which this plugin can work:

## Warning
1. Recalculate prices if certain fields were changed
2. Recalculate prices on every update

The good part is: you can change customer/project and activity and be sure, that the correct rate is used.
The first mode is the better one, but only available from Kimai 1.20.1 on.

The bad part: a manually entered hourly/fixed rate will be overwritten. You HAVE to work with the pre-configured rates on your activities/projects/customers.
You can configure the mode, by default mode 1 is used, unless your Kimai version is too old, then 2 is used.

## Recalculate prices if certain fields were changed

This mode should be preferred.

A timesheet record rate will be recalculated if it was changed in one of these fields: Customer, Project, Activity, User, Price

This still might overwrite custom rates, which were applied to single timesheets.
But this case is very rare and using custom rates for single entries should be avoided anyway.
If you find yourself using this workflow often, consider using the [Expense plugin](https://www.kimai.org/store/expenses-bundle.html).

## Recalculate prices on every update

The good part is:
- you can change customer/project and activity and be sure, that the correct rate is used.

The bad part:
- even setting the export field might change the hourly rate and render your history invalid
- a manually entered hourly/fixed rate will be overwritten
- you HAVE to work with the pre-configured rates on your activities/projects/customers.

## Installation

First clone it to your Kimai installation `plugins` directory:
```
cd /kimai/var/plugins/
```bash
cd var/plugins/
git clone https://github.com/Keleo/RecalculateRatesBundle.git
```

And then rebuild the cache:
```
cd /kimai/
bin/console cache:clear
bin/console cache:warmup
```

You could also [download it as zip](https://github.com/keleo/RecalculateRatesBundle/archive/master.zip) and upload the directory via FTP:
The file structure needs to look like this afterwards:

```
/kimai/var/plugins/
```bash
var/plugins/
├── RecalculateRatesBundle
   ├── RecalculateRatesBundle.php
├── RecalculateRatesBundle.php
| └ ... more files and directories follow here ...
```

And then rebuild the cache:
```bash
bin/console kimai:reload --env=prod
```
2 changes: 1 addition & 1 deletion RecalculateRatesBundle.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the RecalculateRatesBundle for Kimai 2.
* This file is part of the RecalculateRatesBundle for Kimai.
* All rights reserved by Kevin Papst (www.keleo.de).
*
* For the full copyright and license information, please view the LICENSE
Expand Down
27 changes: 27 additions & 0 deletions Resources/translations/system-configuration.de.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="en" target-language="de" datatype="plaintext" original="system-configuration.en.xlf">
<body>
<trans-unit id="rH7H.bA" resname="label.recalculate">
<source>label.recalculate</source>
<target>Preisneuberechnung</target>
</trans-unit>
<trans-unit id="GLQB2Tb" resname="label.recalculate.mode">
<source>label.recalculate.mode</source>
<target>Modus für Preisneuberechnung</target>
</trans-unit>
<trans-unit id="nQcA1bA" resname="recalculate.mode.off">
<source>recalculate.mode.off</source>
<target>Aus</target>
</trans-unit>
<trans-unit id="hjnOnzm" resname="recalculate.mode.always">
<source>recalculate.mode.always</source>
<target>Bei jeder Änderung</target>
</trans-unit>
<trans-unit id="J1cndMp" resname="recalculate.mode.default">
<source>recalculate.mode.default</source>
<target>Bei Änderung von Kunde, Projekt, Tätigkeit, Benutzer oder Preis</target>
</trans-unit>
</body>
</file>
</xliff>
27 changes: 27 additions & 0 deletions Resources/translations/system-configuration.en.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
<file source-language="en" target-language="en" datatype="plaintext" original="system-configuration.en.xlf">
<body>
<trans-unit id="rH7H.bA" resname="label.recalculate">
<source>label.recalculate</source>
<target>Price recalculation</target>
</trans-unit>
<trans-unit id="GLQB2Tb" resname="label.recalculate.mode">
<source>label.recalculate.mode</source>
<target>Mode for price recalculation</target>
</trans-unit>
<trans-unit id="nQcA1bA" resname="recalculate.mode.off">
<source>recalculate.mode.off</source>
<target>Off</target>
</trans-unit>
<trans-unit id="hjnOnzm" resname="recalculate.mode.always">
<source>recalculate.mode.always</source>
<target>With every update</target>
</trans-unit>
<trans-unit id="J1cndMp" resname="recalculate.mode.default">
<source>recalculate.mode.default</source>
<target>With update of customer, project, activity, user or price</target>
</trans-unit>
</body>
</file>
</xliff>
Loading

0 comments on commit 098ed2a

Please sign in to comment.