コード例 #1
0
        private static void OnTimer()
        {
            GetOrdersCall       getOrdersApiCall = new GetOrdersCall(apiContext);
            OrderTypeCollection orders           = null;

            try
            {
                orders = getOrdersApiCall.GetOrders(new TimeFilter {
                    TimeFrom = DateTime.Now.AddDays(DaysAgo), TimeTo = DateTime.Now
                }, TradingRoleCodeType.Seller, OrderStatusCodeType.Completed);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unable to connect to the server");
                return;
            }

            int  counter       = 0;
            var  ordersList    = new List <HeaderRecords>();
            bool orderInserted = false;

            foreach (OrderType order in orders)
            {
                try
                {
                    if (!DAL.HeaderRecords.Exists(order.OrderID))
                    {
                        orderInserted = true;
                        #region Header record
                        var headerRecord = new HeaderRecords
                        {
                            OrderID             = order.OrderID,
                            InvoiceID           = order.ShippingDetails.SellingManagerSalesRecordNumber.ToString(),
                            OrderDate           = order.PaidTime.ToShortDateString(),
                            Email               = order.TransactionArray[0].Buyer.Email, //? it gives the value of "invalid request"
                            BilltoFirstName     = order.ShippingAddress.Name,
                            BilltoLastName      = order.ShippingAddress.Name,
                            BilltoCompanyName   = order.BuyerUserID,
                            DeliveryMethod      = order.ShippingServiceSelected.ShippingService == "ShippingMethodStandard" ? "1" : "5",
                            ShippingAndHandling = order.ShippingServiceSelected.ShippingServiceCost.Value.ToString(),
                            Tax                   = order.ShippingDetails.SalesTax.SalesTaxAmount.Value.ToString(),
                            Discount              = "0",
                            OverallTotal          = order.Total.Value.ToString(),
                            ShiptoFirstName       = order.ShippingAddress.Name,
                            ShiptoLastName        = order.ShippingAddress.Name,
                            ShiptoCompanyName     = order.BuyerUserID,
                            ShiptoStreetAddress   = order.ShippingAddress.Street1,
                            ShiptoOptionalAddress = order.ShippingAddress.Street2,
                            ShiptoCity            = order.ShippingAddress.CityName,
                            ShiptoState           = order.ShippingAddress.StateOrProvince,
                            ShiptoZip             = order.ShippingAddress.PostalCode,
                            ShiptoCountry         = order.ShippingAddress.CountryName,
                            ShiptoPhone           = order.ShippingAddress.Phone,
                            CatalogName           = "EBAY",
                            Source                = "EBAY"
                        };
                        #endregion

                        List <DAL.DetailRecord> detailRecordList = new List <DAL.DetailRecord>();
                        foreach (TransactionType transactionItem in order.TransactionArray)
                        {
                            #region Detail record
                            var dr = new DAL.DetailRecord
                            {
                                InvoiceID          = headerRecord.InvoiceID,
                                PurchaseID         = transactionItem.ShippingDetails.SellingManagerSalesRecordNumber.ToString(),
                                SourceCode         = transactionItem.Item.Site.ToString(),
                                ItemID             = transactionItem.Item.ItemID,
                                ProductDescription = transactionItem.Item.Title,
                                Quantity           = transactionItem.QuantityPurchased,
                                UnitPrice          = transactionItem.TransactionPrice.Value,
                                ExtendedPrice      = transactionItem.TransactionPrice.Value * transactionItem.QuantityPurchased
                            };

                            detailRecordList.Add(dr);
                            #endregion
                        }

                        headerRecord.ProductSubtotal = detailRecordList.Sum(x => x.ExtendedPrice).ToString();
                        headerRecord.Commision       = GetCommission(headerRecord.ProductSubtotal);
                        try
                        {
                            #region Inserting objects in database
                            using (var scope = new TransactionScope())
                            {
                                DAL.HeaderRecords.Add(headerRecord);
                                SetSkuCodeForDetailsRecord(detailRecordList);

                                DAL.DetailRecord.Add(detailRecordList);
                                headerRecord.Items.AddRange(detailRecordList);

                                var orderStatus = new DAL.OrderStatus {
                                    OrderID = order.OrderID, Status = 1
                                };
                                DAL.OrderStatus.Add(orderStatus);

                                var orderMessage = new DAL.OrderMessage {
                                    InvoiceID = headerRecord.InvoiceID
                                };
                                DAL.OrderMessage.Add(orderMessage);

                                scope.Complete();
                                Console.WriteLine("Order #: " + counter + " has been inserted");
                            }
                            #endregion
                        }

                        catch (TransactionAbortedException)
                        {
                            Console.WriteLine("TransactionAbortedException has been thrown");
                        }

                        ordersList.Add(headerRecord);
                    }
                    else
                    {
                        Console.WriteLine("Order #: " + counter + " has been skipped");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(string.Format("An error occured at the step #: {0}; message: {1}", counter, e.Message));
                }

                finally
                {
                    counter++;
                }
            }

            if (orderInserted)
            {
                CreateXmlFile(ordersList);
            }

            RestartTimer();
        }