Пример #1
0
        public async Task <ActionResult> Edit(TripViewModel model)
        {
            try
            {
                string DispatcherName = System.Web.HttpContext.Current.User.Identity.Name;
                string message        = "";
                Trip   trip           = new Trip();
                if (model.VehicleId != 0)
                {
                    if (model.DriverId != 0)
                    {
                        if (model.MeterReadingIn > 0)
                        {
                            if (model.BataRateId != 0)
                            {
                                BataRate bataRate = _bataRateService.GetBataRateById(model.BataRateId);
                                if (bataRate != null)
                                {
                                    TripBata bataDetails = new TripBata();
                                    bataDetails.TripId      = model.TripId;
                                    bataDetails.Description = bataRate.Description;
                                    bataDetails.Amount      = bataRate.Amount;
                                    if (_tripBataService.IsBattaNotExists(model.TripId))
                                    {
                                        _tripBataService.SaveBataData(bataDetails);
                                    }
                                }
                            }

                            //update vehicle is available
                            List <int> vehicle = new List <int>()
                            {
                                model.VehicleId
                            };
                            await Task.Run(() => _vehicleService.UpdateVehicleAvailable(vehicle));

                            //update driver is available
                            List <int> driver = new List <int>()
                            {
                                model.DriverId
                            };
                            await Task.Run(() => _driverService.UpdateDriverAvailable(driver));

                            if (model != null)
                            {
                                trip             = AutoMapper.Mapper.Map <Trip>(model);
                                trip.Updatedby   = DispatcherName;
                                trip.TripMileage = (trip.MeterReadingIn - trip.MeterReadingOut);

                                if (model.PackageIds != null && model.PackageIds.Count() > 0)
                                {
                                    foreach (int pid in model.PackageIds)
                                    {
                                        PreDefineTrip predefTrip = _preDefineTripService.GetPreDefineTripById(pid);
                                        if (predefTrip != null)
                                        {
                                            PackagesList package = new PackagesList();
                                            package.TripId            = model.TripId;
                                            package.PreDefineTripName = predefTrip.PreDefineTripName;
                                            package.Rate = predefTrip.Rate;
                                            if (_packageListService.IsPackageExists(model.TripId, predefTrip.PreDefineTripName))
                                            {
                                                await Task.Run(() => _packageListService.SavePackage(package));
                                            }
                                        }
                                    }
                                }
                                //gps data collection
                                if (model.VehicleNumber != null)
                                {
                                    GpsVehicleDetailsDto gpsDetails = await _gpsClientService.GpsMeterReading(model.VehicleNumber);

                                    if (gpsDetails != null)
                                    {
                                        trip.MeterReadingInGps    = Convert.ToInt32(!string.IsNullOrEmpty(gpsDetails.meter_reading));
                                        trip.MeterReadingInStatus = gpsDetails.error;
                                        trip.MeterReadingInGap    = (trip.MeterReadingInGps - trip.MeterReadingIn);
                                    }
                                }

                                //update vehicle meter reading
                                await Task.Run(() => _vehicleService.UpdateMeterReading(model.VehicleId, Convert.ToInt32(model.MeterReadingIn)));

                                //save trip details
                                await Task.Run(() => _tripService.EditTrip(model.TripId, trip));

                                await Task.Run(() => _tripService.ArchiveTripById(model.TripId));
                            }
                        }
                        else
                        {
                            message = "Error in meter reading in";
                        }
                    }
                    else
                    {
                        message = "Driver cannot be null";
                    }
                }
                else
                {
                    message = "Vehicle cannot be null";
                }
                return(Json(message, JsonRequestBehavior.AllowGet));
            }
            catch (Exception e)
            {
                return(Json(e.Message));
            }
        }