narsil/Tanshu.Accounts.Repository/Cache.cs
2018-08-24 16:11:33 +05:30

115 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using Tanshu.Accounts.Entities;
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 bool _log = false;
public static string Location
{
get
{
if (string.IsNullOrEmpty(_location))
{
var loc = MachineLocationBI.Get(_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()))
{
var loc = PrintLocationBI.Get(Location);
_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()))
{
var loc = PrintLocationBI.Get(Location, productGroupID);
_locations.Add(Location.GetHashCode() ^ productGroupID.GetHashCode(), loc);
}
return _locations[Location.GetHashCode() ^ productGroupID.GetHashCode()];
}
public static IList<ProductGroup> ProductGroups()
{
if (_productGroups == null)
{
var list = ProductGroupBI.SaleList();
_productGroups = list;
foreach (var item in _productGroups)
{
for (var i = item.Products.Count - 1; i >= 0; i--)
{
if (item.Products[i].HasHappyHour)
{
var product = item.Products[i];
product.HasHappyHour = false;
item.Products.Insert(i + 1, new Product()
{
ProductID = product.ProductID,
Name = product.FullName,
Units = product.Units,
ProductGroup = product.ProductGroup,
Vat = product.Vat,
ServiceTax = product.ServiceTax,
ServiceCharge = product.ServiceCharge,
IsScTaxable = product.IsScTaxable,
Price = product.Price,
HasHappyHour = true,
IsActive = product.IsActive,
IsNotAvailable = product.IsNotAvailable,
SortOrder = product.SortOrder,
Quantity = product.Quantity
});
}
}
}
}
return _productGroups;
}
public static IList<Modifier> ProductGroupModifiers(Guid productGroupID)
{
if (!_modifiers.ContainsKey(productGroupID))
{
var list = ModifierBI.List(productGroupID);
_modifiers.Add(productGroupID, list);
}
return _modifiers[productGroupID];
}
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; }
}
}
}