private void purchaseReportToolStripMenuItem_Click_1(object sender, EventArgs e)
 {
     try
     {
         int purchaseid = 0;
         int.TryParse(dgvAllPurchase.CurrentRow.Cells[0].Value.ToString(), out purchaseid);
         frmPurchaseReport frm = new frmPurchaseReport(Convert.ToInt32(purchaseid));
         frm.ShowDialog();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
        private void btnPurchaseConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                ep.Clear();
                if (string.IsNullOrEmpty(SupplierID))
                {
                    ep.SetError(txtSearch, "Please Search Supplier");
                    txtSearch.Focus();
                    return;
                }

                int supplierid = 0;
                int.TryParse(SupplierID, out supplierid);
                if (supplierid == 0)
                {
                    ep.SetError(txtSearch, "Please Search Supplier");
                    txtSearch.Focus();
                    return;
                }

                if (lblSupplierName.Text.Trim().Length == 0)
                {
                    ep.SetError(lblSupplierName, "Please Search Supplier");
                    lblSupplierName.Focus();
                    return;
                }

                if (dgvPurchaseCart == null)
                {
                    ep.SetError(btnADD, "Please Purchase Some Products");
                    cmbProduct.Focus();
                    return;
                }

                if (dgvPurchaseCart.Rows.Count < 1)
                {
                    ep.SetError(btnADD, "Please Purchase Some Products");
                    cmbProduct.Focus();
                    return;
                }

                if (dgvPurchaseCart.Rows.Count > 0)
                {
                    CalculateTotalAmount();
                    float totalamount = 0;
                    float.TryParse(lblTotalCost.Text.Trim(), out totalamount);
                    if (totalamount == 0)
                    {
                        ep.SetError(btnADD, "Please Purchase Some Products");
                        cmbProduct.Focus();
                        return;
                    }
                    string purchaseheaderquery  = string.Format(@"insert into PurchaseTable (EmployeeID, SupplierID, PurchaseDate, TotalAmount, Comments) values ('{0}', '{1}', '{2}', '{3}', '{4}')", UserInfo.EmployeeID, supplierid, DateTime.Now.ToString("yyyy/MM/dd"), totalamount, txtComments.Text.Trim());
                    bool   purchaseheaderresult = DatabaseAccess.Insert(purchaseheaderquery);
                    string purchaseid           = string.Empty;
                    if (purchaseheaderresult == true)
                    {
                        purchaseid = DatabaseAccess.Retrive("select max(purchaseID) from PurchaseTable").Rows[0][0].ToString();
                        foreach (DataGridViewRow product in dgvPurchaseCart.Rows)
                        {
                            string productquery = string.Format(@"insert into purchaseDetailTable (purchaseID, itemID, Qty, unitPrice) values ('{0}', '{1}', '{2}', '{3}')", purchaseid, Convert.ToString(product.Cells[0].Value), Convert.ToString(product.Cells[4].Value), Convert.ToString(product.Cells[5].Value));
                            try
                            {
                                bool purchasedetailresult = DatabaseAccess.Insert(productquery);
                                if (purchasedetailresult == false)
                                {
                                    DeletePurchase(purchaseid);
                                    MessageBox.Show("Un-Expected Issue is Occur Please Try-Again!!!", title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                    return;
                                }

                                string stockupdatequery = string.Format(@"update StockTable set currentPurchasePrice = '{0}', Qty = Qty + '{1}', SaleUnitPrice = '{2}' where itemID = '{3}' ", Convert.ToString(product.Cells[5].Value), Convert.ToString(product.Cells[4].Value), Convert.ToString(product.Cells[6].Value), Convert.ToString(product.Cells[0].Value));
                                DatabaseAccess.Update(stockupdatequery);
                            }
                            catch (Exception ex)
                            {
                                DeletePurchase(purchaseid);
                                MessageBox.Show("Un-Expected Issue is Occur Please Try-Again!!!", title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                return;
                            }
                        }

                        string message = "Purchase is Confirmed...";
                        if (chkIsPaymentSucceed.Checked == true)
                        {
                            string paymentquery = string.Format(@"insert into SupplierPaymentTable (PurchaseID, EmployeeID, PaymentDate, TotalAmount, PaidAmount, BalanceAmount) values ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}')", purchaseid, UserInfo.EmployeeID, DateTime.Now.ToString("yyyy/MM/dd"), totalamount, totalamount, "0");
                            bool   result       = DatabaseAccess.Insert(paymentquery);
                            if (result == true)
                            {
                                message = message + " with Payment!";
                            }
                        }

                        frmPurchaseReport frm = new frmPurchaseReport(Convert.ToInt32(purchaseid));
                        frm.ShowDialog();
                        ResetForm();
                        CalculateTotalAmount();
                        txtComments.Text  = "";
                        lblTotalCost.Text = "";
                        MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Please Provide Purchase Correct Details/ Re-Login!", title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
                else
                {
                    ep.SetError(btnADD, "Please Purchase Some Products");
                    cmbProduct.Focus();
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }