Skip to content

Commit

Permalink
New - Ajout d'une condition pour vérifier si les documents légaux son…
Browse files Browse the repository at this point in the history
…t requis ou non (150€/mois) de CA
  • Loading branch information
noelmugnier committed Nov 7, 2020
1 parent 6cfee2f commit cb0dcf0
Show file tree
Hide file tree
Showing 14 changed files with 3,432 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public EnsureDeclarationIsValidatedCommand(RequestUser requestUser) : base(reque
{
}

public Guid UserId { get; set; }
public Guid ProducerId { 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 CheckLegalsDeclarationRequiredCommand : Command<bool>
{
[JsonConstructor]
public CheckLegalsDeclarationRequiredCommand(RequestUser requestUser) : base(requestUser)
{
}

public Guid UserId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public async Task<Result<bool>> Handle(EnsureDeclarationIsValidatedCommand reque
{
return await ExecuteAsync(request, async () =>
{
var legal = await _context.GetSingleAsync<BusinessLegal>(bl => bl.User.Id == request.UserId, token);
var legal = await _context.GetSingleAsync<BusinessLegal>(bl => bl.User.Id == request.ProducerId, token);
if (legal.Declaration == null)
{
var result = await _mediatr.Process(new CreateDeclarationCommand(request.RequestUser)
Expand Down
36 changes: 35 additions & 1 deletion Sheaft.Application.Handlers/Commands/LegalCommandsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Threading;
using Sheaft.Domain.Models;
using Sheaft.Domain.Enums;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace Sheaft.Application.Handlers
{
Expand All @@ -17,7 +19,8 @@ public class LegalCommandsHandler : ResultsHandler,
IRequestHandler<UpdateBusinessLegalCommand, Result<bool>>,
IRequestHandler<UpdateConsumerLegalCommand, Result<bool>>,
IRequestHandler<CheckBusinessLegalConfigurationCommand, Result<bool>>,
IRequestHandler<CheckConsumerLegalConfigurationCommand, Result<bool>>
IRequestHandler<CheckConsumerLegalConfigurationCommand, Result<bool>>,
IRequestHandler<CheckLegalsDeclarationRequiredCommand, Result<bool>>
{
private readonly IPspService _pspService;

Expand Down Expand Up @@ -234,5 +237,36 @@ public async Task<Result<bool>> Handle(CheckConsumerLegalConfigurationCommand re
return Ok(true);
});
}

public async Task<Result<bool>> Handle(CheckLegalsDeclarationRequiredCommand request, CancellationToken token)
{
return await ExecuteAsync(request, async () =>
{
var legal = await _context.GetSingleAsync<BusinessLegal>(b => b.User.Id == request.UserId, token);
if (legal.User.Kind != ProfileKind.Producer)
return Ok(false);
if (legal.DeclarationRequired)
return Ok(legal.DeclarationRequired);
var currentMonth = DateTimeOffset.UtcNow.Month;
var cumulatedMonthAmount = await _context.PurchaseOrders
.Get(po =>
po.Vendor.Id == request.UserId &&
po.Sender.Kind == ProfileKind.Consumer &&
po.Status == PurchaseOrderStatus.Delivered &&
po.DeliveredOn.HasValue &&
po.DeliveredOn.Value.Month == currentMonth)
.SumAsync(po => po.TotalOnSalePrice, token);
if (cumulatedMonthAmount >= 150)
{
legal.SetDeclarationRequired(true);
await _context.SaveChangesAsync(token);
}
return Ok(legal.DeclarationRequired);
});
}
}
}
16 changes: 10 additions & 6 deletions Sheaft.Application.Handlers/Commands/PayoutCommandsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,17 @@ public async Task<Result<Guid>> Handle(CreatePayoutCommand request, Cancellation
if (!checkConfigurationResult.Success)
return Failed<Guid>(checkConfigurationResult.Exception);
var checkDocumentsResult = await _mediatr.Process(new EnsureProducerDocumentsValidatedCommand(request.RequestUser) { ProducerId = request.ProducerId }, token);
if (!checkDocumentsResult.Success)
return Failed<Guid>(checkDocumentsResult.Exception);
var producerLegals = await _context.GetSingleAsync<BusinessLegal>(c => c.User.Id == request.ProducerId, token);
if (producerLegals.DeclarationRequired)
{
var checkDocumentsResult = await _mediatr.Process(new EnsureProducerDocumentsValidatedCommand(request.RequestUser) { ProducerId = request.ProducerId }, token);
if (!checkDocumentsResult.Success)
return Failed<Guid>(checkDocumentsResult.Exception);
var checkDeclarationResult = await _mediatr.Process(new EnsureDeclarationIsValidatedCommand(request.RequestUser) { UserId = request.ProducerId }, token);
if (!checkDeclarationResult.Success)
return Failed<Guid>(checkDeclarationResult.Exception);
var checkDeclarationResult = await _mediatr.Process(new EnsureDeclarationIsValidatedCommand(request.RequestUser) { ProducerId = request.ProducerId }, token);
if (!checkDeclarationResult.Success)
return Failed<Guid>(checkDeclarationResult.Exception);
}
var wallet = await _context.GetSingleAsync<Wallet>(c => c.User.Id == request.ProducerId, token);
var bankAccount = await _context.GetSingleAsync<BankAccount>(c => c.User.Id == request.ProducerId && c.IsActive, token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Sheaft.Application.Events;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using Sheaft.Domain.Enums;

namespace Sheaft.Application.Handlers
{
Expand Down Expand Up @@ -240,6 +239,10 @@ public async Task<Result<bool>> Handle(DeliverPurchaseOrderCommand request, Canc
await _context.SaveChangesAsync(token);
var producerLegals = await _context.GetSingleAsync<BusinessLegal>(c => c.User.Id == purchaseOrder.Vendor.Id, token);
if(!producerLegals.DeclarationRequired)
_mediatr.Post(new CheckLegalsDeclarationRequiredCommand(request.RequestUser) { UserId = purchaseOrder.Vendor.Id });
_mediatr.Schedule(new CreateTransferCommand(request.RequestUser) { PurchaseOrderId = purchaseOrder.Id }, TimeSpan.FromDays(7));
return Ok(true);
});
Expand Down
13 changes: 13 additions & 0 deletions Sheaft.Domain/BusinessLegal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Sheaft.Domains.Extensions;
using Sheaft.Exceptions;
using System;
using System.Linq;

namespace Sheaft.Domain.Models
{
Expand All @@ -18,13 +19,16 @@ public BusinessLegal(Guid id, Business business, LegalKind kind, string email, s
SetAddress(address);
SetSiret(siret);
SetVatIdentifier(vatIdentifier);
DeclarationRequired = false;
}

public string Email { get; private set; }
public string Siret { get; private set; }
public string VatIdentifier { get; private set; }
public virtual LegalAddress Address { get; private set; }
public virtual Declaration Declaration { get; private set; }
public bool DeclarationRequired { get; private set; }
public bool IsComplete => !DeclarationRequired || (DeclarationRequired && Declaration?.Status == DeclarationStatus.Validated && Documents.All(d => d.Status == DocumentStatus.Validated));

public void SetDeclaration()
{
Expand All @@ -34,6 +38,15 @@ public void SetDeclaration()
Declaration = new Declaration(Guid.NewGuid());
}


public void SetDeclarationRequired(bool validationRequired)
{
if (DeclarationRequired)
throw new ValidationException(MessageKind.Legal_Cannot_Unrequire_Declaration);

DeclarationRequired = validationRequired;
}

public void SetSiret(string siret)
{
if (siret.IsNotNullAndIsEmptyOrWhiteSpace())
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 @@ -151,6 +151,7 @@ public enum MessageKind
Legal_Vat_Required = 28101,
Legal_Kind_Cannot_Be_Natural = 28102,
Legal_Email_Required = 28103,
Legal_Cannot_Unrequire_Declaration = 28104,
Payin = 29000,
Payin_CannotSet_Already_Succeeded = 29101,
Payin_CannotAdd_Refund_PurchaseOrderRefund_AlreadySucceeded = 29102,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public void Configure(EntityTypeBuilder<BusinessLegal> entity)
{
entity.Property(c => c.Siret).IsRequired();
entity.Property(c => c.VatIdentifier);
entity.Ignore(c => c.IsComplete);

entity.OwnsOne(c => c.Address, e => {
e.ToTable("BusinessLegalAddresses");
Expand Down
Loading

0 comments on commit cb0dcf0

Please sign in to comment.