public ActionResult CreateOrder(string note)
        {
            if (Session["Cart"]!=null)
            {
                var cartList = (List<Cart>)Session["Cart"];
                var user = (Account)Session["User"];
                var order = new DrugOrder();
                var orderDetails = new DrugOrderDetail();
                var drugstore =
                    unitOfWork.DrugStoreRepository.Get(b => b.OwnerID == user.AccountID).SingleOrDefault();
                order.DrugstoreID = drugstore.DrugstoreID;
                double totalprice = 0;
                double actualvalue;
                for (int i = 0; i < cartList.Count; i++)
                {
                    orderDetails = new DrugOrderDetail();
                    orderDetails.DrugId = cartList[i].Drug.DrugID;
                    orderDetails.Quantity = cartList[i].Quantity;
                    orderDetails.DeliveryQuantity = cartList[i].Quantity;
                    orderDetails.UnitID = cartList[i].Unit.UnitId;
                    orderDetails.UnitPrice = cartList[i].Drug.Prices.Where(b => b.UnitID == orderDetails.UnitID).Select(b => b.UnitPrice).SingleOrDefault() * orderDetails.Quantity *
                        (100 - cartList[i].Drug.DiscountRates.Where(b => b.DrugstoreTypeID == drugstore.DrugstoreTypeID).Select(b => b.Discount).SingleOrDefault()) / 100;
                    totalprice = totalprice +
                        cartList[i].Drug.Prices.Where(b => b.UnitID == orderDetails.UnitID).Select(b => b.UnitPrice).SingleOrDefault() * orderDetails.Quantity *
                        (100 - cartList[i].Drug.DiscountRates.Where(b => b.DrugstoreTypeID == drugstore.DrugstoreTypeID).Select(b => b.Discount).SingleOrDefault()) / 100;
                    order.DrugOrderDetails.Add(orderDetails);
                }
                order.DateOrder = DateTime.Now;
                order.TotalPrice = totalprice;

                order.IsActive = true;
                order.Note = note;
                order.Status = (int)Status.StatusEnum.NotApprove;
                bool check = unitOfWork.DrugOrderRepository.Insert(order);
                unitOfWork.DrugOrderRepository.SaveChanges();
                unitOfWork.DrugOrderDetailRepository.SaveChanges();
                //drugstore.Debt = drugstore.Debt + totalprice;
                unitOfWork.DrugStoreRepository.Update(drugstore);
                unitOfWork.DrugStoreRepository.SaveChanges();
                if (check)
                {
                    Session["Cart"] = null;
                }
            }
            return View();
        }
        public ActionResult CreateSchedule(string deliveryManID)
        {
            List<int> listOrderID = new List<int>();
            if (Session["ListOrderID"] != null)
            {
                var schedule = new DeliverySchedule();
                listOrderID = (List<int>)Session["ListOrderID"];
                schedule.DeliveryMan = unitOfWork.DeliverymanRepository.GetByID(int.Parse(deliveryManID));
                //schedule.DueDate = DateTime.Parse(dueDate);
                schedule.CreateDate = DateTime.Now;
                var scheduleDetail = new DeliveryScheduleDetail();
                var order = new DrugOrder();
                Point startPoint = new Point();
                startPoint.x = 10.7972388;
                startPoint.y = 106.6803467;
                List<Point> listOrder = new List<Point>();
                for (int i = 0; i < listOrderID.Count; i++)
                {
                    Point drugOrderPoint = new Point();
                    drugOrderPoint.DrugOrder = unitOfWork.DrugOrderRepository.GetByID(listOrderID[i]);
                    var drugstoreCoordinate = drugOrderPoint.DrugOrder.Drugstore.Coordinate;
                    string[] drugstoreCoordinates = drugstoreCoordinate.Split(',');
                    drugOrderPoint.x = double.Parse(drugstoreCoordinates[0]);
                    drugOrderPoint.y = double.Parse(drugstoreCoordinates[1]);
                    listOrder.Add(drugOrderPoint);
                }
                var tempPoint = new Point();
                List<Point> resuledList = new List<Point>();
                bool flag = true;
                tempPoint = startPoint;
                for (int i = listOrder.Count - 1; i >= 0; i--)
                {
                        if (listOrder.Count > 0)
                        {
                            tempPoint = NearestPoint(tempPoint, listOrder);
                            resuledList.Add(tempPoint);
                            listOrder.Remove(tempPoint);
                        }
                }
                for (int i = 0; i < resuledList.Count; i++)
                {
                    scheduleDetail = new DeliveryScheduleDetail();
                    order = new DrugOrder();
                    scheduleDetail.DrugOrderID = resuledList[i].DrugOrder.DrugOrderID;
                    scheduleDetail.Status =(int) Status.StatusEnum.Inprogress;
                    order = resuledList[i].DrugOrder;
                    order.Status = (int)Status.StatusEnum.Inprogress;
                    unitOfWork.DrugOrderRepository.Update(order);
                    schedule.DeliveryScheduleDetails.Add(scheduleDetail);
                }
                schedule.Status = (int)Status.StatusEnum.Inprogress;
                bool check = unitOfWork.DeliveryScheduleRepository.Insert(schedule);
                if (check)
                {
                    Session["ListOrderID"] = null;
                }
                unitOfWork.DeliveryScheduleRepository.SaveChanges();
                unitOfWork.DeliveryScheduleDetailsRepository.SaveChanges();
                unitOfWork.DrugOrderRepository.SaveChanges();
            }

            return Json(listOrderID);
        }
        public ActionResult UpdateStatus(int deliveryScheduleID)
        {
            var deliverySchedule = unitOfWork.DeliveryScheduleRepository.GetByID(deliveryScheduleID);
            var listDeliveryScheduleDetails = deliverySchedule.DeliveryScheduleDetails;
            deliverySchedule.Status = (int)Status.StatusEnum.Complete;
            var drugOrder = new DrugOrder();
            for (int i = 0; i < listDeliveryScheduleDetails.Count; i++)
            {
                var item = listDeliveryScheduleDetails.ElementAt(i);
                drugOrder = new DrugOrder();
                drugOrder = unitOfWork.DrugOrderRepository.GetByID(item.DrugOrderID);
                var drugstore = drugOrder.Drugstore;
                drugOrder.Status = (int)Status.StatusEnum.Complete;
                unitOfWork.DrugOrderRepository.Update(drugOrder);
                unitOfWork.DrugOrderRepository.SaveChanges();
                var payment = new Payment();
                var paymentHistory = unitOfWork.PaymentRepository.Get(b => b.DrugstoreID == drugOrder.DrugstoreID);
                if (paymentHistory.Count()==0)
                {
                    payment.DrugstoreID = drugOrder.DrugstoreID;
                    payment.Amount = drugOrder.TotalPrice;
                    payment.Balance = 0-drugOrder.TotalPrice;
                    drugstore.Debt = 0-drugOrder.TotalPrice;
                }
                else
                {
                    payment.DrugstoreID = drugOrder.DrugstoreID;
                    payment.Amount = drugOrder.TotalPrice;
                    payment.Balance = paymentHistory.Last().Balance - drugOrder.TotalPrice;
                    drugstore.Debt =drugstore.Debt- drugOrder.TotalPrice;
                }
                payment.Date = DateTime.Now;
                payment.PaymentType = true;
                payment.IsActive = true;
                unitOfWork.DrugStoreRepository.Update(drugstore);
                unitOfWork.DrugStoreRepository.SaveChanges();
                unitOfWork.PaymentRepository.Insert(payment);
                unitOfWork.PaymentRepository.SaveChanges();
            }

            unitOfWork.DeliveryScheduleRepository.Update(deliverySchedule);
            unitOfWork.DeliveryScheduleRepository.SaveChanges();
            return null;
        }
 public ActionResult DeleteSchedule(int deliveryScheduleID)
 {
     var deliverySchedule = unitOfWork.DeliveryScheduleRepository.GetByID(deliveryScheduleID);
     var listDeliveryScheduleDetails = deliverySchedule.DeliveryScheduleDetails;
     var drugOrder = new DrugOrder();
     for (int i = listDeliveryScheduleDetails.Count - 1; i >= 0; i--)
     {
         var item = listDeliveryScheduleDetails.ElementAt(i);
         drugOrder = new DrugOrder();
         drugOrder = unitOfWork.DrugOrderRepository.GetByID(item.DrugOrderID);
         drugOrder.Status = (int)Status.StatusEnum.Approved;
         unitOfWork.DrugOrderRepository.Update(drugOrder);
         unitOfWork.DrugOrderRepository.SaveChanges();
     }
     deliverySchedule.Status = (int)Status.StatusEnum.Deleted;
     unitOfWork.DeliveryScheduleRepository.Update(deliverySchedule);
     unitOfWork.DeliveryScheduleRepository.SaveChanges();
     unitOfWork.DeliveryScheduleDetailsRepository.SaveChanges();
     return null;
 }