Skip to content

Commit

Permalink
Fix setting tax rates on invoices and quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
pierredup committed May 11, 2023
1 parent b6d6052 commit 90196e2
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 111 deletions.
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,6 @@ parameters:
count: 1
path: src/ClientBundle/Form/ConstraintBuilder.php

-
message: "#^Class SolidInvoice\\\\ClientBundle\\\\Form\\\\DataTransformer\\\\ContactTypeTransformer implements generic interface Symfony\\\\Component\\\\Form\\\\DataTransformerInterface but does not specify its types\\: T, R$#"
count: 1
path: src/ClientBundle/Form/DataTransformer/ContactTypeTransformer.php

-
message: "#^Parameter \\#2 \\$default of method SolidWorx\\\\FormHandler\\\\Options\\:\\:get\\(\\) expects null, SolidInvoice\\\\ClientBundle\\\\Entity\\\\Client given\\.$#"
count: 1
Expand All @@ -130,11 +125,6 @@ parameters:
count: 1
path: src/ClientBundle/Form/Handler/AbstractContactFormHandler.php

-
message: "#^Class SolidInvoice\\\\ClientBundle\\\\Form\\\\ViewTransformer\\\\ContactTypeTransformer implements generic interface Symfony\\\\Component\\\\Form\\\\DataTransformerInterface but does not specify its types\\: T, R$#"
count: 1
path: src/ClientBundle/Form/ViewTransformer/ContactTypeTransformer.php

-
message: "#^Class SolidInvoice\\\\ClientBundle\\\\Repository\\\\ContactRepository extends generic class Doctrine\\\\Bundle\\\\DoctrineBundle\\\\Repository\\\\ServiceEntityRepository but does not specify its types\\: TEntityClass$#"
count: 1
Expand Down
4 changes: 2 additions & 2 deletions src/ClientBundle/Form/Type/ContactDetailType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

namespace SolidInvoice\ClientBundle\Form\Type;

use SolidInvoice\ClientBundle\Form\DataTransformer\ContactTypeTransformer;
use SolidInvoice\ClientBundle\Repository\ContactTypeRepository;
use SolidInvoice\CoreBundle\Form\DataTransformer\EntityUuidTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
Expand Down Expand Up @@ -55,7 +55,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
],
]
)
->addModelTransformer(new ContactTypeTransformer($this->contactTypeRepository->findAll()))
->addModelTransformer(new EntityUuidTransformer($this->contactTypeRepository->findAll()))
);

$builder->add(
Expand Down
44 changes: 0 additions & 44 deletions src/ClientBundle/Form/ViewTransformer/ContactTypeTransformer.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,51 @@
* with this source code in the file LICENSE.
*/

namespace SolidInvoice\ClientBundle\Form\DataTransformer;
namespace SolidInvoice\CoreBundle\Form\DataTransformer;

use Ramsey\Uuid\UuidInterface;
use SolidInvoice\ClientBundle\Entity\ContactType;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use function method_exists;

class ContactTypeTransformer implements DataTransformerInterface
/**
* @implements DataTransformerInterface<int, object|string>
*/
final class EntityUuidTransformer implements DataTransformerInterface
{
/**
* @var ContactType[]
* @var object[]
*/
private array $types;

/**
* @param ContactType[] $types
* @param object[] $types
*/
public function __construct(array $types)
{
$this->types = $types;
}

/**
* @param ?ContactType $type
* @param ?object $value
*/
public function transform($type): ?UuidInterface
public function transform($value): ?UuidInterface
{
if ($type instanceof ContactType) {
return $type->getId();
if (is_object($value) && method_exists($value, 'getId')) {
return $value->getId();

Check warning on line 45 in src/CoreBundle/Form/DataTransformer/EntityUuidTransformer.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/Form/DataTransformer/EntityUuidTransformer.php#L45

Added line #L45 was not covered by tests
}

return null;
}

/**
* @param string $value
* @param object|string $value
*
* @return ContactType
* @return object
*
* @throws TransformationFailedException
*/
public function reverseTransform($value): ?ContactType
public function reverseTransform($value): ?object
{
if ('' === $value) {
return null;
Expand All @@ -64,6 +67,6 @@ public function reverseTransform($value): ?ContactType
}
}

throw new TransformationFailedException('Invalid contact type');
throw new TransformationFailedException('Invalid value');

Check warning on line 70 in src/CoreBundle/Form/DataTransformer/EntityUuidTransformer.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/Form/DataTransformer/EntityUuidTransformer.php#L70

Added line #L70 was not covered by tests
}
}
5 changes: 1 addition & 4 deletions src/CoreBundle/Form/Type/Select2Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ public function getParent(): string
return ChoiceType::class;
}

