コード例 #1
0
        //This Original Code: Can be cleared once we are sure the new function is doing fine.
        //public async Task<ActionResult> MotorUsage(string venueCodeSelected, string orderStatus)
        //{
        //    //GET DATA REQUIRED FOR CSV GENERATION
        //    OverviewDAL db = new OverviewDAL();
        //    RentalsDAL rentals_db = new RentalsDAL();

        //    DateTime TrackUnitStartDate = Convert.ToDateTime(Session["TrackUnitStartDate"]);
        //    DateTime TrackUnitEndDate = Convert.ToDateTime(Session["TrackUnitEndDate"]);

        //    IEnumerable<RentalOrder> fuelConsumptionPerMachineOrders = await db.GetRentalOrders(ActiveCustomer, TrackUnitStartDate, TrackUnitEndDate);
        //    IEnumerable<MotorUsage> motorUsages = db.GetDWHCustRunningHourPerMachinePerDay(ActiveCustomer, TrackUnitStartDate, TrackUnitEndDate);

        //    //Get rental status codes from query string
        //    if (!string.IsNullOrWhiteSpace(orderStatus))
        //    {
        //        string[] orderStatusCodes = orderStatus.Split(',');
        //        //Filter out machines with no matching order status
        //        fuelConsumptionPerMachineOrders = fuelConsumptionPerMachineOrders.Where(i => orderStatusCodes.Contains(i.OrderStatusId));
        //    }

        //    //Filter out machines with no matchinging order
        //    motorUsages = motorUsages.Where(i => fuelConsumptionPerMachineOrders.Select(x => x.ObjectId).Contains(i.SerialNumber));

        //    //Filterout machine with no run time data
        //    //below line is commented in response to the Ticket#367 so to avoid skipping the non-used dates
        //    //motorUsages = motorUsages.Where(i => i.RunningHour > 0.0m);

        //    //Get a list of non duplicate dates from the motor usage data
        //    List<DateTime> motorUsageDates = motorUsages.GroupBy(i => i.TransDate).Select(i => i.Key).OrderByDescending(i => i).ToList();

        //    //Get a list of non diplicate machine from the data (Serial number is the unique ID here)
        //    List<MotorUsage> motorUsageMachines = motorUsages.GroupBy(i => i.SerialNumber).Select(i => new MotorUsage() { SerialNumber = i.Key, MachineType = i.First().MachineType }).ToList();

        //    //Find the template breeam report
        //    var fi = new FileInfo(HttpContext.Server.MapPath("/App_Data/ReportTemp/motor-usage.xlsx"));

        //    //Load the template into memory and begin adding customer information
        //    using (var energyconsumption = new ExcelPackage(fi))
        //    {
        //        var ws = energyconsumption.Workbook.Worksheets["Consumption"];

        //        Dictionary<string, string> machineColumns = new Dictionary<string, string>();

        //        //Add Date column heading
        //        ws.Cells["A1:C1"].Merge = true;
        //        ws.Cells["A1"].Value = Resources.RunningHours;

        //        int headerRow = 3;
        //        string headingCell = "A" + headerRow.ToString();
        //        ws.Cells[headingCell].IsRichText = true;
        //        var dateRichText = ws.Cells[headingCell].RichText;
        //        //ws.Cells["A7"].Value =
        //        ws.Cells[headingCell].Style.Border.BorderAround(OfficeOpenXml.Style.ExcelBorderStyle.Thin);
        //        var dateRichTextValue = dateRichText.Add(Resources.Date);
        //        dateRichTextValue.Bold = true;

        //        //Loop though and create column headings for each of the machines
        //        for (int i = 0; i < motorUsageMachines.Count(); i++)
        //        {
        //            string columnName = utlStore.GetExcelColumn(i + 2);
        //            machineColumns.Add(motorUsageMachines.ElementAt(i).SerialNumber, columnName);
        //            var Cell = ws.Cells[columnName + headerRow.ToString()];

        //            Cell.IsRichText = true;
        //            var RichText = Cell.RichText;
        //            var RichValue = RichText.Add(motorUsageMachines.ElementAt(i).MachineType + "(" + motorUsageMachines.ElementAt(i).SerialNumber + ")" + Environment.NewLine);
        //            RichValue.Bold = true;
        //            RichValue = RichText.Add(Resources.StartRentalDate + ": " + TrackUnitStartDate.ToString("dd/MM/yyyy"));
        //            RichValue.Color = System.Drawing.Color.Green;
        //            RichValue.Bold = false;
        //            Cell.Style.Border.BorderAround(OfficeOpenXml.Style.ExcelBorderStyle.Thin);
        //            Cell.AutoFitColumns(30);
        //        }

        //        int rowCount = headerRow + 1;

        //        foreach (DateTime date in motorUsageDates)
        //        {
        //            ws.Cells["A" + rowCount.ToString()].Value = date.ToShortDateString();

        //            IEnumerable<MotorUsage> dateMachines = motorUsages.Where(i => i.TransDate.Date == date.Date);

        //            foreach (MotorUsage machine in dateMachines)
        //            {
        //                ws.Cells[machineColumns[machine.SerialNumber] + rowCount.ToString()].Value = machine.RunningHour.ToString("N2");
        //            }

        //            rowCount++;
        //        }
        //        return File(energyconsumption.GetAsByteArray(), ".xlsx", "Motor_Usage_" + TrackUnitStartDate.ToShortDateString() + "-" + TrackUnitEndDate.ToShortDateString() + "_" + ActiveCustomer.Name + ".xlsx");
        //    };
        //}

        private async Task <IEnumerable <MotorUsage> > GetMotroUsage(DateTime TrackUnitStartDate, DateTime TrackUnitEndDate, string orderStatus)
        {
            //GET DATA REQUIRED FOR CSV GENERATION
            OverviewDAL db         = new OverviewDAL();
            RentalsDAL  rentals_db = new RentalsDAL();



            IEnumerable <RentalOrder> fuelConsumptionPerMachineOrders = await db.GetRentalOrders(ActiveCustomer, TrackUnitStartDate, TrackUnitEndDate);

            IEnumerable <MotorUsage> motorUsages = db.GetDWHCustRunningHourPerMachinePerDay(ActiveCustomer, TrackUnitStartDate, TrackUnitEndDate);

            //Get rental status codes from query string
            if (!string.IsNullOrWhiteSpace(orderStatus))
            {
                string[] orderStatusCodes = orderStatus.Split(',');
                //Filter out machines with no matching order status
                fuelConsumptionPerMachineOrders = fuelConsumptionPerMachineOrders.Where(i => orderStatusCodes.Contains(i.OrderStatusId));
            }

            //Filter out machines with no matchinging order
            motorUsages = motorUsages.Where(i => fuelConsumptionPerMachineOrders.Select(x => x.ObjectId).Contains(i.SerialNumber));
            return(motorUsages);
        }