using BiskLog_Point_Of_Sale.Classes; using BiskLog_Point_Of_Sale.Multiple_Login; using BiskLog_Point_Of_Sale.OPS_Report.Sales.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 TruckSummary : Form { SqlConnection cn; SqlCommand cm; SqlDataReader dr; DatabaseConn conn = new DatabaseConn(); List deliveries = new List(); public TruckSummary() { InitializeComponent(); headingPanel.Left = (ClientSize.Width - headingPanel.Width) / 2; rangePanel.Left = (ClientSize.Width - rangePanel.Width) / 2; cn = new SqlConnection(conn.MyConnection()); } public int getList() { try { filter.Invoke(new Action(() => { deliveries.Clear(); dateSetting.Text = "Date : " + dateLower.Value.ToString("yyyy-MM-dd HH:mm:ss") + " to " + dateHigher.Value.ToString("yyyy-MM-dd HH:mm:ss"); sellers.Rows.Clear(); int filterby = 0; if (!String.IsNullOrEmpty(filter.Text)) filterby = int.Parse(filter.Text); sellers.Rows.Clear(); cn.Open(); cm = new SqlCommand("Select top (@filter) t.licensePlate,dh.deliveryID,dh.dateCompleted,dr.fullname,dh.totalcost " + "from tblTruckAssignments TA Inner Join tblTrucks t On t.truckID =TA.truckID Inner Join tblDeliveryHead dh On " + "dh.deliveryID = TA.orderID Inner Join tblDeliveryRecipients dr On dr.deliveryID = TA.orderID where dh.branchID =" + " @branchID and (dh.dateCompleted between(@lower) and (@higher)) and dh.status = 'DELIVERED' group by t.licensePlate, " + "dh.dateCompleted, dh.deliveryID,dr.fullname, dh.totalCost order by dh.dateCompleted asc", cn); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.Parameters.AddWithValue("@filter", filterby); cm.Parameters.AddWithValue("@lower", dateLower.Value.ToString("yyyy-MM-dd")); cm.Parameters.AddWithValue("@higher", dateHigher.Value.ToString("yyyy-MM-dd")); cm.ExecuteNonQuery(); dr = cm.ExecuteReader(); while (dr.Read()) { Deliveries getDelivery = new Deliveries(); getDelivery.truckLicense = dr[0].ToString(); getDelivery.orderid = dr[1].ToString(); getDelivery.deliverydate = Convert.ToDateTime(dr[2].ToString()).ToLongDateString(); getDelivery.recipient = dr[3].ToString(); getDelivery.deliverycost = Settings.Default.currrencyCode + " " + dr[4].ToString(); deliveries.Add(getDelivery); } dr.Close(); cn.Close(); })); return 1; } catch (Exception ex) { cn.Close(); ErrorLogging.WriteToFile(ex.ToString()); return 0; } } private async void TruckSummary_Load(object sender, EventArgs e) { Task task = new Task(getList); 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 (Deliveries varseller in deliveries) { sellers.Rows.Add(varseller.truckLicense, varseller.orderid, varseller.recipient, varseller.deliverydate, varseller.deliverycost); } } 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 = 4; worksheet.Cell("A1").Value = title.Text; worksheet.Cell("A2").Value = dateSetting.Text; worksheet.Cell("A3").Value = "TRUCK"; worksheet.Cell("B3").Value = "ORDER ID"; worksheet.Cell("C3").Value = "RECIPIENT"; worksheet.Cell("D3").Value = "DELIVERY DATE"; worksheet.Cell("E3").Value = "WORTH"; worksheet.Range("A1:E1").Row(1).Merge(); worksheet.Range("A2:E2").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("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; foreach (Deliveries 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.truckLicense; worksheet.Cell("B" + i).Value = row.orderid; worksheet.Cell("C" + i).Value = row.recipient; worksheet.Cell("D" + i).Value = row.deliverydate; worksheet.Cell("E" + i).Value = row.deliverycost; 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("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.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 void Button1_Click(object sender, EventArgs e) { if (deliveries.Count > 0) { string printerReportDefault = Settings.Default.ReportPrinter; string printLocation; ReportHeaders headers = new ReportHeaders(); headers.header1 = title.Text; headers.header2 = dateSetting.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\Deliveries.rdlc"; report.ReportPath = fullpath; report.DataSources.Add(new ReportDataSource("Deliveries", deliveries)); 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\Sales\Reports\Deliveries.rdlc"; report.ReportPath = fullpath; report.DataSources.Add(new ReportDataSource("Deliveries", deliveries)); 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(); } } private async void Button2_Click(object sender, EventArgs e) { if (deliveries.Count > 0) { string filelocation = ""; DialogResult result = excelExport.ShowDialog(); if (result == DialogResult.OK) { filelocation = excelExport.FileName; Task task = new Task(() => GenerateExcel(deliveries, 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(); } } } }