using System; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using BeWoPlanerMobil.Models; using BeWoPlanerMobil.Service; using BeWoPlanerMobil.Util; using BS.Shared; using BS.Shared.DataContracts; using BS.Shared.DataContracts.Compact; using static BeWoPlanerMobil.Util.MobileUtils; namespace BeWoPlanerMobil.Controllers { public class CustomerController : AbstractBaseController { private CustomerModel _Model; public CustomerModel Model { get { if (!MobileSessionFacade.IsUserLoggedIn()) { Logout(); return null; } if (((CustomerModel) Session[ModelSessionConstants.CustomerModelKey])?.Employee == null) { _Model = new CustomerModel{Employee = MobileSessionFacade.LoggedInEmployee}; Session[ModelSessionConstants.CustomerModelKey] = _Model; } else { _Model = (CustomerModel)Session[ModelSessionConstants.CustomerModelKey]; } return _Model; } } [Authorize] public ActionResult Customer() { if (!MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer")) { return Logout(); } InitViewModel(); return View(Model); } private void InitViewModel() { if(Model == null) { Logout(); } if(Model.Employee?.EmployeeOid.HasValue ?? false) { Model.Customers = EmployeeService.GetActiveCompactCustomersForEmployee(Model.Employee.EmployeeOid.Value).OrderBy(customer => customer.LastName).ToList(); } else { Model.Customers = new List(); } } [Authorize] public ActionResult PreselectCustomer(long? customerOid) { if (!MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer")) { return Logout(); } 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 Logout(); } var selectedCustomerOidString = formCollection[FormCollectionConstants.SelectedCustomerOidKey]; 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"); } } }