initial commit
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using Indotalent.ConfigBackEnd.Extensions;
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
|
||||
namespace Indotalent.Features.Purchase.DebitNote.Cqrs;
|
||||
|
||||
public class CreateDebitNoteRequest
|
||||
{
|
||||
public DateTime? DebitNoteDate { get; set; }
|
||||
public Data.Enums.DebitNoteStatus DebitNoteStatus { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? PurchaseReturnId { get; set; }
|
||||
}
|
||||
|
||||
public class CreateDebitNoteResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
}
|
||||
|
||||
public record CreateDebitNoteCommand(CreateDebitNoteRequest Data) : IRequest<CreateDebitNoteResponse>;
|
||||
|
||||
public class CreateDebitNoteHandler : IRequestHandler<CreateDebitNoteCommand, CreateDebitNoteResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public CreateDebitNoteHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<CreateDebitNoteResponse> Handle(CreateDebitNoteCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entityName = nameof(Data.Entities.DebitNote);
|
||||
var autoNo = await _context.GenerateAutoNumberAsync(
|
||||
entityName: entityName,
|
||||
prefixTemplate: $"DN/{{Year}}/{{Month}}/",
|
||||
ct: cancellationToken
|
||||
);
|
||||
|
||||
var entity = new Data.Entities.DebitNote
|
||||
{
|
||||
AutoNumber = autoNo,
|
||||
DebitNoteDate = request.Data.DebitNoteDate,
|
||||
DebitNoteStatus = request.Data.DebitNoteStatus,
|
||||
Description = request.Data.Description,
|
||||
PurchaseReturnId = request.Data.PurchaseReturnId
|
||||
};
|
||||
|
||||
_context.DebitNote.Add(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new CreateDebitNoteResponse
|
||||
{
|
||||
Id = entity.Id,
|
||||
AutoNumber = entity.AutoNumber
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Indotalent.Features.Purchase.DebitNote.Cqrs;
|
||||
|
||||
public class CreateDebitNoteValidator : AbstractValidator<CreateDebitNoteRequest>
|
||||
{
|
||||
public CreateDebitNoteValidator()
|
||||
{
|
||||
RuleFor(x => x.DebitNoteDate).NotEmpty().WithMessage("Date is required");
|
||||
RuleFor(x => x.PurchaseReturnId).NotEmpty().WithMessage("Purchase Return reference is required");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Purchase.DebitNote.Cqrs;
|
||||
|
||||
public record DeleteDebitNoteByIdCommand(string Id) : IRequest<bool>;
|
||||
|
||||
public class DeleteDebitNoteByIdHandler : IRequestHandler<DeleteDebitNoteByIdCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public DeleteDebitNoteByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(DeleteDebitNoteByIdCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.DebitNote
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
_context.DebitNote.Remove(entity);
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Purchase.DebitNote.Cqrs;
|
||||
|
||||
public class DebitNoteItemResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? ProductId { get; set; }
|
||||
public string? ProductName { get; set; }
|
||||
public decimal UnitPrice { get; set; }
|
||||
public double Quantity { get; set; }
|
||||
public decimal Total { get; set; }
|
||||
}
|
||||
|
||||
public class GetDebitNoteByIdResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public DateTime? DebitNoteDate { get; set; }
|
||||
public Data.Enums.DebitNoteStatus DebitNoteStatus { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? PurchaseReturnId { get; set; }
|
||||
public string? PurchaseReturnNumber { get; set; }
|
||||
public string? VendorName { get; set; }
|
||||
public string? VendorStreet { get; set; }
|
||||
public string? VendorCity { get; set; }
|
||||
public string? CompanyName { get; set; }
|
||||
public string? CompanyStreet { get; set; }
|
||||
public string? CompanyCity { 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<DebitNoteItemResponse> Items { get; set; } = new();
|
||||
}
|
||||
|
||||
public record GetDebitNoteByIdQuery(string Id) : IRequest<GetDebitNoteByIdResponse?>;
|
||||
|
||||
public class GetDebitNoteByIdHandler : IRequestHandler<GetDebitNoteByIdQuery, GetDebitNoteByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetDebitNoteByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetDebitNoteByIdResponse?> Handle(GetDebitNoteByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var company = await _context.Company
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Currency)
|
||||
.OrderByDescending(x => x.IsDefault)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
|
||||
var debitNote = await _context.DebitNote
|
||||
.AsNoTracking()
|
||||
.Include(x => x.PurchaseReturn)
|
||||
.ThenInclude(pr => pr!.GoodsReceive)
|
||||
.ThenInclude(gr => gr!.PurchaseOrder)
|
||||
.ThenInclude(po => po!.Vendor)
|
||||
.Include(x => x.PurchaseReturn)
|
||||
.ThenInclude(pr => pr!.GoodsReceive)
|
||||
.ThenInclude(gr => gr!.PurchaseOrder)
|
||||
.ThenInclude(po => po!.Tax)
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
|
||||
|
||||
if (debitNote == null) return null;
|
||||
|
||||
var purchaseOrder = debitNote.PurchaseReturn?.GoodsReceive?.PurchaseOrder;
|
||||
var taxRate = purchaseOrder?.Tax?.PercentageValue ?? 0;
|
||||
|
||||
var transactions = await _context.InventoryTransaction
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Product)
|
||||
.Where(x => x.ModuleId == debitNote.PurchaseReturnId && x.ModuleName == nameof(Data.Entities.PurchaseReturn))
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
var items = new List<DebitNoteItemResponse>();
|
||||
decimal subTotal = 0;
|
||||
|
||||
foreach (var trans in transactions)
|
||||
{
|
||||
var poItem = await _context.PurchaseOrderItem
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(x => x.PurchaseOrderId == purchaseOrder!.Id && x.ProductId == trans.ProductId, cancellationToken);
|
||||
|
||||
var unitPrice = poItem?.UnitPrice ?? 0;
|
||||
var qty = Math.Abs(trans.Movement ?? 0);
|
||||
var total = unitPrice * (decimal)qty;
|
||||
|
||||
items.Add(new DebitNoteItemResponse
|
||||
{
|
||||
Id = trans.Id,
|
||||
ProductId = trans.ProductId,
|
||||
ProductName = trans.Product?.Name,
|
||||
UnitPrice = unitPrice,
|
||||
Quantity = qty,
|
||||
Total = total
|
||||
});
|
||||
|
||||
subTotal += total;
|
||||
}
|
||||
|
||||
var taxAmount = subTotal * (decimal)(taxRate / 100);
|
||||
|
||||
return new GetDebitNoteByIdResponse
|
||||
{
|
||||
Id = debitNote.Id,
|
||||
AutoNumber = debitNote.AutoNumber,
|
||||
DebitNoteDate = debitNote.DebitNoteDate,
|
||||
DebitNoteStatus = debitNote.DebitNoteStatus,
|
||||
Description = debitNote.Description,
|
||||
PurchaseReturnId = debitNote.PurchaseReturnId,
|
||||
PurchaseReturnNumber = debitNote.PurchaseReturn?.AutoNumber,
|
||||
VendorName = purchaseOrder?.Vendor?.Name,
|
||||
VendorStreet = purchaseOrder?.Vendor?.Street,
|
||||
VendorCity = purchaseOrder?.Vendor?.City,
|
||||
CompanyName = company?.Name,
|
||||
CompanyStreet = company?.StreetAddress,
|
||||
CompanyCity = company?.City,
|
||||
CurrencySymbol = company?.Currency?.Symbol ?? "IDR",
|
||||
BeforeTaxAmount = subTotal,
|
||||
TaxAmount = taxAmount,
|
||||
AfterTaxAmount = subTotal + taxAmount,
|
||||
CreatedAt = debitNote.CreatedAt,
|
||||
CreatedBy = debitNote.CreatedBy,
|
||||
UpdatedAt = debitNote.UpdatedAt,
|
||||
UpdatedBy = debitNote.UpdatedBy,
|
||||
Items = items
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Purchase.DebitNote.Cqrs;
|
||||
|
||||
public class GetDebitNoteListResponse
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public DateTime? DebitNoteDate { get; set; }
|
||||
public string? PurchaseReturnNumber { get; set; }
|
||||
public string? VendorName { get; set; }
|
||||
public Data.Enums.DebitNoteStatus DebitNoteStatus { get; set; }
|
||||
}
|
||||
|
||||
public record GetDebitNoteListQuery() : IRequest<List<GetDebitNoteListResponse>>;
|
||||
|
||||
public class GetDebitNoteListHandler : IRequestHandler<GetDebitNoteListQuery, List<GetDebitNoteListResponse>>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetDebitNoteListHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<List<GetDebitNoteListResponse>> Handle(GetDebitNoteListQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.DebitNote
|
||||
.AsNoTracking()
|
||||
.Include(x => x.PurchaseReturn)
|
||||
.ThenInclude(pr => pr!.GoodsReceive)
|
||||
.ThenInclude(gr => gr!.PurchaseOrder)
|
||||
.ThenInclude(po => po!.Vendor)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.Select(x => new GetDebitNoteListResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
AutoNumber = x.AutoNumber,
|
||||
DebitNoteDate = x.DebitNoteDate,
|
||||
PurchaseReturnNumber = x.PurchaseReturn!.AutoNumber,
|
||||
VendorName = x.PurchaseReturn!.GoodsReceive!.PurchaseOrder!.Vendor!.Name,
|
||||
DebitNoteStatus = x.DebitNoteStatus
|
||||
}).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.Purchase.DebitNote.Cqrs;
|
||||
|
||||
public class DebitNoteLookupResponse
|
||||
{
|
||||
public List<LookupItem> PurchaseReturns { 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 GetDebitNoteLookupQuery() : IRequest<DebitNoteLookupResponse>;
|
||||
|
||||
public class GetDebitNoteLookupHandler : IRequestHandler<GetDebitNoteLookupQuery, DebitNoteLookupResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetDebitNoteLookupHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<DebitNoteLookupResponse> Handle(GetDebitNoteLookupQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var response = new DebitNoteLookupResponse();
|
||||
|
||||
response.PurchaseReturns = await _context.PurchaseReturn.AsNoTracking()
|
||||
.Where(x => x.Status == PurchaseReturnStatus.Confirmed)
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.Select(x => new LookupItem { Id = x.Id, Name = x.AutoNumber })
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
response.Statuses = Enum.GetValues(typeof(DebitNoteStatus))
|
||||
.Cast<DebitNoteStatus>()
|
||||
.Select(x => new LookupStatusItem
|
||||
{
|
||||
Value = (int)x,
|
||||
Name = x.GetDescription()
|
||||
}).ToList();
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Purchase.DebitNote.Cqrs;
|
||||
|
||||
public record GetPurchaseReturnDetailForDebitNoteQuery(string PurchaseReturnId) : IRequest<GetDebitNoteByIdResponse?>;
|
||||
|
||||
public class GetPurchaseReturnDetailForDebitNoteHandler : IRequestHandler<GetPurchaseReturnDetailForDebitNoteQuery, GetDebitNoteByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public GetPurchaseReturnDetailForDebitNoteHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetDebitNoteByIdResponse?> Handle(GetPurchaseReturnDetailForDebitNoteQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var purchaseReturn = await _context.PurchaseReturn
|
||||
.AsNoTracking()
|
||||
.Include(x => x.GoodsReceive)
|
||||
.ThenInclude(gr => gr!.PurchaseOrder)
|
||||
.ThenInclude(po => po!.Tax)
|
||||
.FirstOrDefaultAsync(x => x.Id == request.PurchaseReturnId, cancellationToken);
|
||||
|
||||
if (purchaseReturn == null) return null;
|
||||
|
||||
var purchaseOrder = purchaseReturn.GoodsReceive?.PurchaseOrder;
|
||||
var taxRate = purchaseOrder?.Tax?.PercentageValue ?? 0;
|
||||
|
||||
var transactions = await _context.InventoryTransaction
|
||||
.AsNoTracking()
|
||||
.Include(x => x.Product)
|
||||
.Where(x => x.ModuleId == request.PurchaseReturnId && x.ModuleName == nameof(Data.Entities.PurchaseReturn))
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
var items = new List<DebitNoteItemResponse>();
|
||||
decimal subTotal = 0;
|
||||
|
||||
foreach (var trans in transactions)
|
||||
{
|
||||
var poItem = await _context.PurchaseOrderItem
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(x => x.PurchaseOrderId == purchaseOrder!.Id && x.ProductId == trans.ProductId, cancellationToken);
|
||||
|
||||
var unitPrice = poItem?.UnitPrice ?? 0;
|
||||
var qty = Math.Abs(trans.Movement ?? 0);
|
||||
var total = unitPrice * (decimal)qty;
|
||||
|
||||
items.Add(new DebitNoteItemResponse
|
||||
{
|
||||
Id = trans.Id,
|
||||
ProductId = trans.ProductId,
|
||||
ProductName = trans.Product?.Name,
|
||||
UnitPrice = unitPrice,
|
||||
Quantity = qty,
|
||||
Total = total
|
||||
});
|
||||
|
||||
subTotal += total;
|
||||
}
|
||||
|
||||
var taxAmount = subTotal * (decimal)(taxRate / 100);
|
||||
|
||||
return new GetDebitNoteByIdResponse
|
||||
{
|
||||
PurchaseReturnId = purchaseReturn.Id,
|
||||
PurchaseReturnNumber = purchaseReturn.AutoNumber,
|
||||
BeforeTaxAmount = subTotal,
|
||||
TaxAmount = taxAmount,
|
||||
AfterTaxAmount = subTotal + taxAmount,
|
||||
Items = items
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Purchase.DebitNote.Cqrs;
|
||||
|
||||
public class UpdateDebitNoteRequest
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public DateTime? DebitNoteDate { get; set; }
|
||||
public Data.Enums.DebitNoteStatus DebitNoteStatus { get; set; }
|
||||
public string? AutoNumber { get; set; }
|
||||
public string? Description { get; set; }
|
||||
public string? PurchaseReturnId { 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 record UpdateDebitNoteCommand(UpdateDebitNoteRequest Data) : IRequest<bool>;
|
||||
|
||||
public class UpdateDebitNoteHandler : IRequestHandler<UpdateDebitNoteCommand, bool>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
public UpdateDebitNoteHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<bool> Handle(UpdateDebitNoteCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _context.DebitNote
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (entity == null) return false;
|
||||
|
||||
entity.DebitNoteDate = request.Data.DebitNoteDate;
|
||||
entity.DebitNoteStatus = request.Data.DebitNoteStatus;
|
||||
entity.Description = request.Data.Description;
|
||||
entity.PurchaseReturnId = request.Data.PurchaseReturnId;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Indotalent.Features.Purchase.DebitNote.Cqrs;
|
||||
|
||||
public class UpdateDebitNoteValidator : AbstractValidator<UpdateDebitNoteRequest>
|
||||
{
|
||||
public UpdateDebitNoteValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
RuleFor(x => x.PurchaseReturnId).NotEmpty().WithMessage("Purchase Return reference is required");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user