initial commit
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
|
||||
namespace Indotalent.Features.Sales.PaymentReceive.Cqrs;
|
||||
|
||||
public class CreatePaymentReceiveRequest
|
||||
{
|
||||
public string? InvoiceId { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public DateTime? PaymentDate { get; set; }
|
||||
public string? PaymentMethodId { get; set; }
|
||||
public decimal? PaymentAmount { get; set; }
|
||||
public Data.Enums.PaymentReceiveStatus Status { get; set; }
|
||||
}
|
||||
|
||||
public class CreatePaymentReceiveResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
}
|
||||
|
||||
public record CreatePaymentReceiveCommand(CreatePaymentReceiveRequest Data) : IRequest<CreatePaymentReceiveResponse>;
|
||||
|
||||
public class CreatePaymentReceiveHandler : IRequestHandler<CreatePaymentReceiveCommand, CreatePaymentReceiveResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public CreatePaymentReceiveHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreatePaymentReceiveResponse> Handle(CreatePaymentReceiveCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entityName = nameof(Data.Entities.PaymentReceive);
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"PAY/{{Year}}/{{Month}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.PaymentReceive
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
InvoiceId = request.Data.InvoiceId,
|
||||
Description = request.Data.Description,
|
||||
PaymentDate = request.Data.PaymentDate,
|
||||
PaymentMethodId = request.Data.PaymentMethodId,
|
||||
PaymentAmount = request.Data.PaymentAmount ?? 0,
|
||||
Status = request.Data.Status
|
||||
};
|
||||
|
||||
_context.PaymentReceive.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreatePaymentReceiveResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
AutoNumber = entity.AutoNumber
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Indotalent.Features.Sales.PaymentReceive.Cqrs;
|
||||
|
||||
public class CreatePaymentReceiveValidator : AbstractValidator<CreatePaymentReceiveRequest>
|
||||
{
|
||||
public CreatePaymentReceiveValidator()
|
||||
{
|
||||
RuleFor(x => x.InvoiceId).NotEmpty().WithMessage("Invoice reference is required");
|
||||
RuleFor(x => x.PaymentDate).NotEmpty().WithMessage("Payment date is required");
|
||||
RuleFor(x => x.PaymentMethodId).NotEmpty().WithMessage("Payment method is required");
|
||||
RuleFor(x => x.PaymentAmount).GreaterThan(0).WithMessage("Amount must be greater than 0");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Sales.PaymentReceive.Cqrs;
|
||||
|
||||
public record DeletePaymentReceiveByIdCommand(string Id) : IRequest<bool>;
|
||||
|
||||
public class DeletePaymentReceiveByIdHandler : IRequestHandler<DeletePaymentReceiveByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public DeletePaymentReceiveByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeletePaymentReceiveByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.PaymentReceive
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.PaymentReceive.Remove(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Sales.PaymentReceive.Cqrs;
|
||||
|
||||
public record GetInvoiceDetailForPaymentQuery(string InvoiceId) : IRequest<GetPaymentReceiveByIdResponse?>;
|
||||
|
||||
public class GetInvoiceDetailForPaymentHandler : IRequestHandler<GetInvoiceDetailForPaymentQuery, GetPaymentReceiveByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetInvoiceDetailForPaymentHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetPaymentReceiveByIdResponse?> Handle(GetInvoiceDetailForPaymentQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var invoice = await _context.Invoice
|
||||
.AsNoTracking()
|
||||
.Include(x => x.SalesOrder)
|
||||
.ThenInclude(so => so!.Customer)
|
||||
.Include(x => x.SalesOrder)
|
||||
.ThenInclude(so => so!.SalesOrderItemList)
|
||||
.ThenInclude(si => si.Product)
|
||||
.FirstOrDefaultAsync(x => x.Id == request.InvoiceId, cancellationToken);
|
||||
|
||||
if (invoice == null) return null;
|
||||
|
||||
var salesOrder = invoice.SalesOrder;
|
||||
|
||||
return new GetPaymentReceiveByIdResponse
|
||||
{
|
||||
InvoiceId = invoice.Id,
|
||||
InvoiceNumber = invoice.AutoNumber,
|
||||
CustomerName = salesOrder?.Customer?.Name,
|
||||
CustomerStreet = salesOrder?.Customer?.Street,
|
||||
CustomerCity = salesOrder?.Customer?.City,
|
||||
SalesOrderBeforeTaxAmount = salesOrder?.BeforeTaxAmount ?? 0,
|
||||
SalesOrderTaxAmount = salesOrder?.TaxAmount ?? 0,
|
||||
SalesOrderAfterTaxAmount = salesOrder?.AfterTaxAmount ?? 0,
|
||||
PaymentAmount = salesOrder?.AfterTaxAmount ?? 0,
|
||||
Items = salesOrder?.SalesOrderItemList.Select(x => new PaymentReceiveItemResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
ProductId = x.ProductId,
|
||||
ProductName = x.Product?.Name,
|
||||
Summary = x.Summary,
|
||||
UnitPrice = x.UnitPrice ?? 0,
|
||||
Quantity = x.Quantity ?? 0,
|
||||
Total = x.Total ?? 0
|
||||
}).ToList() ?? new()
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Sales.PaymentReceive.Cqrs;
|
||||
|
||||
public class PaymentReceiveItemResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? ProductId { get; set; }
|
||||
public string? ProductName { get; set; }
|
||||
public string? Summary { get; set; }
|
||||
public decimal UnitPrice { get; set; }
|
||||
public double Quantity { get; set; }
|
||||
public decimal Total { get; set; }
|
||||
}
|
||||
|
||||
public class GetPaymentReceiveByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? InvoiceId { get; set; }
|
||||
public string? InvoiceNumber { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public DateTime? PaymentDate { get; set; }
|
||||
public string? PaymentMethodId { get; set; }
|
||||
public string? PaymentMethodName { get; set; }
|
||||
public decimal PaymentAmount { get; set; }
|
||||
public Data.Enums.PaymentReceiveStatus Status { get; set; }
|
||||
public string? CustomerName { get; set; }
|
||||
public string? CustomerStreet { get; set; }
|
||||
public string? CustomerCity { get; set; }
|
||||
public string? CompanyName { get; set; }
|
||||
public string? CompanyStreet { get; set; }
|
||||
public string? CompanyCity { get; set; }
|
||||
public string? CurrencySymbol { get; set; }
|
||||
public decimal SalesOrderBeforeTaxAmount { get; set; }
|
||||
public decimal SalesOrderTaxAmount { get; set; }
|
||||
public decimal SalesOrderAfterTaxAmount { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
public List<PaymentReceiveItemResponse> Items { get; set; } = new();
|
||||
}
|
||||
|
||||
public record GetPaymentReceiveByIdQuery(string Id) : IRequest<GetPaymentReceiveByIdResponse?>;
|
||||
|
||||
public class GetPaymentReceiveByIdHandler : IRequestHandler<GetPaymentReceiveByIdQuery, GetPaymentReceiveByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetPaymentReceiveByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetPaymentReceiveByIdResponse?> Handle(GetPaymentReceiveByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var company = await _context.Company
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Currency)
|
||||
.OrderByDescending(x => x.IsDefault)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
var payment = await _context.PaymentReceive
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Invoice)
|
||||
.ThenInclude(i => i!.SalesOrder)
|
||||
.ThenInclude(so => so!.Customer)
|
||||
.Include(x => x.Invoice)
|
||||
.ThenInclude(i => i!.SalesOrder)
|
||||
.ThenInclude(so => so!.SalesOrderItemList)
|
||||
.ThenInclude(si => si.Product)
|
||||
.Include(x => x.PaymentMethod)
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
|
||||
|
||||
if (payment == null) return null;
|
||||
|
||||
var salesOrder = payment.Invoice?.SalesOrder;
|
||||
|
||||
return new GetPaymentReceiveByIdResponse
|
||||
{
|
||||
Id = payment.Id,
|
||||
AutoNumber = payment.AutoNumber,
|
||||
InvoiceId = payment.InvoiceId,
|
||||
InvoiceNumber = payment.Invoice?.AutoNumber,
|
||||
Description = payment.Description,
|
||||
PaymentDate = payment.PaymentDate,
|
||||
PaymentMethodId = payment.PaymentMethodId,
|
||||
PaymentMethodName = payment.PaymentMethod?.Name,
|
||||
PaymentAmount = payment.PaymentAmount ?? 0,
|
||||
Status = payment.Status,
|
||||
CustomerName = salesOrder?.Customer?.Name,
|
||||
CustomerStreet = salesOrder?.Customer?.Street,
|
||||
CustomerCity = salesOrder?.Customer?.City,
|
||||
CompanyName = company?.Name,
|
||||
CompanyStreet = company?.StreetAddress,
|
||||
CompanyCity = company?.City,
|
||||
CurrencySymbol = company?.Currency?.Symbol ?? "IDR",
|
||||
SalesOrderBeforeTaxAmount = salesOrder?.BeforeTaxAmount ?? 0,
|
||||
SalesOrderTaxAmount = salesOrder?.TaxAmount ?? 0,
|
||||
SalesOrderAfterTaxAmount = salesOrder?.AfterTaxAmount ?? 0,
|
||||
CreatedAt = payment.CreatedAt,
|
||||
CreatedBy = payment.CreatedBy,
|
||||
UpdatedAt = payment.UpdatedAt,
|
||||
UpdatedBy = payment.UpdatedBy,
|
||||
Items = salesOrder?.SalesOrderItemList.Select(x => new PaymentReceiveItemResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
ProductId = x.ProductId,
|
||||
ProductName = x.Product?.Name,
|
||||
Summary = x.Summary,
|
||||
UnitPrice = x.UnitPrice ?? 0,
|
||||
Quantity = x.Quantity ?? 0,
|
||||
Total = x.Total ?? 0
|
||||
}).ToList() ?? new()
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Sales.PaymentReceive.Cqrs;
|
||||
|
||||
public class GetPaymentReceiveListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public DateTime? PaymentDate { get; set; }
|
||||
public string? InvoiceNumber { get; set; }
|
||||
public string? CustomerName { get; set; }
|
||||
public decimal PaymentAmount { get; set; }
|
||||
public Data.Enums.PaymentReceiveStatus Status { get; set; }
|
||||
}
|
||||
|
||||
public record GetPaymentReceiveListQuery() : IRequest<List<GetPaymentReceiveListResponse>>;
|
||||
|
||||
public class GetPaymentReceiveListHandler : IRequestHandler<GetPaymentReceiveListQuery, List<GetPaymentReceiveListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetPaymentReceiveListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetPaymentReceiveListResponse>> Handle(GetPaymentReceiveListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.PaymentReceive
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Invoice)
|
||||
.ThenInclude(i => i!.SalesOrder)
|
||||
.ThenInclude(so => so!.Customer)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.Select(x => new GetPaymentReceiveListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
PaymentDate = x.PaymentDate,
|
||||
InvoiceNumber = x.Invoice!.AutoNumber,
|
||||
CustomerName = x.Invoice!.SalesOrder!.Customer!.Name,
|
||||
PaymentAmount = x.PaymentAmount ?? 0,
|
||||
Status = x.Status
|
||||
}).ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Indotalent.Data.Enums;
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
|
||||
namespace Indotalent.Features.Sales.PaymentReceive.Cqrs;
|
||||
|
||||
public class PaymentReceiveLookupResponse
|
||||
{
|
||||
public List<LookupItem> Invoices { get; set; } = new();
|
||||
public List<LookupItem> PaymentMethods { get; set; } = new();
|
||||
public List<LookupStatusItem> Statuses { get; set; } = new();
|
||||
}
|
||||
|
||||
public class LookupItem
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public class LookupStatusItem
|
||||
{
|
||||
public int Value { get; set; }
|
||||
public string? Name { get; set; }
|
||||
}
|
||||
|
||||
public record GetPaymentReceiveLookupQuery() : IRequest<PaymentReceiveLookupResponse>;
|
||||
|
||||
public class GetPaymentReceiveLookupHandler : IRequestHandler<GetPaymentReceiveLookupQuery, PaymentReceiveLookupResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetPaymentReceiveLookupHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<PaymentReceiveLookupResponse> Handle(GetPaymentReceiveLookupQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var response = new PaymentReceiveLookupResponse();
|
||||
|
||||
response.Invoices = await _context.Invoice.AsNoTracking()
|
||||
.Where(x => x.InvoiceStatus == InvoiceStatus.Confirmed)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.Select(x => new LookupItem { Id = x.Id, Name = x.AutoNumber })
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
response.PaymentMethods = await _context.PaymentMethod.AsNoTracking()
|
||||
.OrderBy(x => x.Name)
|
||||
.Select(x => new LookupItem { Id = x.Id, Name = x.Name })
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
response.Statuses = Enum.GetValues(typeof(PaymentReceiveStatus))
|
||||
.Cast<PaymentReceiveStatus>()
|
||||
.Select(x => new LookupStatusItem
|
||||
{
|
||||
Value = (int)x,
|
||||
Name = x.GetDescription()
|
||||
}).ToList();
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Sales.PaymentReceive.Cqrs;
|
||||
|
||||
public class UpdatePaymentReceiveRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? InvoiceId { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public DateTime? PaymentDate { get; set; }
|
||||
public string? PaymentMethodId { get; set; }
|
||||
public decimal? PaymentAmount { get; set; }
|
||||
public Data.Enums.PaymentReceiveStatus Status { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record UpdatePaymentReceiveCommand(UpdatePaymentReceiveRequest Data) : IRequest<bool>;
|
||||
|
||||
public class UpdatePaymentReceiveHandler : IRequestHandler<UpdatePaymentReceiveCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public UpdatePaymentReceiveHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(UpdatePaymentReceiveCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.PaymentReceive
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
entity.InvoiceId = request.Data.InvoiceId;
|
||||
entity.Description = request.Data.Description;
|
||||
entity.PaymentDate = request.Data.PaymentDate;
|
||||
entity.PaymentMethodId = request.Data.PaymentMethodId;
|
||||
entity.PaymentAmount = request.Data.PaymentAmount ?? 0;
|
||||
entity.Status = request.Data.Status;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Indotalent.Features.Sales.PaymentReceive.Cqrs;
|
||||
|
||||
public class UpdatePaymentReceiveValidator : AbstractValidator<UpdatePaymentReceiveRequest>
|
||||
{
|
||||
public UpdatePaymentReceiveValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.InvoiceId).NotEmpty().WithMessage("Invoice reference is required");
|
||||
RuleFor(x => x.PaymentDate).NotEmpty().WithMessage("Payment date is required");
|
||||
RuleFor(x => x.PaymentAmount).GreaterThan(0).WithMessage("Amount must be greater than 0");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user