public void CreateWithExistingCustomerId()
 {
     long? existingcustomerId = 1;
     string stringifiedexistingCustomerId = Convert.ToString(existingcustomerId.Value);
     var customer = new customer { customerID = existingcustomerId.Value, name = "Test customer", state = "Florida" };
     customer.customerList = new List<SelectListItem>();
     customer.StateList = new List<SelectListItem>();
     customer.TrainList = new List<SelectListItem>();
     customer.CityList = new List<SelectListItem>();
     mockedCustomerRepository.Setup(m => m.FindById(stringifiedexistingCustomerId)).Returns(customer);
     var customerList = new List<customer> { customer };
     mockedRepositoryCustomer.Setup(m => m.GetAll()).Returns(customerList.AsQueryable());
     var stateList = new List<source>() { new source { state = "FL", state_name = "Florida", city = "Florida" } };
     mockedCustomerRepository.Setup(m => m.GetStateList()).Returns(stateList);
     var controller = new CustomerController(mockedRepositoryCustomer.Object, mockedCustomerRepository.Object, mockedRepositorySource.Object);
     controller.ControllerContext = new ControllerContext();
     var mockedHttpContext = new Mock<HttpContextBase>();
     var mockedSessionState = new HttpSessionMock();
     mockedHttpContext.SetupGet(ctx => ctx.Session).Returns(mockedSessionState);
     controller.ControllerContext.HttpContext = mockedHttpContext.Object;
     var returnObj = controller.Create(existingcustomerId);
     Assert.IsNotNull(returnObj);
     Assert.IsInstanceOfType(returnObj, typeof(PartialViewResult));
     var result = (PartialViewResult)returnObj;
     Assert.IsNotNull(result);
     mockedCustomerRepository.Verify(m => m.FindById(Convert.ToString(existingcustomerId)), Times.Once());
     mockedRepositoryCustomer.Verify(m => m.GetAll(), Times.Once());
     mockedCustomerRepository.Verify(m => m.GetStateList(), Times.Once());
 }
 public bool CheckForDuplicate(string name, string state, string city)
 {
     try
     {
         customer customer = new customer();
         customer.name = name;
         customer.city = city;
         customer.state = state;
         return this.modifiedCustRepository.CheckForDuplicates(customer);
     }
     catch
     {
         throw;
     }
 }
        /// <summary>
        /// CostAnalyzer
        /// </summary>        
        /// <returns></returns>
        public ActionResult CostAnalyzer()
        {
            long customerId = 0;
            if (this.Session["CustomerId"] != null)
            {
                customerId = Convert.ToInt64(this.Session["CustomerId"]);
            }
            customer CustDetails = new customer();
            SystemSummaryViewModel SSVMDetails = new SystemSummaryViewModel();
            customer_water CustWaterDetails = null;
            try
            {
                //If valid customer id does not exist in the session
                if (customerId != 0)
                {
                    var Customer = this.customerRepository.GetAll();
                    CustDetails = Customer.Where(x => x.customerID == customerId).FirstOrDefault();
                    SSVMDetails = new SystemSummaryViewModel();
                    SSVMDetails.CustomerDetails = CustDetails;
                    CustWaterDetails = this.modifiedCustRepository.GetWaterSourceIds(customerId);
                }

                if (CustWaterDetails != null)
                {
                    if (CustWaterDetails.first_sourceID != 0)
                    {
                        SSVMDetails.WaterSourceOne = this.modifiedCustRepository.GetWaterSourceDetails(CustWaterDetails.first_sourceID);//Get first watersource details
                        if (SSVMDetails.WaterSourceOne != null)
                        {
                            SSVMDetails.WaterSourceOne.full_site_name = SSVMDetails.WaterSourceOne.full_site_name.Replace("@", string.Empty);//Remove @ symbol from the watersource name
                        }
                    }
                    else
                    {
                        SSVMDetails.WaterSourceOne = null;
                    }
                    if (CustWaterDetails.second_sourceID != 0)
                    {
                        SSVMDetails.WaterSourceTwo = this.modifiedCustRepository.GetWaterSourceDetails(CustWaterDetails.second_sourceID);//Get second watersource details
                        if (SSVMDetails.WaterSourceTwo != null)
                        {
                            SSVMDetails.WaterSourceTwo.full_site_name = SSVMDetails.WaterSourceTwo.full_site_name.Replace("@ ", string.Empty);//Remove @ symbol from the watersource name
                        }
                    }
                    else
                    {
                        SSVMDetails.WaterSourceTwo = null;
                    }
                    SSVMDetails.Trains = this.modifiedCustRepository.GetCustomerTrains(customerId);//Get list of cutomer train details
                }
                else
                {
                    this.Session["IsNewCustomer"] = "True";
                    this.Session["HasTrainDetails"] = "Check";
                }
                SSVMDetails.CustomerType = Convert.ToString(this.Session["IsNewCustomer"]);
                SSVMDetails.HasTrainDetails = Convert.ToString(this.Session["HasTrainDetails"]);
                if (Convert.ToString(this.Session["HasTrainDetails"]) == "Verify")
                {
                    var count = (this.vesselRepository.GetAll().Where(p => p.vessel_customerID == (customerId).ToString())).Count();
                    if (count != 0)
                    {
                        SSVMDetails.HasTrainDetails = "Yes";
                    }
                    else
                    {
                        SSVMDetails.HasTrainDetails = "No";
                    }
                }
            }
            catch
            {
                throw;
            }
            return this.View(SSVMDetails);
        }
        public ActionResult Create(long? customerId)
        {
            try
            {
                customer Cust = new customer()
                {
                    customerList = new List<SelectListItem>(),
                    StateList = new List<SelectListItem>(),
                    TrainList = new List<SelectListItem>(),
                    CityList = new List<SelectListItem>()
                };
                if (customerId != null && customerId != -1)
                {
                    Cust = this.modifiedCustRepository.FindById(Convert.ToString(customerId));

                }
                List<customer> c = new List<customer>();
                List<source> s = new List<source>();
                c = this.customerRepository.GetAll().ToList();
                s = this.modifiedCustRepository.GetStateList();
                Cust.customerList.Add(new SelectListItem() { Text = "New Customer..", Value = "-1" });
                foreach (var item in c)
                {
                    Cust.customerList.Add(new SelectListItem() { Text = item.name, Value = Convert.ToString(item.customerID) });
                }

                var createCustomerData = s.GroupBy(g => new { g.state_name, g.state }).Select(g => g.FirstOrDefault()).ToList();

                foreach (var item in createCustomerData)
                {
                    Cust.StateList.Add(new SelectListItem() { Text = item.state_name, Value = item.state_name });
                }
                for (int i = 1; i <= 20; i++)
                {
                    Cust.TrainList.Add(new SelectListItem() { Text = i.ToString(), Value = i.ToString() });
                }
                if (customerId != null && customerId != -1)
                {
                    foreach (var item in s.Where(x => x.state_name == Cust.state)
                        .GroupBy(g => new {
                            g.city
                        })
                        .Select(g => g.FirstOrDefault()).ToList())
                    {
                        Cust.CityList.Add(new SelectListItem() { Text = item.city, Value = item.city });
                    }
                    this.Session["IsDuplicate"] = "False";
                    return this.PartialView("_Edit", Cust);
                }
                else
                {
                    if (Convert.ToString(this.Session["IsDuplicate"]) == "True")
                    {
                        ModelState.AddModelError("notes", "Customer Already exists");
                    }
                    this.Session["IsDuplicate"] = "False";
                    return this.PartialView("_Create", Cust);
                }
            }
            catch
            {
                throw;
            }
        }
 public ActionResult Edit(customer customer)
 {
     try
     {
         if (ModelState.IsValid)
         {
             this.modifiedCustRepository.UpdateCustomer(customer);
             return RedirectToAction("Index");
         }
         return this.View(customer);
     }
     catch
     {
         throw;
     }
 }
 public ActionResult Create(customer Customer)
 {
     try
     {
         if (ModelState.IsValid)
         {
             if (Customer.customerID == -1)
             {
                 long Id = this.customerRepository.InsertAndGetID(Customer);
                 this.Session["CustomerId"] = Id;
                 this.Session["IsNewCustomer"] = "True";
                 this.Session["IsDuplicate"] = "False";
                 this.Session["HasTrainDetails"] = "Check";
                 return RedirectToAction("Dashboard", "ClientDatabase");
             }
             else
             {
                 this.modifiedCustRepository.UpdateCustomer(Customer);
                 this.Session["CustomerId"] = Customer.customerID;
                 this.Session["IsNewCustomer"] = "False";
                 this.Session["IsDuplicate"] = "False";
                 this.Session["HasTrainDetails"] = "Verify";
                 return RedirectToAction("Dashboard", "ClientDatabase", new { id = Customer.customerID });
             }
         }
         else
         {
             this.Session["IsDuplicate"] = "True";
             return RedirectToAction("Dashboard", "ClientDatabase");
         }
     }
     catch
     {
         throw;
     }
 }
 /// <summary>
 /// Predictive System Performance view
 /// </summary>
 /// <param name="id">customer identifier</param>
 /// <returns>Returns the Predictive System Performance view</returns>
 public ActionResult PredictiveSystemPerformance()
 {
     long customerId = 0;
     if (this.Session["CustomerId"] != null)
     {
         customerId = Convert.ToInt64(this.Session["CustomerId"]);
     }
     customer customerDetails = new customer();
     SystemSummaryViewModel ssvmDetails = new SystemSummaryViewModel();
     customer_water custWaterDetails = null;
     try
     {
         if (customerId != 0)
         {
             var Customer = this.customerRepository.GetAll();
             customerDetails = Customer.Where(x => x.customerID == customerId).FirstOrDefault();
             ssvmDetails = new SystemSummaryViewModel();
             ssvmDetails.CustomerDetails = customerDetails;
             custWaterDetails = this.modifiedCustomerRepository.GetWaterSourceIds(customerId);
         }
         if (custWaterDetails != null)
         {
             if (custWaterDetails.first_sourceID != 0)
             {
                 ssvmDetails.WaterSourceOne = this.modifiedCustomerRepository.GetWaterSourceDetails(custWaterDetails.first_sourceID);//Get first watersource details
                 if (ssvmDetails.WaterSourceOne != null)
                 {
                     ssvmDetails.WaterSourceOne.full_site_name = ssvmDetails.WaterSourceOne.full_site_name.Replace("@", string.Empty);//Remove @ symbol from the watersource name
                 }
             }
             else
             {
                 ssvmDetails.WaterSourceOne = null;
             }
             if (custWaterDetails.second_sourceID != 0)
             {
                 ssvmDetails.WaterSourceTwo = this.modifiedCustomerRepository.GetWaterSourceDetails(custWaterDetails.second_sourceID);//Get second watersource details
                 if (ssvmDetails.WaterSourceTwo != null)
                 {
                     ssvmDetails.WaterSourceTwo.full_site_name = ssvmDetails.WaterSourceTwo.full_site_name.Replace("@ ", string.Empty);//Remove @ symbol from the watersource name
                 }
             }
             else
             {
                 ssvmDetails.WaterSourceTwo = null;
             }
             ssvmDetails.Trains = this.modifiedCustomerRepository.GetCustomerTrains(customerId);//Get list of cutomer train details
         }
         else
         {
             this.Session["IsNewCustomer"] = "True";
             this.Session["HasTrainDetails"] = "Check";
         }
         ssvmDetails.CustomerType = Convert.ToString(this.Session["IsNewCustomer"]);
         ssvmDetails.HasTrainDetails = Convert.ToString(this.Session["HasTrainDetails"]);
         if (Convert.ToString(this.Session["HasTrainDetails"]) == "Verify")
         {
             var count = this.vesselRepository.GetAll().Where(p => p.vessel_customerID == (customerId).ToString()).Count();
             if (count != 0)
             {
                 ssvmDetails.HasTrainDetails = "Yes";
             }
             else
             {
                 ssvmDetails.HasTrainDetails = "No";
             }
         }
         WaterStatisticsViewModel objWaterStatisticsViewModel = new WaterStatisticsViewModel();
         objWaterStatisticsViewModel = this.modifiedPredictiveRepository.GetWaterStatistics(customerId);
         ssvmDetails.waterStatisticsViewModeldetails = objWaterStatisticsViewModel;
     }
     catch (Exception)
     {
         throw;
     }
     return this.View(ssvmDetails);
 }
        public ActionResult WaterConductivity()
        {
            long customerId = 0;
            if (this.Session["CustomerId"] != null)
            {
                customerId = Convert.ToInt64(this.Session["CustomerId"]);
            }
            customer CustDetails = new customer();
            SystemSummaryViewModel SSVMDetails = new SystemSummaryViewModel();
            customer_water CustWaterDetails = null;
            try
            {
                //If valid customer id does not exist in the session
                if (customerId != 0)
                {
                    var Customer = this.customerRepository.GetAll();
                    CustDetails = Customer.Where(x => x.customerID == customerId).FirstOrDefault();
                    SSVMDetails = new SystemSummaryViewModel();
                    SSVMDetails.CustomerDetails = CustDetails;
                    CustWaterDetails = this.modifiedCustRepository.GetWaterSourceIds(customerId);
                }

                if (CustWaterDetails != null)
                {
                    if (CustWaterDetails.first_sourceID != 0)
                    {
                        SSVMDetails.WaterSourceOne = this.modifiedCustRepository.GetWaterSourceDetails(CustWaterDetails.first_sourceID);//Get first watersource details
                        if (SSVMDetails.WaterSourceOne != null)
                        {
                            SSVMDetails.WaterSourceOne.full_site_name = SSVMDetails.WaterSourceOne.full_site_name.Replace("@", string.Empty);//Remove @ symbol from the watersource name
                            SSVMDetails.Source1Statistics = this.conductivityService.CalculateStatistics(CustWaterDetails.first_sourceID);
                        }
                    }
                    else
                    {
                        SSVMDetails.WaterSourceOne = null;
                    }
                    if (CustWaterDetails.second_sourceID != 0)
                    {
                        SSVMDetails.WaterSourceTwo = this.modifiedCustRepository.GetWaterSourceDetails(CustWaterDetails.second_sourceID);//Get second watersource details
                        if (SSVMDetails.WaterSourceTwo != null)
                        {
                            SSVMDetails.WaterSourceTwo.full_site_name = SSVMDetails.WaterSourceTwo.full_site_name.Replace("@ ", string.Empty);//Remove @ symbol from the watersource name
                            SSVMDetails.Source2Statistics = this.conductivityService.CalculateStatistics(CustWaterDetails.second_sourceID);
                        }
                    }
                    else
                    {
                        SSVMDetails.WaterSourceTwo = null;
                    }
                    SSVMDetails.Trains = this.modifiedCustRepository.GetCustomerTrains(customerId);//Get list of cutomer train details
                }
                else
                {
                    this.Session["IsNewCustomer"] = "True";
                    this.Session["HasTrainDetails"] = "Check";
                }

            }
            catch
            {
                throw;
            }
            return this.View(SSVMDetails);
        }
 public void CreateWithoutExistingCustomerIdWithDuplicate()
 {
     var customer = new customer { customerID = (long)1, name = "Test customer", state = "Florida" };
     customer.customerList = new List<SelectListItem>();
     customer.StateList = new List<SelectListItem>();
     customer.TrainList = new List<SelectListItem>();
     customer.CityList = new List<SelectListItem>();
     mockedCustomerRepository.Setup(m => m.FindById(It.IsAny<string>())).Verifiable();
     var customerList = new List<customer> { customer };
     mockedRepositoryCustomer.Setup(m => m.GetAll()).Returns(customerList.AsQueryable());
     var stateList = new List<source>() { new source { state = "FL", state_name = "Florida", city = "Florida" } };
     mockedCustomerRepository.Setup(m => m.GetStateList()).Returns(stateList);
     var controller = new CustomerController(mockedRepositoryCustomer.Object, mockedCustomerRepository.Object, mockedRepositorySource.Object);
     controller.ControllerContext = new ControllerContext();
     var mockedHttpContext = new Mock<HttpContextBase>();
     var mockedSessionState = new HttpSessionMock();
     mockedSessionState["IsDuplicate"] = "True";
     mockedHttpContext.SetupGet(ctx => ctx.Session).Returns(mockedSessionState);
     controller.ControllerContext.HttpContext = mockedHttpContext.Object;
     long? customerId = null;
     var returnObj = controller.Create(customerId);
     Assert.IsNotNull(returnObj);
     Assert.IsInstanceOfType(returnObj, typeof(PartialViewResult));
     var result = (PartialViewResult)returnObj;
     Assert.IsNotNull(result);
     Assert.AreEqual(result.ViewName, "_Create");
     Assert.AreEqual(result.ViewData.ModelState.IsValid, false);
     Assert.AreEqual(result.ViewData.ModelState["notes"].Errors[0].ErrorMessage, "Customer Already exists");
     mockedCustomerRepository.Verify(m => m.FindById(Convert.ToString(It.IsAny<string>())), Times.Never());
     mockedRepositoryCustomer.Verify(m => m.GetAll(), Times.Once());
     mockedCustomerRepository.Verify(m => m.GetStateList(), Times.Once());
 }
 public void UpdateCustomer()
 {
     customer customer = new customer() { customerID = 1 };
     bool IsDuplicate = false;
     mockedCustomerRepository.Setup(item => item.CheckForDuplicates(customer)).Returns(IsDuplicate);
     mockedCustomerRepository.Setup(item => item.UpdateCustomer(customer)).Verifiable();
     var controller = new CustomerController(mockedRepositoryCustomer.Object, mockedCustomerRepository.Object, mockedRepositorySource.Object);
     controller.ControllerContext = new ControllerContext();
     var mockedHttpContext = new Mock<HttpContextBase>();
     var mockedSessionState = new HttpSessionMock();
     mockedHttpContext.SetupGet(ctx => ctx.Session).Returns(mockedSessionState);
     controller.ControllerContext.HttpContext = mockedHttpContext.Object;
     var returnObj = controller.Create(customer);
     Assert.IsNotNull(returnObj);
     Assert.IsInstanceOfType(returnObj, typeof(RedirectToRouteResult));
     var result = (RedirectToRouteResult)returnObj;
     Assert.IsNotNull(result);
     mockedCustomerRepository.Verify(m => m.CheckForDuplicates(customer), Times.Never());
     mockedCustomerRepository.Verify(m => m.UpdateCustomer(customer), Times.Once());
 }
 public void UpdateCustomerWithErrors()
 {
     customer customer = new customer() { customerID = 1 };
     var controller = new CustomerController(mockedRepositoryCustomer.Object, mockedCustomerRepository.Object, mockedRepositorySource.Object);
     controller.ControllerContext = new ControllerContext();
     var mockedHttpContext = new Mock<HttpContextBase>();
     var mockedSessionState = new HttpSessionMock();
     mockedHttpContext.SetupGet(ctx => ctx.Session).Returns(mockedSessionState);
     controller.ControllerContext.HttpContext = mockedHttpContext.Object;
     controller.ModelState.AddModelError("Error", "ErrorMessage"); ;
     var returnObj = controller.Create(customer);
     Assert.IsNotNull(returnObj);
     Assert.IsInstanceOfType(returnObj, typeof(RedirectToRouteResult));
     var result = (RedirectToRouteResult)returnObj;
     Assert.IsNotNull(result);
     mockedCustomerRepository.Verify(m => m.CheckForDuplicates(customer), Times.Never());
     mockedCustomerRepository.Verify(m => m.UpdateCustomer(customer), Times.Never());
 }
 public void SaveCustomer()
 {
     customer customer = new customer() { customerID = -1 };
     long newCustomerId = 0;
     mockedRepositoryCustomer.Setup(item => item.InsertAndGetID(customer)).Returns(newCustomerId);
     var controller = new CustomerController(mockedRepositoryCustomer.Object, mockedCustomerRepository.Object, mockedRepositorySource.Object);
     controller.ControllerContext = new ControllerContext();
     var mockedHttpContext = new Mock<HttpContextBase>();
     var mockedSessionState = new HttpSessionMock();
     mockedHttpContext.SetupGet(ctx => ctx.Session).Returns(mockedSessionState);
     controller.ControllerContext.HttpContext = mockedHttpContext.Object;
     var returnObj = controller.Create(customer);
     Assert.IsNotNull(returnObj);
     Assert.IsInstanceOfType(returnObj, typeof(RedirectToRouteResult));
     var result = (RedirectToRouteResult)returnObj;
     Assert.IsNotNull(result);
     mockedRepositoryCustomer.Verify(m => m.InsertAndGetID(customer), Times.Once());
 }
 public void EditCustomerSaveFail()
 {
     customer customer = new customer();
     var controller = new CustomerController(mockedRepositoryCustomer.Object, mockedCustomerRepository.Object, mockedRepositorySource.Object);
     controller.ControllerContext = new ControllerContext();
     var mockedHttpContext = new Mock<HttpContextBase>();
     var mockedSessionState = new HttpSessionMock();
     mockedHttpContext.SetupGet(ctx => ctx.Session).Returns(mockedSessionState);
     controller.ControllerContext.HttpContext = mockedHttpContext.Object;
     controller.ModelState.AddModelError("Error", "ErrorMessage"); ;
     var returnObj = controller.Edit(customer);
     Assert.IsNotNull(returnObj);
     Assert.IsInstanceOfType(returnObj, typeof(ViewResult));
     var result = (ViewResult)returnObj;
     Assert.IsNotNull(result);
 }
 public void EditCustomerSave()
 {
     customer customer = new customer();
     mockedCustomerRepository.Setup(item => item.UpdateCustomer(customer)).Verifiable();
     var controller = new CustomerController(mockedRepositoryCustomer.Object, mockedCustomerRepository.Object, mockedRepositorySource.Object);
     var returnObj = controller.Edit(customer);
     Assert.IsNotNull(returnObj);
     Assert.IsInstanceOfType(returnObj, typeof(RedirectToRouteResult));
     var result = (RedirectToRouteResult)returnObj;
     Assert.IsNotNull(result);
     mockedCustomerRepository.Verify(m => m.UpdateCustomer(customer), Times.Once());
 }
 public void EditCustomer()
 {
     string customerId = "1";
     List<customer> lstCustomer = new List<customer>();
     customer customer = new customer() { CityList = new List<SelectListItem>(), customerList = new List<SelectListItem>() };
     lstCustomer.Add(customer);
     mockedCustomerRepository.Setup(item => item.FindById(customerId)).Returns(customer);
     mockedRepositoryCustomer.Setup(item => item.GetAll()).Returns(lstCustomer.AsQueryable());
     var controller = new CustomerController(mockedRepositoryCustomer.Object, mockedCustomerRepository.Object, mockedRepositorySource.Object);
     var returnObj = controller.Edit(customerId);
     Assert.IsNotNull(returnObj);
     Assert.IsInstanceOfType(returnObj, typeof(ViewResult));
     var result = (ViewResult)returnObj;
     Assert.IsNotNull(result);
     mockedCustomerRepository.Verify(m => m.FindById(customerId), Times.Once());
     mockedRepositoryCustomer.Verify(m => m.GetAll(), Times.Once());
 }
 public void Deletevalid()
 {
     string customerId = "0";
     customer customer = new customer();
     mockedCustomerRepository.Setup(item => item.FindById(customerId)).Returns(customer);
     var controller = new CustomerController(mockedRepositoryCustomer.Object, mockedCustomerRepository.Object, mockedRepositorySource.Object);
     var returnObj = controller.Delete(customerId);
     Assert.IsNotNull(returnObj);
     Assert.IsInstanceOfType(returnObj, typeof(ViewResult));
     var result = (ViewResult)returnObj;
     Assert.IsNotNull(result);
     mockedCustomerRepository.Verify(m => m.FindById(customerId), Times.Once());
 }