265 lines
8.4 KiB
C#
265 lines
8.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
using BeWoPlanerMobil.Models;
|
|
using BS.Shared.Core;
|
|
using BS.Shared.DataContracts;
|
|
using BS.Shared.Extensions;
|
|
|
|
using DevExpress.XtraScheduler;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using static System.Int32;
|
|
using static System.String;
|
|
|
|
namespace BeWoPlanerMobil.Util
|
|
{
|
|
public class MobileUtils
|
|
{
|
|
public static DateTime ConvertTimeStringToDateTime(string timeString)
|
|
{
|
|
var dt = DateTime.Now.Date;
|
|
|
|
if (IsNullOrEmpty(timeString))
|
|
{
|
|
timeString = "0000";
|
|
}
|
|
|
|
var numberString = Regex.Replace(timeString, "[^0-9]", "");
|
|
|
|
if (numberString.Length < 3)
|
|
{
|
|
if (numberString.Length < 2)
|
|
{
|
|
numberString = "0" + numberString;
|
|
}
|
|
while (numberString.Length < 4)
|
|
{
|
|
numberString = numberString + "0";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
while (numberString.Length < 4)
|
|
{
|
|
numberString = "0" + numberString;
|
|
}
|
|
}
|
|
|
|
if (numberString.Length > 4)
|
|
{
|
|
numberString = numberString.Substring(0, 4);
|
|
}
|
|
|
|
var hours = Convert.ToInt32(numberString.Substring(0, 2));
|
|
var minutes = Convert.ToInt32(numberString.Substring(2, 2));
|
|
|
|
dt = dt.AddHours(hours);
|
|
dt = dt.AddMinutes(minutes);
|
|
|
|
return dt;
|
|
}
|
|
|
|
public static DateTime[] ConvertRecordTimes(string start, string ende, DateTime datum, int dauer)
|
|
{
|
|
var result = new DateTime[2];
|
|
var startDate = datum.Date;
|
|
var endDate = datum.Date;
|
|
|
|
|
|
var dateTemp = ConvertTimeStringToDateTime(start);
|
|
startDate = startDate.AddHours(dateTemp.Hour).AddMinutes(dateTemp.Minute);
|
|
|
|
dateTemp = ConvertTimeStringToDateTime(ende);
|
|
endDate = endDate.AddHours(dateTemp.Hour).AddMinutes(dateTemp.Minute);
|
|
|
|
if (IsNullOrEmpty(start) && !IsNullOrEmpty(ende))
|
|
{
|
|
startDate = endDate.AddMinutes(-1 * dauer);
|
|
}
|
|
|
|
if (IsNullOrEmpty(ende) && !IsNullOrEmpty(start))
|
|
{
|
|
endDate = startDate.AddMinutes(dauer);
|
|
}
|
|
|
|
if (endDate < startDate)
|
|
{
|
|
endDate = startDate;
|
|
}
|
|
|
|
result[0] = startDate;
|
|
result[1] = endDate;
|
|
|
|
return result;
|
|
}
|
|
|
|
public static DateTime[] ConvertRecordTimesWithEndDate(string start, string ende, DateTime datum, DateTime enddatum, int dauer)
|
|
{
|
|
var result = new DateTime[2];
|
|
var startDate = datum;
|
|
var endDate = enddatum;
|
|
|
|
var h = 0;
|
|
var m = 0;
|
|
|
|
|
|
var dateStartTime = ConvertTimeStringToDateTime(start);
|
|
startDate = startDate.AddHours(dateStartTime.Hour).AddMinutes(dateStartTime.Minute);
|
|
|
|
var dateEndTime = ConvertTimeStringToDateTime(ende);
|
|
endDate = endDate.AddHours(dateEndTime.Hour).AddMinutes(dateEndTime.Minute);
|
|
|
|
|
|
|
|
endDate = endDate.AddHours(h);
|
|
endDate = endDate.AddMinutes(m);
|
|
|
|
if (IsNullOrEmpty(start) && !IsNullOrEmpty(ende))
|
|
{
|
|
startDate = endDate.AddMinutes(-1 * dauer);
|
|
}
|
|
|
|
if (IsNullOrEmpty(ende) && !IsNullOrEmpty(start))
|
|
{
|
|
endDate = startDate.AddMinutes(dauer);
|
|
}
|
|
|
|
if (endDate < startDate)
|
|
{
|
|
endDate = startDate;
|
|
}
|
|
|
|
result[0] = startDate;
|
|
result[1] = endDate;
|
|
|
|
return result;
|
|
}
|
|
|
|
public static string SerializeObject(object obj)
|
|
{
|
|
return JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new ShouldSerializeContractResolver(), ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
|
|
}
|
|
|
|
public static string GetSettingValue(string settings, string key)
|
|
{
|
|
if (!IsNullOrEmpty(settings))
|
|
{
|
|
if (settings.IndexOf(";") == -1 && settings.IndexOf("=") == -1)
|
|
{
|
|
return settings;
|
|
}
|
|
|
|
var keyValuePairs = settings.Split(';');
|
|
|
|
return (from pair in keyValuePairs select pair.Split('=') into keyValuePair where keyValuePair.Length == 2 where keyValuePair[0] == key select keyValuePair[1]).FirstOrDefault();
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static RecurrenceInformation GetOccurrenceId(string pRecurrenceInfoString)
|
|
{
|
|
var regex = new Regex("Index=\"[0-9]+\"");
|
|
|
|
var match = regex.Match(pRecurrenceInfoString);
|
|
|
|
var recurrenceInfo = new RecurrenceInfo();
|
|
recurrenceInfo.FromXml(pRecurrenceInfoString);
|
|
|
|
var index = 0;
|
|
|
|
if (!match.Value.IsNullOrEmpty())
|
|
{
|
|
index = Parse(match.Value.Split('"')[1]);
|
|
}
|
|
|
|
return new RecurrenceInformation(recurrenceInfo.Id.ToString(), index);
|
|
}
|
|
|
|
public static bool IsBillable(long serviceDescriptionOid, List<ServiceDescriptionDC> serviceDescriptions)
|
|
{
|
|
var description = serviceDescriptions.FirstOrDefault(f => f.ServiceDescriptionOid.Equals(serviceDescriptionOid));
|
|
|
|
return description != null && description.Category.IsBillable;
|
|
}
|
|
|
|
public static decimal? GetDecimalValue(decimal? decValue, int decimalPlaces)
|
|
{
|
|
return decValue != null ? Math.Round(decValue.Value, decimalPlaces, MidpointRounding.AwayFromZero) : decValue;
|
|
}
|
|
|
|
public static decimal? GetDecimalValueWithMaxDecimal(decimal? decValue, int maxDecimal)
|
|
{
|
|
if (decValue != null)
|
|
{
|
|
var decValueStr = decValue.ToString();
|
|
decValueStr = decValueStr.Replace(".", ",");
|
|
|
|
while (decValueStr.Length > 0 && decValueStr.IndexOf(",") > 0 && (decValueStr.EndsWith("0") || decValueStr.EndsWith(",")))
|
|
{
|
|
decValueStr = decValueStr.Substring(0, decValueStr.Length - 1);
|
|
}
|
|
|
|
var decCount = 0;
|
|
if (decValueStr.IndexOf(",") >= 0)
|
|
{
|
|
decCount = decValueStr.Length - decValueStr.IndexOf(",") - 1;
|
|
}
|
|
|
|
if (maxDecimal >= 0 && maxDecimal < decCount)
|
|
{
|
|
decCount = maxDecimal;
|
|
}
|
|
|
|
return GetDecimalValue(decValue, decCount);
|
|
}
|
|
|
|
return decValue;
|
|
}
|
|
|
|
public static JSONCustomer GetJSONCustomer(CustomerDC pCustomer)
|
|
{
|
|
return new JSONCustomer(pCustomer);
|
|
}
|
|
|
|
public static List<ServiceCategoryModel> CreateServiceCategoryModels(IEnumerable<ServiceDescriptionDC> serviceDescriptions)
|
|
{
|
|
var catOid2ModelDict = new Dictionary<long, ServiceCategoryModel>();
|
|
foreach (var sd in serviceDescriptions)
|
|
{
|
|
if (!catOid2ModelDict.ContainsKey(sd.Category.ServiceCategoryOid.Value))
|
|
{
|
|
catOid2ModelDict[sd.Category.ServiceCategoryOid.Value] = new ServiceCategoryModel
|
|
{
|
|
Name = sd.Category.Name,
|
|
IsDefault = sd.Category.IsDefault,
|
|
Percentage = sd.Category.Percentage,
|
|
Position = sd.Category.Position,
|
|
OhneHilfeplan = sd.Category.OhneHilfeplan,
|
|
ServiceCategoryOid = sd.Category.ServiceCategoryOid,
|
|
ServiceDescriptions = new List<ServiceDescriptionDC>()
|
|
};
|
|
}
|
|
|
|
catOid2ModelDict[sd.Category.ServiceCategoryOid.Value].ServiceDescriptions.Add(sd);
|
|
}
|
|
|
|
var list = catOid2ModelDict.Values.ToList();
|
|
|
|
list.Sort((s1, s2) =>
|
|
{
|
|
if (s1.IsDefault)
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
return s1.Position != s2.Position ? s1.Position.CompareTo(s2.Position) : s1.ServiceCategoryOid.Value.CompareTo(s2.ServiceCategoryOid.Value);
|
|
});
|
|
|
|
return list;
|
|
}
|
|
}
|
|
} |