public ActionResult CustomerDetails([Bind(Include = "CustomerID,Name,CustType,Multi,Number,Email,Address")] CustomerViewModel customerViewModel, Customer customer) { // returns this view after customer passes regiter view in accounts string reciever = ""; string customername = ""; int Accid = 0; customerViewModel.CustType = "Customer"; customer.Disabled = false; if (TempData.ContainsKey("Username")) { string name = TempData["Username"].ToString(); var user = db.Accounts.Where(x => x.UserName == name).Select(y => y.AccountID).First(); Accid = Convert.ToInt32(user); } if (ModelState.IsValid) { reciever = customerViewModel.Email; customername = customerViewModel.Name; // inserts customer info to customer table using corresponding account ID customer.CustomerID = customerViewModel.CustomerID; customer.AccountID = Accid; customer.Name = customerViewModel.Name; customer.Type = customerViewModel.CustType; customer.Multi = customerViewModel.Multi; customer.Number = customerViewModel.Number; customer.Address = customerViewModel.Address; db.Customers.Add(customer); db.SaveChanges(); AccountsController acc = new AccountsController(); var subject = "Farmer's Fresh Produce Registration"; var body = "Welcome to Farmer's Fresh Produce " + customername + "\r\n Please enjoy our services"; acc.Confirmation(reciever, customername, subject, body); return(RedirectToAction("Store", "Stocks")); } return(View(customerViewModel)); }
public ActionResult CheckOut(CheckoutViewModel checkout) { DateTime today = DateTime.Now.Date; string name = User.Identity.Name; var getAcctId = (from x in db.Accounts where x.UserName == name select x.AccountID).First(); int accid = Convert.ToInt16(getAcctId); var getCustId = (from x in db.Customers where x.AccountID == accid select x.CustomerID).First(); int customerID = Convert.ToInt16(getCustId); var email = (from x in db.Customers where x.CustomerID == customerID select x.Email).First(); string custEmail = email.ToString(); var getCustName = (from x in db.Customers where x.CustomerID == customerID select x.Name).First(); string custname = getCustName.ToString(); var getOrderid = (from x in db.Orders where x.CustomerID == customerID select x.OrderID).First(); int OrderId = Convert.ToInt16(getOrderid); //var orders = db.Orders.Where(a => a.CustomerID == customerID).OrderBy(x => x.Customer.Name).ToList(); checkout.CustomerID = customerID; checkout.CustomerName = custname; Order order = db.Orders.Find(OrderId); order.status = "Processing"; db.Entry(order).State = EntityState.Modified; db.SaveChanges(); int customerId = Convert.ToInt16(order.CustomerID); int orderId = Convert.ToInt16(order.OrderID); statOrderId = Convert.ToInt16(order.OrderID); int total = Convert.ToInt16(order.Total); Sale sale = new Sale(); sale.CustomerID = customerId; sale.Date = DateTime.Now.Date; sale.Total = total; sale.OrderID = orderId; SalesController sc = new SalesController(); sc.AddSale(sale); string subject = "Order" + orderId + "Confirmation"; string body = custname + " your order= " + orderId + " of " + total + " has be successfully placed."; AccountsController acc = new AccountsController(); acc.Confirmation(custEmail, custname, subject, body); var stockoid = db.StockOrders.Where(x => x.OrderID == orderId).Select(y => y.SOID).First(); int stockorderid = Convert.ToInt16(stockoid); StockOrder stockorders = db.StockOrders.Find(stockorderid); db.StockOrders.Remove(stockorders); db.SaveChanges(); return(RedirectToAction("OrderHistory", "Orders")); }
public ActionResult AddEmployee([Bind(Include = "EmployeeID,Name,Surname,IDNumber,DateHired,ContactNum,KinContactNum,Email,FarmID,PositionID,GroupID,UserName,Password,ConfirmPassword,Type")] EmployeeAccountsViewModel employeeAccounts) { // employeeAccounts.Type = "Employee"; if (ModelState.IsValid) { try { // add employee account to account table var varBefore = db.Accounts.Select(x => x.AccountID).Count(); int before = Convert.ToInt16(varBefore); Account account = new Account(); account.UserName = employeeAccounts.UserName; account.Password = employeeAccounts.Password; account.ConfirmPassword = employeeAccounts.ConfirmPassword; db.Accounts.Add(account); db.SaveChanges(); //Acc ID var accFind = db.Accounts.Where(x => x.UserName == account.UserName).Select(y => y.AccountID).Single(); int accId = Convert.ToInt16(accFind); // add employee details to employee table var varAfter = db.Accounts.Select(x => x.AccountID).Count(); int after = Convert.ToInt16(varAfter); var beforeEmployeeIsAdded = db.Employees.Select(x => x.EmployeeID).Count(); if (before < after) { int positionId = employeeAccounts.PositionID; int farmId = employeeAccounts.FarmID; int groupId = employeeAccounts.GroupID; Employee employees = new Employee(); //employees.Account = new Account(); //employees.Position = new Position(); employees.EmployeeID = employeeAccounts.EmployeeID; employees.Email = employeeAccounts.Email; employees.Name = employeeAccounts.Name; employees.Surname = employeeAccounts.Surname; employees.AccountID = accId; employees.KinContactNum = employeeAccounts.KinContactNum; employees.IDNumber = employeeAccounts.IDNumber; employees.ContactNum = employeeAccounts.ContactNum; employees.DateHired = employeeAccounts.DateHired; DateTime hired = employeeAccounts.DateHired; employees.PositionID = positionId; employees.FarmID = farmId; employees.GroupID = groupId; // employees.Account.UserName = employeeAccounts.UserName; db.Employees.Add(employees); db.SaveChanges(); var checkEmp = db.Employees.Select(x => x.EmployeeID).Count(); if (beforeEmployeeIsAdded < checkEmp) { EmpPos employeePosition = new EmpPos(); employeePosition.EmployeeID = employees.EmployeeID; employeePosition.Started = hired; employeePosition.PositionID = positionId; employeePosition.Ended = null; db.EmpPos.Add(employeePosition); db.SaveChanges(); return(RedirectToAction("Index")); } } } catch (Exception ex) { // undo account commit if employee transaction fails var acc = db.Accounts.Where(x => x.UserName == employeeAccounts.UserName).Select(y => y.AccountID).Single(); int ac = Convert.ToInt16(acc); AccountsController emp = new AccountsController(); emp.DeleteConfirmed(ac); ViewBag.FarmID = new SelectList(db.Farms, "FarmID", "FarmName", employeeAccounts.FarmID); ViewBag.PositionID = new SelectList(db.Positions, "PositionID", "Name", employeeAccounts.PositionID); ViewBag.GroupID = new SelectList(db.Groups, "GroupID", "GName", employeeAccounts.GroupID); ViewBag.fatalError = ex.ToString(); return(View(employeeAccounts)); } } ViewBag.FarmID = new SelectList(db.Farms, "FarmID", "FarmName", employeeAccounts.FarmID); ViewBag.PositionID = new SelectList(db.Positions, "PositionID", "Name", employeeAccounts.PositionID); ViewBag.GroupID = new SelectList(db.Groups, "GroupID", "GName", employeeAccounts.GroupID); return(View(employeeAccounts)); }