Ejemplo n.º 1
0
        public void SaveData(Stream stream, FormatTypeEnum formatType)
        {
            var upload = new UploadFile();

            upload.UploadDate   = DateTime.Now;
            upload.FormatType   = formatType;
            upload.Transactions = new List <Transaction>();

            if (formatType == FormatTypeEnum.CSV)
            {
                StreamReader reader = new StreamReader(stream);

                while (!reader.EndOfStream)
                {
                    upload.Transactions.Add(CsvHelper.FromCsv(reader.ReadLine()));
                }
            }
            else
            {
                upload.Transactions.AddRange(XmlHelper.FromXml(stream));
            }

            try
            {
                db.SaveData(upload);
            }
            catch (Exception ex)
            {
                throw new Exception("Error save", ex);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Ported from format_datetime
        /// </summary>
        public string FormatDateTime(DateTime when, FormatTypeEnum formatType = FormatTypeEnum.FMT_PRINTED, string format = null)
        {
            if (formatType == FormatTypeEnum.FMT_WRITTEN)
            {
                return(WrittenDateTimeIO.Format(when));
            }
            else if (formatType == FormatTypeEnum.FMT_CUSTOM && format != null)
            {
                DateTimeIO dateIO;
                if (TempDateTimeIO.TryGetValue(format, out dateIO))
                {
                    return(dateIO.Format(when));
                }
                else
                {
                    dateIO = new DateTimeIO(format, false);
                    TempDateTimeIO.Add(format, dateIO);
                    return(dateIO.Format(when));
                }
            }
            else if (formatType == FormatTypeEnum.FMT_PRINTED)
            {
                return(PrintedDateTimeIO.Format(when));
            }

            throw new InvalidOperationException("formatter");
        }
Ejemplo n.º 3
0
        private static string GetDecimalPlaces(CultureInfo ci, int decimalDigits, FormatTypeEnum numberFormatType)
        {
            string separator = null;

            if (decimalDigits <= 0)
            {
                return(string.Empty);
            }

            switch (numberFormatType)
            {
            case FormatTypeEnum.Currency: separator = ci.NumberFormat.CurrencyDecimalSeparator; break;

            case FormatTypeEnum.Number: separator = ci.NumberFormat.NumberDecimalSeparator; break;

            case FormatTypeEnum.Percentage: separator = ci.NumberFormat.PercentDecimalSeparator; break;
            }

            return(separator + String.Empty.PadLeft(decimalDigits, '0'));
        }
Ejemplo n.º 4
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());
        }
Ejemplo n.º 5
0
 public FormatType(string displayName, FormatTypeEnum internalValue)
 {
     DisplayName = displayName;
     InternalValue = internalValue;
 }