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.

457 lines
22 KiB

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 bestworst : Form
{
SqlConnection cn;
SqlCommand cm;
SqlDataReader dr;
DatabaseConn conn = new DatabaseConn();
List<Sellers> bestSellersList = new List<Sellers>();
List<Sellers> worstSellersList = new List<Sellers>();
public bestworst()
{
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;
}
private void Bestsellers_CheckedChanged(object sender, EventArgs e)
{
if (bestsellers.Checked)
{
title.Text = "Best Sellers";
if (bestSellersList.Count > 0)
{
sellers.Rows.Clear();
foreach (Sellers res in bestSellersList)
{
sellers.Rows.Add(res.category, res.productname, res.quantity, Settings.Default.currrencyCode + " " + res.revenue);
}
}
}
}
private void WorstSellers_CheckedChanged(object sender, EventArgs e)
{
if (worstSellers.Checked)
{
title.Text = "Worst Sellers";
if (worstSellersList.Count > 0)
{
sellers.Rows.Clear();
foreach (Sellers res in worstSellersList)
{
sellers.Rows.Add(res.category, res.productname, res.quantity, Settings.Default.currrencyCode + " " + res.revenue);
}
}
}
}
public int getSellers()
{
bestSellersList.Clear();
worstSellersList.Clear();
try
{
filter.Invoke(new Action(() =>
{
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) c.category,p.product_name,Sum(Cart.quantity) as sold,Sum(Cart.price),u.unitshort from tblProduct p Inner Join tblCategory c On c.id = p.cid " +
"Inner Join tblCart Cart On p.pcode = Cart.id Inner Join UnitOfMeasure u On u.unitCode = Cart.unit " +
"where Cart.branchID = @branchID and(cart.date between(@lower) and(@higher)) and cart.status = 'SOLD' group by c.category, product_name,u.unitshort order by sold desc " +
"Select top(@filter) c.category, p.product_name, Sum(Cart.quantity) as sold, Sum(Cart.price) from tblProduct p Inner Join tblCategory c On c.id = p.cid " +
"Inner Join tblCart Cart On p.pcode = Cart.id Inner Join UnitOfMeasure u On u.unitCode = Cart.unit where Cart.branchID = @branchID and(cart.date between(@lower) and(@higher)) and cart.status = 'SOLD' " +
"group by c.category, product_name,u.unitshort order by sold 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())
{
Sellers best = new Sellers();
best.category = dr[0].ToString().ToUpper();
best.productname = dr[1].ToString().ToUpper();
best.quantity = int.Parse(dr[2].ToString());
best.revenue = decimal.Parse(dr[3].ToString());
best.unit = dr[4].ToString();
bestSellersList.Add(best);
}
dr.NextResult();
while (dr.Read())
{
Sellers worst = new Sellers();
worst.category = dr[0].ToString().ToUpper();
worst.productname = dr[1].ToString().ToUpper();
worst.quantity = int.Parse(dr[2].ToString());
worst.revenue = decimal.Parse(dr[3].ToString());
worstSellersList.Add(worst);
}
dr.Close();
cn.Close();
}));
return 1;
}
catch (Exception ex)
{
cn.Close();
ErrorLogging.WriteToFile(ex.ToString());
return 0;
}
}
private async void Button3_Click(object sender, EventArgs e)
{
Task<int> task = new Task<int>(getSellers);
task.Start();
int result = await task;
if (result == 1)
{
if (bestsellers.Checked)
{
foreach (Sellers res in bestSellersList)
{
sellers.Rows.Add(res.category, res.productname, res.quantity + " " +res.unit, Settings.Default.currrencyCode + " " + res.revenue);
}
}
else
{
foreach (Sellers res in worstSellersList)
{
sellers.Rows.Add(res.category, res.productname, res.quantity + " " + res.unit, Settings.Default.currrencyCode + " " + res.revenue);
}
}
}
}
private async void Button4_Click(object sender, EventArgs e)
{
Task<int> task = new Task<int>(getSellers);
task.Start();
int result = await task;
if (result == 1)
{
if (bestsellers.Checked)
{
foreach (Sellers res in bestSellersList)
{
sellers.Rows.Add(res.category, res.productname, res.quantity + " " + res.unit, Settings.Default.currrencyCode + " " + res.revenue);
}
}
else
{
foreach (Sellers res in worstSellersList)
{
sellers.Rows.Add(res.category, res.productname, res.quantity + " " + res.unit, Settings.Default.currrencyCode + " " + res.revenue);
}
}
}
}
private async void Bestworst_Load(object sender, EventArgs e)
{
Task<int> task = new Task<int>(getSellers);
task.Start();
int result = await task;
if (result == 1)
{
if (bestsellers.Checked)
{
foreach (Sellers res in bestSellersList)
{
sellers.Rows.Add(res.category, res.productname, res.quantity + " " + res.unit, Settings.Default.currrencyCode + " " + res.revenue);
}
}
else
{
foreach (Sellers res in worstSellersList)
{
sellers.Rows.Add(res.category, res.productname, res.quantity + " " + res.unit, Settings.Default.currrencyCode + " " + res.revenue);
}
}
}
}
private void Button1_Click(object sender, EventArgs e)
{
string printerReportDefault = Settings.Default.ReportPrinter;
string printLocation;
ReportHeaders headers = new ReportHeaders();
headers.header1 = title.Text;
headers.header2 = dateSetting.Text;
List<ReportHeaders> reportHeaders = new List<ReportHeaders>();
reportHeaders.Add(headers);
if (bestsellers.Checked)
{
if (bestSellersList.Count > 0)
{
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\BestSeller.rdlc";
report.ReportPath = fullpath;
report.DataSources.Add(new ReportDataSource("Sellers", bestSellersList));
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\BestSeller.rdlc";
report.ReportPath = fullpath;
report.DataSources.Add(new ReportDataSource("Sellers", bestSellersList));
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();
}
}
else
{
if (worstSellersList.Count > 0)
{
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\BestSeller.rdlc";
report.ReportPath = fullpath;
report.DataSources.Add(new ReportDataSource("Sellers", worstSellersList));
report.DataSources.Add(new ReportDataSource("Header", reportHeaders));
ReportConfig.PrintToPrinter(report, printLocation, printerReportDefault);
}
else
{
this.Dispose();
}
}
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\BestSeller.rdlc";
report.ReportPath = fullpath;
report.DataSources.Add(new ReportDataSource("Sellers", worstSellersList));
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 int GenerateExcel(List<Sellers> 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 = "CATEGORY";
worksheet.Cell("B3").Value = "PRODUCT NAME";
worksheet.Cell("C3").Value = "QUANTITY";
worksheet.Cell("D3").Value = "TOTAL VALUE";
worksheet.Range("A1:D1").Row(1).Merge();
worksheet.Range("A2:D2").Row(1).Merge();
worksheet.Range("A1:D1").Style.Fill.BackgroundColor = XLColor.FromArgb(10,10,56);
worksheet.Range("A2:D2").Style.Fill.BackgroundColor = XLColor.FromArgb(10, 10, 56);
worksheet.Range("A3:D3").Style.Fill.BackgroundColor = XLColor.FromArgb(10, 10, 56);
worksheet.Range("A1:D1").Style.Font.FontColor = XLColor.White;
worksheet.Range("A2:D2").Style.Font.FontColor = XLColor.White;
worksheet.Range("A3:D3").Style.Font.FontColor = XLColor.White;
foreach (Sellers row in publish)
{
worksheet.Cell("A" + i).Style.Border.SetLeftBorder(XLBorderStyleValues.Thick);
worksheet.Cell("D" + i).Style.Border.SetRightBorder(XLBorderStyleValues.Thick);
worksheet.Cell("A" + i).Value = row.category;
worksheet.Cell("B" + i).Value = row.productname;
worksheet.Cell("C" + i).Value = row.quantity.ToString();
worksheet.Cell("D" + i).Value = Settings.Default.currrencyCode + " " + row.revenue;
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);
i++;
}
worksheet.Cell("A1").Style.Border.SetLeftBorder(XLBorderStyleValues.Thick);
worksheet.Cell("D1").Style.Border.SetRightBorder(XLBorderStyleValues.Thick);
worksheet.Cell("A2").Style.Border.SetLeftBorder(XLBorderStyleValues.Thick);
worksheet.Cell("D2").Style.Border.SetRightBorder(XLBorderStyleValues.Thick);
worksheet.Cell("A3").Style.Border.SetLeftBorder(XLBorderStyleValues.Thick);
worksheet.Cell("D3").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.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)
{
string filelocation = "";
DialogResult result = excelExport.ShowDialog();
if (result == DialogResult.OK)
{
filelocation = excelExport.FileName;
if (bestsellers.Checked)
{
if (bestSellersList.Count > 0)
{
Task<int> task = new Task<int>(() => GenerateExcel(bestSellersList, 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();
}
}
else
{
if (worstSellersList.Count > 0)
{
Task<int> task = new Task<int>(() => GenerateExcel(worstSellersList, filelocation));
EpE.Visible = true;
holding.Value = 0;
var progress = new Progress<int>(percent =>
{
holding.Value = percent;
});
await Task.Run(() => Progression.DoSomething(progress));
task.Start();
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();
}
}
}
}
}
}