automatic billable calculation (#3200)
This commit is contained in:
@@ -10,12 +10,22 @@
|
||||
namespace App\Form\API;
|
||||
|
||||
use App\Form\TimesheetEditForm;
|
||||
use App\Form\Type\BillableType;
|
||||
use App\Form\Type\TagsInputType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class TimesheetApiEditForm extends TimesheetEditForm
|
||||
{
|
||||
protected function addBillable(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
if (!$options['include_billable']) {
|
||||
return;
|
||||
}
|
||||
|
||||
$builder->add('billable', BillableType::class, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -49,6 +59,7 @@ class TimesheetApiEditForm extends TimesheetEditForm
|
||||
// because the docs are cached without these fields otherwise
|
||||
'include_user' => true,
|
||||
'include_exported' => true,
|
||||
'include_billable' => true,
|
||||
'include_rate' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Form\Type\BillableType;
|
||||
use App\Form\Type\BudgetType;
|
||||
use App\Form\Type\DurationType;
|
||||
use App\Form\Type\MetaFieldsCollectionType;
|
||||
@@ -48,7 +49,9 @@ trait EntityFormTrait
|
||||
$builder
|
||||
->add('visible', YesNoType::class, [
|
||||
'label' => 'label.visible',
|
||||
]);
|
||||
])
|
||||
->add('billable', BillableType::class)
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -265,7 +265,7 @@ class TimesheetMultiUpdate extends AbstractType
|
||||
'include_user' => false,
|
||||
'include_rate' => false,
|
||||
'include_exported' => false,
|
||||
'include_billable' => true,
|
||||
'include_billable' => false,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
namespace App\Form;
|
||||
|
||||
use App\Entity\Timesheet;
|
||||
use App\Form\Type\BillableType;
|
||||
use App\Form\Type\DateTimePickerType;
|
||||
use App\Form\Type\DescriptionType;
|
||||
use App\Form\Type\DurationType;
|
||||
@@ -18,10 +17,12 @@ use App\Form\Type\FixedRateType;
|
||||
use App\Form\Type\HourlyRateType;
|
||||
use App\Form\Type\MetaFieldsCollectionType;
|
||||
use App\Form\Type\TagsType;
|
||||
use App\Form\Type\TimesheetBillableType;
|
||||
use App\Form\Type\UserType;
|
||||
use App\Form\Type\YesNoType;
|
||||
use App\Repository\CustomerRepository;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\CallbackTransformer;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
@@ -169,7 +170,7 @@ class TimesheetEditForm extends AbstractType
|
||||
{
|
||||
$durationOptions = [
|
||||
'required' => false,
|
||||
'docu_chapter' => 'timesheet.html#duration-format',
|
||||
'docu_chapter' => 'duration-format.html',
|
||||
'attr' => [
|
||||
'placeholder' => '0:00',
|
||||
],
|
||||
@@ -261,11 +262,57 @@ class TimesheetEditForm extends AbstractType
|
||||
|
||||
protected function addBillable(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
if (!$options['include_billable']) {
|
||||
return;
|
||||
if ($options['include_billable']) {
|
||||
$builder->add('billableMode', TimesheetBillableType::class, []);
|
||||
}
|
||||
|
||||
$builder->add('billable', BillableType::class, []);
|
||||
$builder->addModelTransformer(new CallbackTransformer(
|
||||
function (Timesheet $record) {
|
||||
if ($record->getBillableMode() === Timesheet::BILLABLE_DEFAULT) {
|
||||
if ($record->isBillable()) {
|
||||
$record->setBillableMode(Timesheet::BILLABLE_YES);
|
||||
} else {
|
||||
$record->setBillableMode(Timesheet::BILLABLE_NO);
|
||||
}
|
||||
}
|
||||
|
||||
return $record;
|
||||
},
|
||||
function (Timesheet $record) {
|
||||
switch ($record->getBillableMode()) {
|
||||
case Timesheet::BILLABLE_NO:
|
||||
$record->setBillable(false);
|
||||
break;
|
||||
case Timesheet::BILLABLE_YES:
|
||||
$record->setBillable(true);
|
||||
break;
|
||||
case Timesheet::BILLABLE_AUTOMATIC:
|
||||
$billable = true;
|
||||
|
||||
$activity = $record->getActivity();
|
||||
if ($activity !== null && !$activity->isBillable()) {
|
||||
$billable = false;
|
||||
}
|
||||
|
||||
$project = $record->getProject();
|
||||
if ($billable && $project !== null && !$project->isBillable()) {
|
||||
$billable = false;
|
||||
}
|
||||
|
||||
if ($billable && $project !== null) {
|
||||
$customer = $project->getCustomer();
|
||||
if ($customer !== null && !$customer->isBillable()) {
|
||||
$billable = false;
|
||||
}
|
||||
}
|
||||
|
||||
$record->setBillable($billable);
|
||||
break;
|
||||
}
|
||||
|
||||
return $record;
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,7 +14,6 @@ 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
|
||||
{
|
||||
|
||||
44
src/Form/Type/TimesheetBillableType.php
Normal file
44
src/Form/Type/TimesheetBillableType.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?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 App\Entity\Timesheet;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
/**
|
||||
* Custom form field type to select if a timesheet is billable.
|
||||
*/
|
||||
class TimesheetBillableType extends AbstractType
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'label' => 'label.billable',
|
||||
'choices' => [
|
||||
'automatic' => Timesheet::BILLABLE_AUTOMATIC,
|
||||
'yes' => Timesheet::BILLABLE_YES,
|
||||
'no' => Timesheet::BILLABLE_NO,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getParent()
|
||||
{
|
||||
return ChoiceType::class;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user