Beispiel #1
0
        public async Task <ActionResult> Create(TripViewModel TripData)
        {
            try
            {
                string DispatcherName = System.Web.HttpContext.Current.User.Identity.Name;
                string message        = "";
                int    insertedTripId = 0;
                string VoucherNumber  = "";
                Trip   trip           = new Trip();
                if (TripData.VehicleId != 0)
                {
                    if (TripData.DriverId != 0)
                    {
                        if (_tripService.VehicleAvailability(TripData.VehicleId))
                        {
                            if (_tripService.DriverAvailability(TripData.DriverId))
                            {
                                int customerId = TripData.Customer != null?_customerService.GetCustomerId(TripData.Customer.PhoneNumber) : 0;

                                TripData.Customer = null;
                                if (customerId != 0)
                                {
                                    TripData.CustomerId = customerId;
                                    if (TripData != null)
                                    {
                                        trip        = AutoMapper.Mapper.Map <Trip>(TripData);
                                        trip.IsOpen = true;
                                        //trip.DispatchedHotel = Session["SelectedHotel"].ToString();
                                        trip.Createdby       = DispatcherName;
                                        trip.PaymentDateTime = CustomDataHelper.CurrentDateTimeSL.GetCurrentDate();
                                        trip.TimeIn          = CustomDataHelper.CurrentDateTimeSL.GetCurrentDate();
                                        trip.TimeOut         = CustomDataHelper.CurrentDateTimeSL.GetCurrentDate();
                                        trip.VoucherNumber   = GenarateVoucherNumber();
                                        VoucherNumber        = trip.VoucherNumber;
                                    }

                                    //upadte vehicle mete readings
                                    await Task.Run(() => _vehicleService.UpdateMeterReading(trip.VehicleId, Convert.ToInt32(TripData.MeterReadingOut)));

                                    //update vehicle is unavailable
                                    List <int> vehicle = new List <int>()
                                    {
                                        trip.VehicleId
                                    };
                                    await Task.Run(() => _vehicleService.UpdateVehicleUnAvailable(vehicle));

                                    //update driver is unavailable
                                    List <int> driver = new List <int>()
                                    {
                                        trip.DriverId
                                    };
                                    await Task.Run(() => _driverService.UpdateDriverUnAvailable(driver));


                                    if (trip != null)
                                    {
                                        //gps data collection
                                        if (TripData.VehicleNumber != null)
                                        {
                                            GpsVehicleDetailsDto gpsDetails = await _gpsClientService.GpsMeterReading(TripData.VehicleNumber);

                                            if (gpsDetails != null)
                                            {
                                                trip.MeterReadingInGps    = Convert.ToInt32(!string.IsNullOrEmpty(gpsDetails.meter_reading));
                                                trip.MeterReadingInStatus = gpsDetails.error;
                                                trip.MeterReadingInGap    = (trip.MeterReadingInGps - trip.MeterReadingIn);
                                            }
                                        }
                                        //save trip details
                                        insertedTripId = await Task.Run(() => _tripService.SaveTripDetails(trip));
                                    }

                                    if (TripData.PackageIds != null)
                                    {
                                        foreach (int id in TripData.PackageIds)
                                        {
                                            PreDefineTrip predefTrip = _preDefineTripService.GetPreDefineTripById(id);
                                            if (predefTrip != null)
                                            {
                                                PackagesList package = new PackagesList();
                                                package.TripId            = insertedTripId;
                                                package.PreDefineTripName = predefTrip.PreDefineTripName;
                                                package.Rate = predefTrip.Rate;

                                                await Task.Run(() => _packageListService.SavePackage(package));

                                                await Task.Run(() => _tripService.UpdatePackageCost(insertedTripId, predefTrip.Rate));
                                            }
                                        }
                                    }
                                    message = "Trip successfully created.";
                                }
                                else
                                {
                                    message = "Invalid customer(Save, if new customer.)";
                                }
                            }
                            else
                            {
                                message = "Driver is already assigned to another trip";
                            }
                        }
                        else
                        {
                            message = "Vehicle is already assigned to another trip";
                        }
                    }
                    else
                    {
                        message = "Driver cannot be null";
                    }
                }
                else
                {
                    message = "Vehicle cannot be null";
                }

                return(Json(new { VoucherNumber, message }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                return(RedirectToAction("Create", "Trips"));
            }
        }