private ResultInfoOrders GetOrders(DateTime fromDate, DateTime toDate, RequestsCounterData requestsCounter) { var response = GetOrders(fromDate, toDate, null, requestsCounter); var orders = new ResultDataByResponseOrders(response); var rez = new ResultInfoOrders(orders); var countOrders = orders.CountOrders; if (countOrders == 0) { WriteToLog("no data"); return(rez); } else { WriteToLog("page {0,3} of {1,3}: {2}", 1, response.PaginationResult.TotalNumberOfPages, countOrders); } if (response.HasMoreOrdersSpecified && response.HasMoreOrders) { var pages = response.PaginationResult.TotalNumberOfPages; for (int i = 1; i < pages; i++) { var data = new ResultDataByResponseOrders(GetOrders(fromDate, toDate, i + 1, requestsCounter)); rez.AddData(data); WriteToLog("page {0,3} of {1,3}: {2}", i + 1, response.PaginationResult.TotalNumberOfPages, data.CountOrders); } } return(rez); }
} // FetchOnePeriodTransactions private EbayDatabaseOrdersList ParseOrdersInfo(ResultInfoOrders data) { var rez = new EbayDatabaseOrdersList(data.SubmittedDate) { RequestsCounter = data.RequestsCounter, }; foreach (OrderType o in data) { if (o == null) { continue; } var item = new EbayDatabaseOrderItem { CreatedTime = o.CreatedTimeSpecified ? o.CreatedTime.ToUniversalTime() : (DateTime?)null, ShippedTime = o.ShippedTimeSpecified ? o.ShippedTime.ToUniversalTime() : (DateTime?)null, PaymentTime = o.PaidTimeSpecified ? o.PaidTime.ToUniversalTime() : (DateTime?)null, BuyerName = o.BuyerUserID, AdjustmentAmount = ConvertToBaseCurrency(o.AdjustmentAmount, o.CreatedTime), AmountPaid = ConvertToBaseCurrency(o.AmountPaid, o.CreatedTime), SubTotal = ConvertToBaseCurrency(o.Subtotal, o.CreatedTime), Total = ConvertToBaseCurrency(o.Total, o.CreatedTime), OrderStatus = o.OrderStatusSpecified ? (EBayOrderStatusCodeType)Enum.Parse(typeof(EBayOrderStatusCodeType), o.OrderStatus.ToString()) : EBayOrderStatusCodeType.Default, PaymentHoldStatus = o.PaymentHoldStatusSpecified ? o.PaymentHoldStatus.ToString() : string.Empty, CheckoutStatus = o.CheckoutStatus != null && o.CheckoutStatus.StatusSpecified ? o.CheckoutStatus.Status.ToString() : string.Empty, PaymentMethod = o.CheckoutStatus != null && o.CheckoutStatus.PaymentMethodSpecified ? o.CheckoutStatus.PaymentMethod.ToString() : string.Empty, PaymentStatus = o.CheckoutStatus != null && o.CheckoutStatus.eBayPaymentStatusSpecified ? o.CheckoutStatus.eBayPaymentStatus.ToString() : string.Empty, PaymentMethods = o.PaymentMethods == null ? null : string.Join(",", o.PaymentMethods), ShippingAddressData = o.ShippingAddress == null ? null : o.ShippingAddress.ConvertToDatabaseType(), }; if (o.ExternalTransaction != null && o.ExternalTransaction.Length > 0) { item.ExternalTransactionData = new EBayDatabaseExternalTransactionList(); foreach (var et in o.ExternalTransaction) { if (et == null) { continue; } var exItem = new EBayDatabaseExternalTransactionItem(); exItem.TransactionID = et.ExternalTransactionID; exItem.TransactionTime = et.ExternalTransactionTimeSpecified ? et.ExternalTransactionTime : (DateTime?)null; exItem.FeeOrCreditAmount = ConvertToBaseCurrency(et.FeeOrCreditAmount, et.ExternalTransactionTime); exItem.PaymentOrRefundAmount = ConvertToBaseCurrency( et.PaymentOrRefundAmount, et.ExternalTransactionTime ); item.ExternalTransactionData.Add(exItem); } // for each } // if if (o.TransactionArray != null && o.TransactionArray.Length > 0) { item.TransactionData = new EbayDatabaseTransactionDataList(); foreach (var td in o.TransactionArray) { if (td == null || td.Item == null) { continue; } var itemType = td.Item; var trItem = new EbayDatabaseTransactionDataItem { CreatedDate = td.CreatedDate, QuantityPurchased = td.QuantityPurchased, PaymentHoldStatus = td.Status != null && td.Status.PaymentHoldStatusSpecified ? td.Status.PaymentHoldStatus.ToString() : string.Empty, PaymentMethodUsed = td.Status != null && td.Status.PaymentMethodUsedSpecified ? td.Status.PaymentMethodUsed.ToString() : string.Empty, TransactionPrice = ConvertToBaseCurrency(td.TransactionPrice, td.CreatedDate), ItemSKU = itemType.SKU, ItemID = itemType.ItemID, ItemPrivateNotes = itemType.PrivateNotes, ItemSellerInventoryID = itemType.SellerInventoryID, eBayTransactionId = td.TransactionID }; item.TransactionData.Add(trItem); } // for each } // if rez.Add(item); } // for each o in data return(rez); } // ParseOrdersInfo
public override ResultInfoOrders GetOrders(RequestsCounterData requestsCounter) { var now = DateTime.Now; var diffDays = (int)now.Subtract(FromDate).TotalDays; WriteToLog("---------------------------------"); WriteToLog("{0:g} - {1:g}", FromDate, ToDate); WriteToLog("Count Days: {0}", diffDays); if (diffDays <= MAX_RETURN_DAYS) { WriteToLog("all."); var orders = GetOrders(FromDate, ToDate, requestsCounter); WriteToLog("---------------------------------"); return(orders); } int rem; var iter = Math.DivRem(diffDays, MAX_RETURN_DAYS, out rem); WriteToLog("Count Iterations: {0}", iter); ResultInfoOrders rez = null; for (int i = 0; i < iter; i++) { DateTime fromDate = i == 0 ? FromDate : FromDate.AddDays(i * MAX_RETURN_DAYS); DateTime toDate = FromDate.AddDays((i + 1) * MAX_RETURN_DAYS); WriteToLog("iter: {0,3}. {1:g} - {2:g}", i + 1, fromDate, toDate); var orders = GetOrders(fromDate, toDate, requestsCounter); if (rez == null) { rez = new ResultInfoOrders(orders); } else { rez.AddData(orders); } } if (rem > 0) { DateTime fromDate = FromDate.AddDays(iter * MAX_RETURN_DAYS); DateTime toDate = ToDate; WriteToLog("remainder. {0:g} - {1:g}", fromDate, toDate); var orders = GetOrders(fromDate, toDate, requestsCounter); if (rez == null) { rez = new ResultInfoOrders(orders); } else { rez.AddData(orders); } } WriteToLog("---------------------------------"); return(rez); }
} // class FetchResultCounters private FetchResultCounters FetchOnePeriodTransactions( int mpID, IDatabaseCustomerMarketPlace databaseCustomerMarketPlace, ElapsedTimeInfo elapsedTimeInfo, DataProviderCreationInfo info, MP_CustomerMarketplaceUpdatingHistory historyRecord, FetchPeriod period ) { this.log.Debug("Fetching eBay orders '{0}'@{1}...", mpID, period); ResultInfoOrders orders = ElapsedTimeHelper.CalculateAndStoreElapsedTimeForCallInSeconds( elapsedTimeInfo, mpID, ElapsedDataMemberType.RetrieveDataFromExternalService, () => DataProviderGetOrders.GetOrders( info, new ParamsDataInfoGetOrdersFromDateToDateCreated(period.From, period.To) ) ); EbayDatabaseOrdersList databaseOrdersList = ParseOrdersInfo(orders); DateTime?max = null; DateTime?min = null; foreach (EbayDatabaseOrderItem o in databaseOrdersList.Where(x => x.CreatedTime != null)) { DateTime c = o.CreatedTime.Value; if ((min == null) || (c < min.Value)) { min = c; } if ((max == null) || (c > max.Value)) { max = c; } } // for each this.log.Debug( "Fetching eBay orders '{0}'@{1}: {2} are ready to be stored; " + "min order date is '{3}', max order date is '{4}'.", mpID, period, Grammar.Number(databaseOrdersList.Count, "order"), (min == null) ? "N/A" : min.Value.ToString("d/MMM/yyyy H:mm:ss", CultureInfo.InvariantCulture), (max == null) ? "N/A" : max.Value.ToString("d/MMM/yyyy H:mm:ss", CultureInfo.InvariantCulture) ); ElapsedTimeHelper.CalculateAndStoreElapsedTimeForCallInSeconds( elapsedTimeInfo, mpID, ElapsedDataMemberType.StoreDataToDatabase, () => Helper.AddEbayOrdersData(databaseCustomerMarketPlace, databaseOrdersList, historyRecord) ); this.log.Debug( "Fetching eBay orders '{0}'@{1}: {2} were stored.", mpID, period, Grammar.Number(databaseOrdersList.Count, "order") ); EbayDatabaseOrdersList allEBayOrders = ElapsedTimeHelper.CalculateAndStoreElapsedTimeForCallInSeconds( elapsedTimeInfo, mpID, ElapsedDataMemberType.RetrieveDataFromDatabase, () => Helper.GetAllEBayOrders(orders.SubmittedDate, databaseCustomerMarketPlace) ); this.log.Debug( "Fetching eBay orders '{0}'@{1}: {2} were loaded from DB.", mpID, period, Grammar.Number(allEBayOrders.Count, "order") ); if (this.settings.DownloadCategories) { IEnumerable <string> topSealedProductItems = GetTopSealedProductItems(allEBayOrders); if (topSealedProductItems != null) { List <MP_EBayOrderItemDetail> orderItemDetails = topSealedProductItems.Select( item => FindEBayOrderItemInfo( databaseCustomerMarketPlace, info, item, databaseOrdersList.RequestsCounter, elapsedTimeInfo ) ).Where(d => d != null) .ToList(); Helper.UpdateOrderItemsInfo(orderItemDetails, elapsedTimeInfo, mpID); } // if } // if var frc = new FetchResultCounters( databaseOrdersList.RequestsCounter, (orders.Orders == null) ? 0 : orders.Orders.Count ); this.log.Debug( "Done fetching eBay orders '{0}'@{1}: {2} fetched.", mpID, period, Grammar.Number(frc.OrderCount, "order") ); return(frc); } // FetchOnePeriodTransactions