Пример #1
0
        public IEnumerable <Transaction> GetCsvData(int year, int month, string accountName, IEnumerable <char> data, string sourceFilename,
                                                    bool applyMap = false)
        {
            var account = _accountService.Get(accountName);

            if (account == null)
            {
                throw new ArgumentException($"{accountName} does not identify an account", nameof(accountName));
            }
            var tokens  = new CsvTokens(data);
            var records = new CsvRecords(tokens);
            Func <string, string> mapDescriptionToCategory;

            if (applyMap)
            {
                var map = _mapService.Get().ToDictionary(m => m.Category, m => m.Patterns.Select(p => new Regex(p)).ToList());
                mapDescriptionToCategory = s => MapDescription(map, s);
            }
            else
            {
                mapDescriptionToCategory = s => null;
            }
            var transactionParser = account.GetTransactionParser(year, month, Path.GetFileName(sourceFilename),
                                                                 mapDescriptionToCategory);

            return(new CsvData <Transaction>(records, transactionParser));
        }
        public IEnumerable <KeyValuePair <Map, decimal?> > Get(int year, int month)
        {
            var map          = _mapService.Get();
            var transactions = _transactionService.Get(year, month).ToList();

            return(map
                   .GroupJoin(transactions, m => m.Category, t => t.Category,
                              (m, ts) => new { map = m, transactions = ts.ToList() })
                   .Select(m =>
            {
                decimal?sum;
                if (m.transactions.Count == 0)
                {
                    sum = null;
                }
                else
                {
                    sum = m.transactions.Select(t => (t.MoneyIn ?? 0) - (t.MoneyOut ?? 0)).Sum();
                }
                return new KeyValuePair <Map, decimal?>(m.map, sum);
            }));
        }