public BillingCycle FetchOrCreateCurrentBillingCycle() { BillingCycle cycle = GetBillingCycle(0); if (cycle != null) { //check that cycle is still current if (cycle.StartDate.Month != this.GetCurrentTime().Month) { cycle = null; //this will invoke cache refresh and creation of new billing cycle } } if (cycle == null) //create a new cycle { DalBillingCycle dal = new DalBillingCycle(this, this.storageAccount); cycle = dal.GetLatestBillingCycle(); if (cycle == null) //create a new cycle, since persisted storage does not contain any cycle { DateTime currentDate = this.GetCurrentTime(); DateTime startDate = new DateTime(currentDate.Year, currentDate.Month, 1); DateTime endDate = new DateTime(currentDate.Year, currentDate.Month, 28); //using 28 since it exists in every month, leap year etc cycle = new BillingCycle { Id = Guid.NewGuid(), StartDate = startDate, EndDate = endDate }; dal.SaveBillingCycle(cycle); string cacheKey = string.Format("{0}#{1}", cycle.StartDate.Year.ToString(), cycle.StartDate.Month.ToString()); billingCycleCache.AddObject(cacheKey, cycle); //add to cache } } return(cycle); }
/// <summary> /// Fetch a given cycle /// where cycleNumber = 0, implies current billing cycle /// -1, previous cycle /// -2, previous to previous and so on /// </summary> /// <param name="cycleNumber"></param> /// <returns></returns> public BillingCycle GetBillingCycle(int cycleNumber, bool forceCacheRefresh) { if (cycleNumber > 0 || cycleNumber < -11) { throw new ArgumentException("Cycles from current to last 1 year are supported"); } int currentMonth = this.GetCurrentTime().Month; //since cycle number is counted -ve backwards, so need to add to current month int requestedCycleMonth = currentMonth + cycleNumber; int requestedYear = this.GetCurrentTime().Year; if (requestedCycleMonth < 1) { //wrap around of 1 year requestedCycleMonth += 12; requestedYear--; } string cacheKey = string.Format("{0}#{1}", requestedYear, requestedCycleMonth); BillingCycle cycle = billingCycleCache.GetObject(cacheKey) as BillingCycle; if (cycle == null || forceCacheRefresh) { //fetch from table storage DalBillingCycle dal = new DalBillingCycle(this, this.storageAccount); cycle = dal.GetBillingCycle(requestedYear, requestedCycleMonth); } if (cycle != null) //add to cache { billingCycleCache.AddObject(cacheKey, cycle); } else if (forceCacheRefresh) { //dummy up the cache entry simulating discarding the entry billingCycleCache.AddObject(cacheKey, "dummy"); } return(cycle); }
public bool UpdateBillingCycleStatus(int year, int month, int status) { DalBillingCycle dal = new DalBillingCycle(this, this.storageAccount); return(dal.UpdateBillingCycleStatus(year, month, status)); }