示例#1
0
文件: Post.cs 项目: maznabili/nledger
        /// <summary>
        /// Ported from post_t::primary_date()
        /// </summary>
        public override Date PrimaryDate()
        {
            if (HasXData && XData.Date.IsValid())
            {
                return(XData.Date);
            }

            if (!Date.HasValue)
            {
                if (Xact == null)
                {
                    throw new InvalidOperationException("Xact is empty");
                }

                return(Xact.GetDate());
            }

            return(Date.Value);
        }
示例#2
0
        public virtual void WriteXact(Xact xact)
        {
            if (xact.Pos != null)
            {
                Out.AppendFormat("\"{0}\" {1} ", xact.Pos.PathName, xact.Pos.BegLine);
            }
            else
            {
                Out.Append("\"\" -1 ");
            }

            DateTime when = xact.GetDate().FromLocalToUtc();
            int      date = when.ToPosixTime();

            Out.AppendFormat("({0} {1} 0) ", date / 65536, date % 65536);

            if (!String.IsNullOrEmpty(xact.Code))
            {
                Out.AppendFormat("\"{0}\" ", xact.Code);
            }
            else
            {
                Out.Append("nil ");
            }

            if (String.IsNullOrEmpty(xact.Payee))
            {
                Out.Append("nil");
            }
            else
            {
                Out.AppendFormat("\"{0}\"", xact.Payee);
            }

            Out.AppendLine();
        }
