helper for handling billable fields (#1900)

This commit is contained in:
Kevin Papst
2020-08-20 15:03:57 +02:00
committed by GitHub
parent 7a1dada9dc
commit b9ee811cbf
12 changed files with 213 additions and 44 deletions

View File

@@ -346,4 +346,13 @@ abstract class AbstractToolbarForm extends AbstractType
],
]);
}
protected function addBillableChoice(FormBuilderInterface $builder)
{
$builder->add('billable', BillableType::class, [
'required' => false,
'placeholder' => null,
'search' => false,
]);
}
}

View File

@@ -0,0 +1,40 @@
<?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\Form\Toolbar;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class BillableType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'label' => 'label.billable',
'choices' => [
'entryState.all' => null,
'yes' => true,
'no' => false,
],
]);
}
/**
* {@inheritdoc}
*/
public function getParent()
{
return ChoiceType::class;
}
}

View File

@@ -0,0 +1,38 @@
<?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\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Custom form field type to select if something is billable.
* To be used in combination with the invoicing system.
*/
class BillableType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'label' => 'label.billable',
]);
}
/**
* {@inheritdoc}
*/
public function getParent()
{
return YesNoType::class;
}
}

View File

@@ -0,0 +1,48 @@
<?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\Repository\Query;
interface BillableInterface
{
/**
* Returns the internal value (null = ignore billable, true = is billable, false = is not billable).
*
* @return bool|null
*/
public function getBillable(): ?bool;
/**
* Returns true if the billable flag should be used and should match true.
*
* @return bool
*/
public function isBillable(): bool;
/**
* Returns true if the billable flag should be used and should match false.
*
* @return bool
*/
public function isNotBillable(): bool;
/**
* Returns true if the billable flag should NOT be used.
*
* @return bool
*/
public function isIgnoreBillable(): bool;
/**
* Pas null if you want to ignore the billable flag.
*
* @param bool|null $isBillable
*/
public function setBillable(?bool $isBillable): void;
}

View File

@@ -0,0 +1,43 @@
<?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\Repository\Query;
trait BillableTrait
{
/**
* @var bool|null
*/
private $billable = null;
public function getBillable(): ?bool
{
return $this->billable;
}
public function isBillable(): bool
{
return $this->billable === true;
}
public function isNotBillable(): bool
{
return $this->billable === false;
}
public function isIgnoreBillable(): bool
{
return $this->billable === null;
}
public function setBillable(?bool $isBillable): void
{
$this->billable = $isBillable;
}
}

View File

@@ -25,7 +25,7 @@ class InvoiceQuery extends TimesheetQuery
public function __construct()
{
parent::__construct();
$this->setBillable(InvoiceQuery::STATE_BILLABLE);
$this->setBillable(true);
}
public function getTemplate(): ?InvoiceTemplate

View File

