Exemple #1
0
        // GET: Orders
        public ActionResult Index(DateTime?searchDate)
        {
            //var model = orderRepo.GetItems().Where(p => p.OrderStatus == OrderStatus.Behandlar);
            var model = orderRepo.GetItems();

            if (searchDate.HasValue)
            {
                model = model.Where(x => x.PlannedDeliveryDate == searchDate).ToArray();
            }
            return(View(model));
        }
Exemple #2
0
 // GET: OrderProducts/Create
 public ActionResult Create()
 {
     ViewBag.OrderId   = new SelectList(orderRepo.GetItems(), "Id", "Id");
     ViewBag.ProductId = new SelectList(productRepo.GetItems(), "Id", "Name");
     return(View());
 }
Exemple #3
0
        public ActionResult UploadJson(OrderUploadVM model)
        {
            byte[] data;

            if (model.File == null)
            {
                ModelState.AddModelError("NullFile", "Inget filnamn angivet");
                return(View());
            }

            using (MemoryStream ms = new MemoryStream())
            {
                model.File.InputStream.CopyTo(ms);
                data = ms.ToArray();
            }

            var json = Encoding.Default.GetString(data);

            JObject jCustomerOrder = JObject.Parse(json);

            var items = customerAddressRepo.GetItems();

            var cid = (string)jCustomerOrder["customerid"];
            var aid = (string)jCustomerOrder["addressid"];

            var customerAddress = customerAddressRepo.GetItems().Where(
                a => a.AddressType == AddressType.Leverans &&
                a.Address.AddressOrderId == aid &&
                a.Customer.CustomerOrderId == cid).SingleOrDefault();

            if (customerAddress == null)
            {
                ModelState.AddModelError("", $"Kundorderid: '{cid}' eller Adressorderid: '{aid}' är felaktigt.");
                return(View());
            }

            var customer = customerAddress.Customer;

            var allProducts = productRepo.GetItems();

            string   dStr = (string)jCustomerOrder["date"];
            DateTime dDate;

            if (dStr.Length < 1)
            {
                dDate = DateTime.Now;
            }
            else if (!DateTime.TryParse(dStr, out dDate))
            {
                ModelState.AddModelError("", $"Önskad leveransdatum: '{dStr}' är felaktigt.");
                return(View());
            }

            var order = new Order
            {
                CustomerId          = customerAddress.Customer.Id,
                AddressId           = customerAddress.Address.Id,
                OrderDate           = DateTime.Today,
                DesiredDeliveryDate = dDate,
                Comment             = (string)jCustomerOrder["comment"]
            };

            if (customer != null)
            {
                var firstPossibleDate = DateTime.Today.AddDays(customer.DaysToDelievery);
                order.PlannedDeliveryDate = (order.DesiredDeliveryDate.CompareTo(firstPossibleDate) <= 0) ? firstPossibleDate : order.DesiredDeliveryDate;
            }

            var jProducts = jCustomerOrder["products"].ToArray();

            foreach (var jProduct in jProducts)
            {
                var prodString = (string)jProduct["pno"];
                var product    = productRepo.GetItem(prodString);
                if (product == null)
                {
                    ModelState.AddModelError("", $"Produktorderid: '{prodString}' finns inte.");
                    return(View());
                }

                var  aStr = (string)jProduct["amount"];
                uint oa;
                if (!uint.TryParse(aStr, out oa))
                {
                    ModelState.AddModelError("", $"Beställt antal: '{aStr}' är felaktigt.");
                    return(View());
                }
                if (oa < 1)
                {
                    ModelState.AddModelError("", $"Beställt antal: '{aStr}' mindre än 1.");
                    return(View());
                }
                var orderProduct = new OrderProduct
                {
                    OrderId       = order.Id,
                    Comment       = (string)jProduct["comment"],
                    ProductId     = productRepo.GetItem((string)jProduct["pno"]).Id,
                    OrderedAmount = (int)oa
                };
                order.OrderProducts.Add(orderProduct);
            }

            var existingOrders = orderRepo.GetItems().Where(o => o.AddressId == order.AddressId && o.CustomerId == order.CustomerId);

            foreach (var item in existingOrders)
            {
                if (item.DesiredDeliveryDate.Equals(order.DesiredDeliveryDate) && item.Comment.Equals(order.Comment))
                {
                    ModelState.AddModelError("", "En order med samma kundorderid, adressorderid och önskat leveransdatum är redan registrerad.");
                    return(View());
                }
            }

            orderRepo.CreateItem(order);
            //foreach (var item in order.OrderProducts)
            //{
            //    orderProductRepo.CreateItem(item);
            //}
            //orderRepo.HandleOrder(order);
            return(RedirectToAction("Index", "Orders"));
        }