Esempio n. 1
0
        private void AddDeliveryCost(BankEntry entry, List <BankEntry> updatedEntries, Myorder allegroEntry,
                                     AllegroDataContainer container)
        {
            if (allegroEntry.delivery.cost.amount != "0.00")
            {
                //lets add a delivery cost as a separate entry, but assign it a category etc. of the most expensive item
                Offer mostExpensive =
                    allegroEntry.offers.OrderByDescending(x => this.converter.ToDecimal(x.offerPrice.amount)).First();

                BankEntry deliveryEntry = BankEntry.Clone(entry);
                deliveryEntry.Amount = this.converter.ToDecimal(allegroEntry.delivery.cost.amount) * -1;
                deliveryEntry.Note   =
                    $"DOSTAWA: {mostExpensive.title} (Oferta {mostExpensive.id}, Suma zamówień: {allegroEntry.offers.Length})";
                deliveryEntry.Recipient = "allegro.pl - " + allegroEntry.seller.login;
                deliveryEntry.Tags.Add("dostawa");
                if (string.IsNullOrEmpty(deliveryEntry.Payer))
                {
                    deliveryEntry.Payer = container.ServiceUserName;
                }

                deliveryEntry.FullDetails +=
                    $"\r\nPłatność Allegro: {allegroEntry.payment.id}.\r\n URL: {mostExpensive.friendlyUrl}";
                updatedEntries.Add(deliveryEntry);
            }
        }
Esempio n. 2
0
        private void HandlePartialRefunds(BankEntry entry, List <BankEntry> updatedEntries, Myorder order,
                                          AllegroDataContainer container)
        {
            List <Offer> offersWithProperPrice =
                order.offers.Where(x => this.converter.ToDecimal(x.offerPrice.amount) == entry.Amount).ToList();

            if (offersWithProperPrice.Any())
            {
                if (offersWithProperPrice.Count == 1)
                {
                    Offer     offer    = offersWithProperPrice.First();
                    BankEntry newEntry = BankEntry.Clone(entry);

                    newEntry.Note = $"ZWROT: {offer.title} (Ilość sztuk: {offer.quantity}, Oferta {offer.id})";

                    newEntry.Payer        = "allegro.pl - " + order.seller.login;
                    newEntry.Recipient    = container.ServiceUserName;
                    newEntry.FullDetails += $"\r\nPłatność Allegro: {order.payment.id}.\r\n URL: {offer.friendlyUrl}";
                    updatedEntries.Add(newEntry);
                }
                else
                {
                    BankEntry newEntry = BankEntry.Clone(entry);

                    newEntry.Note = "ZWROT pasujący do jednej z ofert: " +
                                    string.Join("\r\n", offersWithProperPrice.Select(x => x.title));

                    newEntry.Payer        = "allegro.pl - " + order.seller.login;
                    newEntry.Recipient    = container.ServiceUserName;
                    newEntry.FullDetails += $"\r\nPłatność Allegro: {order.payment.id}.\r\n{newEntry.Note}";
                    updatedEntries.Add(newEntry);
                }
            }
        }
Esempio n. 3
0
        private async Task HandleDownloadedData(ServiceUser serviceUser, OldDataManager oldDataManager,
                                                AllegroDataContainer newData,
                                                AllegroDataContainer oldData, List <AllegroDataContainer> allUsersData)
        {
            oldDataManager.StoreData(newData);

            AllegroDataContainer consolidatedData =
                AllegroDataContainer.Consolidate(new List <AllegroDataContainer>()
            {
                newData, oldData
            });

            allUsersData.Add(consolidatedData);

            if (!processedUsers.Contains(serviceUser.UserName))
            {
                processedUsers.Add(serviceUser.UserName);
                this.logger.Info($"Processed user {serviceUser.UserName}");
                if (processedUsers.Count == this.config.Users.Count)
                {
                    allUsersCompletionCallback(allUsersData);
                }
                else
                {
                    this.currentUserIndex++;
                    await this.GetDataForUser();
                }
            }
        }
