2026-03-19 01:37:43 -03:00
|
|
|
using Core.Interfaces;
|
2026-03-19 17:41:49 -03:00
|
|
|
using Domain.Dtos.Sales;
|
2026-03-23 12:08:57 -03:00
|
|
|
using Domain.Entities;
|
2026-03-20 23:03:46 -03:00
|
|
|
using Domain.Generics;
|
2026-03-19 01:37:43 -03:00
|
|
|
using Models.Interfaces;
|
|
|
|
|
|
|
|
|
|
namespace Core.Services
|
|
|
|
|
{
|
|
|
|
|
public class DeliveryNoteService(IPhSDeliveryNoteRepository deliveryNoteRepository) : IDeliveryNoteDom
|
|
|
|
|
{
|
|
|
|
|
private readonly IPhSDeliveryNoteRepository _deliveryNoteRepository = deliveryNoteRepository;
|
|
|
|
|
|
2026-03-20 23:03:46 -03:00
|
|
|
public Task<PagedResult<DeliveryNoteSummaryDto>> SearchAsync(
|
|
|
|
|
int? customerId,
|
|
|
|
|
string? customerText,
|
|
|
|
|
string? deliveryNoteNumber,
|
|
|
|
|
int? quoteId,
|
|
|
|
|
string? quoteNumber,
|
|
|
|
|
DateTime? issueDateFrom,
|
|
|
|
|
DateTime? issueDateTo,
|
|
|
|
|
string? status,
|
|
|
|
|
int page = 1,
|
|
|
|
|
int pageSize = 50)
|
|
|
|
|
{
|
|
|
|
|
page = page <= 0 ? 1 : page;
|
|
|
|
|
pageSize = pageSize <= 0 ? 50 : pageSize;
|
|
|
|
|
|
|
|
|
|
return _deliveryNoteRepository.SearchAsync(
|
|
|
|
|
customerId,
|
|
|
|
|
string.IsNullOrWhiteSpace(customerText) ? null : customerText.Trim(),
|
|
|
|
|
string.IsNullOrWhiteSpace(deliveryNoteNumber) ? null : deliveryNoteNumber.Trim(),
|
|
|
|
|
quoteId,
|
|
|
|
|
string.IsNullOrWhiteSpace(quoteNumber) ? null : quoteNumber.Trim(),
|
|
|
|
|
issueDateFrom,
|
|
|
|
|
issueDateTo,
|
|
|
|
|
string.IsNullOrWhiteSpace(status) ? null : status.Trim(),
|
|
|
|
|
page,
|
|
|
|
|
pageSize);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-19 17:41:49 -03:00
|
|
|
public Task<DeliveryNoteDto?> GetDtoByIdAsync(int id)
|
2026-03-19 01:37:43 -03:00
|
|
|
{
|
|
|
|
|
if (id <= 0)
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(id), "El identificador del remito es inválido.");
|
|
|
|
|
|
2026-03-19 17:41:49 -03:00
|
|
|
return _deliveryNoteRepository.GetDtoByIdAsync(id);
|
2026-03-19 01:37:43 -03:00
|
|
|
}
|
|
|
|
|
|
2026-03-19 17:41:49 -03:00
|
|
|
public Task<DeliveryNoteDto?> GetDtoByDeliveryNoteNumberAsync(string deliveryNoteNumber)
|
2026-03-19 01:37:43 -03:00
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(deliveryNoteNumber))
|
|
|
|
|
throw new ArgumentException("El número de remito es obligatorio.", nameof(deliveryNoteNumber));
|
|
|
|
|
|
2026-03-19 17:41:49 -03:00
|
|
|
return _deliveryNoteRepository.GetDtoByDeliveryNoteNumberAsync(deliveryNoteNumber.Trim());
|
2026-03-19 01:37:43 -03:00
|
|
|
}
|
|
|
|
|
|
2026-03-19 17:41:49 -03:00
|
|
|
public Task<IEnumerable<DeliveryNoteDto>> GetDtosByQuoteIdAsync(int quoteId)
|
2026-03-19 01:37:43 -03:00
|
|
|
{
|
|
|
|
|
if (quoteId <= 0)
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(quoteId), "El identificador del presupuesto es inválido.");
|
|
|
|
|
|
2026-03-19 17:41:49 -03:00
|
|
|
return _deliveryNoteRepository.GetDtosByQuoteIdAsync(quoteId);
|
2026-03-19 01:37:43 -03:00
|
|
|
}
|
2026-03-23 12:08:57 -03:00
|
|
|
|
|
|
|
|
public Task<DeliveryNoteCreateResponse> CreateAndIssueDeliveryNoteAsync(DeliveryNoteCreateRequest request)
|
|
|
|
|
{
|
|
|
|
|
ArgumentNullException.ThrowIfNull(request);
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(request.DeliveryNoteNumber))
|
|
|
|
|
throw new ArgumentException("El número de remito es obligatorio.", nameof(request.DeliveryNoteNumber));
|
|
|
|
|
|
|
|
|
|
if (request.CustomerId <= 0)
|
|
|
|
|
throw new ArgumentException("Debe seleccionar un cliente.", nameof(request.CustomerId));
|
|
|
|
|
|
|
|
|
|
if (request.IssueDate == default)
|
|
|
|
|
throw new ArgumentException("La fecha de emisión es obligatoria.", nameof(request.IssueDate));
|
|
|
|
|
|
|
|
|
|
if (request.Items is null || request.Items.Count == 0)
|
|
|
|
|
throw new InvalidOperationException("Debe incluir al menos un ítem.");
|
|
|
|
|
|
|
|
|
|
if (request.Items.Any(i => i.Quantity <= 0))
|
|
|
|
|
throw new InvalidOperationException("Todas las cantidades deben ser mayores a cero.");
|
|
|
|
|
|
|
|
|
|
if (request.Items.Any(i => string.IsNullOrWhiteSpace(i.Description)))
|
|
|
|
|
throw new InvalidOperationException("Todos los ítems deben incluir descripción.");
|
|
|
|
|
|
|
|
|
|
var entity = new EDeliveryNote
|
|
|
|
|
{
|
2026-03-23 18:14:00 -03:00
|
|
|
Deliverynotenumber = request.DeliveryNoteNumber.Trim(),
|
2026-03-23 12:08:57 -03:00
|
|
|
QuoteId = request.QuoteId,
|
2026-03-23 18:14:00 -03:00
|
|
|
Issuedate = request.IssueDate,
|
2026-03-23 12:08:57 -03:00
|
|
|
CustomerId = request.CustomerId,
|
|
|
|
|
Observations = request.Observations,
|
2026-03-23 18:14:00 -03:00
|
|
|
ExtrainfoJson = request.ExtraInfoJson
|
2026-03-23 12:08:57 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(new DeliveryNoteCreateResponse
|
|
|
|
|
{
|
|
|
|
|
Id = entity.Id,
|
2026-03-23 18:14:00 -03:00
|
|
|
DeliveryNoteNumber = entity.Deliverynotenumber
|
2026-03-23 12:08:57 -03:00
|
|
|
});
|
|
|
|
|
}
|
2026-03-19 01:37:43 -03:00
|
|
|
}
|
|
|
|
|
}
|