public UpdateDeliveryResult UpdateDelivery(string productId, string orderId, int quantity, string user, string token)
        {
            var log = new MongoHistoryAPI()
            {
                APIUrl     = "/api/manageorder/updatedelivery",
                CreateTime = DateTime.Now,
                Sucess     = 1
            };

            var result = new UpdateDeliveryResult()
            {
                id  = "1",
                msg = "success"
            };

            try
            {
                if (!mongoHelper.checkLoginSession(user, token))
                {
                    throw new Exception("Wrong token and user login!");
                }

                HaiStaff staff = db.HaiStaffs.Where(p => p.UserLogin == user).FirstOrDefault();
                if (staff == null)
                {
                    throw new Exception("Sai thong tin nguoi dat");
                }


                var checkOrderProduct = db.OrderProducts.Where(p => p.OrderId == orderId && p.ProductId == productId).FirstOrDefault();

                if (checkOrderProduct == null)
                {
                    throw new Exception("Sai thong tin");
                }


                checkOrderProduct.QuantityFinish = quantity;

                checkOrderProduct.ModifyDate = DateTime.Now;

                db.Entry(checkOrderProduct).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();


                //
                var    order       = checkOrderProduct.HaiOrder;
                string deliveryStt = GetDeliveryStatus(order);
                var    status      = db.DeliveryStatus.Find(deliveryStt);
                order.DStatus = deliveryStt;

                result.deliveryStatus     = status.Name;
                result.deliveryStatusCode = status.Id;
                result.finish             = quantity;

                db.Entry(order).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();


                //
                // update process: nhan vien khoi tao
                var history = new OrderProductHistory()
                {
                    Id         = Guid.NewGuid().ToString(),
                    CreateDate = DateTime.Now,
                    Notes      = "Nhan vien cong ty cap nhat",
                    OrderId    = checkOrderProduct.OrderId,
                    ProductId  = checkOrderProduct.ProductId,
                    Quantity   = quantity,
                    StaffId    = staff.Id
                };

                db.OrderProductHistories.Add(history);
                db.SaveChanges();

                HaiUtil.SendNotifi("Đơn hàng " + order.Code, "Đã cập nhật số lượng giao " + HaiUtil.ConvertProductQuantityText(checkOrderProduct.ProductInfo.Quantity, quantity, checkOrderProduct.ProductInfo.Unit) +
                                   "\nCho sản phẩm " + checkOrderProduct.ProductInfo.PName, staff.UserLogin, db, mongoHelper);
            }
            catch (Exception e)
            {
                result.id  = "0";
                result.msg = e.Message;
                log.Sucess = 0;
            }

            log.ReturnInfo = new JavaScriptSerializer().Serialize(result);

            mongoHelper.createHistoryAPI(log);

            return(result);
        }
        public ActionResult UpdateDelivery(string orderId, string productId, int?can, int?box)
        {
            if (can == null || box == null)
            {
                return(Json(new { id = 0 }, JsonRequestBehavior.AllowGet));
            }

            var orderProduct = db.OrderProducts.Where(p => p.ProductId == productId && p.OrderId == orderId).FirstOrDefault();

            if (orderProduct == null)
            {
                return(Json(new { id = 0 }, JsonRequestBehavior.AllowGet));
            }

            var quantity = box + orderProduct.ProductInfo.Quantity * can;

            orderProduct.QuantityFinish = quantity;

            db.Entry(orderProduct).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();

            string stt = "";

            if (quantity == 0)
            {
                stt = "Chưa giao";
            }
            else if (quantity == orderProduct.Quantity)
            {
                stt = "Giao đủ";
            }
            else if (quantity > orderProduct.Quantity)
            {
                stt = "Giao nhiều hơn";
            }
            else if (quantity < orderProduct.Quantity)
            {
                stt = "Giao ít hơn";
            }

            var    order       = orderProduct.HaiOrder;
            string deliveryStt = GetDeliveryStatus(order);

            order.DStatus         = deliveryStt;
            db.Entry(order).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();

            // save history
            var haiStaff = db.HaiStaffs.Where(p => p.UserLogin == User.Identity.Name).FirstOrDefault();
            var history  = new OrderProductHistory()
            {
                Id         = Guid.NewGuid().ToString(),
                CreateDate = DateTime.Now,
                Notes      = "Quan ly cong ty cap nhat",
                OrderId    = orderProduct.OrderId,
                ProductId  = orderProduct.ProductId,
                Quantity   = quantity,
                StaffId    = haiStaff.Id
            };

            db.OrderProductHistories.Add(history);
            db.SaveChanges();


            Utitl.Send("Đơn hàng " + orderProduct.HaiOrder.Code, "Đã cập nhật số lượng giao " + HaiUtil.ConvertProductQuantityText(orderProduct.ProductInfo.Quantity, quantity, orderProduct.ProductInfo.Unit) +
                       "\nCho sản phẩm " + orderProduct.ProductInfo.PName, haiStaff.UserLogin, db, mongoHelp);

            return(Json(new { id = 1, money = (quantity * orderProduct.PerPrice).Value.ToString("C", Util.Cultures.VietNam), stt = stt }, JsonRequestBehavior.AllowGet));
        }