public bool EditInvoice(Invoice obj, List <InvoiceDetail> listObj, out string serverMessage) { try { using (var dataContext = new SalesManagementEntities()) { using (var transaction = dataContext.Database.BeginTransaction()) { try { Invoice objE = dataContext.Invoices.Single(o => o.InvoiceID == obj.InvoiceID); objE.EmployeeID = obj.EmployeeID; objE.CustomerID = obj.CustomerID; objE.Freight = obj.Freight; objE.InvoiceDate = obj.InvoiceDate; dataContext.SaveChanges(); var listObjE = dataContext.InvoiceDetails.Where(o => o.InvoiceID == obj.InvoiceID).ToList(); foreach (var objD in listObj) { var objDE = listObjE.Single(i => i.ProductID == objD.ProductID); var prdE = dataContext.Products.Single(o => o.ProductID == objD.ProductID); if (!prdE.Discontinued) { int quantity = objD.Quantity - objDE.Quantity; prdE.UnitsInStock -= quantity; prdE.UnitsOnInvoice += quantity; objDE.Quantity = objD.Quantity; objDE.UnitPrice = objD.UnitPrice; } else { // roll back lại nếu như sản phẩm ko còn bán nữa transaction.Rollback(); serverMessage = "The product: " + prdE.ProductName + " has been discontinued"; return(false); } dataContext.SaveChanges(); } transaction.Commit(); serverMessage = obj.InvoiceID.ToString(); return(true); } catch (Exception ex) { transaction.Rollback(); serverMessage = ExceptionMessage(ex); return(false); } } } } catch (Exception ex) { serverMessage = ExceptionMessage(ex); return(false); } }
public bool AddInvoice(Invoice obj, List <InvoiceDetail> listObj, out string serverMessage) { try { using (var dataContext = new SalesManagementEntities()) { using (var transaction = dataContext.Database.BeginTransaction()) { try { dataContext.Invoices.Add(obj); dataContext.SaveChanges(); foreach (var objD in listObj) { var prdE = dataContext.Products.Single(o => o.ProductID == objD.ProductID); if (!prdE.Discontinued) { objD.InvoiceID = obj.InvoiceID; dataContext.InvoiceDetails.Add(objD); prdE.UnitsInStock -= objD.Quantity; prdE.UnitsOnInvoice += objD.Quantity; } else { // roll back lại nếu như sản phẩm ko còn bán nữa transaction.Rollback(); serverMessage = "The product: " + prdE.ProductName + " has been discontinued"; return(false); } dataContext.SaveChanges(); } transaction.Commit(); serverMessage = obj.InvoiceID.ToString(); return(true); } catch (Exception ex) { transaction.Rollback(); serverMessage = ExceptionMessage(ex); return(false); } } } } catch (Exception ex) { serverMessage = ExceptionMessage(ex); return(false); } }
public bool EditEmployee(Employee obj, out string serverMessage) { try { using (var dataContext = new SalesManagementEntities()) { Employee objE = dataContext.Employees.Single(o => o.EmployeeID == obj.EmployeeID); objE.Name = obj.Name; objE.BirthDate = obj.BirthDate; objE.Gender = obj.Gender; objE.ID = obj.ID; objE.PhoneNumber = obj.PhoneNumber; objE.Address = obj.Address; objE.JobTitle = obj.JobTitle; dataContext.SaveChanges(); serverMessage = obj.EmployeeID.ToString(); } return(true); } catch (Exception ex) { serverMessage = ExceptionMessage(ex); return(false); } }
public bool EditProduct(Product obj, out string serverMessage) { try { using (var dataContext = new SalesManagementEntities()) { Product objE = dataContext.Products.Single(o => o.ProductID == obj.ProductID); objE.ProductName = obj.ProductName; objE.AddedDate = obj.AddedDate; objE.QuantityPerUnit = obj.QuantityPerUnit; objE.UnitPrice = obj.UnitPrice; objE.UnitsInStock = obj.UnitsInStock; objE.UnitsOnInvoice = obj.UnitsOnInvoice; objE.Discontinued = obj.Discontinued; dataContext.SaveChanges(); serverMessage = obj.ProductID.ToString(); } return(true); } catch (Exception ex) { serverMessage = ExceptionMessage(ex); return(false); } }
public bool DeleteInvoice(int id, out string serverMessage) { try { using (var dataContext = new SalesManagementEntities()) { using (var transaction = dataContext.Database.BeginTransaction()) { try { var listObj = dataContext.InvoiceDetails.Where(o => o.InvoiceID == id).ToList(); foreach (var objD in listObj) { var prdE = dataContext.Products.Single(o => o.ProductID == objD.ProductID); prdE.UnitsInStock += objD.Quantity; prdE.UnitsOnInvoice -= objD.Quantity; dataContext.InvoiceDetails.Remove(objD); dataContext.SaveChanges(); } Invoice obj = dataContext.Invoices.Single(o => o.InvoiceID == id); dataContext.Invoices.Remove(obj); dataContext.SaveChanges(); transaction.Commit(); serverMessage = obj.InvoiceID.ToString(); return(true); } catch (Exception ex) { transaction.Rollback(); serverMessage = ExceptionMessage(ex); return(false); } } } } catch (Exception ex) { serverMessage = ExceptionMessage(ex); return(false); } }
public bool AddCustomer(Customer obj, out string serverMessage) { try { using (var dataContext = new SalesManagementEntities()) { dataContext.Customers.Add(obj); dataContext.SaveChanges(); serverMessage = obj.CustomerID.ToString(); } return(true); } catch (Exception ex) { serverMessage = ExceptionMessage(ex); return(false); } }
public bool DeleteCustomer(int id, out string serverMessage) { try { using (var dataContext = new SalesManagementEntities()) { Customer obj = dataContext.Customers.Single(o => o.CustomerID == id); dataContext.Customers.Remove(obj); dataContext.SaveChanges(); serverMessage = obj.CustomerID.ToString(); } return(true); } catch (Exception ex) { serverMessage = ExceptionMessage(ex); return(false); } }
public bool EditCustomer(Customer obj, out string serverMessage) { try { using (var dataContext = new SalesManagementEntities()) { Customer objE = dataContext.Customers.Single(o => o.CustomerID == obj.CustomerID); objE.Name = obj.Name; objE.Gender = obj.Gender; objE.PhoneNumber = obj.PhoneNumber; objE.Address = obj.Address; objE.Email = obj.Email; dataContext.SaveChanges(); serverMessage = obj.CustomerID.ToString(); } return(true); } catch (Exception ex) { serverMessage = ExceptionMessage(ex); return(false); } }