private bool UploadFileToBuilding(tblBuilding building, Data.ManagementPackData.ManagementPack dataItem, out string url)
        {
            string fileName    = "ManagementPack_" + dataItem.Period.ToString("yyyy_MMM") + ".pdf";
            string description = "Management Pack " + dataItem.Period;

            url = _ClientPortal.UploadBuildingDocument(DocumentCategoryType.MonthlyFinancial,
                                                       dataItem.Period,
                                                       building.id, description, fileName, dataItem.ReportData, string.Empty);
            return(true);
        }
        private void CreateReport(List <TableOfContentForPdfRecord> records)
        {
            if (dlgSave.ShowDialog() != DialogResult.OK)
            {
                return;
            }


            button3.Enabled = false;
            byte[] reportData = null;
            try
            {
                var building = cmbBuilding.SelectedItem as Building;
                var year     = cmbYear.SelectedItem as IdValue;
                var month    = cmbMonth.SelectedItem as IdValue;
                var tocList  = new List <TOCDataItem>();
                int pos      = 1;
                foreach (var item in records.Where(a => a.IncludeInTOC).OrderBy(a => a.Position))
                {
                    var toc = new TOCDataItem()
                    {
                        ItemNumber      = pos.ToString(),
                        ItemDescription = item.PrintDescription,
                        PageNumber      = item.Pages,
                    };
                    pos++;
                    tocList.Add(toc);
                }

                int x = records.Count(a => a.IncludeInTOC == false);
                if (x > 0)
                {
                    int maxPos = tocList.Count();
                    var toc    = new TOCDataItem()
                    {
                        ItemNumber      = (maxPos + 1).ToString(),
                        ItemDescription = "Invoicing",
                        PageNumber      = x,
                    };
                    tocList.Add(toc);
                }

                using (var reportService = ReportServiceClient.CreateInstance())
                {
                    reportData = reportService.ManagementPackCoverPage(
                        new DateTime(year.Id, month.Id, 1),
                        building.Name, Controller.user.name, tocList.ToArray());
                    if (reportData != null)
                    {
                        if (File.Exists(dlgSave.FileName))
                        {
                            File.Delete(dlgSave.FileName);
                        }

                        using (FileStream ms = new FileStream(dlgSave.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                        {
                            using (Document doc = new Document())
                            {
                                using (PdfCopy copy = new PdfCopy(doc, ms))
                                {
                                    doc.Open();

                                    AddPdfDocument(copy, reportData);
                                    foreach (var record in records)
                                    {
                                        var fileBytes = File.ReadAllBytes(record.Path);
                                        if (GetTotalPages(fileBytes) > 0)
                                        {
                                            AddPdfDocument(copy, fileBytes);
                                        }
                                        else
                                        {
                                            Controller.HandleError("Unable to verify " + record.File + " pdf is invalid");
                                            return;
                                        }
                                        Application.DoEvents();
                                    }
                                    //write output file
                                    ms.Flush();
                                }
                            }
                        }

                        var      dt  = new DateTime(year.Id, month.Id, 1);
                        DateTime now = DateTime.Now;

                        using (var context = SqlDataHandler.GetDataContext())
                        {
                            var managementPackReport = context.ManagementPackSet.Include(a => a.Items).SingleOrDefault(a => a.BuildingId == building.ID && a.Period == dt);
                            if (managementPackReport == null)
                            {
                                managementPackReport = new Data.ManagementPackData.ManagementPack()
                                {
                                    BuildingId  = building.ID,
                                    Period      = dt,
                                    DateCreated = now,
                                    Items       = new List <ManagementPackReportItem>()
                                };
                                context.ManagementPackSet.Add(managementPackReport);
                            }
                            managementPackReport.UserId      = Controller.user.id;
                            managementPackReport.DateUpdated = now;
                            managementPackReport.ReportData  = File.ReadAllBytes(dlgSave.FileName);
                            managementPackReport.Published   = false;



                            var itemsToRemove = managementPackReport.Items.ToList();
                            foreach (var i in itemsToRemove)
                            {
                                managementPackReport.Items.Remove(i);
                                context.ManagementPackReportItemSet.Remove(i);
                            }

                            foreach (var item in records.Where(a => a.IncludeInTOC).OrderBy(a => a.Position))
                            {
                                var i = new ManagementPackReportItem()
                                {
                                    ManagementPack   = managementPackReport,
                                    ManagementPackId = managementPackReport.id,
                                    Path             = item.Path,
                                    File             = item.File,
                                    Description      = item.Description,
                                    Description2     = item.Description2,
                                    Pages            = item.Pages,
                                    Position         = item.Position,
                                    FileDate         = item.FileDate,
                                    IsTempFile       = item.IsTempFile
                                };

                                if (i.IsTempFile)
                                {
                                    i.FileData = File.ReadAllBytes(i.Path);
                                }

                                managementPackReport.Items.Add(i);
                            }
                            managementPackReport.Commments         = tbComments.Text;
                            managementPackReport.SubmitForApproval = false;
                            if (managementPackReport.Declined)
                            {
                                if (Controller.AskQuestion("This report is in a declined status.\n"
                                                           + "Would you like to resubmit the report for approval?"))
                                {
                                    managementPackReport.Declined          = false;
                                    managementPackReport.SubmitForApproval = true;
                                    managementPackReport.Submitted         = DateTime.Now;
                                }
                            }


                            if (cbSubmitForApproval.Checked)
                            {
                                managementPackReport.SubmitForApproval = true;
                                managementPackReport.Submitted         = DateTime.Now;
                            }

                            tbComments.Text = "";
                            context.SaveChanges();
                        }
                        Process.Start(dlgSave.FileName);
                    }
                    else
                    {
                        Controller.HandleError("Management pack failed to generate.");
                    }
                }
            }
            catch (Exception exp)
            {
                Controller.HandleError(exp);
            }
            finally
            {
                button3.Enabled = true;
            }
        }