Ejemplo n.º 1
0
        public IActionResult Accept(int?id)
        {
            if (id == null)
            {
                TempData["Msg"]     = "Something went wrong, try again later!";
                TempData["Styling"] = "alert rounded shadow alert-danger";
                return(RedirectToAction("index"));
            }
            var returnn       = _context.Returns.Include(o => o.Customer).FirstOrDefault(o => o.Id == id);
            var returnDetails = _context.ReturnDetails.Include(o => o.Product).FirstOrDefault(o => o.ReturnId == id);
            var customer      = _context.Customers.FirstOrDefault(o => o.Id == returnn.CustomerId);
            var product       = _context.Products.FirstOrDefault(o => o.Id == returnDetails.ProductId);
            var orderDetail   = _context.OrderDetails.FirstOrDefault(o => o.ProductId == product.Id && o.OrderId == returnn.OrderId);

            if (returnn == null || returnDetails == null || customer == null || product == null || orderDetail == null || returnn.ReturnStatusId != 1)
            {
                TempData["Msg"]     = "Something went wrong, try again later!";
                TempData["Styling"] = "alert rounded shadow alert-danger";
                return(RedirectToAction("index"));
            }
            customer.Credit       += returnn.TotalPrice;
            returnn.ReturnStatusId = 2;
            DateTime now     = DateTime.Now;
            DateTime expDate = (DateTime)orderDetail.ExpDate;

            if ((expDate - now).Days > 30)
            {
                product.Quantity += returnDetails.Quantity;
                try
                {
                    _context.Update(product);
                    _context.SaveChanges();
                }
                catch
                {
                    TempData["Msg"]     = "Something went wrong, try again later!";
                    TempData["Styling"] = "alert rounded shadow alert-danger";
                    return(RedirectToAction("index"));
                }
            }
            try
            {
                _context.Update(customer);
                _context.Update(returnn);
                _context.SaveChanges();
            }
            catch
            {
                TempData["Msg"]     = "Something went wrong, try again later!";
                TempData["Styling"] = "alert rounded shadow alert-danger";
                return(RedirectToAction("index"));
            }

            TempData["Msg"]     = "Return " + returnn.Id + " was accepted";
            TempData["Styling"] = "alert rounded shadow alert-success";
            return(RedirectToAction("index"));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> DisableVoucher(int id)
        {
            var vaucher = await _context.Vauchers.FirstOrDefaultAsync(c => c.Id == id);

            if (vaucher == null)
            {
                return(NotFound());
            }
            vaucher.IsUsed = true;

            try
            {
                _context.Update(vaucher);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!VaucherExists(vaucher.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            return(RedirectToAction(nameof(Index)));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name")] Categories categories)
        {
            if (id != categories.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(categories);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CategoriesExists(categories.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(categories));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,UserId,ContactPerson,PhoneNumber,Mail,Company,Cui,RegistrationNumber,Adress,CityId,Bank,Iban,IsAccepted,Credit,TypeId")] Customers customers)
        {
            if (id != customers.Id)
            {
                return(NotFound());
            }

            var UserId = customers.UserId;
            var user   = await userManager.FindByIdAsync(UserId);

            string role;

            if (customers.IsAccepted)
            {
                role = "Customer";
            }
            else
            {
                role = "User";
            }


            if (ModelState.IsValid)
            {
                try
                {
                    var roles = await userManager.GetRolesAsync(user);

                    await userManager.RemoveFromRolesAsync(user, roles.ToArray());

                    var result = await userManager.AddToRoleAsync(user, role);

                    _context.Update(customers);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CustomersExists(customers.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CityId"] = new SelectList(_context.Cities, "Id", "Name", customers.CityId);
            ViewData["TypeId"] = new SelectList(_context.Type, "Id", "Name", customers.TypeId);
            return(View(customers));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Edit(int?id, IEnumerable <OrderDetails> details)
        {
            var orders = await _context.Orders
                         .Include(o => o.Customer)
                         .Include(o => o.Employee)
                         .Include(o => o.OrderStatus)
                         .FirstOrDefaultAsync(o => o.Id == id);

            if (orders == null || orders.OrderStatusId != 8)
            {
                return(NotFound());
            }


            foreach (var item in details)
            {
                var orderDb = await _context.OrderDetails.FirstOrDefaultAsync(o => o.ProductId == item.ProductId && o.OrderId == item.OrderId);

                if (item.Quantity != orderDb.Quantity)
                {
                    var prod = await _context.Products.FirstOrDefaultAsync(o => o.Id == orderDb.ProductId);

                    prod.Quantity = prod.Quantity - (item.Quantity - orderDb.Quantity);
                    if (prod.Quantity < 0)
                    {
                        ViewBag.Order    = id;
                        ViewBag.customer = orders.Customer.Company;
                        ViewBag.Error    = "Quantity exceeded on product " + prod.Name;
                        var orderview = _context.OrderDetails.Include(o => o.Order).Include(o => o.Product).Where(o => o.OrderId == id);
                        return(View(await orderview.ToListAsync()));
                    }
                    orderDb.Quantity = item.Quantity;

                    try
                    {
                        _context.Update(orderDb);
                        _context.Update(prod);
                    }
                    catch
                    {
                        return(NotFound());
                    }
                }
            }
            await _context.SaveChangesAsync();


            return(RedirectToAction("Index"));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> payment(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            var customer = await GetCustomerByUserIdAsync();

            Orders order = await _context.Orders.FirstOrDefaultAsync(o => o.Id == id);

            if (order == null || order.isPaid || order.OrderStatusId != 11)
            {
                return(NotFound());
            }
            customer.Credit     = customer.Credit + order.TotalPrice;
            order.OrderStatusId = 13;
            order.isPaid        = true;
            try
            {
                _context.Update(customer);
                _context.Update(order);
                await _context.SaveChangesAsync();
            }
            catch
            {
                return(NotFound());
            }
            //return RedirectToAction("index");

            var customers = await GetCustomerByUserIdAsync();

            ViewBag.credit = customers.Credit;
            var mobiSysContext = _context.Orders.Where(o => o.CustomerId == customers.Id).Include(o => o.Customer).Include(o => o.Employee).Include(o => o.OrderStatus);

            TempData["Msg"]     = "Paid succeded!";
            TempData["Styling"] = "alert rounded shadow alert-success";
            return(RedirectToAction("index"));
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> finishDeliver(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            var deliver = await _context.Delivers.Include(o => o.StatusDeliver).FirstOrDefaultAsync(o => o.Id == id);

            if (deliver == null)
            {
                return(NotFound());
            }
            var order = await _context.Orders.Include(o => o.OrderStatus).FirstOrDefaultAsync(o => o.Id == deliver.OrderId);

            if (order == null || order.OrderStatus.Status != "Delivering" || deliver.StatusDeliver.Status != "Ready")

            {
                return(NotFound());
            }
            order.OrderStatusId     = 11;
            deliver.StatusDeliverId = 2;
            try
            {
                TempData["successMsg"] = "Deliver number " + deliver.Id.ToString() + " was finished";
                _context.Update(order);
                _context.Update(deliver);

                await _context.SaveChangesAsync();
            }
            catch
            {
                return(NotFound());
            }

            TempData["success"] = "alert rounded shadow alert-success";
            return(RedirectToAction("index"));
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> AddQuantity(int?ProductId, int?Quantity)
        {
            if (ProductId == null)
            {
                return(NotFound());
            }
            if (Quantity <= 0 || Quantity == null)
            {
                TempData["Msg"]     = "Quantity must be greater than 0!";
                TempData["Styling"] = "alert rounded shadow alert-danger";
                return(RedirectToAction("details", new { id = ProductId }));
            }
            var product = _context.Products.FirstOrDefault(o => o.Id == ProductId);

            if (product == null)
            {
                TempData["Msg"]     = "Something went wrong, try again later!";
                TempData["Styling"] = "alert rounded shadow alert-danger";
                return(RedirectToAction("details", new { id = ProductId }));
            }
            product.Quantity += (int)Quantity;
            try
            {
                _context.Update(product);
                await _context.SaveChangesAsync();
            }
            catch
            {
                TempData["Msg"]     = "Something went wrong, try again later!";
                TempData["Styling"] = "alert rounded shadow alert-danger";
                return(RedirectToAction("details", new { id = ProductId }));
            }
            TempData["Msg"]     = "Quantity updated!";
            TempData["Styling"] = "alert rounded shadow alert-success";
            return(RedirectToAction("details", new { id = ProductId }));
        }
Ejemplo n.º 9
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,UserId,FullName,email,Adress,CityId,JobId,Salary")] Employees employees)
        {
            if (id != employees.Id)
            {
                return(NotFound());
            }
            var UserId = employees.UserId;
            var user   = await userManager.FindByIdAsync(UserId);

            if (ModelState.IsValid)
            {
                try
                {
                    var roles = await userManager.GetRolesAsync(user);

                    await userManager.RemoveFromRolesAsync(user, roles.ToArray());

                    var result = await userManager.AddToRoleAsync(user, employees.JobId);
                }
                catch (Exception)
                {
                    ViewBag.Class    = "alert alert-danger";
                    ViewBag.ErrorMsg = "Role not valid";
                    return(View(employees));
                }
                try
                {
                    _context.Update(employees);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!EmployeesExists(employees.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CityId"] = new SelectList(_context.Cities, "Id", "Name", employees.CityId);
            return(View(employees));
        }
Ejemplo n.º 10
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,ContactPerson,PhoneNumber,Mail,Company,Cui,RegistrationNumber,Adress,CityId,Bank,Iban,TypeId")] Customers customers)
        {
            var userId = getUserID();

            customers.UserId = userId;
            ModelState.Remove("UserId");

            if (id != customers.Id)
            {
                return(NotFound());
            }


            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(customers);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CustomersExists(customers.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CityId"] = new SelectList(_context.Cities, "Id", "Name", customers.CityId);
            ViewData["TypeId"] = new SelectList(_context.Type, "Id", "Name", customers.TypeId);
            return(View(customers));
        }
Ejemplo n.º 11
0
        public async Task <IActionResult> AddToCart(int id, int quantityInput)
        {
            var product = await _context.Products
                          .Include(p => p.Cateogry)
                          .Include(p => p.Manufacturer)
                          .Include(p => p.Um)
                          .FirstOrDefaultAsync(m => m.Id == id);

            if (product == null)
            {
                return(NotFound());
            }

            var customer = await GetCustomerByUserIdAsync();

            if (customer == null)
            {
                return(NotFound());
            }



            if (quantityInput <= 0) //validate
            {
                TempData["WarningQuantityInput"] = "Qantity must be greater then 0";
                TempData["danger"] = "alert rounded shadow alert-danger";

                return(RedirectToAction("Details", new { ID = id }));
            }

            if (quantityInput > product.Quantity) // validate
            {
                TempData["WarningQuantityInput"] = "Quantity exceeded";
                TempData["danger"] = "alert rounded shadow alert-danger";
                return(RedirectToAction("Details", new { ID = id }));
            }



            var cart = _context.Carts.FirstOrDefault(m => m.CutomerId == customer.Id);

            if (cart == null)
            {
                return(NotFound());
            }

            var cartDetailsSql = _context.CartDetails  // select * from cartdetails where productid==id and cartid==cart.id
                                 .Where(c => c.ProductId == id && c.CartId == cart.Id);


            CartDetails cartDetails = await cartDetailsSql.FirstOrDefaultAsync(c => c.ProductId == product.Id && c.CartId == cart.Id);

            if (cartDetails == null)
            {
                CartDetails cartDetail = new CartDetails();
                cartDetail.ProductId = product.Id;
                cartDetail.Quantity  = quantityInput;
                cartDetail.CartId    = cart.Id;
                _context.CartDetails.Add(cartDetail);
                await _context.SaveChangesAsync();

                TempData["SuccesQuantityInput"] = "The product was added to cart";
                TempData["success"]             = "alert rounded shadow alert-success";
            }

            if (cartDetails != null)
            {
                cartDetails.Quantity += quantityInput;
                try
                {
                    _context.Update(cartDetails);
                    await _context.SaveChangesAsync();

                    TempData["SuccesQuantityInput"] = "The product was added to cart";
                    TempData["success"]             = "alert rounded shadow alert-success";
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CartDetailsExists(cartDetails.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
            }


            return(RedirectToAction("Details", new { ID = id }));
        }
Ejemplo n.º 12
0
        public async Task <IActionResult> UseVaucherAsync(string vaucherInput)
        {
            var cart = await GetCurrentUserCartAsync();

            if (cart == null)
            {
                return(NotFound());
            }
            var customer = await GetCustomerByUserIdAsync();

            if (customer == null)
            {
                return(NotFound());
            }
            var vaucher = await _context.Vauchers.FirstOrDefaultAsync(c => c.CustomerId == customer.Id &&
                                                                      c.IsUsed == false &&
                                                                      c.Code == vaucherInput);

            if (vaucher == null)
            {
                TempData["error"] = "Vaucher Invalid";
                return(RedirectToAction("Index"));
            }


            if (vaucher.Value != null)
            {
                if (cart.DiscountVal == null)
                {
                    cart.DiscountVal = vaucher.Value;
                }
                else
                {
                    cart.DiscountVal += vaucher.Value;
                }
            }

            if (vaucher.Percentage != null)
            {
                if (cart.DiscountPer == null)
                {
                    cart.DiscountPer = vaucher.Percentage;
                }
                else
                {
                    cart.DiscountPer += vaucher.Percentage;
                }
            }
            await CalculaTetoatalAsync();



            if (ViewBag.total < 0)
            {
                TempData["error"] = "The value of discount can`t be greater then subtotal!";
                return(RedirectToAction("Index"));
            }

            if (cart.DiscountPer > 50)
            {
                TempData["error"] = "The value of percentage discount can`t be greater then 50!";
                return(RedirectToAction("Index"));
            }


            try
            {
                _context.Update(cart);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CartsExists(cart.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            vaucher.IsUsed = true;

            try
            {
                _context.Update(vaucher);

                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!VaucherExists(vaucher.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            TempData["succes"] = "Vaucher was applied";
            return(RedirectToAction("index"));
        }