private async Task ValidateGiftCardAmountsAsync() { // grab every gift card that the customer applied to this order IList <GiftCard> appliedGiftCards = await _giftCardService.GetActiveGiftCardsAppliedByCustomerAsync(await _workContext.GetCurrentCustomerAsync()); if (appliedGiftCards.Count > 1) { throw new Exception("Only one gift card may be applied to an order"); } foreach (GiftCard nopGiftCard in appliedGiftCards) { // check isam to make sure each gift card has the right $$ GiftCard isamGiftCard = _isamGiftCardService.GetGiftCardInfo(nopGiftCard.GiftCardCouponCode).GiftCard; decimal nopAmtLeft = nopGiftCard.Amount; List <GiftCardUsageHistory> nopGcHistory = (await _giftCardService.GetGiftCardUsageHistoryAsync(nopGiftCard)).ToList(); foreach (var history in nopGcHistory) { nopAmtLeft -= history.UsedValue; } if (isamGiftCard.Amount != nopAmtLeft) { throw new Exception("A gift card has been used since it was placed on this order"); } } }
/// <summary> /// Inserts an entire order from nopcommerce into isam /// </summary> /// <param name="order"></param> public async Task InsertOrderAsync(Order order) { var shipToRows = await _yahooService.GetYahooShipToRowsAsync(order); foreach (var row in shipToRows) { InsertUsingService( SHIPTO_TABLE_NAME, _shiptoCols, _shiptoParams, row.ToStringValues() ); } var headerRows = await _yahooService.GetYahooHeaderRowsAsync(order); foreach (var row in headerRows) { InsertUsingService( HEADER_TABLE_NAME, _headerCols, _headerParams, row.ToStringValues() ); } var detailRows = await _yahooService.GetYahooDetailRowsAsync(order); foreach (var row in detailRows) { InsertUsingService( DETAIL_TABLE_NAME, _detailCols, _detailParams, row.ToStringValues() ); } _baseIsamService.ExecuteBatch(); // store amount used before insert order // insert order changes the amount in the object memory to properly calculate gift card amounts // for multiple orders decimal giftCardAmtUsed = 0; var giftCardUsageHistory = await _giftCardService.GetGiftCardUsageHistoryAsync(order); if (giftCardUsageHistory.Any()) { giftCardAmtUsed = giftCardUsageHistory.OrderByDescending(gcu => gcu.CreatedOnUtc).FirstOrDefault().UsedValue; } // if there is a gift card, update gift card amt in isam if (giftCardUsageHistory.Any()) { GiftCardUsageHistory orderGcUsage = giftCardUsageHistory.FirstOrDefault(); GiftCard orderGiftCard = await _giftCardService.GetGiftCardByIdAsync(orderGcUsage.GiftCardId); var isamGiftCardInfo = _isamGiftCardService.GetGiftCardInfo(orderGiftCard.GiftCardCouponCode); GiftCard isamGiftCard = isamGiftCardInfo.GiftCard; decimal isamGiftCardAmtUsed = isamGiftCardInfo.AmountUsed; _isamGiftCardService.UpdateGiftCardAmt(isamGiftCard, isamGiftCardAmtUsed + giftCardAmtUsed); await _giftCardUsageHistoryRepository.DeleteAsync(orderGcUsage); await _giftCardService.UpdateGiftCardAsync(orderGiftCard); } }