using BiskLog_Point_Of_Sale.Classes; using BiskLog_Point_Of_Sale.Multiple_Login; using BiskLog_Point_Of_Sale.OPS_Report.Customers.Classes; using BiskLog_Point_Of_Sale.OPS_Report.Products.Classes; using BiskLog_Point_Of_Sale.Properties; using ClosedXML.Excel; using Microsoft.Reporting.WinForms; using Point_Of_Sale_Managment; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace BiskLog_Point_Of_Sale.OPS_Report.Sales { public partial class CustomerList : Form { SqlConnection cn; SqlCommand cm; SqlDataReader dr; DatabaseConn conn = new DatabaseConn(); List customers = new List(); public CustomerList() { InitializeComponent(); cn = new SqlConnection(conn.MyConnection()); headingPanel.Left = (ClientSize.Width - headingPanel.Width) / 2; buttons.Left = (ClientSize.Width - buttons.Width) / 2; EpE.Left = (ClientSize.Width - EpE.Width) / 2; EpE.Top = (ClientSize.Height - EpE.Height) / 2; } public int getCustomers() { try { sellers.Invoke(new Action(() => { sellers.Rows.Clear(); int filter = 100; if (!String.IsNullOrEmpty(this.filter.Text)) { filter = int.Parse(this.filter.Text); } this.customers.Clear(); cn.Open(); cm = new SqlCommand("SELECT TOP (@filter) customerID,firstname,surname,address,telephone,tin,email from tblCustomers where branchID = @branchID", cn); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Parameters.AddWithValue("@filter", filter); cm.ExecuteNonQuery(); dr = cm.ExecuteReader(); while (dr.Read()) { CustomersClass customers = new CustomersClass(); customers.customerID = dr[0].ToString().ToUpper(); customers.firstname = dr[1].ToString().ToUpper(); customers.surname = dr[2].ToString().ToUpper(); customers.address = dr[3].ToString().ToUpper(); customers.telephone = dr[4].ToString(); customers.tin = dr[5].ToString().ToUpper(); customers.email = dr[6].ToString().ToUpper(); this.customers.Add(customers); } dr.Close(); cn.Close(); })); return 1; } catch (Exception ex) { cn.Close(); ErrorLogging.WriteToFile(ex.ToString()); return 0; } } private int GenerateExcel(List publish, string filepath) { try { var workbook = new XLWorkbook(); var worksheet = workbook.Worksheets.Add(title.Text); int i = 4; worksheet.Cell("A1").Value = title.Text; worksheet.Cell("A3").Value = "CUSTOMER ID"; worksheet.Cell("B3").Value = "TIN"; worksheet.Cell("C3").Value = "FIRSTNAME"; worksheet.Cell("D3").Value = "SURNAME"; worksheet.Cell("E3").Value = "ADDRESS"; worksheet.Cell("F3").Value = "EMAIL"; worksheet.Cell("G3").Value = "PHONE"; worksheet.Range("A1:G1").Row(1).Merge(); worksheet.Range("A2:G2").Row(1).Merge(); worksheet.Range("A1:G1").Style.Fill.BackgroundColor = XLColor.FromArgb(10, 10, 56); worksheet.Range("A2:G2").Style.Fill.BackgroundColor = XLColor.FromArgb(10, 10, 56); worksheet.Range("A3:G3").Style.Fill.BackgroundColor = XLColor.FromArgb(10, 10, 56); worksheet.Range("A1:G1").Style.Font.FontColor = XLColor.White; worksheet.Range("A2:G2").Style.Font.FontColor = XLColor.White; worksheet.Range("A3:G3").Style.Font.FontColor = XLColor.White; foreach (CustomersClass row in publish) { worksheet.Cell("A" + i).Style.Border.SetLeftBorder(XLBorderStyleValues.Thick); worksheet.Cell("G" + i).Style.Border.SetRightBorder(XLBorderStyleValues.Thick); worksheet.Cell("A" + i).Value = row.customerID; worksheet.Cell("B" + i).Value = row.tin; worksheet.Cell("C" + i).Value = row.firstname; worksheet.Cell("D" + i).Value = row.surname; worksheet.Cell("E" + i).Value = row.address; worksheet.Cell("F" + i).Value = row.email; worksheet.Cell("G" + i).Value = row.telephone; worksheet.Cell("A" + i).Style.Border.SetBottomBorder(XLBorderStyleValues.Thin); worksheet.Cell("B" + i).Style.Border.SetOutsideBorder(XLBorderStyleValues.Thin); worksheet.Cell("C" + i).Style.Border.SetOutsideBorder(XLBorderStyleValues.Thin); worksheet.Cell("D" + i).Style.Border.SetBottomBorder(XLBorderStyleValues.Thin); worksheet.Cell("E" + i).Style.Border.SetBottomBorder(XLBorderStyleValues.Thin); worksheet.Cell("F" + i).Style.Border.SetBottomBorder(XLBorderStyleValues.Thin); worksheet.Cell("G" + i).Style.Border.SetOutsideBorder(XLBorderStyleValues.Thin); i++; } worksheet.Cell("A1").Style.Border.SetLeftBorder(XLBorderStyleValues.Thick); worksheet.Cell("G1").Style.Border.SetRightBorder(XLBorderStyleValues.Thick); worksheet.Cell("A2").Style.Border.SetLeftBorder(XLBorderStyleValues.Thick); worksheet.Cell("G2").Style.Border.SetRightBorder(XLBorderStyleValues.Thick); worksheet.Cell("A3").Style.Border.SetLeftBorder(XLBorderStyleValues.Thick); worksheet.Cell("G3").Style.Border.SetRightBorder(XLBorderStyleValues.Thick); worksheet.Cell("A3").Style.Border.SetBottomBorder(XLBorderStyleValues.Thick); worksheet.Cell("B3").Style.Border.SetBottomBorder(XLBorderStyleValues.Thick); worksheet.Cell("C3").Style.Border.SetBottomBorder(XLBorderStyleValues.Thick); worksheet.Cell("D3").Style.Border.SetBottomBorder(XLBorderStyleValues.Thick); worksheet.Cell("E3").Style.Border.SetBottomBorder(XLBorderStyleValues.Thick); worksheet.Cell("F3").Style.Border.SetBottomBorder(XLBorderStyleValues.Thick); worksheet.Cell("G3").Style.Border.SetBottomBorder(XLBorderStyleValues.Thick); worksheet.Cells().Style.Alignment.Vertical = XLAlignmentVerticalValues.Center; worksheet.Cells().Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; worksheet.Row(1).Height = 20.0; worksheet.Row(2).Height = 20.0; worksheet.Columns().AdjustToContents(); worksheet.Column(1).Width = 30.43; workbook.SaveAs(filepath); return 1; } catch (Exception ex) { ErrorLogging.WriteToFile(ex.ToString()); return 0; } } private async void CustomerList_Load(object sender, EventArgs e) { Task task = new Task(getCustomers); EpE.Visible = true; holding.Value = 0; task.Start(); var progress = new Progress(percent => { holding.Value = percent; }); await Task.Run(() => Progression.DoSomething(progress)); int result = await task; if (result == 1) { foreach (CustomersClass varseller in customers) { sellers.Rows.Add(varseller.customerID, varseller.tin, varseller.firstname, varseller.surname, varseller.address, varseller.email, varseller.telephone); } } else { string title = "Error Occurred"; string message = "An error occurred while trying to generate report, please try again later"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } EpE.Visible = false; } private async void button2_Click(object sender, EventArgs e) { if (customers.Count > 0) { string filelocation = ""; DialogResult result = excelExport.ShowDialog(); if (result == DialogResult.OK) { filelocation = excelExport.FileName; Task task = new Task(() => GenerateExcel(customers, filelocation)); EpE.Visible = true; holding.Value = 0; task.Start(); var progress = new Progress(percent => { holding.Value = percent; }); await Task.Run(() => Progression.DoSomething(progress)); int report = await task; if (report == 1) { string title = "Success"; string message = "Excel report exported successfully"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } else { string title = "Error Occurred"; string message = "An error occurred while exporting the excel report, please try again later."; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } EpE.Visible = false; } } else { string title = "Report Not available"; string message = "Sorry there was no report generated to complete this action, please try again later."; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } } private void button1_Click(object sender, EventArgs e) { if (customers.Count > 0) { string printerReportDefault = Settings.Default.ReportPrinter; string printLocation; ProductHeaders headers = new ProductHeaders(); headers.header1 = title.Text; List reportHeaders = new List(); reportHeaders.Add(headers); if (printerReportDefault == "Microsoft XPS Document Writer" || printerReportDefault == "Microsoft Print to PDF") { FolderBrowserDialog folder = new FolderBrowserDialog(); DialogResult result = folder.ShowDialog(); if (result == DialogResult.OK && !String.IsNullOrEmpty(folder.SelectedPath)) { printLocation = folder.SelectedPath + @"\" + title.Text + @"report" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf"; LocalReport report = new LocalReport(); string path = Path.GetDirectoryName(Application.ExecutablePath); string fullpath = Path.GetDirectoryName(Application.ExecutablePath).Remove(path.Length - 10) + @"\OPS Report\Customers\Reports\Customers.rdlc"; report.ReportPath = fullpath; report.DataSources.Add(new ReportDataSource("Customers", customers)); report.DataSources.Add(new ReportDataSource("Headers", reportHeaders)); ReportConfig.PrintToPrinter(report, printLocation, printerReportDefault); } } else { printLocation = ""; LocalReport report = new LocalReport(); string path = Path.GetDirectoryName(Application.ExecutablePath); string fullpath = Path.GetDirectoryName(Application.ExecutablePath).Remove(path.Length - 10) + @"\OPS Report\Customers\Reports\Customers.rdlc"; report.ReportPath = fullpath; report.DataSources.Add(new ReportDataSource("Customers", customers)); report.DataSources.Add(new ReportDataSource("Headers", reportHeaders)); ReportConfig.PrintToPrinter(report, printLocation, printerReportDefault); } } else { string title = "Report Not available"; string message = "Sorry there was no report generated to complete this action, please try again later."; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); } } } }