public JsonResult TextPasswordResetCode(string phoneNumber) { using (var db = new WorktripEntities()) { var user = db.Users.FirstOrDefault(u => u.PhoneNumber == phoneNumber); if (user == null) { return(Json(new { status = -1, message = "This number is not registered with us" })); } //string code = UserManager.GeneratePasswordResetToken(user.Id); var tokenProvider = new TotpSecurityStampBasedTokenProvider <ApplicationUser, String>(); string code = tokenProvider.GenerateAsync("smspwcode", UserManager, UserManager.FindById(user.Id)).Result; var callbackUrl = Url.Action("ResetPassword", "Home", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); UserManager.SmsService.Send(new IdentityMessage { Destination = phoneNumber, Body = "Your WorkTrip Password Reset Link: " + callbackUrl }); return(Json(new { status = 0 })); } }
public JsonResult SetInternationalLayovers(string userId, int year, bool isChecked) { using (var db = new WorktripEntities()) { var miscTaxInfo = db.UserMiscTaxInfoes.FirstOrDefault(umt => umt.UserId == userId && umt.TaxYear == year); //If no record exists, create new in the db if (miscTaxInfo == null) { miscTaxInfo = new UserMiscTaxInfo { UserId = userId, TaxYear = year, InternationalLayovers = isChecked }; db.UserMiscTaxInfoes.Add(miscTaxInfo); } else { miscTaxInfo.InternationalLayovers = isChecked; } db.SaveChanges(); return(Json(new { status = 0 })); } return(Json(new { status = -1 })); }
public JsonResult CalculatePerDiem(int taxYear, List <Layover> layovers, string userId) { var curUserId = userId == null?User.Identity.GetUserId() : userId; double total = 0; foreach (var layover in layovers) { if (WorktripInit.perDiemRates.ContainsKey(layover.AirportCode)) { total += WorktripInit.perDiemRates[layover.AirportCode] * layover.Days; } } using (var db = new WorktripEntities()) { var userMiscInfo = UserInfoViewModel.GetOrCreateUserInfo(db, curUserId, taxYear); userMiscInfo.LayoversPerDiem = total; db.SaveChanges(); } return(Json(new { status = 0, total })); }
public JsonResult UpdateTaxPrice(string userId, decimal price, int year) { using (var db = new WorktripEntities()) { var curUserId = User.Identity.GetUserId(); var link = db.UserToPreparers.FirstOrDefault(up => up.UserId == userId && up.Year == year); if (link == null) { link = new UserToPreparer { UserId = userId, Year = year, StatusId = 1 }; db.UserToPreparers.Add(link); } link.Fee = price; db.SaveChanges(); return(Json(new { status = 0 })); } return(Json(new { status = -1 })); }
public static void InitPerDiemRates() { perDiemRates = new Dictionary <string, double>(); using (var db = new WorktripEntities()) { var retrieved = db.PerDiemRates.ToList(); foreach (var perDiem in retrieved) { perDiemRates.Add(perDiem.AirportCode, perDiem.Rate); } } }
public JsonResult UpdateTaxReturnAmount(string userId, decimal amount, int year) { using (var db = new WorktripEntities()) { var info = UserInfoViewModel.GetOrCreateUserInfo(db, userId, year); info.TaxReturn = amount; db.SaveChanges(); return(Json(new { status = 0 })); } return(Json(new { status = -1 })); }
public JsonResult UpdateTaxNotes(string userId, string notes, int year) { using (var db = new WorktripEntities()) { var info = UserInfoViewModel.GetOrCreateUserInfo(db, userId, year); info.PreparerNotes = notes; db.SaveChanges(); return(Json(new { status = 0 })); } return(Json(new { status = -1 })); }
public JsonResult SetFirstModified(string userId, int year) { using (var db = new WorktripEntities()) { var user = db.UserToPreparers.FirstOrDefault(up => up.UserId == userId && up.Year == year); if (user != null && user.FirstModified == null) { user.FirstModified = DateTime.Now; db.SaveChanges(); } return(Json(new { status = 0 })); } }
public JsonResult UpdateTaxStatus(string userId, string status, int year) { using (var db = new WorktripEntities()) { var curUserId = User.Identity.GetUserId(); var link = db.UserToPreparers.FirstOrDefault(up => up.UserId == userId && up.Year == year); var statusObj = db.Status.FirstOrDefault(s => s.Name == status); if (link != null && statusObj != null) { link.StatusId = statusObj.Id; db.SaveChanges(); return(Json(new { status = 0 })); } } return(Json(new { status = -1 })); }
public void DbTest() { using (var db = new WorktripEntities()) { var list = new List <int>() { 140, 3, 24, 3432, 2345106, 4324, 3, 6, 8 }; var evens = list.Where(n => n % 2 == 0).OrderByDescending(n => n); var firstOver10k = list.FirstOrDefault(n => n > 10000); var odds = from n in list where n % 2 == 1 select n; Trace.WriteLine(evens); } }
public JsonResult RegisterAndSendTextCode(string phoneNumber, string firstName, string lastName, string promoCode = null) { var user = new ApplicationUser { UserName = phoneNumber, FirstName = firstName, LastName = lastName, SignUpTime = DateTime.UtcNow }; using (var db = new WorktripEntities()) { var existingUser = db.Users.FirstOrDefault(u => u.UserName == phoneNumber || u.PhoneNumber == phoneNumber); if (existingUser != null) { if (existingUser.Email != null) { //existing valid account return(Json(new { status = -1, message = "That phone number is already in use" })); } else { //accounted created early via phone number, but did not validate, so try again user = UserManager.FindById(existingUser.Id); user.FirstName = firstName; user.LastName = lastName; UserManager.Update(user); } } else { var registerResult = UserManager.Create(user); if (!registerResult.Succeeded) { return(Json(new { status = -1, message = registerResult.Errors.First() })); } UserManager.AddToRole(user.Id, "Customer"); } } var code = UserManager.GenerateChangePhoneNumberToken(user.Id, phoneNumber); if (UserManager.SmsService != null) { var message = new IdentityMessage { Destination = phoneNumber, Body = "Your WorkTrip security code is: " + code }; UserManager.SmsService.Send(message); using (var db = new WorktripEntities()) { //create empty user->preparer link UserInfoViewModel.GetOrCreateUserInfo(db, user.Id, DateTime.Now.AddYears(-1).Year, promoCode); //text all preparers regarding new user sign up var preparers = db.Users.Include(u => u.Roles).Where(u => u.Roles.Any(r => r.Name == "Preparer")).ToList(); foreach (var p in preparers) { UserManager.SmsService.Send(new IdentityMessage { Destination = p.PhoneNumber, Body = "Sign up alert: " + firstName + " " + lastName + ", " + phoneNumber }); } } return(Json(new { status = 0, userId = user.Id, phoneNumber = phoneNumber })); } return(Json(new { status = -2 })); }
public JsonResult GetUserDocuments(string userId, int taxYear, int?skip, int?amount) { bool downloadSuccessful = true; var results = new List <Tuple <string, string, string> >(); using (var db = new WorktripEntities()) { try { Regex filePattern = new Regex(@"http.*\/.*\/(?<directory>.*)\/(?<filename>.*)"); var user = db.Users.FirstOrDefault(u => u.Id == userId); var miscDocs = db.UserMiscDocs.Where(d => d.UserId == userId && d.Year == taxYear); var taxReturn = db.UserTaxReturns.Where(d => d.UserId == userId && d.Year == taxYear); miscDocs = miscDocs.OrderBy(d => d.Id); taxReturn = taxReturn.OrderBy(d => d.Id); if (skip.HasValue) { miscDocs = miscDocs.Skip(skip.Value); taxReturn = taxReturn.Skip(skip.Value); } if (amount.HasValue) { miscDocs = miscDocs.Take(amount.Value); taxReturn = taxReturn.Take(amount.Value); } var fileUrls = miscDocs.Select(d => d.Path).ToList(); fileUrls.AddRange(taxReturn.Select(d => d.Path).ToList()); var parsedFilePaths = new List <Tuple <string, string> >(); foreach (var url in fileUrls) { Match match = filePattern.Match(url); if (match.Success) { var newTuple = new Tuple <string, string>( match.Groups["directory"].Value, match.Groups["filename"].Value ); parsedFilePaths.Add(newTuple); } } CloudStorageAccount storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("StorageConnectionString")); CloudFileClient fileClient = storageAccount.CreateCloudFileClient(); CloudFileShare share = fileClient.GetShareReference("worktripdocs"); CloudFileDirectory rootDir = share.GetRootDirectoryReference(); CloudFileDirectory userDir = null; var userDirName = ""; foreach (var parsedPath in parsedFilePaths) { if (userDirName != parsedPath.Item1) { userDir = rootDir.GetDirectoryReference(parsedPath.Item1); if (!userDir.Exists()) { continue; } userDirName = parsedPath.Item1; } var filename = parsedPath.Item2; CloudFile file = userDir.GetFileReference(filename); if (!file.Exists()) { continue; } file.FetchAttributes(); string fileContents = ""; if (file.Properties.ContentType != null && file.Properties.ContentType.StartsWith("image/")) { MemoryStream fileStream = new MemoryStream(); file.DownloadToStream(fileStream); fileContents = ConvertImageStreamToBase64String(fileStream); } else if (file.Properties.ContentType.ToLower() == "application/pdf") { MemoryStream fileStream = new MemoryStream(); file.DownloadToStream(fileStream); fileContents = ConvertStreamToBase64String(fileStream); } else { fileContents = file.DownloadText(); } results.Add( new Tuple <string, string, string>(filename, file.Properties.ContentType, fileContents) ); } } catch (Exception e) { //Do some error logging here.. downloadSuccessful = false; } } if (downloadSuccessful) { return(Json(new { status = 0, files = results })); } else { return(Json(new { status = -1, message = "Error in downloading files" })); } }
public JsonResult UploadUserTaxReturn(int taxYear, string subFolder, string userId) { var curUserId = userId == null?User.Identity.GetUserId() : userId; bool uploadedSuccessfully = false; string uploadedURI = ""; using (var db = new WorktripEntities()) { try { var user = db.Users.FirstOrDefault(u => u.Id == curUserId); HttpPostedFileBase uploadedFile = Request.Files.Get(0); var compressedStream = uploadedFile.InputStream; if (uploadedFile.ContentType.StartsWith("image/")) { compressedStream = ResizePictureForBandwidth(uploadedFile); } CloudStorageAccount storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("StorageConnectionString")); CloudFileClient fileClient = storageAccount.CreateCloudFileClient(); CloudFileShare share = fileClient.GetShareReference("worktripdocs"); CloudFileDirectory rootDir = share.GetRootDirectoryReference(); CloudFileDirectory userDir = rootDir.GetDirectoryReference(user.FirstName + " " + user.LastName + " " + user.PhoneNumber); userDir.CreateIfNotExists(); if (!string.IsNullOrEmpty(subFolder)) { userDir = userDir.GetDirectoryReference(subFolder); userDir.CreateIfNotExists(); } var newFileName = uploadedFile.FileName; var fileExtension = Path.GetExtension(newFileName); CloudFile file = userDir.GetFileReference(newFileName); int fileDuplicateCount = 1; while (file.Exists()) { //generate a file name that doesn't exist yet newFileName = Path.GetFileNameWithoutExtension(newFileName) + "(" + (fileDuplicateCount++) + ")" + fileExtension; file = userDir.GetFileReference(newFileName);; } file.Properties.ContentType = uploadedFile.ContentType; file.UploadFromStream(compressedStream); uploadedURI = file.Uri.ToString(); UserTaxReturn newReturn = new UserTaxReturn() { UserId = curUserId, Date = DateTime.UtcNow, Path = uploadedURI, Year = taxYear }; db.UserTaxReturns.Add(newReturn); db.SaveChanges(); uploadedSuccessfully = true; UserInfoViewModel.UpdateUserActionsLog(curUserId, "uploaded " + taxYear + " " + (fileExtension == ".pdf" ? "tax return" : "tax form(s)")); } catch (Exception e) { //Do some error logging here.. uploadedSuccessfully = false; } } if (uploadedSuccessfully) { return(Json(new { status = 0 })); } else { return(Json(new { status = -1, message = "Error in saving file" })); } }
public JsonResult GetSingleUserTaxReturn(string userId, int taxYear) { bool downloadSuccessful = true; var results = new List <Tuple <string, string, byte[]> >(); using (var db = new WorktripEntities()) { try { Regex filePattern = new Regex(@"http.*\/.*\/(?<directory>.*)\/(?<filename>.*)"); var user = db.Users.FirstOrDefault(u => u.Id == userId); var taxReturn = db.UserTaxReturns.Where(d => d.UserId == userId && d.Year == taxYear); taxReturn = taxReturn.OrderBy(d => d.Id); var fileUrls = new List <UserTaxReturn>(); if (taxReturn.Count() != 0) { fileUrls.Add(taxReturn.AsEnumerable().Last()); byte[] bytes = new byte[64000]; var parsedFilePaths = new List <Tuple <string, string> >(); foreach (var url in fileUrls) { Match match = filePattern.Match(url.Path); if (match.Success) { var newTuple = new Tuple <string, string>( match.Groups["directory"].Value, match.Groups["filename"].Value ); parsedFilePaths.Add(newTuple); } } CloudStorageAccount storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("StorageConnectionString")); CloudFileClient fileClient = storageAccount.CreateCloudFileClient(); CloudFileShare share = fileClient.GetShareReference("worktripdocs"); CloudFileDirectory rootDir = share.GetRootDirectoryReference(); CloudFileDirectory userDir = null; var userDirName = ""; foreach (var parsedPath in parsedFilePaths) { if (userDirName != parsedPath.Item1) { userDir = rootDir.GetDirectoryReference(parsedPath.Item1); if (!userDir.Exists()) { continue; } userDirName = parsedPath.Item1; } var filename = parsedPath.Item2; CloudFile file = userDir.GetFileReference(filename); if (!file.Exists()) { continue; } file.FetchAttributes(); string fileContents = ""; if (file.Properties.ContentType.ToLower() == "application/pdf") { MemoryStream fileStream = new MemoryStream(); file.DownloadToStream(fileStream); bytes = fileStream.ToArray(); fileContents = ConvertStreamToBase64String(fileStream); } else { fileContents = file.DownloadText(); } results.Add( new Tuple <string, string, byte[]>(filename, file.Properties.ContentType, bytes) ); } } } catch (Exception e) { //Do some error logging here.. downloadSuccessful = false; } } if (downloadSuccessful && results.Count > 0) { return(Json(new MyJsonResult { status = 0, fileName = results.ElementAtOrDefault(0).Item1, fileContentType = results.ElementAtOrDefault(0).Item2, fileContents = results.ElementAtOrDefault(0).Item3 })); } else { return(Json(new { status = -1, message = "Error in downloading files" })); } }
public void PopulatePerDiem() { Excel.Application xlApp = new Excel.Application(); Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\Users\David\Desktop\excels\Airport IRS Database2.xlsx"); Excel._Worksheet xlWorksheet = xlWorkbook.Sheets["Merge"]; Excel.Range xlRange = xlWorksheet.UsedRange; Dictionary <string, PerDiem> perDiems = new Dictionary <string, PerDiem>(); for (int i = 2; i < 1450; i++) { //for (int j = 1; j <= 12; j++) //{ // //new line // if (j == 1) // Console.Write("\r\n"); // //write the value to the console // if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null) // { // var a = xlRange.Cells[i, j].Value2.ToString(); // Console.Write(a + "\t"); // } // //add useful things here! //} if (xlWorksheet.Cells[i, "K"] == null || xlWorksheet.Cells[i, "K"].Value2 == null) { continue; } string Country = xlWorksheet.Cells[i, "E"].Value2 != null ? xlWorksheet.Cells[i, "E"].Value2.ToString() : "US"; string State = xlWorksheet.Cells[i, "D"].Value2 != null ? xlWorksheet.Cells[i, "D"].Value2.ToString() : "N/A"; string City = xlWorksheet.Cells[i, "C"].Value2 != null ? xlWorksheet.Cells[i, "C"].Value2.ToString() : "N/A"; string Airport = xlWorksheet.Cells[i, "G"].Value2 != null ? xlWorksheet.Cells[i, "G"].Value2.ToString() : "N/A"; int Rate = 0; bool parsed = int.TryParse(xlWorksheet.Cells[i, "K"].Value2.ToString(), out Rate); if (!perDiems.ContainsKey(Airport)) { var perDiem = new PerDiem() { Country = Country, State = State, City = City, Airport = Airport, Rate = Rate }; perDiems.Add(Airport, perDiem); } } xlWorkbook = xlApp.Workbooks.Open(@"C:\Users\David\Desktop\excels\FY2017-PerDiemRatesMasterFile2.xls"); xlWorksheet = xlWorkbook.Sheets["FY2017"]; for (int i = 4; i < 348; i++) { string state = xlWorksheet.Cells[i, "A"].Value2.ToString().ToLower(); string city = xlWorksheet.Cells[i, "B"].Value2.ToString().ToLower(); int rate = 0; bool parsed = int.TryParse(xlWorksheet.Cells[i, "G"].Value2.ToString(), out rate); foreach (var kv in perDiems) { if (kv.Value.State.ToLower() == state && kv.Value.City.ToLower() == city) { kv.Value.Rate = rate; } } } using (var db = new WorktripEntities()) { foreach (var kv in perDiems) { PerDiemRate pdr = new PerDiemRate() { AirportCode = kv.Key, Rate = kv.Value.Rate }; db.PerDiemRates.Add(pdr); } db.SaveChanges(); } }