Exemplo n.º 1
0
        private byte[] CreatePdf(string result)
        {
            string             pdf_page_size   = "A4";
            PdfPageSize        pageSize        = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pdf_page_size, true);
            string             pdf_orientation = "Portrait";
            PdfPageOrientation pdfOrientation  = (PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation),
                                                                                pdf_orientation, true);
            int webPageWidth  = 1024;
            int webPageHeight = 0;

            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();

            // set converter options
            converter.Options.PdfPageSize        = pageSize;
            converter.Options.PdfPageOrientation = pdfOrientation;
            converter.Options.WebPageWidth       = webPageWidth;
            converter.Options.WebPageHeight      = webPageHeight;
            PdfDocument doc = converter.ConvertHtmlString(result);

            //Conversion failure. Could not find 'Select.Html.dep'.
            // save pdf document
            byte[] data = doc.Save();

            return(data);
        }
        public async Task <HttpResponseMessage> SendMail()
        {
            Response oResponse = new Response();

            bool MailStatus = true;

            HttpContent requestContent = Request.Content;
            var         Content        = await requestContent.ReadAsStringAsync();

            var JsonData = JsonConvert.DeserializeObject <JsonResponse>(Content);

            byte[] data       = Convert.FromBase64String(JsonData.Html);
            string Html       = System.Text.Encoding.UTF8.GetString(data);
            long   CaseId     = Convert.ToInt64(JsonData.CaseId);
            long   ModifiedBy = Convert.ToInt64(JsonData.ModifiedBy);

            using (var DB = new MHMDal.Models.MHM())
            {
                try
                {
                    #region Generating PDF

                    // read parameters from the webpage
                    string baseUrl = "app.myhealthmath.com";

                    string pdf_page_size           = "A4";
                    SelectPdf.PdfPageSize pageSize = (SelectPdf.PdfPageSize)Enum.Parse(typeof(SelectPdf.PdfPageSize),
                                                                                       pdf_page_size, true);

                    string pdf_orientation = "Portrait";
                    SelectPdf.PdfPageOrientation pdfOrientation =
                        (SelectPdf.PdfPageOrientation)Enum.Parse(typeof(SelectPdf.PdfPageOrientation),
                                                                 pdf_orientation, true);

                    int webPageWidth = 1024;


                    int webPageHeight = 0;

                    // instantiate a html to pdf converter object
                    HtmlToPdf converter = new HtmlToPdf();

                    // set converter options
                    converter.Options.PdfPageSize        = pageSize;
                    converter.Options.PdfPageOrientation = pdfOrientation;
                    converter.Options.WebPageWidth       = webPageWidth;
                    converter.Options.WebPageHeight      = webPageHeight;
                    // create a new pdf document converting an url
                    PdfDocument doc = converter.ConvertHtmlString(Html, baseUrl);

                    // save pdf document
                    byte[] outPdfBuffer = doc.Save();

                    // close pdf document
                    doc.Close();

                    #endregion

                    string Mail = "";

                    var    MailDetails   = DB.JobMasters.Where(r => r.JobNumber == JsonData.JobNumber).Select(t => new { t.EmailSubjText, t.EmailBodyText, t.EmailSignText }).First();
                    string EmailSubjText = MailDetails.EmailSubjText.ToString();
                    string EmailBodyText = MailDetails.EmailBodyText.ToString();
                    string EmailSignText = MailDetails.EmailSignText.ToString();
                    if (EmailSubjText.Contains("##CaseTitle##"))
                    {
                        EmailSubjText = EmailSubjText.Replace("##CaseTitle##", JsonData.CaseTitle);
                    }
                    if (EmailBodyText.Contains("##ApplicantName##"))
                    {
                        EmailBodyText = EmailBodyText.Replace("##ApplicantName##", JsonData.ApplicantName.ToString());
                    }
                    if (EmailSignText.Contains("##AgentName##"))
                    {
                        EmailSignText = EmailSignText.Replace("##AgentName##", JsonData.AgentName);
                    }
                    if (EmailSignText.Contains("##AgentEmail##"))
                    {
                        EmailSignText = EmailSignText.Replace("##AgentEmail##", JsonData.AgentEmail);
                    }
                    if (EmailSignText.Contains("##AgentPhone##"))
                    {
                        EmailSignText = EmailSignText.Replace("##AgentPhone##", String.Format("{0:(###) ###-####}", Convert.ToInt64(JsonData.AgentPhone)));
                    }
                    if (EmailBodyText.Contains("##PlanTotalCostRange##"))
                    {
                        EmailBodyText = EmailBodyText.Replace("##PlanTotalCostRange##", "$" + String.Format("{0:n0}", Convert.ToDecimal(JsonData.PlanTotalCostRange)));
                    }
                    if (EmailBodyText.Contains("##TotalEmployerContribution##"))
                    {
                        var str = "";
                        if (Convert.ToDecimal(JsonData.EmployerHSAContribution) != 0 && Convert.ToDecimal(JsonData.EmployerHRAReimbursement) > 0)
                        {
                            str  = "If you enroll in the " + JsonData.OptimalPlanName + " (your optimal plan), your employer will contribute approximately " + "$" + String.Format("{0:n0}", Convert.ToDecimal(JsonData.TotalEmployerContribution)) + " to your expenses. This contribution consists of:" + Environment.NewLine;
                            str += "<ul><li>" + "$" + String.Format("{0:n0}", Convert.ToDecimal(JsonData.EmployerPremiumContribution)) + " to your premium</li>";
                            str += "<li>" + "$" + String.Format("{0:n0}", Convert.ToDecimal(JsonData.EmployerHSAContribution)) + " to your health savings account, and</li>";
                            str += "<li>an estimated " + "$" + String.Format("{0:n0}", Convert.ToDecimal(JsonData.EmployerHRAReimbursement)) + " in HRA reimbursements.</li></ul>";
                        }
                        else if (Convert.ToDecimal(JsonData.EmployerHSAContribution) != 0 && Convert.ToDecimal(JsonData.EmployerHRAReimbursement) == 0)
                        {
                            str  = "If you enroll in the " + JsonData.OptimalPlanName + " (your optimal plan), your employer will contribute approximately " + "$" + String.Format("{0:n0}", Convert.ToDecimal(JsonData.TotalEmployerContribution)) + " to your expenses. This contribution consists of:" + Environment.NewLine;
                            str += "<ul><li>" + "$" + String.Format("{0:n0}", Convert.ToDecimal(JsonData.EmployerPremiumContribution)) + " to your premium</li>";
                            str += "<li>" + "$" + String.Format("{0:n0}", Convert.ToDecimal(JsonData.EmployerHSAContribution)) + " to your health savings account.</li></ul>";
                        }
                        else if (Convert.ToDecimal(JsonData.EmployerHSAContribution) == 0 && Convert.ToDecimal(JsonData.EmployerHRAReimbursement) > 0)
                        {
                            str  = "If you enroll in the " + JsonData.OptimalPlanName + " (your optimal plan), your employer will contribute approximately " + "$" + String.Format("{0:n0}", Convert.ToDecimal(JsonData.TotalEmployerContribution)) + " to your expenses. This contribution consists of:" + Environment.NewLine;
                            str += "<ul><li>" + "$" + String.Format("{0:n0}", Convert.ToDecimal(JsonData.EmployerPremiumContribution)) + " to your premium</li>";
                            str += "<li>an estimated " + "$" + String.Format("{0:n0}", Convert.ToDecimal(JsonData.EmployerHRAReimbursement)) + " in HRA reimbursements.</li></ul>";
                        }
                        else if (Convert.ToDecimal(JsonData.EmployerHSAContribution) == 0 && Convert.ToDecimal(JsonData.EmployerHRAReimbursement) == 0)
                        {
                            str = "If you enroll in the " + JsonData.OptimalPlanName + " (your optimal plan), your employer will contribute approximately " + "$" + String.Format("{0:n0}", Convert.ToDecimal(JsonData.TotalEmployerContribution)) + " to your premium.";
                        }
                        EmailBodyText = EmailBodyText.Replace("##TotalEmployerContribution##", str);
                    }

                    Mail = EmailBodyText + EmailSignText;

                    Service.SendMailWithAttaitchment(JsonData.ApplicantEmail, JsonData.AgentEmail, "*****@*****.**", EmailSubjText, Mail, outPdfBuffer, JsonData.CaseTitle + ".pdf");

                    //var oCase = DB.Cases.Where(r => r.CaseID == CaseId).FirstOrDefault();

                    UpdateCaseToSent(CaseId, MailStatus, ModifiedBy);

                    oResponse.Status  = true;
                    oResponse.Message = "Success";
                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, oResponse);
                    return(response);
                }
                catch (Exception ex)
                {
                    MailStatus = false;
                    UpdateCaseToSent(CaseId, MailStatus, ModifiedBy);
                    oResponse.Status  = false;
                    oResponse.Message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest, oResponse);
                    return(response);
                }
            }
        }