Exemplo n.º 1
0
        public async Task <int> addPurchasing(PurchasingViewModel c, IUrlHelper Url)
        {
            bool res = false;
            // var price = context.BrandModel.Select(x => new { x.Price, x.modelId }).FirstOrDefault(x => x.modelId == c.modelId);
            Purchasing model = new Purchasing()
            {
                Date      = c.Date,
                Quantity  = c.Quantity,
                Amount    = c.Amount,
                modelId   = c.modelId,
                vendor_id = c.vendor_id,
                store_id  = c.store_id,
                takenBy   = c.takenBy,
            };

            context.Purchasings.Add(model);
            context.SaveChanges();
            res = util.updateSingleQuantity(c.modelId, c.Quantity, c.store_id, "Add");
            if (res == false)
            {
                return(0);
            }
            string StoreName = util.GetAllStores().FirstOrDefault(x => x.store_id == c.store_id).StoreName;
            var    users     = UserManager.Users.Where(x => x.store_id == c.store_id).ToList();

            NotificationsViewModel n = new NotificationsViewModel();

            n.heading = "Purchasing #" + model.purchase_id;
            n.Text    = "Items Purchased By " + c.takenBy;
            n.Url     = Url.Action("Details", "Purchasing", new { id = model.purchase_id });
            n.read    = false;
            n.When    = DateTime.Now;
            await _hubContext.Clients.Groups(StoreName).SendAsync("RecieveNotification", n);

            foreach (var em in users)
            {
                n.UserId = em.Id;
                await util.AddNotification(n);
            }
            context.SaveChanges();
            return(model.purchase_id);
        }
Exemplo n.º 2
0
        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)
            });;
        }