/**
* @return string
*/
public function getBlockPrefix()
public function getBlockPrefix(): string
{
return 'select2';
}
Expand Down
22 changes: 9 additions & 13 deletions src/CoreBundle/Resources/views/Form/fields.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,13 @@
{% for attrname, attrvalue in attr %}{% if attrname in ['placeholder', 'title'] %}{{ attrname }}="{{ attrvalue|trans({}, translation_domain) }}" {% else %}{{ attrname }}="{{ attrvalue }}" {% endif %}{% endfor %}
{%- endblock widget_attributes %}

{% block form_widget_simple -%}{{ parent() }}{% if help is defined and help is not empty %}<span class="help-{{ help_type }}"><small>{{ help|trans|nl2br }}</small></span>{% endif %}{%- endblock form_widget_simple %}

{% block form_row -%}
<div class="form-group{% if (not compound or force_error|default(false)) and not valid %} has-error{% endif %}">
{{ form_label(form) }}
{{ form_widget(form) }}
{#{% if help is defined and help is not empty %}
<span class="help-{{ help_type }}"><small>{{ help|trans|nl2br }}</small></span>
{% endif %}#}
</div>
{%- endblock form_row %}
{% block form_help -%}
{%- if help is not empty -%}
<div class="alert alert-info mt-2">
{{ parent()|replace({'text-muted': ''})|raw }}
</div>
{%- endif -%}
{%- endblock form_help %}

{% block checkbox_widget -%}
{{ parent() }}
Expand Down Expand Up @@ -75,7 +71,7 @@
{% include "@SolidInvoiceClient/Form/contacts.html.twig" %}
{% endblock contact_widget %}

{% block tax_field_widget -%}
{#{% block tax_field_widget -%}
{% if expanded %}
{{- block('choice_widget_expanded') -}}
{% else %}
Expand All @@ -98,7 +94,7 @@
{{ block('tax_choice_widget_options') }}
</select>
{% endif %}
{%- endblock %}
{%- endblock %}#}

{% block tax_number_widget -%}
<div class="input-group">
Expand Down
4 changes: 2 additions & 2 deletions src/InvoiceBundle/Form/Type/ItemType.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'tax',
TaxEntityType::class,
[
'class' => Tax::class,
'placeholder' => 'No Tax',
'attr' => [
'class' => 'select2 input-mini invoice-item-tax',
Expand All @@ -94,7 +93,8 @@ public function getBlockPrefix(): string

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefault('data_class', Item::class)
$resolver
->setDefault('data_class', Item::class)
->setRequired('currency')
->setAllowedTypes('currency', [Currency::class]);
}
Expand Down
42 changes: 33 additions & 9 deletions src/TaxBundle/Form/Type/TaxEntityType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,47 @@

namespace SolidInvoice\TaxBundle\Form\Type;

use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use SolidInvoice\CoreBundle\Form\DataTransformer\EntityUuidTransformer;
use SolidInvoice\TaxBundle\Entity\Tax;
use SolidInvoice\TaxBundle\Repository\TaxRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use function array_combine;

class TaxEntityType extends AbstractType
final class TaxEntityType extends AbstractType
{
public function getParent(): string
private TaxRepository $repository;

public function __construct(TaxRepository $repository)

Check warning on line 29 in src/TaxBundle/Form/Type/TaxEntityType.php

View check run for this annotation

Codecov / codecov/patch

src/TaxBundle/Form/Type/TaxEntityType.php#L29

Added line #L29 was not covered by tests
{
return EntityType::class;
$this->repository = $repository;

Check warning on line 31 in src/TaxBundle/Form/Type/TaxEntityType.php

View check run for this annotation

Codecov / codecov/patch

src/TaxBundle/Form/Type/TaxEntityType.php#L31

Added line #L31 was not covered by tests
}

/**
* Returns the name of this type.
*
* @return string The name of this type
* @param array<string, mixed> $options
*/
public function getBlockPrefix()
public function buildForm(FormBuilderInterface $builder, array $options): void

Check warning on line 37 in src/TaxBundle/Form/Type/TaxEntityType.php

View check run for this annotation

Codecov / codecov/patch

src/TaxBundle/Form/Type/TaxEntityType.php#L37

Added line #L37 was not covered by tests
{
$builder->addModelTransformer(new EntityUuidTransformer($this->repository->findAll()));

Check warning on line 39 in src/TaxBundle/Form/Type/TaxEntityType.php

View check run for this annotation

Codecov / codecov/patch

src/TaxBundle/Form/Type/TaxEntityType.php#L39

Added line #L39 was not covered by tests
}

public function configureOptions(OptionsResolver $resolver): void

Check warning on line 42 in src/TaxBundle/Form/Type/TaxEntityType.php

View check run for this annotation

Codecov / codecov/patch

src/TaxBundle/Form/Type/TaxEntityType.php#L42

Added line #L42 was not covered by tests
{
$values = $this->repository->findAll();

Check warning on line 44 in src/TaxBundle/Form/Type/TaxEntityType.php

View check run for this annotation

Codecov / codecov/patch

src/TaxBundle/Form/Type/TaxEntityType.php#L44

Added line #L44 was not covered by tests

$resolver->setDefault(
'choices',
array_combine(
$values,
array_map(static fn (Tax $tax) => $tax->getId()->toString(), $values),
)
);

Check warning on line 52 in src/TaxBundle/Form/Type/TaxEntityType.php

View check run for this annotation

Codecov / codecov/patch

src/TaxBundle/Form/Type/TaxEntityType.php#L46-L52

Added lines #L46 - L52 were not covered by tests
}

public function getParent(): string

Check warning on line 55 in src/TaxBundle/Form/Type/TaxEntityType.php

View check run for this annotation

Codecov / codecov/patch

src/TaxBundle/Form/Type/TaxEntityType.php#L55

Added line #L55 was not covered by tests
{
return 'tax_field';
return ChoiceType::class;

Check warning on line 57 in src/TaxBundle/Form/Type/TaxEntityType.php

View check run for this annotation

Codecov / codecov/patch

src/TaxBundle/Form/Type/TaxEntityType.php#L57

Added line #L57 was not covered by tests
}
}
1 change: 1 addition & 0 deletions src/TaxBundle/Form/Type/TaxType.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
[
'choices' => array_map('ucwords', Tax::getTypes()),
'help' => 'tax.rates.explanation',
'help_html' => true,
'placeholder' => 'tax.rates.type.select',
]
);
Expand Down
13 changes: 1 addition & 12 deletions src/TaxBundle/Repository/TaxRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,7 @@ public function getTaxList(): array

public function taxRatesConfigured(): bool
{
return $this->getTotal() > 0;
}

/**
* Gets an array of all the available tax rates.
*/
public function getTotal(): int
{
$queryBuilder = $this->createQueryBuilder('t')
->select('COUNT(t.id)');

return (int) $queryBuilder->getQuery()->getSingleScalarResult();
return $this->count([]) > 0;
}

public function getGridQuery(): QueryBuilder
Expand Down
9 changes: 9 additions & 0 deletions src/TaxBundle/Resources/config/services/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:

_defaults:
public: false
autowire: true
autoconfigure: true

SolidInvoice\TaxBundle\Repository\TaxRepository: ~
SolidInvoice\TaxBundle\Form\Type\TaxEntityType: ~
1 change: 0 additions & 1 deletion src/TaxBundle/Resources/config/services/twig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ services:
autoconfigure: true

SolidInvoice\TaxBundle\Twig\Extension\TaxExtension: ~
SolidInvoice\TaxBundle\Repository\TaxRepository: ~
4 changes: 3 additions & 1 deletion src/TaxBundle/Resources/translations/messages.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ tax:
title: 'Tax Rates'
add: 'Add Tax Rate'
save: Save
explanation: "Inclusive: Tax is included in the item price\nExclusive: Tax is calculated on the item price"
explanation: |
<b>Inclusive</b>: The Tax is already included in the item price<br />
<b>Exclusive</b>: Tax is calculated on top of the item price
type:
select: Select
inclusive: Inclusive
Expand Down

0 comments on commit 90196e2

Please sign in to comment.