using System; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using BeWoPlanerMobil.Models; using BeWoPlanerMobil.Service; using BS.Shared; using BS.Shared.DataContracts; using BS.Shared.DataContracts.Compact; using static BeWoPlanerMobil.Util.MobileUtils; namespace BeWoPlanerMobil.Controllers { public class CustomerController : AbstractBaseController { public static string CustomerModelModelSessionKey = "CustomerModelModel"; private CustomerModel _Model; public CustomerModel Model { get { if (!MobileSessionFacade.IsUserLoggedIn()) { RedirectToActionPermanent("Index", "Login"); return null; } if (((CustomerModel) Session[CustomerModelModelSessionKey])?.Employee == null) { _Model = new CustomerModel{Employee = MobileSessionFacade.LoggedInEmployee}; Session[CustomerModelModelSessionKey] = _Model; } else { _Model = (CustomerModel)Session[CustomerModelModelSessionKey]; } return _Model; } } [Authorize] public ActionResult Customer() { if (!MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer")) { return RedirectToActionPermanent("Index", "Login"); } InitViewModel(); return View(Model); } private void InitViewModel() { var myRelatedCustomers = Model.Employee == null ? new List() : Model.Employee.RelatedCustomers; if (MobileSessionFacade.CheckForUserRight(UserRightType.CustomerView_View)) { Model.Customers = CustomerService.GetAllActiveCompactCustomers().OrderBy(k => k.LastName).ToList(); } else if (MobileSessionFacade.CheckForUserRight(UserRightType.Customer_ViewMyCustomers)) { Model.Customers = CustomerService.GetCompactCustomersById(myRelatedCustomers.Select(c => c.Customer.CustomerOid).ToList()).OrderBy(k => k.LastName).ToList(); } else { Model.Customers = new List(); } } [Authorize] public ActionResult PreselectCustomer(long? customerOid) { if (!MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer")) { return RedirectToActionPermanent("Index", "Login"); } InitViewModel(); if (customerOid.HasValue && customerOid > 0) { Model.SelectedCustomerOid = customerOid; Model.SelectedCustomer = CustomerService.LoadCustomer(customerOid.Value); } return RedirectToActionPermanent("Customer"); } [Authorize] [HttpPost] public ActionResult SelectCustomer(FormCollection formCollection) { try { if (Model == null) { return RedirectToActionPermanent("Index", "Login"); } var selectedCustomerOidString = formCollection["SelectedCustomerOid"]; if (long.TryParse(selectedCustomerOidString, out var selectedCustomerOid)) { Model.SelectedCustomerOid = selectedCustomerOid == -1 ? (long?) null : selectedCustomerOid; Model.SelectedCustomer = selectedCustomerOid != -1 ? CustomerService.LoadCustomer(selectedCustomerOid) : null; } return RedirectToActionPermanent("Customer"); } catch (Exception exception) { Log.Error(exception.Message, exception); } return RedirectToActionPermanent("Customer"); } } }