Esempio n. 4
0
        public AllegroDataContainer GetOldData()
        {
            List <AllegroDataContainer> sheets = new List <AllegroDataContainer>();

            if (this.dataRetentionDirectory != null)
            {
                foreach (FileInfo fileInfo in this.dataRetentionDirectory.GetFiles("*.json"))
                {
                    try
                    {
                        AllegroDataContainer deserialized = JsonConvert.DeserializeObject <AllegroDataContainer>(File.ReadAllText(fileInfo.FullName));
                        if (deserialized.ServiceUserName == this.serviceUserConfig.UserName && deserialized.Model.myorders != null)
                        {
                            sheets.Add(deserialized);
                        }
                    }
                    catch (Exception ex)
                    {
                        this.logger.Warning($"Exception while loading old data from {fileInfo.Name}. {ex.Message}");
                    }
                }
            }

            if (sheets.Any())
            {
                return(AllegroDataContainer.Consolidate(sheets));
            }

            return(null);
        }
Esempio n. 5
0
        private async Task GetDataForUser()
        {
            ServiceUser          serviceUser    = this.config.Users[currentUserIndex];
            OldDataManager       oldDataManager = new OldDataManager(serviceUser, this.logger);
            AllegroDataContainer oldData        = oldDataManager.GetOldData();

            DateTime oldestEntryAdjusted = AdjustOldestEntryToDownloadBasedOnOldData(this.oldestEntryToDownload, oldData);

            await this.dataLoader.GetData(serviceUser, oldestEntryAdjusted, async newData
                                          => await HandleDownloadedData(serviceUser, oldDataManager, newData, oldData, allUsersData)
                                          );
        }
Esempio n. 6
0
 public void StoreData(AllegroDataContainer container)
 {
     if (this.dataRetentionDirectory != null)
     {
         List <AllegroDataContainer> allegroDataContainers = AllegroDataContainer.SplitPerMonth(container);
         foreach (AllegroDataContainer allegroDataContainer in allegroDataContainers)
         {
             string path = Path.Combine(this.dataRetentionDirectory.FullName,
                                        $"{allegroDataContainer.ServiceUserName} {allegroDataContainer.OldestEntry:yyyy-MM-dd} {allegroDataContainer.NewestEntry:yyyy-MM-dd}.json");
             string serialized = JsonConvert.SerializeObject(allegroDataContainer);
             File.WriteAllText(path, serialized);
         }
     }
 }
Esempio n. 7
0
        private BankEntry PrepareNewBankEntryForOffer(Myorder allegroEntry, int offerIndex, BankEntry entry,
                                                      AllegroDataContainer container)
        {
            Offer     offer    = allegroEntry.offers[offerIndex];
            BankEntry newEntry = BankEntry.Clone(entry);

            newEntry.Amount = this.converter.ToDecimal(offer.offerPrice.amount);

            newEntry.Note = $"ZWROT: {offer.title} (Ilość sztuk: {offer.quantity}, Oferta {offer.id}, Pozycja {offerIndex + 1}/{allegroEntry.offers.Length})";

            newEntry.Payer        = "allegro.pl - " + allegroEntry.seller.login;
            newEntry.Recipient    = container.ServiceUserName;
            newEntry.FullDetails += $"\r\nPłatność Allegro: {allegroEntry.payment.id}.\r\n URL: {offer.friendlyUrl}";
            return(newEntry);
        }
Esempio n. 8
0
        public static List <AllegroDataContainer> SplitPerMonth(AllegroDataContainer container)
        {
            var list = new List <AllegroDataContainer>();
            IEnumerable <IGrouping <string, Myorder> > groupings =
                container.Model.myorders.myorders.GroupBy(x => x.orderDate.ToString("yyyy-MM"));

            foreach (IGrouping <string, Myorder> grouping in groupings)
            {
                AllegroDataContainer clone = container.Clone();
                clone.Model.myorders.myorders = grouping.ToArray();
                clone.Model.myorders.total    = clone.Model.myorders.myorders.Length;
                clone.AssignTimeRange();
                list.Add(clone);
            }

            return(list);
        }
