Files
BeWoPlaner/BeWoPlanerMobil/Controllers/CustomerController.cs
2020-02-07 15:21:36 +01:00

202 lines
6.8 KiB
C#

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<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>();
}
Model.AbsenceReasons = CustomerService.GetAllAbsenceReasons();
}
[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");
}
public string GetSelectedCustomer(long customerOid)
{
return Model == null ? "SessionTimeout" : SerializeObject(GetJSONCustomer(Model.SelectedCustomer));
}
[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");
}
[Authorize]
[HttpPost]
public ActionResult CreateAbsenceTime(FormCollection formCollection)
{
if (Model?.SelectedCustomerOid == null)
{
return RedirectToActionPermanent("Index", "Login");
}
try
{
var startDateRaw = formCollection["absence-time-start-input"];
var endDateRaw = formCollection["absence-time-end-input"];
var absenceReasonOidRaw = formCollection["SelectedAbsenceReasonOid"];
var notice = formCollection["absencetime-notice-input"];
DateTime? endDate = null;
DateTime? startDate = null;
if (!string.IsNullOrWhiteSpace(startDateRaw))
{
if (DateTime.TryParse(startDateRaw, out var startDateTime))
{
startDate = startDateTime;
}
}
else
{
return RedirectToActionPermanent("Customer");
}
if (!string.IsNullOrWhiteSpace(endDateRaw))
{
if (DateTime.TryParse(endDateRaw, out var endDateTime))
{
endDate = endDateTime;
}
}
AbsenceReasonDC absenceReason = null;
if (!string.IsNullOrWhiteSpace(absenceReasonOidRaw))
{
if (long.TryParse(absenceReasonOidRaw, out var absenceReasonOid))
{
absenceReason = CustomerService.LoadAbsenceReason(absenceReasonOid);
}
}
if (absenceReason != null)
{
var absenceTime = new AbsenceTimeDC
{
CustomerOid = Model.SelectedCustomerOid,
End = endDate,
Start = startDate,
Notice = notice,
Reason = absenceReason
};
CustomerService.InsertAbsenceTimes(new List<AbsenceTimeDC> {absenceTime});
}
}
catch (Exception exception)
{
Log.Error(exception.Message, exception);
}
return RedirectToActionPermanent("PreselectCustomer", new {customerOid = Model.SelectedCustomerOid});
}
}
}