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 LiveCharts; using LiveCharts.Wpf; 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 EmployeeSalesChart : Form { SqlConnection cn; SqlCommand cm; SqlDataReader dr; DatabaseConn conn = new DatabaseConn(); List employeeSales = new List(); public EmployeeSalesChart() { InitializeComponent(); cn = new SqlConnection(conn.MyConnection()); headingPanel.Left = (ClientSize.Width - headingPanel.Width) / 2; rangePanel.Left = (ClientSize.Width - rangePanel.Width) / 2; buttons.Left = (ClientSize.Width - buttons.Width) / 2; } public void setChart() { try { Func labelPoint = chartPoint => string.Format("{0} ({1:P})", chartPoint.Y, chartPoint.Participation); SeriesCollection pieChart = new SeriesCollection(); foreach (EmployeeSalesClass chart in employeeSales) { pieChart.Add( new PieSeries { Title = chart.employeename, Values = new ChartValues { chart.sold }, DataLabels = true, LabelPoint = labelPoint }); //chart2.InnerRadius = 25; } Saleschart.Series = pieChart; Saleschart.LegendLocation = LegendLocation.Right; } catch (Exception ex) { ErrorLogging.WriteToFile(ex.ToString()); } } public int getSales() { try { employeeSales.Clear(); Saleschart.Invoke(new Action(() => { int filter = 5; if (!String.IsNullOrEmpty(this.filter.Text)) { filter = int.Parse(this.filter.Text); } title.Text = "Top " + filter.ToString() + " Employee Sales Chart"; head.Text = "Date : " + dateLower.Value.ToLongDateString() + " to " + dateHigher.Value.ToLongDateString(); cn.Open(); cm = new SqlCommand("Select top (@filter) u.firstname,u.surname,Sum(tc.quantity) as number,Sum(tc.price) from tblUsers u Inner Join tblCart tc On tc.cashier = u.username " + "where u.branchID = @branchID and(tc.date between @lower and @higher) group by u.firstname, u.surname order by number desc", cn); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Parameters.AddWithValue("@lower", dateLower.Value.ToString("yyyy-MM-dd")); cm.Parameters.AddWithValue("@higher", dateHigher.Value.ToString("yyyy-MM-dd")); cm.Parameters.AddWithValue("@filter", filter); cm.ExecuteNonQuery(); dr = cm.ExecuteReader(); while (dr.Read()) { EmployeeSalesClass salesClass = new EmployeeSalesClass(); salesClass.employeename = dr[0].ToString().ToUpper() + " " + dr[1].ToString().ToUpper(); salesClass.sold = int.Parse(dr[2].ToString()); salesClass.sales = Settings.Default.currrencyCode + " " + dr[3].ToString(); salesClass.total = decimal.Parse(dr[3].ToString()); employeeSales.Add(salesClass); } dr.Close(); cn.Close(); })); return 1; } catch (Exception ex) { cn.Close(); ErrorLogging.WriteToFile(ex.ToString()); return 0; } } private async void EmployeeSalesChart_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) { setChart(); } 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 void Button1_Click(object sender, EventArgs e) { if (employeeSales.Count > 0) { string printerReportDefault = Settings.Default.ReportPrinter; string printLocation; ProductHeaders headers = new ProductHeaders(); headers.header1 = title.Text; headers.header2 = head.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\Employee\Reports\EmployeeSalesChart.rdlc"; report.ReportPath = fullpath; report.DataSources.Add(new ReportDataSource("Sales", employeeSales)); report.DataSources.Add(new ReportDataSource("Header", 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\Employee\Reports\EmployeeSalesChart.rdlc"; report.ReportPath = fullpath; report.DataSources.Add(new ReportDataSource("Sales", employeeSales)); report.DataSources.Add(new ReportDataSource("Header", 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(); } } } }