initial commit
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
|
||||
namespace Indotalent.Features.Sales.Invoice.Cqrs;
|
||||
|
||||
public class CreateInvoiceRequest
|
||||
{
|
||||
public DateTime? InvoiceDate { get; set; }
|
||||
public Data.Enums.InvoiceStatus InvoiceStatus { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? SalesOrderId { get; set; }
|
||||
}
|
||||
|
||||
public class CreateInvoiceResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
}
|
||||
|
||||
public record CreateInvoiceCommand(CreateInvoiceRequest Data) : IRequest<CreateInvoiceResponse>;
|
||||
|
||||
public class CreateInvoiceHandler : IRequestHandler<CreateInvoiceCommand, CreateInvoiceResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public CreateInvoiceHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateInvoiceResponse> Handle(CreateInvoiceCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entityName = nameof(Data.Entities.Invoice);
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"INV/{{Year}}/{{Month}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.Invoice
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
InvoiceDate = request.Data.InvoiceDate,
|
||||
InvoiceStatus = request.Data.InvoiceStatus,
|
||||
Description = request.Data.Description,
|
||||
SalesOrderId = request.Data.SalesOrderId
|
||||
};
|
||||
|
||||
_context.Invoice.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateInvoiceResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
AutoNumber = entity.AutoNumber
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Indotalent.Features.Sales.Invoice.Cqrs;
|
||||
|
||||
public class CreateInvoiceValidator : AbstractValidator<CreateInvoiceRequest>
|
||||
{
|
||||
public CreateInvoiceValidator()
|
||||
{
|
||||
RuleFor(x => x.InvoiceDate).NotEmpty().WithMessage("Date is required");
|
||||
RuleFor(x => x.SalesOrderId).NotEmpty().WithMessage("Sales Order is required");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Sales.Invoice.Cqrs;
|
||||
|
||||
public record DeleteInvoiceByIdCommand(string Id) : IRequest<bool>;
|
||||
|
||||
public class DeleteInvoiceByIdHandler : IRequestHandler<DeleteInvoiceByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public DeleteInvoiceByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteInvoiceByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Invoice
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.Invoice.Remove(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Sales.Invoice.Cqrs;
|
||||
|
||||
public class InvoiceSalesOrderItemResponse
|
||||
{
|
||||
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 GetInvoiceByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public DateTime? InvoiceDate { get; set; }
|
||||
public Data.Enums.InvoiceStatus InvoiceStatus { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? SalesOrderId { get; set; }
|
||||
public string? SalesOrderAutoNumber { 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? CompanyState { get; set; }
|
||||
public string? CompanyZipCode { get; set; }
|
||||
public string? CompanyPhone { get; set; }
|
||||
public string? CurrencySymbol { get; set; }
|
||||
public decimal? BeforeTaxAmount { get; set; }
|
||||
public decimal? TaxAmount { get; set; }
|
||||
public decimal? AfterTaxAmount { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
public List<InvoiceSalesOrderItemResponse> Items { get; set; } = new();
|
||||
}
|
||||
|
||||
public record GetInvoiceByIdQuery(string Id) : IRequest<GetInvoiceByIdResponse?>;
|
||||
|
||||
public class GetInvoiceByIdHandler : IRequestHandler<GetInvoiceByIdQuery, GetInvoiceByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetInvoiceByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetInvoiceByIdResponse?> Handle(GetInvoiceByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var company = await _context.Company
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Currency)
|
||||
.OrderByDescending(x => x.IsDefault)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
return await _context.Invoice
|
||||
.AsNoTracking()
|
||||
.Include(x => x.SalesOrder)
|
||||
.ThenInclude(x => x!.Customer)
|
||||
.Include(x => x.SalesOrder)
|
||||
.ThenInclude(x => x!.SalesOrderItemList)
|
||||
.ThenInclude(x => x.Product)
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetInvoiceByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
InvoiceDate = x.InvoiceDate,
|
||||
InvoiceStatus = x.InvoiceStatus,
|
||||
Description = x.Description,
|
||||
SalesOrderId = x.SalesOrderId,
|
||||
SalesOrderAutoNumber = x.SalesOrder!.AutoNumber,
|
||||
CustomerName = x.SalesOrder!.Customer!.Name,
|
||||
CustomerStreet = x.SalesOrder!.Customer!.Street,
|
||||
CustomerCity = x.SalesOrder!.Customer!.City,
|
||||
CompanyName = company != null ? company.Name : string.Empty,
|
||||
CompanyStreet = company != null ? company.StreetAddress : string.Empty,
|
||||
CompanyCity = company != null ? company.City : string.Empty,
|
||||
CompanyState = company != null ? company.StateProvince : string.Empty,
|
||||
CompanyZipCode = company != null ? company.ZipCode : string.Empty,
|
||||
CompanyPhone = company != null ? company.Phone : string.Empty,
|
||||
CurrencySymbol = company != null && company.Currency != null ? company.Currency.Symbol : "IDR",
|
||||
BeforeTaxAmount = x.SalesOrder!.BeforeTaxAmount,
|
||||
TaxAmount = x.SalesOrder!.TaxAmount,
|
||||
AfterTaxAmount = x.SalesOrder!.AfterTaxAmount,
|
||||
CreatedAt = x.CreatedAt,
|
||||
CreatedBy = x.CreatedBy,
|
||||
UpdatedAt = x.UpdatedAt,
|
||||
UpdatedBy = x.UpdatedBy,
|
||||
Items = x.SalesOrder!.SalesOrderItemList.Select(i => new InvoiceSalesOrderItemResponse
|
||||
{
|
||||
Id = i.Id,
|
||||
ProductId = i.ProductId,
|
||||
ProductName = i.Product!.Name,
|
||||
Summary = i.Summary,
|
||||
UnitPrice = i.UnitPrice,
|
||||
Quantity = i.Quantity,
|
||||
Total = i.Total
|
||||
}).ToList()
|
||||
}).FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Sales.Invoice.Cqrs;
|
||||
|
||||
public class GetInvoiceListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public DateTime? InvoiceDate { get; set; }
|
||||
public string? SalesOrderAutoNumber { get; set; }
|
||||
public string? CustomerName { get; set; }
|
||||
public decimal? AfterTaxAmount { get; set; }
|
||||
public Data.Enums.InvoiceStatus InvoiceStatus { get; set; }
|
||||
}
|
||||
|
||||
public record GetInvoiceListQuery() : IRequest<List<GetInvoiceListResponse>>;
|
||||
|
||||
public class GetInvoiceListHandler : IRequestHandler<GetInvoiceListQuery, List<GetInvoiceListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetInvoiceListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetInvoiceListResponse>> Handle(GetInvoiceListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Invoice
|
||||
.AsNoTracking()
|
||||
.Include(x => x.SalesOrder)
|
||||
.ThenInclude(so => so!.Customer)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.Select(x => new GetInvoiceListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
InvoiceDate = x.InvoiceDate,
|
||||
SalesOrderAutoNumber = x.SalesOrder!.AutoNumber,
|
||||
CustomerName = x.SalesOrder!.Customer!.Name,
|
||||
AfterTaxAmount = x.SalesOrder!.AfterTaxAmount,
|
||||
InvoiceStatus = x.InvoiceStatus
|
||||
}).ToListAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Indotalent.Data.Enums;
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
|
||||
namespace Indotalent.Features.Sales.Invoice.Cqrs;
|
||||
|
||||
public class InvoiceLookupResponse
|
||||
{
|
||||
public List<LookupItem> SalesOrders { 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 GetInvoiceLookupQuery() : IRequest<InvoiceLookupResponse>;
|
||||
|
||||
public class GetInvoiceLookupHandler : IRequestHandler<GetInvoiceLookupQuery, InvoiceLookupResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetInvoiceLookupHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<InvoiceLookupResponse> Handle(GetInvoiceLookupQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var response = new InvoiceLookupResponse();
|
||||
|
||||
response.SalesOrders = await _context.SalesOrder.AsNoTracking()
|
||||
.Where(x => x.OrderStatus == SalesOrderStatus.Confirmed)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.Select(x => new LookupItem { Id = x.Id, Name = x.AutoNumber })
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
response.Statuses = Enum.GetValues(typeof(InvoiceStatus))
|
||||
.Cast<InvoiceStatus>()
|
||||
.Select(x => new LookupStatusItem
|
||||
{
|
||||
Value = (int)x,
|
||||
Name = x.GetDescription()
|
||||
}).ToList();
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Sales.Invoice.Cqrs;
|
||||
|
||||
public record GetSalesOrderDetailForInvoiceQuery(string SalesOrderId) : IRequest<GetInvoiceByIdResponse?>;
|
||||
|
||||
public class GetSalesOrderDetailForInvoiceHandler : IRequestHandler<GetSalesOrderDetailForInvoiceQuery, GetInvoiceByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetSalesOrderDetailForInvoiceHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetInvoiceByIdResponse?> Handle(GetSalesOrderDetailForInvoiceQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.SalesOrder
|
||||
.AsNoTracking()
|
||||
.Include(x => x.SalesOrderItemList)
|
||||
.ThenInclude(x => x.Product)
|
||||
.Where(x => x.Id == request.SalesOrderId)
|
||||
.Select(x => new GetInvoiceByIdResponse
|
||||
{
|
||||
SalesOrderId = x.Id,
|
||||
BeforeTaxAmount = x.BeforeTaxAmount,
|
||||
TaxAmount = x.TaxAmount,
|
||||
AfterTaxAmount = x.AfterTaxAmount,
|
||||
Items = x.SalesOrderItemList.Select(i => new InvoiceSalesOrderItemResponse
|
||||
{
|
||||
Id = i.Id,
|
||||
ProductId = i.ProductId,
|
||||
ProductName = i.Product!.Name,
|
||||
Summary = i.Summary,
|
||||
UnitPrice = i.UnitPrice,
|
||||
Quantity = i.Quantity,
|
||||
Total = i.Total
|
||||
}).ToList()
|
||||
}).FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Sales.Invoice.Cqrs;
|
||||
|
||||
public class UpdateInvoiceRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public DateTime? InvoiceDate { get; set; }
|
||||
public Data.Enums.InvoiceStatus InvoiceStatus { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? SalesOrderId { get; set; }
|
||||
public DateTimeOffset? CreatedAt { get; set; }
|
||||
public string? CreatedBy { get; set; }
|
||||
public DateTimeOffset? UpdatedAt { get; set; }
|
||||
public string? UpdatedBy { get; set; }
|
||||
}
|
||||
|
||||
public record UpdateInvoiceCommand(UpdateInvoiceRequest Data) : IRequest<bool>;
|
||||
|
||||
public class UpdateInvoiceHandler : IRequestHandler<UpdateInvoiceCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public UpdateInvoiceHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(UpdateInvoiceCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.Invoice
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
entity.InvoiceDate = request.Data.InvoiceDate;
|
||||
entity.InvoiceStatus = request.Data.InvoiceStatus;
|
||||
entity.Description = request.Data.Description;
|
||||
entity.SalesOrderId = request.Data.SalesOrderId;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Indotalent.Features.Sales.Invoice.Cqrs;
|
||||
|
||||
public class UpdateInvoiceValidator : AbstractValidator<UpdateInvoiceRequest>
|
||||
{
|
||||
public UpdateInvoiceValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.SalesOrderId).NotEmpty().WithMessage("Sales Order is required");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user