public ActionResult PreviewEstimate(Estimate estimate)
        {
            // Instantiate the view model we will use in this view
            EstimatesViewModel ViewModel = new EstimatesViewModel();

            // Make sure the id is for a valid design order
            DesignOrder DOrder = db.DesignOrders.Find(estimate.DesignOrderSID);

            if (DOrder == null)
            {
                return(RedirectToAction("DesignOrders"));
            }
            else
            {
                // Store the design order in the view model
                ViewModel.DesignOrders = new List <DesignOrder> {
                    DOrder
                };
            }

            // Get other needed models for populating drop down lists etc
            ViewModel.StressWorkTypes  = db.StressWorkTypes.ToList();
            ViewModel.ToolTypes        = db.ToolTypes.ToList();
            ViewModel.FamilyClasses    = db.FamilyClasses.ToList();
            ViewModel.AppUsers         = db.AppUsers.ToList();
            ViewModel.Statistics       = db.Statistics.ToList();
            ViewModel.ComplexityLevels = db.ComplexityLevels.ToList();

            ViewModel.Estimates = new List <Estimate> {
                estimate
            };

            return(View(ViewModel));
        }
        public ActionResult DeleteOrder(int?id)
        {
            if (id != null)
            {
                DesignOrder DOrder = db.DesignOrders.Find(id);

                if (DOrder != null)
                {
                    DOrder.IsDeleted       = true;
                    db.Entry(DOrder).State = EntityState.Modified;
                    db.SaveChanges();
                }

                // Delete all estimates made for that design order
                List <Estimate> lEstimate = db.Estimates.Where(e => e.DesignOrderSID == id).ToList();
                foreach (Estimate eEstimate in lEstimate)
                {
                    eEstimate.IsDeleted       = true;
                    db.Entry(eEstimate).State = EntityState.Modified;
                }
                db.SaveChanges();
            }

            return(RedirectToAction("DesignOrders"));
        }
        public ActionResult CreateOrder([Bind(Include = "DesignOrderSID,DescriptiveName,DesignOrderNumber,ToolTypeID,PartNumber,Completed,ActualHours")] DesignOrder DOrder)
        {
            if (ModelState.IsValid)
            {
                // check uniqueness

                // aka if the db has a design order with that design order number already
                if (db.DesignOrders.Where(v => v.DesignOrderNumber == DOrder.DesignOrderNumber).FirstOrDefault() != null)
                {
                    ModelState.AddModelError("DOrderExists", "<span class='text-danger'> Design Order already exists. Click <a href=\"List/" + db.DesignOrders.Where(v => v.DesignOrderNumber == DOrder.DesignOrderNumber).FirstOrDefault().DesignOrderSID + "\">here</a> to view it.</span>");

                    // Initialize the view model
                    EstimatesViewModel ViewModel = new EstimatesViewModel();

                    // Add the tool types to the view model (for drop down)
                    ViewModel.DesignOrders = new List <DesignOrder> {
                        new DesignOrder()
                    };
                    ViewModel.ToolTypes = db.ToolTypes.ToList();

                    // Return the view
                    return(View(ViewModel));
                }

                else
                {
                    db.DesignOrders.Add(DOrder);

                    DOrder.CreatedDate     = DateTime.Today;
                    DOrder.IsDeleted       = false;
                    DOrder.CreatedByUserID = db.AppUsers.Single(u => u.UserEmail == User.Identity.Name).AppUserID;

                    db.SaveChanges();


                    if (Request.Form["saveAndEstimate"] != null)
                    {
                        return(RedirectToAction("Create", new { id = DOrder.DesignOrderSID }));
                    }

                    return(RedirectToAction("DesignOrders"));
                }
            }

            return(View(DOrder));
        }
        public ActionResult EditOrder([Bind(Include = "DesignOrderSID,DescriptiveName,DesignOrderNumber,ToolTypeID,PartNumber,IsCompleted,ActualHours")] DesignOrder DOrder)
        {
            if (ModelState.IsValid)
            {
                DesignOrder NewOrder = db.DesignOrders.Find(DOrder.DesignOrderSID);

                NewOrder.DesignOrderNumber = DOrder.DesignOrderNumber;
                NewOrder.DescriptiveName   = DOrder.DescriptiveName;
                NewOrder.ToolTypeID        = DOrder.ToolTypeID;
                NewOrder.PartNumber        = DOrder.PartNumber;
                NewOrder.IsCompleted       = DOrder.IsCompleted;
                NewOrder.ActualHours       = DOrder.ActualHours;

                db.Entry(NewOrder).State = EntityState.Modified;

                NewOrder.UpdatedDate = DateTime.Today;

                db.SaveChanges();
                return(RedirectToAction("DesignOrders"));
            }

            // Initialize view model
            EstimatesViewModel ViewModel = new EstimatesViewModel();

            // Redirect if the ID is not a valid design order id
            if (DOrder == null)
            {
                return(RedirectToAction("DesignOrders", "Estimates"));
            }

            // Put needed elements on the view model
            ViewModel.ToolTypes    = db.ToolTypes.ToList();
            ViewModel.DesignOrders = new List <DesignOrder> {
                DOrder
            };

            return(View(ViewModel));
        }
        // GET Edit a design Order
        public ActionResult EditOrder(int?id)
        {
            // Initialize view model
            EstimatesViewModel ViewModel = new EstimatesViewModel();

            // Get the design order from the submitted ID
            DesignOrder DOrder = db.DesignOrders.Find(id);

            // Redirect if the ID is not a valid design order id
            if (DOrder == null)
            {
                return(RedirectToAction("DesignOrders", "Estimates"));
            }

            // Put needed elements on the view model
            ViewModel.ToolTypes    = db.ToolTypes.ToList();
            ViewModel.DesignOrders = new List <DesignOrder> {
                DOrder
            };

            // Return the view
            return(View(ViewModel));
        }
        // GET: Estimates/Create
        public ActionResult Create(int?id)
        {
            // Instantiate the view model we will use in this view
            EstimatesViewModel ViewModel = new EstimatesViewModel();

            // Some unused code for checking errors on hidden form elements
            //IEnumerable<ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors);
            //ViewBag.Errors = allErrors;

            // Check if they have submitted a design order id
            if (id == null)
            {
                return(RedirectToAction("DesignOrders"));
            }

            // Make sure the id is for a valid design order
            DesignOrder DOrder = db.DesignOrders.Find(id);

            if (DOrder == null)
            {
                return(RedirectToAction("DesignOrders"));
            }
            else
            {
                // Store the design order in the view model
                ViewModel.DesignOrders = new List <DesignOrder> {
                    DOrder
                };
            }

            // Get other needed models for populating drop down lists etc
            ViewModel.StressWorkTypes  = db.StressWorkTypes.ToList();
            ViewModel.ToolTypes        = db.ToolTypes.ToList();
            ViewModel.FamilyClasses    = db.FamilyClasses.ToList();
            ViewModel.AppUsers         = db.AppUsers.ToList();
            ViewModel.Statistics       = db.Statistics.ToList();
            ViewModel.ComplexityLevels = db.ComplexityLevels.ToList();

            // Get the latest estimate for this design order, if any
            Estimate LatestEstimate = db.Estimates
                                      .Where(e => e.DesignOrderSID == DOrder.DesignOrderSID)
                                      .Where(e => e.IsLatestEstimate == true)
                                      .ToList().FirstOrDefault();

            // Create an empty estimate and set the fields we already know
            Estimate est = new Estimate();

            est.DesignOrderSID = DOrder.DesignOrderSID;

            // If there aren't any previous estimates, set a default value for "reason for estimate change" to "initial estimate"
            if (LatestEstimate == null)
            {
                est.ReasonForEstimateChange = "Initial Estimate";
            }
            else
            {
                // Auto-fill this estimate with values from the latest estimate
                est.NeedsSurfacing      = LatestEstimate.NeedsSurfacing;
                est.IsStressIncluded    = LatestEstimate.IsStressIncluded;
                est.StressWorkTypeID    = LatestEstimate.StressWorkTypeID;
                est.ComplexityLevel     = LatestEstimate.ComplexityLevel;
                est.EngineeringReleased = LatestEstimate.EngineeringReleased;
                est.FamilyClassID       = LatestEstimate.FamilyClassID;
            }

            // Return everything to the view
            ViewModel.Estimates = new List <Estimate> {
                est
            };
            return(View(ViewModel));
        }