diff --git a/server/biz/SubscriptionBiz.cs b/server/biz/SubscriptionBiz.cs index 5c86297..99a07b3 100644 --- a/server/biz/SubscriptionBiz.cs +++ b/server/biz/SubscriptionBiz.cs @@ -237,28 +237,26 @@ namespace Sockeye.Biz await Task.CompletedTask; } - //////////////////////////////////////////////////////////////////////////////////////////////// - // - // - // - internal async Task SubscriptionGetForReportingAsync(SockType sType, long id) - { - //if it's the entire workorder just get, populate and return as normal - if (sType == SockType.Subscription) - return await GetAsync(id, false); - var subId = await ct.SubscriptionItem.AsNoTracking().Where(z => z.Id == id).Select(z => z.SubscriptionId).FirstOrDefaultAsync(); - return await GetAsync(subId, false); - - } //////////////////////////////////////////////////////////////////////////////////////////////// //REPORTING // public async Task GetReportData(DataListSelectedRequest dataListSelectedRequest, Guid jobId) + { + if (dataListSelectedRequest.SockType == SockType.Subscription) + return await GetSubscriptionsReportData(dataListSelectedRequest, jobId); + else + //subscription items + return await GetSubscriptionItemsReportData(dataListSelectedRequest, jobId); + } + + + public async Task GetSubscriptionsReportData(DataListSelectedRequest dataListSelectedRequest, Guid jobId) { var idList = dataListSelectedRequest.SelectedRowIds; JArray ReportData = new JArray(); + List batchResults = new List(); while (idList.Any()) { @@ -270,7 +268,8 @@ namespace Sockeye.Biz foreach (long batchId in batch) { if (!ReportRenderManager.KeepGoing(jobId)) return null; - batchResults.Add(await SubscriptionGetForReportingAsync(dataListSelectedRequest.SockType, batchId)); + //var subId = await ct.SubscriptionItem.AsNoTracking().Where(z => z.Id == id).Select(z => z.SubscriptionId).FirstOrDefaultAsync(); + batchResults.Add(await GetAsync(batchId, false)); } //order the results back into original @@ -287,8 +286,6 @@ namespace Sockeye.Biz { if (!ReportRenderManager.KeepGoing(jobId)) return null; var jo = JObject.FromObject(w); - if (!JsonUtil.JTokenIsNullOrEmpty(jo["CustomFields"])) - jo["CustomFields"] = JObject.Parse((string)jo["CustomFields"]); ReportData.Add(jo); } orderedList = null; @@ -296,6 +293,51 @@ namespace Sockeye.Biz vc.Clear(); return ReportData; } + + public async Task GetSubscriptionItemsReportData(DataListSelectedRequest dataListSelectedRequest, Guid jobId) + { + var idList = dataListSelectedRequest.SelectedRowIds; + JArray ReportData = new JArray(); + + List batchResults = new List(); + while (idList.Any()) + { + if (!ReportRenderManager.KeepGoing(jobId)) return null; + + var batch = idList.Take(IReportAbleObject.REPORT_DATA_BATCH_SIZE); + idList = idList.Skip(IReportAbleObject.REPORT_DATA_BATCH_SIZE).ToArray(); + batchResults.Clear(); + foreach (long batchId in batch) + { + if (!ReportRenderManager.KeepGoing(jobId)) return null; + //var subId = .Select(z => z.SubscriptionId).FirstOrDefaultAsync(); + var subItem = await ct.SubscriptionItem.AsNoTracking().Where(z => z.Id == batchId).FirstOrDefaultAsync(); + await PopulateItemVizFields(subItem, 0); + batchResults.Add(subItem); + } + + //order the results back into original + //What is happening here: + //for performance the query is batching a bunch at once by fetching a block of items from the sql server + //however it's returning in db order which is often not the order the id list is in + //so it needs to be sorted back into the same order as the ide list + //This would not be necessary if just fetching each one at a time individually (like in workorder get report data) + + var orderedList = from id in batch join z in batchResults on id equals z.Id select z; + batchResults = null; + + foreach (SubscriptionItem w in orderedList) + { + if (!ReportRenderManager.KeepGoing(jobId)) return null; + var jo = JObject.FromObject(w); + ReportData.Add(jo); + } + orderedList = null; + } + vc.Clear(); + return ReportData; + } + private VizCache vc = new VizCache(); @@ -310,20 +352,50 @@ namespace Sockeye.Biz foreach (var item in o.Items)//some subscriptions have a bunch of the same monthly or yearly raven user in them so this will save in that case { - if (!vc.Has("productname", item.ProductId)) - { - var productInfo = await ct.Product.AsNoTracking().Where(x => x.Id == item.ProductId).FirstOrDefaultAsync(); - vc.Add(productInfo.Name, "productname", item.ProductId); - vc.Add(productInfo.InitialPrice.ToString(), "productinitialprice", item.ProductId); - vc.Add(productInfo.RenewPrice.ToString(), "productrenewprice", item.ProductId); + await PopulateItemVizFields(item, o.CustomerId); + // if (!vc.Has("productname", item.ProductId)) + // { + // var productInfo = await ct.Product.AsNoTracking().Where(x => x.Id == item.ProductId).FirstOrDefaultAsync(); + // vc.Add(productInfo.Name, "productname", item.ProductId); + // vc.Add(productInfo.InitialPrice.ToString(), "productinitialprice", item.ProductId); + // vc.Add(productInfo.RenewPrice.ToString(), "productrenewprice", item.ProductId); - } - item.ProductViz = vc.Get("productname", item.ProductId); - item.RenewPriceViz = vc.GetAsDecimal("productrenewprice", item.ProductId) ?? 0; - item.InitialPriceViz = vc.GetAsDecimal("productinitialprice", item.ProductId) ?? 0; + // } + // item.ProductViz = vc.Get("productname", item.ProductId); + // item.RenewPriceViz = vc.GetAsDecimal("productrenewprice", item.ProductId) ?? 0; + // item.InitialPriceViz = vc.GetAsDecimal("productinitialprice", item.ProductId) ?? 0; } } + //populate viz fields from provided object + private async Task PopulateItemVizFields(SubscriptionItem o, long CustomerId) + { + + if (CustomerId == 0) + CustomerId = await ct.Subscription.Where(z => z.Id == o.SubscriptionId).Select(z => z.CustomerId).FirstOrDefaultAsync(); + + if (CustomerId != 0) + { + if (!vc.Has("customer", CustomerId)) + { + vc.Add(await ct.Customer.AsNoTracking().Where(x => x.Id == CustomerId).Select(x => x.Name).FirstOrDefaultAsync(), "customer", CustomerId); + } + o.CustomerViz = vc.Get("customer", CustomerId); + } + + if (!vc.Has("productname", o.ProductId)) + { + var productInfo = await ct.Product.AsNoTracking().Where(x => x.Id == o.ProductId).FirstOrDefaultAsync(); + vc.Add(productInfo.Name, "productname", o.ProductId); + vc.Add(productInfo.InitialPrice.ToString(), "productinitialprice", o.ProductId); + vc.Add(productInfo.RenewPrice.ToString(), "productrenewprice", o.ProductId); + } + o.ProductViz = vc.Get("productname", o.ProductId); + o.RenewPriceViz = vc.GetAsDecimal("productrenewprice", o.ProductId) ?? 0; + o.InitialPriceViz = vc.GetAsDecimal("productinitialprice", o.ProductId) ?? 0; + + } + //////////////////////////////////////////////////////////////////////////////////////////////// // IMPORT EXPORT // diff --git a/server/models/SubscriptionItem.cs b/server/models/SubscriptionItem.cs index 8547e00..e758248 100644 --- a/server/models/SubscriptionItem.cs +++ b/server/models/SubscriptionItem.cs @@ -39,6 +39,9 @@ namespace Sockeye.Models [NotMapped] public string ProductViz { get; set; } + [NotMapped] + public string CustomerViz { get; set; } + public List Tags { get; set; } = new List();