118 lines
4.4 KiB
C#
118 lines
4.4 KiB
C#
using EnVisage.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Entity;
|
|
using System.Linq;
|
|
using System.Web;
|
|
|
|
namespace EnVisage.Code.BLL
|
|
{
|
|
public class ExpenditureCategoryManager
|
|
{
|
|
private readonly EnVisageEntities _dbContext;
|
|
private readonly bool _isContexLocal = false;
|
|
|
|
public ExpenditureCategoryManager(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 Expenditure Category from the database.
|
|
/// </summary>
|
|
/// <param name="value">Unique identifier of the Expenditure Category.</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="ExpenditureCategory"/> object retrieved from database.</returns>
|
|
public ExpenditureCategory Load(Guid? value, bool isReadOnly = true)
|
|
{
|
|
if (value == null || value == Guid.Empty)
|
|
return new ExpenditureCategory();
|
|
|
|
return isReadOnly
|
|
? _dbContext.ExpenditureCategory.AsNoTracking().FirstOrDefault(t => t.Id == value)
|
|
: _dbContext.ExpenditureCategory.Find(value);
|
|
}
|
|
|
|
public void Save(ExpenditureCategoryModel model)
|
|
{
|
|
if (model == null)
|
|
throw new ArgumentNullException("model");
|
|
|
|
#region Save ExpenditureCategory data
|
|
ExpenditureCategory dbObj = null;
|
|
if (model.Id != Guid.Empty)
|
|
dbObj = _dbContext.ExpenditureCategory.Find(model.Id);
|
|
if (dbObj == null)
|
|
{
|
|
dbObj = new ExpenditureCategory { Id = Guid.NewGuid() };
|
|
}
|
|
model.CopyTo(dbObj);
|
|
dbObj.CGEFX = "CG";
|
|
if (model.Id == Guid.Empty)
|
|
_dbContext.ExpenditureCategory.Add(dbObj);
|
|
else
|
|
_dbContext.Entry(dbObj).State = EntityState.Modified;
|
|
#endregion
|
|
|
|
if (_isContexLocal)
|
|
_dbContext.SaveChanges();
|
|
}
|
|
|
|
public List<Tuple<Guid, string, decimal>> GetCategoriesWithUoMValues()
|
|
{
|
|
// SA. ENV-839
|
|
var query = (from e in _dbContext.ExpenditureCategory.AsNoTracking()
|
|
join f in _dbContext.VW_ExpenditureCategory.AsNoTracking() on e.Id equals f.Id
|
|
orderby f.ExpCategoryWithCcName
|
|
select new
|
|
{
|
|
Id = e.Id,
|
|
Name = f.ExpCategoryWithCcName,
|
|
UoMValue = e.UOM.UOMValue });
|
|
List<Tuple<Guid, string, decimal>> ret = new List<Tuple<Guid,string,decimal>>();
|
|
foreach (var item in query)
|
|
{
|
|
ret.Add(new Tuple<Guid, string, decimal>(item.Id, item.Name, item.UoMValue));
|
|
}
|
|
return ret;
|
|
}
|
|
public Dictionary<string, ExpenditureDetail> GetAllCategories()
|
|
{
|
|
// SA. ENV-839
|
|
var query = (from e in _dbContext.ExpenditureCategory.AsNoTracking()
|
|
join f in _dbContext.VW_ExpenditureCategory.AsNoTracking() on e.Id equals f.Id
|
|
orderby f.ExpCategoryWithCcName
|
|
select new ExpenditureDetail
|
|
{
|
|
ExpenditureCategoryId = e.Id,
|
|
ExpenditureCategoryName = f.ExpCategoryWithCcName,
|
|
UOMId = e.UOMId,
|
|
UOMValue = e.UOM.UOMValue,
|
|
CGEFX = e.CGEFX,
|
|
CreditId = e.CreditId,
|
|
GLId = e.GLId,
|
|
SystemAttributeOne = e.SystemAttributeOne,
|
|
SystemAttributeTwo = e.SystemAttributeTwo,
|
|
Type = e.Type ?? 0,
|
|
UseType = e.UseType ?? 1,
|
|
|
|
});
|
|
return query.ToDictionary(t=>t.ExpenditureCategoryId.ToString());
|
|
}
|
|
#endregion
|
|
}
|
|
} |