All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (pull_request) Successful in 6m9s
close #64
204 lines
7.9 KiB
C#
204 lines
7.9 KiB
C#
using Domain.Dtos.Sales;
|
|
using Domain.Entities;
|
|
using Domain.Generics;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Models.Helpers;
|
|
using Models.Interfaces;
|
|
using Models.Models;
|
|
|
|
namespace Models.Repositories
|
|
{
|
|
public class PhSSalesDocumentRepository(PhronCareOperationsHubContext context) : IPhSSalesDocumentRepository
|
|
{
|
|
private readonly PhronCareOperationsHubContext _context = context;
|
|
|
|
public async Task<PagedResult<SalesDocumentSummaryDto>> SearchAsync(
|
|
int? customerId,
|
|
string? customerText,
|
|
int? quoteId,
|
|
int? documentType,
|
|
int? status,
|
|
DateTime? issueDateFrom,
|
|
DateTime? issueDateTo,
|
|
int page = 1,
|
|
int pageSize = 50)
|
|
{
|
|
page = page <= 0 ? 1 : page;
|
|
pageSize = pageSize <= 0 ? 50 : pageSize;
|
|
|
|
var query = _context.PhSSalesDocuments
|
|
.Include(x => x.Customer)
|
|
.Include(x => x.BillToCustomer)
|
|
.AsNoTracking()
|
|
.AsQueryable();
|
|
|
|
if (customerId.HasValue && customerId.Value > 0)
|
|
{
|
|
query = query.Where(x => x.CustomerId == customerId.Value || x.BillToCustomerId == customerId.Value);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(customerText))
|
|
{
|
|
var normalizedCustomerText = customerText.Trim();
|
|
query = query.Where(x =>
|
|
(x.Customer.Name ?? string.Empty).Contains(normalizedCustomerText) ||
|
|
(x.BillToCustomer.Name ?? string.Empty).Contains(normalizedCustomerText));
|
|
}
|
|
|
|
if (quoteId.HasValue && quoteId.Value > 0)
|
|
{
|
|
query = query.Where(x => x.QuoteId == quoteId.Value);
|
|
}
|
|
|
|
if (documentType.HasValue && documentType.Value > 0)
|
|
{
|
|
query = query.Where(x => x.DocumentType == documentType.Value);
|
|
}
|
|
|
|
if (status.HasValue && status.Value > 0)
|
|
{
|
|
query = query.Where(x => x.Status == status.Value);
|
|
}
|
|
|
|
if (issueDateFrom.HasValue)
|
|
{
|
|
query = query.Where(x => x.IssueDate >= issueDateFrom.Value);
|
|
}
|
|
|
|
if (issueDateTo.HasValue)
|
|
{
|
|
query = query.Where(x => x.IssueDate <= issueDateTo.Value);
|
|
}
|
|
|
|
var totalItems = await query.CountAsync();
|
|
|
|
var items = await query
|
|
.OrderByDescending(x => x.IssueDate)
|
|
.ThenByDescending(x => x.Id)
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.Select(x => new SalesDocumentSummaryDto
|
|
{
|
|
Id = x.Id,
|
|
InternalDocumentNumber = x.InternalDocumentNumber,
|
|
DocumentType = x.DocumentType,
|
|
Status = x.Status,
|
|
QuoteId = x.QuoteId,
|
|
CustomerId = x.CustomerId,
|
|
CustomerName = x.Customer.Name ?? string.Empty,
|
|
BillToCustomerId = x.BillToCustomerId,
|
|
BillToCustomerName = x.BillToCustomer.Name ?? string.Empty,
|
|
IssueDate = x.IssueDate,
|
|
Currency = x.Currency,
|
|
NetAmount = x.NetAmount,
|
|
TaxAmount = x.TaxAmount,
|
|
TotalAmount = x.TotalAmount,
|
|
PeriodFrom = x.PeriodFrom,
|
|
PeriodTo = x.PeriodTo,
|
|
Createdat = x.Createdat,
|
|
Modifiedat = x.Modifiedat
|
|
})
|
|
.ToListAsync();
|
|
|
|
return new PagedResult<SalesDocumentSummaryDto>
|
|
{
|
|
Items = items,
|
|
TotalItems = totalItems,
|
|
Page = page,
|
|
PageSize = pageSize
|
|
};
|
|
}
|
|
|
|
public async Task<ESalesDocument> CreateAsync(ESalesDocument entity)
|
|
{
|
|
var mapped = EntityMapper.MapEntity<ESalesDocument, PhSSalesDocument>(entity);
|
|
|
|
await _context.PhSSalesDocuments.AddAsync(mapped);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return EntityMapper.MapEntity<PhSSalesDocument, ESalesDocument>(mapped);
|
|
}
|
|
|
|
public async Task<SalesDocumentDto?> GetDtoByIdAsync(int id)
|
|
{
|
|
var entity = await _context.PhSSalesDocuments
|
|
.Include(x => x.Customer)
|
|
.Include(x => x.BillToCustomer)
|
|
.Include(x => x.PhSSalesDocumentDetails)
|
|
.Include(x => x.PhSSalesDocumentCoverages)
|
|
.AsNoTracking()
|
|
.FirstOrDefaultAsync(x => x.Id == id);
|
|
|
|
if (entity == null)
|
|
return null;
|
|
|
|
return new SalesDocumentDto
|
|
{
|
|
Id = entity.Id,
|
|
FormseriesId = entity.FormseriesId,
|
|
InternalSequenceNumber = entity.InternalSequenceNumber,
|
|
InternalDocumentNumber = entity.InternalDocumentNumber,
|
|
DocumentType = entity.DocumentType,
|
|
FiscalVoucherType = entity.FiscalVoucherType,
|
|
FiscalVoucherLetter = entity.FiscalVoucherLetter,
|
|
Status = entity.Status,
|
|
QuoteId = entity.QuoteId,
|
|
CustomerId = entity.CustomerId,
|
|
CustomerName = entity.Customer?.Name ?? string.Empty,
|
|
BillToCustomerId = entity.BillToCustomerId,
|
|
BillToCustomerName = entity.BillToCustomer?.Name ?? string.Empty,
|
|
IssueDate = entity.IssueDate,
|
|
Currency = entity.Currency,
|
|
ExchangeRate = entity.ExchangeRate,
|
|
NetAmount = entity.NetAmount,
|
|
TaxAmount = entity.TaxAmount,
|
|
TotalAmount = entity.TotalAmount,
|
|
Observations = entity.Observations,
|
|
ExtraInfoJson = entity.ExtraInfoJson,
|
|
PeriodFrom = entity.PeriodFrom,
|
|
PeriodTo = entity.PeriodTo,
|
|
Createdat = entity.Createdat,
|
|
Modifiedat = entity.Modifiedat,
|
|
Details = entity.PhSSalesDocumentDetails.Select(x => new SalesDocumentDetailDto
|
|
{
|
|
Id = x.Id,
|
|
SalesDocumentId = x.SalesdocumentId,
|
|
LineNumber = x.LineNumber,
|
|
OriginType = x.OriginType,
|
|
OriginId = x.OriginId,
|
|
QuoteDetailId = x.QuoteDetailId,
|
|
ProductId = x.ProductId,
|
|
Description = x.Description,
|
|
Quantity = x.Quantity,
|
|
AuthorizedUnitPrice = x.AuthorizedUnitPrice,
|
|
AuthorizedAmount = x.AuthorizedAmount,
|
|
BilledPercentage = x.BilledPercentage,
|
|
UnitPrice = x.UnitPrice,
|
|
NetAmount = x.NetAmount,
|
|
TaxAmount = x.TaxAmount,
|
|
TotalAmount = x.TotalAmount,
|
|
OriginSnapshotJson = x.OriginSnapshotJson,
|
|
Createdat = x.Createdat,
|
|
Modifiedat = x.Modifiedat
|
|
}).ToList(),
|
|
Coverage = entity.PhSSalesDocumentCoverages.Select(x => new SalesDocumentCoverageDto
|
|
{
|
|
Id = x.Id,
|
|
SalesDocumentId = x.SalesdocumentId,
|
|
SalesDocumentDetailId = x.SalesdocumentdetailId,
|
|
QuoteId = x.QuoteId,
|
|
QuoteDetailId = x.QuoteDetailId,
|
|
CoverageType = x.CoverageType,
|
|
CoveragePercentage = x.CoveragePercentage,
|
|
CoverageAmount = x.CoverageAmount,
|
|
PeriodFrom = x.PeriodFrom,
|
|
PeriodTo = x.PeriodTo,
|
|
Notes = x.Notes,
|
|
Createdat = x.Createdat,
|
|
Modifiedat = x.Modifiedat
|
|
}).ToList()
|
|
};
|
|
}
|
|
}
|
|
}
|