narsil/Tanshu.Accounts.Repository/Cache.cs
tanshu caf9b3106c Feature: Added Machine Locations so that setting the location in the config file is not needed.
Feature: Settings database table added to store string based settings.
         It is right now used to store bill header and footer.
         Hard Coded header/footer removed from file.
Feature: Tax Analysis form created to easily show the tax calculation.
Feature: Management form uses background workers.  Dont' know if it is functional though.
Chore: Reorder Table form moved to masters from sales folder.
Refactor: ManagementBI and SalesAnalysisBI
2016-01-04 10:52:01 +05:30

125 lines
4.3 KiB
C#

using System;
using System.Collections.Generic;
using NHibernate;
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.Entities.Auth;
namespace Tanshu.Accounts.Repository
{
public class Cache
{
private static IList<ProductGroup> _productGroups = null;
private static Dictionary<Guid, IList<Modifier>> _modifiers = new Dictionary<Guid, IList<Modifier>>();
private static string _machine = Environment.MachineName;
private static string _location = null;
private static Dictionary<int, PrintLocation> _locations = new Dictionary<int, PrintLocation>();
private static IList<Role> _roles = null;
private static bool _log = false;
public static string Location
{
get
{
if (string.IsNullOrEmpty(_location))
{
using (var bi = new MachineLocationBI())
{
var loc = bi.Get(x => x.Machine == _machine);
if (loc != null)
_location = loc.Location;
}
}
return _location;
}
}
public static PrintLocation BasePrinter
{
get
{
if (string.IsNullOrEmpty(Location))
throw new ArgumentException("No location for Machine");
if (!_locations.ContainsKey(Location.GetHashCode()))
{
using (var bi = new PrintLocationBI())
{
var loc = bi.Get(x => x.Location == Location && x.ProductGroup == null);
_locations.Add(Location.GetHashCode(), loc);
}
}
return _locations[Location.GetHashCode()];
}
}
public static PrintLocation KotPrinter(Guid productGroupID)
{
if (string.IsNullOrEmpty(Location))
throw new ArgumentException("No location for Machine");
if (!_locations.ContainsKey(Location.GetHashCode() ^ productGroupID.GetHashCode()))
{
using (var bi = new PrintLocationBI())
{
var loc = bi.Get(x => x.Location == Location && x.ProductGroup.ProductGroupID == productGroupID) ??
bi.Get(x => x.Location == Location && x.ProductGroup == null);
_locations.Add(Location.GetHashCode() ^ productGroupID.GetHashCode(), loc);
}
}
return _locations[Location.GetHashCode() ^ productGroupID.GetHashCode()];
}
public static IList<ProductGroup> ProductGroups()
{
if (_productGroups == null)
{
using (var bi = new ProductGroupBI())
{
var list = bi.SaleList();
foreach (var item in list)
{
NHibernateUtil.Initialize(item.Products);
}
_productGroups = list;
}
}
return _productGroups;
}
public static IList<Modifier> ProductGroupModifiers(Guid productGroupID)
{
if (!_modifiers.ContainsKey(productGroupID))
{
using (var bi = new ProductGroupModifierBI())
{
var list = bi.List(productGroupID);
_modifiers.Add(productGroupID, list);
}
}
return _modifiers[productGroupID];
}
public static IList<Role> UserRoles(Guid userID)
{
if (_roles == null)
{
using (var bi = new UserBI())
{
_roles = bi.Roles(userID);
}
}
return _roles;
}
public static void ClearRoles()
{
_roles = null;
}
public static void Invalidate()
{
_productGroups = null;
_machine = Environment.MachineName;
_location = null;
_locations = new Dictionary<int, PrintLocation>();
_modifiers = new Dictionary<Guid, IList<Modifier>>();
}
public static bool Log
{
get { return _log; }
set { _log = value; }
}
}
}