private void BtnSave_Click(object sender, RoutedEventArgs e) { bool sellerIsSelected = (comboSeller.SelectedItem != null && !comboSeller.SelectedItem.ToString().Equals("Выберите покупателя")) ? true : false; bool tableIsFiled = moovingList.Count > 0 ? true : false; if (!sellerIsSelected) { MessageBox.Show("Выберите продавца из списка или создайте нового", "Продавец не выбран", MessageBoxButton.OK, MessageBoxImage.Error); } else if (!tableIsFiled) { ErrorMessages.MoovingTableIsEmpty(); } else if (sellerIsSelected && tableIsFiled) { DataBaseReader dbReader = new DataBaseReader(this.sqlConnect); string sellerID; string sellerName = comboSeller.SelectedItem.ToString(); string command = "SELECT [Buyers].[BuyerID] FROM [Buyers] " + "WHERE [Buyers].[BuyerName] = '" + sellerName + "'"; sellerID = dbReader.GetString(command); string lastUpdate = Convert.ToDateTime(DateTime.Now).ToString("yyyyMMdd"); List <Warehouses> listWarehouses = GetWarehousesList(); for (int i = 0; i < moovingList.Count; i++) { string goodsID; string goodsName = moovingList[i].GoodsName; command = "SELECT [Goods].[GoodsID] FROM [Goods] " + "WHERE [Goods].[GoodsName] = '" + goodsName + "'"; goodsID = dbReader.GetString(command); string goodsQuantity = moovingList[i].GoodsQuantity; string goodsPrice = moovingList[i].GoodsPrice; //ID склада string warehouseSourse = moovingList[i].WarehouseSourse; int index = listWarehouses.FindIndex( indx => string.Equals(indx.WarehouseName, warehouseSourse, StringComparison.CurrentCultureIgnoreCase)); string warehouseSourceID = listWarehouses[index].RowNumber; command = "SELECT [GoodsInSale].[GoodsName] FROM [GoodsInSale] " + "WHERE [GoodsInSale].[BuyerName] = '" + sellerID + "' " + "AND [GoodsInSale].[GoodsName] = '" + goodsID + "'"; bool rowIsFinded = dbReader.SearchMatch(command); DataBaseWriter dbWriter = new DataBaseWriter(this.sqlConnect); if (rowIsFinded) { command = "SELECT [GoodsInSale].[GoodsQuantity] FROM [GoodsInSale] " + "WHERE [GoodsInSale].[BuyerName] = '" + sellerID + "' " + "AND [GoodsInSale].[GoodsName] = '" + goodsID + "'"; float oldQuantityFromSeller = float.Parse(dbReader.GetString(command)); float newQuantityFromSeller = oldQuantityFromSeller + float.Parse(goodsQuantity); command = "UPDATE [GoodsInSale] " + "SET [GoodsQuantity] = '" + TextHandler.FloatToString(newQuantityFromSeller) + "', " + "[GoodsPrice] = '" + goodsPrice + "', " + "[LastUpdate] = '" + lastUpdate + "' " + "WHERE [GoodsInSale].[BuyerName] = '" + sellerID + "' " + "AND [GoodsInSale].[GoodsName] = '" + goodsID + "'"; dbWriter.WriteData(command); } else { //Продавец dbWriter.InsDataRow.Add(new InsertDataToRow { ColumnName = "BuyerName", Data = sellerID }); //Товар dbWriter.InsDataRow.Add(new InsertDataToRow { ColumnName = "GoodsName", Data = goodsID }); //Количество dbWriter.InsDataRow.Add(new InsertDataToRow { ColumnName = "GoodsQuantity", Data = goodsQuantity }); //Цена dbWriter.InsDataRow.Add(new InsertDataToRow { ColumnName = "GoodsPrice", Data = goodsPrice }); //Дата последней поставки dbWriter.InsDataRow.Add(new InsertDataToRow { ColumnName = "LastUpdate", Data = lastUpdate }); command = "INSERT INTO [GoodsInSale] (BuyerName, GoodsName, GoodsQuantity, GoodsPrice, LastUpdate) " + "VALUES (@BuyerName, @GoodsName, @GoodsQuantity, @GoodsPrice, @LastUpdate)"; dbWriter.WriteData(command); } //Обновления количества товара на складе поставщике command = "SELECT [GoodsInWarehouses].[GoodsBalance] FROM [GoodsInWarehouses] " + "WHERE [GoodsInWarehouses].[WarehouseName] = '" + warehouseSourceID + "' " + "AND [GoodsInWarehouses].[GoodsName] = '" + goodsID + "'"; float oldQuantity = float.Parse(dbReader.GetString(command)); float newQuantity = oldQuantity - float.Parse(goodsQuantity); DataBaseWriter updateData = new DataBaseWriter(this.sqlConnect); command = "UPDATE [GoodsInWarehouses]" + "SET [GoodsBalance] = " + TextHandler.FloatToString(newQuantity) + " " + "WHERE [GoodsName] = '" + goodsID + "'" + "AND [WarehouseName] = '" + warehouseSourceID + "'"; updateData.WriteData(command); } GoodsMoved?.Invoke(sender, e); MessageBox.Show(string.Format("Перемещение {0} продавцу {1} успешно завершено", moovingList.Count > 1 ? "товаров" : "товара", sellerName), "Выполнено", MessageBoxButton.OK, MessageBoxImage.Information); this.Close(); } }