public void SetImageToModel(ref EmploymentPapersUploadVModel Model, HttpContextBase HttpContext)
        {
            try
            {
                // if (Model.Images != null&&!Model.Images.Any(i=>i==null))
                //return;

                if (HttpContext.Request.Files != null && HttpContext.Request.Files.Count > 0)
                {
                    List <HttpPostedFileBase> ImagesList = new List <HttpPostedFileBase>();
                    for (int i = 0; i < HttpContext.Request.Files.Count; i++)
                    {
                        var file = HttpContext.Request.Files[i];
                        if (file.ContentLength > 0)
                        {
                            ImagesList.Add(file);
                        }
                    }

                    Model.Images = ImagesList.Count() > 0 ? ImagesList.ToArray() : null;
                }
            }
            catch
            {
            }
        }
        public JsonResult SaveDoc(EmploymentPapersUploadVModel Model)
        { 
            EmploymentPapers _EmploymentPapers = new EmploymentPapers(HrUnitOfWork);
            _EmploymentPapers.SetImageToModel(ref Model, HttpContext);

            string ValidationErrorMessage;
            if (!_EmploymentPapers.IsValid(ModelState, Model, Culture, out ValidationErrorMessage))
                return Json(new { Result = false, Message = ValidationErrorMessage });

            if (Model.Images != null)
            {
                string contentType;
                Model.ImageStream = ITextSharpProcesses.CompineFilesIntoPDF(Model.Images, out contentType);
                Model.ContentType = contentType;
            }

            int CurrentEmpStatus, OldEmpStatus;
            string ErrorMessage;
            bool ProcessDone = _EmploymentPapers.Save(Model, CompanyId, User.Identity.Name, User.Identity.GetCulture(), out ErrorMessage,out CurrentEmpStatus,out OldEmpStatus);
           
            if(ProcessDone)
            return Json(new { Result = ProcessDone, Message = MsgUtils.Instance.Trls("SaveProcessSuceeded"), CurrentEmpStatus= CurrentEmpStatus, OldEmpStatus= OldEmpStatus });
            else
                return Json(new { Result = ProcessDone, Message = ErrorMessage });

        }
        private void SaveGrid_Section(EmploymentPapersUploadVModel Model, string Culture, CompanyDocsViews doc)
        {
            IList <CompanyDocAttr> oldCompDocAttr = null;

            if (Model.Stream_Id != null)
            {
                oldCompDocAttr = hrUnitOfWork.CompanyDocAttrRepository.GetDocAttrByStreamId(Model.Stream_Id.Value);
            }

            foreach (CompanyDocAttrViewModel UpdatedAttr in Model.BatchGridData_List)
            {
                //if (UpdatedAttr. !string.IsNullOrEmpty(UpdatedAttr.Value))
                //{
                if (!UpdatedAttr.Insert && oldCompDocAttr != null)
                {
                    var docAttr = oldCompDocAttr.Where(a => a.AttributeId == UpdatedAttr.Id).FirstOrDefault();
                    docAttr.Value   = Constants.CompanyDocAttr.getValue(UpdatedAttr, Culture);
                    docAttr.ValueId = UpdatedAttr.ValueId;
                }
                else
                {
                    var docAttr = new CompanyDocAttr();
                    docAttr.AttributeId = UpdatedAttr.Id;
                    docAttr.Value       = Constants.CompanyDocAttr.getValue(UpdatedAttr, Culture);
                    docAttr.ValueId     = UpdatedAttr.ValueId;
                    doc.CompanyDocAttrs.Add(docAttr);
                }
                //}
            }

            if (Model.Stream_Id == null)
            {
                hrUnitOfWork.CompanyDocsViewsRepository.Add(doc);
            }
        }
        private CompanyDocsViews Save_CompanyDocsViews(EmploymentPapersUploadVModel Model, string UserName, string Culture, int CompanyId, out int CurrentEmpStatus, out int OldEmpStatus)
        {
            OldEmpStatus     = 0;
            CurrentEmpStatus = 0;

            CompanyDocsViews doc = null;

            if (Model.Stream_Id != null)
            {
                doc = hrUnitOfWork.CompanyDocsViewsRepository.GetByStreamID(Model.Stream_Id.Value);
                doc.ModifiedUser = UserName;
                FillCommonData_CompanyDocsViews(ref doc, Model, Culture);
            }
            else
            {
                doc             = new CompanyDocsViews();
                doc.TypeId      = Model.TypeId;
                doc.CompanyId   = CompanyId;
                doc.CreatedUser = UserName;
                doc.Source      = Constants.Sources.People;
                doc.SourceId    = Model.EmpID;
                FillCommonData_CompanyDocsViews(ref doc, Model, Culture);
            }


            CurrentEmpStatus = UpdatePersonStatus(Model.EmpID, Model.RequiredDocTypeIDsList, Model.TypeId, null, out OldEmpStatus);
            return(doc);
        }
        public bool Save(EmploymentPapersUploadVModel Model, int CompanyId, string UserName, string Culture, out string ErrorMessage, out int CurrentEmpStatus, out int OldEmpStatus)
        {
            OldEmpStatus     = 0;
            CurrentEmpStatus = 0;
            ErrorMessage     = string.Empty;
            List <Error> errors = new List <Error>();

            try
            {
                CompanyDocsViews doc = Save_CompanyDocsViews(Model, UserName, Culture, CompanyId, out CurrentEmpStatus, out OldEmpStatus);
                SaveGrid_Section(Model, Culture, doc);
                errors = hrUnitOfWork.SaveChanges();

                if (errors.Count() > 0)
                {
                    ErrorMessage = errors.First().errors.First().message;
                    return(false);
                }

                return(true);
            }
            catch (Exception ex)
            {
                ErrorMessage = ex.Message;
                return(false);
            }
            // }
        }
        public PartialViewResult UploadPaperPartial(int EmpID, int JobID, int Gender, int Nationality, int DocTypeID = 0, bool HasExpiryDate = false, Guid? Stream_Id = null,string RequiredDocTypeIDs="",bool IsEmpPaper=false)
        {
            int DocumentType = 0;
            bool IsAddNewOthers = true;
            List<SelectListItem> DocTypesList = new List<SelectListItem>();
            EmploymentPapers _EmploymentPapers = new EmploymentPapers(_hrUnitOfWork);

            if (DocTypeID == 0)
            {
                IsAddNewOthers = false;
                DocTypesList = _EmploymentPapers.GetPapers_Others(JobID, Gender, Nationality, CompanyId, User.Identity.GetCulture()).Select(d => new SelectListItem() { Value = d.Id.ToString(), Text = d.Name }).ToList();
            }
            else
            {
                DocumentType = _EmploymentPapers.GetDocumentType_ByDocTypeID(DocTypeID);
            }

            EmploymentPapersUploadVModel Model = new EmploymentPapersUploadVModel();
            Model.RequiredDocTypeIDs = RequiredDocTypeIDs;
            Model.ValidFileExtensions = "'.jpg','.jpeg','.bmp','.png','.gif','.pdf'";/*,'.doc','.docx','.xls','.xlsx'*/
            Model.DocumenType = DocumentType;
            Model.TypeId = DocTypeID;
            Model.DocTypesList = DocTypesList;
            Model.IsAddNewOthers = IsAddNewOthers;
            Model.HasExpiryDate = HasExpiryDate;
            Model.EmpID = EmpID;


            if (Stream_Id != null)
            {
                Model.IsUploaded = true;
                Model.Stream_Id = Stream_Id;

                CompanyDocsViews DocView = _EmploymentPapers.GetCompanyDocsViews_ByStreamID(Stream_Id.Value);
                if (DocView != null)
                {
                    Model.Keyword = DocView.Keyword;
                    Model.name = DocView.name;
                    Model.Description = DocView.Description;
                    Model.ExpireDate_string = DocView.ExpiryDate.ToMyDateString(User.Identity.GetCulture(), "dd/MM/yyyy");
                    Model.File_Type = DocView.file_type;
                    Model.IsEmpPaper = IsEmpPaper;
                }

            }
            //Model.OldModel_Serialized = new JavaScriptSerializer().Serialize(Model);
            FillBasicData(false, false,true, true);

            return PartialView("_UploadPaperPartial", Model);
        }
        //public void UpdateEmp_EmpPapersStatus(int EmpID,List<Paper_UploadStatus> EmpDocsTypes)
        //{
        //    try
        //    {
        //        double Total = EmpDocsTypes.Count(a => a.IsRequired && a.IsEmpPaper);
        //        double Uploaded = EmpDocsTypes.Count(a => a.IsRequired && a.IsEmpPaper && a.IsUploaded);

        //        // double Percentage = (Uploaded / Total) * 100;
        //        string PaperStatus = string.Format("{0} / {1}", Uploaded, Total);
        //        //Update employee record
        //        var person = hrUnitOfWork.PeopleRepository.GetPerson(EmpID);
        //        if (person != null)
        //            person.PaperStatus = PaperStatus;

        //        hrUnitOfWork.Save();
        //    }
        //    catch
        //    {

        //    }
        //}



        public bool IsValid(ModelStateDictionary ModelState, EmploymentPapersUploadVModel Model, string Culture, out string ErrorMessage)
        {
            if (!Model.HasExpiryDate)
            {
                ModelState.Remove("ExpireDate_string");
            }

            bool Result = true;

            ErrorMessage = string.Empty;
            if (!ModelState.IsValid)
            {
                Result = CreateErrorMessage(MsgUtils.Instance.Trls(Culture, Constants.MessagesKeys.InvalidData), ref ErrorMessage);
            }

            if (Model.EmpID == 0)
            {
                Result = CreateErrorMessage(MsgUtils.Instance.Trls(Culture, "No employee found"), ref ErrorMessage);
            }

            if (!string.IsNullOrEmpty(Model.ExpireDate_string))
            {
                DateTime ExpireDate = Model.ExpireDate_string.ToMyDateTime(Culture);

                if (ExpireDate < DateTime.Now.Date)
                {
                    Result = CreateErrorMessage(MsgUtils.Instance.Trls(Culture, "Invalid Expire Date"), ref ErrorMessage);
                }
            }

            string KendoErrorMessage;

            if (!Constants.KendoGridValidation.IsValid(Model.BatchGridData_List, Culture, out KendoErrorMessage))
            {
                Result = CreateErrorMessage(KendoErrorMessage, ref ErrorMessage);
            }

            if (Model.Images == null && Model.Stream_Id == null)
            {
                Result = CreateErrorMessage(MsgUtils.Instance.Trls(Culture, "Please insert image"), ref ErrorMessage);
            }

            if (!ImageProcesses.IsValidExtension(Model.Images, Model.ValidFileExtensions))
            {
                Result = CreateErrorMessage(MsgUtils.Instance.Trls(Culture, "Invalid file extension"), ref ErrorMessage);
            }

            return(Result);
        }
        private void FillCommonData_CompanyDocsViews(ref CompanyDocsViews doc, EmploymentPapersUploadVModel Model, string Culture)
        {
            //byte[] ImageStream = null;

            //if (Model.Images != null)
            //    ImageStream =  ImageProcesses.ReadFully(Model.Images.InputStream);

            doc.name        = Model.name;
            doc.Keyword     = Model.Keyword;
            doc.Description = Model.Description;
            doc.ExpiryDate  = (!string.IsNullOrEmpty(Model.ExpireDate_string)) ? (DateTime?)Model.ExpireDate_string.ToMyDateTime(Culture) : null;
            if (Model.ImageStream != null)
            {
                doc.file_stream = Model.ImageStream;
                doc.file_type   = Model.ContentType;
            }
        }