Advertisement
Guest User

AnimalController

a guest
Dec 18th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Data;
  5. using System.Data.Entity;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Web;
  9. using System.Web.Mvc;
  10. using ProyectoV1.Models;
  11. using System.Web.Helpers;
  12. using PagedList;
  13. using System.Drawing.Imaging;
  14. using System.IO;
  15. using ZXing;
  16. using ZXing.Common;
  17. using System.Windows.Media.Imaging;
  18. using Rotativa;
  19. using System.Web.UI;
  20. using System.Web.Security;
  21. using System.ComponentModel.DataAnnotations;
  22. namespace ProyectoV1.Controllers
  23. {
  24.     public class AnimalController : Controller
  25.     {
  26.         private bdagricolaEntities db = new bdagricolaEntities();
  27.         public ActionResult Error()
  28.         {
  29.             return View();
  30.         }
  31.         public ActionResult CodigoQR(int? id)
  32.         {
  33.             animal animal = db.animal.Find(id);
  34.             return View(animal);
  35.         }
  36.         public ActionResult CodigoQRAnimales()
  37.         {
  38.             var animal = db.animal.Include(a => a.estado);
  39.             return View(animal);
  40.         }
  41.         public ActionResult ExportarQR()
  42.         {
  43.             return new ActionAsPdf("CodigoQR", db.animal);
  44.  
  45.         }
  46.         public JsonResult codigoValido(string codigo_sag)
  47.         {
  48.             return Json(!db.animal.Any(a => a.codigo_sag == codigo_sag), JsonRequestBehavior.AllowGet);
  49.         }
  50.         public JsonResult fechaValida(DateTime fec_nac)
  51.         {
  52.             DateTime fechaActual = DateTime.Today;
  53.             bool respuesta = false;
  54.             if(fec_nac > fechaActual)
  55.             {
  56.                 respuesta = false;
  57.             }
  58.             else
  59.             {
  60.                 respuesta = true;
  61.             }
  62.             return Json(respuesta, JsonRequestBehavior.AllowGet);
  63.         }
  64.         // GET: Animal
  65.         public ActionResult Index(int? page)
  66.         {
  67.             var animal = db.animal.Include(a => a.estado).Include(a => a.raza).Include(a => a.tipo).Where(a => a.estado.nombre != "VENDIDO" && a.estado.nombre != "MUERTO");
  68.            
  69.             return View(animal.ToList().ToPagedList(page ?? 1, 5));
  70.         }
  71.         [HttpPost]
  72.         public ActionResult Index(int? page, string nombre)
  73.         {
  74.             using (bdagricolaEntities dc = new bdagricolaEntities())
  75.             {
  76.                 var animales = db.animal.Include(a => a.estado).Include(a => a.raza).Include(a => a.tipo).Where(a => a.estado.nombre != "VENDIDO" && a.estado.nombre != "MUERTO");
  77.                 if (!string.IsNullOrEmpty(nombre))
  78.                 {
  79.                     animales = db.animal.Where(m => m.codigo_sag.Contains(nombre)).Where(a => a.estado.nombre != "VENDIDO" && a.estado.nombre != "MUERTO");
  80.  
  81.                 }
  82.                 return View(animales.ToList().ToPagedList(page ?? 1, 5));
  83.                 //var medicamentos = db.medicamento.Where(m => m.nombre.Equals(nombre) );
  84.                 //return View(medicamentos.ToList().ToPagedList(page ?? 1, 5));
  85.             }
  86.  
  87.         }
  88.         public ActionResult Historicos(int? page)
  89.         {
  90.             var animal = db.animal.Include(a => a.estado).Include(a => a.raza).Include(a => a.tipo);
  91.  
  92.             return View(animal.ToList().ToPagedList(page ?? 1, 5));
  93.         }
  94.         [HttpPost]
  95.         public ActionResult Historicos(int? page, string nombre)
  96.         {
  97.             using (bdagricolaEntities dc = new bdagricolaEntities())
  98.             {
  99.                 var animales = from s in db.animal select s;
  100.                 if (!string.IsNullOrEmpty(nombre))
  101.                 {
  102.                     animales = db.animal.Where(m => m.codigo_sag.Contains(nombre));
  103.  
  104.                 }
  105.                 return View(animales.ToList().ToPagedList(page ?? 1, 5));
  106.                 //var medicamentos = db.medicamento.Where(m => m.nombre.Equals(nombre) );
  107.                 //return View(medicamentos.ToList().ToPagedList(page ?? 1, 5));
  108.             }
  109.  
  110.         }
  111.         public ActionResult PorVender(int? page)
  112.         {
  113.            
  114.             List<animal> datos = new List<animal>();
  115.             var animal = db.animal.Include(a => a.estado).Include(a => a.raza).Include(a => a.tipo);
  116.             foreach(var anim in animal)
  117.             {
  118.                 DateTime? naci = anim.fec_nac;
  119.                 DateTime nac_fec = Convert.ToDateTime(naci);
  120.                 DateTime edad = DateTime.MinValue + (DateTime.Today - nac_fec);
  121.                 int año = edad.Year - 1;
  122.                 if(año >= 2 && anim.fec_nac != null && anim.estado.nombre != "MUERTO" && anim.estado.nombre != "VENDIDO")
  123.                 {
  124.                     datos.Add(anim);
  125.                    
  126.                 }
  127.             }
  128.             return View(datos.ToList().ToPagedList(page ?? 1, 5));
  129.  
  130.         }
  131.            
  132.  
  133.  
  134.        
  135.         // GET: Animal/Details/5
  136.         public ActionResult Details(int? id)
  137.         {
  138.  
  139.             if (id == null)
  140.             {
  141.                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  142.             }
  143.             animal animal = db.animal.Find(id);
  144.             if (animal == null)
  145.             {
  146.                 return HttpNotFound();
  147.             }
  148.             using (bdagricolaEntities dc = new bdagricolaEntities())
  149.             {
  150.                 string error;
  151.  
  152.  
  153.                 DateTime? naci = animal.fec_nac;
  154.                 if (naci == null)
  155.                 {
  156.  
  157.                 }
  158.                 else
  159.                 {
  160.                     if (animal.estado.nombre.Equals("MUERTO") || animal.estado.nombre.Equals("VENDIDO"))
  161.                     {
  162.  
  163.                     }
  164.                     else
  165.                     {
  166.  
  167.                         DateTime nac_fec = Convert.ToDateTime(naci);
  168.                         DateTime edad = DateTime.MinValue + (DateTime.Today - nac_fec);
  169.                         int año = edad.Year - 1;
  170.                         if (año >= 2)
  171.                         {
  172.                             TempData["notice"] = "Este animal esta en la edad Optima para ser Vendido";
  173.                         }
  174.                     }
  175.  
  176.                 }
  177.             }
  178.             return View(animal);
  179.         }
  180.  
  181.         // GET: Animal/Create
  182.         public ActionResult Create()
  183.         {
  184.             ViewBag.estado_id = new SelectList(db.estado, "id", "nombre");
  185.             ViewBag.raza_id = new SelectList(db.raza, "id", "nombre");
  186.             ViewBag.tipo_id = new SelectList(db.tipo, "id", "nombre");
  187.             return View();
  188.         }
  189.  
  190.         // POST: Animal/Create
  191.         // Para protegerse de ataques de publicación excesiva, habilite las propiedades específicas a las que desea enlazarse. Para obtener
  192.         // más información vea http://go.microsoft.com/fwlink/?LinkId=317598.
  193.         [HttpPost]
  194.         [ValidateAntiForgeryToken]
  195.         public ActionResult Create([Bind(Include = "id,codigo_sag,sexo,fec_nac,fecha_ing,tipo_id,raza_id,estado_id")] animal animal)
  196.         {
  197.            
  198.             if (ModelState.IsValid)
  199.             {
  200.                
  201.                 if(animal.fec_nac > animal.fecha_ing)
  202.                 {
  203.                     ViewBag.Error = "Fecha de nacimiento no debe ser superior a la de ingreso al fundo";
  204.                    
  205.                 }
  206.                 else
  207.                 {
  208.                     DateTime? hoy = DateTime.Today;
  209.                     if(animal.fec_nac > hoy || animal.fecha_ing > hoy)
  210.                     {
  211.                         ViewBag.Error = "Las fechas ingresadas no deben superar el dia actual";
  212.                     }
  213.                     else
  214.                     {
  215.                         db.animal.Add(animal);
  216.                         db.SaveChanges();
  217.                         return RedirectToAction("Index");
  218.                     }
  219.                 }
  220.  
  221.             }
  222.  
  223.             ViewBag.estado_id = new SelectList(db.estado, "id", "nombre", animal.estado_id);
  224.             ViewBag.raza_id = new SelectList(db.raza, "id", "nombre", animal.raza_id);
  225.             ViewBag.tipo_id = new SelectList(db.tipo, "id", "nombre", animal.tipo_id);
  226.             return View(animal);
  227.         }
  228.        
  229.         // GET: Animal/Edit/5
  230.         public ActionResult Edit(int? id)
  231.         {
  232.             if (id == null)
  233.             {
  234.                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  235.             }
  236.             animal animal = db.animal.Find(id);
  237.             if (animal == null)
  238.             {
  239.                 return HttpNotFound();
  240.             }
  241.             ViewBag.estado_id = new SelectList(db.estado, "id", "nombre", animal.estado_id);
  242.             ViewBag.raza_id = new SelectList(db.raza, "id", "nombre", animal.raza_id);
  243.             ViewBag.tipo_id = new SelectList(db.tipo, "id", "nombre", animal.tipo_id);
  244.  
  245.             return View(animal);
  246.         }
  247.  
  248.         // POST: Animal/Edit/5
  249.         // Para protegerse de ataques de publicación excesiva, habilite las propiedades específicas a las que desea enlazarse. Para obtener
  250.         // más información vea http://go.microsoft.com/fwlink/?LinkId=317598.
  251.         [HttpPost]
  252.         [ValidateAntiForgeryToken]
  253.         public ActionResult Edit([Bind(Include = "id,codigo_sag,sexo,fec_nac,fecha_ing,tipo_id,raza_id,estado_id")] animal animal)
  254.         {
  255.             if (ModelState.IsValid)
  256.             {
  257.                 if (animal.fec_nac > animal.fecha_ing)
  258.                 {
  259.                     ViewBag.Error = "Fecha de nacimiento no debe ser superior a la de ingreso al fundo";
  260.  
  261.                 }
  262.                 else
  263.                 {
  264.                     DateTime? hoy = DateTime.Today;
  265.                     if (animal.fec_nac > hoy || animal.fecha_ing > hoy)
  266.                     {
  267.                         ViewBag.Error = "Las fechas ingresadas no deben superar el dia actual";
  268.                     }
  269.                     else
  270.                     {
  271.                         db.Entry(animal).State = EntityState.Modified;
  272.                         db.SaveChanges();
  273.                         return RedirectToAction("Index");
  274.                     }
  275.                 }
  276.                
  277.             }
  278.             ViewBag.estado_id = new SelectList(db.estado, "id", "nombre", animal.estado_id);
  279.             ViewBag.raza_id = new SelectList(db.raza, "id", "nombre", animal.raza_id);
  280.             ViewBag.tipo_id = new SelectList(db.tipo, "id", "nombre", animal.tipo_id);
  281.             return View(animal);
  282.         }
  283.  
  284.         // GET: Animal/Delete/5
  285.         public ActionResult Delete(int? id)
  286.         {
  287.             if (id == null)
  288.             {
  289.                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  290.             }
  291.             animal animal = db.animal.Find(id);
  292.             if (animal == null)
  293.             {
  294.                 return HttpNotFound();
  295.             }
  296.             return View(animal);
  297.         }
  298.  
  299.         // POST: Animal/Delete/5
  300.         [HttpPost, ActionName("Delete")]
  301.         [ValidateAntiForgeryToken]
  302.         public ActionResult DeleteConfirmed(int id)
  303.         {
  304.             animal animal = db.animal.Find(id);
  305.             try
  306.             {
  307.                
  308.                 db.animal.Remove(animal);
  309.                 db.SaveChanges();
  310.                 return RedirectToAction("Index");
  311.             }
  312.             catch (Exception e)
  313.             {
  314.  
  315.                 ViewBag.Error = "No se puede eliminar debido a que existen datos asociados a el animal";
  316.                
  317.  
  318.             }
  319.             return View(animal);
  320.         }
  321.         //public ActionResult Inicio()
  322.         //{
  323.         //    List<animal> allCust = new List<animal>();
  324.         //    using (MyDatabaseEntities dc = new MyDatabaseEntities())
  325.         //    {
  326.         //        allCust = dc.CustomerInfoes.ToList();
  327.         //    }
  328.         //    return View(allCust);
  329.         //}
  330.         public void GetExcel()
  331.         {
  332.             List<animal> animal = new List<animal>();
  333.  
  334.             List<tipo> tipo = new List<tipo>();
  335.             List<raza> raza = new List<raza>();
  336.             List<estado> estado = new List<estado>();
  337.             using (bdagricolaEntities dc = new bdagricolaEntities())
  338.             {
  339.                 animal = dc.animal.Where(a => a.estado.nombre != "VENDIDO" && a.estado.nombre != "MUERTO").ToList();
  340.                
  341.                 tipo = dc.tipo.ToList();
  342.                 raza = dc.raza.ToList();
  343.                 estado = dc.estado.ToList();
  344.             }
  345.  
  346.             WebGrid grid = new WebGrid(source: animal, canPage: false, canSort: false);
  347.  
  348.             string gridData = grid.GetHtml(
  349.                 columns: grid.Columns(
  350.                         grid.Column("id", "ID"),
  351.                         grid.Column("codigo_sag", "Codigo de SAG"),
  352.                         grid.Column("sexo", "Sexo"),
  353.                         grid.Column("fec_nac", "Nacimiento"),
  354.                         grid.Column("fecha_ing", "Ingreso"),
  355.                         grid.Column("tipo.nombre", "Tipo"),
  356.                         grid.Column("raza.nombre", "Raza"),
  357.                         grid.Column("estado.nombre", "Estado")
  358.                         )
  359.                     ).ToString();
  360.  
  361.             Response.ClearContent();
  362.             Response.AddHeader("content-disposition", "attachment; filename=AnimalesFundo.xls");
  363.             Response.ContentType = "application/excel";
  364.             Response.Write(gridData);
  365.             Response.End();
  366.         }
  367.         public void GetExcel2()
  368.         {
  369.             List<animal> animal = new List<animal>();
  370.  
  371.             List<tipo> tipo = new List<tipo>();
  372.             List<raza> raza = new List<raza>();
  373.             List<estado> estado = new List<estado>();
  374.             using (bdagricolaEntities dc = new bdagricolaEntities())
  375.             {
  376.                 animal = dc.animal.ToList();
  377.                 tipo = dc.tipo.ToList();
  378.                 raza = dc.raza.ToList();
  379.                 estado = dc.estado.ToList();
  380.             }
  381.  
  382.             WebGrid grid = new WebGrid(source: animal, canPage: false, canSort: false);
  383.  
  384.             string gridData = grid.GetHtml(
  385.                 columns: grid.Columns(
  386.                         grid.Column("id", "ID"),
  387.                         grid.Column("codigo_sag", "Codigo de SAG"),
  388.                         grid.Column("sexo", "Sexo"),
  389.                         grid.Column("fec_nac", "Nacimiento"),
  390.                         grid.Column("fecha_ing", "Ingreso"),
  391.                         grid.Column("tipo.nombre", "Tipo"),
  392.                         grid.Column("raza.nombre", "Raza"),
  393.                         grid.Column("estado.nombre", "Estado")
  394.                         )
  395.                     ).ToString();
  396.  
  397.             Response.ClearContent();
  398.             Response.AddHeader("content-disposition", "attachment; filename=Animales.xls");
  399.             Response.ContentType = "application/excel";
  400.             Response.Write(gridData);
  401.             Response.End();
  402.         }
  403.        
  404.         protected override void Dispose(bool disposing)
  405.         {
  406.             if (disposing)
  407.             {
  408.                 db.Dispose();
  409.             }
  410.             base.Dispose(disposing);
  411.         }
  412.     }
  413. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement