public async Task <bool> Process(CommerceContext commerceContext) { using (CommandActivity.Start(commerceContext, this)) { var customerTotals = new Dictionary <string, OrdersTotals>(); try { var orderTotals = await this._commerceCommander.GetEntity <OrdersTotals>(commerceContext, "Orders_Totals", true); if (!orderTotals.IsPersisted) { orderTotals.Id = "Orders_Totals"; } orderTotals.LastRunStarted = DateTimeOffset.UtcNow; orderTotals.LastRunEnded = DateTimeOffset.MinValue; orderTotals.MonitorCycle++; var batchSize = 300; var arg = new FindEntitiesInListArgument(typeof(CommerceEntity), "Orders", System.Convert.ToInt32(orderTotals.LastSkip), batchSize); var ordersBatch = await this._commerceCommander.Pipeline <FindEntitiesInListPipeline>().Run(arg, commerceContext.GetPipelineContextOptions()); while (ordersBatch.List.Items.Count > 0) { var returnedOrders = ordersBatch.List.Items.OfType <Order>(); foreach (var order in returnedOrders) { orderTotals.Totals.GrandTotal.Amount = orderTotals.Totals.GrandTotal.Amount + order.Totals.GrandTotal.Amount; orderTotals.Totals.SubTotal.Amount = orderTotals.Totals.SubTotal.Amount + order.Totals.SubTotal.Amount; orderTotals.Totals.PaymentsTotal.Amount = orderTotals.Totals.PaymentsTotal.Amount + order.Totals.PaymentsTotal.Amount; orderTotals.Totals.AdjustmentsTotal.Amount = orderTotals.Totals.AdjustmentsTotal.Amount + order.Totals.AdjustmentsTotal.Amount; foreach (var adjustment in order.Adjustments) { var adjustmentType = orderTotals.Adjustments.FirstOrDefault(p => p.Name == adjustment.AdjustmentType); if (adjustmentType == null) { adjustmentType = new TotalsAdjustmentsModel { Name = adjustment.AdjustmentType }; orderTotals.Adjustments.Add(adjustmentType); } adjustmentType.Adjustment.Amount = adjustmentType.Adjustment.Amount + adjustment.Adjustment.Amount; } if (order.HasComponent <ContactComponent>()) { OrdersTotals orderTotalsByCust = null; var orderContactComponent = order.GetComponent <ContactComponent>(); var customerTotalsId = $"Order_Totals_ByCust_{orderContactComponent.CustomerId.Replace("Entity-Customer-", "")}"; if (customerTotals.ContainsKey(customerTotalsId)) { orderTotalsByCust = customerTotals[customerTotalsId]; } else { orderTotalsByCust = await this._commerceCommander .GetEntity <OrdersTotals>(commerceContext, customerTotalsId, true); } if (!orderTotalsByCust.IsPersisted) { orderTotalsByCust.Id = customerTotalsId; } else { if (orderTotalsByCust.OrderCount == 0 || orderTotalsByCust.MonitorCycle != orderTotals.MonitorCycle) { orderTotalsByCust.Totals.GrandTotal.Amount = 0; orderTotalsByCust.Totals.SubTotal.Amount = 0; orderTotalsByCust.Totals.PaymentsTotal.Amount = 0; orderTotalsByCust.Totals.AdjustmentsTotal.Amount = 0; orderTotalsByCust.MonitorCycle = orderTotals.MonitorCycle; orderTotalsByCust.Adjustments.Clear(); } } orderTotalsByCust.OrderCount++; orderTotalsByCust.LastSkip = orderTotalsByCust.LastSkip + batchSize; orderTotalsByCust.Totals.GrandTotal.Amount = orderTotalsByCust.Totals.GrandTotal.Amount + order.Totals.GrandTotal.Amount; orderTotalsByCust.Totals.SubTotal.Amount = orderTotalsByCust.Totals.SubTotal.Amount + order.Totals.SubTotal.Amount; orderTotalsByCust.Totals.PaymentsTotal.Amount = orderTotalsByCust.Totals.PaymentsTotal.Amount + order.Totals.PaymentsTotal.Amount; orderTotalsByCust.Totals.AdjustmentsTotal.Amount = orderTotalsByCust.Totals.AdjustmentsTotal.Amount + order.Totals.AdjustmentsTotal.Amount; foreach (var adjustment in order.Adjustments) { var adjustmentType = orderTotalsByCust.Adjustments.FirstOrDefault(p => p.Name == adjustment.AdjustmentType); if (adjustmentType == null) { adjustmentType = new TotalsAdjustmentsModel { Name = adjustment.AdjustmentType }; orderTotalsByCust.Adjustments.Add(adjustmentType); } adjustmentType.Adjustment.Amount = adjustmentType.Adjustment.Amount + adjustment.Adjustment.Amount; } } orderTotals.OrderCount++; } orderTotals.LastSkip = orderTotals.LastSkip + batchSize; orderTotals.History.Add(new HistoryEntryModel { Name = "MonitorTotals.BatchCompleted", EventMessage = $"Completed batch: {orderTotals.LastSkip}" }); orderTotals.LastRunEnded = DateTimeOffset.UtcNow; orderTotals.LastSkip = (orderTotals.LastSkip - batchSize) + returnedOrders.Count(); await this._commerceCommander.PersistEntity(commerceContext, orderTotals); foreach (var orderCustomerTotal in customerTotals) { await this._commerceCommander.PersistEntity(commerceContext, orderCustomerTotal.Value); } customerTotals.Clear(); await Task.Delay(100); arg = new FindEntitiesInListArgument(typeof(CommerceEntity), "Orders", System.Convert.ToInt32(orderTotals.LastSkip), batchSize); ordersBatch = await this._commerceCommander.Pipeline <FindEntitiesInListPipeline>().Run(arg, commerceContext.GetPipelineContextOptions()); } } catch (Exception ex) { commerceContext.Logger.LogError($"ChildViewEnvironments.Exception: Message={ex.Message}"); } return(true); } }
public override async Task <EntityView> Run(EntityView entityView, CommercePipelineExecutionContext context) { Condition.Requires(entityView).IsNotNull($"{Name}: The argument cannot be null"); var pluginPolicy = context.GetPolicy <PluginPolicy>(); OrdersTotals orderTotals = null; if (entityView.EntityId.Contains("Entity-Customer-")) { var entityViewArgument = _commerceCommander.Command <ViewCommander>().CurrentEntityViewArgument(context.CommerceContext); orderTotals = await _commerceCommander.GetEntity <OrdersTotals>(context.CommerceContext, $"Order_Totals_ByCust_{entityViewArgument.EntityId.Replace("Entity-Customer-", "")}", true); if (!orderTotals.IsPersisted) { return(entityView); } } else if (entityView.Name == "OrdersDashboard") { orderTotals = await _commerceCommander.GetEntity <OrdersTotals>(context.CommerceContext, "Orders_Totals", true); if (!orderTotals.IsPersisted) { return(entityView); } } if (orderTotals == null) { return(entityView); } if (orderTotals.IsPersisted) { var ordersTotalsView = new EntityView { Name = "Orders.Enhancements.Totals", DisplayName = "Order Totals", UiHint = "Flat", DisplayRank = 10, Icon = pluginPolicy.Icon, ItemId = string.Empty, }; entityView.ChildViews.Add(ordersTotalsView); ordersTotalsView.Properties.Add( new ViewProperty { Name = "Orders", IsHidden = false, IsReadOnly = true, RawValue = orderTotals.OrderCount }); ordersTotalsView.Properties.Add( new ViewProperty { Name = "GrandTotal", IsHidden = false, IsReadOnly = true, RawValue = orderTotals.Totals.GrandTotal.AsCurrency() }); ordersTotalsView.Properties.Add( new ViewProperty { Name = "Payments", IsHidden = false, IsReadOnly = true, RawValue = orderTotals.Totals.PaymentsTotal.AsCurrency() }); ordersTotalsView.Properties.Add( new ViewProperty { Name = "SubTotal", IsHidden = false, IsReadOnly = true, RawValue = orderTotals.Totals.SubTotal.AsCurrency() }); ordersTotalsView.Properties.Add( new ViewProperty { Name = "Updated", IsHidden = false, IsReadOnly = true, RawValue = orderTotals.DateUpdated.Value.ToString("yyyy-MMM-dd hh:mm") }); ordersTotalsView.Properties.Add( new ViewProperty { Name = "Version", IsHidden = false, IsReadOnly = true, RawValue = orderTotals.Version }); ordersTotalsView.Properties.Add( new ViewProperty { Name = "LastRecord", DisplayName = "Last Record", IsHidden = false, IsReadOnly = true, RawValue = orderTotals.LastSkip }); ordersTotalsView.Properties.Add( new ViewProperty { Name = "LastRunStart", DisplayName = "Last Run Start", IsHidden = false, IsReadOnly = true, RawValue = orderTotals.LastRunStarted.ToString("yyyy-MMM-dd hh:mm") }); ordersTotalsView.Properties.Add( new ViewProperty { Name = "LastRunEnd", DisplayName = "Last Run End", IsHidden = false, IsReadOnly = true, RawValue = orderTotals.LastRunEnded.ToString("yyyy-MMM-dd hh:mm") }); ordersTotalsView.Properties.Add( new ViewProperty { Name = "Run Time", DisplayName = "Run Time (s)", IsHidden = false, IsReadOnly = true, RawValue = (orderTotals.LastRunEnded - orderTotals.LastRunStarted).TotalSeconds.ToString() }); if (orderTotals.Adjustments.Count > 0) { foreach (var adjustment in orderTotals.Adjustments) { adjustment.Adjustment.CurrencyCode = "USD"; ordersTotalsView.Properties.Add( new ViewProperty { Name = adjustment.Name, IsHidden = false, IsReadOnly = true, RawValue = adjustment.Adjustment }); } } } return(entityView); }