Exemplo n.º 1
0
    private List<ParcelItem> GetEbayAccountTransactions(EbayServiceBL service, KeyValuePair<int, string> account, int UserCode)
    {
        int accountID = account.Key;
        DataModelEntities context = new DataModelEntities();
        List<PriceManagerDAL.ParcelItem> parcelItems = context.ParcelItems.Where(f => f.AccountID == accountID && f.User_Code == UserCode && f.Is_Active == true).ToList();

        string currentItemID = string.Empty, currentTransactionID = string.Empty, currentRecordNo = string.Empty;

        SellingManagerSoldOrderType[] results = service.GetPendingShipmentItems(account.Value);

        if(results == null)
            results = new SellingManagerSoldOrderType[0];

        // delete all database entries that does not exist in the API results.
        foreach (PriceManagerDAL.ParcelItem existingItem in parcelItems)
        {
            long transactionID = long.Parse(existingItem.TransactionID);
            SellingManagerSoldOrderType checkResult = results.FirstOrDefault(r => r.SellingManagerSoldTransaction.Count(c => c.ItemID == existingItem.ItemID && c.TransactionID == transactionID) > 0);
            if (checkResult == null)
            {
                context.ParcelItems.DeleteObject(existingItem);
            }
        }

        // delete entries which do not have correct shipping details
        foreach (PriceManagerDAL.ParcelItem existingItem in parcelItems.Where(p => string.IsNullOrEmpty(p.BuyerName) == true))
        {
            context.ParcelItems.DeleteObject(existingItem);
        }

        // now traverse through API results and save only those which are new
        List<ParcelItem> items = new List<ParcelItem>();
        if (results != null && results.Length > 0)
        {
            List<ChargeCode> chargeCodes = new DataModelEntities().ChargeCodes.Where(u => u.Is_Active == true && u.User_Code == UserCode).ToList();
            foreach (SellingManagerSoldOrderType result in results)
            {
                foreach (SellingManagerSoldTransactionType transaction in result.SellingManagerSoldTransaction)
                {
                    try
                    {

                        currentItemID = transaction.ItemID;
                        currentTransactionID = transaction.TransactionID.ToString();

                        // ignore if the item already exists in our database
                        if (context.ParcelItems.FirstOrDefault(f => f.AccountID == accountID && f.ItemID == currentItemID && f.TransactionID == currentTransactionID) != null)
                        {
                            continue;
                        }

                        ParcelItem item = new ParcelItem();
                        item.Type = "EBAY";

                        SellingManagerSoldOrderType itemDetails = service.GetSaleRecordDetails(transaction.ItemID, transaction.TransactionID.ToString(), account.Value);

                        currentRecordNo = itemDetails.SaleRecordID.ToString();

                        item.AccountID = account.Key.ToString();
                        item.ItemID = transaction.ItemID;
                        item.TransactionID = transaction.TransactionID.ToString();
                        item.ItemName = transaction.ItemTitle;
                        item.CustomLabel = transaction.CustomLabel;
                        item.CustomLabelText = transaction.SaleRecordID.ToString() + ":" + transaction.CustomLabel;
                        if (itemDetails.ShippingAddress != null)
                        {
                            if (StateHelper.States.Where(s => s.Key == itemDetails.ShippingAddress.StateOrProvince.ToLower()).Count() > 0)
                                item.State = StateHelper.States[itemDetails.ShippingAddress.StateOrProvince.ToLower()];
                            else
                                item.State = itemDetails.ShippingAddress.StateOrProvince.ToLower();
                            item.BuyerName = itemDetails.ShippingAddress.Name;
                            item.Street = itemDetails.ShippingAddress.Street;
                            item.Street2 = itemDetails.ShippingAddress.Street1;
                            item.Street3 = itemDetails.ShippingAddress.Street2;
                            item.City = itemDetails.ShippingAddress.CityName;
                            item.PostalCode = itemDetails.ShippingAddress.PostalCode.TrimStart('0'); ;
                            item.Country = itemDetails.ShippingAddress.Country.ToString();
                            item.Phone = itemDetails.ShippingAddress.Phone;
                        }
                        else
                        {

                        }
                        item.EmailAddress = itemDetails.BuyerEmail;
                        item.BuyerID = itemDetails.BuyerID;
                        item.Quantity = transaction.QuantitySold;
                        if (itemDetails.ActualShippingCost != null)
                            item.ShippingCost = itemDetails.ActualShippingCost.Value;
                        item.SaleRecordId = itemDetails.SaleRecordID.ToString();

                        bool IspostCodeOK = true;
                        if (itemDetails.ShippingDetails != null)
                        {
                            if (itemDetails.ShippingDetails.InsuranceFee == null)
                                item.HasInsurance = false;
                            else
                            {
                                item.HasInsurance = true;
                                item.Insurance = itemDetails.ShippingDetails.InsuranceFee.Value;
                            }
                            if (itemDetails.ShippingDetails != null && itemDetails.ShippingDetails.ShippingServiceOptions != null)
                                item.ShippingMethod = itemDetails.ShippingDetails.ShippingServiceOptions[0].ShippingService;
                            else
                                item.ShippingMethod = "N/A";

                            IspostCodeOK = Common.VerifyPostCode(itemDetails.ShippingAddress.PostalCode, itemDetails.ShippingAddress.CityName);
                            if (IspostCodeOK)
                                item.PostCodeImageURL = Constant.tickURL;
                            else
                                item.PostCodeImageURL = Constant.crossURL;
                        }

                        if (itemDetails.SellingManagerSoldTransaction != null)
                        {
                            item.Currency = itemDetails.SellingManagerSoldTransaction[0].ItemPrice.currencyID.ToString();
                            item.Price = itemDetails.SellingManagerSoldTransaction[0].ItemPrice.Value;
                        }

                        item.RecordNumber = itemDetails.SaleRecordID.ToString();

                        ChargeCode code = chargeCodes.FirstOrDefault(u => item.ShippingMethod.ToLower().Contains(u.Ebay_Code.ToLower()) == true);
                        if (code != null && code.Charge_Code_Name.ToLower() == "ignore")
                        {
                            continue; // ignore the item
                        }

                        GetMemberMessagesResponseType messages = service.GetTransactionMessages(item.ItemID, item.BuyerID, account.Value);
                        if (messages.MemberMessage != null)
                        {
                            List<ParcelMessage> ebayMessages = service.ConvertEbayMessages(messages);
                            item.Messages = Common.Serialize(ebayMessages);
                        }

                        items.Add(item);

                        currentItemID = string.Empty;
                        currentRecordNo = string.Empty;
                        currentTransactionID = string.Empty;
                    }
                    catch (Exception ex)
                    {
                        Logging.WriteLog(LogType.Critical, ex.ToString());
                    }
                }
            }
            context.SaveChanges();
            return items.OrderBy(i => i.BuyerID).ToList();
        }
        else
        {
            context.SaveChanges();
            return null;
        }
    }