public ActionResult Purchase(string id) { var stock = db.Stocks.Include("Product").FirstOrDefault(x => x.StockID.ToString() == id); if (stock == null) { throw new Exception("Errore!! Lo Stock selezionato non esiste"); } var username = User.Identity.GetUserName(); var machineReservation = new MachineReservation { ConsumerID = username, IsDeleted = false, IsLocked = false, MachineID = stock.MachineID, MachineReservationID = Guid.NewGuid(), RequestDate = DateTime.Now, Status = MachineReservationStatus.Reserved }; var transactionDetail = new TransactionDetail { ProductID = stock.ProductID, Quantity = 1, TotalAmount = stock.Product.Price, TransactionDetailID = Guid.NewGuid(), UnitPrice = stock.Product.Price }; var user = db.Users.FirstOrDefault(x => x.UserName == username); var shippingDetail = new ShippingDetail { Address = "Via Cantalupo, 4", City = "Cesenatico", Email = user.Email, FirstName = user.FirstName, LastName = user.LastName, Mobile = user.PhoneNumber, PostalCode = "47042", Province = "FC", ShippingID = Guid.NewGuid() }; var transaction = new Transaction { CashChannel = CashChannelType.Unknown, ConsumerID = username, MachineID = stock.MachineID, MachineReservation = machineReservation, Status = TransactionStatus.InProgress, TotalAmount = stock.Product.Price, TransactionDate = DateTime.Now, Type = TransactionType.Totem, TransactionID = Guid.NewGuid(), TransactionDetail = transactionDetail, ShippingDetail = shippingDetail }; db.Transactions.Add(transaction); db.SaveChanges(); MessagesRepository _messageRepository = new MessagesRepository(); _messageRepository.SetToReaded(); // Invoke the direct method on the device, passing the payload serviceClient = ServiceClient.CreateFromConnectionString(connectionString); // Invoke the direct method asynchronously and get the response from the simulated device. var getRfidMessage = new Microsoft.Azure.Devices.Message(Encoding.ASCII.GetBytes("{ \"Method\": \"Purchase\", \"transactionid\": \"" + transaction.TransactionID + "\", \"total\": \"" + stock.Product.Price + "\", \"selection\": \"" + stock.MachineKeyBoardNumber + "\" }")); serviceClient.SendAsync("raspberry", getRfidMessage); return(View(stock)); }
/** * Ajax return Reservation in Day when select day in Homepage */ public IActionResult GetMachineHaveReservationInDay(string day) { var langRequest = Request.HttpContext.Features.Get <IRequestCultureFeature>(); var culture = langRequest.RequestCulture.Culture; var _lang = culture.ToString(); DateTime start = DateTime.Parse(day); var startTime = new DateTime(start.Year, start.Month, start.Day, 0, 0, 0); var endTime = startTime.AddDays(1); var reservations = _db.Reservations.Where(r => r.StartTime >= startTime && r.EndTime <= endTime) .Join( _db.Users, x => x.UserId, user => user.UserId, (res, user) => new { userId = user.UserId, groupId = user.GroupId, userName = (_lang == "en-US") ? user.UserNameEn : user.UserNameJp, machineId = res.MachineId, //userName = user.UserNameJp, reserveId = res.ReserveId, startTime = res.StartTime, endTime = res.EndTime, color = res.Color, price = res.Price } ).Join( _db.Machines, rs => rs.machineId, machine => machine.MachineId, (rs, machine) => new { machineName = (_lang == "en-US") ? machine.MachineNameEn :machine.MachineNameJp, //machineName = machine.MachineNameJp, userId = rs.userId, groupId = rs.groupId, userName = rs.userName, machineId = rs.machineId, reserveId = rs.reserveId, startTime = rs.startTime, endTime = rs.endTime, color = rs.color, price = rs.price } ) .Join( _db.Groups, rs => rs.groupId, group => group.GroupId, (rs, group) => new { machineName = rs.machineName, groupName = group.GroupNameEn, userId = rs.userId, userName = rs.userName, machineId = rs.machineId, reserveId = rs.reserveId, startTime = rs.startTime, endTime = rs.endTime, color = rs.color, price = rs.price } ); var asc_data = reservations.OrderBy(o => o.machineId).ThenBy(o => o.startTime).ToList(); List <MachineReservation> ListAsc = new List <MachineReservation>(); List <int> listId = new List <int>(); foreach (var item in asc_data) { if (!listId.Contains(item.machineId)) { listId.Add(item.machineId); MachineReservation mr = new MachineReservation(); mr.MachineId = item.machineId; mr.MachineName = item.machineName; mr.Events = null; ListAsc.Add(mr); } } foreach (var mr in ListAsc) { List <Event> le = new List <Event>(); foreach (var item in asc_data) { if (mr.MachineId == item.machineId) { Event e = new Event(); e.MachineId = item.machineId; e.MachineName = item.machineName; e.ReserveId = item.reserveId; e.UserId = item.userId; e.UserName = item.userName; e.GroupName = item.groupName; e.StartTime = item.startTime; e.EndTime = item.endTime; e.Color = item.color; e.Price = item.price; le.Add(e); } } mr.Events = le; } var desc_data = reservations.OrderByDescending(o => o.machineId).ThenBy(o => o.startTime).ToList(); List <MachineReservation> ListDesc = new List <MachineReservation>(); List <int> listIddesc = new List <int>(); foreach (var item in desc_data) { if (!listIddesc.Contains(item.machineId)) { listIddesc.Add(item.machineId); MachineReservation mr = new MachineReservation(); mr.MachineId = item.machineId; mr.MachineName = item.machineName; mr.Events = null; ListDesc.Add(mr); } } foreach (var mr in ListDesc) { List <Event> le = new List <Event>(); foreach (var item in asc_data) { if (mr.MachineId == item.machineId) { Event e = new Event(); e.MachineId = item.machineId; e.MachineName = item.machineName; e.ReserveId = item.reserveId; e.UserId = item.userId; e.UserName = item.userName; e.GroupName = item.groupName; e.StartTime = item.startTime; e.EndTime = item.endTime; e.Color = item.color; e.Price = item.price; le.Add(e); } } mr.Events = le; } ViewBag.AscData = ListAsc; ViewBag.DescData = ListDesc; return(PartialView("_Home_Machine_Reservation")); }