PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/ITELCA_CLASSLIBRARY/Services/ServicioFeriado.cs

https://gitlab.com/oscarsalazarsevilla/ITELCA
C# | 208 lines | 192 code | 16 blank | 0 comment | 18 complexity | 0e568a9dbe12adf01eb736be480e96cb MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. using System.Data.Entity;
  6. using ITELCA_CLASSLIBRARY.Models;
  7. using ITELCA_CLASSLIBRARY.Repositories;
  8. using ITELCA_CLASSLIBRARY.CustomClasses;
  9. using System.Collections;
  10. using System.Web;
  11. using UmbrellaClassLibrary.CustomClasses;
  12. using System.Data.Entity.Validation;
  13. namespace ITELCA_CLASSLIBRARY.Services
  14. {
  15. public class ServicioFeriado
  16. {
  17. private UnitOfWork unidad;
  18. public ServicioFeriado()
  19. {
  20. this.unidad = new UnitOfWork();
  21. }
  22. public bool Guardar(FERIADOS feriado)
  23. {
  24. try
  25. {
  26. feriado.FECHA_CREACION = DateTime.Now;
  27. unidad.RepositorioFeriado.Add(feriado);
  28. unidad.Save();
  29. return true;
  30. }
  31. catch (DbEntityValidationException e)
  32. {
  33. foreach (var eve in e.EntityValidationErrors)
  34. {
  35. Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
  36. eve.Entry.Entity.GetType().Name, eve.Entry.State);
  37. foreach (var ve in eve.ValidationErrors)
  38. {
  39. Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
  40. ve.PropertyName, ve.ErrorMessage);
  41. }
  42. }
  43. return false;
  44. }
  45. }
  46. public bool Modificar(FERIADOS entity)
  47. {
  48. try
  49. {
  50. unidad.RepositorioFeriado.Modify(entity);
  51. unidad.Save();
  52. return true;
  53. }
  54. catch
  55. {
  56. return false;
  57. }
  58. }
  59. public List<object> Eliminar(decimal id)
  60. {
  61. List<object> lista = new List<object>();
  62. try
  63. {
  64. var aux = unidad.RepositorioFeriado.GetById((int)id);
  65. if (aux != null)
  66. {
  67. lista.Add(new
  68. {
  69. ok = 1,
  70. msg = "Feriado Eliminado"
  71. });
  72. unidad.RepositorioFeriado.Delete(aux);
  73. unidad.Save();
  74. return lista;
  75. }
  76. else
  77. {
  78. lista.Add(new
  79. {
  80. ok = 0,
  81. msg = "Feriado No encontrado"
  82. });
  83. return lista;
  84. }
  85. }
  86. catch
  87. {
  88. lista.Clear();
  89. lista.Add(new
  90. {
  91. ok = 0,
  92. msg = "Error Eliminando Feriado"
  93. });
  94. return lista;
  95. }
  96. }
  97. public object ObtenerDataGrid( string sidx, string sord, int page, int rows, string filters)
  98. {
  99. int totalPages = 0;
  100. int totalRecords = 0;
  101. IEnumerable<FERIADOS> feriados;
  102. feriados = unidad.RepositorioFeriado.GetAll().Where(u => u.ESTADO == 1 || (u.ESTADO == 0 && u.CONDICION != "E"));
  103. feriados = JqGrid<FERIADOS>.GetFilteredContent(sidx, sord, page, rows, filters, feriados.AsQueryable(), ref totalPages, ref totalRecords);
  104. var rowsModel = (
  105. from feriado in feriados.ToList()
  106. select new
  107. {
  108. i = feriado.FERIADO_ID,
  109. cell = new string[] {
  110. feriado.FERIADO_ID.ToString(),
  111. feriado.NOMBRE.ToString(),
  112. feriado.DIA.ToString(),
  113. feriado.MES.ToString(),
  114. (feriado.ANIO.HasValue) ? feriado.ANIO.Value.ToString() : "---",
  115. (feriado.PERIODICO.HasValue) ? (feriado.PERIODICO.Value== 0 ? "": "Anual") : "" ,
  116. (feriado.ESTADO == 1) ? "Activo" : "Inactivo",
  117. "<a title=\"Editar\" href=\"/Feriado/Editar/"+ feriado.FERIADO_ID+"\"><span id=\""+ feriado.FERIADO_ID+"\" class=\"ui-icon ui-icon-pencil\"></a>",
  118. "<span id=\""+ feriado.FERIADO_ID+"\" class=\"ui-icon ui-icon-close\" ></span>" }
  119. }).ToArray();
  120. return JqGrid<FERIADOS>.SetJsonData(totalPages, totalRecords, page, rowsModel);
  121. }
  122. public FERIADOS ObtenerPorClave(int id)
  123. {
  124. try
  125. {
  126. return unidad.RepositorioFeriado.GetById(id);
  127. }
  128. catch
  129. {
  130. return null;
  131. }
  132. }
  133. public IEnumerable<FERIADOS> ObtenerTodos()
  134. {
  135. try
  136. {
  137. IEnumerable<FERIADOS> feriado = unidad.RepositorioFeriado.GetAll();
  138. if (feriado == null)
  139. return Enumerable.Empty<FERIADOS>();
  140. else
  141. return feriado;
  142. }
  143. catch {
  144. return Enumerable.Empty<FERIADOS>();
  145. }
  146. }
  147. public bool Duplicado(decimal FERIADO_ID, string NOMBRE)
  148. {
  149. try
  150. {
  151. if (unidad.RepositorioFeriado.GetAll().Where(u => u.NOMBRE.ToLower() == NOMBRE.ToLower() &&
  152. u.FERIADO_ID != FERIADO_ID).FirstOrDefault() != null)
  153. return true;
  154. else
  155. return false;
  156. }
  157. catch (DbEntityValidationException e)
  158. {
  159. foreach (var eve in e.EntityValidationErrors)
  160. {
  161. Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
  162. eve.Entry.Entity.GetType().Name, eve.Entry.State);
  163. foreach (var ve in eve.ValidationErrors)
  164. {
  165. Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
  166. ve.PropertyName, ve.ErrorMessage);
  167. }
  168. }
  169. return false;
  170. }
  171. }
  172. public bool VerificarFeriado(DateTime fecha){
  173. if (unidad.RepositorioFeriado.GetAll().Where(u => u.DIA == fecha.Day && u.MES == fecha.Month).Count() > 0)
  174. return false;
  175. else
  176. return true;
  177. }
  178. public List<string> ObtenerListaFeriados()
  179. {
  180. List<string> lista = new List<string>();
  181. try
  182. {
  183. IEnumerable<FERIADOS> aux = ObtenerTodos();;
  184. foreach(var ope in aux){
  185. lista.Add(ope.DIA+"/"+ope.MES+"/"+ope.ANIO);
  186. }
  187. return lista;
  188. }
  189. catch { return lista; }
  190. }
  191. }
  192. }