public ActionResult GenerateIMRForm(int id)
        {
            StorageModel _storageModel = new StorageModel();

            _storageModel            = Mapper.Map <StorageModel>(_iCommonService.GetStorageStuctureByID(id, GlobalConst.ConstantChar.Char_R));
            _storageModel.path       = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings[GlobalConst.VirtualDirectoryPath.VirtualPath].ToString());
            _storageModel.FolderName = GlobalConst.FolderName.LegalDocs;


            string Filename     = id.ToString() + GlobalConst.ReportName.IMRForm + GlobalConst.Extension.pdf;
            string savePath     = _storageService.GeneateStorage(_storageModel) + GlobalConst.ConstantChar.DoubleBackSlash + Filename;
            User   objUserModel = new User();
            var    _UserResult  = _iUserService.getUserByID(MMCUser.UserId);

            objUserModel = Mapper.Map <User>(_UserResult);

            string reportURL = string.Format(ConfigurationManager.AppSettings[GlobalConst.SSRSReportName.RptIMRForm], id, GlobalConst.Extension.PDF);

            if (System.IO.File.Exists(savePath))
            {
                System.IO.File.Delete(savePath);
            }
            using (WebClient client = new WebClient())
            {
                client.Credentials = CredentialCache.DefaultNetworkCredentials;
                client.DownloadFile(reportURL, savePath);
                client.Dispose();
            }
            MMCApp.Domain.Models.IntakeModel.RFAReferralFile _rfaReferralFile = new MMCApp.Domain.Models.IntakeModel.RFAReferralFile();
            _rfaReferralFile.RFAReferralID       = id;
            _rfaReferralFile.RFAReferralFileName = Filename;
            _rfaReferralFile.RFAFileCreationDate = DateTime.Now;
            _rfaReferralFile.RFAFileUserID       = MMCUser.UserId;
            _rfaReferralFile.RFAFileTypeID       = GlobalConst.FileType.IMRApplication;
            _intakeService.addReferralFile(Mapper.Map <serviceModel.IntakeService.RFAReferralFile>(_rfaReferralFile));
            return(Json(""));
            //return File(savePath, GlobalConst.FileDownloadExtension.Application_Pdf, Filename);
        }
        public ActionResult GenerateDeterminationLetter(int id)
        {
            StorageModel _storageModel = new StorageModel();

            MMCApp.Domain.Models.IntakeModel.RFAReferralFile _rfaReferralFile = new MMCApp.Domain.Models.IntakeModel.RFAReferralFile();
            _storageModel            = Mapper.Map <StorageModel>(_iCommonService.GetStorageStuctureByID(id, GlobalConst.ConstantChar.Char_R));
            _storageModel.path       = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings[GlobalConst.VirtualDirectoryPath.VirtualPath].ToString());
            _storageModel.FolderName = GlobalConst.FolderName.LegalDocs;

            string Filename           = id.ToString() + GlobalConst.ReportName.Determination + GlobalConst.Extension.pdf;
            string savePath           = _storageService.GeneateStorage(_storageModel) + GlobalConst.ConstantChar.DoubleBackSlash + Filename;
            string _saveSignaturePath = _intakeService.GetSignaturePathAndDescriptionByReferralID(id);

            string uSignURL       = GlobalConst.Extension.http + Request.Url.Host.ToLower() + GlobalConst.ConstantChar.Colon + Request.Url.Port + GlobalConst.ConstantChar.ForwardSlash + _saveSignaturePath;
            string uClientPathURL = GlobalConst.Extension.http + Request.Url.Host.ToLower() + GlobalConst.ConstantChar.Colon + Request.Url.Port + GlobalConst.ConstantChar.ForwardSlash;

            string reportURL = string.Format(ConfigurationManager.AppSettings[GlobalConst.SSRSReportName.RptDetermination], id, uSignURL, uClientPathURL, GlobalConst.Extension.PDF);

            if (System.IO.File.Exists(savePath))
            {
                System.IO.File.Delete(savePath);
            }
            using (WebClient client = new WebClient())
            {
                client.Credentials = CredentialCache.DefaultNetworkCredentials;
                client.DownloadFile(reportURL, savePath);
                client.Dispose();
            }
            _rfaReferralFile.RFAReferralID       = id;
            _rfaReferralFile.RFAFileTypeID       = GlobalConst.FileType.DeterminationLetter;
            _rfaReferralFile.RFAReferralFileName = Filename;
            _rfaReferralFile.RFAFileCreationDate = DateTime.Now;
            _rfaReferralFile.RFAFileUserID       = MMCUser.UserId;
            _intakeService.addReferralFile(Mapper.Map <serviceModel.IntakeService.RFAReferralFile>(_rfaReferralFile));
            return(Json(""));
            //return File(savePath, GlobalConst.FileDownloadExtension.Application_Pdf, Filename);
        }
        public ActionResult GenerateProofOfService(int id)
        {
            StorageModel _storageModel = new StorageModel();

            MMCApp.Domain.Models.IntakeModel.RFAReferralFile _rfaReferralFile = new MMCApp.Domain.Models.IntakeModel.RFAReferralFile();
            _storageModel            = Mapper.Map <StorageModel>(_iCommonService.GetStorageStuctureByID(id, GlobalConst.ConstantChar.Char_R));
            _storageModel.path       = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings[GlobalConst.VirtualDirectoryPath.VirtualPath].ToString());
            _storageModel.FolderName = GlobalConst.FolderName.LegalDocs;

            string Filename     = id.ToString() + GlobalConst.ReportName.ProofOfService + GlobalConst.Extension.pdf;
            string savePath     = _storageService.GeneateStorage(_storageModel) + GlobalConst.ConstantChar.DoubleBackSlash + Filename;
            User   objUserModel = new User();
            var    _UserResult  = _iUserService.getUserByID(MMCUser.UserId);

            objUserModel = Mapper.Map <User>(_UserResult);
            string uSignURL = GlobalConst.Extension.http + Request.Url.Host.ToLower() + GlobalConst.ConstantChar.Colon + Request.Url.Port + GlobalConst.VirtualPathFolders.StorageUserPath + objUserModel.UserId + GlobalConst.ConstantChar.ForwardSlash + objUserModel.UserSignatureFileName;

            string reportURL = string.Format(ConfigurationManager.AppSettings[GlobalConst.SSRSReportName.RptProofOfService], id, objUserModel.UserFirstName + ' ' + objUserModel.UserLastName, uSignURL, GlobalConst.Extension.PDF);

            if (System.IO.File.Exists(savePath))
            {
                System.IO.File.Delete(savePath);
            }
            using (WebClient client = new WebClient())
            {
                client.Credentials = CredentialCache.DefaultNetworkCredentials;
                client.DownloadFile(reportURL, savePath);
                client.Dispose();
            }
            _rfaReferralFile.RFAReferralID       = id;
            _rfaReferralFile.RFAFileTypeID       = GlobalConst.FileType.ProofofService;
            _rfaReferralFile.RFAReferralFileName = Filename;
            _rfaReferralFile.RFAFileCreationDate = DateTime.Now;
            _rfaReferralFile.RFAFileUserID       = MMCUser.UserId;
            _intakeService.addReferralFile(Mapper.Map <serviceModel.IntakeService.RFAReferralFile>(_rfaReferralFile));
            return(Json(""));
        }
        public ActionResult CreateInvoice(IEnumerable <Billing> BillingDetails)
        {
            string     referralIDList  = GlobalConst.ConstantChar.StringBlank;
            int        _patClientID    = 0;
            bool       CreateStatement = false;
            List <int> _listInvoiceNum = new List <int>();
            List <RFAReferralInvoice> _InvoiceModelList        = new List <RFAReferralInvoice>();
            RFAReferralAdditionalLink objAdditionalLinkInvoice = new RFAReferralAdditionalLink();
            var _listPatientClaimID = BillingDetails.Where(claim => claim.IsChecked).Select(claim => claim.PatientClaimID).Distinct();

            //Check Whether to create Statement or not

            var _clientPrivateLabel = _iClientService.getClientPrivateLabelDetailByClientID(BillingDetails.FirstOrDefault().PatClientID);

            if (_clientPrivateLabel != null)
            {
                if (_clientPrivateLabel.ClientStatementStart != null)
                {
                    CreateStatement = true;
                }
            }


            //Optimized Removed Foreach....For adding referralID in List and generating the InvoiceTemplate according to InvoiceID

            foreach (var _patClaimID in _listPatientClaimID) // For adding referralID in List and generating the InvoiceTemplate according to InvoiceID
            {
                var _referralIDList = BillingDetails.Where(claim => claim.IsChecked && claim.PatientClaimID == _patClaimID).Select(claim => new { claim.RFAReferralID, claim.PatClientID }).Distinct();

                foreach (var refId in _referralIDList)
                {
                    referralIDList += refId.RFAReferralID.ToString() + ",";
                    _patClientID    = refId.PatClientID;
                }
                //Optimized Removed Foreach....For Adding PatientClaimID in List
                StorageModel _storageModel = new StorageModel();
                MMCApp.Domain.Models.IntakeModel.RFAReferralFile _rfaReferralFile = new MMCApp.Domain.Models.IntakeModel.RFAReferralFile();
                _storageModel            = Mapper.Map <StorageModel>(_iCommonService.GetStorageStuctureByID(_patClaimID, GlobalConst.ConstantChar.Char_C));
                _storageModel.path       = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings[GlobalConst.VirtualDirectoryPath.VirtualPath].ToString());
                _storageModel.FolderName = GlobalConst.FolderName.Invoices;

                RFAReferralInvoice _InvoiceModel = new RFAReferralInvoice();
                _InvoiceModel.ClientID             = _patClientID;
                _InvoiceModel.PatientClaimID       = _patClaimID;
                _InvoiceModel.UserID               = MMCUser.UserId;
                _InvoiceModel.CreatedDate          = System.DateTime.Now;
                _InvoiceModel.RFAReferralInvoiceID = 0;

                string _invoiceNum = _iBillingService.addRFAReferralInvoice(Mapper.Map <MMCService.BillingService.RFAReferralInvoice>(_InvoiceModel));
                string Filename    = _invoiceNum + GlobalConst.ReportName.Invoice + GlobalConst.Extension.pdf;
                string savePath    = _storageService.GeneateStorage(_storageModel) + GlobalConst.ConstantChar.DoubleBackSlash + Filename;
                string reportURL   = string.Format(ConfigurationManager.AppSettings[GlobalConst.SSRSReportName.RptInvoice], referralIDList, _patClaimID, _invoiceNum, GlobalConst.Extension.PDF);

                if (System.IO.File.Exists(savePath))
                {
                    System.IO.File.Delete(savePath);
                }
                using (WebClient client = new WebClient())
                {
                    client.Credentials = CredentialCache.DefaultNetworkCredentials;
                    client.DownloadFile(reportURL, savePath);
                    client.Dispose();
                    referralIDList = "";
                }

                _InvoiceModel.InvoiceNumber = _invoiceNum;
                _InvoiceModelList.Add(_InvoiceModel);

                int _invoiceID = 0;
                _invoiceID = _iBillingService.getInvoiceIDByInvoiceNumber(_invoiceNum);
                foreach (var _refID in _referralIDList)
                {
                    objAdditionalLinkInvoice.RFAReferralID        = _refID.RFAReferralID;
                    objAdditionalLinkInvoice.RFAReferralInvoiceID = _invoiceID;
                    _intakeService.AddUpdateRFAReferralAdditionalLinkInvoiceIDbyRefID(Mapper.Map <MMCService.IntakeService.RFAReferralAdditionalLink>(objAdditionalLinkInvoice));
                    if (CreateStatement == false)
                    {
                        _iPreparationService.updateProcessLevel(_refID.RFAReferralID, GlobalConst.ProcessLevel.FileStorage);
                    }
                }
            }

            //..........Statement
            if (CreateStatement == true)
            {
                ClientStatement _ClientStatement = new ClientStatement();
                _ClientStatement.ClientID     = BillingDetails.FirstOrDefault().PatClientID;
                _ClientStatement.UserID       = MMCUser.UserId;
                _ClientStatement.CreationDate = DateTime.Now;
                int    _StatementId = _iBillingService.addClientStatement(Mapper.Map <MMCService.BillingService.ClientStatement>(_ClientStatement));
                string StmntNum     = _iBillingService.getStatementNumberByStatementID(_StatementId);
                //Add statementID to
                foreach (var _patClaimID in _listPatientClaimID) // For adding referralID in List and generating the InvoiceTemplate according to InvoiceID
                {
                    var _referralIDListStatement = BillingDetails.Where(claim => claim.IsChecked && claim.PatientClaimID == _patClaimID).Select(claim => new { claim.RFAReferralID, claim.PatClientID }).Distinct();
                    foreach (var _refID in _referralIDListStatement)
                    {
                        objAdditionalLinkInvoice.RFAReferralID     = _refID.RFAReferralID;
                        objAdditionalLinkInvoice.ClientStatementID = _StatementId;
                        _intakeService.AddUpdateRFAReferralAdditionalLinkStatementIDbyRefID(Mapper.Map <MMCService.IntakeService.RFAReferralAdditionalLink>(objAdditionalLinkInvoice));
                        _iPreparationService.updateProcessLevel(_refID.RFAReferralID, GlobalConst.ProcessLevel.FileStorage);
                    }
                }

                string StateMentFilename = StmntNum + "_" + GlobalConst.FileName.InvoiceStatement + GlobalConst.Extension.pdf;
                string StatementsavePath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings[GlobalConst.VirtualDirectoryPath.VirtualPath].ToString()); //+GlobalConst.ConstantChar.DoubleBackSlash + Filename;
                StatementsavePath += GlobalConst.ConstantChar.DoubleBackSlash + _ClientStatement.ClientID + GlobalConst.ConstantChar.DoubleBackSlash + GlobalConst.FolderName.InvoiceStatement;

                if (!System.IO.Directory.Exists(StatementsavePath))
                {
                    System.IO.Directory.CreateDirectory(StatementsavePath);
                }
                StatementsavePath += GlobalConst.ConstantChar.DoubleBackSlash + StateMentFilename;
                string reportURL = string.Format(ConfigurationManager.AppSettings[GlobalConst.SSRSReportName.RptClientInvoiceStatement], _StatementId, _ClientStatement.ClientID, GlobalConst.Extension.PDF);

                if (System.IO.File.Exists(StatementsavePath))
                {
                    System.IO.File.Delete(StatementsavePath);
                }
                using (WebClient client = new WebClient())
                {
                    client.Credentials = CredentialCache.DefaultNetworkCredentials;
                    client.DownloadFile(reportURL, StatementsavePath);
                    client.Dispose();
                }
            }
            //   Tuple<string, string> t = DownloadPrintDocumnent(_InvoiceModelList);
            //   return File(t.Item1, GlobalConst.FileDownloadExtension.Application_Pdf, t.Item2);
            return(Json(GlobalConst.Message.InvoiceCreatedSuccessfully));
        }