using BiskLog_Point_Of_Sale.Classes; using BiskLog_Point_Of_Sale.Invoice; using BiskLog_Point_Of_Sale.Multiple_Login; using BiskLog_Point_Of_Sale.Properties; using Point_Of_Sale_Managment; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace BiskLog_Point_Of_Sale.POSDialogs { public partial class Initializing : Form { SqlConnection cn; SqlCommand cm; SqlDataReader dr; DatabaseConn conn = new DatabaseConn(); public List getInvoiceList; List getNewInvoice = new List(); List GetNews = new List(); List products; List distinct; public Initializing(List getInvoiceList) { InitializeComponent(); this.getInvoiceList = getInvoiceList; products = getInvoiceList.Select(p => p.productCode).ToList(); distinct = getInvoiceList.Select(p => p.distinctiveCode).ToList(); cn = new SqlConnection(conn.MyConnection()); } private int loadDetails() { try { string[] productArray = products.Select(i => i.ToString()).ToArray(); string[] distinctArray = distinct.Select(i => i.ToString()).ToArray(); cn.Open(); cm = new SqlCommand("Select TI.pcode,TI.quantity,TP.price from tblInventory TI Inner Join tblProduct TP On TP.pcode = TI.pcode " + "where TP.branchID = @branchID and TI.pcode in (@products) " + "Select TI.pcode,TI.quantity/PAU.[quantity/unit],PAU.[price/unit],PAU.distinctiveCode from tblInventory TI " + "Inner Join ProductAltUnit PAU On PAU.pcode = TI.pcode where PAU.branchID = @branchID and TI.pcode in " + "(Select pcode from ProductAltUnit where branchID = @branchID and distinctiveCode in (@distinctive))", cn); cm.Parameters.AddWithValue("@branchID", Settings.Default.BranchID); cm.AddParametersWithValues("@products", productArray); cm.AddParametersWithValues1("@distinctive", distinctArray); cm.ExecuteNonQuery(); dr = cm.ExecuteReader(); while (dr.Read()) { InvoicePOS pos = new InvoicePOS(productCode: dr[0].ToString(), quantity: int.Parse(dr[1].ToString()), totalprice: 0, distinctiveCode:dr[0].ToString(), unitprice: decimal.Parse(dr[2].ToString())); getNewInvoice.Add(pos); } dr.NextResult(); while (dr.Read()) { InvoicePOS pos = new InvoicePOS(productCode: dr[0].ToString(), quantity: int.Parse(dr[1].ToString()), totalprice: 0, distinctiveCode: dr[3].ToString(), unitprice: decimal.Parse(dr[2].ToString())); getNewInvoice.Add(pos); } dr.Close(); cn.Close(); return 1; } catch (Exception ex) { ErrorLogging.WriteToFile(ex.ToString()); cn.Close(); return 0; } } public void check() { foreach (InvoicePOS invoice in getNewInvoice) { string code = invoice.productCode; string distinctive = invoice.distinctiveCode; int qty = invoice.quantity; InvoicePOS oldies = getInvoiceList .First(p => p.distinctiveCode == distinctive); int oldquantity = oldies.quantity; decimal oldprice = oldies.unitprice; string una = oldies.unitName; if ((qty < oldquantity) || (invoice.unitprice != oldprice)) { OldAndNew old = new OldAndNew(); old.productCode = code; old.productname = oldies.productname; old.old_quantity = oldquantity; old.old_price = oldprice; old.new_price = invoice.unitprice; old.new_quantity = invoice.quantity; old.unitCode = oldies.unitCode; old.unitName = oldies.unitName; old.distinctiveCode = oldies.distinctiveCode; GetNews.Add(old); } } } private async void Initializing_Load(object sender, EventArgs e) { Task task = new Task(loadDetails); holding.Value = 0; var progress = new Progress(percent => { holding.Value = percent; }); await Task.Run(() => Progression.DoSomething(progress)); task.Start(); int result = await task; if (result == 1) { try { check(); } catch { } if (GetNews.Count != 0) { InvoiceRecheck invoiceRecheck = new InvoiceRecheck(GetNews); invoiceRecheck.BringToFront(); invoiceRecheck.ShowDialog(); if (invoiceRecheck.DialogResult == DialogResult.OK) { getInvoiceList = invoiceRecheck.vetted; this.DialogResult = DialogResult.OK; this.Close(); } else { this.DialogResult = DialogResult.Abort; this.Close(); } } else { this.DialogResult = DialogResult.OK; this.Close(); } } else { string title = "Error Occurred"; string message = "Sorry an unexpected error occurred while getting invoice ready for sale"; NoAction noAction = new NoAction(title, message); noAction.BringToFront(); noAction.ShowDialog(); this.DialogResult = DialogResult.Cancel; this.Close(); } holding.Visible = false; } } }