public override void save_button_Click(object sender, EventArgs e) { if (purchase_dataGridView.Rows.Count == 0 || purchase_dataGridView.Rows.Count < 0) { MainClass.ShowMsg("Please enter all required information.", "error"); MainClass.disbale_reset(left_panel_sample2); enable_crude_buttons(); } else { MainClass.disbale_reset(left_panel_sample2); enable_crude_buttons(); try { using (TransactionScope sc = new TransactionScope()) { int compare, compareTO; here: for (int i = 0; i < purchase_dataGridView.Rows.Count; ++i) { compare = Convert.ToInt32(purchase_dataGridView.Rows[i].Cells["productIDGV"].Value); for (int j = i + 1; j < purchase_dataGridView.Rows.Count; ++j) { compareTO = Convert.ToInt32(purchase_dataGridView.Rows[j].Cells["productIDGV"].Value); if (compare == compareTO) { if (correctIT == false) { DialogResult dr = MessageBox.Show("You added a duplicate product which is not allowed.\n\nDo you want to remove that duplicate product and add its quantity?", "Duplicate product(s) encountered!", MessageBoxButtons.YesNo, MessageBoxIcon.Error); if (DialogResult.Yes == dr) { correctIT = true; goto here; } else { correctIT = false; throw new Exception("Duplicate products are not allowed!"); } } else { Int32 originalproductTOTAL, originalproductVALUE, dupliactedproductVALUE, dupliactedproductTOTAL; originalproductVALUE = Convert.ToInt32(purchase_dataGridView.Rows[i].Cells["productquantityGV"].Value); dupliactedproductVALUE = Convert.ToInt32(purchase_dataGridView.Rows[j].Cells["productquantityGV"].Value); originalproductTOTAL = Convert.ToInt32(purchase_dataGridView.Rows[i].Cells["totalGV"].Value); dupliactedproductTOTAL = Convert.ToInt32(purchase_dataGridView.Rows[j].Cells["totalGV"].Value); originalproductVALUE += dupliactedproductVALUE; originalproductTOTAL += dupliactedproductTOTAL; purchase_dataGridView.Rows[i].Cells["productquantityGV"].Value = originalproductVALUE; purchase_dataGridView.Rows[i].Cells["totalGV"].Value = originalproductTOTAL; int beforeREMOVE = purchase_dataGridView.Rows.Count; int afterREMOVE; purchase_dataGridView.Rows.Remove(purchase_dataGridView.Rows[j]); afterREMOVE = purchase_dataGridView.Rows.Count; if (afterREMOVE < beforeREMOVE) { MainClass.ShowMsg("Operation successfull.", "Success"); } else { throw new Exception("An error occured during removing duplicated products!"); } } } } } SQL_TASKS ss = new SQL_TASKS(); foreach (DataGridViewRow row in purchase_dataGridView.Rows) { //inserting purchase invoice purchaseinvoiceID_FROM_SQL_TASKS = ss.InsertPurchaseinvoice(DateTime.Today, LoginCodeClass.USERID, Convert.ToInt32(row.Cells["supplierIDGV"].Value)); //inserting purchase invoice details count += ss.insertPurchaseinvoicedetails(purchaseinvoiceID_FROM_SQL_TASKS, Convert.ToInt32(row.Cells["productIDGV"].Value.ToString()), Convert.ToInt32(row.Cells["productquantityGV"].Value.ToString()), Convert.ToSingle(row.Cells["totalGV"].Value.ToString())); SqlConnection sql_con = new SqlConnection(MainClass.connection()); SqlCommand cmd = new SqlCommand(); cmd.Connection = sql_con; cmd.CommandType = CommandType.Text; cmd.CommandText = "select pp_prodID from ProductPrice where pp_ProductBarcode='" + row.Cells["productbarcodeGV"].Value.ToString() + "' "; sql_con.Open(); object check = cmd.ExecuteScalar(); sql_con.Close(); if (check == null) //product not present { Hashtable ht1 = new Hashtable(); ht1.Add("@prodID", row.Cells["productIDGV"].Value.ToString()); ht1.Add("@bp", Convert.ToSingle(row.Cells["totalGV"].Value.ToString())); ht1.Add("@sp", DBNull.Value); ht1.Add("@buyingdate", DateTime.Today); ht1.Add("@profitper", DBNull.Value); ht1.Add("@discount", DBNull.Value); ht1.Add("@productbarcode", row.Cells["productbarcodeGV"].Value.ToString()); // inserting product price: if (SQL_TASKS.insert_update_delete("st_insertPRODUCTPRICE", ht1) > 0) { success = true; } else { success = false; } } else //product present { Hashtable ht2 = new Hashtable(); ht2.Add("@prodID", row.Cells["productIDGV"].Value.ToString()); cmd.CommandText = "select pp_buyingPrice from ProductPrice where pp_prodID='" + row.Cells["productIDGV"].Value.ToString() + "' "; sql_con.Open(); Int64 ExistingProductBuyingPrice = Convert.ToInt64(cmd.ExecuteScalar()); sql_con.Close(); Int64 finalp = Convert.ToInt64(row.Cells["totalGV"].Value) + ExistingProductBuyingPrice; Hashtable htx = new Hashtable(); htx.Add("@prodID", row.Cells["productIDGV"].Value.ToString()); htx.Add("@bp", finalp); if (SQL_TASKS.insert_update_delete("st_updatePRODUCTPRICEforPURCHASEINVOICE", htx) > 0) { success = true; } else { success = false; } } int q; object stockcount; bool DoesExist = SQL_TASKS.IfProductExist(Convert.ToInt64(row.Cells["productIDGV"].Value.ToString())); if (DoesExist) //if product does exist { //update stock stockcount = SQL_TASKS.getProductQuantityfromSTOCK(Convert.ToInt32(row.Cells["productIDGV"].Value.ToString())); q = Convert.ToInt32(stockcount); q += Convert.ToInt32(row.Cells["productquantityGV"].Value.ToString()); Hashtable hta = new Hashtable(); hta.Add("@prodID", Convert.ToInt32(row.Cells["productIDGV"].Value.ToString())); hta.Add("@quan", q); //updating stock: if (SQL_TASKS.insert_update_delete("st_updateSTOCK", hta) > 0) { success = true; } } else //if product does not exist { //insert Hashtable htb = new Hashtable(); htb.Add("@prodID", Convert.ToInt32(row.Cells["productIDGV"].Value.ToString())); htb.Add("@quan", Convert.ToInt32(row.Cells["productquantityGV"].Value.ToString())); // inserting stock: if (SQL_TASKS.insert_update_delete("st_insertSTOCK", htb) > 0) { success = true; } else { success = false; } } } if (count > 0 && success == true) { MainClass.ShowMsg("Purchase Invoice created successfully.", "Success"); MainClass.disbale_reset(left_panel_sample2); enable_crude_buttons(); purchase_dataGridView.Rows.Clear(); gross_total_price_label.Text = ""; } else { MainClass.ShowMsg("Unable to create Purchase Invoice", "Error"); MainClass.disbale_reset(left_panel_sample2); enable_crude_buttons(); purchase_dataGridView.Rows.Clear(); gross_total_price_label.Text = ""; } sc.Complete(); } } catch (Exception ex) { MainClass.ShowMsg(ex.Message, "Error"); MainClass.disbale_reset(left_panel_sample2); enable_crude_buttons(); } } }
//private void barcode_textBox_Validating(object sender, System.ComponentModel.CancelEventArgs e) //{ // try // { // if (barcode_textBox.Text != "") // { // if (sales_return_dataGridView.Rows.Count > 0) // { // using (TransactionScope sc = new TransactionScope()) // { // foreach (DataGridViewRow row in sales_return_dataGridView.Rows) // { // if (barcode_textBox.Text == row.Cells["BarcodeGV"].Value.ToString()) // { // DialogResult dr = MessageBox.Show("Are you sure you want to return " + row.Cells["ProductGV"].Value.ToString() + "?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question); // if (dr == DialogResult.Yes) // { // //----------------------INSERTING RECORD TO REFUND TABLE------------------ // Hashtable htx = new Hashtable(); // htx.Add("@refSaleID", Convert.ToInt64(row.Cells["ProductIDGV"].Value.ToString())); // htx.Add("@refDate", DateTime.Now); // htx.Add("@refDoneBy", Convert.ToInt64(row.Cells["UserIDGV"].Value.ToString())); // htx.Add("@refProdID", Convert.ToInt64(row.Cells["ProductIDGV"].Value.ToString())); // htx.Add("@refQuantity", Convert.ToInt64(row.Cells["QuantityGV"].Value.ToString())); // htx.Add("@refAmount", Convert.ToSingle(row.Cells["ProductPriceGV"].Value.ToString())); // int x = 0; // x += SQL_TASKS.insert_update_delete("st_insertRefundReturn", htx); // if (x > 0) // { // MainClass.ShowMsg("Return and Refund Successfull", "Success"); // x = 0; // ht.Clear(); // } // else // { // throw new Exception("Something went wrong"); // } // //----------------------UPDATING STOCK------------------ // int currentQuantity = (int)SQL_TASKS.getProductQuantity(Convert.ToInt32((row.Cells["ProductIDGV"].Value.ToString()))); // int finalQuantity = currentQuantity - Convert.ToInt32(row.Cells["QuantityGV"].Value.ToString()); // Hashtable htinsetrStock = new Hashtable(); // htinsetrStock.Add("@prodID", Convert.ToInt64(row.Cells["ProductIDGV"].Value.ToString())); // htinsetrStock.Add("@quan", finalQuantity); // SQL_TASKS.insert_update_delete("st_updateSTOCK", htinsetrStock); // Int64 productID = Convert.ToInt64(row.Cells["ProductIDGV"].Value.ToString()); // float productPrice = Convert.ToSingle(row.Cells["ProductPriceGV"].Value.ToString()); // int product_quantity = Convert.ToInt32(row.Cells["QuantityGV"].Value.ToString()); // amount_refund += productPrice; // amount_to_refund_textBox.Text = Math.Round(amount_refund, 0).ToString(); // if (product_quantity == 0) // { // if (ht.ContainsKey(row.Cells["ProductIDGV"].Value)) // { // Int64 prodIDht = Convert.ToInt64(row.Cells["ProductIDGV"].Value.ToString()); // ht[prodIDht] = Convert.ToInt32(ht[prodIDht]) - 1; // } // else // { // ht.Add(row.Cells["ProductIDGV"].Value, 1); // } // sales_return_dataGridView.Rows.Remove(row); // } // else // { // row.Cells["QuantityGV"].Value = product_quantity; // row.Cells["PerProductTotalGV"].Value = Convert.ToSingle(row.Cells["PerProductTotalGV"].Value.ToString()); // if (ht.ContainsKey(row.Cells["ProductIDGV"].Value)) // { // Int64 prodIDht = Convert.ToInt64(row.Cells["ProductIDGV"].Value.ToString()); // ht[prodIDht] = Convert.ToInt32(ht[prodIDht]) + 1; // } // else // { // ht.Add(row.Cells["ProductIDGV"].Value, 1); // } // sales_return_dataGridView.Rows.Remove(row); // } // } // } // break; // } // sc.Complete(); // } // } // } // } // catch (Exception ex) // { // MainClass.ShowMsg(ex.Message, "Error"); // } //} private void return_button_Click(object sender, EventArgs e) { try { bool found = false; if (barcode_textBox.Text != "") { if (sales_return_dataGridView.Rows.Count > 0) { using (TransactionScope sc = new TransactionScope()) { foreach (DataGridViewRow row in sales_return_dataGridView.Rows) { if (barcode_textBox.Text == row.Cells["BarcodeGV"].Value.ToString()) { DialogResult dr = MessageBox.Show("Are you sure you want to return " + row.Cells["ProductGV"].Value.ToString() + "?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dr == DialogResult.Yes) { //----------------------INSERTING RECORD TO REFUND TABLE------------------ Hashtable htx = new Hashtable(); htx.Add("@refSaleID", Convert.ToInt64(row.Cells["saleIDGV"].Value.ToString())); htx.Add("@refDate", DateTime.Now); htx.Add("@refDoneBy", Convert.ToInt64(row.Cells["UserIDGV"].Value.ToString())); htx.Add("@refProdID", Convert.ToInt64(row.Cells["ProductIDGV"].Value.ToString())); htx.Add("@refQuantity", Convert.ToInt64(row.Cells["QuantityGV"].Value.ToString())); htx.Add("@refAmount", Convert.ToSingle(row.Cells["ProductPriceGV"].Value.ToString())); int x = 0; x += SQL_TASKS.insert_update_delete("st_insertRefundReturn", htx); if (x > 0) { MainClass.ShowMsg("Return and Refund Successfull", "Success"); x = 0; ht.Clear(); } else { throw new Exception("Something went wrong"); } //----------------------UPDATING STOCK------------------ int currentQuantity = (int)SQL_TASKS.getProductQuantityfromSTOCK(Convert.ToInt64((row.Cells["ProductIDGV"].Value.ToString()))); int finalQuantity = currentQuantity - Convert.ToInt32(row.Cells["QuantityGV"].Value.ToString()); Hashtable htinsertStock = new Hashtable(); htinsertStock.Add("@prodID", row.Cells["ProductIDGV"].Value.ToString()); htinsertStock.Add("@quan", finalQuantity); SQL_TASKS.insert_update_delete("st_updateSTOCK", htinsertStock); Int64 productID = Convert.ToInt64(row.Cells["ProductIDGV"].Value.ToString()); float productPrice = Convert.ToSingle(row.Cells["ProductPriceGV"].Value.ToString()); int product_quantity = Convert.ToInt32(row.Cells["QuantityGV"].Value.ToString()); amount_refund += productPrice; amount_to_refund_textBox.Text = Math.Round(amount_refund, 0).ToString(); if (product_quantity == 0) { if (ht.ContainsKey(row.Cells["ProductIDGV"].Value)) { Int64 prodIDht = Convert.ToInt64(row.Cells["ProductIDGV"].Value.ToString()); ht[prodIDht] = Convert.ToInt32(ht[prodIDht]) - 1; } else { ht.Add(row.Cells["ProductIDGV"].Value, 1); } sales_return_dataGridView.Rows.Remove(row); SalesReturnReceipt srr = new SalesReturnReceipt(); srr.ShowDialog(); } else { row.Cells["QuantityGV"].Value = product_quantity; row.Cells["PerProductTotalGV"].Value = Convert.ToSingle(row.Cells["PerProductTotalGV"].Value.ToString()); if (ht.ContainsKey(row.Cells["ProductIDGV"].Value)) { Int64 prodIDht = Convert.ToInt64(row.Cells["ProductIDGV"].Value.ToString()); ht[prodIDht] = Convert.ToInt32(ht[prodIDht]) + 1; } else { ht.Add(row.Cells["ProductIDGV"].Value, 1); } sales_return_dataGridView.Rows.Remove(row); SalesReturnReceipt srr = new SalesReturnReceipt(); srr.ShowDialog(); } } found = true; } if (found) { break; } } sc.Complete(); } } } } catch (Exception ex) { MainClass.ShowMsg(ex.Message, "Error"); } }