/// <summary> /// Generate Invoince and convert it to PDF /// T1 - File name /// T2 - Content Type /// T3 - Byte array /// </summary> /// <param name="invoiceNo"></param> /// <param name="repositoryWrapper"></param> /// <param name="templatePath"></param> /// <returns></returns> public static Tuple <string, string, byte[]> GenerateInvoice(string invoiceNo, IRepositoryWrapper repositoryWrapper, string templatePath) { var templateFile = $"{templatePath}{ConfigurationManager.AppSettings["FileName"]}"; if (string.IsNullOrEmpty(templateFile)) { throw new Exception("Invoice Template file could not be found"); } if (!File.Exists(templateFile)) { throw new Exception($"Template file does not exists on location:{Path.GetDirectoryName(templateFile)}"); } tblInv_Info inv_Info = repositoryWrapper.InvoiceInfo.GetById(invoiceNo); if (inv_Info == null) { throw new Exception($"No Invoice exists with invoice Number:{invoiceNo}"); } tblClientInfo clientInfo = repositoryWrapper.ClientInfo.GetById(inv_Info.Client_id); List <spGetCartItems_Result> cartItems = repositoryWrapper.InvoiceItems.GetCartItems(invoiceNo, inv_Info.Client_id); string fileName = string.Empty; using (DocX document = DocX.Load(templateFile)) { ReplaceClientDetails(document, clientInfo); ReplaceInvoiceDetails(document, inv_Info); ReplaceInvoiceItems(document, cartItems, inv_Info.Inv_Paid); //save the new file fileName = $@"{Path.GetDirectoryName(templateFile)}\{invoiceNo}.docx"; document.SaveAs(fileName); } var fileExtension = Path.GetExtension(fileName); if (string.IsNullOrWhiteSpace(fileExtension)) { return(null); } if (!new[] { ".doc", ".docx" }.Contains(fileExtension)) { return(null); } var pdfFileName = $@"{Path.GetDirectoryName(fileName)}\{Path.GetFileName(fileName).Replace(fileExtension, ".pdf")}"; return(ConvertToPDF(fileName, pdfFileName)); }
/// <summary> /// Replace Client details /// </summary> /// <param name="document"></param> /// <param name="clientInfo"></param> private static void ReplaceClientDetails(DocX document, tblClientInfo clientInfo) { document.ReplaceText("{CName}", clientInfo.C_name); document.ReplaceText("{CSurname}", clientInfo.C_surname); document.ReplaceText("{Address}", $"{clientInfo.Address} {clientInfo.Code}"); document.ReplaceText("{THome}", clientInfo.C_Tel_H); document.ReplaceText("{TWork}", clientInfo.C_Tel_W); document.ReplaceText("{Mobile}", clientInfo.C_Tel_Cell ?? string.Empty); document.ReplaceText("{Email}", clientInfo.C_Email ?? string.Empty); }
/// <summary> /// Save data to database when you click save button /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnSave_Click(object sender, EventArgs e) { string idNumber = txtClientID.Text; IsUpdate = bool.Parse(hdnIsCreate.Value); if (!IsUpdate && !idNumber.IsValidSouthAfricaIdNumber()) { lblIDError.Text = "Invalid South African Identity Number"; return; } lblIDError.Text = string.Empty; tblClientInfo dbClient = _repositoryWrapper.ClientInfo.GetById(idNumber); if (!IsUpdate && dbClient != null) { lblIDError.Text = "Client already exists in the database"; return; } tblClientInfo clientInfo = new tblClientInfo { Client_id = idNumber, C_name = txtName.Text, C_surname = txtSurname.Text, Address = txtAddress.Text, Code = txtACode.Text, C_Tel_H = txtTelH.Text, C_Tel_W = txtTelW.Text, C_Tel_Cell = txtTelM.Text, C_Email = txtEmail.Text, Reference_ID = int.Parse(DropDownRefID.SelectedValue) }; if (dbClient == null) { _repositoryWrapper.ClientInfo.Add(clientInfo); } else { _repositoryWrapper.ClientInfo.Update(clientInfo, clientInfo.Client_id); } Response.Redirect("~/Forms/Clients/ViewClients.aspx"); //ClearControls(); }
/// <summary> /// Send an email to Client /// </summary> /// <param name="invoiceNumber">Invoice Number</param> /// <param name="subject">Optional Email subject</param> /// <param name="content">Optional Email Content</param> /// <param name="templatePath">Invice Template path</param> /// <param name="repositoryWrapper"></param> /// <param name="emailSender"></param> /// <param name="textFormat"></param> /// <param name="CC"></param> /// <param name="BCC"></param> public static void SendEmail(string invoiceNumber, string templatePath, IRepositoryWrapper repositoryWrapper, IEmailSender emailSender, MimeKit.Text.TextFormat textFormat = MimeKit.Text.TextFormat.Html, IEnumerable <string> CC = null, IEnumerable <string> BCC = null, string subject = null, string content = null) { subject = subject ?? "Your Shopping Item(s)"; content = content ?? "Hi <br/> Please see attached invoice for your shopping items.<br/><br/><br/> Regards,<br/>Alt Health Sales Team."; tblInv_Info inv_Info = repositoryWrapper.InvoiceInfo.GetById(invoiceNumber); if (string.IsNullOrWhiteSpace(inv_Info?.Client_id ?? string.Empty)) { return; } tblClientInfo tblClientInfo = repositoryWrapper.ClientInfo.GetById(inv_Info.Client_id); if (string.IsNullOrWhiteSpace(tblClientInfo?.C_Email ?? string.Empty)) { return; } Tuple <string, string, byte[]> invoice = DocumentHelper.GenerateInvoice(invoiceNumber, repositoryWrapper, templatePath); Dictionary <string, byte[]> attachments = new Dictionary <string, byte[]> { { invoice.Item1, invoice.Item3 } }; emailSender.SendEmail(new EmailHelper.Classes.Message(new[] { tblClientInfo.C_Email }, subject, content, CC, BCC, textFormat, attachments)); }
/// <summary> /// Load data when client ID is supplied /// </summary> private void PopulateFields() { if (IsUpdate) { var clientId = Request.GetQueryString(StringHelper.ClientID); if (!string.IsNullOrWhiteSpace(clientId)) { tblClientInfo tblClientInfo = _repositoryWrapper.ClientInfo.GetById(clientId); txtClientID.Text = tblClientInfo?.Client_id; txtName.Text = tblClientInfo?.C_name; txtSurname.Text = tblClientInfo.C_surname; txtTelH.Text = tblClientInfo?.C_Tel_H; txtTelW.Text = tblClientInfo?.C_Tel_W; txtTelM.Text = tblClientInfo?.C_Tel_Cell; txtAddress.Text = tblClientInfo?.Address; txtACode.Text = tblClientInfo?.Code; txtEmail.Text = tblClientInfo?.C_Email; DropDownRefID.SelectedValue = tblClientInfo?.Reference_ID.ToString(); } } }