/// <summary> /// Starts up the Carpark Rate Engine Library, loads the rates and gets the calculated rate to charge the customer for the given carpark session /// </summary> /// <param name="oInput"></param> /// <returns></returns> private string GetCalculatedRate(CPRateRQ oInput, Rates oRateCard) { // Create a return object CPRateRS oRate = new CPRateRS(); try { // Create and instance of the Rate Engine and load the Rate table var cpEngine = new CarparkRE_Lib.RateEngine(); if (cpEngine.LoadRates(oRateCard) >= 0) { // Get the Rate and Total Amount to charge the customer oRate = cpEngine.CalculateParkingCharge(oInput); } else { // Add a custom message if the rates failed to load oRate.ErrorHResult = -1; oRate.ErrorMsg = "Failed to load rates."; } } catch (Exception e) { // Record any errors in the return object oRate = new CPRateRS() { ErrorHResult = e.HResult, ErrorMsg = e.ToString() }; } // Return as a Json string return(JsonConvert.SerializeObject(oRate)); }
/// <summary> /// Determines the Rate and Amount to charge the customer for Parking /// </summary> /// <param name="oRequest">Is an Object from the CarparkRE Library that contains the EntryDateTime and ExitDateTime of the customers parking session</param> /// <returns></returns> public CPRateRS CalculateParkingCharge(CPRateRQ oRequest) { CPRateRS oRet = new CPRateRS(); try { // Make sure we have some rates loaded if (_mRates.RateCount() == 0) { return(oRet); } // Begin with the Standard Rate as the parking charge by default oRet.RateName = _mRates.StandardRates.Name; oRet.TotalPrice = GetStandardRateAmount(oRequest, GetStandardRates()); // Find any flat rates that apply List <FlatRate> lstFlatRates = GetFlatRates(oRequest, GetFlatRates()); // Select the cheapest rate for the customer, default is the Standard Rate foreach (var r in lstFlatRates) { if (r.Amount < oRet.TotalPrice) { // Overwrite the default Standard Rate with the Flat Rate instead oRet.RateName = r.Name; oRet.TotalPrice = r.Amount; } } } catch (Exception e) { // Record any errors in the return object oRet = new CPRateRS() { ErrorHResult = e.HResult, ErrorMsg = e.ToString() }; } return(oRet); }