Files
BeWoPlaner/BeWoPlanerMobil/Controllers/ReportController.cs
2020-10-07 19:42:17 +02:00

169 lines
5.8 KiB
C#

using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Razor.Parser.SyntaxTree;
using BeWo.Report.ReportObjects;
using BeWoPlanerMobil.Models;
using BeWoPlanerMobil.Service;
using BeWoPlanerMobil.Util;
using BS.Shared.Core;
using BS.Shared.DataContracts;
using BS.Shared.Extensions;
namespace BeWoPlanerMobil.Controllers
{
/*
* Quittierungsbeleg wie im Fat-Client implementieren
* Mit Unterschriftenfunktion für den Mitarbeiter.
* Liste mit allen ServiceRecords des Mitarbeiters für einen Klienten.
*
* TODO: Warnung anzeigen, dass man die betroffenen Einträge nicht mehr ändern kann.
*
* TODO: Unterschrift zu hinzufügen
*/
public class ReportController : AbstractBaseController
{
private ReportModel _Model;
public ReportModel Model
{
get
{
if(!MobileSessionFacade.IsUserLoggedIn())
{
RedirectToActionPermanent("Index", "Login");
return null;
}
if(((ReportModel) Session[ModelSessionConstants.ReportModelKey])?.Employee == null)
{
_Model = new ReportModel {Employee = MobileSessionFacade.LoggedInEmployee};
Session[ModelSessionConstants.ReportModelKey] = _Model;
}
else
{
_Model = (ReportModel) Session[ModelSessionConstants.ReportModelKey];
}
return _Model;
}
}
[Authorize]
public ActionResult Report()
{
if(!MobileSessionFacade.IsUserLoggedIn() || Request.Browser.Browser.Equals("InternetExplorer") || !AbstractModel.IsAllowedToSeeScheduler)
{
return Logout();
}
var customers = EmployeeService.GetActiveCompactCustomersForEmployee(null);
Model.Customers = customers;
var kek = new ConfirmationReceiptSignatureDC();
OperationsService.InsertConfirmationReceiptSignature(kek);
var alle = OperationsService.GetAllConfirmationReceiptSignatures();
var test = OperationsService.GetConfirmationReceiptSignaturesByEmployee(1, new DateTimeSpan(new DateTime(2020, 1, 1), new DateTime(2020, 2, 1)));
return View(Model);
}
[Authorize]
[HttpPost]
public ActionResult CreateServiceOverview(FormCollection formCollection)
{
if(Model?.Employee?.EmployeeOid == null)
{
return Logout();
}
var customerOidString = formCollection[FormCollectionConstants.SelectedCustomerOidKey];
var monthString = formCollection[FormCollectionConstants.SelectedMonthKey];
var yearString = formCollection[FormCollectionConstants.SelectedYearKey];
var isCustomerOidSuccessful = long.TryParse(customerOidString, out var customerOid);
var isMonthSuccessful = int.TryParse(monthString, out var month);
var isYearSuccessful = int.TryParse(yearString, out var year);
if(!isCustomerOidSuccessful || !isMonthSuccessful || !isYearSuccessful)
{
return Report();
}
Model.QuittierungsbelegItems.Clear();
var selectedCustomer = Model.Customers.FirstOrDefault(customer => customer.CustomerOid == customerOid);
Model.SelectedCustomer = selectedCustomer;
Model.SelectedYear = year;
Model.SelectedMonth = month;
var startDay = new DateTime(year, month, 1);
var endDay = startDay.AddMonths(1).AddDays(-1);
var reportObject = ServicesOverviewRO.Create(customerOid, month, year, false, 0L, Model.Employee.EmployeeOid.Value, 1, endDay.Day, null);
reportObject.Services.DoForEach(serviceDetail => { Model.QuittierungsbelegItems.Add(new QuittierungsbelegItem(serviceDetail)); });
if(Model.QuittierungsbelegItems.Count == 0)
{
TempData["HasWarningMessage"] = true;
}
return RedirectToActionPermanent("Report");
}
[Authorize]
public string SetCustomer(long customerOid)
{
if(Model == null)
{
return _LeerzeichenFuerGetMethoden;
}
Model.SelectedCustomer = Model.Customers.FirstOrDefault(customer => customer.CustomerOid == customerOid);
return _LeerzeichenFuerGetMethoden;
}
[Authorize]
[HttpPost]
public string CreateEmployeeSignatureForServiceOverview(string base64SignatureString, string date)
{
if(Model == null)
{
Logout();
}
var confirmationReceiptSignature = new ConfirmationReceiptSignatureDC();
var serviceRecordOids = Model.QuittierungsbelegItems.Select(item => item.ServiceRecordOid).ToList();
var serviceRecords = OperationsService.GetServiceRecordsByIds(serviceRecordOids);
confirmationReceiptSignature.ServiceRecords = serviceRecords;
confirmationReceiptSignature.EmployeeOid = Model.Employee.EmployeeOid;
using(var signatureBitmap = SignatureUtils.Base64StringToBitmap(base64SignatureString.Split(',')[1]))
{
var isValid = SignatureUtils.ValidateSignature(signatureBitmap);
if(!isValid)
{
return MobileUtils.SerializeObject("Das Unterschriftenfeld darf nicht leer sein.");
}
confirmationReceiptSignature.SignatureImage = base64SignatureString;
OperationsService.InsertConfirmationReceiptSignature(confirmationReceiptSignature);
}
return _LeerzeichenFuerGetMethoden;
}
}
}