public static void DeleteItemBeli(SqlConnection conn, int idx) { SqlCommand com = new SqlCommand(QueryString.DeleteItemBeli, conn); List <ItemBeli> list = f.ItemBeliOLV.Objects.Cast <ItemBeli>().ToList(); ItemBeli item = list[idx]; com.Parameters.Add("@itemid", Int).Value = item.Id; try { if (conn.State != ConnectionState.Open) { conn.Open(); } int rowsAffected = com.ExecuteNonQuery(); if (rowsAffected != 1) { Console.WriteLine("Delete ItemBeli failed"); // throw Exception ? } } catch (Exception e) { Console.WriteLine("Error :" + e.Message); } }
// tot = old total public static void UpdateItemBeli(SqlConnection conn, int idx) { var price = Utilities.GetInt(f.ItemBeliHargaBeliTextBox.Text); var quantity = Utilities.GetInt(f.ItemBeliQtyTextBox.Text); SqlTransaction transaction = conn.BeginTransaction(IsolationLevel.Serializable); List <ItemBeli> list = f.ItemBeliOLV.Objects.Cast <ItemBeli>().ToList(); ItemBeli item = list[idx]; int id = item.StockId; int p = 0; int q = 0; int old = 0; int oPrice = 0; int stockQ = 0; SqlConnectionStringBuilder connectionToLocal = new SqlConnectionStringBuilder(); connectionToLocal.ConnectionString = "Data Source=localhost;Initial Catalog=Vera;"; connectionToLocal.UserID = "sa"; connectionToLocal.Password = "******"; using (var connection = new SqlConnection(connectionToLocal.ConnectionString)) { connection.Open(); SqlCommand command = new SqlCommand(QueryString.GetItemBeliTotal, connection); command.Parameters.Add("@id", Int).Value = item.Id; // NEED ID LABEL HERE var rec = command.ExecuteReader(); if (rec.Read()) { p = rec.GetInt32(0); q = rec.GetInt32(1); old = p * q; } rec.Close(); oPrice = Stocks.GetStockPrice(connection, id); stockQ = Stocks.GetStockQuantity(connection, id); } try { SqlCommand com = new SqlCommand(QueryString.UpdateItemBeli, conn); com.Transaction = transaction; com.Parameters.Add("@price", Int).Value = price; com.Parameters.Add("@quantity", Int).Value = quantity; com.Parameters.Add("@id", Int).Value = item.Id; if (conn.State != ConnectionState.Open) { conn.Open(); } int rowsAffected = com.ExecuteNonQuery(); com.CommandText = QueryString.UpdateStockQty; Utilities.print(oPrice + " " + stockQ + " " + old + " " + price + " " + quantity + " " + p + " " + q); com.Parameters.Add("@stockprice", BigInt).Value = ((oPrice * stockQ) - old + (price * quantity)) / (stockQ - q + quantity); com.Parameters.Add("@qty", Int).Value = stockQ - q + quantity; com.Parameters.Add("@stockid", Int).Value = id; rowsAffected = com.ExecuteNonQuery(); if (rowsAffected != 1) { Utilities.print("Update ItemIndex({0}) failed", idx); // throw Exception ? try { transaction.Rollback(); } catch (SqlException ex) { if (transaction.Connection != null) { Console.WriteLine("An exception of type " + ex.GetType() + " was encountered while attempting to roll back the transaction."); } } return; } transaction.Commit(); item.Price = price; item.Quantity = quantity; list[idx] = item; // update total faktur var total = price * quantity; var ppn = f.FakturBeliPPNCheckBox.Checked; double oldT = Utilities.GetInt(f.FakturBeliTotalBeliTextBox.Text, true); double oldTax = Utilities.GetInt(f.FakturBeliTotalPPNTextBox.Text, true); double oldTotal = Utilities.GetInt(f.FakturBeliTotalAkhirTextBox.Text, true); oldT = ppn ? (oldT - old) / 1.1 : oldT - old; // - oldItemTotal oldTax = ppn ? oldTax - old * .1: 0; // - oldItemPPN oldTotal = oldTotal - old; // - oldItemTotal ? oldT += ppn ? total / 1.1 : total; oldTax += ppn ? .1 * total : 0; oldTotal += total; f.FakturBeliTotalAkhirTextBox.Text = string.Format(Utilities.FormatRupiah, oldTotal); f.FakturBeliTotalBeliTextBox.Text = string.Format(Utilities.FormatRupiah, oldT); f.FakturBeliTotalPPNTextBox.Text = string.Format(Utilities.FormatRupiah, oldTax); item.Price = price; item.Quantity = quantity; f.ItemBeliOLV.RefreshObject(item); } catch (Exception e) { Console.WriteLine("Error :" + e.Message); try { transaction.Rollback(); } catch (SqlException ex) { if (transaction.Connection != null) { Console.WriteLine("An exception of type " + ex.GetType() + " was encountered while attempting to roll back the transaction."); } } return; } }