public ActionResult CompleteItem(int deliveryDetailID)
 {
     var deliveryDetails = unitOfWork.DeliveryScheduleDetailsRepository.GetByID(deliveryDetailID);
     deliveryDetails.Status = (int)Status.StatusEnum.Complete;
     var drugOrder = deliveryDetails.DrugOrder;
     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();
     var deliverySchedule = deliveryDetails.DeliverySchedule;
     if (deliverySchedule.DeliveryScheduleDetails.Count(b => b.Status==(int)Status.StatusEnum.Inprogress)>0)
     {
         return RedirectToAction("ScheduleDetail", new { scheduleID = deliverySchedule.DeliveryScheduleID });
     }
     else
     {
         deliverySchedule.Status = (int) Status.StatusEnum.Complete;
         unitOfWork.DeliveryScheduleRepository.SaveChanges();
         return RedirectToAction("List");
     }
 }
        public ActionResult Payment(int DrugstoreID, string amount, string paymentType)
        {
            var payment = new Payment();
            payment.DrugstoreID = DrugstoreID;
            var paymentHistory = unitOfWork.PaymentRepository.Get(b => b.DrugstoreID == DrugstoreID).ToList();
            var drugstore = unitOfWork.DrugStoreRepository.Get(b => b.DrugstoreID == DrugstoreID).Single();
            //if (paymentHistory.Count() == 0)
            //{
            //    payment.DrugstoreID = DrugstoreID;
            //    payment.Amount = float.Parse(amount);
            //    payment.Balance = float.Parse(amount);
            //}
            //else
            //{
            //    payment.DrugstoreID = DrugstoreID;
            //    payment.Amount = float.Parse(amount);
            //    payment.Balance = paymentHistory.Last().Balance + float.Parse(amount);
            //}
            if (paymentHistory.Count() > 0)
            {
                if (paymentType == "2")
                {
                    payment.DrugstoreID = DrugstoreID;
                    payment.Amount = float.Parse(amount);
                    payment.Balance = paymentHistory.Last().Balance + float.Parse(amount);
                    payment.PaymentType = true;
                    drugstore.Debt = payment.Balance;

                }
                else if (paymentType == "1")
                {
                    payment.DrugstoreID = DrugstoreID;
                    payment.Amount = float.Parse(amount);
                    payment.Balance = paymentHistory.Last().Balance - float.Parse(amount);
                    payment.PaymentType = false;
                    drugstore.Debt = payment.Balance;
                }

            }
            else
            {
                if (paymentType == "2")
                {
                    payment.DrugstoreID = DrugstoreID;
                    payment.Amount = float.Parse(amount);
                    payment.Balance = 0 + float.Parse(amount);
                    payment.PaymentType = true;
                    drugstore.Debt = payment.Balance;
                }
                else if (paymentType == "1")
                {
                    payment.DrugstoreID = DrugstoreID;
                    payment.Amount = float.Parse(amount);
                    payment.Balance = 0 - float.Parse(amount);
                    payment.PaymentType = false;
                    drugstore.Debt = payment.Balance;
                }
            }

            payment.IsActive = true;
            payment.Date = DateTime.Now;
            unitOfWork.DrugStoreRepository.Update(drugstore);
            unitOfWork.DrugStoreRepository.SaveChanges();
            unitOfWork.PaymentRepository.Insert(payment);
            unitOfWork.PaymentRepository.SaveChanges();
            return new EmptyResult();
            // return RedirectToAction("DrugstorePayment", "Staff");
        }
        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;
        }