public ActionResult Create([Bind(Include = "Id,UserId,Paid,Value")] Bill bill)
        {
            if (ModelState.IsValid)
            {
                db.Bills.Add(bill);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(bill));
        }
        public ActionResult Create([Bind(Include = "Id,Model,RentPrice")] Machine machine)
        {
            if (ModelState.IsValid)
            {
                db.Machines.Add(machine);
                db.SaveChanges();
                return(RedirectToAction("Manage"));
            }

            return(View(machine));
        }
        public ActionResult Create([Bind(Include = "Id,UserId,MachineId,From,To,Status")] Command command)
        {
            if (ModelState.IsValid)
            {
                db.Commands.Add(command);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.MachineId = new SelectList(db.Machines, "Id", "Model", command.MachineId);
            return(View(command));
        }
Exemplo n.º 4
0
        public ActionResult AddToCommands(int cartId)
        {
            var         user        = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>().FindById(User.Identity.GetUserId());
            CartItem    cartItem    = db.CartItems.Find(cartId);
            Machine     machine     = db.Machines.Find(cartItem.MachineId);
            Command     command     = new Command();
            Bill        bill        = new Bill();
            BillCommand billCommand = new BillCommand();

            // validation of the disponibility
            if (validate(machine, cartItem.From, cartItem.To))
            {
                long cost     = 0;
                long Duration = DateTimeHelper.DateTimeHelper.LongDiff(cartItem.From, cartItem.To).Days + 1; // Same date -> diff = 0
                cost += machine.RentPrice * Duration;

                command.From      = cartItem.From;
                command.To        = cartItem.To;
                command.MachineId = cartItem.MachineId;
                command.UserId    = user.Id;
                command.Status    = null;

                // Create command and remove from cart
                db.Commands.Add(command);
                db.CartItems.Remove(cartItem);

                // Create bill
                bill.UserId = user.Id;
                bill.Value  = (int)cost;
                db.Bills.Add(bill);

                // Add commands to bill
                billCommand.BillId    = bill.Id;
                billCommand.CommandId = command.Id;
                db.BillCommands.Add(billCommand);

                // Save
                db.SaveChanges();
                return(View("AddToCommands"));
            }
            else // return to index if not valid
            {
                return(RedirectToAction("Index"));
            }
        }