Esempio n. 9
0
 private static void EnrichUnrecognizedAllegroOffer(BankEntry entry, AllegroDataContainer container, List <Offer> potentiallyRelatedOfferIds)
 {
     entry.Payer = "allegro.pl (Nierozpoznany sprzedawca)";
     if (!string.IsNullOrEmpty(container?.ServiceUserName))
     {
         entry.Recipient = container.ServiceUserName;
     }
     else
     {
         entry.Recipient = "Nierozpoznany odbiorca";
     }
     if (potentiallyRelatedOfferIds != null && potentiallyRelatedOfferIds.Any())
     {
         entry.Note = "ZWROT pasujący do jednej z ofert: " + string.Join("\r\n", potentiallyRelatedOfferIds.Select(x => x.title));
     }
     else
     {
         entry.Note = "Nierozpoznany zwrot";
     }
 }
Esempio n. 10
0
        private List <Myorder> GetRelevantOrders(BankEntry entry, List <AllegroDataContainer> allegroDataContainers, out AllegroDataContainer associatedContainer, out List <Offer> potentiallyRelatedOfferIds)
        {
            associatedContainer = allegroDataContainers.FirstOrDefault(x => x.ServiceUserName == entry.Payer);
            AllegroData  model = associatedContainer?.Model;
            List <Offer> offersWhichMatchThePriceButNotTheDateBecauseTheyAreRefunds = null;

            List <Myorder> result = null;

            if (model != null)
            {
                result = this.GetAllegroEntries(entry, model, out _);
            }

            if (result != null)
            {
                potentiallyRelatedOfferIds = null;
                return(result);
            }
            else
            {
                //the payer can be empty or it can be somehow incorrect, but if we have an entry that matches the exact price and date... it's probably IT
                foreach (AllegroDataContainer container in allegroDataContainers)
                {
                    List <Myorder> entries = this.GetAllegroEntries(entry, container.Model, out List <Offer> offersMatchingPrice);
                    if (offersMatchingPrice != null && offersMatchingPrice.Any())
                    {
                        offersWhichMatchThePriceButNotTheDateBecauseTheyAreRefunds = new List <Offer>(offersMatchingPrice);
                    }
                    if (entries != null && entries.Any())
                    {
                        associatedContainer        = container;
                        potentiallyRelatedOfferIds = null;
                        return(entries);
                    }
                }
            }

            potentiallyRelatedOfferIds = offersWhichMatchThePriceButNotTheDateBecauseTheyAreRefunds?.ToList();
            return(null);
        }
Esempio n. 11
0
        /// <summary>
        /// Don't download old data if older or equal data is already stored
        /// </summary>
        /// <param name="oldestEntry"></param>
        /// <param name="oldData"></param>
        /// <returns></returns>
        private static DateTime AdjustOldestEntryToDownloadBasedOnOldData(DateTime oldestEntry, AllegroDataContainer oldData)
        {
            if (oldData != null)
            {
                if (oldData.OldestEntry <= oldestEntry)
                {
                    oldestEntry = oldData.NewestEntry;
                }
            }

            return(oldestEntry);
        }
Esempio n. 12
0
        private List <Myorder> GetRelevantOrders(BankEntry entry, List <AllegroDataContainer> allegroDataContainers, out AllegroDataContainer associatedContainer)
        {
            //get data model for the payer
            associatedContainer = allegroDataContainers.FirstOrDefault(x => x.ServiceUserName == entry.Payer);
            string      associatedUserName = associatedContainer?.ServiceUserName;
            AllegroData model = associatedContainer?.Model;

            List <Myorder> result = null;

            if (model != null)
            {
                result = this.GetAllegroOrders(entry, model);
            }
            //model might be not-null but the result might be null
            if (result != null)
            {
                return(result);
            }
            else
            {
                //the payer can be empty or it can be somehow incorrect, but if we have an order that matches the exact price and date... it's probably IT
                //so try finding order in any persons data
                List <AllegroDataContainer> alternativeContainers = FindAlternativeDataContainers(allegroDataContainers, associatedUserName);

                foreach (AllegroDataContainer container in alternativeContainers)
                {
                    List <Myorder> allegroOrders = this.GetAllegroOrders(entry, container.Model);
                    if (allegroOrders != null && allegroOrders.Any())
                    {
                        associatedContainer = container;
                        return(allegroOrders);
                    }
                }
            }
            return(null);
        }