Skip to content

Commit

Permalink
New - Ajout de la possibilité de déclarer un Producteur comme non ass…
Browse files Browse the repository at this point in the history
…ujetti à la TVA
  • Loading branch information
noelmugnier committed Nov 8, 2020
1 parent 5133667 commit 7944763
Show file tree
Hide file tree
Showing 18 changed files with 3,377 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public RegisterProducerCommand(RequestUser requestUser) : base(requestUser)
public BusinessLegalInput Legals { get; set; }
public FullAddressInput Address { get; set; }
public bool OpenForNewBusiness { get; set; }
public bool NotSubjectToVat { get; set; }
public IEnumerable<Guid> Tags { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using Sheaft.Core;
using Newtonsoft.Json;

namespace Sheaft.Application.Commands
{
public class SetProducerProductsWithNoVatCommand : Command<bool>
{
[JsonConstructor]
public SetProducerProductsWithNoVatCommand(RequestUser requestUser) : base(requestUser)
{
}

public Guid ProducerId { get; set; }
}
}
2 changes: 1 addition & 1 deletion Sheaft.Application.Commands/Product/ProductCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected ProductCommand(RequestUser requestUser) : base(requestUser)
public decimal QuantityPerUnit { get; set; }
public UnitKind Unit { get; set; }
public ConditioningKind Conditioning { get; set; }
public decimal Vat { get; set; }
public decimal? Vat { get; set; }
public decimal? Weight { get; set; }
public string Description { get; set; }
public bool? Available { get; set; }
Expand Down
25 changes: 23 additions & 2 deletions Sheaft.Application.Handlers/Commands/ProducerCommandsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class ProducerCommandsHandler : ResultsHandler,
IRequestHandler<UpdateProducerCommand, Result<bool>>,
IRequestHandler<CheckProducerConfigurationCommand, Result<bool>>,
IRequestHandler<EnsureProducerDocumentsValidatedCommand, Result<bool>>,
IRequestHandler<UpdateProducerTagsCommand, Result<bool>>
IRequestHandler<UpdateProducerTagsCommand, Result<bool>>,
IRequestHandler<SetProducerProductsWithNoVatCommand, Result<bool>>
{
private readonly RoleOptions _roleOptions;

Expand Down Expand Up @@ -58,6 +59,7 @@ public async Task<Result<Guid>> Handle(RegisterProducerCommand request, Cancella
producer = new Producer(request.RequestUser.Id, request.Name, request.FirstName, request.LastName, request.Email,
address, request.OpenForNewBusiness, request.Phone, request.Description);
producer.SetNotSubjectToVat(request.NotSubjectToVat);
if (request.Tags != null && request.Tags.Any())
{
Expand Down Expand Up @@ -215,12 +217,31 @@ public async Task<Result<bool>> Handle(UpdateProducerTagsCommand request, Cancel
{
var producer = await _context.GetByIdAsync<Producer>(request.ProducerId, token);
var productTags = await _context.Products.Where(p => p.Producer.Id == producer.Id).SelectMany(p => p.Tags).Select(p => p.Tag).Distinct().ToListAsync(token);
var productTags = await _context.Products.Get(p => p.Producer.Id == producer.Id).SelectMany(p => p.Tags).Select(p => p.Tag).Distinct().ToListAsync(token);
producer.SetTags(productTags);
await _context.SaveChangesAsync(token);
return Ok(true);
});
}

public async Task<Result<bool>> Handle(SetProducerProductsWithNoVatCommand request, CancellationToken token)
{
return await ExecuteAsync(request, async () =>
{
var producer = await _context.GetByIdAsync<Producer>(request.ProducerId, token);
if (!producer.NotSubjectToVat)
return Ok(false);
var products = await _context.FindAsync<Product>(p => p.Producer.Id == producer.Id, token);
foreach(var product in products)
{
product.SetVat(0);
await _context.SaveChangesAsync(token);
}
return Ok(true);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ public async Task<Result<Guid>> Handle(CreateProductCommand request, Cancellatio
}
var producer = await _context.GetByIdAsync<Producer>(request.RequestUser.Id, token);
var entity = new Product(Guid.NewGuid(), request.Reference, request.Name, request.WholeSalePricePerUnit, request.Conditioning, request.Unit, request.QuantityPerUnit, request.Vat, producer);
var entity = new Product(Guid.NewGuid(), request.Reference, request.Name, request.WholeSalePricePerUnit, request.Conditioning, request.Unit, request.QuantityPerUnit, producer);
entity.SetVat(request.Vat);
entity.SetDescription(request.Description);
entity.SetAvailable(request.Available ?? true);
entity.SetStoreVisibility(request.VisibleToStores ?? false);
Expand Down Expand Up @@ -102,6 +103,7 @@ public async Task<Result<bool>> Handle(UpdateProductCommand request, Cancellatio
{
var entity = await _context.GetByIdAsync<Product>(request.Id, token);
entity.SetVat(request.Vat);
entity.SetName(request.Name);
entity.SetDescription(request.Description);
entity.SetVat(request.Vat);
Expand Down
1 change: 1 addition & 0 deletions Sheaft.Application.Models/Dto/ProducerDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class ProducerDto
public string VatIdentifier { get; set; }
public string Siret { get; set; }
public bool OpenForNewBusiness { get; set; }
public bool NotSubjectToVat { get; set; }
public AddressDto Address { get; set; }
public IEnumerable<TagDto> Tags { get; set; }
}
Expand Down
1 change: 1 addition & 0 deletions Sheaft.Application.Models/Inputs/BusinessInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class BusinessInput
public string Picture { get; set; }
public FullAddressInput Address { get; set; }
public bool OpenForNewBusiness { get; set; }
public bool NotSubjectToVat { get; set; }
public IEnumerable<Guid> Tags { get; set; }
}
}
2 changes: 1 addition & 1 deletion Sheaft.Application.Models/Inputs/ProductInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class CreateProductInput
public decimal QuantityPerUnit { get; set; }
public UnitKind Unit { get; set; }
public ConditioningKind Conditioning { get; set; }
public decimal Vat { get; set; }
public decimal? Vat { get; set; }
public decimal? Weight { get; set; }
public string Description { get; set; }
public bool? Available { get; set; }
Expand Down
6 changes: 6 additions & 0 deletions Sheaft.Domain/Producer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public Producer(Guid id, string name, string firstname, string lastname, string
public virtual IReadOnlyCollection<ProducerTag> Tags => _tags?.AsReadOnly();

public bool CanDirectSell { get; set; }
public bool NotSubjectToVat { get; set; }

public void SetTags(IEnumerable<Tag> tags)
{
Expand All @@ -32,5 +33,10 @@ public void SetTags(IEnumerable<Tag> tags)

_tags = tags.Select(t => new ProducerTag(t)).ToList();
}

public void SetNotSubjectToVat(bool notSubjectToVat)
{
NotSubjectToVat = notSubjectToVat;
}
}
}
12 changes: 8 additions & 4 deletions Sheaft.Domain/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected Product()
{
}

public Product(Guid id, string reference, string name, decimal price, ConditioningKind conditioning, UnitKind unit, decimal quantityPerUnit, decimal vat, Producer producer)
public Product(Guid id, string reference, string name, decimal price, ConditioningKind conditioning, UnitKind unit, decimal quantityPerUnit, Producer producer)
{
Id = id;
Producer = producer ?? throw new ValidationException(MessageKind.Product_Producer_Required);
Expand All @@ -28,7 +28,6 @@ public Product(Guid id, string reference, string name, decimal price, Conditioni
SetReference(reference);
SetConditioning(conditioning, quantityPerUnit, unit);
SetWholeSalePricePerUnit(price);
SetVat(vat);

_tags = new List<ProductTag>();
_ratings = new List<Rating>();
Expand Down Expand Up @@ -136,15 +135,20 @@ public void SetWeight(decimal? newWeight)
Weight = Math.Round(newWeight.Value, DIGITS_COUNT);
}

public void SetVat(decimal newVat)
public void SetVat(decimal? newVat)
{
if (Producer.NotSubjectToVat)
newVat = 0;
else if(!newVat.HasValue)
throw new ValidationException(MessageKind.Product_Vat_Required);

if (newVat < 0)
throw new ValidationException(MessageKind.Product_Vat_CannotBe_LowerThan, 0);

if (newVat > 100)
throw new ValidationException(MessageKind.Product_Vat_CannotBe_GreaterThan, 100);

Vat = newVat;
Vat = newVat.Value;
RefreshPrices();
}

Expand Down
1 change: 1 addition & 0 deletions Sheaft.Exceptions/Enums/MessageKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public enum MessageKind
Product_TagNotFound = 6110,
Product_CannotRate_AlreadyRated = 6111,
Product_BulkConditioning_Requires_Unit_ToBe_Specified = 6112,
Product_Vat_Required = 6113,
Consumer = 7000,
Consumer_Id_Invalid = 7101,
Consumer_Email_Required = 7102,
Expand Down
1 change: 1 addition & 0 deletions Sheaft.GraphQL.Types/Inputs/RegisterProducerInputType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class RegisterProducerInputType : SheaftInputType<RegisterProducerInput>
protected override void Configure(IInputObjectTypeDescriptor<RegisterProducerInput> descriptor)
{
descriptor.Field(c => c.OpenForNewBusiness);
descriptor.Field(c => c.NotSubjectToVat);
descriptor.Field(c => c.Description);
descriptor.Field(c => c.Phone);
descriptor.Field(c => c.Picture);
Expand Down
1 change: 1 addition & 0 deletions Sheaft.GraphQL.Types/Outputs/ProducerType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ protected override void Configure(IObjectTypeDescriptor<ProducerDto> descriptor)
descriptor.Field(c => c.Description);
descriptor.Field(c => c.VatIdentifier);
descriptor.Field(c => c.OpenForNewBusiness);
descriptor.Field(c => c.NotSubjectToVat);
descriptor.Field(c => c.FirstName);
descriptor.Field(c => c.LastName);
descriptor.Field(c => c.Email);
Expand Down
Loading

0 comments on commit 7944763

Please sign in to comment.