private ProcessResult LogMissingAirbnbTrasactions(List <ImportFile> importFiles, DateTime logDate, FtpTransactionType fileTransactionType) { var result = new ProcessResult(); string localFile = string.Empty; try { // speed up bulk insertion _dbContext.Configuration.AutoDetectChangesEnabled = false; _dbContext.Configuration.ValidateOnSaveEnabled = false; string tempFolder = GetDownloadFolder(); var ftpService = new AirbnbFtpService(); var importService = new AirbnbImportService(); var payoutProvider = new OwnerPayoutService(_dbContext); foreach (var importFile in importFiles) { result.Count++; // download file from ftp site string csvFileUrl = importFile.Id; localFile = Path.Combine(tempFolder, importFile.Name); ftpService.Download(csvFileUrl, localFile); //DojoLogger.Info(string.Format("Download file {0} completed.", localFile)); // process only those data that is later than the most recent imported payout date in db DateTime?startPayoutDate = payoutProvider.GetMostRecentPayoutDate(importFile.Name); var count = importService.LogMissingCompletedAirbnbTransactions( localFile, logDate.ToString("yyyy-MM-dd"), startPayoutDate); if (count == -1) { result.Skip++; } else { result.Bad += count; } importService.DeleteFileIfAllowed(localFile); } } catch (Exception ex) { DojoLogger.Error(string.Format("Import file {0} fails. Exception: {1}", localFile, ex.Message)); // fall through } result.Good = result.Count - result.Bad - result.Skip; return(result); }
public JsonResult GetImportFileList(DateTime reportDate, FtpTransactionType reportType) { List <SelectListItem> fileList = new List <SelectListItem>(); try { var fptService = new AirbnbFtpService(); NameValueCollection urlList; if (reportType == FtpTransactionType.Completed) { urlList = fptService.GetFileList(FtpTransactionType.Completed, reportDate); } else if (reportType == FtpTransactionType.Future) { urlList = fptService.GetFileList(FtpTransactionType.Future, reportDate); } else { urlList = fptService.GetFileList(FtpTransactionType.Gross, reportDate); } // map NameValueCollection to SelectListItem foreach (var key in urlList.AllKeys) { fileList.Add(new SelectListItem { Text = key, Value = key + ";" + urlList[key] // js uses this format to parse out the filename and url }); } return(Json(fileList, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { DojoLogger.Error(ex.Message, typeof(OwnerPayoutController)); Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError; // code = 500 return(Json(fileList, JsonRequestBehavior.AllowGet)); } }
public AirbnbImportCalendarModel GetImportStatistics() { var calendarModel = new AirbnbImportCalendarModel(); try { // get the most recent owner payout date from db DateTime importDate = DateTime.Today; // default to today var mostRecentPayout = _context.OwnerPayouts.Where(x => x.IsDeleted == false).OrderByDescending(x => x.PayoutDate).FirstOrDefault(); if (mostRecentPayout != null) { importDate = mostRecentPayout.PayoutDate.Value; } DateTime startDate = importDate.Date.AddDays(-AirbnbImportCalendarModel.IMPORT_DISPLAY_DAYS); // retrieve IMPORT_DISPLAY_DAYS worth of data SqlParameter[] sqlParams = new SqlParameter[2]; sqlParams[0] = new SqlParameter("@StartDate", SqlDbType.DateTime); sqlParams[0].Value = startDate; sqlParams[1] = new SqlParameter("@Account", SqlDbType.NVarChar); sqlParams[1].Value = string.Empty; // get all accounts var importItems = _context.Database.SqlQuery <AirbnbImportAccountItemModel>("GetImportStatistics @StartDate, @Account", sqlParams).ToList(); string lastDateText = importItems.OrderByDescending(x => x.ImportDate).Select(x => x.ImportDate).FirstOrDefault(); if (lastDateText != null) { var latestDate = DateTime.ParseExact(lastDateText, "yyyy-MM-dd", new CultureInfo("en-US")); calendarModel.LatestDate = latestDate; calendarModel.DisplayDays = AirbnbImportCalendarModel.IMPORT_DISPLAY_DAYS; // days to display calendarModel.CalendarHeaderRow = MakeCalendarHeader(latestDate, calendarModel.DisplayDays); var currentAccount = string.Empty; AirbnbImportCalendarRowModel calendarRow = null; foreach (var item in importItems) { int column = ComputeColumn(latestDate, item.ImportDate); if (column >= 0 && column < calendarModel.DisplayDays) { if (currentAccount != item.Account) { if (calendarRow != null) { calendarModel.calendarRows.Add(calendarRow); } calendarRow = new AirbnbImportCalendarRowModel(calendarModel.DisplayDays); currentAccount = item.Account; calendarRow.Account = item.Account.Substring(0, item.Account.IndexOf("@")); } if (calendarRow != null) { calendarRow.CalendarCols[column].Column = column; calendarRow.CalendarCols[column].ReservationData = item.ReservationCount.ToString(); calendarRow.CalendarCols[column].ResolutionData = item.ResolutionCount.ToString(); calendarRow.CalendarCols[column].GrossData = string.Empty; } } } // the last row if (calendarRow != null) { calendarModel.calendarRows.Add(calendarRow); } // fill in missing active accounts from AirbnAccount var airbnbAccounts = _context.AirbnbAccounts.Where(x => x.Status == "Active").Select(x => x.Email).ToList(); foreach (string account in airbnbAccounts) { var accountName = account.Substring(0, account.IndexOf("@")); var row = calendarModel.calendarRows.Where(x => x.Account == accountName).FirstOrDefault(); if (row == null) // these files has no data for the display range { var accountRow = new AirbnbImportCalendarRowModel(calendarModel.DisplayDays); accountRow.Account = accountName; accountRow.HasInputFile = 0; calendarModel.calendarRows.Add(accountRow); } } calendarModel.calendarRows = calendarModel.calendarRows.OrderBy(x => x.Account).ToList(); // mark accounts for associated inpurt files AirbnbFtpService ftpService = new AirbnbFtpService(); var ftpDirectories = ftpService.GetFolderList(AirbnbFtpConfig.CompletedFtpUrl); string dirName = latestDate.ToString("MMMM d yyyy"); var ftpFileUrls = ftpService.GetFileList(ftpDirectories[dirName]); foreach (string fileName in ftpFileUrls.AllKeys) { var accountName = fileName.Substring(0, fileName.IndexOf("@")); var row = calendarModel.calendarRows.Where(x => x.Account == accountName).FirstOrDefault(); if (row != null) { row.HasInputFile = 1; // has current date data file } } // check date before most recent date for missing file dirName = latestDate.AddDays(-1).ToString("MMMM d yyyy"); ftpFileUrls = ftpService.GetFileList(ftpDirectories[dirName]); foreach (string fileName in ftpFileUrls.AllKeys) { var accountName = fileName.Substring(0, fileName.IndexOf("@")); var row = calendarModel.calendarRows.Where(x => x.Account == accountName).FirstOrDefault(); if (row != null) { row.HasInputFile ^= 2; // has previous date's data file } } // check 2 date before most recent date for missing file dirName = latestDate.AddDays(-2).ToString("MMMM d yyyy"); ftpFileUrls = ftpService.GetFileList(ftpDirectories[dirName]); foreach (string fileName in ftpFileUrls.AllKeys) { var accountName = fileName.Substring(0, fileName.IndexOf("@")); var row = calendarModel.calendarRows.Where(x => x.Account == accountName).FirstOrDefault(); if (row != null) { row.HasInputFile ^= 4; // has previous's previous date's data file } } } } catch { throw; } return(calendarModel); }
private ProcessResult ImportAirbnbTrasactions(List <ImportFile> importFiles, DateTime importDate, FtpTransactionType fileTransactionType) { var result = new ProcessResult(); string localFile = string.Empty; try { // speed up bulk insertion _dbContext.Configuration.AutoDetectChangesEnabled = false; _dbContext.Configuration.ValidateOnSaveEnabled = false; string tempFolder = GetDownloadFolder(); var ftpService = new AirbnbFtpService(); var importService = new AirbnbImportService(); DojoLogger.Info(string.Format("Temporary download folder is {0} for {1} files from FTP server.", tempFolder, importFiles.Count.ToString())); if (fileTransactionType == FtpTransactionType.Completed) { var payoutProvider = new OwnerPayoutService(_dbContext); foreach (var importFile in importFiles) { result.Count++; // download file from ftp site string csvFileUrl = importFile.Id; localFile = Path.Combine(tempFolder, importFile.Name); ftpService.Download(csvFileUrl, localFile); //DojoLogger.Info(string.Format("Download file {0} completed.", localFile)); // process only those data that is later than the most recent imported payout date in db DateTime?startPayoutDate = payoutProvider.GetMostRecentPayoutDate(importFile.Name); var count = importService.ImportCompletedAirbnbTransactions( localFile, importDate.ToString("yyyy-MM-dd"), startPayoutDate); if (count == -1) { result.Skip++; } else { result.Bad += count; } importService.DeleteFileIfAllowed(localFile); } } else if (fileTransactionType == FtpTransactionType.Future) { var reservationProvider = new ReservationService(_dbContext); string mostRecentDateString = reservationProvider.GetMostRecentFutureDate(); foreach (var importFile in importFiles) { result.Count++; //string sortableDate = importService.ParseDateFromTransactionFileUrl(importFile.Id); //if (string.Compare(sortableDate, mostRecentDateString) > 0) //{ // download file from ftp site string csvFileUrl = importFile.Id; localFile = Path.Combine(tempFolder, importFile.Name); ftpService.Download(csvFileUrl, localFile); var count = importService.ImportFutureAirbnbTransactions(localFile, importDate.ToString("yyyy-MM-dd")); if (count == -1) { result.Skip++; } else { result.Bad += count; } importService.DeleteFileIfAllowed(localFile); //} //else // result.Skip++; } } else { var reservationProvider = new ReservationService(_dbContext); string mostRecentDateString = reservationProvider.GetMostRecentGrossDate(); foreach (var importFile in importFiles) { result.Count++; //string sortableDate = importService.ParseDateFromTransactionFileUrl(importFile.Id); //if (string.Compare(sortableDate, mostRecentDateString) > 0) //{ // download file from ftp site string csvFileUrl = importFile.Id; localFile = Path.Combine(tempFolder, importFile.Name); ftpService.Download(csvFileUrl, localFile); var count = importService.ImportGrossEarningTransactions(localFile, importDate.ToString("yyyy-MM-dd")); if (count == -1) { result.Skip++; } else { result.Bad += count; } importService.DeleteFileIfAllowed(localFile); } } } catch (Exception ex) { DojoLogger.Error(string.Format("Import file {0} fails. Exception: {1}", localFile, ex.Message)); // fall through } result.Good = result.Count - result.Bad - result.Skip; return(result); }