public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
 {
     return(EnumCostDistribution.CostDistributionToString((CostDistribution)value));
 }
        public static void AddChargeTable(Document document, ChargeDataGrid selectedCharge, bool useDefaultFolder = false, string bankAccountCaption = null)
        {
            Document doc   = document;
            Style    style = doc.Styles["Normal"];

            style.Font.Name = "Calibri";
            style           = doc.Styles.AddStyle("Table", "Normal");
            style.Font.Name = "Calibri";
            style.Font.Size = 12;

            var   section = doc.LastSection;
            Table address = OwnerTableInfo(selectedCharge.Owner, selectedCharge.Building, selectedCharge.Apartment);

            address.Borders.Color = Colors.Transparent;
            document.LastSection.Add(address);

            Paragraph sep = new Paragraph();

            sep.Format.SpaceAfter = "0.5cm";
            document.LastSection.Add(sep);

            Table table = new Table();

            table.Borders.Width = 0.5;

            table.AddColumn(Unit.FromCentimeter(5.5));
            table.AddColumn(Unit.FromCentimeter(3.75));
            var column = table.AddColumn(Unit.FromCentimeter(2.25));

            column.Format.Alignment = ParagraphAlignment.Center;
            column = table.AddColumn(Unit.FromCentimeter(1.25));
            column.Format.Alignment = ParagraphAlignment.Right;
            column = table.AddColumn(Unit.FromCentimeter(2.25));
            column.Format.Alignment = ParagraphAlignment.Right;
            column = table.AddColumn(Unit.FromCentimeter(2));
            column.Format.Alignment = ParagraphAlignment.Right;

            Row row = table.AddRow();

            row.Shading.Color = new Color(135, 176, 77);
            Cell cell = row.Cells[0];

            cell.AddParagraph("KATEGORIA");
            cell.Format.Font.Bold = true;
            cell = row.Cells[1];
            cell.AddParagraph("JEDNOSTKA");
            cell.Format.Font.Bold = true;
            cell = row.Cells[3];
            cell.AddParagraph("KOSZT JEDN.");
            cell.Format.Font.Bold = true;
            cell = row.Cells[4];
            cell.AddParagraph("JEDN.");
            cell.Format.Font.Bold = true;
            cell = row.Cells[5];
            cell.AddParagraph("SUMA");
            cell.Format.Font.Bold = true;
            cell = row.Cells[2];
            cell.AddParagraph("GRUPA");
            cell.Format.Font.Bold = true;

            var     length = selectedCharge.Components.Count;
            int     groupsCount;
            decimal sum = 0;
            List <BuildingChargeGroupBankAccount> bankAccounts;

            using (var db = new DB.DomenaDBContext())
            {
                bankAccounts = db.BuildingChargeGroupBankAccounts.Include(x => x.Building).Include(x => x.GroupName).Where(x => !x.IsDeleted && x.Building.BuildingId == selectedCharge.Building.BuildingId).ToList();
                foreach (var c in selectedCharge.Components)
                {
                    row  = table.AddRow();
                    cell = row.Cells[0];
                    cell.AddParagraph(db.CostCategories.FirstOrDefault(x => x.BuildingChargeBasisCategoryId.Equals(c.CostCategoryId)).CategoryName);
                    cell = row.Cells[1];
                    cell.AddParagraph(EnumCostDistribution.CostDistributionToString((CostDistribution)c.CostDistribution));
                    cell = row.Cells[3];
                    cell.AddParagraph(c.CostPerUnit + " zł");
                    cell = row.Cells[4];

                    string units = "";
                    switch ((CostDistribution)c.CostDistribution)
                    {
                    default:
                        break;

                    case CostDistribution.PerApartmentTotalArea:
                        units = (selectedCharge.Apartment.AdditionalArea + selectedCharge.Apartment.ApartmentArea).ToString();
                        break;

                    case CostDistribution.PerAdditionalArea:
                        units = (selectedCharge.Apartment.AdditionalArea).ToString();
                        break;

                    case CostDistribution.PerApartment:
                        units = "1";
                        break;

                    case CostDistribution.PerApartmentArea:
                        units = (selectedCharge.Apartment.ApartmentArea).ToString();
                        break;

                    case CostDistribution.PerLocators:
                        units = (selectedCharge.Apartment.Locators).ToString();
                        break;
                    }
                    cell.AddParagraph(units);
                    cell = row.Cells[5];
                    cell.AddParagraph(c.Sum + " zł");
                    cell = row.Cells[2];
                    cell.AddParagraph(c.GroupName.GroupName);
                    sum += c.Sum;
                }
                var groups = selectedCharge.Components.GroupBy(x => x.GroupName.GroupName);
                row               = table.AddRow();
                row.HeightRule    = RowHeightRule.Exactly;
                row.Height        = 1;
                row.Shading.Color = Colors.Black;
                groupsCount       = groups.Count();
                foreach (var g in groups)
                {
                    row  = table.AddRow();
                    cell = row.Cells[0];
                    cell.AddParagraph("Razem - " + g.Key);
                    cell = row.Cells[5];
                    cell.AddParagraph(g.Select(c => c.Sum).Sum() + " zł");
                }
            }
            row  = table.AddRow();
            cell = row.Cells[0];
            cell.AddParagraph("Razem");
            cell.Format.Font.Bold = true;
            cell = row.Cells[5];
            cell.AddParagraph(sum + " zł");
            cell.Format.Font.Bold = true;

            table.SetEdge(0, 0, 6, length + 3 + groupsCount, Edge.Box, MigraDoc.DocumentObjectModel.BorderStyle.Single, 1, Colors.Black);

            document.LastSection.Add(table);
            if (bankAccounts != null && bankAccounts.Count > 0 && selectedCharge.Components != null &&
                selectedCharge.Components.Count() > 0 && selectedCharge.Components.GroupBy(x => x.GroupName).Count() > 0
                )
            {
                Paragraph paragraph = document.LastSection.AddParagraph();

                if (bankAccountCaption == null)
                {
                    using (var db = new DB.DomenaDBContext())
                    {
                        bankAccountCaption = db.Settings.FirstOrDefault(s => s.Key == "bank-account")?.Value;
                    }
                }
                //paragraph.AddText("Wpłat należy dokonywać regularnie do dnia 10 każdego miesiąca na rachunek bankowy: ");
                paragraph.AddText(bankAccountCaption);

                foreach (var g in selectedCharge.Components.GroupBy(x => x.GroupName))
                {
                    var ba = bankAccounts.FirstOrDefault(x => x.GroupName.BuildingChargeGroupNameId == g.Key.BuildingChargeGroupNameId);
                    if (ba != null)
                    {
                        paragraph = document.LastSection.AddParagraph();
                        paragraph.AddText(g.Key.GroupName + ": " + ba.BankAccount);
                    }
                }
            }

            MigraDoc.Rendering.DocumentRenderer docRenderer = new MigraDoc.Rendering.DocumentRenderer(doc);
            docRenderer.PrepareDocument();

            PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);

            renderer.Document = doc;
            renderer.RenderDocument();
            // Save the document...

            try
            {
                if (!useDefaultFolder)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "PDF file|*.pdf";
                    sfd.Title  = "Zapisz raport jako...";
                    sfd.ShowDialog();
                    if (sfd.FileName != "")
                    {
                        renderer.PdfDocument.Save(sfd.FileName);
                        System.Diagnostics.Process.Start(sfd.FileName);
                    }
                }
                else
                {
                    System.IO.FileInfo file = new System.IO.FileInfo("Reports\\");
                    file.Directory.Create();
                    string filename = selectedCharge.ChargeDate.ToString("MMMM_yyyy") + "_" + selectedCharge.Building.Name + "_" + selectedCharge.Apartment.ApartmentNumber + ".pdf";
                    renderer.PdfDocument.Save(Path.Combine(file.FullName, filename.Replace(' ', '_')));
                    System.Diagnostics.Process.Start(Path.Combine(file.FullName, filename.Replace(' ', '_')));
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Błąd zapisu pliku - plik może być aktualnie używany. Spróbuj ponownie.");
                Log.Logger.Error(e, "Error in report file save");
            }
        }
Exemple #3
0
 public CostDistributionCollectionItem(CostDistribution value)
 {
     this.Name      = EnumCostDistribution.CostDistributionToString(value);
     this.EnumValue = (int)value;
 }