public ActionResult Approve(RunningchartModel model)
 {
     try
     {
         var user = Session[SessionKeys.UserInfo] as UserModel;
         runningchartService.ApproveRunningChart(model.RunningchartId, user.Id);
         return RedirectToAction("Index");
     }
     catch (Exception)
     {
         throw;
     }
 }
        public ActionResult Create()
        {
            IVehicleService vehicleServcie = new VehicleService();
            IProjectService projectService = new ProjectService();

            RunningchartModel model = new RunningchartModel();
            model.BillDate = DateTime.Now;
            model.Vehicles = ModelMapper.GetVehicleModelList(vehicleServcie.GetAllVehicles());
            model.Lubricants = ModelMapper.GetLubricantModelList(vehicleServcie.GetAllLubricantTypes());
            model.Projects = ModelMapper.GetProjectModelList(projectService.GetAllActiveProjects());

            ViewBag.Mode = "create";
            return View(model);
        }
        public ActionResult Create(RunningchartModel model)
        {
            try
            {
                Runningchart runningChart = ModelMapper.GetRunningchartFromRunningchartModel(model);
                runningChart.EnteredBy = (Session[SessionKeys.UserInfo] as UserModel).Id; // Set DEO
                int chartId = runningchartService.AddRunningchart(runningChart);
                return RedirectToAction("index", new { lastChartId = chartId });
            }
            catch
            {
                IVehicleService vehicleServcie = new VehicleService();
                IProjectService projectService = new ProjectService();

                model.RunningchartId = runningchartService.GetNextRunningchartId();
                model.Vehicles = ModelMapper.GetVehicleModelList(vehicleServcie.GetAllVehicles());
                model.Lubricants = ModelMapper.GetLubricantModelList(vehicleServcie.GetAllLubricantTypes());
                model.Projects = ModelMapper.GetProjectModelList(projectService.GetAllActiveProjects());

                return View(model);
            }
        }
        public static RunningchartModel GetRunningchartModel(Runningchart runningChart)
        {
            RunningchartModel model = new RunningchartModel()
            {
                RunningchartId = runningChart.Id,
                BillNumber = runningChart.BillNumber,
                BillDate = runningChart.BillDate,
                DriverName = runningChart.DriverName,
                SelectedVehicleId = runningChart.VehicleId,
                //SelectedVehicleNo = c.VehicleNumber, //TODO: Remove
                FuelLeftBegningOfDay = runningChart.FuelLeftBegning,
                FuelLeftEndOfDay = runningChart.FuelLeftEnd,
                FuelUsageOfDay = runningChart.FuelUsageOfDay,
                DailyNote = runningChart.DailyNote,
                DayStartime = runningChart.DayStartime,
                DayEndTime = runningChart.DayEndTime,
                IsApproved = runningChart.IsApproved,
                ApprovedBy = runningChart.ApprovedBy,
                EnteredBy = runningChart.EnteredBy,
                SelectedChartItems = GetChartItemModelList(runningChart.RunningchartDetails),
                SelectedPumpstations = GetChartPumpstationItemModelList(runningChart.RunningchartPumpstation),
                SelectedLubricants = GetChartLubricantItemModelList(runningChart.RunningchartLubricants),
            };

            return model;
        }
        public static Runningchart GetRunningchartFromRunningchartModel(RunningchartModel model)
        {
            Runningchart runningChart = new Runningchart()
            {
                BillNumber = model.BillNumber,
                BillDate = model.BillDate,
                DriverName = model.DriverName,
                VehicleId = model.SelectedVehicleId,
                //VehicleRate = model.vh
                FuelLeftBegning = model.FuelLeftBegningOfDay,
                FuelLeftEnd = model.FuelLeftEndOfDay,
                FuelUsageOfDay = model.FuelUsageOfDay,
                DailyNote = model.DailyNote,
                IsApproved = model.IsApproved,
                ApprovedBy = model.ApprovedBy,
                EnteredBy = model.EnteredBy,
                RunningchartDetails = new List<RunningchartDetails>(),
                RunningchartPumpstation = new List<RunningchartPumpstation>(),
                RunningchartLubricants = new List<RunningchartLubricant>()
            };

            var startTimePart = Convert.ToDateTime(model.DayStartime);
            var endTimePart = Convert.ToDateTime(model.DayEndTime);
            DateTime startTime = new DateTime(model.BillDate.Year, model.BillDate.Month, model.BillDate.Day,
                startTimePart.Hour, startTimePart.Minute, startTimePart.Second);
            DateTime endTime = new DateTime(model.BillDate.Year, model.BillDate.Month, model.BillDate.Day,
                endTimePart.Hour, endTimePart.Minute, endTimePart.Second);
            runningChart.DayStartime = startTime;
            runningChart.DayEndTime = endTime;

            foreach (var item in model.SelectedChartItems)
            {
                var startProjTimePart = Convert.ToDateTime(item.StartTime);
                var endProjTimePart = Convert.ToDateTime(item.EndTime);
                DateTime startProjTime = new DateTime(model.BillDate.Year, model.BillDate.Month, model.BillDate.Day,
                    startTimePart.Hour, startTimePart.Minute, startTimePart.Second);
                DateTime endProjTime = new DateTime(model.BillDate.Year, model.BillDate.Month, model.BillDate.Day,
                    endTimePart.Hour, endTimePart.Minute, endTimePart.Second);

                runningChart.RunningchartDetails.Add(new RunningchartDetails()
                {
                    StartTime = startProjTime,
                    EndTime = endProjTime,
                    TimeDifference = item.TimeDifference,
                    StartMeter = item.StartMeter,
                    EndMeter = item.EndMeter,
                    MeterDifference = item.MeterDifference,
                    IdleHours = item.IdleHours,
                    ProjectId = item.SelectedProjectId,
                    ProjectManagerName = item.SelectedProjectManager,
                    RentalTypeId = item.SelectedRentalTypeId
                });
            }

            if (model.SelectedPumpstations != null && model.SelectedPumpstations.Count > 0)
            {
                foreach (var item in model.SelectedPumpstations)
                {
                    if (item.SelectedPumpstationId > 0 && item.PumpAmount > 0)
                    {
                        runningChart.RunningchartPumpstation.Add(new RunningchartPumpstation()
                        {
                            PumpstationId = item.SelectedPumpstationId,
                            Amount = item.PumpAmount
                        });
                    }
                }
            }

            if (model.SelectedLubricants != null && model.SelectedLubricants.Count > 0)
            {
                foreach (var item in model.SelectedLubricants)
                {
                    if (item.SelectedPumpstationId > 0 && item.SelectedLubricantTypeId > 0 && item.PumpAmount > 0)
                    {
                        runningChart.RunningchartLubricants.Add(new RunningchartLubricant()
                        {
                            PumpstationId = item.SelectedPumpstationId,
                            LubricantTypeId = item.SelectedLubricantTypeId,
                            Amount = item.PumpAmount
                        });
                    }
                }
            }

            return runningChart;
        }
        public ActionResult PoulatePumpstationItems(bool isAddingNewItem)
        {
            RunningchartModel model = new RunningchartModel();
            List<ChartPumpstationItemModel> existingPumpstationItems = new List<ChartPumpstationItemModel>();
            UpdateModel<List<ChartPumpstationItemModel>>(existingPumpstationItems);

            IPumpstationService pumpstationService = new PumpstationService();
            model.Pumpstations = ModelMapper.GetPumpStationModelList(pumpstationService.GetAllPumpstations());
            model.SelectedPumpstations = new List<ChartPumpstationItemModel>();

            // Add existing chart items
            foreach (var item in existingPumpstationItems)
            {
                if (!item.IsRemoving)
                {
                    model.SelectedPumpstations.Add(item);
                }
            }

            // Add new chart item
            if (isAddingNewItem)
            {
                model.SelectedPumpstations.Add(new ChartPumpstationItemModel()
                {
                    SelectedPumpstationId = 0,
                    PumpAmount = 0
                });
            }

            return PartialView("_Pumpstations", model);
        }
        public ActionResult PoulateChartItems(bool isAddingNewItem, bool isVehicle)
        {
            RunningchartModel model = new RunningchartModel();
            List<ChartItemModel> existingChartItems = new List<ChartItemModel>();
            UpdateModel<List<ChartItemModel>>(existingChartItems);

            IProjectService projectService = new ProjectService();
            model.Projects = ModelMapper.GetProjectModelList(projectService.GetAllActiveProjects());
            model.VehicleRentalTypes = new List<VehicleRentalTypeModel>()
            {
                new VehicleRentalTypeModel() { Id = (int)RentalType.Company, RentalTypeName = StringEnum.GetEnumStringValue(RentalType.Company) },
                new VehicleRentalTypeModel() { Id = (int)RentalType.CompanyHired, RentalTypeName = StringEnum.GetEnumStringValue(RentalType.CompanyHired) },
                new VehicleRentalTypeModel() { Id = (int)RentalType.Hired, RentalTypeName = StringEnum.GetEnumStringValue(RentalType.Hired) }
            }; // Not worth to do a DB call since this is very static

            model.SelectedChartItems = new List<ChartItemModel>();
            model.isVehicle = isVehicle;

            // Add existing chart items
            foreach (var item in existingChartItems)
            {
                if (!item.IsRemoving)
                {
                    model.SelectedChartItems.Add(item);
                }
            }

            // Add new chart item
            if (isAddingNewItem)
            {
                ChartItemModel newChartItem = new ChartItemModel()
                {
                    StartTime = string.Empty,
                    EndTime = string.Empty,
                    SelectedProjectId = 0,
                    SelectedProjectManager = string.Empty
                };

                if (existingChartItems.Count > 0)
                {
                    newChartItem.SelectedProjectId = existingChartItems[0].SelectedProjectId;
                    newChartItem.SelectedProjectManager = existingChartItems[0].SelectedProjectManager;
                    newChartItem.SelectedRentalTypeId = existingChartItems[0].SelectedRentalTypeId;
                }

                model.SelectedChartItems.Add(newChartItem);
            }

            return PartialView("_ChartItems", model);
        }