Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored tax rate. #147

Merged
merged 2 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal class CreateTaxValidator : AbstractValidator<CreateTaxPayload>
public CreateTaxValidator()
{
RuleFor(x => x.Code).SetValidator(new TaxCodeValidator());
RuleFor(x => x.Rate).GreaterThan(0.0);
RuleFor(x => x.Rate).SetValidator(new TaxRateValidator());

When(x => !string.IsNullOrWhiteSpace(x.Flags), () => RuleFor(x => x.Flags!).SetValidator(new FlagsValidator()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal class ReplaceTaxValidator : AbstractValidator<ReplaceTaxPayload>
public ReplaceTaxValidator()
{
RuleFor(x => x.Code).SetValidator(new TaxCodeValidator());
RuleFor(x => x.Rate).GreaterThan(0.0);
RuleFor(x => x.Rate).SetValidator(new TaxRateValidator());

When(x => !string.IsNullOrWhiteSpace(x.Flags), () => RuleFor(x => x.Flags!).SetValidator(new FlagsValidator()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal class UpdateTaxValidator : AbstractValidator<UpdateTaxPayload>
public UpdateTaxValidator()
{
When(x => !string.IsNullOrWhiteSpace(x.Code), () => RuleFor(x => x.Code!).SetValidator(new TaxCodeValidator()));
When(x => x.Rate.HasValue, () => RuleFor(x => x.Rate!.Value).GreaterThan(0.0));
When(x => x.Rate.HasValue, () => RuleFor(x => x.Rate!.Value).SetValidator(new TaxRateValidator()));

When(x => !string.IsNullOrWhiteSpace(x.Flags?.Value), () => RuleFor(x => x.Flags!.Value!).SetValidator(new FlagsValidator()));
}
Expand Down
5 changes: 3 additions & 2 deletions backend/src/Faktur.Domain/Receipts/ReceiptTaxValidator.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using FluentValidation;
using Faktur.Domain.Taxes;
using FluentValidation;

namespace Faktur.Domain.Receipts;

public class ReceiptTaxValidator : AbstractValidator<ReceiptTaxUnit>
{
public ReceiptTaxValidator()
{
RuleFor(x => x.Rate).GreaterThan(0.0);
RuleFor(x => x.Rate).SetValidator(new TaxRateValidator());
RuleFor(x => x.TaxableAmount).GreaterThanOrEqualTo(0m);
RuleFor(x => x.Amount).GreaterThanOrEqualTo(0m);
}
Expand Down
10 changes: 6 additions & 4 deletions backend/src/Faktur.Domain/Taxes/TaxAggregate.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Faktur.Contracts;
using Faktur.Domain.Products;
using Faktur.Domain.Taxes.Events;
using FluentValidation;
using Logitar.EventSourcing;

namespace Faktur.Domain.Taxes;
Expand All @@ -24,16 +25,17 @@ public TaxCodeUnit Code
}
}
}
private readonly TaxRateValidator _rateValidator = new(nameof(Rate));
private double _rate = 0;
public double Rate
{
get => _rate;
set
{
// TODO(fpion): validation

if (value != _rate)
{
_rateValidator.ValidateAndThrow(value);

_rate = value;
_updatedEvent.Rate = value;
}
Expand All @@ -58,9 +60,9 @@ public TaxAggregate(AggregateId id) : base(id)
{
}

public TaxAggregate(TaxCodeUnit code, double rate = 0.0, ActorId actorId = default, TaxId? id = null) : base((id ?? TaxId.NewId()).AggregateId)
public TaxAggregate(TaxCodeUnit code, double rate, ActorId actorId = default, TaxId? id = null) : base((id ?? TaxId.NewId()).AggregateId)
{
// TODO(fpion): validate rate
new TaxRateValidator(nameof(rate)).ValidateAndThrow(rate);

Raise(new TaxCreatedEvent(code, rate), actorId);
}
Expand Down
11 changes: 11 additions & 0 deletions backend/src/Faktur.Domain/Taxes/TaxRateValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using FluentValidation;

namespace Faktur.Domain.Taxes;

public class TaxRateValidator : AbstractValidator<double>
{
public TaxRateValidator(string? propertyName = null)
{
RuleFor(x => x).GreaterThan(0).LessThan(1).WithPropertyName(propertyName);
}
}
24 changes: 17 additions & 7 deletions frontend/src/components/taxes/TaxRateInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,33 @@ defineProps<{
required?: boolean | string;
}>();

defineEmits<{
const emit = defineEmits<{
(e: "update:model-value", value?: number): void;
}>();
function onModelValueUpdate(value: any): void {
const number: number | undefined = parseNumber(value);
emit("update:model-value", number ? number / 100 : undefined);
}
</script>

<template>
<AppInput
floating
id="rate"
label="taxes.rate"
min="0.00001"
max="1"
:model-value="modelValue?.toString()"
min="0.001"
max="99.999"
:model-value="((modelValue ?? 0) * 100).toString()"
placeholder="taxes.rate"
:required="required"
step="0.00001"
step="0.001"
type="number"
@update:model-value="$emit('update:model-value', parseNumber($event))"
/>
@update:model-value="onModelValueUpdate"
>
<template #append>
<span class="input-group-text">
<font-awesome-icon icon="fas fa-percent" />
</span>
</template>
</AppInput>
</template>
2 changes: 2 additions & 0 deletions frontend/src/fontAwesome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
faFlag,
faHome,
faKey,
faPercent,
faPlus,
faRobot,
faRotate,
Expand Down Expand Up @@ -47,6 +48,7 @@ library.add(
faHome,
faFlag,
faKey,
faPercent,
faPlus,
faRobot,
faRotate,
Expand Down