Biskilog POS desktop appilcation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

297 lines
15 KiB

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 DailySalesDetailed : Form
{
SqlConnection cn;
SqlCommand cm;
SqlDataReader dr;
DatabaseConn conn = new DatabaseConn();
List<EmployeeSalesClass> salesClasses = new List<EmployeeSalesClass>();
public DailySalesDetailed()
{
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,p.product_name ,tc.quantity,tc.date,tc.total,u.unitshort from tblCart tc " +
"Inner Join tblUsers e On e.username = tc.cashier Inner Join tblProduct p On p.pcode = tc.id Inner Join UnitOfMeasure u On u.unitCode = " +
"tc.unit where tc.branchID = @branchID " +
"and tc.status = 'SOLD' and (YEAR(tc.date) = @year and MONTH(tc.date) = @month and Day(tc.date) = @day) " +
"group by e.firstname, e.surname, tc.transno, p.product_name, tc.quantity, tc.date, tc.total,u.unitshort " +
"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.product = dr[3].ToString();
employeeSales.sold = int.Parse(dr[4].ToString());
employeeSales.time = Convert.ToDateTime(dr[5].ToString()).ToLongTimeString();
employeeSales.total = decimal.Parse(dr[6].ToString());
employeeSales.sales = Settings.Default.currrencyCode + " " + dr[6].ToString();
employeeSales.unit = dr[7].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<int> task = new Task<int>(getSales);
EpE.Visible = true;
holding.Value = 0;
task.Start();
var progress = new Progress<int>(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.product, varseller.sold + " "+ varseller.unit, 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<EmployeeSalesClass> 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 = "PRODUCT NAME";
worksheet.Cell("E4").Value = "QUANTITY";
worksheet.Cell("F4").Value = "REVENUE";
worksheet.Range("A1:F1").Row(1).Merge();
worksheet.Range("A2:F2").Row(1).Merge();
worksheet.Range("A3:F3").Row(1).Merge();
worksheet.Range("A1:F1").Style.Fill.BackgroundColor = XLColor.FromArgb(10, 10, 56);
worksheet.Range("A2:F2").Style.Fill.BackgroundColor = XLColor.FromArgb(10, 10, 56);
worksheet.Range("A3:F3").Style.Fill.BackgroundColor = XLColor.FromArgb(10, 10, 56);
worksheet.Range("A4:F4").Style.Fill.BackgroundColor = XLColor.FromArgb(10, 10, 56);
worksheet.Range("A1:F1").Style.Font.FontColor = XLColor.White;
worksheet.Range("A2:F2").Style.Font.FontColor = XLColor.White;
worksheet.Range("A3:F3").Style.Font.FontColor = XLColor.White;
worksheet.Range("A4:F4").Style.Font.FontColor = XLColor.White;
foreach (EmployeeSalesClass row in publish)
{
worksheet.Cell("A" + i).Style.Border.SetLeftBorder(XLBorderStyleValues.Thick);
worksheet.Cell("F" + 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.product;
worksheet.Cell("E" + i).Value = row.sold;
worksheet.Cell("F" + 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);
worksheet.Cell("F" + i).Style.Border.SetOutsideBorder(XLBorderStyleValues.Thin);
i++;
}
worksheet.Cell("A1").Style.Border.SetLeftBorder(XLBorderStyleValues.Thick);
worksheet.Cell("F1").Style.Border.SetRightBorder(XLBorderStyleValues.Thick);
worksheet.Cell("A2").Style.Border.SetLeftBorder(XLBorderStyleValues.Thick);
worksheet.Cell("F2").Style.Border.SetRightBorder(XLBorderStyleValues.Thick);
worksheet.Cell("A3").Style.Border.SetLeftBorder(XLBorderStyleValues.Thick);
worksheet.Cell("F3").Style.Border.SetRightBorder(XLBorderStyleValues.Thick);
worksheet.Cell("A4").Style.Border.SetLeftBorder(XLBorderStyleValues.Thick);
worksheet.Cell("F4").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.Cell("F4").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<int> task = new Task<int>(() => GenerateExcel(salesClasses, filelocation));
EpE.Visible = true;
holding.Value = 0;
task.Start();
var progress = new Progress<int>(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<ProductHeaders> reportHeaders = new List<ProductHeaders>();
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\DailySalesDetailed.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\DailySalesDetailed.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();
}
}
}
}