Example #1
0
        private static void generateListForWarehouse(Arguments arguments)
        {
            if (!arguments.HasArgument("gwl"))
            {
                "You need to supply an output filepath (which should be an xlsx) to write the list to".ConsoleWriteLine();
            }
            else if (!arguments.ArgumentDictionary.TryGetValue("gwl", out var filename) || string.IsNullOrEmpty(filename?.ToString()))
            {
                "No file name provided for generating warehouse list".ConsoleWriteLine();
            }
            else if (!File.Exists(filename.ToString()))
            {
                $"Could not find file '{filename.ToString()}' as source for generating warehouse list".ConsoleWriteLine();
            }
            else
            {
                filename.ToString().ConsoleWriteLine();
                var lines = File.ReadLines(filename.ToString())
                            .Select(x => x.Split(','))
                            .Select(x => (Id: x[0].Replace("-", string.Empty), Quantity: int.Parse(x[1])))
                            .ToList();

                var records = KeepaAPI.GetDetailsForIdentifiers(lines.Select(x => x.Id).ToArray());

                lines.Select(x =>
                {
                    records.TryGetValue(x.Id, out var rec);
                    return(x.Id, x.Quantity, rec);
                })
                .WriteWarehouseFile(arguments.GetArgument("gwl"));
            }
        }
Example #2
0
        private static void generateSalesReport(Arguments arguments)
        {
            string encaseStringWithComma(string input_) =>
            ((!string.IsNullOrEmpty(input_) && input_.Contains(',')) ||
             (!string.IsNullOrEmpty(input_) && int.TryParse(input_, out var _)))
                    ? $"\"{input_}\""
                    : input_;

            var targetFile = arguments.GetArgument(Args.Keys.GenerateSalesReportKey);

            if (string.IsNullOrEmpty(targetFile))
            {
                "Where should the salesreport (csv) be saved to (full path)? :".ConsoleWriteLine();
                targetFile = Console.ReadLine();
            }

            if (SalesBinderAPI.Invoices == null)
            {
                "No invoices found.  Have you downloaded them yet?".ConsoleWriteLine();
            }
            else if (SalesBinderAPI.Inventory == null)
            {
                "No inventory found.  Have you downloaded them yet?".ConsoleWriteLine();
            }
            else if (SalesBinderAPI.Accounts == null)
            {
                "No accounts found.  Have you downloaded them yet?".ConsoleWriteLine();
            }
            else
            {
                var invoicesByAllItems = SalesBinderAPI.Invoices
                                         .SelectMany(inv => inv.Items.Select(i => (Invoice: inv, Item: i)));

                var inventoryLookup = SalesBinderAPI.InventoryById;
                var accountLookup   = SalesBinderAPI.AccountById;

                var rows = invoicesByAllItems.Select(i =>
                {
                    inventoryLookup.TryGetValue(i.Item.ItemId, out var book);
                    accountLookup.TryGetValue(i.Invoice.CustomerId, out var account);
                    var keepaRecord = book == null ? null : KeepaAPI.GetRecordForIdentifier(book.BarCode);

                    return(Invoice: i.Invoice, SalesItem: i.Item, Book: book, Account: account, Keepa: keepaRecord);
                })
                           .ToList();

                string getItemAt(string[] arr_, int index_) =>
                arr_ == null || arr_.Length < (index_ + 1) ? null : arr_[index_];

                var setups =
                    new (string Title, Func <(SalesBinderInvoice Invoice, SalesBinderInvoiceItem SalesItem,
                                              SalesBinderInventoryItem Book, SalesBinderAccount Account, KeepaRecord Keepa), string> Func)[]
Example #3
0
        private static void keepaLookupPrimeRecords()
        {
            var items = SalesBinderAPI.Inventory.Where(x => !string.IsNullOrEmpty(x.BarCode))
                        .Where(x => KeepaAPI.LastLookupTime(x.BarCode) == null);

            if (!items.Any())
            {
                $"Have already tried to get keepa records for all items in inventory".ConsoleWriteLine();
            }
            else
            {
                var ids = items.Take(100).Select(x => x.BarCode).Distinct().ToArray();
                KeepaAPI.GetDetailsForIdentifiers(ids);
            }
        }
Example #4
0
        private static void keepaLookupRefreshCurrentInventory()
        {
            var items = SalesBinderAPI.Inventory.Where(x => x.Quantity > 0 && !string.IsNullOrEmpty(x.BarCode))
                        .Select(x => (Book: x, LastLookup: KeepaAPI.LastLookupTime(x.BarCode)))
                        .Where(x => x.LastLookup == null || (DateTime.Now - x.LastLookup.Value).TotalDays > 1)
                        .Select(x => x.Book)
                        .ToArray();

            // only want to do 100 as don't want to blow limits on keepa
            if (!items.Any())
            {
                "Nothing to do - have been updated or attempted to be updated at least once in the last 24 hours"
                .ConsoleWriteLine();
            }
            else
            {
                var ids = items.Take(100).Select(x => x.BarCode).Distinct().ToArray();
                KeepaAPI.GetDetailsForIdentifiers(ids, forceRefresh_: true);
            }
        }
Example #5
0
        private static void salesBinderInventoryList(Arguments arguments, string arg_, bool onlyCurrent_)
        {
            var targetPath = arguments.GetArgument(arg_);

            if (string.IsNullOrEmpty(targetPath))
            {
                "Where should the inventory list be saved to (fullpath to csv)?: "
                .ConsoleWriteLine();
                targetPath = Console.ReadLine();
            }

            var list = SalesBinderAPI.RetrieveAndSaveInventory(true);

            if (onlyCurrent_)
            {
                list = list.Where(x => x.Quantity > 0).ToArray();
            }

            if (Args.KeepaAugmentList)
            {
                var excludeFromtree = new HashSet <string>(new[] { "Books", "Subjects" });
                var excludeBinding  = new HashSet <string>(new[] { "Kindle Edition" });

                list.Where(x => !string.IsNullOrEmpty(x.BarCode))
                .Select(x => (Item: x, Keepa: KeepaAPI.GetRecordForIdentifier(x.BarCode)))
                .Where(x => x.Keepa != null)
                .ForEach(l =>
                {
                    if (string.IsNullOrEmpty(l.Item.Publisher))
                    {
                        l.Item.Publisher = l.Keepa.Manufacturer;
                    }

                    if (!string.IsNullOrEmpty(l.Keepa.Binding) && !excludeBinding.Contains(l.Keepa.Binding))
                    {
                        l.Item.Style = l.Keepa.Binding;
                    }

                    if (l.Keepa.CategoryTree != null)
                    {
                        var tree = l.Keepa.CategoryTree.Where(x => !excludeFromtree.Contains(x));

                        if (tree.Any())
                        {
                            l.Item.ProductType  = string.Join(" / ", tree);
                            l.Item.ProductType2 = tree.Last();
                        }

                        l.Item.KidsOrAdult =
                            l.Keepa.CategoryTree.Any(x => x.ToLower().Contains("child"))
                                    ? "Kids"
                                    : "Adult";
                    }
                });
            }

            using (var writer = new StreamWriter(targetPath))
                using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                {
                    csv.WriteRecords(list
                                     .OrderByDescending(x => Math.Abs(x.Quantity))
                                     .ThenBy(x => x.Name));
                }
        }