public object addCustomer(Customer newcustomer) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); dynamic foundCustomer = new ExpandoObject(); try { var customer = db.Customers.Where((x => (x.CusName == newcustomer.CusName && x.CusSurname == newcustomer.CusSurname))).FirstOrDefault(); if (customer == null) { db.Customers.Add(newcustomer); db.SaveChanges(); toReturn.Message = newcustomer.CusName + " " + newcustomer.CusSurname + " has been added successfully."; } else { toReturn.Message = "Customer Already Exists"; } } catch { toReturn.Message = "Add UnSuccessful"; } return(toReturn); }
public object addProvince(Province newProvince) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { var name = newProvince.ProvName; List <Province> provList = db.Provinces.ToList(); var dupCheck = false; var specCheck = false; foreach (var item in provList) { if (name == item.ProvName) { dupCheck = true; } } if (dupCheck == true) { toReturn.Message = "Duplicate province record"; } else if (dupCheck == false) { string specialChar = @"\|!#$%&/()=?»«@£§€{}.-;'<>_,"; foreach (var item in specialChar) { if (name.Contains(item)) { specCheck = true; } } if (name.Equals(null) || specCheck == true || name.Equals("")) { toReturn.Message = "Invalid input"; } else { db.Provinces.Add(newProvince); db.SaveChanges(); toReturn.Message = "Province Add Successful"; } } } catch (Exception) { toReturn.Message = "Province Add Unsuccsessful"; } return(toReturn); }
public object addCreditorPayment(Creditor_Payment newCreditorPayment) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { db.Creditor_Payment.Add(newCreditorPayment); Creditor creditor = db.Creditors.Where(x => x.SupplierID == newCreditorPayment.SupplierID).FirstOrDefault(); creditor.CredAccountBalance = creditor.CredAccountBalance - newCreditorPayment.CredPaymentAmount; if (creditor.CredAccountBalance < newCreditorPayment.CredPaymentAmount) { float amount = (float)(creditor.CredAccountBalance + newCreditorPayment.CredPaymentAmount); toReturn.Message = "Payment is greater than the Creditor's Balance. Balance for this Creditor is R" + amount; } else { db.SaveChanges(); toReturn.Message = "Creditor payment has been added successfully. Balance = R" + creditor.CredAccountBalance; } } catch (Exception) { toReturn.Message = "Payment not added. The selected supplier is not a creditor for ORDRA."; } return(toReturn); }
public object registerUser(User user) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { User newUser = new User(); var password = user.UserPassword; var hash = GenerateHash(ApplySalt(password)); User foundUser = db.Users.Where(x => x.UserEmail == user.UserEmail).FirstOrDefault(); if (foundUser == null) { newUser.UserName = user.UserName; newUser.UserSurname = user.UserSurname; newUser.UserPassword = hash; newUser.UserCell = user.UserCell; newUser.UserEmail = user.UserEmail; Guid guid = Guid.NewGuid(); newUser.SessionID = guid.ToString(); newUser.UserTypeID = 2; db.Users.Add(newUser); db.SaveChanges(); toReturn.Message = "Registration Successful"; } else if (foundUser != null) { toReturn.Error = "Email already Registered"; return(toReturn); } } catch { toReturn.Error = "Registration Unsuccesful"; } return(toReturn); }
public object AddDonationRecipient(Donation_Recipient newDonationRecipient) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { db.Donation_Recipient.Add(newDonationRecipient); db.SaveChanges(); toReturn.Message = "Add Succsessful"; } catch (Exception) { toReturn.Message = "Add UnSuccsessful"; } return(toReturn); }
public object addLocation(Location newLocation) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { db.Locations.Add(newLocation); db.SaveChanges(); toReturn.Message = "Location Add Succsessful"; } catch (Exception) { toReturn.Message = "Failed to add location"; } return(toReturn); }
public object AddProductCategory(Product_Category newProductCategory) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { db.Product_Category.Add(newProductCategory); db.SaveChanges(); toReturn.Message = "Add Succsessful"; } catch (Exception) { toReturn.Message = "Add UnSuccsessful"; } return(toReturn); }
public object addCreditor(Creditor newCreditor) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { db.Creditors.Add(newCreditor); db.SaveChanges(); toReturn.Message = "Creditor added successfully."; } catch (Exception) { toReturn.Message = "Failed to add creditor"; } return(toReturn); }
public object addArea(Area newArea) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { db.Areas.Add(newArea); db.SaveChanges(); toReturn.Message = "The Area has been added successfully."; } catch (Exception) { toReturn.Message = "Failed to add the Area"; } return(toReturn); }
public object AddContainer(Container newConatiner) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { db.Containers.Add(newConatiner); db.SaveChanges(); toReturn.Message = "Add Succsessful"; } catch (Exception) { toReturn.Message = "Add UnSuccsessful"; } return(toReturn); }
public dynamic createEmployee(Employee employee) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { //Get User Details From Input Parameter User user = db.Users.Where(x => x.UserID == employee.UserID).FirstOrDefault(); Employee dupObj = db.Employees.Where(z => z.UserID == employee.UserID).FirstOrDefault(); if (dupObj == null) { //Set employee Details To Return object Employee employeeDetails = new Employee(); if (employee != null) { employeeDetails.User = user; employeeDetails.EmpStartDate = employee.EmpStartDate; employeeDetails.EmpShiftsCompleted = employee.EmpShiftsCompleted; db.Employees.Add(employeeDetails); db.SaveChanges(); toReturn.Message = "Employee Profile Succesfully Created"; } else { toReturn.Message = "Employee Profile Not Found"; } } else { toReturn.Message = "Employee already exists "; } } catch (Exception) { toReturn = "Failed to create an employee record "; } return(toReturn); }
public object deleteSupplier(int id) { db.Configuration.ProxyCreationEnabled = false; Supplier objectSupplier = new Supplier(); dynamic toReturn = new ExpandoObject(); try { objectSupplier = db.Suppliers.Find(id); if (objectSupplier == null) { toReturn.Message = "Supplier Record Not Found"; } else { Supplier_Order order = db.Supplier_Order.Where(x => x.SupplierID == id).FirstOrDefault(); Product product = db.Products.Where(x => x.SupplierID == id).FirstOrDefault(); if (product == null || order == null) { db.Suppliers.Remove(objectSupplier); db.SaveChanges(); toReturn.Message = "Delete Successful"; } else { toReturn.Message = "Delete Restricted"; } } } catch { toReturn.Message = "Delete Resticted"; } return(toReturn); }
public dynamic createManager(Manager manager) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { //Get User Details From Input Parameter User user = db.Users.Where(x => x.UserID == manager.UserID).FirstOrDefault(); //get user type of manager User_Type usertype = db.User_Type.Where(x => x.UTypeDescription == "Manager").FirstOrDefault(); //set usertype to manager user.User_Type = usertype; db.SaveChanges(); //Get Lists 0f Contains To Set In Create Dynamic Object From Input Parameter List <Container> containers = manager.Containers.ToList(); List <Container> managedContainers = new List <Container>(); foreach (var con in containers) { Container container = db.Containers.Where(x => x.ContainerID == con.ContainerID).SingleOrDefault(); managedContainers.Add(container); } //Set Manager Details To add Manager managerDetails = new Manager(); if (manager != null) { managerDetails.User = user; managerDetails.ManQualification = manager.ManQualification; managerDetails.ManNationality = manager.ManNationality; managerDetails.ManIDNumber = manager.ManIDNumber; managerDetails.ManNextOfKeenFName = manager.ManNextOfKeenFName; managerDetails.ManNextOfKeenCell = manager.ManNextOfKeenCell; managerDetails.Containers = managedContainers; db.Managers.Add(managerDetails); db.SaveChanges(); toReturn.Message = "Manager Profile Succesfully Created"; } else { toReturn.Message = "Manager Profile Not Found"; } } catch { toReturn.Message = "Search Interrupted.Retry"; } return(toReturn); }
public object AddDonation(Donation newDonation) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { db.Donations.Add(newDonation); db.SaveChanges(); toReturn.Message = "Donation Add Successful"; } catch (Exception) { toReturn.Message = "Donation Add Unsuccessful"; } return(toReturn); }
public object addUserTypeAccess(int accessid, int usertypeid) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); try { if (accessid == 0) { return(toReturn.Message = "Add Unsuccesful: Access Not Selected"); } if (usertypeid == 0) { return(toReturn.Message = "Add Unsuccesful: User Type Not Selected"); } Access access = db.Accesses.Where(x => x.AccessID == accessid).FirstOrDefault(); User_Type user_Type = db.User_Type.Where(x => x.UserTypeID == usertypeid).FirstOrDefault(); if (access != null && user_Type != null) { User_Type_Access newaccess = new User_Type_Access(); newaccess.UserTypeID = user_Type.UserTypeID; newaccess.AccessID = access.AccessID; newaccess.AccessGranted = DateTime.Now; newaccess.Access = access; newaccess.User_Type = user_Type; User_Type_Access found = db.User_Type_Access.Where(x => x.AccessID == newaccess.AccessID && x.UserTypeID == newaccess.AccessID).FirstOrDefault(); if (found == null) { db.User_Type_Access.Add(newaccess); db.SaveChanges(); toReturn.Message = "User Type Access Added"; } else { toReturn.Message = "User Type Access Is Already Set"; } } } catch { toReturn.Error = "Adding Access Unsuccesful "; } return(toReturn); }
public object initiatePlaceOrder(int customerID, dynamic session) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); toReturn.customer = new ExpandoObject(); toReturn.orderInfo = new ExpandoObject(); toReturn.products = new ExpandoObject(); try { Container con = new Container(); //get container of current user string sessionID = session.token; var user = db.Users.Where(x => x.SessionID == sessionID).FirstOrDefault(); if (user.ContainerID == null) { return(toReturn.Error = ("Curent Container Not Found")); } con = db.Containers.Where(x => x.ContainerID == user.ContainerID).FirstOrDefault(); if (con == null) { return(toReturn.Error = ("Curent Container Not Found")); } //get products in container List <Container_Product> conProd = db.Container_Product.Include(x => x.Product).Where(x => x.CPQuantity < 1 && x.ContainerID == con.ContainerID).ToList(); //get customer details Customer customer = new Customer(); customer = db.Customers.Where(x => x.CustomerID == customerID).FirstOrDefault(); toReturn.customer = customer; if (customer != null) { //Get Order No Customer_Order prevOrder = db.Customer_Order.ToList().LastOrDefault(); Customer_Order prevID = db.Customer_Order.ToList().LastOrDefault(); int prevOrderNo = Convert.ToInt32(prevOrder.CusOrdNumber); int OrderNo = prevOrderNo + 1; //Get Todays date var orderDate = DateTime.Now.ToString("yyyy-MM-dd"); //Set Order No And Order Date In Dynamic Object dynamic orderInfo = new ExpandoObject(); orderInfo.OrderNo = OrderNo; orderInfo.OrderDate = orderDate; orderInfo.VatPerc = db.VATs.Where(x => x.VATStartDate <= DateTime.Now).ToList().LastOrDefault(); toReturn.orderInfo = orderInfo; //Get List Of products with current price List <Product> productsList = db.Products.ToList(); List <dynamic> products = new List <dynamic>(); foreach (var prod in conProd) { Price price = db.Prices.Include(x => x.Product).Where(x => x.PriceStartDate <= DateTime.Now && x.PriceEndDate >= DateTime.Now && x.ProductID == prod.ProductID).FirstOrDefault(); if (price != null) { double Price = (double)price.UPriceR; dynamic productDetails = new ExpandoObject(); productDetails.ProductCategoryID = prod.Product.ProductCategoryID; productDetails.ProductID = prod.ProductID; productDetails.ProdDescription = prod.Product.ProdDesciption; productDetails.Prodname = prod.Product.ProdName; productDetails.Quantity = 0; productDetails.Price = Math.Round(Price, 2); productDetails.Subtotal = 0.0; products.Add(productDetails); } } toReturn.products = products; toReturn.VAT = db.VATs.Where(x => x.VATStartDate <= DateTime.Now).ToList().LastOrDefault(); Customer_Order_Status order_Status = db.Customer_Order_Status.Where(x => x.CODescription == "Placed").FirstOrDefault(); //set up sale Customer_Order customerOrder = new Customer_Order(); customerOrder.Customer = customer; customerOrder.Customer_Order_Status = order_Status; customerOrder.UserID = user.UserID; customerOrder.User = user; customerOrder.Container = con; customerOrder.ContainerID = con.ContainerID; customerOrder.CusOrdNumber = Convert.ToString(OrderNo); customerOrder.CusOrdDate = DateTime.Now; db.Customer_Order.Add(customerOrder); db.SaveChanges(); toReturn.CustomerOrder = db.Customer_Order.ToList().LastOrDefault(); orderInfo.CustomerOrderID = customerOrder.CustomerOrderID; } else { toReturn.Message = "No products were found. All products seem to be in stock."; } } catch (Exception error) { toReturn.Message = error.Message; } return(toReturn); }
public object addProductToOrder(int containerID, int supplierID, int productID, int quantity) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); toReturn.product = new ExpandoObject(); try { //search for the supplier and product in the database Supplier supplier = db.Suppliers.Where(X => X.SupplierID == supplierID).FirstOrDefault(); Product product = db.Products.Where(x => x.ProductID == productID).FirstOrDefault(); if (supplier != null && product != null) { DateTime date = DateTime.Now; string date_ = date.ToString("yyyy-MM-dd"); date = Convert.ToDateTime(date_); //check to see if the supplier order was already created today in the current container Supplier_Order supplier_Order = db.Supplier_Order.Where(x => x.SupplierID == supplierID && x.SODate == date && x.SupplierOrderStatusID == 1 && x.ContainerID == containerID).FirstOrDefault(); if (supplier_Order == null) { //get the "Placed" order status Supplier_Order_Status status = db.Supplier_Order_Status.Where(x => x.SupplierOrderStatusID == 1).FirstOrDefault(); //create new supplier order Supplier_Order newOrder = new Supplier_Order(); newOrder.SupplierID = supplierID; newOrder.SODate = DateTime.Now; newOrder.ContainerID = containerID; newOrder.SupplierOrderStatusID = status.SupplierOrderStatusID; db.Supplier_Order.Add(newOrder); db.SaveChanges(); //retrive the placed order Supplier_Order order = db.Supplier_Order.ToList().LastOrDefault(); if (order != null) { //add the product to the created order Supplier_Order_Product addProd = new Supplier_Order_Product(); addProd.ProductID = productID; addProd.SupplierOrderID = order.SupplierOrderID; addProd.SOPQuantityOrdered = quantity; addProd.SOPQuantityRecieved = 0; db.Supplier_Order_Product.Add(addProd); db.SaveChanges(); //returning the product so you can see it in the console toReturn.product = db.Supplier_Order_Product.Where(x => x.ProductID == productID && x.SupplierOrderID == order.SupplierOrderID).FirstOrDefault(); } } else { //add product to existing Order Supplier_Order_Product addProd = new Supplier_Order_Product(); addProd.ProductID = productID; addProd.SupplierOrderID = supplier_Order.SupplierOrderID; addProd.SOPQuantityOrdered = quantity; addProd.SOPQuantityRecieved = 0; db.Supplier_Order_Product.Add(addProd); db.SaveChanges(); //returning the product so you can see it in the console if you want toReturn.product = db.Supplier_Order_Product.Where(x => x.ProductID == productID && x.SupplierOrderID == supplier_Order.SupplierOrderID).FirstOrDefault(); } toReturn.Message = "Product Added To Order"; } else { toReturn.Error = "Supplier Or Product Details Not Found"; } } catch { toReturn.Error = "Product Already Ordered. Awaiting Supplier Order Deliery"; } return(toReturn); }
public object initiatePlaceOrder(int customerID, dynamic session) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); toReturn.products = new ExpandoObject(); toReturn.CustomerOrder = new Sale(); toReturn.Customer = new ExpandoObject(); toReturn.VAT = new ExpandoObject(); try { //get customer details Customer customer = new Customer(); customer = db.Customers.Where(x => x.CustomerID == customerID).FirstOrDefault(); toReturn.Customer = customer; Container con = new Container(); //get container of current user string sessionID = session.token; var user = db.Users.Where(x => x.SessionID == sessionID).FirstOrDefault(); if (user.ContainerID == null) { return(toReturn.Error = ("Curent Container Not Found")); } con = db.Containers.Where(x => x.ContainerID == user.ContainerID).FirstOrDefault(); if (con == null) { return(toReturn.Error = ("Curent Container Not Found")); } //get products in container List <Container_Product> conProd = db.Container_Product.Include(x => x.Product).Where(x => x.CPQuantity < 1 && x.ContainerID == con.ContainerID).ToList(); //get todays date DateTime CustomerOrderDate = DateTime.Now; //get payment types toReturn.Customer = db.Payment_Type.ToList(); if (conProd != null) { Customer_Order prevOrder = db.Customer_Order.ToList().LastOrDefault(); int prevOrderNo = Convert.ToInt32(prevOrder.CusOrdNumber); int OrderNo = prevOrderNo + 1; //Get List Of products with current price List <Product> productsList = db.Products.ToList(); List <dynamic> products = new List <dynamic>(); foreach (var prod in conProd) { Price price = db.Prices.Include(x => x.Product).Where(x => x.PriceStartDate <= DateTime.Now && x.PriceEndDate >= DateTime.Now && x.ProductID == prod.ProductID).ToList().LastOrDefault(); if (price != null) { double Price = (double)price.UPriceR; dynamic productDetails = new ExpandoObject(); productDetails.ProductCategoryID = prod.Product.ProductCategoryID; productDetails.ProductID = prod.Product.ProductID; productDetails.ProdBarcode = prod.Product.ProdBarcode; productDetails.ProdDescription = prod.Product.ProdDesciption; productDetails.Prodname = prod.Product.ProdName; productDetails.CPQuantity = prod.CPQuantity; productDetails.Quantity = 0; productDetails.Price = Math.Round(Price, 2); productDetails.Subtotal = 0.0; products.Add(productDetails); } } toReturn.products = products; //get VAT toReturn.VAT = db.VATs.Where(x => x.VATStartDate <= DateTime.Now).ToList().LastOrDefault(); //set up sale Customer_Order newCustomerOrder = new Customer_Order(); newCustomerOrder.CusOrdDate = CustomerOrderDate; newCustomerOrder.CusOrdNumber = Convert.ToString(OrderNo); newCustomerOrder.UserID = user.UserID; newCustomerOrder.User = user; newCustomerOrder.CustomerID = customer.CustomerID; newCustomerOrder.CustomerOrderStatusID = 3; newCustomerOrder.Customer = customer; newCustomerOrder.Container = con; newCustomerOrder.ContainerID = con.ContainerID; db.Customer_Order.Add(newCustomerOrder); db.SaveChanges(); //getsale toReturn.CustomerOrder = db.Customer_Order.ToList().LastOrDefault(); } else { return(toReturn.Message = "All products seem to be in stock."); } } catch { toReturn.Error = "Please Reload Page to Initiate Order"; } return(toReturn); }
public object initiateMakeSale(dynamic session) { db.Configuration.ProxyCreationEnabled = false; dynamic toReturn = new ExpandoObject(); toReturn.products = new ExpandoObject(); toReturn.Sale = new Sale(); toReturn.paymetTypes = new ExpandoObject(); toReturn.VAT = new ExpandoObject(); try { Container con = new Container(); //get container of current user string sessionID = session.token; var user = db.Users.Where(x => x.SessionID == sessionID).FirstOrDefault(); if (user.ContainerID == null) { return(toReturn.Error = ("Curent Container Not Found")); } con = db.Containers.Where(x => x.ContainerID == user.ContainerID).FirstOrDefault(); if (con == null) { return(toReturn.Error = ("Curent Container Not Found")); } //get products in container List <Container_Product> conProd = db.Container_Product.Include(x => x.Product).Where(x => x.CPQuantity > 0 && x.ContainerID == con.ContainerID).ToList(); //get todays date DateTime SaleDate = DateTime.Now; //get payment types toReturn.paymentTypes = db.Payment_Type.ToList(); if (conProd != null) { //Get List Of products with current price List <Product> productsList = db.Products.ToList(); List <dynamic> products = new List <dynamic>(); foreach (var prod in conProd) { Price price = db.Prices.Include(x => x.Product).Where(x => x.PriceStartDate <= DateTime.Now && x.PriceEndDate >= DateTime.Now && x.ProductID == prod.ProductID).ToList().LastOrDefault(); if (price != null) { double Price = (double)price.UPriceR; dynamic productDetails = new ExpandoObject(); productDetails.ProductCategoryID = prod.Product.ProductCategoryID; productDetails.ProductID = prod.Product.ProductID; productDetails.ProdBarcode = prod.Product.ProdBarcode; productDetails.ProdDescription = prod.Product.ProdDesciption; productDetails.Prodname = prod.Product.ProdName; productDetails.CPQuantity = prod.CPQuantity; productDetails.Quantity = 0; productDetails.Price = Math.Round(Price, 2); productDetails.Subtotal = 0.0; products.Add(productDetails); } } toReturn.products = products; //get VAT toReturn.VAT = db.VATs.Where(x => x.VATStartDate <= DateTime.Now).ToList().LastOrDefault(); //set up sale Sale newSale = new Sale(); newSale.SaleDate = SaleDate; newSale.UserID = user.UserID; newSale.User = user; newSale.Container = con; newSale.ContainerID = con.ContainerID; db.Sales.Add(newSale); db.SaveChanges(); //getsale toReturn.Sale = db.Sales.ToList().LastOrDefault(); } else { return(toReturn.Message = "There are no products in stock for the operating Container"); } } catch { toReturn.Error = "Please Reload Page to Initiate Sale"; } return(toReturn); }