/// <summary> /// write out the securities in the given list starting with an expandable/collapsable group header. /// </summary> private void WriteSecurities(IReportWriter writer, List <SecurityPurchase> bySecurity, ref decimal totalMarketValue, ref decimal totalCostBasis, ref decimal totalGainLoss) { if (bySecurity.Count == 0) { return; } decimal marketValue = 0; decimal costBasis = 0; decimal gainLoss = 0; decimal currentQuantity = 0; decimal price = 0; Security current = null; // compute the totals for the group header row. foreach (SecurityPurchase i in bySecurity) { current = i.Security; price = current.Price; // for tax reporting we need to report the real GainLoss, but if CostBasis is zero then it doesn't make sense to report something // as a gain or loss, it will be accounted for under MarketValue, but it will be misleading as a "Gain". So we tweak the value here. decimal gain = (i.CostBasisPerUnit == 0) ? 0 : i.GainLoss; marketValue += i.MarketValue; costBasis += i.TotalCostBasis; gainLoss += gain; currentQuantity += i.UnitsRemaining; } if (current == null) { return; } totalMarketValue += marketValue; totalCostBasis += costBasis; totalGainLoss += gainLoss; // this is the expander row. writer.StartExpandableRowGroup(); decimal averageUnitPrice = currentQuantity == 0 ? 0 : costBasis / currentQuantity; WriteRow(writer, false, false, FontWeights.Bold, null, current.Name, current.Name, currentQuantity, price, marketValue, averageUnitPrice, costBasis, gainLoss); foreach (SecurityPurchase i in bySecurity) { // for tax reporting we need to report the real GainLoss, but if CostBasis is zero then it doesn't make sense to report something // as a gain or loss, it will be accounted for under MarketValue, but it will be misleading as a "Gain". So we tweak the value here. decimal gain = (i.CostBasisPerUnit == 0) ? 0 : i.GainLoss; WriteRow(writer, false, false, FontWeights.Normal, i.DatePurchased, i.Security.Name, i.Security.Name, i.UnitsRemaining, i.CostBasisPerUnit, i.MarketValue, i.Security.Price, i.TotalCostBasis, gain); } writer.EndExpandableRowGroup(); }
private void GenerateGroup(IReportWriter writer, Dictionary <Category, CashFlowColumns> columns, CashFlowColumns columnTotals, string groupName, Func <Category, bool> inGroup) { List <Category> rootCategories = new List <Category>(columns.Keys); rootCategories.Sort(new Comparison <Category>((a, b) => { return(string.Compare(GetCategoryCaption(a), GetCategoryCaption(b))); })); // start the group writer.StartExpandableRowGroup(); CashFlowColumns groupTotals = new CashFlowColumns(); // compute group totals; foreach (Category c in rootCategories) { if (inGroup(c)) { foreach (string columnName in this.columns) { CashFlowColumns cc = columns[c]; decimal amount = cc.GetValue(columnName); columnTotals.AddValue(columnName, null, amount); groupTotals.AddValue(columnName, null, amount); } } } WriteRow(writer, true, false, groupName, FormatValues(groupTotals.GetOrderedValues(this.columns)).ToArray()); // now add the detail rows of the group foreach (Category c in rootCategories) { if (inGroup(c)) { List <CashFlowCell> cells = new List <CashFlowCell>(); CashFlowColumns cc = columns[c]; foreach (string columnName in this.columns) { CashFlowCell cell = cc.GetCell(columnName); cells.Add(cell); } WriteRow(writer, false, false, GetCategoryCaption(c), cells); } } writer.EndExpandableRowGroup(); }
bool GenerateForm(TaxForm form, IReportWriter writer, ICollection <Transaction> transactions) { var byCategory = new Dictionary <Category, decimal>(); // could be one to many mapping. Dictionary <TaxCategory, List <Category> > map = new Dictionary <TaxCategory, List <Category> >(); // find our matching category foreach (TaxCategory tc in form.Categories) { foreach (Category c in this.myMoney.Categories.GetCategories()) { if (c.TaxRefNum == tc.RefNum) { byCategory[c] = 0M; List <Category> list = null; if (!map.TryGetValue(tc, out list)) { list = new List <Category>(); map[tc] = list; } list.Add(c); } } } bool found = false; // summarize the year. foreach (Transaction t in transactions) { if (t.Date.Year == this.year) { found |= Summarize(byCategory, t); } } if (!found) { return(false); } writer.WriteHeading("Form " + form.Name); writer.StartTable(); writer.StartColumnDefinitions(); writer.WriteColumnDefinition("20", 20, 20); // expander column writer.WriteColumnDefinition("300", 300, 300); // row category name writer.WriteColumnDefinition("100", 100, 00); // row value writer.EndColumnDefinitions(); foreach (TaxCategory tc in form.Categories) { List <Category> list = null; if (map.TryGetValue(tc, out list)) { if (list.Count > 1) { decimal total = 0; foreach (Category c in list) { total += byCategory[c]; } if (total != 0) { writer.StartExpandableRowGroup(); // header row for the total. writer.StartRow(); writer.StartCell(); writer.WriteParagraph(tc.Name); writer.EndCell(); writer.StartCell(); writer.WriteNumber(total.ToString("N0")); writer.EndCell(); writer.EndRow(); foreach (Category c in list) { decimal v = byCategory[c]; if (v != 0) { writer.StartRow(); writer.StartCell(); writer.WriteParagraph(" " + c.GetFullName()); writer.EndCell(); writer.StartCell(); writer.WriteNumber(v.ToString("N0")); AddHyperlink(c, writer); writer.EndCell(); writer.EndRow(); } } writer.EndExpandableRowGroup(); } } else if (list.Count == 1) { Category c = list[0]; decimal v = byCategory[c]; if (v != 0) { writer.StartRow(); writer.StartCell(); // null expander writer.EndCell(); writer.StartCell(); writer.WriteParagraph(tc.Name); writer.EndCell(); writer.StartCell(); writer.WriteNumber(v.ToString("N0")); AddHyperlink(c, writer); writer.EndCell(); writer.EndRow(); } } } } writer.EndTable(); return(true); }
bool GenerateForm(TaxForm form, IReportWriter writer, ICollection <Transaction> transactions) { var byCategory = new Dictionary <Category, decimal>(); // could be one to many mapping. Dictionary <TaxCategory, List <Category> > map = new Dictionary <TaxCategory, List <Category> >(); // find our matching category foreach (TaxCategory tc in form.Categories) { foreach (Category c in this.myMoney.Categories.GetCategories()) { if (c.TaxRefNum == tc.RefNum) { byCategory[c] = 0M; List <Category> list = null; if (!map.TryGetValue(tc, out list)) { list = new List <Category>(); map[tc] = list; } list.Add(c); } } } bool found = false; // summarize the year. foreach (Transaction t in transactions) { if (t.Transfer != null || t.IsDeleted || t.Status == TransactionStatus.Void) { continue; } bool include = t.Date >= this.startDate && t.Date < this.endDate; var extra = myMoney.TransactionExtras.FindByTransaction(t.Id); if (extra != null && extra.TaxYear != -1) { var taxYearStartDate = new DateTime(extra.TaxYear, fiscalYearStart + 1, 1); if (fiscalYearStart > 0) { // Note: "FY2020" means July 2019 to July 2020, in other words // it is the end date that represents the year. taxYearStartDate = taxYearStartDate.AddYears(-1); } var taxYearEndDate = taxYearStartDate.AddYears(1); include = taxYearStartDate >= this.startDate && taxYearEndDate <= this.endDate; } if (include) { found |= Summarize(byCategory, t); } } if (!found) { return(false); } writer.WriteHeading("Form " + form.Name); writer.StartTable(); writer.StartColumnDefinitions(); writer.WriteColumnDefinition("20", 20, 20); // expander column writer.WriteColumnDefinition("300", 300, 300); // row category name writer.WriteColumnDefinition("100", 100, 00); // row value writer.EndColumnDefinitions(); foreach (TaxCategory tc in form.Categories) { List <Category> list = null; if (map.TryGetValue(tc, out list)) { if (list.Count > 1) { decimal total = 0; foreach (Category c in list) { total += byCategory[c]; } if (total != 0) { writer.StartExpandableRowGroup(); // header row for the total. writer.StartRow(); writer.StartCell(); writer.WriteParagraph(tc.Name); writer.EndCell(); writer.StartCell(); writer.WriteNumber(total.ToString("N0")); writer.EndCell(); writer.EndRow(); foreach (Category c in list) { decimal v = byCategory[c]; if (v != 0) { writer.StartRow(); writer.StartCell(); writer.WriteParagraph(" " + c.GetFullName()); writer.EndCell(); writer.StartCell(); writer.WriteNumber(v.ToString("N0")); AddHyperlink(c, writer); writer.EndCell(); writer.EndRow(); } } writer.EndExpandableRowGroup(); } } else if (list.Count == 1) { Category c = list[0]; decimal v = byCategory[c]; if (v != 0) { writer.StartRow(); writer.StartCell(); // null expander writer.EndCell(); writer.StartCell(); writer.WriteParagraph(tc.Name); writer.EndCell(); writer.StartCell(); writer.WriteNumber(v.ToString("N0")); AddHyperlink(c, writer); writer.EndCell(); writer.EndRow(); } } } } writer.EndTable(); return(true); }