public ActionResult Reporting()
        {
            try
            {
                // GET THE "hotelrates.json" FILE
                var jsonFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, hotelRatesFile);
                var hotelRates   = new HotelRates();
                using (StreamReader reader = new StreamReader(jsonFilePath))
                {
                    string json = reader.ReadToEnd();
                    hotelRates = JsonConvert.DeserializeObject <HotelRates>(json);
                }

                // GENERATE THE REPORT FROM THE DATA
                var reportFilePath = GenerateReport(hotelRates);
                if (reportFilePath == null)
                {
                    logger.Error($"Value for file {reportFilePath} was null.");
                    return(RedirectToAction("Error"));
                }

                // DOWLOAD THE FILE AT USER'S LOCAL MACHINE
                return(DownloadExtractedData(reportFilePath));
            }
            catch (FileNotFoundException ex)
            {
                logger.Error($"FileNotFoundException. Error: {ex.Message}. Stack trace: {ex.StackTrace}");
                return(RedirectToAction("Error"));
            }
            catch (Exception ex)
            {
                logger.Error($"Caught exception. Error: {ex.Message}. Stack trace: {ex.StackTrace}");
                return(RedirectToAction("Error"));
            }
        }
        public string GenerateReport(HotelRates hotelRates)
        {
            var reportFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, reportFile);

            DeleteFileIfExists(reportFilePath);

            var excelData = new List <ExcelData>();

            foreach (var hotelRate in hotelRates.hotelRates)
            {
                var departureDay  = Convert.ToDateTime(hotelRate.targetDay).AddDays(hotelRate.los).ToString();
                var hotelRateData = new ExcelData
                {
                    ArrivalDate       = FormatDateTime(hotelRate.targetDay),
                    DepartureDate     = FormatDateTime(departureDay),
                    Price             = hotelRate.Price.numericFloat,
                    Currency          = hotelRate.Price.currency,
                    RateName          = hotelRate.rateName,
                    Adults            = hotelRate.adults,
                    BreakfastIncluded = hotelRate.rateTags.First().shape ? 1 : 0
                };
                excelData.Add(hotelRateData);
            }

            var dt = new DataTable();

            dt.Columns.Add("ARRIVAL_DATE");
            dt.Columns.Add("DEPARTURE_DATE");
            dt.Columns.Add("PRICE");
            dt.Columns.Add("CURRENCY");
            dt.Columns.Add("RATENAME");
            dt.Columns.Add("ADULTS");
            dt.Columns.Add("BREAKFAST_INCLUDED");

            foreach (var item in excelData)
            {
                dt.Rows.Add(item.ArrivalDate, item.DepartureDate, item.Price, item.Currency,
                            item.RateName, item.Adults, item.BreakfastIncluded);
            }

            var wb = new XLWorkbook();
            var ws = wb.Worksheets.Add(dt, "Hotel_Rates");

            wb.SaveAs(reportFilePath); // SAVE THE FILE LOCALLY

            logger.Info($"Report file was generated successfully. Path = {reportFilePath}");
            return(reportFilePath);
        }
        public void ReportIsGeneratedSuccessfully()
        {
            var rateTag = new List <RateTags>();

            rateTag.Add(new RateTags
            {
                name  = "rate tag name",
                shape = true
            });

            var hotelRate = new List <HotelRate>();

            hotelRate.Add(new HotelRate {
                adults = 4,
                los    = 2,
                Price  = new Price
                {
                    currency       = "EUR",
                    numericFloat   = 187F,
                    numericInteger = 178
                },
                rateDescription = "Very long desc",
                rateID          = "rateId",
                rateName        = "rate name",
                rateTags        = rateTag,
                targetDay       = DateTime.Now.ToShortTimeString()
            });

            var hotelRates = new HotelRates
            {
                hotel = new Hotel
                {
                    hotelID        = 1,
                    classification = 6,
                    name           = "Test Hotel",
                    reviewscore    = 7.8F
                },
                hotelRates = hotelRate
            };

            var filepath = homeController.GenerateReport(hotelRates);

            Assert.IsTrue(File.Exists(filepath));
        }
Exemplo n.º 4
0
        public HotelRates Filter(int hotelId, DateTime?arrivalDate, string filterOperator)
        {
            if (!arrivalDate.HasValue)
            {
                return((from c in _hotelRates where c.hotel.hotelID == hotelId select c).FirstOrDefault());
            }

            var hotel         = (from c in _hotelRates where c.hotel.hotelID == hotelId select c).FirstOrDefault();
            var ratesFiltered = new List <HotelRate>();

            TimeSpan timeInitDay = new TimeSpan(0, 0, 0, 0);
            TimeSpan timeEndDay  = new TimeSpan(0, 23, 59, 59);

            DateTime dateInit = arrivalDate.Value.Add(timeInitDay);
            DateTime dateEnd  = arrivalDate.Value.Add(timeEndDay);

            if (hotel != null)
            {
                var ratesToFilter = hotel.hotelRates;

                ratesFiltered = filterOperator switch
                {
                    "=" => (from c in ratesToFilter where c.targetDay >= dateInit && c.targetDay <= dateEnd select c).ToList(),
                    "<" => (from c in ratesToFilter where c.targetDay < dateInit select c).ToList(),
                    "<=" => (from c in ratesToFilter where c.targetDay <= dateInit select c).ToList(),
                    ">" => (from c in ratesToFilter where c.targetDay > dateEnd select c).ToList(),
                    ">=" => (from c in ratesToFilter where c.targetDay >= dateEnd select c).ToList(),
                    _ => ratesToFilter,
                };

                var hotelReturn = new HotelRates
                {
                    hotel      = hotel.hotel,
                    hotelRates = ratesFiltered
                };

                return(hotelReturn);
            }

            return(null);
        }