private void CheckSelfTrade(Order order) { if (!_priceBookList.TryGetValue(order.Instrument.Symbol, out var book)) { book = new PriceBook(); _priceBookList.Add(order.Instrument.Symbol, book); } if (book.TradeTest(order)) { if (PreventMethod == SelfTradePreventMethod.Reject) { SendOrderReject(order); } else { _logger.Debug($"{order.Text}: 检测到自成交可能,进入等待队列."); order.DateTime = DateTime.Now; _pendingList.Add(order); } } else { book.AddOrder(order); order.Send(); } }
private async Task <PriceCard> UpdateOrCreatePriceCard(CommercePipelineExecutionContext context, PriceBook priceBook, string priceCardName, string displayName = "", string description = "") { var cardId = string.Format("{0}{1}-{2}", CommerceEntity.IdPrefix <PriceCard>(), priceBook.Name, priceCardName); var priceCard = await _findEntityPipeline.Run(new FindEntityArgument(typeof(PriceCard), cardId), context).ConfigureAwait(false) as PriceCard; if (priceCard != null) { return(priceCard); } var priceCardArgument = new AddPriceCardArgument(priceBook, priceCardName) { Description = description, DisplayName = displayName }; var createdPriceCard = await _addPriceCardPipeline.Run(priceCardArgument, context) .ConfigureAwait(false); if (createdPriceCard != null) { priceCard = createdPriceCard; } return(priceCard); }
/// <summary> /// Creates the variants card. /// </summary> /// <param name="book">The book.</param> /// <param name="context">The context.</param> private void CreateVariantsCard(PriceBook book, CommercePipelineExecutionContext context) { context.CommerceContext.Models.RemoveAll(m => m is PriceSnapshotAdded || m is PriceTierAdded); var date = DateTimeOffset.UtcNow; // ADVENTURE VARIANTS CARD var adventureVariantsCard = this._addPriceCardPipeline.Run(new AddPriceCardArgument(book, "AdventureVariantsPriceCard"), context).Result; // READY FOR APPROVAL SNAPSHOT adventureVariantsCard = this._addPriceSnapshotPipeline.Run(new PriceCardSnapshotArgument(adventureVariantsCard, new PriceSnapshotComponent(date.AddMinutes(-10))), context).Result; var readyForApprovalSnapshot = adventureVariantsCard.Snapshots.FirstOrDefault(s => s.Id.Equals(context.CommerceContext.Models.OfType <PriceSnapshotAdded>().FirstOrDefault()?.PriceSnapshotId, StringComparison.OrdinalIgnoreCase)); adventureVariantsCard = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureVariantsCard, readyForApprovalSnapshot, new PriceTier("USD", 1, 2000M)), context).Result; context.CommerceContext.Models.RemoveAll(m => m is PriceSnapshotAdded || m is PriceTierAdded); // FIRST APPROVED SNAPSHOT adventureVariantsCard = this._addPriceSnapshotPipeline.Run(new PriceCardSnapshotArgument(adventureVariantsCard, new PriceSnapshotComponent(date.AddHours(-1))), context).Result; var firstSnapshot = adventureVariantsCard.Snapshots.FirstOrDefault(s => s.Id.Equals(context.CommerceContext.Models.OfType <PriceSnapshotAdded>().FirstOrDefault()?.PriceSnapshotId, StringComparison.OrdinalIgnoreCase)); adventureVariantsCard = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureVariantsCard, firstSnapshot, new PriceTier("USD", 1, 9M)), context).Result; adventureVariantsCard = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureVariantsCard, firstSnapshot, new PriceTier("USD", 5, 6M)), context).Result; adventureVariantsCard = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureVariantsCard, firstSnapshot, new PriceTier("USD", 10, 3M)), context).Result; adventureVariantsCard = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureVariantsCard, firstSnapshot, new PriceTier("CAD", 1, 7M)), context).Result; adventureVariantsCard = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureVariantsCard, firstSnapshot, new PriceTier("CAD", 5, 4M)), context).Result; adventureVariantsCard = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureVariantsCard, firstSnapshot, new PriceTier("CAD", 10, 2M)), context).Result; adventureVariantsCard = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureVariantsCard, firstSnapshot, new PriceTier("EUR", 1, 2M)), context).Result; context.CommerceContext.Models.RemoveAll(m => m is PriceSnapshotAdded || m is PriceTierAdded); // DRAFT SNAPSHOT adventureVariantsCard = this._addPriceSnapshotPipeline.Run(new PriceCardSnapshotArgument(adventureVariantsCard, new PriceSnapshotComponent(date)), context).Result; var draftSnapshot = adventureVariantsCard.Snapshots.FirstOrDefault(s => s.Id.Equals(context.CommerceContext.Models.OfType <PriceSnapshotAdded>().FirstOrDefault()?.PriceSnapshotId, StringComparison.OrdinalIgnoreCase)); adventureVariantsCard = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureVariantsCard, draftSnapshot, new PriceTier("USD", 1, 1000M)), context).Result; context.CommerceContext.Models.RemoveAll(m => m is PriceSnapshotAdded || m is PriceTierAdded); // SECOND APPROVED SNAPSHOT adventureVariantsCard = this._addPriceSnapshotPipeline.Run(new PriceCardSnapshotArgument(adventureVariantsCard, new PriceSnapshotComponent(date.AddDays(30))), context).Result; var secondSnapshot = adventureVariantsCard.Snapshots.FirstOrDefault(s => s.Id.Equals(context.CommerceContext.Models.OfType <PriceSnapshotAdded>().FirstOrDefault()?.PriceSnapshotId, StringComparison.OrdinalIgnoreCase)); adventureVariantsCard = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureVariantsCard, secondSnapshot, new PriceTier("USD", 1, 8M)), context).Result; adventureVariantsCard = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureVariantsCard, secondSnapshot, new PriceTier("USD", 5, 4M)), context).Result; adventureVariantsCard = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureVariantsCard, secondSnapshot, new PriceTier("USD", 10, 2M)), context).Result; adventureVariantsCard = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureVariantsCard, secondSnapshot, new PriceTier("CAD", 1, 7M)), context).Result; adventureVariantsCard = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureVariantsCard, secondSnapshot, new PriceTier("CAD", 5, 3M)), context).Result; adventureVariantsCard = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureVariantsCard, secondSnapshot, new PriceTier("CAD", 10, 1M)), context).Result; adventureVariantsCard = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureVariantsCard, secondSnapshot, new PriceTier("EUR", 1, 2M)), context).Result; context.CommerceContext.Models.RemoveAll(m => m is PriceSnapshotAdded || m is PriceTierAdded); readyForApprovalSnapshot?.SetComponent(new ApprovalComponent(context.GetPolicy <ApprovalStatusPolicy>().ReadyForApproval)); firstSnapshot?.SetComponent(new ApprovalComponent(context.GetPolicy <ApprovalStatusPolicy>().Approved)); secondSnapshot?.SetComponent(new ApprovalComponent(context.GetPolicy <ApprovalStatusPolicy>().Approved)); this._persistEntityPipeline.Run(new PersistEntityArgument(adventureVariantsCard), context).Wait(); }
/// <summary> /// Creates the tags card. /// </summary> /// <param name="book">The book.</param> /// <param name="context">The context.</param> private async Task CreateTagsCard(PriceBook book, CommercePipelineExecutionContext context) { // TAGS CARD var card = await this._addPriceCardPipeline.Run(new AddPriceCardArgument(book, "Habitat_TagsPriceCard"), context).ConfigureAwait(false); // TAGS CARD FIRST SNAPSHOT card = await this._addPriceSnapshotPipeline.Run(new PriceCardSnapshotArgument(card, new PriceSnapshotComponent(DateTimeOffset.UtcNow)), context).ConfigureAwait(false); var firstSnapshot = card.Snapshots.FirstOrDefault(); // TAGS CARD FIRST SNAPSHOT TIERS card = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(card, firstSnapshot, new PriceTier("USD", 1, 250M)), context).ConfigureAwait(false); card = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(card, firstSnapshot, new PriceTier("USD", 5, 200M)), context).ConfigureAwait(false); card = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(card, firstSnapshot, new PriceTier("CAD", 1, 251M)), context).ConfigureAwait(false); card = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(card, firstSnapshot, new PriceTier("CAD", 5, 201M)), context).ConfigureAwait(false); // TAGS CARD FIRST SNAPSHOT TAGS card = await this._addPriceSnapshotTagPipeline.Run(new PriceCardSnapshotTagArgument(card, firstSnapshot, new Tag("Habitat")), context).ConfigureAwait(false); card = await this._addPriceSnapshotTagPipeline.Run(new PriceCardSnapshotTagArgument(card, firstSnapshot, new Tag("Habitat 2")), context).ConfigureAwait(false); card = await this._addPriceSnapshotTagPipeline.Run(new PriceCardSnapshotTagArgument(card, firstSnapshot, new Tag("common")), context).ConfigureAwait(false); // TAGS CARD SECOND SNAPSHOT card = await this._addPriceSnapshotPipeline.Run(new PriceCardSnapshotArgument(card, new PriceSnapshotComponent(DateTimeOffset.UtcNow.AddSeconds(1))), context).ConfigureAwait(false); var secondSnapshot = card.Snapshots.FirstOrDefault(s => !s.Id.Equals(firstSnapshot?.Id, StringComparison.OrdinalIgnoreCase)); // TAGS CARD SECOND SNAPSHOT TIERS card = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(card, secondSnapshot, new PriceTier("USD", 1, 150M)), context).ConfigureAwait(false); card = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(card, secondSnapshot, new PriceTier("USD", 5, 100M)), context).ConfigureAwait(false); card = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(card, secondSnapshot, new PriceTier("CAD", 1, 101M)), context).ConfigureAwait(false); card = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(card, secondSnapshot, new PriceTier("CAD", 5, 151M)), context).ConfigureAwait(false); // TAGS CARD SECOND SNAPSHOT TAGS card = await this._addPriceSnapshotTagPipeline.Run(new PriceCardSnapshotTagArgument(card, secondSnapshot, new Tag("Habitat variants")), context).ConfigureAwait(false); card = await this._addPriceSnapshotTagPipeline.Run(new PriceCardSnapshotTagArgument(card, secondSnapshot, new Tag("Habitat variants 2")), context).ConfigureAwait(false); card = await this._addPriceSnapshotTagPipeline.Run(new PriceCardSnapshotTagArgument(card, secondSnapshot, new Tag("common")), context).ConfigureAwait(false); // TAGS CARD APPROVAl COMPONENT card.Snapshots.ForEach(s => { s.SetComponent(new ApprovalComponent(context.GetPolicy <ApprovalStatusPolicy>().Approved)); }); await this._persistEntityPipeline.Run(new PersistEntityArgument(card), context).ConfigureAwait(false); }
public void Add(string key, PriceBook book) { if (!_priceBook.ContainsKey(key)) { _priceBook.Add(key, book); _sortedPriceBook.Add(book); } else { _priceBook.Remove(key); _sortedPriceBook.Remove(book); _priceBook.Add(key, book); _sortedPriceBook.Add(book); } }
/// <summary> /// Creates the tags card. /// </summary> /// <param name="book">The book.</param> /// <param name="context">The context.</param> private void CreateTagsCard(PriceBook book, CommercePipelineExecutionContext context) { // ADVENTURE TAGS CARD var card = this._addPriceCardPipeline.Run(new AddPriceCardArgument(book, "AdventureTagsPriceCard"), context).Result; // ADVENTURE TAGS CARD FIRST SNAPSHOT card = this._addPriceSnapshotPipeline.Run(new PriceCardSnapshotArgument(card, new PriceSnapshotComponent(DateTimeOffset.UtcNow)), context).Result; var firstSnapshot = card.Snapshots.FirstOrDefault(); // ADVENTURE TAGS CARD FIRST SNAPSHOT TIERS card = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(card, firstSnapshot, new PriceTier("USD", 1, 250M)), context).Result; card = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(card, firstSnapshot, new PriceTier("USD", 5, 200M)), context).Result; card = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(card, firstSnapshot, new PriceTier("CAD", 1, 251M)), context).Result; card = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(card, firstSnapshot, new PriceTier("CAD", 5, 201M)), context).Result; // ADVENTURE TAGS CARD FIRST SNAPSHOT TAGS card = this._addPriceSnapshotTagPipeline.Run(new PriceCardSnapshotTagArgument(card, firstSnapshot, new Tag("adventure works")), context).Result; card = this._addPriceSnapshotTagPipeline.Run(new PriceCardSnapshotTagArgument(card, firstSnapshot, new Tag("adventure works 2")), context).Result; card = this._addPriceSnapshotTagPipeline.Run(new PriceCardSnapshotTagArgument(card, firstSnapshot, new Tag("common")), context).Result; // ADVENTURE TAGS CARD SECOND SNAPSHOT card = this._addPriceSnapshotPipeline.Run(new PriceCardSnapshotArgument(card, new PriceSnapshotComponent(DateTimeOffset.UtcNow.AddSeconds(1))), context).Result; var secondSnapshot = card.Snapshots.FirstOrDefault(s => !s.Id.Equals(firstSnapshot?.Id)); // ADVENTURE TAGS CARD SECOND SNAPSHOT TIERS card = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(card, secondSnapshot, new PriceTier("USD", 1, 150M)), context).Result; card = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(card, secondSnapshot, new PriceTier("USD", 5, 100M)), context).Result; card = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(card, secondSnapshot, new PriceTier("CAD", 1, 101M)), context).Result; card = this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(card, secondSnapshot, new PriceTier("CAD", 5, 151M)), context).Result; // ADVENTURE TAGS CARD SECOND SNAPSHOT TAGS card = this._addPriceSnapshotTagPipeline.Run(new PriceCardSnapshotTagArgument(card, secondSnapshot, new Tag("adventure works variants")), context).Result; card = this._addPriceSnapshotTagPipeline.Run(new PriceCardSnapshotTagArgument(card, secondSnapshot, new Tag("adventure works variants 2")), context).Result; card = this._addPriceSnapshotTagPipeline.Run(new PriceCardSnapshotTagArgument(card, secondSnapshot, new Tag("common")), context).Result; // ADVENTURE TAGS CARD APPROVAl COMPONENT card.Snapshots.ForEach(s => { s.SetComponent(new ApprovalComponent(context.GetPolicy <ApprovalStatusPolicy>().Approved)); }); this._persistEntityPipeline.Run(new PersistEntityArgument(card), context).Wait(); }
/// <summary> /// The run. /// </summary> /// <param name="arg"> /// The argument. /// </param> /// <param name="context"> /// The context. /// </param> /// <returns> /// The <see cref="string" />. /// </returns> public override async Task <string> Run(string arg, CommercePipelineExecutionContext context) { var artifactSet = "Environment.Habitat.Pricing-1.0"; // Check if this environment has subscribed to this Artifact Set if (!context.GetPolicy <EnvironmentInitializationPolicy>().InitialArtifactSets.Contains(artifactSet)) { return(arg); } context.Logger.LogInformation($"{Name}.InitializingArtifactSet: ArtifactSet={artifactSet}"); try { string currencySetId = context.GetPolicy <GlobalCurrencyPolicy>().DefaultCurrencySet; // BOOK PriceBook book = await _addPriceBookPipeline.Run( new AddPriceBookArgument("Habitat_PriceBook") { ParentBook = string.Empty, Description = "Habitat price book", DisplayName = "Habitat", CurrencySetId = currencySetId }, context).ConfigureAwait(false); await CreateProductsCard(book, context).ConfigureAwait(false); await CreateVariantsCard(book, context).ConfigureAwait(false); await CreateTagsCard(book, context).ConfigureAwait(false); await AssociateCatalogToBook(book.Name, "Habitat_Master", context).ConfigureAwait(false); } catch (Exception ex) { context.CommerceContext.LogException(Name, ex); } return(arg); }
/// <summary> /// Creates the products card. /// </summary> /// <param name="book">The book.</param> /// <param name="context">The context.</param> private async Task CreateProductsCard(PriceBook book, CommercePipelineExecutionContext context) { context.CommerceContext.RemoveModels(m => m is PriceSnapshotAdded || m is PriceTierAdded); var date = DateTimeOffset.UtcNow; // CARD var adventureCard = await this._addPriceCardPipeline.Run(new AddPriceCardArgument(book, "Habitat_PriceCard"), context).ConfigureAwait(false); // READY FOR APPROVAL SNAPSHOT adventureCard = await this._addPriceSnapshotPipeline.Run(new PriceCardSnapshotArgument(adventureCard, new PriceSnapshotComponent(date.AddMinutes(-10))), context).ConfigureAwait(false); var readyForApprovalSnapshot = adventureCard.Snapshots.FirstOrDefault(s => s.Id.Equals(context.CommerceContext.GetModel <PriceSnapshotAdded>()?.PriceSnapshotId, StringComparison.OrdinalIgnoreCase)); adventureCard = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, readyForApprovalSnapshot, new PriceTier("USD", 1, 2000M)), context).ConfigureAwait(false); context.CommerceContext.RemoveModels(m => m is PriceSnapshotAdded || m is PriceTierAdded); // CARD FIRST SNAPSHOT adventureCard = await this._addPriceSnapshotPipeline.Run(new PriceCardSnapshotArgument(adventureCard, new PriceSnapshotComponent(date.AddHours(-1))), context).ConfigureAwait(false); var firstSnapshot = adventureCard.Snapshots.FirstOrDefault(s => s.Id.Equals(context.CommerceContext.GetModel <PriceSnapshotAdded>()?.PriceSnapshotId, StringComparison.OrdinalIgnoreCase)); adventureCard = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, firstSnapshot, new PriceTier("USD", 1, 10M)), context).ConfigureAwait(false); adventureCard = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, firstSnapshot, new PriceTier("USD", 5, 5M)), context).ConfigureAwait(false); adventureCard = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, firstSnapshot, new PriceTier("USD", 10, 1M)), context).ConfigureAwait(false); adventureCard = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, firstSnapshot, new PriceTier("CAD", 1, 15M)), context).ConfigureAwait(false); adventureCard = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, firstSnapshot, new PriceTier("CAD", 5, 10M)), context).ConfigureAwait(false); adventureCard = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, firstSnapshot, new PriceTier("CAD", 10, 5M)), context).ConfigureAwait(false); adventureCard = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, firstSnapshot, new PriceTier("EUR", 1, 1M)), context).ConfigureAwait(false); context.CommerceContext.RemoveModels(m => m is PriceSnapshotAdded || m is PriceTierAdded); // DRAFT SNAPSHOT adventureCard = await this._addPriceSnapshotPipeline.Run(new PriceCardSnapshotArgument(adventureCard, new PriceSnapshotComponent(date)), context).ConfigureAwait(false); var draftSnapshot = adventureCard.Snapshots.FirstOrDefault(s => s.Id.Equals(context.CommerceContext.GetModel <PriceSnapshotAdded>()?.PriceSnapshotId, StringComparison.OrdinalIgnoreCase)); adventureCard = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, draftSnapshot, new PriceTier("USD", 1, 1000M)), context).ConfigureAwait(false); adventureCard = await this._addPriceSnapshotTagPipeline.Run(new PriceCardSnapshotTagArgument(adventureCard, draftSnapshot, new Tag("new pricing")), context).ConfigureAwait(false); context.CommerceContext.RemoveModels(m => m is PriceSnapshotAdded || m is PriceTierAdded); // CARD SECOND SNAPSHOT adventureCard = await this._addPriceSnapshotPipeline.Run(new PriceCardSnapshotArgument(adventureCard, new PriceSnapshotComponent(date.AddDays(30))), context).ConfigureAwait(false); var secondSnapshot = adventureCard.Snapshots.FirstOrDefault(s => s.Id.Equals(context.CommerceContext.GetModel <PriceSnapshotAdded>()?.PriceSnapshotId, StringComparison.OrdinalIgnoreCase)); adventureCard = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, secondSnapshot, new PriceTier("USD", 1, 7M)), context).ConfigureAwait(false); adventureCard = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, secondSnapshot, new PriceTier("USD", 5, 4M)), context).ConfigureAwait(false); adventureCard = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, secondSnapshot, new PriceTier("USD", 10, 3M)), context).ConfigureAwait(false); adventureCard = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, secondSnapshot, new PriceTier("CAD", 1, 6M)), context).ConfigureAwait(false); adventureCard = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, secondSnapshot, new PriceTier("CAD", 5, 3M)), context).ConfigureAwait(false); adventureCard = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, secondSnapshot, new PriceTier("CAD", 10, 2M)), context).ConfigureAwait(false); adventureCard = await this._addPriceTierPipeline.Run(new PriceCardSnapshotTierArgument(adventureCard, secondSnapshot, new PriceTier("EUR", 1, 1M)), context).ConfigureAwait(false); adventureCard = await this._addPriceSnapshotTagPipeline.Run(new PriceCardSnapshotTagArgument(adventureCard, secondSnapshot, new Tag("future pricing")), context).ConfigureAwait(false); context.CommerceContext.RemoveModels(m => m is PriceSnapshotAdded || m is PriceTierAdded); readyForApprovalSnapshot?.SetComponent(new ApprovalComponent(context.GetPolicy <ApprovalStatusPolicy>().ReadyForApproval)); firstSnapshot?.SetComponent(new ApprovalComponent(context.GetPolicy <ApprovalStatusPolicy>().Approved)); secondSnapshot?.SetComponent(new ApprovalComponent(context.GetPolicy <ApprovalStatusPolicy>().Approved)); await this._persistEntityPipeline.Run(new PersistEntityArgument(adventureCard), context).ConfigureAwait(false); }
public static void AddBuySellTransaction(BuyPriceBookList buyList, SellPriceBookList sellList, PriceBook book, string priceBookType) { if (book.Price <= 0 || book.Quantity <= 0 || string.IsNullOrWhiteSpace(book.OrderId)) { return; } if (priceBookType == "BUY") { buyList.Add(book.OrderId, book); } if (priceBookType == "SELL") { sellList.Add(book.OrderId, book); } }
public static void RunOrderMatchingEngine(string str, BuyPriceBookList buyList, SellPriceBookList sellList) { var strArray = str.Split(' '); switch (strArray[0]) { case "BUY": { var orderId = strArray[4]; var book = PriceBook.CreatePriceBook(strArray[1], int.Parse(strArray[2]), int.Parse(strArray[3]), strArray[4]); //buyList.Add(orderId, book); AddBuySellTransaction(buyList, sellList, book, "BUY"); HandleGFDTrade(buyList, sellList); if (book.OrderType == OrderType.IOC) { buyList.Remove(book.OrderId); } break; } case "SELL": { var orderId = strArray[4]; var book = PriceBook.CreatePriceBook(strArray[1], int.Parse(strArray[2]), int.Parse(strArray[3]), strArray[4]); //sellList.Add(orderId, book); AddBuySellTransaction(buyList, sellList, book, "SELL"); HandleGFDTrade(buyList, sellList); if (book.OrderType == OrderType.IOC) { sellList.Remove(book.OrderId); } break; } case "CANCEL": { var orderId = strArray[1]; if (buyList.ContainsKey(orderId)) { buyList.Remove(orderId); } if (sellList.ContainsKey(orderId)) { sellList.Remove(orderId); } break; } case "MODIFY": { var orderId = strArray[1]; var buyOrSell = strArray[2]; var newPrice = int.Parse(strArray[3]); var newQuantity = int.Parse(strArray[4]); PriceBook book = null; if (buyList.ContainsKey(orderId)) { book = buyList.Get(orderId); buyList.Remove(orderId); } if (sellList.ContainsKey(orderId)) { book = sellList.Get(orderId); sellList.Remove(orderId); } book.CurrentTime = DateTime.Now; book.Price = int.Parse(strArray[3]); book.Quantity = int.Parse(strArray[4]); var newBook = PriceBook.CreatePriceBook("GFD", newPrice, newQuantity, orderId); AddBuySellTransaction(buyList, sellList, newBook, strArray[2]); HandleGFDTrade(buyList, sellList); break; } case "PRINT": { Print(buyList, sellList); break; } default: break; } }
public override async Task <PriceCard> Run(PriceCard arg, CommercePipelineExecutionContext context) { Condition.Requires(arg).IsNotNull(Name + ": The price card can not be null"); PriceCard card = arg; PriceCardSnapshotCustomTierArgument argument = context.CommerceContext.GetObjects <PriceCardSnapshotCustomTierArgument>().FirstOrDefault(); CommercePipelineExecutionContext executionContext; if (argument == null) { executionContext = context; var commerceContext = context.CommerceContext; string error = context.GetPolicy <KnownResultCodes>().Error; string defaultMessage = "Argument of type " + typeof(PriceCardSnapshotCustomTierArgument).Name + " was not found in context."; executionContext.Abort(await commerceContext.AddMessage(error, "ArgumentNotFound", new object[] { typeof(PriceCardSnapshotCustomTierArgument).Name }, defaultMessage) .ConfigureAwait(false), context); executionContext = null; return(card); } Condition.Requires(argument.PriceSnapshot).IsNotNull(Name + ": The price snapshot cannot be null."); Condition.Requires(argument.PriceSnapshot.Id).IsNotNullOrEmpty(Name + ": The price snapshot id cannot be null or empty."); Condition.Requires(argument.PriceTier).IsNotNull(Name + ": The price tier cannot be null."); Condition.Requires(argument.PriceTier.Currency).IsNotNullOrEmpty(Name + ": The price tier currency cannot be null or empty."); Condition.Requires(argument.PriceTier.Quantity).IsNotNull(Name + ": The price tier quantity cannot be null."); Condition.Requires(argument.PriceTier.Price).IsNotNull(Name + ": The price tier price cannot be null."); var snapshot = argument.PriceSnapshot; var tier = argument.PriceTier; var membershipTiersComponent = snapshot.GetComponent <MembershipTiersComponent>(); if (membershipTiersComponent.Tiers.Any(x => x.Currency == tier.Currency && x.MembershipLevel == tier.MembershipLevel && x.Quantity == tier.Quantity)) { executionContext = context; CommerceContext commerceContext = context.CommerceContext; string validationError = context.GetPolicy <KnownResultCodes>().ValidationError; string defaultMessage = "A tier for specified Currency, Membership Level and Quantity already exists for price snapshot '" + snapshot.Id + "' in price card '" + card.FriendlyId + "'."; executionContext.Abort(await commerceContext.AddMessage(validationError, "PriceTierAlreadyExists", new object[] { snapshot.Id, card.FriendlyId }, defaultMessage) .ConfigureAwait(false), context); executionContext = null; return(card); } GlobalPricingPolicy policy = context.GetPolicy <GlobalPricingPolicy>(); if (argument.PriceTier.Quantity <= policy.MinimumPricingQuantity) { executionContext = context; CommerceContext commerceContext = context.CommerceContext; string error = context.GetPolicy <KnownResultCodes>().Error; string defaultMessage = string.Format("Invalid quantity. Quantity must be greater than '{0}'.", policy.MinimumPricingQuantity); executionContext.Abort(await commerceContext.AddMessage(error, "InvalidQuantity", new object[] { policy.MinimumPricingQuantity }, defaultMessage) .ConfigureAwait(false), context); executionContext = null; return(card); } if (argument.PriceTier.Price < policy.MinimumPrice) { executionContext = context; CommerceContext commerceContext = context.CommerceContext; string error = context.GetPolicy <KnownResultCodes>().Error; string defaultMessage = string.Format("Invalid price. Minimum price allowed is '{0}'.", policy.MinimumPrice); executionContext.Abort(await commerceContext.AddMessage(error, "InvalidPrice", new object[] { policy.MinimumPrice }, defaultMessage) .ConfigureAwait(false), context); executionContext = null; return(card); } PriceBook priceBook = await _findEntityPipeline.Run(new FindEntityArgument(typeof(PriceBook), card.Book.EntityTarget, false), context) .ConfigureAwait(false) as PriceBook; CurrencySet currencySet = await _getCurrencySetPipeline.Run(priceBook?.CurrencySet?.EntityTarget, context) .ConfigureAwait(false); bool flag = false; string str = string.Empty; if (currencySet != null && currencySet.HasComponent <CurrenciesComponent>()) { CurrenciesComponent component = currencySet.GetComponent <CurrenciesComponent>(); str = string.Join(", ", component.Currencies.Select(c => c.Code)); if (component.Currencies.Any(c => c.Code.Equals(argument.PriceTier.Currency, StringComparison.OrdinalIgnoreCase))) { flag = true; } } if (!flag) { executionContext = context; CommerceContext commerceContext = context.CommerceContext; string validationError = context.GetPolicy <KnownResultCodes>().ValidationError; string defaultMessage = "Invalid currency '" + argument.PriceTier.Currency + "'. Valid currencies are '" + str + "'."; executionContext.Abort(await commerceContext.AddMessage(validationError, "InvalidCurrency", new object[] { argument.PriceTier.Currency, str }, defaultMessage) .ConfigureAwait(false), context); executionContext = null; return(card); } tier.Id = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture); PriceSnapshotComponent snapshotComponent = card.Snapshots.FirstOrDefault(n => n.Id.Equals(snapshot.Id, StringComparison.OrdinalIgnoreCase)); if (snapshotComponent != null) { var component = snapshotComponent.GetComponent <MembershipTiersComponent>(); if (component != null) { component.Tiers.Add(tier); } } PriceTierAdded priceTierAdded = new PriceTierAdded(tier.Id) { Name = tier.Name }; context.CommerceContext.AddModel(priceTierAdded); return(card); }
public override async Task <EntityView> Run(EntityView entityView, CommercePipelineExecutionContext context) { Condition.Requires(entityView).IsNotNull($"{this.Name}: The argument cannot be null"); if (entityView.Name != "PricingDashboard") { return(entityView); } var priceBooksView = entityView.ChildViews.FirstOrDefault(p => p.Name == "PriceBooks"); if (priceBooksView != null) { foreach (EntityView childView in (priceBooksView as EntityView).ChildViews) { var foundEntity = context.CommerceContext.GetObjects <FoundEntity>().FirstOrDefault(p => p.EntityId == childView.ItemId); if (foundEntity != null) { PriceBook priceBook = foundEntity.Entity as PriceBook; var listName = string.Format(context.GetPolicy <KnownPricingListsPolicy>().PriceBookCards, priceBook.Name); var listCount = await this._commerceCommander.Command <GetListCountCommand>() .Process(context.CommerceContext, listName); childView.Properties.Add(new ViewProperty { Name = "Count", DisplayName = "Count", RawValue = listCount }); if (priceBook.HasComponent <ActionHistoryComponent>()) { var actionHistoryComponent = priceBook.GetComponent <ActionHistoryComponent>(); var actionHistoryModel = actionHistoryComponent.History.First(); childView.Properties.Add(new ViewProperty { Name = "Start", DisplayName = "Started", RawValue = actionHistoryModel.StartTime.ToString("yyyy-MMM-dd hh:mm:ss") }); childView.Properties.Add(new ViewProperty { Name = "Complete", DisplayName = "Completed", RawValue = actionHistoryModel.Completed.ToString("yyyy-MMM-dd hh:mm:ss") }); var elapsed = (actionHistoryModel.Completed - actionHistoryModel.StartTime).TotalMilliseconds; childView.Properties.Add(new ViewProperty { Name = "Elapse", DisplayName = "Elapsed", RawValue = System.Convert.ToInt32(System.Math.Round(elapsed, 0)) }); } else { childView.Properties.Add(new ViewProperty { Name = "Start", DisplayName = "Started", RawValue = string.Empty }); childView.Properties.Add(new ViewProperty { Name = "Complete", DisplayName = "Completed", RawValue = string.Empty }); childView.Properties.Add(new ViewProperty { Name = "Elapse", DisplayName = "Elapsed", RawValue = string.Empty }); } } } } return(entityView); }
public override async Task <PriceCard> Run(PriceCard arg, CommercePipelineExecutionContext context) { Condition.Requires(arg).IsNotNull(Name + ": The price card can not be null"); PriceCard card = arg; var argument = context.CommerceContext.GetObjects <PriceCardSnapshotCustomTierArgument>().FirstOrDefault(); CommercePipelineExecutionContext executionContext; if (argument == null) { executionContext = context; CommerceContext commerceContext = context.CommerceContext; string error = context.GetPolicy <KnownResultCodes>().Error; string defaultMessage = "Argument of type " + typeof(PriceCardSnapshotCustomTierArgument).Name + " was not found in context."; executionContext.Abort(await commerceContext.AddMessage(error, "ArgumentNotFound", new object[] { typeof(PriceCardSnapshotCustomTierArgument).Name }, defaultMessage) .ConfigureAwait(false), context); executionContext = null; return(card); } Condition.Requires(argument.PriceSnapshot).IsNotNull(Name + ": The price snapshot can not be null"); Condition.Requires(argument.PriceSnapshot.Id).IsNotNullOrEmpty(Name + ": The price snapshot id can not be null or empty"); Condition.Requires(argument.PriceTier).IsNotNull(Name + ": The price tier can not be null"); Condition.Requires(argument.PriceTier.Currency).IsNotNullOrEmpty(Name + ": The price tier currency can not be null or empty"); PriceBook book = await _findEntityPipeline.Run(new FindEntityArgument(typeof(PriceBook), card.Book.EntityTarget, false), context) .ConfigureAwait(false) as PriceBook; CurrencySet currencySet = await _getCurrencySetPipeline.Run(book?.CurrencySet?.EntityTarget, context) .ConfigureAwait(false); if (!(currencySet != null && currencySet.HasComponent <CurrenciesComponent>() && currencySet.GetComponent <CurrenciesComponent>().Currencies.Any(c => c.Code.Equals(argument.PriceTier.Currency, StringComparison.OrdinalIgnoreCase)))) { executionContext = context; CommerceContext commerceContext = context.CommerceContext; string error = context.GetPolicy <KnownResultCodes>().Error; string defaultMessage = "Currency '" + argument.PriceTier.Currency + "' is no longer valid for book '" + book?.FriendlyId + "'. Either remove the tier or modify the book to include the currency."; executionContext.Abort(await commerceContext.AddMessage(error, "CurrencyNotLongerValid", new object[] { argument.PriceTier.Currency, book?.FriendlyId }, defaultMessage).ConfigureAwait(false), (object)context); executionContext = null; return(card); } GlobalPricingPolicy policy = context.GetPolicy <GlobalPricingPolicy>(); if (argument.PriceTier.Price < policy.MinimumPrice) { executionContext = context; CommerceContext commerceContext = context.CommerceContext; string error = context.GetPolicy <KnownResultCodes>().Error; string defaultMessage = string.Format("Invalid price. Minimum price allowed is '{0}'.", (object)policy.MinimumPrice); executionContext.Abort(await commerceContext.AddMessage(error, "InvalidPrice", new object[] { policy.MinimumPrice }, defaultMessage).ConfigureAwait(false), context); executionContext = null; return(card); } PriceSnapshotComponent snapshot = argument.PriceSnapshot; PriceSnapshotComponent snapshotComponent = card.Snapshots.FirstOrDefault(n => n.Id.Equals(snapshot.Id, StringComparison.OrdinalIgnoreCase)); if (snapshotComponent == null) { await context.CommerceContext.AddMessage(context.GetPolicy <KnownResultCodes>().ValidationError, "PriceSnapshotNotFound", new object[] { snapshot.Id, card.FriendlyId }, "Price snapshot '" + snapshot.Id + "' on price card '" + card.FriendlyId + "' was not found.") .ConfigureAwait(false); return(card); } var tier = argument.PriceTier; if (string.IsNullOrEmpty(tier.Id)) { tier.Id = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture); } var membershipTiersComponent = snapshotComponent.GetComponent <MembershipTiersComponent>(); var existingTier = membershipTiersComponent?.Tiers.FirstOrDefault(t => t.Id.Equals(tier.Id, StringComparison.OrdinalIgnoreCase)); if (existingTier == null) { executionContext = context; CommerceContext commerceContext = context.CommerceContext; string validationError = context.GetPolicy <KnownResultCodes>().ValidationError; string defaultMessage = "Price tier '" + tier.Id + "' was not found in snapshot '" + snapshot.Id + "' for card '" + card.FriendlyId + "."; executionContext.Abort(await commerceContext.AddMessage(validationError, "PriceTierNotFound", new object[] { tier.Id, snapshot.Id, card.FriendlyId }, defaultMessage).ConfigureAwait(false), context); executionContext = null; return(card); } await context.CommerceContext.AddMessage(context.GetPolicy <KnownResultCodes>().Information, null, null, "Edited price tier '" + tier.Id + "' from price snapshot '" + snapshot.Id + "'").ConfigureAwait(false); existingTier.Price = tier.Price; return(card); }