public List<SyncInvoice> GetSelectedInvoices() { var oSyncInvoices = new List<SyncInvoice>(); var emailTemplate = new InvoiceEmail(); foreach (DataGridViewRow oRow in m_grdInvoices.Rows) { var dataItem = oRow.DataBoundItem as InvoiceRow; DataGridViewCheckBoxCell oCheckboxColumn = oRow.Cells["IsSync"] as DataGridViewCheckBoxCell; if (oCheckboxColumn.Value == oCheckboxColumn.TrueValue) { DataGridViewCell oInvoiceIDCell = oRow.Cells["ID"]; DataGridViewCheckBoxCell oInvoiceEmailCell = oRow.Cells["IsEmail"] as DataGridViewCheckBoxCell; SyncInvoice oSyncInvoice = new SyncInvoice(); oSyncInvoice.ID = (string)oInvoiceIDCell.Value; if (oInvoiceEmailCell.Value == oInvoiceEmailCell.TrueValue) { oSyncInvoice.IsEmail = true; if (dataItem != null) { if (dataItem.CustomEmail) { oSyncInvoice.EmailSubject = dataItem.EmailSubject; oSyncInvoice.EmailBody = dataItem.EmailBody; } else { oSyncInvoice.EmailSubject = emailTemplate.EmailSubject; oSyncInvoice.EmailBody = emailTemplate.EmailBody; } } } else { oSyncInvoice.IsEmail = false; } oSyncInvoices.Add(oSyncInvoice); } } return oSyncInvoices; }
private void ProcessInvoice(SyncInvoice oSyncInvoice) { this._logger.DebugFormat("Processing invoice: {0}", oSyncInvoice.ID); m_oInvoice.Get(oSyncInvoice.ID); int iJobID = 0; DateTime oDateTime = DateTime.Today; DateTime.TryParse(m_oInvoice.GetCustomField("Export to SSMS"), out oDateTime); int.TryParse(m_oInvoice.GetCustomField("JobID"), out iJobID); this._logger.DebugFormat("Processing invoice job #: {0}", iJobID); // Only import the invoice under the following conditions: // 1. The invoice SSMS value is today's date or before // 2. The date cannot be 0001 as that means we couldn't parse the SSMS date if (oDateTime.CompareTo(DateTime.Today) <= 0 && oDateTime.Year != 0001 && iJobID > 0) { this._logger.DebugFormat("Processing invoice: {0}", oSyncInvoice.ID); PdfDocument oDocument = m_oPdfManager.CreateDocument(); TextReader oTextReader = File.OpenText(Environment.CurrentDirectory + @"\templates\invoice.html"); string sHtml = oTextReader.ReadToEnd(); this._logger.DebugFormat("Read invoice tempate from: {0}", Environment.CurrentDirectory + @"\templates\invoice.html"); string sAddress1 = string.Empty; string sAddress2 = string.Empty; string sAddress3 = string.Empty; string sCity = string.Empty; string sState = string.Empty; string sZip = string.Empty; // Extract Address Line 1 Regex oRegexAddress1 = new Regex("<Addr1>.*</Addr1>"); Match oMatchAddress1 = oRegexAddress1.Match(m_oInvoice.BillingAddress); if (oMatchAddress1.Success) { sAddress1 = oMatchAddress1.Value; sAddress1 = sAddress1.Replace("<Addr1>", string.Empty); sAddress1 = sAddress1.Replace("</Addr1>", string.Empty); sAddress1 = sAddress1.Replace("'", "'"); } // Extract Address Line 2 Regex oRegexAddress2 = new Regex("<Addr2>.*</Addr2>"); Match oMatchAddress2 = oRegexAddress2.Match(m_oInvoice.BillingAddress); if (oMatchAddress2.Success) { sAddress2 = oMatchAddress2.Value; sAddress2 = sAddress2.Replace("<Addr2>", string.Empty); sAddress2 = sAddress2.Replace("</Addr2>", string.Empty); } // Extract Address Line 3 Regex oRegexAddress3 = new Regex("<Addr3>.*</Addr3>"); Match oMatchAddress3 = oRegexAddress3.Match(m_oInvoice.BillingAddress); if (oMatchAddress3.Success) { sAddress3 = oMatchAddress3.Value; sAddress3 = sAddress3.Replace("<Addr3>", string.Empty); sAddress3 = sAddress3.Replace("</Addr3>", string.Empty); } // Extract City Regex oRegexCity = new Regex("<City>.*</City>"); Match oMatchCity = oRegexCity.Match(m_oInvoice.BillingAddress); if (oMatchCity.Success) { sCity = oMatchCity.Value; sCity = sCity.Replace("<City>", string.Empty); sCity = sCity.Replace("</City>", string.Empty); } // Extract State Regex oRegexState = new Regex("<State>.*</State>"); Match oMatchState = oRegexState.Match(m_oInvoice.BillingAddress); if (oMatchState.Success) { sState = oMatchState.Value; sState = sState.Replace("<State>", string.Empty); sState = sState.Replace("</State>", string.Empty); } // Extract Zip Regex oRegexZip = new Regex("<PostalCode>.*</PostalCode>"); Match oMatchZip = oRegexZip.Match(m_oInvoice.BillingAddress); if (oMatchZip.Success) { sZip = oMatchZip.Value; sZip = sZip.Replace("<PostalCode>", string.Empty); sZip = sZip.Replace("</PostalCode>", string.Empty); } string sFullAddress = string.Empty; if (sAddress1 != string.Empty) { sFullAddress += sAddress1 + "<br/>"; } if (sAddress2 != string.Empty) { sFullAddress += sAddress2 + "<br/>"; } if (sAddress3 != string.Empty) { sFullAddress += sAddress3 + "<br/>"; } sFullAddress += sCity + " " + sState + ", " + sZip; // Perform replacements sHtml = sHtml.Replace("[InvoiceDate]", m_oInvoice.TransactionDate); sHtml = sHtml.Replace("[InvoiceNumber]", m_oInvoice.RefNumber); sHtml = sHtml.Replace("[InvoiceBillTo]", sFullAddress); sHtml = sHtml.Replace("[InvoiceTerms]", "1.5% after 30 days"); sHtml = sHtml.Replace("[InvoiceDueDate]", m_oInvoice.DueDate); if (m_oInvoice.LineItems.Count > 0) { string sLineItems = string.Empty; foreach (InvoiceItem oInvoiceItem in m_oInvoice.LineItems) { string sServiceDate = string.IsNullOrEmpty(oInvoiceItem.ServiceDate) ? " " : oInvoiceItem.ServiceDate; string sDescription = this.CleanDescription(oInvoiceItem.Description); sLineItems += string.Format("<tr><td>{0}</td><td width=\"50%\">{1}</td><td class=\"right\">{2}</td></tr>", sServiceDate, sDescription, oInvoiceItem.Amount); } sHtml = sHtml.Replace("[InvoiceLineItems]", sLineItems); } sHtml = sHtml.Replace("[CustomerMessage]", m_oInvoice.CustomerMessageName); sHtml = sHtml.Replace("[InvoiceTotal]", m_oInvoice.Subtotal); sHtml = sHtml.Replace("[InvoiceCredit]", m_oInvoice.AppliedAmount); sHtml = sHtml.Replace("[InvoiceBalanceDue]", m_oInvoice.BalanceRemaining); this._logger.DebugFormat("Invoice Html: {0}", sHtml); oDocument.ImportFromUrl(sHtml); string sFilename = string.Format("{0}.pdf", Guid.NewGuid()); this._logger.DebugFormat("Writing PDF: {0}", sFilename); oDocument.Save(string.Format(@"{0}\{1}", m_sDirectoryRoot, sFilename)); oDocument.Close(); this._logger.DebugFormat("PDF Written: {0}", string.Format(@"{0}\{1}", m_sDirectoryRoot, sFilename)); SpeedySpots.API.Interfaces.Invoice oSpeedySpotsInvoice = new SpeedySpots.API.Interfaces.Invoice(); oSpeedySpotsInvoice.InvoiceID = m_oInvoice.RefId; oSpeedySpotsInvoice.IAJobID = iJobID; oSpeedySpotsInvoice.CustomerID = m_oInvoice.CustomerId; oSpeedySpotsInvoice.InvoiceNumber = m_oInvoice.RefNumber; oSpeedySpotsInvoice.IsEmail = oSyncInvoice.IsEmail; oSpeedySpotsInvoice.EmailSubject = oSyncInvoice.EmailSubject; oSpeedySpotsInvoice.EmailBody = oSyncInvoice.EmailBody; DateTime oDueDateTime = new DateTime(1900, 1, 1, 0, 0, 0, 0); DateTime.TryParse(m_oInvoice.DueDate, out oDueDateTime); oSpeedySpotsInvoice.DueDateTime = oDueDateTime; decimal fAmount = 0; decimal.TryParse(m_oInvoice.BalanceRemaining, out fAmount); oSpeedySpotsInvoice.Amount = fAmount; oSpeedySpotsInvoice.Filename = sFilename; this._logger.Debug("Added invoice to collection"); m_oListInvoices.Add(oSpeedySpotsInvoice); } else { this._logger.DebugFormat("Invoice date didn't match rule: {0}", oDateTime.ToString()); } this.UpdateProgress(); }