Exemple #1
0
        public IActionResult PlaceOrder(OrderFinalDetails details)
        {
            service = new ProductserviceController();
            int invoiceId = service.PlaceOrder(details);

            return(Ok(invoiceId));
        }
        public ProductViewModelCart[] GetOrderDetails(string paymode, out int InvoiceId)
        {
            InvoiceId = 0;
            string json = context.Session.GetString("Cart");

            ProductViewModelCart[] result = JsonConvert.DeserializeObject <ProductViewModelCart[]>(json);
            int customerId = Convert.ToInt32(context.Session.GetString("cid"));

            int Tamount = 0;

            foreach (var p in result)
            {
                Tamount += (int)p.Price * p.Quantity;
            }

            OrderFinalDetails obj = new OrderFinalDetails();

            obj.Cid         = customerId;
            obj.TotalAmount = Tamount;
            obj.PaymentMode = paymode;
            obj.Products    = result;

            string              json1    = JsonConvert.SerializeObject(obj);
            HttpContent         content  = new StringContent(json1, Encoding.UTF8, "application/json");
            HttpResponseMessage response = client.PostAsync("ProductOrder/PlaceOrder", content).Result;

            if (response.IsSuccessStatusCode)
            {
                InvoiceId = Convert.ToInt32(response.Content.ReadAsStringAsync().Result);
            }
            return(result); // product array
        }
        public int PlaceOrder(OrderFinalDetails details)
        {
            Ordertable order = new Ordertable();

            order.TotalAmount = details.TotalAmount;
            order.CustomerId  = details.Cid;
            order.OrderDate   = DateTime.Now;
            order.CategoryId  = details.Products[0].CategoryId;

            context.Ordertable.Add(order);
            context.SaveChanges();
            //~~~~~~~~~~~~~~~~~~OrderDetails table~~~~~~~~~~~~~~~~~~~~~~

            Orderdetails[] products = new Orderdetails[details.Products.Length];
            for (var i = 0; i < details.Products.Length; i++)
            {
                products[i] = new Orderdetails()
                {
                    OrderId   = order.OrderId,
                    Price     = details.Products[i].Price,
                    Quantity  = details.Products[i].Quantity,
                    ProductId = details.Products[i].ProductId
                };
                // context.Orderdetails.Add(products[i]);
            }
            context.Orderdetails.AddRange(products);
            //~~~~~~~~~~~~~~~~~~~~~payment table~~~~~~~~~~~~~~~~~~~~~

            Payment payment = new Payment();

            payment = new Payment()
            {
                OrderId     = order.OrderId,
                Amount      = (decimal)order.TotalAmount.Value,
                PaymentDate = DateTime.Now,
                Mode        = details.PaymentMode
            };

            context.Payment.Add(payment);
            Product p;

            for (int i = 0; i < details.Products.Length; i++)
            {
                p           = context.Product.SingleOrDefault(c => c.ProductId == details.Products[i].ProductId);
                p.Quantity -= details.Products[i].Quantity;
            }

            context.SaveChanges();
            return(payment.InvoiceId);
        }