public IActionResult GetProduct(int Phoneid, string[] modelId, string[] models) { var data = new List <Products>(); string[] modelsexist = new string[] { }; int[] modelsExists = new int[] { }; if (modelId[0] != null) { var ids = modelId[0].Split(','); int[] idslist = Array.ConvertAll(ids, i => int.Parse(i)); string CompanyName = util.GetAllCompany().FirstOrDefault(x => x.Phoneid == Phoneid).Com_name; var list = util.getModelList(Phoneid).Where(x => idslist.Contains(x.modelId)).ToList(); if (!String.IsNullOrEmpty(models[0])) { modelsexist = models[0].Split(','); modelsExists = Array.ConvertAll(modelsexist, i => int.Parse(i)); ViewData["Index"] = modelsExists.Length; } else { ViewData["Index"] = 0; } var Prices = util.Price(idslist); for (int i = 0; i < list.Count(); i++) { if (modelsExists.Length == 0 || !modelsExists.Contains(list[i].modelId)) { Products p = new Products() { modelId = list[i].modelId, model_name = list[i].model_name, Phoneid = Phoneid, com_Name = CompanyName, price = Prices.FirstOrDefault(x => x.Item1 == list[i].modelId).Item2, Quantity = 1, }; data.Add(p); } } } return(PartialView("_DisplayOrder", data)); }
public async Task <List <Tuple <int, bool> > > addOrder(OrderViewModel c, IUrlHelper Url) { List <Tuple <int, bool> > quantityCheck = util.checkingquantity(c.Products, c.store_id); if (quantityCheck.Any(x => x.Item2 == false)) { return(quantityCheck); } if (c.CustRef > 0 && Convert.ToString(c.CustRef) != String.Empty) { var result = context.Customer.Select(x => new { x.cus_id, x.CustRef }).FirstOrDefault(x => x.CustRef == c.CustRef); if (result != null) { c.cus_id = result.cus_id; } else { Customer cus = new Customer() { cus_name = c.cus_name, cus_phone = c.cus_phone, CustRef = c.CustRef, }; if (c.Address != null) { cus.Address = new Address() { City = util.getCities().FirstOrDefault(x => x.id == c.CityId).city, StreetAddress = c.Address.StreetAddress }; } context.Customer.Add(cus); context.SaveChanges(); c.cus_id = cus.cus_id; } } var prices = util.Price(c.Products.Select(x => x.modelId).ToArray()); Order model = new Order() { Date = DateTime.Now, store_id = c.store_id, cus_id = c.cus_id, status = Status.Pending, PaymentMethod = c.Method, Products = c.Products.Select(x => new Model.Order.Product() { //Phoneid = x.Phoneid, modelId = x.modelId, Quantity = x.Quantity, price = prices.FirstOrDefault(p => p.Item1 == x.modelId).Item2 *x.Quantity, }).ToList() }; if (c.TakenBy != null) { model.TakenBy = c.TakenBy; } if (c.Method == PaymentMethods.Stripe) { if (c.stripeToken == null) { return(new List <Tuple <int, bool> >() { new Tuple <int, bool>(-2, false) }); } double a = model.Products.Sum(x => x.price) * 0.63; //double per = 2.9 / 100 * a; var Options = new ChargeCreateOptions { Amount = Convert.ToInt32(a), Currency = "usd", Description = "Customer Ref: " + c.CustRef, Source = c.stripeToken }; var service = new ChargeService(); Charge charge = service.Create(Options); if (charge.BalanceTransactionId == null || charge.Status.ToLower() != "succeeded") { return(new List <Tuple <int, bool> >() { new Tuple <int, bool>(-2, false) }); } model.Charges = new List <OrderCharges>() { new OrderCharges() { ChargeId = charge.Id, priority = 1, } }; } context.Order.Add(model); context.SaveChanges(); if (c.orderStatus == Status.Completed) { bool res = UpdateStatus(protector.Protect(model.order_id.ToString()), Status.Completed, c.TakenBy); if (!res) { return new List <Tuple <int, bool> >() { new Tuple <int, bool>(-1, false) } } ; } string StoreName = util.GetAllStores().FirstOrDefault(x => x.store_id == c.store_id).StoreName; c.StoreName = StoreName; var users = Usermanager.Users.Where(x => x.store_id == c.store_id).ToList(); NotificationsViewModel n = new NotificationsViewModel(); n.heading = "Order #" + model.order_id; n.Text = "Order With Status " + c.orderStatus + " is Placed"; n.Url = Url.Action("Details", "Order", new { id = model.order_id }); n.read = false; n.When = DateTime.Now; //await _hubContext.Clients.All.SendAsync("RecieveNotification", n); await _hubContext.Clients.Groups(StoreName).SendAsync("RecieveNotification", n); foreach (var em in users) { n.UserId = em.Id; await util.AddNotification(n); } context.SaveChanges(); return(new List <Tuple <int, bool> >() { new Tuple <int, bool>(model.order_id, true) });; }