using System; using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using BeWo.View.Navigation.Filter; using BeWoPlanerMobil.Models; using BeWoPlanerMobil.Service; using BeWoPlanerMobil.Util; using BS.Shared; using BS.Shared.Core; using BS.Shared.DataContracts; using BS.Shared.DataContracts.Compact; using DevExpress.XtraReports.UI; 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() { try { if (!MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer")) { return Logout(); } var customerFilter = MobileUserSettingsUtils.GetSettingValueAsEnum(AbstractModel.UserSettings, SettingsKeys.CustomerFilterCustomers, CustomerFilterEnum.MyCustomer); Model.SelectedCustomerFilter = customerFilter; InitViewModel(); } catch (Exception e) { Log.Info($"Exception: " + e.Message + "\n" + e.StackTrace); if (e.InnerException != null) { Log.Info($"\n\nInnerException: " + e.InnerException.Message + "\n" + e.InnerException.StackTrace); } throw; } return View(Model); } private void InitViewModel() { if(Model == null || !MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer")) { Logout(); } if(Model.Employee?.EmployeeOid.HasValue ?? false) { Model.Customers = EmployeeService.GetActiveCustomersForEmployee(Model.Employee.EmployeeOid.Value, Model.SelectedCustomerFilter).OrderBy(customer => customer.LastName).ToList(); } else { Model.Customers = new List(); } // ReportTypes laden! Erstmal nur Medikamentenansicht if(AbstractModel.IsAllowedToSeeMedikamentenliste) { Model.ReportTypes = new List { CustomerReportType.Medikamentenliste, CustomerReportType.Test }; } } [Authorize] public ActionResult PreselectCustomer(long? customerOid) { if (Model == null || !MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer")) { return Logout(); } InitViewModel(); if (customerOid > 0 && Model.Customers.Any(customer => customer.CustomerOid.Equals(customerOid.Value))) { Model.SelectedCustomerOid = customerOid; Model.SelectedCustomer = CustomerService.LoadCustomer(customerOid.Value); } return RedirectToActionPermanent("Customer"); } [Authorize] [HttpPost] public ActionResult SelectCustomer(FormCollection formCollection) { try { if (Model == null || !MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer")) { return Logout(); } var selectedCustomerOidString = formCollection[FormCollectionConstants.SelectedCustomerOidKey]; if (long.TryParse(selectedCustomerOidString, out var selectedCustomerOid)) { var isCustomerInList = Model.Customers.Any(customer => customer.CustomerOid == selectedCustomerOid); if(isCustomerInList) { Model.FolderTree = OperationsService.GetFolderTree(TableID.Customer, 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"); } [HttpPost] [Authorize] public ActionResult SetCustomerFilter(FormCollection formCollection) { if(Model == null || !MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer")) { return Logout(); } var selectedCustomerFilter = formCollection[nameof(Model.SelectedCustomerFilter)]; if(!Enum.TryParse(selectedCustomerFilter, out CustomerFilterEnum customerFilter)) { customerFilter = CustomerFilterEnum.MyCustomer; } UpdateApplicationSettings(SettingsKeys.CustomerFilterCustomers, customerFilter.ToString()); return RedirectToActionPermanent("Customer"); } [Authorize] public FileResult DownloadFile(long fileOid) { if (Model == null || !MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer")) { Logout(); return null; } var fileAttachment = DownloadService.GetFileAttachmentForMobile(fileOid, MobileSessionFacade.Tenant); return File(fileAttachment.BinaryData, "application/octet-stream", fileAttachment.FileName); } [Authorize] public string SaveEditedNotice(string newNotice) { if(Model == null || !MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer")) { Logout(); return LeerzeichenFuerGetMethoden; } var isAllowedToEdit = MobileSessionFacade.LoggedInUser.CheckForAtLeastOneRight(new List { UserRightType.CustomerView_Edit, UserRightType.EditAll }); if(isAllowedToEdit && Model.SelectedCustomer != null) { Model.SelectedCustomer.Notice = newNotice; var customerOid = CustomerService.UpdateCustomer(Model.SelectedCustomer); Model.SelectedCustomer = CustomerService.LoadCustomer(customerOid); } return LeerzeichenFuerGetMethoden; } [Authorize] [HttpPost] public ActionResult SelectReport(FormCollection formCollection) { if(Model == null || !MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer")) { return Logout(); } var selectedReportType = formCollection[nameof(Model.SelectedReportType)]; if(int.TryParse(selectedReportType, out var reportTypeInt)) { if(AbstractModel.IsAllowedToSeeMedikamentenliste && reportTypeInt == (int) CustomerReportType.Medikamentenliste) { // MedListType = true => Bedarfsliste GenerateMobileMedListForSelectedCustomer(); } } return RedirectToActionPermanent("Customer"); } public void GenerateMobileMedListForSelectedCustomer() { // TODO: Die neueste MedVerListOid laden, sofern eine vorhanden ist. var mvlOid = CustomerService.GetActiveMedVerListOidForCustomer(Model.SelectedCustomerOid.Value); var listen = Model.SelectedCustomer.Medikamentenverordnungslisten; var aktuelleListe = listen.FirstOrDefault(f => f.Gueltig && f.MedListType == false); if (aktuelleListe?.MedikamentenverordnungslistenOid != null && Request?.Url != null) { using (var memoryStream = ReportService.CreateMedikamentenlistenReport(aktuelleListe.MedikamentenverordnungslistenOid.Value, MobileSessionFacade.Tenant)) { var report = new XtraReport(); report.PrintingSystem.LoadDocument(memoryStream); Model.Report = report; } } } public ActionResult UpdateCustomer(FormCollection formCollection) { if(Model is null) { return Logout(); } // ToDo: Stammdaten aus formCollection holen, validieren und Customer-Objekt aktualisieren. var firstname = formCollection["vorname"]; var lastname = formCollection["nachname"]; var geburtdatum = formCollection["geburtstag"]; var geschlecht = formCollection["geschlecht"]; var adresszusatz = formCollection["adresszusatz"]; var street = formCollection["strasse"]; var postalCode = formCollection["plz"]; var city = formCollection["ort"]; var nameInvoiceAddress = formCollection["name-rechnungsadresse"]; var streetInvoiceAddress = formCollection["strasse-rechnungsadresse"]; var postalCodeInvoiceAddress = formCollection["plz-rechnungsadresse"]; var cityInvoiceAddress = formCollection["ort-rechnungsadresse"]; var mobile = formCollection["handy"]; var phone = formCollection["telefon"]; var fax = formCollection["fax"]; var eMailAddress = formCollection["mail"]; var notice = formCollection["notiz"]; var customer = Model.SelectedCustomer; if(customer is null) { return RedirectToActionPermanent("Customer"); } customer.FirstName = firstname; customer.LastName = lastname; if(DateTime.TryParse(geburtdatum, out var dateOfBirth)) { customer.DateOfBirth = dateOfBirth; } if(int.TryParse(geschlecht, out var sexIndex)) { var castSex = sexIndex >= 0 ? (Sex?)sexIndex : null; customer.Sex = castSex; } customer.AddressLine1 = adresszusatz; customer.Street = street; customer.PostalCode = postalCode; customer.Town = city; customer.InvoiceAddressLine1 = nameInvoiceAddress; customer.InvoiceAddressStreet = streetInvoiceAddress; customer.InvoiceAddressPostalCode = postalCodeInvoiceAddress; customer.InvoiceAddressTown = cityInvoiceAddress; var mobileContactInfo = customer.ContactInformations.FirstOrDefault(contactInformation => contactInformation.ContactType == ContactType.business_MobilePhone) ?? new ContactDC { ContactType = ContactType.business_MobilePhone }; mobileContactInfo.ContactValue = mobile; if(customer.ContactInformations.All(a => a.ContactType != ContactType.business_MobilePhone)) { customer.ContactInformations.Add(mobileContactInfo); } var phoneContactInfo = customer.ContactInformations.FirstOrDefault(contactInformation => contactInformation.ContactType == ContactType.business_Phone) ?? new ContactDC { ContactType = ContactType.business_Phone }; phoneContactInfo.ContactValue = phone; if(customer.ContactInformations.All(a => a.ContactType != ContactType.business_Phone)) { customer.ContactInformations.Add(phoneContactInfo); } var faxContactInfo = customer.ContactInformations.FirstOrDefault(contactInformation => contactInformation.ContactType == ContactType.business_Fax) ?? new ContactDC { ContactType = ContactType.business_Fax }; faxContactInfo.ContactValue = fax; if(customer.ContactInformations.All(a => a.ContactType != ContactType.business_Fax)) { customer.ContactInformations.Add(faxContactInfo); } var eMailContactInfo = customer.ContactInformations.FirstOrDefault(contactInformation => contactInformation.ContactType == ContactType.business_Mail) ?? new ContactDC { ContactType = ContactType.business_Mail }; eMailContactInfo.ContactValue = eMailAddress; if(customer.ContactInformations.All(a => a.ContactType != ContactType.business_Mail)) { customer.ContactInformations.Add(eMailContactInfo); } customer.Notice = notice; CustomerService.UpdateCustomer(customer); return RedirectToActionPermanent("PreselectCustomer", new { customerOid = customer.CustomerOid }); } } public enum CustomerReportType { Medikamentenliste = 0, Test = 1 } }