@@ -17,15 +17,15 @@ use App\Form\Model\DateRange;
/**
* Can be used for advanced timesheet repository queries.
*/
class TimesheetQuery extends ActivityQuery
class TimesheetQuery extends ActivityQuery implements BillableInterface
{
use BillableTrait;
public const STATE_ALL = 1;
public const STATE_RUNNING = 2;
public const STATE_STOPPED = 3;
public const STATE_EXPORTED = 4;
public const STATE_NOT_EXPORTED = 5;
public const STATE_BILLABLE = 6;
public const STATE_NOT_BILLABLE = 7;
public const TIMESHEET_ORDER_ALLOWED = ['begin', 'end', 'duration', 'rate', 'customer', 'project', 'activity', 'description'];
@@ -45,10 +45,6 @@ class TimesheetQuery extends ActivityQuery
* @var int
*/
protected $exported = self::STATE_ALL;
/**
* @var int
*/
private $billable = self::STATE_ALL;
/**
* @var \DateTime|null
*/
@@ -295,30 +291,6 @@ class TimesheetQuery extends ActivityQuery
return $this;
}
public function getBillable(): int
{
return $this->billable;
}
public function isBillable(): bool
{
return $this->billable === self::STATE_BILLABLE;
}
public function isNotBillable(): bool
{
return $this->billable === self::STATE_NOT_BILLABLE;
}
public function setBillable(int $billable): TimesheetQuery
{
if (\in_array($billable, [self::STATE_ALL, self::STATE_BILLABLE, self::STATE_NOT_BILLABLE], true)) {
$this->billable = $billable;
}
return $this;
}
public function getModifiedAfter(): ?\DateTime
{
return $this->modifiedAfter;

View File

@@ -10,7 +10,6 @@
namespace App\Tests\Repository\Query;
use App\Repository\Query\InvoiceQuery;
use App\Repository\Query\TimesheetQuery;
/**
* @covers \App\Repository\Query\InvoiceQuery
@@ -36,7 +35,11 @@ class InvoiceQueryTest extends TimesheetQueryTest
$this->assertMarkAsExported($sut);
$this->assertModifiedAfter($sut);
self::assertEquals(TimesheetQuery::STATE_BILLABLE, $sut->getBillable());
self::assertTrue($sut->getBillable());
self::assertTrue($sut->isBillable());
self::assertFalse($sut->isNotBillable());
self::assertFalse($sut->isIgnoreBillable());
self::assertTrue($sut->isBillable());
self::assertFalse($sut->isNotBillable());
$this->assertBillable($sut);

View File

@@ -37,7 +37,11 @@ class TimesheetQueryTest extends BaseQueryTest
$this->assertSearchTerm($sut);
$this->assertModifiedAfter($sut);
self::assertEquals(TimesheetQuery::STATE_ALL, $sut->getBillable());
self::assertNull($sut->getBillable());
self::assertFalse($sut->isBillable());
self::assertFalse($sut->isNotBillable());
self::assertTrue($sut->isIgnoreBillable());
self::assertFalse($sut->isBillable());
self::assertFalse($sut->isNotBillable());
$this->assertBillable($sut);
@@ -140,22 +144,22 @@ class TimesheetQueryTest extends BaseQueryTest
protected function assertBillable(TimesheetQuery $sut)
{
self::assertInstanceOf(TimesheetQuery::class, $sut->setBillable(TimesheetQuery::STATE_ALL));
self::assertEquals(TimesheetQuery::STATE_ALL, $sut->getBillable());
$sut->setBillable(null);
self::assertNull($sut->getBillable());
self::assertFalse($sut->isBillable());
self::assertFalse($sut->isNotBillable());
self::assertTrue($sut->isIgnoreBillable());
$sut->setBillable(PHP_INT_MAX);
self::assertEquals(TimesheetQuery::STATE_ALL, $sut->getBillable());
$sut->setBillable(TimesheetQuery::STATE_BILLABLE);
self::assertEquals(TimesheetQuery::STATE_BILLABLE, $sut->getBillable());
$sut->setBillable(true);
self::assertTrue($sut->getBillable());
self::assertTrue($sut->isBillable());
self::assertFalse($sut->isNotBillable());
self::assertFalse($sut->isIgnoreBillable());
$sut->setBillable(TimesheetQuery::STATE_NOT_BILLABLE);
self::assertEquals(TimesheetQuery::STATE_NOT_BILLABLE, $sut->getBillable());
$sut->setBillable(false);
self::assertFalse($sut->getBillable());
self::assertFalse($sut->isBillable());
self::assertTrue($sut->isNotBillable());
self::assertFalse($sut->isIgnoreBillable());
}
}

View File

@@ -340,6 +340,10 @@
<source>placeholder.type_message</source>
<target>Schreibe Deine Nachricht...</target>
</trans-unit>
<trans-unit id="label.billable">
<source>label.billable</source>
<target>Abrechenbar</target>
</trans-unit>
<!--
Buttons & Actions

View File

@@ -340,6 +340,10 @@
<source>placeholder.type_message</source>
<target>Type your message...</target>
</trans-unit>
<trans-unit id="label.billable">
<source>label.billable</source>
<target>Billable</target>
</trans-unit>
<!--
Buttons & Actions

View File

@@ -312,6 +312,10 @@
<source>label.create_more</source>
<target>צור רשומות נוספות</target>
</trans-unit>
<trans-unit id="label.billable">
<source>label.billable</source>
<target>להחזר</target>
</trans-unit>
<trans-unit id="zldXQq6" resname="label.actions">
<source>label.actions</source>
<target>פעולות</target>