Exemplo n.º 1
0
        public async Task <IActionResult> CreatePercentage(PercentageModelView vaucher)
        {
            if (ModelState.IsValid)
            {
                string code = await CreatePerCodeAsync(vaucher.Percentage);

                Vauchers perVaucher = new Vauchers();
                perVaucher.Code       = code;
                perVaucher.Percentage = vaucher.Percentage;
                Employees emp = await GetEmployeeByUserIdAsync();

                perVaucher.CreatedBy  = emp.Id;
                perVaucher.CustomerId = vaucher.CustomerId;
                perVaucher.IsUsed     = false;
                _context.Add(perVaucher);
                await _context.SaveChangesAsync();

                TempData["Styling"] = "alert rounded shadow alert-success";
                TempData["Msg"]     = "Voucher " + perVaucher.Code + " added succesfully!";
                return(RedirectToAction(nameof(Index)));
            }
            TempData["Styling"] = "alert rounded shadow alert-danger";
            TempData["Msg"]     = "Percentage must be > 0 and < 50!";

            return(RedirectToAction("index"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("Id,Name")] Categories categories)
        {
            if (ModelState.IsValid)
            {
                _context.Add(categories);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(categories));
        }
        public async Task <IActionResult> Create([Bind("Id,Details")] Promotions promotions)
        {
            if (ModelState.IsValid)
            {
                _context.Add(promotions);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(promotions));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Create([Bind("Id,Status")] OrderStatus orderStatus)
        {
            if (ModelState.IsValid)
            {
                _context.Add(orderStatus);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(orderStatus));
        }
Exemplo 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"));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> Create([Bind("Id,FullName,email,Adress,CityId,JobId,Salary")] Employees employees)
        {
            var user = await userManager.FindByNameAsync(employees.email);

            ViewData["CityId"] = new SelectList(_context.Cities, "Id", "Name");
            ViewData["JobId"]  = new SelectList(roleManager.Roles);
            if (user == null)
            {
                ViewBag.Class    = "alert alert-danger";
                ViewBag.ErrorMsg = " This email dose not corespond to an user account.";
                return(View());
            }
            else
            {
                employees.UserId = user.Id;
                if (_context.Employees.FirstOrDefault(e => e.email == employees.email) != null)
                {
                    ViewBag.Class    = "alert alert-danger";
                    ViewBag.ErrorMsg = " This email is already linked to an employee acount.";
                    return(View());
                }
                else
                {
                    if (ModelState.IsValid)
                    {
                        try
                        {
                            var result = await userManager.AddToRoleAsync(user, employees.JobId);
                        }
                        catch (Exception)
                        {
                            ViewBag.Class    = "alert alert-danger";
                            ViewBag.ErrorMsg = "Role not valid";
                            View(employees);
                        }
                        _context.Add(employees);
                        await _context.SaveChangesAsync();

                        return(RedirectToAction(nameof(Index)));
                    }


                    return(View(employees));
                }
            }
        }
Exemplo n.º 7
0
        public async Task <IActionResult> Create(ProductsViewModel products)
        {
            if (ModelState.IsValid)
            {
                string uniquFileName = null;
                if (products.photo != null)
                {
                    string uploadsFolder = Path.Combine(hostingEnviroment.WebRootPath, "images");
                    uniquFileName = Guid.NewGuid().ToString() + "_" + products.Name + "_" + products.photo.FileName;
                    string filePath = Path.Combine(uploadsFolder, uniquFileName);
                    products.photo.CopyTo(new FileStream(filePath, FileMode.Create));
                }
                Products product = new Products
                {
                    ProductCode    = products.ProductCode,
                    Name           = products.Name,
                    Weight         = products.Weight,
                    UmId           = products.UmId,
                    InitialPrice   = products.InitialPrice,
                    Discount       = products.Discount,
                    FinalPrice     = products.FinalPrice,
                    Quantity       = products.Quantity,
                    Details        = products.Details,
                    UnitsPerBox    = products.UnitsPerBox,
                    CateogryId     = products.CateogryId,
                    ManufacturerId = products.ManufacturerId,
                    Image          = uniquFileName
                };

                product.FinalPrice = product.InitialPrice - product.Discount;
                _context.Add(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction("details", new { id = product.Id }));
            }

            ViewData["CateogryId"]     = new SelectList(_context.Categories, "Id", "Name", products.CateogryId);
            ViewData["ManufacturerId"] = new SelectList(_context.Manufacturers, "Id", "Name", products.ManufacturerId);
            ViewData["UmId"]           = new SelectList(_context.Um, "Id", "Name", products.UmId);
            return(View(products));
        }
Exemplo n.º 8
0
        public async Task <IActionResult> Create([Bind("Id,UserId,ContactPerson,PhoneNumber,Mail,Company,Cui,RegistrationNumber,Adress,CityId,Bank,Iban,TypeId")] Customers customers)
        {
            var userId = getUserID();

            customers.UserId = userId;

            ModelState.Remove("UserId");
            if (ModelState.IsValid)
            {
                var user = await userManager.FindByIdAsync(userId);

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

                if (!result.Succeeded)
                {
                    foreach (IdentityError error in result.Errors)
                    {
                        ModelState.AddModelError("", error.Description);
                    }
                }
            }
            customers.IsAccepted = false;
            if (ModelState.IsValid)
            {
                _context.Add(customers);
                await _context.SaveChangesAsync();

                Customers customer = await GetCustomerByUserIdAsync();

                Carts cart = new Carts();
                cart.CutomerId = customer.Id;
                _context.Add(cart);
                await _context.SaveChangesAsync();

                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));
        }
Exemplo n.º 9
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));
        }
Exemplo n.º 10
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"));
        }
Exemplo n.º 11
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"));
        }
Exemplo n.º 12
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 }));
        }
Exemplo n.º 13
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"));
        }