Example #1
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 #2
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));
                }
        }