Exemplo n.º 1
0
        public ProductProfitReport(string PriceSymbol)
        {
            this.PriceSymbol = PriceSymbol;


            WidhtMillimeters   = 210 - 40; // html right20 + left20 margin 40
            HeightMillimeters  = 280 - 20; // html top10 + bottom10 margin 20
            MarginsMillimeters = PaperMargins.All(Length.Millimeters(0));
        }
Exemplo n.º 2
0
        public ImportMiddleReport(Import import, List <Product> productList, Printer printer, int heightMillimeters, string PriceSymbol, long Lang = -1)
        {
            this.Import      = import;
            this.ProductList = productList;
            this.Printer     = printer;
            this.Lang        = Lang;
            this.PriceSymbol = PriceSymbol;

            WidhtMillimeters   = 200;
            HeightMillimeters  = heightMillimeters - 10;
            MarginsMillimeters = PaperMargins.All(Length.Millimeters(5));
        }
Exemplo n.º 3
0
        public OrderCheckoutMiddleReport(Order order, List <Product> productList, Printer printer, int heightMillimeters, string PriceSymbol, long Lang = -1)
        {
            this.Order       = order;
            this.ProductList = productList;
            this.Printer     = printer;
            this.Lang        = Lang;
            this.PriceSymbol = PriceSymbol;
            this.BillLang    = order.Lang;


            WidhtMillimeters   = 200;
            HeightMillimeters  = heightMillimeters - 10;
            MarginsMillimeters = PaperMargins.All(Length.Millimeters(5));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Runs the report returning the number of statements that were generated
        /// </summary>
        public int RunReport()
        {
            UpdateProgress("Connecting...");

            // Login and setup options for REST calls
            RockConfig rockConfig = RockConfig.Load();

            _rockRestClient = new RockRestClient(rockConfig.RockBaseUrl);
            _rockRestClient.Login(rockConfig.Username, rockConfig.Password);

            UpdateProgress("Getting Recipients...");
            var recipientList = _rockRestClient.PostDataWithResult <Rock.StatementGenerator.StatementGeneratorOptions, List <Rock.StatementGenerator.StatementGeneratorRecipient> >("api/FinancialTransactions/GetStatementGeneratorRecipients", this.Options);

            this.RecordCount = recipientList.Count;
            this.RecordIndex = 0;

            var tasks = new List <Task>();

            // initialize the pdfStreams list for all the recipients so that it can be populated safely in the pdf generation threads
            List <Stream> pdfStreams = recipientList.Select(a => ( Stream )null).ToList();

            UpdateProgress("Getting Statements...");
            foreach (var recipent in recipientList)
            {
                StringBuilder sbUrl = new StringBuilder();
                sbUrl.Append($"api/FinancialTransactions/GetStatementGeneratorRecipientResult?GroupId={recipent.GroupId}");
                if (recipent.PersonId.HasValue)
                {
                    sbUrl.Append($"&PersonId={recipent.PersonId.Value}");
                }

                var recipentResult = _rockRestClient.PostDataWithResult <Rock.StatementGenerator.StatementGeneratorOptions, Rock.StatementGenerator.StatementGeneratorRecipientResult>(sbUrl.ToString(), this.Options);

                int documentNumber = this.RecordIndex;
                if ((this.Options.ExcludeOptedOutIndividuals && recipentResult.OptedOut) || (recipentResult.Html == null))
                {
                    // don't generate a statement if opted out or no statement html
                    pdfStreams[documentNumber] = null;
                }
                else
                {
                    var html       = recipentResult.Html;
                    var footerHtml = recipentResult.FooterHtml;

                    var task = Task.Run(() =>
                    {
                        var pdfGenerator = Pdf.From(html);

                        string footerHtmlPath = Path.ChangeExtension(Path.GetTempFileName(), "html");
                        string footerUrl      = null;

                        if (!string.IsNullOrEmpty(footerHtml))
                        {
                            File.WriteAllText(footerHtmlPath, footerHtml);
                            footerUrl = "file:///" + footerHtmlPath.Replace('\\', '/');
                        }

                        if (footerUrl != null)
                        {
                            pdfGenerator = pdfGenerator.WithObjectSetting("footer.htmlUrl", footerUrl);
                        }
                        else
                        {
                            pdfGenerator = pdfGenerator.WithObjectSetting("footer.fontSize", "10");
                            pdfGenerator = pdfGenerator.WithObjectSetting("footer.right", "Page [page] of [topage]");
                        }

                        var pdfBytes = pdfGenerator
                                       .WithMargins(PaperMargins.All(Length.Millimeters(10)))
                                       .WithoutOutline()
                                       .Portrait()
                                       .Content();

                        var pdfStream = new MemoryStream(pdfBytes);
                        System.Diagnostics.Debug.Assert(pdfStreams[documentNumber] == null, "Threading issue: pdfStream shouldn't already be assigned");
                        pdfStreams[documentNumber] = pdfStream;

                        if (File.Exists(footerHtmlPath))
                        {
                            File.Delete(footerHtmlPath);
                        }
                    });

                    tasks.Add(task);

                    tasks = tasks.Where(a => a.Status != TaskStatus.RanToCompletion).ToList();
                }

                this.RecordIndex++;
                UpdateProgress("Processing...");
            }

            Task.WaitAll(tasks.ToArray());

            UpdateProgress("Creating PDF...");
            this.RecordIndex = 0;

            // remove any statements that didn't get generated due to OptedOut
            pdfStreams       = pdfStreams.Where(a => a != null).ToList();
            this.RecordCount = pdfStreams.Count();

            int maxStatementsPerChapter = RecordCount;

            bool useChapters = this.Options.StatementsPerChapter.HasValue;

            if (this.Options.StatementsPerChapter.HasValue)
            {
                maxStatementsPerChapter = this.Options.StatementsPerChapter.Value;
            }

            if (maxStatementsPerChapter < 1)
            {
                // just in case they entered 0 or a negative number
                useChapters             = false;
                maxStatementsPerChapter = RecordCount;
            }

            int statementsInChapter = 0;
            int chapterIndex        = 1;

            PdfDocument resultPdf = new PdfDocument();

            try
            {
                if (pdfStreams.Any())
                {
                    var lastPdfStream = pdfStreams.LastOrDefault();
                    foreach (var pdfStream in pdfStreams)
                    {
                        UpdateProgress("Creating PDF...");
                        this.RecordIndex++;
                        PdfDocument pdfDocument = PdfReader.Open(pdfStream, PdfDocumentOpenMode.Import);

                        foreach (var pdfPage in pdfDocument.Pages.OfType <PdfPage>())
                        {
                            resultPdf.Pages.Add(pdfPage);
                        }

                        statementsInChapter++;
                        if (useChapters && ((statementsInChapter >= maxStatementsPerChapter) || pdfStream == lastPdfStream))
                        {
                            string filePath = string.Format(@"{0}\{1}-chapter{2}.pdf", this.Options.SaveDirectory, this.Options.BaseFileName, chapterIndex);
                            SavePdfFile(resultPdf, filePath);
                            resultPdf.Dispose();
                            resultPdf           = new PdfDocument();
                            statementsInChapter = 0;
                            chapterIndex++;
                        }
                    }

                    if (useChapters)
                    {
                        // just in case we still have statements that haven't been written to a pdf
                        if (statementsInChapter > 0)
                        {
                            string filePath = string.Format(@"{0}\{1}-chapter{2}.pdf", this.Options.SaveDirectory, this.Options.BaseFileName, chapterIndex);
                            SavePdfFile(resultPdf, filePath);
                        }
                    }
                    else
                    {
                        string filePath = string.Format(@"{0}\{1}.pdf", this.Options.SaveDirectory, this.Options.BaseFileName);
                        SavePdfFile(resultPdf, filePath);
                    }
                }
            }
            finally
            {
                resultPdf.Dispose();
            }

            UpdateProgress("Complete");

            return(this.RecordCount);
        }