public async Task <Response> SaveProduct(ProductRequest productRequest) { if (productRequest == null) { throw new ArgumentNullException(nameof(productRequest)); } ProductAgg product = null; ControlTransactionFields transactionInfo = TransactionInfo.GetTransactionData(productRequest.UserName); IDbContextTransaction transaction = _bagelSalesControlContext.Database.BeginTransaction(); if (productRequest.ProductId != 0) { product = _bagelSalesControlContext.Product.ToList().Find(p => p.ProductId == productRequest.ProductId); } if (product == null) { product = MaterializeProduct(productRequest, transactionInfo); await _bagelSalesControlContext.AddAsync <ProductAgg>(product); } else { product.NameProduct = productRequest.NameProduct; product.InitialExistence = productRequest.InitialExistence; product.Existence = productRequest.Existence; product.PurchasePrice = productRequest.PurchasePrice; product.SalePrice = productRequest.SalePrice; product.Wholesaleprice = productRequest.Wholesaleprice; product.TransactionModificationDate = DateTime.Now; } await _bagelSalesControlContext.SaveChangesAsync(); transaction.Commit(); _bagelSalesControlContext.Dispose(); return(new Response { Data = product, Message = "Product Saved Successfully" }); }
public async Task <Response> SaveSale(SalesRequest salesRequest) { if (salesRequest == null) { throw new ArgumentNullException(nameof(salesRequest)); } string notification = ""; Sales sale = null; IDbContextTransaction transaction = _bagelSalesControlContext.Database.BeginTransaction(); ControlTransactionFields transactionInfo = TransactionInfo.GetTransactionData(salesRequest.UserName); if (salesRequest.SaleId != 0) { sale = _bagelSalesControlContext.Sales.ToList().Find(s => s.SaleId == salesRequest.SaleId); } if (sale == null) { sale = MaterializeSale(salesRequest, transactionInfo); await _bagelSalesControlContext.AddAsync <Sales>(sale); } else { sale.SoldQuantity = salesRequest.SoldQuantity; sale.TypeSale = salesRequest.TypeSale; sale.Total = salesRequest.Total; sale.Commentary = salesRequest.Commentary; sale.TransactionModificationDate = DateTime.Now; } ProductAgg product = _bagelSalesControlContext.Product.ToList().Find(p => p.ProductId == salesRequest.ProductId); if (product != null) { if (product.Existence < salesRequest.SoldQuantity) { notification = "No hay suficientes " + product.NameProduct + " disponibles"; return(new Response { Notification = notification }); } product.Existence = product.Existence - salesRequest.SoldQuantity; if (product.Existence <= 5) { notification = "El Producto " + product.NameProduct + " está a punto de acabarse"; } } await _bagelSalesControlContext.SaveChangesAsync(); transaction.Commit(); _bagelSalesControlContext.Dispose(); transaction.Dispose(); return(new Response { Message = "The sale was registered successfully", Data = sale, Notification = notification }); }