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.

365 lines
17 KiB

using BiskLog_Point_Of_Sale.Classes;
using BiskLog_Point_Of_Sale.Multiple_Login;
using BiskLog_Point_Of_Sale.Properties;
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.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BiskLog_Point_Of_Sale.Delivery
{
public partial class DeliveryManagement : Form
{
SqlConnection cn;
SqlCommand cm;
SqlDataReader dr;
DatabaseConn conn = new DatabaseConn();
string SelecteddeliveryID, status, fullname, email;
int rowSelectedIndex;
DeliveryMain main;
public DeliveryManagement(DeliveryMain main)
{
InitializeComponent();
cn = new SqlConnection(conn.MyConnection());
this.main = main;
holding.Left = (ClientSize.Width - holding.Width) / 2;
}
private void AddProducts_Click(object sender, EventArgs e)
{
NewDelivery newDelivery = new NewDelivery();
newDelivery.BringToFront();
newDelivery.ShowDialog();
}
public int LoadPreviousOrders()
{
try
{
deliveries.Invoke(new Action(() =>
{
int i = 1;
deliveries.Rows.Clear();
cn.Open();
cm = new SqlCommand("Select DH.deliveryID,DH.dateInitiated,DH.destination,DH.dateCompleted,DH.customerID,DH.generatedBy,DH.totalCost,DH.status,DR.fullname,DR.email from " +
"tblDeliveryHead DH Inner Join tblDeliveryRecipients DR on DR.deliveryID = DH.deliveryID where DH.branchID = @branchID order by DH.dateInitiated desc", cn);
cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID);
cm.ExecuteNonQuery();
dr = cm.ExecuteReader();
while (dr.Read())
{
if (!String.IsNullOrEmpty(dr[3].ToString()))
{
deliveries.Rows.Add(i, dr[0].ToString().ToUpper(), Convert.ToDateTime(dr[1].ToString()).ToLongDateString(), dr[2].ToString(),
Convert.ToDateTime(dr[3].ToString()).ToLongDateString(), dr[4].ToString(), dr[5].ToString(), Settings.Default.currrencyCode + " " + dr[6].ToString(),
dr[7].ToString(), dr[8].ToString().ToUpper(), dr[9].ToString());
}
else
{
deliveries.Rows.Add(i, dr[0].ToString().ToUpper(), Convert.ToDateTime(dr[1].ToString()).ToLongDateString(), dr[2].ToString(),
"-", dr[4].ToString(), dr[5].ToString(), Settings.Default.currrencyCode + " " + dr[6].ToString(), dr[7].ToString()
, dr[8].ToString().ToUpper(), dr[9].ToString());
}
i++;
}
cn.Close();
deliveries.ClearSelection();
}));
return 1;
}
catch
{
cn.Close();
return 0;
}
}
private async void DeliveryManagement_Load(object sender, EventArgs e)
{
hold.Visible = true;
Task<int> task = new Task<int>(LoadPreviousOrders);
task.Start();
await task;
hold.Visible = false;
}
private void Deliveries_CellClick(object sender, DataGridViewCellEventArgs e)
{
//if (e.RowIndex != -1)
//{
// string deliveryIdentification = deliveries.Rows[e.RowIndex].Cells[1].Value.ToString();
// this.Invoke(new Action(() =>
// {
// DeliveryDetails delivery = new DeliveryDetails(deliveryIdentification);
// delivery.BringToFront();
// delivery.ShowDialog();
// }));
//}
}
private async void REMAILORDERINVOICEToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(SelecteddeliveryID))
{
hold.Visible = true;
string file = Environment.CurrentDirectory + @"\Order Invoice\" + SelecteddeliveryID + ".pdf";
if (File.Exists(file))
{
if (!String.IsNullOrEmpty(email))
{
if (IsValidEmail(email))
{
Task<int> taskO = new Task<int>(sendInvoice);
taskO.Start();
int youin = await taskO;
if (youin == 1)
{
string titled = "Invoice Mailed";
string messaged = "Order invoice has been successfully mailed to the customer";
NoAction noActiond = new NoAction(titled, messaged);
noActiond.BringToFront();
noActiond.ShowDialog();
}
else
{
string titles = "Invoice not mailed";
string messages = "An error occurred while trying to mail invoice to customer , please make sure you have an active internet " +
"connection and try again !!!";
NoAction noActions = new NoAction(titles, messages);
noActions.BringToFront();
noActions.ShowDialog();
}
}
else
{
string title = "Invalid Email";
string message = "An error occurred while sending order invoice due to an invalid email address entered," +
" please correct the email and try again";
NoAction noAction = new NoAction(title, message);
noAction.BringToFront();
noAction.ShowDialog();
}
}
}
else
{
Task<int> task = new Task<int>(() =>
{
RegenerateInvoice.generateFile(SelecteddeliveryID);
return 1;
});
task.Start();
await task;
Task<int> taskO = new Task<int>(sendInvoice);
taskO.Start();
int youin = await taskO;
if (youin == 1)
{
string titled = "Invoice Mailed";
string messaged = "Order invoice has been successfully mailed to the customer";
NoAction noActiond = new NoAction(titled, messaged);
noActiond.BringToFront();
noActiond.ShowDialog();
}
else
{
string titles = "Invoice not mailed";
string messages = "An error occurred while trying to mail invoice to customer, please try again";
NoAction noActions = new NoAction(titles, messages);
noActions.BringToFront();
noActions.ShowDialog();
}
}
hold.Visible = false;
}
}
bool IsValidEmail(string email)
{
try
{
var addr = new MailAddress(email);
return addr.Address == email;
}
catch
{
return false;
}
}
private void Deliveries_MouseClick(object sender, MouseEventArgs e)
{
SelecteddeliveryID = "";
try
{
if (e.Button == MouseButtons.Right)
{
deliveries.ClearSelection();
DeliveryOptions.Show(deliveries, new Point(e.X, e.Y));
DataGridView.HitTestInfo hitTestInfo = deliveries.HitTest(e.X, e.Y);
deliveries.Rows[hitTestInfo.RowIndex].Selected = true;
SelecteddeliveryID = deliveries.Rows[hitTestInfo.RowIndex].Cells[1].Value.ToString();
status = deliveries.Rows[hitTestInfo.RowIndex].Cells[8].Value.ToString();
rowSelectedIndex = hitTestInfo.RowIndex;
fullname = deliveries.Rows[hitTestInfo.RowIndex].Cells[9].Value.ToString();
email = deliveries.Rows[hitTestInfo.RowIndex].Cells[10].Value.ToString();
}
}
catch (Exception ex)
{
ErrorLogging.WriteToFile(ex.ToString());
}
}
private async void PRINTORDERINVOICEToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(SelecteddeliveryID))
{
string printerReportDefault = Settings.Default.ReportPrinter;
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))
{
string printLocation = folder.SelectedPath + @"\" + SelecteddeliveryID + ".pdf";
holding.Visible = true;
Task<int> task = new Task<int>(() => generatePrint(printLocation));
task.Start();
await task;
holding.Visible = false;
}
}
else
{
holding.Visible = true;
Task<int> task = new Task<int>(() => generatePrint());
task.Start();
await task;
holding.Visible = false;
}
}
}
private void ASSIGNTRUCKToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(SelecteddeliveryID))
{
main.Loadingbay(SelecteddeliveryID);
}
}
private void ORDERREVIEWToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(SelecteddeliveryID))
{
this.Invoke(new Action(() =>
{
DeliveryDetails delivery = new DeliveryDetails(SelecteddeliveryID, status);
delivery.BringToFront();
delivery.ShowDialog();
if (delivery.delivered)
{
deliveries.Rows[rowSelectedIndex].Cells[8].Value = "DELIVERED";
}
else if (delivery.cancelled)
{
deliveries.Rows[rowSelectedIndex].Cells[8].Value = "CANCELLED";
}
}));
}
}
public int sendInvoice()
{
try
{
MailMessage mail = new MailMessage();
string client = Settings.Default.smtpServer;
string clientPort = Settings.Default.smtpPort;
string username = Settings.Default.emailAddress;
string password = passwordEncryption.Decrypt(Settings.Default.password);
SmtpClient smtpClient = new SmtpClient(client);
mail.From = new MailAddress(username);
mail.To.Add(email);
mail.Subject = "Order invoice";
mail.Body = "Hello " + fullname + " your order invoice has been attached to this mail";
string file = Environment.CurrentDirectory + @"\Order Invoice\" + SelecteddeliveryID + ".pdf";
Attachment attachment = new Attachment(file);
mail.Attachments.Add(attachment);
smtpClient.Port = int.Parse(clientPort);
smtpClient.Credentials = new System.Net.NetworkCredential(username, password);
smtpClient.EnableSsl = true;
smtpClient.Send(mail);
return 1;
}
catch
{
return 0;
}
}
public int generatePrint(string printLocation = null)
{
try
{
cn.Open();
List<OrderInvoice> deliveryInvoice = new List<OrderInvoice>();
List<products> productsList = new List<products>();
string fullname, address, telephone, email, charges, itemTotal, fromDate, toDate, allTotal;
OrderInvoice order = null;
cm = new SqlCommand("Declare @totalProduct decimal(19,2) = (Select Sum(cost) from tblDeliveryDetails where deliveryID = @deliveryID) " +
"Declare @Addition decimal(19, 2) = case when(Select totalCost from tblDeliveryHead where deliveryID = @deliveryID) - @totalProduct <= 0 then 0 " +
"else (Select totalCost from tblDeliveryHead where deliveryID = @deliveryID) - @totalProduct end " +
"Select DR.fullname,DR.address,DR.telephone,DR.email,DR.fromDate,DR.toDate,DH.totalCost,DP.product_name,DD.quantity,DD.cost,@totalProduct,@Addition,DD.pcode,u.unitname from " +
"tblDeliveryDetails DD Inner Join tblDeliveryRecipients DR on DR.deliveryID = DD.deliveryID Inner Join tblDeliveryHead DH On DH.deliveryID = DD.deliveryID Inner Join " +
"tblProduct DP On DP.pcode = DD.pcode Inner Join UnitOfMeasure u On u.unitCode = DD.unit where DD.deliveryID = @deliveryID and DH.branchID = @branchID", cn);
cm.Parameters.AddWithValue("@deliveryID", SelecteddeliveryID);
cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID);
cm.ExecuteNonQuery();
dr = cm.ExecuteReader();
while (dr.Read())
{
fullname = dr[0].ToString().ToUpper();
address = dr[1].ToString().ToUpper();
telephone = dr[2].ToString();
email = dr[3].ToString();
fromDate = dr[4].ToString();
toDate = dr[5].ToString();
charges = Settings.Default.currrencyCode + " " + dr[11].ToString();
itemTotal = Settings.Default.currrencyCode + " " + dr[10].ToString();
allTotal = Settings.Default.currrencyCode + " " + dr[6].ToString();
products p = new products(dr[7].ToString().ToUpper(), dr[8].ToString(), Settings.Default.currrencyCode + " " + dr[9].ToString(),dr[12].ToString());
productsList.Add(p);
order = new OrderInvoice(
orderDate: DateTime.Now.ToString("yyyy-MM-dd"), beginDate: fromDate, endDate: toDate,
ProductsCost: itemTotal, additional: charges, All: allTotal, address: address, email: email,
telephone: telephone, branchtelephone: Settings.Default.BranchTelephone, branchLocation: Settings.Default.BranchCity, branchName: Settings.Default.BranchName,
orderid: SelecteddeliveryID, companyName: Settings.Default.CompanyName, customer: fullname
);
}
dr.Close();
cn.Close();
deliveryInvoice.Add(order);
LocalReport report = new LocalReport();
string path = Path.GetDirectoryName(Application.ExecutablePath);
string fullpath = Path.GetDirectoryName(Application.ExecutablePath).Remove(path.Length - 10) +
@"\Delivery\OrderInvoice.rdlc";
report.ReportPath = fullpath;
report.DataSources.Add(new ReportDataSource("Details", deliveryInvoice));
report.DataSources.Add(new ReportDataSource("products", productsList));
OrderInvoiceConfig.PrintToPrinter(report, printLocation, Settings.Default.ReportPrinter);
return 1;
}
catch
{
cn.Close();
return 0;
}
}
}
}