public ActionResult CreateDispatchOut()
        {
            var entity = new DispatchOutEntity();

            ListDropdown();
            return(View(entity));
        }
        public ActionResult EditDispatchOut(int?id)
        {
            ListDropdown();

            var File = _iplfile.ViewDetailByDispatch(long.Parse(id.ToString()), true);

            if (File != null)
            {
                ViewBag.FileName = File.Name;
                ViewBag.FileId   = File.Id;
            }
            DispatchOutEntity entity = new DispatchOutEntity();

            if (id != null)
            {
                entity = _ipldispatchout.ViewDetail((int)id);
            }
            return(View("CreateDispatchOut", entity));
        }
        public void SaveFile(int DispatchId, int UserId, DispatchOutEntity model)
        {
            //bool isSavedSuccessfully = true;
            string fName = "";

            try
            {
                //foreach (string fileName in Request.Files)
                //{
                HttpPostedFileBase file = model.File;
                //Save file content goes here
                fName = file.FileName;
                if (file != null && file.ContentLength > 0)
                {
                    var originalDirectory = new DirectoryInfo(string.Format("{0}Upload\\File", Server.MapPath(@"\")));

                    string pathString = System.IO.Path.Combine(originalDirectory.ToString(), "DispatchOut");

                    var fileName1 = Path.GetFileNameWithoutExtension(file.FileName) + "_u" + UserId + "_" + DateTime.Now.ToString("ddMMyyyyHHmm") + Path.GetExtension(fName);

                    bool isExists = System.IO.Directory.Exists(pathString);

                    if (!isExists)
                    {
                        System.IO.Directory.CreateDirectory(pathString);
                    }

                    var path = string.Format("{0}\\{1}", pathString, fileName1);
                    file.SaveAs(path);

                    var modelfiles = new FileEntity();
                    modelfiles.DispatchId = DispatchId;
                    modelfiles.Name       = fileName1;
                    modelfiles.UserId     = UserId;
                    string fileExtension = "";

                    if (!string.IsNullOrEmpty(fName))
                    {
                        fileExtension = Path.GetExtension(fName);
                    }
                    modelfiles.MimeType = fileExtension;

                    modelfiles.FileSize   = Utils.ToPrettySize(file.ContentLength).ToString();
                    modelfiles.Private    = false;
                    modelfiles.IsDel      = false;
                    modelfiles.UploadDate = DateTime.Now;
                    modelfiles.Type       = true;//false là công văn đến true là công văn đi
                    var Id = _iplfile.Insert(modelfiles);

                    model.Id     = DispatchId;
                    model.FileId = int.Parse(Id.ToString());
                    _ipldispatchout.UpdateFileId(model);
                }

                //}
            }
            catch (Exception ex)
            {
                //isSavedSuccessfully = false;
            }


            //if (isSavedSuccessfully)
            //{
            //    return Json(new { Message = fName });
            //}
            //else
            //{
            //    return Json(new { Message = "Lỗi khi upload file" });
            //}
        }
        public ActionResult Save(DispatchOutEntity model)
        {
            var entity   = new DispatchOutEntity();
            var sessUser = SessionSystem.GetUser();

            if (model.File != null)
            {
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    model.File.InputStream.CopyTo(memoryStream);
                }
            }
            if (ModelState.IsValid)
            {
                //model.FileId = 0;
                if (model.Id > 0) //update
                {
                    var sess = SessionSystem.GetUser();
                    Logs.logs("Sửa công văn đi", "Truy cập vào trang DispatchOut", "/DispatchOut/CreateDispatchOut", sess.UserId);
                    entity = _ipldispatchout.ViewDetail(model.Id);
                    if (entity != null && entity.Id > 0)
                    {
                        entity.DispatchNo   = model.DispatchNo;
                        entity.DispatchName = model.DispatchName;
                        entity.DispatchType = model.DispatchType;
                        entity.Priority     = model.Priority;
                        entity.DateWrite    = model.DateWrite;

                        entity.ApproverId     = model.ApproverId;
                        entity.DispatchStatus = model.DispatchStatus;

                        entity.DepartmentId = model.DepartmentId;

                        entity.ChiefOfStaffId = model.ChiefOfStaffId;
                        entity.Note           = model.Note;
                        entity.Description    = model.Description;
                        entity.ModifiedBy     = sessUser.UserId;
                        entity.ModifiedDate   = DateTime.Now;



                        var retVal = _ipldispatchout.Update(entity);

                        if (retVal)
                        {
                            int departid = entity.Id;

                            var OldFile = _iplfile.ViewDetail(model.FileId);
                            if (model.File != null)
                            {
                                OldFile.IsDel = true;
                                _iplfile.Update(OldFile); //đổi file cũ sang trạng thái đã xóa
                            }
                            SaveFile(model.Id, sessUser.UserId, model);
                            return(RedirectToAction("Index", "DispatchOut", new { DepartmentId = model.DepartmentId }));
                        }
                    }
                }
                else //insert
                {
                    var sess = SessionSystem.GetUser();
                    Logs.logs("Thêm công văn đi", "Truy cập vào trang DispatchOut", "/DispatchOut/CreateDispatchOut", sess.UserId);
                    model.CreateBy    = sessUser.UserId;
                    model.CreatedDate = DateTime.Now;
                    var Id = _ipldispatchout.Insert(model);
                    if (Id > 0)
                    {
                        SaveFile(Id, sessUser.UserId, model);

                        return(RedirectToAction("Index", "DispatchOut", new { DepartmentId = model.DepartmentId }));
                    }
                }
            }
            else
            {
                ListDropdown();
            }


            ViewBag.Msg = ConstantMsg.ErrorProgress;
            return(View("CreateDispatchOut", model));
        }