public Transaction[] Parse(string input, Stock userStock, Stock externalStock,
                                   TransactionType defaultOutcome, TransactionType defaultIncome, bool generateMissingStocks = false)
        {
            Balances.Clear();
            var output = new List <Transaction>();

            var transfer      = new Regex(TRANSFER_REGEX);
            var cardOperation = new Regex(CARD_OPERATION_REGEX);

            foreach (Match match in cardOperation.Matches(input))
            {
                //card operation matches to all results
                //transfer is special option (with additional info),
                //so we want to check that info if possible
                if (transfer.IsMatch(match.Value))
                {
                    var m = transfer.Match(match.Value);
                    output.Add(CreateTransaction(m, userStock, externalStock, defaultOutcome, defaultIncome));
                }
                else
                {
                    output.Add(CreateTransaction(match, userStock, externalStock, defaultOutcome, defaultIncome));
                }
            }

            ParseBalanceHelper.AdjustMissingBalances(output, Balances);

            return(output.ToArray());
        }
        public Transaction[] Parse(string input, Stock userStock, Stock externalStock, TransactionType defaultOutcome,
                                   TransactionType defaultIncome, bool generateMissingStocks = false)
        {
            Balances.Clear();
            if (string.IsNullOrEmpty(input))
            {
                return(null);
            }

            var results  = new List <Transaction>();
            var elements = input.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);

            if (elements.Length >= LINES_PER_ENTRY)
            {
                int i = 0;
                while (i + LINES_PER_ENTRY <= elements.Length)
                {
                    try
                    {
                        string   title       = elements[i];
                        DateTime date        = DateTime.Parse(elements[i + 1]);
                        string   stringValue = elements[i + 2].Replace(" PLN", string.Empty).Replace(" ", string.Empty);
                        decimal  value       = decimal.Parse(stringValue);
                        decimal  balance     = decimal.Parse(elements[i + 3].Replace(" ", string.Empty));

                        bool income = value > 0m;

                        var positions   = new[] { new Position(title, Math.Abs(value)) };
                        var transaction = new Transaction(income ? defaultIncome : defaultOutcome, date, title,
                                                          $"Saldo: {balance:#,##0.00}",
                                                          positions, userStock, externalStock);

                        results.Add(transaction);
                        if (!Balances.ContainsKey(userStock))
                        {
                            Balances[userStock] = new Dictionary <DateTime, decimal>();
                        }
                        if (!Balances[userStock].ContainsKey(date))
                        {
                            Balances[userStock][date] = balance;
                        }
                    }
                    catch (Exception)
                    {
                        bool hasTitle             = !string.IsNullOrEmpty(elements[i]);
                        bool isSkippedTransaction = NOT_PERFORMED_TRANSACTION.Equals(elements[i + 1].Trim());
                        if (hasTitle && isSkippedTransaction)
                        {
                            i -= 2;
                        }
                    }

                    i += LINES_PER_ENTRY;
                }
            }

            ParseBalanceHelper.AdjustMissingBalances(results, Balances);

            return(results.ToArray());
        }
Beispiel #3
0
        public Transaction[] Parse(string input, Stock userStock, Stock externalStock,
                                   TransactionType defaultOutcome, TransactionType defaultIncome, bool generateMissingStocks = false)
        {
            Balances.Clear();
            var output = new List <Transaction>();

            var regex = new Regex(RegexValue);

            foreach (Match match in regex.Matches(input))
            {
                output.Add(CreateTransaction(match, userStock, externalStock, defaultOutcome, defaultIncome));
            }

            ParseBalanceHelper.AdjustMissingBalances(output, Balances);

            return(output.ToArray());
        }
        public Transaction[] Parse(string input, Stock userStock, Stock externalStock, TransactionType defaultOutcome,
                                   TransactionType defaultIncome, bool generateMissingStocks = false)
        {
            Balances.Clear();
            if (string.IsNullOrEmpty(input))
            {
                return(null);
            }

            var results = new List <Transaction>();

            try
            {
                var elements = input.Remove(0, input.IndexOf("Zaznacz", StringComparison.Ordinal))
                               .Replace("\t", string.Empty)
                               .Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
                elements = elements.Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
                if (elements.Length >= LINES_PER_ENTRY)
                {
                    int i = 0;
                    while (i + LINES_PER_ENTRY <= elements.Length)
                    {
                        try
                        {
                            string   type          = elements[i + 2].Split(':')[1].Trim();
                            string   title         = elements[i + 3].Split(':')[1].Trim();
                            DateTime date          = DateTime.Parse(elements[i + 1].Split('/')[0].Trim());
                            string   stringValue   = elements[i + 4].Replace(" PLN", string.Empty).Replace(" ", string.Empty).Trim();
                            decimal  value         = decimal.Parse(stringValue);
                            string   balanceString = elements[i + 5]
                                                     .Replace("Saldo:", string.Empty)
                                                     .Replace(" ", string.Empty)
                                                     .Replace("PLN", string.Empty);
                            decimal balance = decimal.Parse(balanceString);

                            bool income = value > 0m;

                            var    positions   = new[] { new Position(title, Math.Abs(value)) };
                            string note        = $"{type}, Saldo: {balance:#,##0.00}";
                            var    transaction = new Transaction(income ? defaultIncome : defaultOutcome, date, title, note,
                                                                 positions, userStock, externalStock);

                            results.Add(transaction);
                            if (!Balances.ContainsKey(userStock))
                            {
                                Balances[userStock] = new Dictionary <DateTime, decimal>();
                            }
                            if (!Balances[userStock].ContainsKey(date))
                            {
                                Balances[userStock][date] = balance;
                            }
                        }
                        catch (Exception e)
                        {
                            _logger.Value.Debug($"Invalid line entry: {string.Join("\n", elements.Skip(i - 1).Take(4))}", e);
                        }

                        i += LINES_PER_ENTRY;
                    }
                }
            }
            catch (Exception e)
            {
                _logger.Value.Debug("Parsing failed", e);
            }

            ParseBalanceHelper.AdjustMissingBalances(results, Balances);

            return(results.ToArray());
        }