using BiskLog_Point_Of_Sale.Classes; using BiskLog_Point_Of_Sale.Multiple_Login; using BiskLog_Point_Of_Sale.OPS_Report.Employee.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 DailySales : Form { SqlConnection cn; SqlCommand cm; SqlDataReader dr; DatabaseConn conn = new DatabaseConn(); List salesClasses = new List(); public DailySales() { InitializeComponent(); cn = new SqlConnection(conn.MyConnection()); headingPanel.Left = (ClientSize.Width - headingPanel.Width) / 2; rangePanel.Left = (ClientSize.Width - rangePanel.Width) / 2; EpE.Left = (ClientSize.Width - EpE.Width) / 2; EpE.Top = (ClientSize.Height - EpE.Height) / 2; buttons.Left = (ClientSize.Width - buttons.Width) / 2; } public int getSales() { try { sellers.Invoke(new Action(() => { sellers.Rows.Clear(); salesClasses.Clear(); salesDate.Text = "Date : " + dateSales.Value.ToLongDateString(); cn.Open(); cm = new SqlCommand("Select e.firstname,e.surname,tc.transno,Sum(tc.quantity),tc.date,Sum(tc.total) from tblCart tc Inner Join tblUsers e On e.username = tc.cashier " + "where tc.branchID = @branchID and tc.status = @status and(YEAR(tc.date) = @year and MONTH(tc.date) = @month and Day(tc.date) = @day) "+ "group by e.firstname, e.surname, tc.transno,tc.date order by tc.date asc", cn); cm.Parameters.AddWithValue("@status", "SOLD"); cm.Parameters.AddWithValue("@year", dateSales.Value.Year.ToString()); cm.Parameters.AddWithValue("@month", dateSales.Value.Month.ToString()); cm.Parameters.AddWithValue("@day", dateSales.Value.Day.ToString()); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.ExecuteNonQuery(); dr = cm.ExecuteReader(); while (dr.Read()) { EmployeeSalesClass employeeSales = new EmployeeSalesClass(); employeeSales.employeename = dr[0].ToString().ToUpper() + " " + dr[1].ToString().ToUpper(); employeeSales.receiptid = dr[2].ToString(); employeeSales.sold = int.Parse(dr[3].ToString()); employeeSales.time = Convert.ToDateTime(dr[4].ToString()).ToLongTimeString(); employeeSales.total = decimal.Parse(dr[5].ToString()); employeeSales.sales = Settings.Default.currrencyCode + " " + dr[5].ToString(); salesClasses.Add(employeeSales); } dr.Close(); cn.Close(); decimal totalEveything = salesClasses.Sum(t => t.total); dateWorth.Text = "Total Sales : " + Settings.Default.currrencyCode + " " + totalEveything.ToString(); })); return 1; } catch (Exception ex) { cn.Close(); ErrorLogging.WriteToFile(ex.ToString()); return 0; } } private async void DailySales_Load(object sender, EventArgs e) { Task task = new Task(getSales); 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 (EmployeeSalesClass varseller in salesClasses) { sellers.Rows.Add(varseller.employeename, varseller.time, varseller.receiptid, varseller.sold, varseller.sales); } } 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 int GenerateExcel(List publish, string filepath) { try { var workbook = new XLWorkbook(); var worksheet = workbook.Worksheets.Add(title.Text); int i = 5; worksheet.Cell("A1").Value = title.Text; worksheet.Cell("A2").Value = salesDate.Text; worksheet.Cell("A3").Value = dateWorth.Text; worksheet.Cell("A4").Value = "EMPLOYEE"; worksheet.Cell("B4").Value = "TIME"; worksheet.Cell("C4").Value = "RECEIPT ID"; worksheet.Cell("D4").Value = "QUANTITY"; worksheet.Cell("E4").Value = "REVENUE"; worksheet.Range("A1:E1").Row(1).Merge(); worksheet.Range("A2:E2").Row(1).Merge(); worksheet.Range("A3:E3").Row(1).Merge(); worksheet.Range("A1:E1").Style.Fill.BackgroundColor = XLColor.FromArgb(10, 10, 56); worksheet.Range("A2:E2").Style.Fill.BackgroundColor = XLColor.FromArgb(10, 10, 56); worksheet.Range("A3:E3").Style.Fill.BackgroundColor = XLColor.FromArgb(10, 10, 56); worksheet.Range("A4:E4").Style.Fill.BackgroundColor = XLColor.FromArgb(10, 10, 56); worksheet.Range("A1:E1").Style.Font.FontColor = XLColor.White; worksheet.Range("A2:E2").Style.Font.FontColor = XLColor.White; worksheet.Range("A3:E3").Style.Font.FontColor = XLColor.White; worksheet.Range("A4:E4").Style.Font.FontColor = XLColor.White; foreach (EmployeeSalesClass row in publish) { worksheet.Cell("A" + i).Style.Border.SetLeftBorder(XLBorderStyleValues.Thick); worksheet.Cell("E" + i).Style.Border.SetRightBorder(XLBorderStyleValues.Thick); worksheet.Cell("A" + i).Value = row.employeename; worksheet.Cell("B" + i).Value = row.time; worksheet.Cell("C" + i).Value = row.receiptid; worksheet.Cell("D" + i).Value = row.sold; worksheet.Cell("E" + i).Value = row.sales; 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.SetOutsideBorder(XLBorderStyleValues.Thin); worksheet.Cell("E" + i).Style.Border.SetOutsideBorder(XLBorderStyleValues.Thin); i++; } worksheet.Cell("A1").Style.Border.SetLeftBorder(XLBorderStyleValues.Thick); worksheet.Cell("E1").Style.Border.SetRightBorder(XLBorderStyleValues.Thick); worksheet.Cell("A2").Style.Border.SetLeftBorder(XLBorderStyleValues.Thick); worksheet.Cell("E2").Style.Border.SetRightBorder(XLBorderStyleValues.Thick); worksheet.Cell("A3").Style.Border.SetLeftBorder(XLBorderStyleValues.Thick); worksheet.Cell("E3").Style.Border.SetRightBorder(XLBorderStyleValues.Thick); worksheet.Cell("A4").Style.Border.SetLeftBorder(XLBorderStyleValues.Thick); worksheet.Cell("E4").Style.Border.SetRightBorder(XLBorderStyleValues.Thick); worksheet.Cell("A4").Style.Border.SetBottomBorder(XLBorderStyleValues.Thick); worksheet.Cell("B4").Style.Border.SetBottomBorder(XLBorderStyleValues.Thick); worksheet.Cell("C4").Style.Border.SetBottomBorder(XLBorderStyleValues.Thick); worksheet.Cell("D4").Style.Border.SetBottomBorder(XLBorderStyleValues.Thick); worksheet.Cell("E4").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 Button2_Click(object sender, EventArgs e) { if (salesClasses.Count > 0) { string filelocation = ""; DialogResult result = excelExport.ShowDialog(); if (result == DialogResult.OK) { filelocation = excelExport.FileName; Task task = new Task(() => GenerateExcel(salesClasses, 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 (salesClasses.Count > 0) { string printerReportDefault = Settings.Default.ReportPrinter; string printLocation; ProductHeaders headers = new ProductHeaders(); headers.header1 = title.Text; headers.header2 = salesDate.Text; headers.header3 = dateWorth.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\Sales\Reports\DailySalesSummary.rdlc"; report.ReportPath = fullpath; report.DataSources.Add(new ReportDataSource("Sales", salesClasses)); 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\Sales\Reports\DailySalesSummary.rdlc"; report.ReportPath = fullpath; report.DataSources.Add(new ReportDataSource("Sales", salesClasses)); 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(); } } } }