示例#3
0
        /// <summary>
        /// Ported from print_xact
        /// </summary>
        public static string PrintXact(Report report, Xact xact)
        {
            StringBuilder sbOut = new StringBuilder();

            FormatTypeEnum formatType = FormatTypeEnum.FMT_WRITTEN;
            string         format     = null;

            if (report.DateFormatHandler.Handled)
            {
                formatType = FormatTypeEnum.FMT_CUSTOM;
                format     = report.DateFormatHandler.Str();
            }

            StringBuilder buf = new StringBuilder();

            buf.Append(TimesCommon.Current.FormatDate(Item.UseAuxDate ? xact.GetDate() : xact.PrimaryDate(), formatType, format));
            if (!Item.UseAuxDate && (xact.DateAux.HasValue))
            {
                buf.AppendFormat("={0}", TimesCommon.Current.FormatDate(xact.DateAux.Value, formatType, format));
            }

            buf.Append(" ");

            buf.Append(xact.State == ItemStateEnum.Cleared ? "* " : (xact.State == ItemStateEnum.Pending ? "! " : ""));

            if (!String.IsNullOrEmpty(xact.Code))
            {
                buf.AppendFormat("({0}) ", xact.Code);
            }

            buf.Append(xact.Payee);

            string leader = buf.ToString();

            sbOut.Append(leader);

            int columns = report.ColumnsHandler.Handled ? int.Parse(report.ColumnsHandler.Str()) : 80;

            if (!String.IsNullOrEmpty(xact.Note))
            {
                sbOut.Append(PrintNote(xact.Note, xact.Flags.HasFlag(SupportsFlagsEnum.ITEM_NOTE_ON_NEXT_LINE), columns, leader.Length));
            }
            sbOut.AppendLine();

            if (xact.GetMetadata() != null)
            {
                foreach (KeyValuePair <string, ItemTag> data in xact.GetMetadata())
                {
                    if (!data.Value.IsParsed)
                    {
                        sbOut.Append("    ; ");
                        if (!Value.IsNullOrEmptyOrFalse(data.Value.Value))
                        {
                            sbOut.AppendFormat("{0}: {1}", data.Key, data.Value.Value.ToString());
                        }
                        else
                        {
                            sbOut.AppendFormat(":{0}: ", data.Key);
                        }
                        sbOut.AppendLine();
                    }
                }
            }

            int count = xact.Posts.Count;
            int index = 0;

            foreach (Post post in xact.Posts)
            {
                index++;

                if (!report.GeneratedHandler.Handled && (post.Flags.HasFlag(SupportsFlagsEnum.ITEM_TEMP) || post.Flags.HasFlag(SupportsFlagsEnum.ITEM_GENERATED)) &&
                    !post.Flags.HasFlag(SupportsFlagsEnum.POST_ANONYMIZED))
                {
                    continue;
                }

                sbOut.Append("    ");

                StringBuilder pbuf = new StringBuilder();

                if (xact.State == ItemStateEnum.Uncleared)
                {
                    pbuf.Append(post.State == ItemStateEnum.Cleared ? "* " : (post.State == ItemStateEnum.Cleared ? "! " : ""));
                }

                if (post.Flags.HasFlag(SupportsFlagsEnum.POST_VIRTUAL))
                {
                    pbuf.Append(post.Flags.HasFlag(SupportsFlagsEnum.POST_MUST_BALANCE) ? "[" : "(");
                }

                pbuf.Append(post.Account.FullName);

                if (post.Flags.HasFlag(SupportsFlagsEnum.POST_VIRTUAL))
                {
                    pbuf.Append(post.Flags.HasFlag(SupportsFlagsEnum.POST_MUST_BALANCE) ? "]" : ")");
                }

                string name = pbuf.ToString();

                int accountWidth = report.AccountWidthHandler.Handled ? int.Parse(report.AccountWidthHandler.Str()) : 36;

                if (accountWidth < name.Length)
                {
                    accountWidth = name.Length;
                }

                if (!post.Flags.HasFlag(SupportsFlagsEnum.POST_CALCULATED) || report.GeneratedHandler.Handled)
                {
                    sbOut.Append(name);
                    int slip = accountWidth - name.Length;

                    int amountWidth = report.AmountWidthHandler.Handled ? int.Parse(report.AmountWidthHandler.Str()) : 12;

                    string amt = String.Empty;
                    if (post.AmountExpr != null)
                    {
                        amt = UniString.Justify(post.AmountExpr.Text, amountWidth, true);
                    }
                    else if (count == 2 && index == 2 && PostHasSimpleAmount(post) && PostHasSimpleAmount(xact.Posts.First()) &&
                             xact.Posts.First().Amount.Commodity == post.Amount.Commodity)
                    {
                        // If there are two postings and they both simple amount, and
                        // they are both of the same commodity, don't bother printing
                        // the second amount as it's always just an inverse of the
                        // first.
                    }
                    else
                    {
                        amt = Value.Get(post.Amount).Print(amountWidth, -1, AmountPrintEnum.AMOUNT_PRINT_RIGHT_JUSTIFY | (report.GeneratedHandler.Handled ? 0 : AmountPrintEnum.AMOUNT_PRINT_NO_COMPUTED_ANNOTATIONS));
                    }

                    string trimmedAmt = amt.TrimStart();
                    int    amtSlip    = amt.Length - trimmedAmt.Length;

                    StringBuilder amtBuf = new StringBuilder();
                    if (slip + amtSlip < 2)
                    {
                        amtBuf.Append(new string(' ', 2 - (slip + amtSlip)));
                    }
                    amtBuf.Append(amt);

                    if (post.GivenCost != null && !post.Flags.HasFlag(SupportsFlagsEnum.POST_CALCULATED) && !post.Flags.HasFlag(SupportsFlagsEnum.POST_COST_CALCULATED))
                    {
                        string costOp = post.Flags.HasFlag(SupportsFlagsEnum.POST_COST_IN_FULL) ? "@@" : "@";
                        if (post.Flags.HasFlag(SupportsFlagsEnum.POST_COST_VIRTUAL))
                        {
                            costOp = "(" + costOp + ")";
                        }

                        if (post.Flags.HasFlag(SupportsFlagsEnum.POST_COST_VIRTUAL))
                        {
                            amtBuf.AppendFormat(" {0} {1}", costOp, post.GivenCost.Abs());
                        }
                        else
                        {
                            amtBuf.AppendFormat(" {0} {1}", costOp, (post.GivenCost / post.Amount).Abs());
                        }
                    }

                    if (post.AssignedAmount != null)
                    {
                        amtBuf.AppendFormat(" = {0}", post.AssignedAmount);
                    }

                    string trailer = amtBuf.ToString();
                    if (!String.IsNullOrEmpty(trailer))
                    {
                        if (slip > 0)
                        {
                            sbOut.AppendFormat(StringExtensions.GetWidthAlignFormatString(slip), " ");
                        }
                        sbOut.Append(trailer);

                        accountWidth += trailer.Length;
                    }
                }
                else
                {
                    sbOut.Append(pbuf.ToString());
                }

                if (!String.IsNullOrEmpty(post.Note))
                {
                    sbOut.Append(PrintNote(post.Note, post.Flags.HasFlag(SupportsFlagsEnum.ITEM_NOTE_ON_NEXT_LINE), columns, 4 + accountWidth));
                }
                sbOut.AppendLine();
            }
            return(sbOut.ToString());
        }