public async Task <IActionResult> Create([Bind("Id,SaleTime,EmployeeId,ProductId,ClientId, Quantity, TotalAmount")] Sale sale)
        {
            if (ModelState.IsValid)
            {
                //Subtrai a quantidade do produto em estoque
                var saleQuantity = sale.Quantity;
                var prod         = _context.Product.Where(p => p.Id == sale.ProductId).FirstOrDefault();
                if (saleQuantity <= prod.ProductQuantity)
                {
                    prod.ProductQuantity -= saleQuantity;
                    sale.TotalAmount      = prod.ProductPrice * saleQuantity;

                    _context.Add(sale);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                ModelState.AddModelError("", "A quantidade do pedido excede a quantidade em estoque");
            }

            ViewData["ClientId"]   = new SelectList(_context.Client, "Id", "Cpf", sale.ClientId);
            ViewData["EmployeeId"] = new SelectList(_context.Employee, "Id", "FirstName", sale.EmployeeId);
            ViewData["ProductId"]  = new SelectList(_context.Product, "Id", "ProductName", sale.ProductId);
            return(View(sale));
        }
        public async Task <IActionResult> Create(EmployeesViewModel model)
        {
            var salary = model.Employee.Salary;

            if (ModelState.IsValid)
            {
                string uniqueFileName = UploadedFile(model);

                Employee employee = new Employee
                {
                    Age            = model.Employee.Age,
                    DepartmentId   = model.Employee.DepartmentId,
                    FirstName      = model.Employee.FirstName,
                    Gender         = model.Employee.Gender,
                    Id             = model.Employee.Id,
                    LastName       = model.Employee.LastName,
                    Salary         = model.Employee.Salary,
                    ProfilePicture = uniqueFileName,
                };

                _context.Add(employee);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            //ViewData["DepartmentId"] = new SelectList(_context.Department, "Id", "DepartmentName", employee.DepartmentId);
            ViewData["DepartmentId"] = new SelectList(_context.Department, "Id", "DepartmentName", model.Employee.DepartmentId);
            //return View(employee);
            return(View(model));
        }
        public async Task <IActionResult> Create([Bind("Id,Cpf,Name,BirthDate,Gender,Phone,Email,SalesRecord,Cep,State,City,District,Address,Number,Complement")] Client client)
        {
            if (ModelState.IsValid)
            {
                _context.Add(client);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(client));
        }
        public async Task <IActionResult> Create([Bind("Id,DepartmentName")] Department department)
        {
            if (ModelState.IsValid)
            {
                _context.Add(department);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(department));
        }
Exemple #5
0
        public async Task <IActionResult> Create([Bind("Id,TypeProdName")] TypeProduct typeProduct)
        {
            if (ModelState.IsValid)
            {
                _context.Add(typeProduct);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(typeProduct));
        }
        public async Task <IActionResult> Create([Bind("Id,ProductName,ProductDescription,ProductPrice,ProductQuantity,TypeProductId")] Product product)
        {
            if (ModelState.IsValid)
            {
                _context.Add(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["TypeProductId"] = new SelectList(_context.TypeProduct, "Id", "TypeProdName", product.TypeProductId);
            return(View(product));
        }
        public virtual async Task <JsonResult> AddRemoveProduct([Bind("Id,ProductId, Quantity, Price")] Stock stock)
        {
            if (ModelState.IsValid)
            {
                int     quantityChange = stock.Quantity;
                decimal priceChange    = stock.Price;

                var prod = _context.Product.Where(p => p.Id == stock.ProductId).FirstOrDefault();

                //Se algum input foi preenchido
                if (quantityChange != 0 || priceChange > 0)
                {
                    if (quantityChange != 0)
                    {
                        prod.ProductQuantity += quantityChange;
                    }

                    if (priceChange > 0)
                    {
                        prod.ProductPrice = priceChange;
                    }

                    //Se o estoque foi alterado e está > 0
                    if (prod.ProductQuantity >= 0)
                    {
                        _context.Add(stock);
                        await _context.SaveChangesAsync();

                        return(Json(true));
                    }
                }
            }
            ViewData["ProductName"]     = new SelectList(_context.Product, "Id", "ProductName");
            ViewData["ProductQuantity"] = new SelectList(_context.Product, "Id", "ProductQuantity");
            ViewData["ProductPrice"]    = new SelectList(_context.Product, "Id", "ProductPrice");
            return(Json(false));
        }