| 
									
										
										
										
											2025-05-23 15:47:39 -03:00
										 |  |  | using System; | 
					
						
							|  |  |  | using System.Collections.Generic; | 
					
						
							|  |  |  | using System.ComponentModel.DataAnnotations; | 
					
						
							| 
									
										
										
										
											2025-06-03 13:45:20 -03:00
										 |  |  | using System.Linq; | 
					
						
							| 
									
										
										
										
											2025-05-23 15:47:39 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace GestionIntegral.Api.Dtos.Distribucion | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-06-03 13:45:20 -03:00
										 |  |  |     [CustomValidation(typeof(CreateBulkEntradaSalidaCanillaDto), nameof(ValidateNoDuplicatePublicationsInItems))] | 
					
						
							| 
									
										
										
										
											2025-05-23 15:47:39 -03:00
										 |  |  |     public class CreateBulkEntradaSalidaCanillaDto | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         [Required(ErrorMessage = "El ID del canillita es obligatorio.")] | 
					
						
							|  |  |  |         public int IdCanilla { get; set; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         [Required(ErrorMessage = "La fecha del movimiento es obligatoria.")] | 
					
						
							| 
									
										
										
										
											2025-06-03 13:45:20 -03:00
										 |  |  |         public DateTime Fecha { get; set; } | 
					
						
							| 
									
										
										
										
											2025-05-23 15:47:39 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |         [Required(ErrorMessage = "Debe haber al menos un ítem de movimiento.")] | 
					
						
							|  |  |  |         [MinLength(1, ErrorMessage = "Debe agregar al menos una publicación.")] | 
					
						
							| 
									
										
										
										
											2025-06-03 13:45:20 -03:00
										 |  |  |         // La validación de cada item se hará por los atributos en EntradaSalidaCanillaItemDto | 
					
						
							| 
									
										
										
										
											2025-05-23 15:47:39 -03:00
										 |  |  |         public List<EntradaSalidaCanillaItemDto> Items { get; set; } = new List<EntradaSalidaCanillaItemDto>(); | 
					
						
							|  |  |  |          | 
					
						
							| 
									
										
										
										
											2025-06-03 13:45:20 -03:00
										 |  |  |         public static ValidationResult? ValidateNoDuplicatePublicationsInItems(CreateBulkEntradaSalidaCanillaDto instanceToValidate, ValidationContext context) | 
					
						
							| 
									
										
										
										
											2025-05-23 15:47:39 -03:00
										 |  |  |         { | 
					
						
							| 
									
										
										
										
											2025-06-03 13:45:20 -03:00
										 |  |  |             if (instanceToValidate == null)  | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 return new ValidationResult("El objeto principal de la solicitud es nulo."); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (instanceToValidate.Items == null)  | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                  return ValidationResult.Success; // O error si una lista nula de items es inválida | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (instanceToValidate.Items.Any())  | 
					
						
							| 
									
										
										
										
											2025-05-23 15:47:39 -03:00
										 |  |  |             { | 
					
						
							| 
									
										
										
										
											2025-06-03 13:45:20 -03:00
										 |  |  |                 if (instanceToValidate.Items.Any(item => item == null)) | 
					
						
							|  |  |  |                 { | 
					
						
							|  |  |  |                     return new ValidationResult("La lista de ítems contiene entradas nulas.", new[] { nameof(Items) }); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 var duplicateGroups = instanceToValidate.Items | 
					
						
							|  |  |  |                     .Where(item => item.IdPublicacion != 0) // Considerar solo IDs válidos si 0 no lo es | 
					
						
							|  |  |  |                     .GroupBy(item => item.IdPublicacion)  | 
					
						
							| 
									
										
										
										
											2025-05-23 15:47:39 -03:00
										 |  |  |                     .Where(group => group.Count() > 1) | 
					
						
							|  |  |  |                     .ToList(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-03 13:45:20 -03:00
										 |  |  |                 if (duplicateGroups.Any()) | 
					
						
							| 
									
										
										
										
											2025-05-23 15:47:39 -03:00
										 |  |  |                 { | 
					
						
							| 
									
										
										
										
											2025-06-03 13:45:20 -03:00
										 |  |  |                     var duplicatedIds = string.Join(", ", duplicateGroups.Select(g => g.Key)); | 
					
						
							|  |  |  |                     return new ValidationResult( | 
					
						
							|  |  |  |                         $"No puede agregar la misma publicación varias veces. Publicaciones duplicadas IDs: {duplicatedIds}", | 
					
						
							|  |  |  |                         new[] { nameof(Items) }  | 
					
						
							|  |  |  |                     ); | 
					
						
							| 
									
										
										
										
											2025-05-23 15:47:39 -03:00
										 |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return ValidationResult.Success; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |