private void btnGenSend_Click(object sender, EventArgs e)
        {
            String   fileName = String.Empty;
            DateTime statementDate;

            if (CreateStatement(true, out fileName, out statementDate))
            {
                List <String> attachments = new List <string>();
                attachments.Add(fileName);
                String attachment = txtAttachment.Text;
                if (!attachment.StartsWith("K:") && !String.IsNullOrEmpty(attachment))
                {
                    File.Copy(attachment, Path.Combine(Pastel.PastelRoot + "Debtors System\\statement test", Path.GetFileName(attachment)), true);
                    attachment = Path.GetFileName(attachment);
                }
                if (!String.IsNullOrEmpty(attachment))
                {
                    attachments.Add(attachment);
                }
                String[] att          = attachments.ToArray();
                String   status       = String.Empty;
                string   emailAddress = "";
                if (stmt != null && stmt.email1 != null && stmt.email1.Length > 0)
                {
                    emailAddress = stmt.email1[0];
                }

                var statementURL = _ClientPortal.InsertStatement(stmt.BuildingId, stmt.AccNo, stmt.StmtDate, fileName, File.ReadAllBytes(fileName), emailAddress);

                bool isRental = stmt.BuildingName == "ASTRODON RENTALS";
                if (Email.EmailProvider.SendStatement(stmt.DebtorEmail, stmt.email1, stmt.AccNo, fileName, stmt.StmtDate, statementURL, isRental))
                {
                    MessageBox.Show("Message Sent");
                }
                else
                {
                    Controller.HandleError("Unable to send mail: " + status);
                }
            }
            else
            {
                Controller.HandleError("Unable to create statement");
            }
        }
        private void btnProcess_Click(object sender, EventArgs e)
        {
            _ProgressForm = frmProgress.ShowForm();

            List <string> statementFileList = new List <string>();

            this.Cursor = Cursors.WaitCursor;
            statements  = new Statements {
                statements = new List <Statement>()
            };
            Dictionary <String, bool> hasStatements = new Dictionary <string, bool>();

            foreach (DataGridViewRow dvr in dgBuildings.Rows)
            {
                if ((bool)dvr.Cells[0].Value)
                {
                    String buildingName = dvr.Cells[1].Value.ToString();
                    AddProgressString("Loading Building " + buildingName);

                    SetBuildingStatement(buildingName);
                    String datapath = dvr.Cells[5].Value.ToString();
                    int    period   = (int)dvr.Cells[6].Value;
                    if (dvr.Cells[2].Value == null)
                    {
                        MessageBox.Show("ishoa");
                    }

                    StatementBuilding strm = dvr.DataBoundItem as StatementBuilding;


                    List <Statement> bStatements = SetBuildings(strm.GetBuildingId(), buildingName, datapath, period, (bool)dvr.Cells[2].Value);

                    int     idx           = dvr.Index;
                    DataRow dr            = dsBuildings.Tables[0].Rows[idx];
                    String  pm            = dr["pm"].ToString();
                    String  bankName      = dr["bankName"].ToString();
                    String  accName       = dr["accName"].ToString();
                    String  bankAccNumber = dr["bankAccNumber"].ToString();
                    String  branch        = dr["branch"].ToString();
                    bool    isStd         = bankName.ToLower().Contains("standard");
                    foreach (Statement s in bStatements)
                    {
                        s.pm                = pm;
                        s.bankName          = bankName;
                        s.accName           = accName;
                        s.BankAccountNumber = bankAccNumber;
                        s.branch            = branch;
                        s.isStd             = isStd;
                        statements.statements.Add(s);
                    }
                }
            }
            PDF generator = new PDF(true);

            foreach (Statement stmt in statements.statements)
            {
                String fileName   = String.Empty;
                bool   canProcess = false;
                if (stmt.IsInTransfer && stmt.InTransferLetter != null)
                {
                    string folderPath = generator.StatementFolderPath;
                    fileName = Path.Combine(folderPath, String.Format("{0} - InTransfer - {1}{2}.pdf", stmt.AccNo.Replace(@"/", "-").Replace(@"\", "-"), DateTime.Now.ToString("dd-MMMM-yyyy"), ""));
                    if (!Directory.Exists(Path.GetDirectoryName(fileName)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(fileName));
                    }
                    if (File.Exists(fileName))
                    {
                        File.Delete(fileName);
                    }
                    File.WriteAllBytes(fileName, stmt.InTransferLetter);
                    canProcess = true;
                }
                else
                {
                    canProcess = generator.CreateStatement(stmt, stmt.BuildingName != "ASTRODON RENTALS" ? true : false, out fileName, stmt.isStd);
                }

                if (canProcess)
                {
                    #region Upload Letter

                    String actFileTitle = Path.GetFileNameWithoutExtension(fileName);
                    String actFile      = Path.GetFileName(fileName);

                    #endregion Upload Letter

                    string statementURL = string.Empty;

                    #region Upload Me
                    try
                    {
                        AddProgressString(stmt.BuildingName + ": " + stmt.accName + " - Upload statement to website");

                        string emailAddress = "";
                        if (stmt.email1 != null && stmt.email1.Length > 0)
                        {
                            emailAddress = stmt.email1[0];
                        }

                        statementURL = _ClientPortal.InsertStatement(stmt.BuildingId, stmt.AccNo, stmt.StmtDate, fileName, File.ReadAllBytes(fileName), emailAddress);
                    }
                    catch (Exception ex)
                    {
                        statementURL = "";
                        AddProgressString(stmt.BuildingName + ": " + stmt.accName + " - Error Upload statement to website " + ex.Message);
                    }
                    #endregion

                    #region Email Me
                    if (stmt.EmailMe)
                    {
                        if (!String.IsNullOrEmpty(fileName))
                        {
                            if (!hasStatements.ContainsKey(stmt.BuildingName))
                            {
                                hasStatements.Add(stmt.BuildingName, true);
                            }
                            if (Controller.user.id != 1 && !String.IsNullOrWhiteSpace(statementURL))
                            {
                                SetupEmail(stmt, fileName, statementURL);
                            }
                        }
                    }
                    #endregion

                    #region Print Me
                    if (stmt.PrintMe && Controller.user.id != 1)
                    {
                        if (!String.IsNullOrWhiteSpace(fileName))
                        {
                            AddProgressString(stmt.BuildingName + ": " + stmt.AccNo + " - Add Statement to List - " + Path.GetFileName(fileName));
                            statementFileList.Add(fileName);
                            // SendToPrinter(fileName, stmt.BuildingName , stmt.AccNo);
                        }
                        else
                        {
                            AddProgressString(stmt.BuildingName + ": " + stmt.AccNo + " - Error Printing Statement - file name blank");
                        }
                    }
                    #endregion

                    Application.DoEvents();
                }
                else
                {
                    AddProgressString(stmt.BuildingName + ": " + stmt.AccNo + " - ERROR Processing Statement");
                    Application.DoEvents();
                }
            }

            foreach (DataGridViewRow dvr in dgBuildings.Rows)
            {
                dvr.Cells[0].Value = false;
            }

            foreach (KeyValuePair <String, bool> hasStatement in hasStatements)
            {
                String query = "INSERT INTO tblStatements(building, lastProcessed) VALUES(@building, @lastProcessed)";
                Dictionary <String, Object> sqlParms = new Dictionary <string, object>();
                sqlParms.Add("@building", hasStatement.Key);
                sqlParms.Add("@lastProcessed", DateTime.Now);
                dh.SetData(query, sqlParms, out status);
            }

            CombinePDFsAndPrint(statementFileList);

            this.Cursor = Cursors.Arrow;

            _ProgressForm.Focus();
            _ProgressForm.ProcessComplete();
            _ProgressForm = null;
        }