/ITELCA_CLASSLIBRARY/Services/ServicioPrecioProducto.cs

https://gitlab.com/oscarsalazarsevilla/ITELCA · C# · 215 lines · 198 code · 15 blank · 2 comment · 18 complexity · 4c2ea167a02907c77807c5577e718cce 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. namespace ITELCA_CLASSLIBRARY.Services
  13. {
  14. public class ServicioPrecioProducto
  15. {
  16. private UnitOfWork unidad;
  17. public ServicioPrecioProducto()
  18. {
  19. this.unidad = new UnitOfWork();
  20. }
  21. public bool Guardar(PRECIOSPRODUCTOS precioProducto)
  22. {
  23. try
  24. {
  25. precioProducto.CONDICION = "C";
  26. unidad.RepositorioPreciosProductos.Add(precioProducto);
  27. unidad.Save();
  28. return true;
  29. }
  30. catch
  31. {
  32. return false;
  33. }
  34. }
  35. public bool Modificar(PRECIOSPRODUCTOS precioProducto)
  36. {
  37. try
  38. {
  39. precioProducto.CONDICION = "A";
  40. unidad.RepositorioPreciosProductos.Modify(precioProducto);
  41. unidad.Save();
  42. return true;
  43. }
  44. catch
  45. {
  46. return false;
  47. }
  48. }
  49. public List<object> Eliminar(decimal id)
  50. {
  51. List<object> lista = new List<object>();
  52. try
  53. {
  54. var aux = unidad.RepositorioPreciosProductos.GetById((int)id);
  55. if (aux != null)
  56. {
  57. lista.Add(new
  58. {
  59. ok = 1,
  60. msg = "Precio del producto eliminado"
  61. });
  62. aux.CONDICION = "E";
  63. aux.ESTADO = 0;
  64. unidad.RepositorioPreciosProductos.Modify(aux);
  65. //unidad.RepositorioPreciosProductos.Delete(aux);
  66. unidad.Save();
  67. return lista;
  68. }
  69. else
  70. {
  71. lista.Add(new
  72. {
  73. ok = 0,
  74. msg = "Precio del producto no encontrado"
  75. });
  76. return lista;
  77. }
  78. }
  79. catch
  80. {
  81. lista.Add(new
  82. {
  83. ok = 0,
  84. msg = "Error eliminando precio del producto"
  85. });
  86. return lista;
  87. }
  88. }
  89. public List<object> Eliminar(string setid, string inv_item_id, DateTime fecha_efectiva)
  90. {
  91. List<object> lista = new List<object>();
  92. try
  93. {
  94. var aux = ObtenerPorClave(setid, inv_item_id, fecha_efectiva);
  95. if (aux != null)
  96. {
  97. lista.Add(new
  98. {
  99. ok = 1,
  100. msg = "Precio del producto eliminado"
  101. });
  102. aux.CONDICION = "E";
  103. aux.ESTADO = 0;
  104. unidad.RepositorioPreciosProductos.Modify(aux);
  105. //unidad.RepositorioPreciosProductos.Delete(aux);
  106. unidad.Save();
  107. return lista;
  108. }
  109. else
  110. {
  111. lista.Add(new
  112. {
  113. ok = 0,
  114. msg = "Precio del producto no encontrado"
  115. });
  116. return lista;
  117. }
  118. }
  119. catch
  120. {
  121. lista.Add(new
  122. {
  123. ok = 0,
  124. msg = "Error eliminando precio del producto"
  125. });
  126. return lista;
  127. }
  128. }
  129. public object ObtenerDataGrid(string sidx, string sord, int page, int rows, string filters)
  130. {
  131. int totalPages = 0;
  132. int totalRecords = 0;
  133. IEnumerable<PRECIOSPRODUCTOS> preciosProductos;
  134. preciosProductos = unidad.RepositorioPreciosProductos.GetAll().Where(u => u.ESTADO == 1 || (u.ESTADO == 0 && u.CONDICION != "E"));
  135. preciosProductos = JqGrid<PRECIOSPRODUCTOS>.GetFilteredContent(sidx, sord, page, rows, filters, preciosProductos.AsQueryable(), ref totalPages, ref totalRecords);
  136. var rowsModel = (
  137. from precioProducto in preciosProductos.ToList()
  138. select new
  139. {
  140. i = precioProducto.INV_ITEM_ID,
  141. cell = new string[] {
  142. precioProducto.INV_ITEM_ID,
  143. precioProducto.PRODUCTOS.DESCR,
  144. String.Format("{0:N2}", precioProducto.MONTO),
  145. precioProducto.FECHA_EFECTIVA.ToString("dd/MM/yyyy"),
  146. (precioProducto.ESTADO == 1) ? "Activo" : "Inactivo",
  147. "<a title=\"Editar\" href=\"/PreciosProductos/Editar?setid="+ precioProducto.SETID +"&inv_item_id="+precioProducto.INV_ITEM_ID+"&fecha_efectiva="+precioProducto.FECHA_EFECTIVA.ToString("dd/MM/yyyy")+"\"><span id=\""+precioProducto.INV_ITEM_ID+"\" class=\"ui-icon ui-icon-pencil\"></a>",
  148. "<span id=\""+precioProducto.SETID +"|"+precioProducto.INV_ITEM_ID+"|"+precioProducto.FECHA_EFECTIVA.ToString("dd/MM/yyyy")+"\" class=\"ui-icon ui-icon-close\" ></span>" }
  149. }).ToArray();
  150. return JqGrid<PRECIOSPRODUCTOS>.SetJsonData(totalPages, totalRecords, page, rowsModel);
  151. }
  152. public PRECIOSPRODUCTOS ObtenerPorClave(int id)
  153. {
  154. try
  155. {
  156. return unidad.RepositorioPreciosProductos.GetById(id);
  157. }
  158. catch
  159. {
  160. return null;
  161. }
  162. }
  163. public PRECIOSPRODUCTOS ObtenerPorClave(string setid, string inv_item_id, DateTime fecha_efectiva)
  164. {
  165. try
  166. {
  167. return ObtenerTodos().Where(p=>p.SETID == setid &&
  168. p.INV_ITEM_ID == inv_item_id &&
  169. p.FECHA_EFECTIVA == fecha_efectiva).FirstOrDefault();
  170. }
  171. catch
  172. {
  173. return null;
  174. }
  175. }
  176. public IEnumerable<PRECIOSPRODUCTOS> ObtenerTodos()
  177. {
  178. try
  179. {
  180. IEnumerable<PRECIOSPRODUCTOS> sedes= unidad.RepositorioPreciosProductos.GetAll().Where(u=>u.ESTADO==1);
  181. if (sedes == null)
  182. return Enumerable.Empty<PRECIOSPRODUCTOS>();
  183. else
  184. return sedes;
  185. }
  186. catch {
  187. return Enumerable.Empty<PRECIOSPRODUCTOS>();
  188. }
  189. }
  190. public decimal ObtenerPrecioProducto(string inv_item_id)
  191. {
  192. PRECIOSPRODUCTOS p = ObtenerTodos().Where(pp => pp.INV_ITEM_ID == inv_item_id &&
  193. pp.FECHA_EFECTIVA <= DateTime.Now).OrderByDescending(pp => pp.FECHA_EFECTIVA).FirstOrDefault();
  194. if (p == null)
  195. return 0;
  196. else
  197. return p.MONTO;
  198. }
  199. }
  200. }