Files
BeWoPlaner/BeWoPlanerMobil/Controllers/CustomerController.cs
Lyndon Jetten 63d9844e3f BeWoPlanerMobil:
Beim Bearbeiten von Zeiterfassungseinträgen ohne Zeitangaben wird das Datum nicht mehr auf 0 Uhr gesetzt
2020-09-21 11:57:10 +02:00

124 lines
4.1 KiB
C#

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())
{
RedirectToActionPermanent("Index", "Login");
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 RedirectToActionPermanent("Index", "Login");
}
InitViewModel();
return View(Model);
}
private void InitViewModel()
{
var myRelatedCustomers = Model.Employee == null ? new List<CustomerEmployeeRelationDC>() : 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<CompactCustomerDC>();
}
}
[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[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");
}
}
}