public void RegisterPofMerch(purchase_of_merchandise purchase, Merchandise merchandise) { using (var en = new DB_SystemFoodTrucksEntities()) { using (DbContextTransaction transaction = en.Database.BeginTransaction()) { try { var merch_bd = en.Merchandise.First(x => x.Id == merchandise.Id); merch_bd.Stock = merchandise.Stock; en.SaveChanges(); en.purchase_of_merchandise.Add(purchase); en.SaveChanges(); transaction.Commit(); } catch (Exception) { transaction.Rollback(); } } //en.SP_PurchaseMerchandise(detail, amount, datePurchase, qty, idMerch, idSuppl,idUser); } }
private void btnPurchase_Click(object sender, RoutedEventArgs e) { try { var supplQuery = new Supplier { Supplier1 = cboSuppl.SelectedValue.ToString() }; var merchQuery = new Merchandise { Name = cboMerch.SelectedValue.ToString() }; int IdMerch = MerLogic.GetInstance().GetMerchId(merchQuery); int IdSuppl = MerLogic.GetInstance().GetIdSuppl(supplQuery); int AmountP = Convert.ToInt32(txtCostPur.Text); int Qty = Convert.ToInt32(txtCant.Text); int IdUser = LoginBL.GetInstance().IdUser; var purchase = new purchase_of_merchandise { Detail = txtDetail.Text, Amount = AmountP, Date_purchase = DateTime.Now, Quantity = Qty, Id_merchandise = IdMerch, Id_supplier = IdSuppl, Id_user = IdUser }; var updateMerch = new Merchandise() { Id = IdMerch, Stock = Qty, Name = cboMerch.SelectedValue.ToString() }; MerLogic.GetInstance().RegisterPofMerch(purchase, updateMerch); MessageBoxRM.Show("Compra registrada correctamente!", "Registro de compras", MessageBoxButtonRM.OK, MessageBoxIconRM.Information); txtDetail.Clear(); txtCant.Clear(); txtCostPur.Clear(); cboMerch.SelectedIndex = -1; cboSuppl.SelectedIndex = -1; RechargeAllCboS(); } catch (Exception ex) { MessageBoxRM.Show(ex.Message, "Ha ocurrido un error :(", MessageBoxButtonRM.OK, MessageBoxIconRM.Error); } }