示例#1
0
        public Value ConvertCommand(CallScope args)
        {
            Report  report  = args.Context <Report>();
            Journal journal = report.Session.Journal;

            string bucketName;

            if (report.AccountHandler.Handled)
            {
                bucketName = report.AccountHandler.Str();
            }
            else
            {
                bucketName = "Equity:Unknown";
            }

            Account bucket  = journal.Master.FindAccount(bucketName);
            Account unknown = journal.Master.FindAccount("Expenses:Unknown");

            // Create a flat list
            IList <Xact> currentXacts = new List <Xact>(journal.Xacts);

            // Read in the series of transactions from the CSV file

            PrintXacts formatter   = new PrintXacts(report);
            string     csvFilePath = args.Get <string>(0);

            report.Session.ParsingContext.Push(csvFilePath);
            ParseContext context = report.Session.ParsingContext.GetCurrent();

            context.Journal = journal;
            context.Master  = bucket;

            CsvReader reader = new CsvReader(context);

            try
            {
                Xact xact;
                while ((xact = reader.ReadXact(report.RichDataHandler.Handled)) != null)
                {
                    if (report.InvertHandler.Handled)
                    {
                        foreach (Post post in xact.Posts)
                        {
                            post.Amount.InPlaceNegate();
                        }
                    }

                    string sref = xact.HasTag("UUID") ? xact.GetTag("UUID").ToString() : SHA1.GetHash(reader.LastLine);

                    if (journal.ChecksumMapping.ContainsKey(sref))
                    {
                        continue;
                    }

                    if (report.RichDataHandler.Handled && !xact.HasTag("UUID"))
                    {
                        xact.SetTag("UUID", Value.StringValue(sref));
                    }

                    if (xact.Posts.First().Account == null)
                    {
                        Account acct = report.AutoMatchHandler.Handled ? Lookup.LookupProbableAccount(xact.Payee, currentXacts.Reverse(), bucket).Item2 : null;
                        if (acct != null)
                        {
                            xact.Posts.First().Account = acct;
                        }
                        else
                        {
                            xact.Posts.First().Account = unknown;
                        }
                    }

                    if (!journal.AddXact(xact))
                    {
                        throw new RuntimeError(RuntimeError.ErrorMessageFailedToFinalizeDerivedTransactionCheckCommodities);
                    }
                    else
                    {
                        XactPostsIterator xactIter = new XactPostsIterator(xact);
                        foreach (Post post in xactIter.Get())
                        {
                            formatter.Handle(post);
                        }
                    }
                }
                formatter.Flush();
            }
            catch
            {
                ErrorContext.Current.AddErrorContext(String.Format("While parsing file {0}", ErrorContext.FileContext(reader.PathName, reader.LineNum)));
                ErrorContext.Current.AddErrorContext("While parsing CSV line:");
                ErrorContext.Current.AddErrorContext(ErrorContext.LineContext(reader.LastLine));
                throw;
            }

            // If not, transform the payee according to regexps

            // Set the account to a default vaule, then transform the account according
            // to the payee

            // Print out the final form of the transaction

            return(Value.True);
        }
示例#2
0
        /// <summary>
        /// Ported from debug_assert
        /// </summary>
        public static void DebugAssert(string reason, string nspace, string classname, string methodname, string file, long line)
        {
            var message = String.Format("Assertion failed in {0} [namespace {1}, class {2}, method {3}]; assertion function {4}.", ErrorContext.FileContext(file, line),
                                        nspace, classname, methodname, reason);

            throw new AssertionFailedError(message);
        }