95 lines
3.1 KiB
C#
95 lines
3.1 KiB
C#
using EnVisage.Code.BLL;
|
|
using EnVisage.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Entity;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
|
|
namespace EnVisage.Code
|
|
{
|
|
public class RateManager
|
|
{
|
|
private readonly EnVisageEntities _dbContext;
|
|
private readonly bool _isContexLocal = false;
|
|
|
|
public RateManager(EnVisageEntities dbContext)
|
|
{
|
|
if (dbContext == null)
|
|
{
|
|
_dbContext = new EnVisageEntities();
|
|
_isContexLocal = true;
|
|
}
|
|
else
|
|
{
|
|
_dbContext = dbContext;
|
|
}
|
|
}
|
|
public void Dispose()
|
|
{
|
|
if (_isContexLocal)
|
|
_dbContext.Dispose();
|
|
}
|
|
|
|
#region Public Methods
|
|
/// <summary>
|
|
/// Loads a Rate from the database.
|
|
/// </summary>
|
|
/// <param name="value">Unique identifier of the Rate .</param>
|
|
/// <param name="isReadOnly">Indicates that object will not be saved later in the code. Use <b>false</b> if you need to save an updated object.</param>
|
|
/// <returns>A <see cref="Rate"/> object retrieved from database.</returns>
|
|
public Rate Load(Guid? value, bool isReadOnly = true)
|
|
{
|
|
if (value == null || value == Guid.Empty)
|
|
return new Rate();
|
|
|
|
return isReadOnly
|
|
? _dbContext.Rates.AsNoTracking().FirstOrDefault(t => t.Id == value)
|
|
: _dbContext.Rates.Find(value);
|
|
}
|
|
|
|
public void Save(RateModel model)
|
|
{
|
|
if (model == null)
|
|
throw new ArgumentNullException("model");
|
|
|
|
#region Save Rate data
|
|
Rate dbObj = null;
|
|
if (model.Id != Guid.Empty)
|
|
dbObj = _dbContext.Rates.Find(model.Id);
|
|
if (dbObj == null)
|
|
{
|
|
dbObj = new Rate { Id = Guid.NewGuid() };
|
|
}
|
|
model.CopyTo(dbObj);
|
|
if (model.Id == Guid.Empty)
|
|
_dbContext.Rates.Add(dbObj);
|
|
else
|
|
_dbContext.Entry(dbObj).State = EntityState.Modified;
|
|
#endregion
|
|
|
|
#region Update Related Scenarios
|
|
var scenarioManager = new ScenarioManager(_dbContext);
|
|
scenarioManager.ApplyRateAndRecalculateScenarios(dbObj);
|
|
#endregion
|
|
|
|
if (_isContexLocal)
|
|
_dbContext.SaveChanges();
|
|
}
|
|
|
|
[Obsolete("This method is called in loops - this is not acceptable. Replace with single non-db dictionary loop")]
|
|
public decimal GetRateValue(Guid expenditureCategoryId, DateTime currentDate)
|
|
{
|
|
if(expenditureCategoryId == Guid.Empty)
|
|
throw new ArgumentException("Parameter 'expenditureCatetoryId' can not be Guid.Empty");
|
|
|
|
var rate = _dbContext.Rates
|
|
.FirstOrDefault(x => x.Type == (short)EnVisage.Models.RateModel.RateType.Global && x.ExpenditureCategoryId == expenditureCategoryId
|
|
&& currentDate >= x.StartDate && currentDate <= x.EndDate);
|
|
|
|
return rate != null ? rate.Rate1 : 0;
|
|
}
|
|
#endregion
|
|
}
|
|
} |