private void SelfReplacement_Save() { dlo = new DataLoadOptions(); dlo.LoadWith<Ajancy.Person>(p => p.User); dlo.LoadWith<Ajancy.User>(u => u.UsersInRoles); dlo.LoadWith<Ajancy.Person>(p => p.DriverCertifications); dlo.LoadWith<Ajancy.DriverCertification>(dc => dc.DriverCertificationCars); dlo.LoadWith<Ajancy.DriverCertificationCar>(dcc => dcc.CarPlateNumber); dlo.LoadWith<Ajancy.CarPlateNumber>(cpn => cpn.Car); dlo.LoadWith<Ajancy.Car>(c => c.FuelCards); db = new Ajancy.Kimia_Ajancy(Public.ConnectionString); db.LoadOptions = dlo; #region AjancyType Ajancy.Person person = db.Persons.First<Ajancy.Person>(p => p.PersonID == Public.ToInt(this.ViewState["PersonID"])); Ajancy.DriverCertification driverCertification = person.DriverCertifications.First<Ajancy.DriverCertification>(dc => dc.CertificationType == (byte)Public.AjancyType.TaxiAjancy); Ajancy.DriverCertificationCar driverCertificationCar = driverCertification.DriverCertificationCars.Last<Ajancy.DriverCertificationCar>(dcc => dcc.LockOutDate == null); Ajancy.CarPlateNumber carPlateNumber = driverCertificationCar.CarPlateNumber; Ajancy.ZonePlateNumber plateNumber = carPlateNumber.ZonePlateNumber; Ajancy.Car car = carPlateNumber.Car; Ajancy.FuelCard fuelCard = car.FuelCards.Last<Ajancy.FuelCard>(); if (db.Cars.Any<Ajancy.Car>(c => c.VIN == this.txtCarVIN.Text.Trim() && c.CarID != car.CarID)) { this.lblMessage.Text = "خودرو بخش کارت سوخت آژانسی با این شماره VIN متعلق به شخص دیگری میباشد"; return; } if (db.FuelCards.Any<Ajancy.FuelCard>(fc => fc.PAN == this.txtFuelCardPAN.Text && fc.FuelCardID != fuelCard.FuelCardID)) { this.lblMessage.Text = "شماره PAN کارت سوخت خودرو بخش کارت سوخت آژانسی قبلا برای خودرو دیگری ثبت شده"; return; } Ajancy.ZonePlateNumber pln = db.ZonePlateNumbers.FirstOrDefault<Ajancy.ZonePlateNumber>(pl => pl.CityID == Public.ToShort(this.drpCarPlateNumberCity.SelectedValue) && pl.Number == this.txtCarPlateNumber_5.Text.Trim() && pl.ZonePlateNumberID != carPlateNumber.ZonePlateNumberID); if (pln != null) { this.lblMessage.Text = "شماره پلاک وارد شده در بخش کارت سوخت آژانسی متعلق به شخص دیگری میباشد"; return; } // --------------- setting values driverCertificationCar.LockOutDate = DateTime.Now; driverCertificationCar.AjancyDrivers.Last<Ajancy.AjancyDriver>(jd => jd.LockOutDate == null).LockOutDate = DateTime.Now; person.FirstName = this.txtFirstName.Text.Trim(); person.LastName = this.txtLastName.Text.Trim(); plateNumber.CityID = Public.ToShort(this.drpCarPlateNumberCity.SelectedValue); plateNumber.Number = this.txtCarPlateNumber_5.Text.Trim(); car.CarTypeID = Public.ToShort(this.drpCarType.SelectedValue); car.FuelType = Public.ToByte(this.drpFuelType.SelectedValue); car.Model = this.txtCarModel.Text; fuelCard.CardType = (byte)Public.FuelCardType.Ajancy; fuelCard.PAN = this.txtFuelCardPAN.Text.Trim(); fuelCard.DiscardDate = DateTime.Now; car.VIN = this.txtCarVIN.Text.Trim().ToUpper(); #endregion #region PersonalType if (db.Cars.Any<Ajancy.Car>(c => c.VIN == this.txtCarVIN_2.Text.Trim())) { this.lblMessage.Text = "خودرو بخش کارت سوخت شخصی با این شماره VIN متعلق به شخص دیگری میباشد"; return; } if (db.FuelCards.Any<Ajancy.FuelCard>(fc => fc.PAN == this.txtFuelCardPAN_2.Text)) { this.lblMessage.Text = "شماره PAN کارت سوخت خودرو بخش کارت سوخت شخصی قبلا برای خودرو دیگری ثبت شده"; return; } Ajancy.ZonePlateNumber pnl = db.ZonePlateNumbers.FirstOrDefault<Ajancy.ZonePlateNumber>(pl => pl.CityID == Public.ToShort(this.drpCarPlateNumberCity_2.SelectedValue) && pl.Number == this.txtCarPlateNumber_5_2.Text.Trim()); if (pnl != null) { this.lblMessage.Text = "شماره پلاک وارد شده در بخش کارت سوخت شخصی متعلق به شخص دیگری میباشد"; return; } Ajancy.DriverCertificationCar driverCertificationCar_2 = new Ajancy.DriverCertificationCar { SubmitDate = DateTime.Now }; Ajancy.CarPlateNumber carPlateNumber_2 = new Ajancy.CarPlateNumber(); Ajancy.ZonePlateNumber plateNumber_2 = new Ajancy.ZonePlateNumber(); Ajancy.Car car_2 = new Ajancy.Car(); Ajancy.FuelCard fuelCard_2 = new Ajancy.FuelCard { SubmitDate = DateTime.Now }; driverCertificationCar_2.CarPlateNumber = carPlateNumber_2; carPlateNumber_2 = new Ajancy.CarPlateNumber { Car = car_2, ZonePlateNumber = plateNumber_2 }; driverCertificationCar_2 = new Ajancy.DriverCertificationCar { CarPlateNumber = carPlateNumber_2, SubmitDate = DateTime.Now }; driverCertificationCar_2.AjancyDrivers.Add(new Ajancy.AjancyDriver { AjancyID = Public.ToInt(this.drpAjancies_2.SelectedValue), MembershipDate = DateTime.Now }); driverCertification.DriverCertificationCars.Add(driverCertificationCar_2); car_2.FuelCards.Add(fuelCard_2); // --------------- setting values if (this.txtCarPlateNumber_5.Text.Trim().Equals(this.txtCarPlateNumber_5_2.Text.Trim()) && this.drpCarPlateNumberCity.SelectedValue.Equals(this.drpCarPlateNumberCity_2.SelectedValue)) { carPlateNumber_2.ZonePlateNumber = plateNumber; } //else //{ // plateNumber_2.Number = this.txtCarPlateNumber_5_2.Text.Trim(); // plateNumber_2.CityID = Public.ToShort(this.drpCarPlateNumberCity_2.SelectedValue); //} plateNumber_2.Number = this.txtCarPlateNumber_5_2.Text.Trim(); plateNumber_2.CityID = Public.ToShort(this.drpCarPlateNumberCity_2.SelectedValue); car_2.CarTypeID = Public.ToShort(this.drpCarType_2.SelectedValue); car_2.FuelType = Public.ToByte(this.drpFuelType_2.SelectedValue); car_2.Model = this.txtCarModel_2.Text; fuelCard_2.CardType = (byte)Public.FuelCardType.Ajancy; fuelCard_2.PAN = this.txtFuelCardPAN_2.Text.Trim(); car_2.VIN = this.txtCarVIN_2.Text.Trim().ToUpper(); #endregion switch ((Public.Role)Public.ActiveUserRole.RoleID) { case Public.Role.ProvinceManager: person.User.CityID = Public.ToShort(this.drpCity.SelectedValue); break; case Public.Role.Admin: person.User.ProvinceID = Public.ToByte(this.drpProvince.SelectedValue); person.User.CityID = Public.ToShort(this.drpCity.SelectedValue); break; } #region Owners bool ajancyTypeOwner = false; bool personalTypeOwner = false; Ajancy.Person ownerPer = null; Ajancy.Person ownerPer_2 = null; if (!string.IsNullOrEmpty(this.txtOwnerName.Text.Trim()) && !string.IsNullOrEmpty(this.txtOwnerFamily.Text.Trim()) && !string.IsNullOrEmpty(this.txtOwnerNationalCode.Text.Trim()) && !this.txtOwnerNationalCode.Text.Trim().Equals(person.NationalCode)) { ajancyTypeOwner = true; } if (!string.IsNullOrEmpty(this.txtOwnerName_2.Text.Trim()) && !string.IsNullOrEmpty(this.txtOwnerFamily_2.Text.Trim()) && !string.IsNullOrEmpty(this.txtOwnerNationalCode_2.Text.Trim()) && !this.txtOwnerNationalCode_2.Text.Trim().Equals(person.NationalCode)) { personalTypeOwner = true; } if (ajancyTypeOwner && personalTypeOwner && this.txtOwnerNationalCode_2.Text == this.txtOwnerNationalCode.Text) // Both owners are the same person { ownerPer = db.Persons.FirstOrDefault<Ajancy.Person>(p => p.NationalCode == this.txtOwnerNationalCode.Text.Trim()); if (ownerPer == null) { ownerPer = new Ajancy.Person { NationalCode = this.txtOwnerNationalCode.Text.Trim(), SubmitDate = DateTime.Now }; Ajancy.User ownerUser = new Ajancy.User { UserName = this.txtOwnerNationalCode.Text.Trim() , ProvinceID = person.User.ProvinceID , CityID = person.User.CityID , SubmitDate = DateTime.Now }; ownerUser.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = (short)Public.Role.CarOwner, MembershipDate = DateTime.Now, LockOutDate = DateTime.Now }); ownerPer.User = ownerUser; db.Persons.InsertOnSubmit(ownerPer); } else if (!ownerPer.User.UsersInRoles.Any<Ajancy.UsersInRole>(ur => ur.RoleID == (short)Public.Role.CarOwner)) { ownerPer.User.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = (short)Public.Role.CarOwner, MembershipDate = DateTime.Now, LockOutDate = DateTime.Now }); } ownerPer.FirstName = this.txtOwnerName.Text.Trim(); ownerPer.LastName = this.txtOwnerFamily.Text.Trim(); ownerPer.CarPlateNumbers.Add(carPlateNumber); ownerPer.CarPlateNumbers.Add(carPlateNumber_2); } else { if (personalTypeOwner && this.txtNationalCode.Text == this.txtOwnerNationalCode_2.Text) // Cross / { if (!person.User.UsersInRoles.Any<Ajancy.UsersInRole>(ur => ur.RoleID == (short)Public.Role.CarOwner)) { person.User.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = (short)Public.Role.CarOwner, MembershipDate = DateTime.Now, LockOutDate = DateTime.Now }); } person.CarPlateNumbers.Add(carPlateNumber_2); } else { if (personalTypeOwner) { ownerPer_2 = db.Persons.FirstOrDefault<Ajancy.Person>(p => p.NationalCode == this.txtOwnerNationalCode_2.Text.Trim()); if (ownerPer_2 == null) { ownerPer_2 = new Ajancy.Person { NationalCode = this.txtOwnerNationalCode_2.Text.Trim(), SubmitDate = DateTime.Now }; Ajancy.User ownerUser = new Ajancy.User { UserName = this.txtOwnerNationalCode_2.Text.Trim() , ProvinceID = person.User.ProvinceID , CityID = person.User.CityID , SubmitDate = DateTime.Now }; ownerUser.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = (short)Public.Role.CarOwner, MembershipDate = DateTime.Now, LockOutDate = DateTime.Now }); ownerPer_2.User = ownerUser; db.Persons.InsertOnSubmit(ownerPer_2); } else if (!ownerPer_2.User.UsersInRoles.Any<Ajancy.UsersInRole>(ur => ur.RoleID == (short)Public.Role.CarOwner)) { ownerPer_2.User.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = (short)Public.Role.CarOwner, MembershipDate = DateTime.Now, LockOutDate = DateTime.Now }); } ownerPer_2.FirstName = this.txtOwnerName_2.Text.Trim(); ownerPer_2.LastName = this.txtOwnerFamily_2.Text.Trim(); ownerPer_2.CarPlateNumbers.Add(carPlateNumber_2); } else { person.CarPlateNumbers.Add(carPlateNumber_2); if (carPlateNumber_2.OwnerPersonID > 0 && carPlateNumber_2.OwnerPersonID != person.PersonID) // Set driver as owner again { carPlateNumber_2.Person = person; } } } if (ajancyTypeOwner && this.txtNationalCode_2.Text == this.txtOwnerNationalCode.Text) // Cross \ { if (!person.User.UsersInRoles.Any<Ajancy.UsersInRole>(ur => ur.RoleID == (short)Public.Role.CarOwner)) { person.User.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = (short)Public.Role.CarOwner, MembershipDate = DateTime.Now, LockOutDate = DateTime.Now }); } person.CarPlateNumbers.Add(carPlateNumber); } else { if (ajancyTypeOwner) { ownerPer = db.Persons.FirstOrDefault<Ajancy.Person>(p => p.NationalCode == this.txtOwnerNationalCode.Text.Trim()); if (ownerPer == null) { ownerPer = new Ajancy.Person { NationalCode = this.txtOwnerNationalCode.Text.Trim(), SubmitDate = DateTime.Now }; Ajancy.User ownerUser = new Ajancy.User { UserName = this.txtOwnerNationalCode.Text.Trim() , ProvinceID = person.User.ProvinceID , CityID = person.User.CityID , SubmitDate = DateTime.Now }; ownerUser.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = (short)Public.Role.CarOwner, MembershipDate = DateTime.Now, LockOutDate = DateTime.Now }); ownerPer.User = ownerUser; db.Persons.InsertOnSubmit(ownerPer); } else if (!ownerPer.User.UsersInRoles.Any<Ajancy.UsersInRole>(ur => ur.RoleID == (short)Public.Role.CarOwner)) { ownerPer.User.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = (short)Public.Role.CarOwner, MembershipDate = DateTime.Now, LockOutDate = DateTime.Now }); } ownerPer.FirstName = this.txtOwnerName.Text.Trim(); ownerPer.LastName = this.txtOwnerFamily.Text.Trim(); ownerPer.CarPlateNumbers.Add(carPlateNumber); } else { person.CarPlateNumbers.Add(carPlateNumber); if (carPlateNumber.OwnerPersonID > 0 && carPlateNumber.OwnerPersonID != person.PersonID) // Set driver as owner again { carPlateNumber.Person = person; } } } } #endregion if (Public.ActiveUserRole.RoleID == (short)Public.Role.ProvinceManager) { person.User.CityID = Public.ToShort(this.drpCity.SelectedValue); } Ajancy.FuelCardSubstitution formerReq = null; if (fuelCard.FuelCardID > 0) { formerReq = db.FuelCardSubstitutions.FirstOrDefault<Ajancy.FuelCardSubstitution>(fcs => (fcs.AjancyTypeFuelCardID == fuelCard.FuelCardID || fcs.PersonalTypeFuelCardID.GetValueOrDefault() == fuelCard.FuelCardID)); if (formerReq != null) { this.lblMessage.Text = "برای کارت سوخت بخش آژانسی قبلا درخواست ابطال یا جایگزین ثبت شده است"; return; } } if (fuelCard_2.FuelCardID > 0) { formerReq = db.FuelCardSubstitutions.FirstOrDefault<Ajancy.FuelCardSubstitution>(fcs => (fcs.AjancyTypeFuelCardID == fuelCard_2.FuelCardID || fcs.PersonalTypeFuelCardID.GetValueOrDefault() == fuelCard_2.FuelCardID)); if (formerReq != null) { this.lblMessage.Text = "برای کارت سوخت بخش شخصی قبلا درخواست ابطال یا جایگزین ثبت شده است"; return; } } formerReq = new Ajancy.FuelCardSubstitution { FuelCard = fuelCard, UserInRoleID = Public.ActiveUserRole.UserRoleID, SubmitDate = DateTime.Now }; db.FuelCardSubstitutions.InsertOnSubmit(formerReq); db.SubmitChanges(); formerReq.PersonalTypeFuelCardID = fuelCard_2.FuelCardID; db.SubmitChanges(); DisposeContext(); Response.Redirect("~/Message.aspx?mode=11"); }
protected void btnSave_Click(object sender, EventArgs e) { if (Page.IsValid) { DataLoadOptions dlo = new DataLoadOptions(); dlo.LoadWith<Ajancy.Person>(p => p.User); dlo.LoadWith<Ajancy.User>(u => u.UsersInRoles); dlo.LoadWith<Ajancy.Person>(p => p.DriverCertifications); dlo.LoadWith<Ajancy.DriverCertification>(dc => dc.DriverCertificationCars); dlo.LoadWith<Ajancy.DriverCertificationCar>(dcc => dcc.CarPlateNumber); dlo.LoadWith<Ajancy.CarPlateNumber>(cpn => cpn.Car); dlo.LoadWith<Ajancy.Car>(c => c.FuelCards); db = new Ajancy.Kimia_Ajancy(Public.ConnectionString); db.LoadOptions = dlo; #region AjancyType Ajancy.Person person = new Ajancy.Person(); Ajancy.DriverCertification driverCertification = new Ajancy.DriverCertification(); Ajancy.DriverCertificationCar driverCertificationCar = new Ajancy.DriverCertificationCar(); Ajancy.CarPlateNumber carPlateNumber = new Ajancy.CarPlateNumber(); Ajancy.ZonePlateNumber plateNumber = new Ajancy.ZonePlateNumber(); Ajancy.Car car = new Ajancy.Car(); Ajancy.FuelCard fuelCard = new Ajancy.FuelCard(); if (this.ViewState["PersonID"] == null) // New Person { if (db.Cars.Any<Ajancy.Car>(c => c.VIN == this.txtCarVIN.Text.Trim())) { this.lblMessage.Text = "خودرو بخش کارت سوخت آژانسی با این شماره VIN متعلق به شخص دیگری میباشد"; return; } if (db.FuelCards.Any<Ajancy.FuelCard>(fc => fc.PAN == this.txtFuelCardPAN.Text)) { this.lblMessage.Text = "شماره PAN کارت سوخت خودرو بخش کارت سوخت آژانسی قبلا برای خودرو دیگری ثبت شده"; return; } Ajancy.ZonePlateNumber pln = db.ZonePlateNumbers.FirstOrDefault<Ajancy.ZonePlateNumber>(pl => pl.CityID == Public.ToShort(this.drpCarPlateNumberCity.SelectedValue) && pl.Number == this.txtCarPlateNumber_5.Text.Trim()); if (pln != null) { this.lblMessage.Text = "شماره پلاک وارد شده در بخش کارت سوخت آژانسی متعلق به شخص دیگری میباشد"; return; } Ajancy.User user = new Ajancy.User(); user.UserName = this.txtNationalCode.Text.Trim(); user.ProvinceID = Public.ToByte(this.drpProvince.SelectedValue); user.CityID = Public.ToShort(this.drpCity.SelectedValue); user.SubmitDate = DateTime.Now; user.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = (short)Public.Role.TaxiDriver, MembershipDate = DateTime.Now }); person.User = user; person.NationalCode = this.txtNationalCode.Text.Trim(); person.SubmitDate = DateTime.Now; driverCertification = new Ajancy.DriverCertification { CertificationType = (byte)Public.AjancyType.TaxiAjancy, SubmitDate = DateTime.Now }; carPlateNumber = new Ajancy.CarPlateNumber { Car = car, ZonePlateNumber = plateNumber, Person = person }; driverCertificationCar = new Ajancy.DriverCertificationCar { CarPlateNumber = carPlateNumber, SubmitDate = DateTime.Now }; driverCertificationCar.AjancyDrivers.Add(new Ajancy.AjancyDriver { AjancyID = Public.ToInt(this.drpAjancies.SelectedValue), MembershipDate = DateTime.Now }); driverCertification.DriverCertificationCars.Add(driverCertificationCar); person.DriverCertifications.Add(driverCertification); fuelCard.SubmitDate = DateTime.Now; car.FuelCards.Add(fuelCard); db.Persons.InsertOnSubmit(person); } else // Person Exists { person = db.Persons.FirstOrDefault<Ajancy.Person>(p => p.PersonID == Public.ToInt(this.ViewState["PersonID"])); if (person.User.UsersInRoles.Any<Ajancy.UsersInRole>(ur => ur.RoleID == (short)Public.Role.TaxiDriver)) // Person is a driver { driverCertification = person.DriverCertifications.First<Ajancy.DriverCertification>(dc => dc.CertificationType == (byte)Public.AjancyType.TaxiAjancy); driverCertificationCar = driverCertification.DriverCertificationCars.Last<Ajancy.DriverCertificationCar>(dcc => dcc.LockOutDate == null); carPlateNumber = driverCertificationCar.CarPlateNumber; plateNumber = carPlateNumber.ZonePlateNumber; car = carPlateNumber.Car; fuelCard = car.FuelCards.Last<Ajancy.FuelCard>(); if (db.Cars.Any<Ajancy.Car>(c => c.VIN == this.txtCarVIN.Text.Trim() && c.CarID != car.CarID)) { this.lblMessage.Text = "خودرو بخش کارت سوخت آژانسی با این شماره VIN متعلق به شخص دیگری میباشد"; return; } if (db.FuelCards.Any<Ajancy.FuelCard>(fc => fc.PAN == this.txtFuelCardPAN.Text && fc.FuelCardID != fuelCard.FuelCardID)) { this.lblMessage.Text = "شماره PAN کارت سوخت خودرو بخش کارت سوخت آژانسی قبلا برای خودرو دیگری ثبت شده"; return; } Ajancy.ZonePlateNumber pln = db.ZonePlateNumbers.FirstOrDefault<Ajancy.ZonePlateNumber>(pl => pl.CityID == Public.ToShort(this.drpCarPlateNumberCity.SelectedValue) && pl.Number == this.txtCarPlateNumber_5.Text.Trim() && pl.ZonePlateNumberID != carPlateNumber.ZonePlateNumberID); if (pln != null) { this.lblMessage.Text = "شماره پلاک وارد شده در بخش کارت سوخت آژانسی متعلق به شخص دیگری میباشد"; return; } Ajancy.AjancyDriver ajancyDriver = driverCertificationCar.AjancyDrivers.Last<Ajancy.AjancyDriver>(jd => jd.LockOutDate == null); int ajancyId = Public.ToInt(this.drpAjancies.SelectedValue); if (ajancyDriver.AjancyID != ajancyId) // Driver has moved to one other ajancy { ajancyDriver.LockOutDate = DateTime.Now; driverCertificationCar.AjancyDrivers.Add(new Ajancy.AjancyDriver { AjancyID = ajancyId, MembershipDate = DateTime.Now }); } //if (Public.ActiveUserRole.RoleID == (short)Public.Role.ProvinceManager) //{ // person.User.CityID = Public.ToShort(this.drpCity.SelectedValue); //} } else // Person is not driver { if (db.Cars.Any<Ajancy.Car>(c => c.VIN == this.txtCarVIN.Text.Trim())) { this.lblMessage.Text = "خودرو بخش کارت سوخت آژانسی با این شماره VIN متعلق به شخص دیگری میباشد"; return; } if (db.FuelCards.Any<Ajancy.FuelCard>(fc => fc.PAN == this.txtFuelCardPAN.Text)) { this.lblMessage.Text = "شماره PAN کارت سوخت خودرو بخش کارت سوخت آژانسی قبلا برای خودرو دیگری ثبت شده"; return; } Ajancy.ZonePlateNumber pln = db.ZonePlateNumbers.FirstOrDefault<Ajancy.ZonePlateNumber>(pl => pl.CityID == Public.ToShort(this.drpCarPlateNumberCity.SelectedValue) && pl.Number == this.txtCarPlateNumber_5.Text.Trim()); if (pln != null) { this.lblMessage.Text = "شماره پلاک وارد شده در بخش کارت سوخت آژانسی متعلق به شخص دیگری میباشد"; return; } person.User.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = (short)Public.Role.TaxiDriver, MembershipDate = DateTime.Now }); driverCertification = new Ajancy.DriverCertification { CertificationType = (byte)Public.AjancyType.TaxiAjancy, SubmitDate = DateTime.Now }; carPlateNumber = new Ajancy.CarPlateNumber { Car = car, ZonePlateNumber = plateNumber, Person = person }; driverCertificationCar = new Ajancy.DriverCertificationCar { CarPlateNumber = carPlateNumber, SubmitDate = DateTime.Now }; driverCertificationCar.AjancyDrivers.Add(new Ajancy.AjancyDriver { AjancyID = Public.ToInt(this.drpAjancies.SelectedValue), DriverCertificationCar = driverCertificationCar, MembershipDate = DateTime.Now }); driverCertification.DriverCertificationCars.Add(driverCertificationCar); person.DriverCertifications.Add(driverCertification); fuelCard.SubmitDate = DateTime.Now; car.FuelCards.Add(fuelCard); } switch ((Public.Role)Public.ActiveUserRole.RoleID) { case Public.Role.ProvinceManager: person.User.CityID = Public.ToShort(this.drpCity.SelectedValue); break; case Public.Role.Admin: person.User.ProvinceID = Public.ToByte(this.drpProvince.SelectedValue); person.User.CityID = Public.ToShort(this.drpCity.SelectedValue); break; } } // --------------- setting values person.FirstName = this.txtFirstName.Text.Trim(); person.LastName = this.txtLastName.Text.Trim(); plateNumber.CityID = Public.ToShort(this.drpCarPlateNumberCity.SelectedValue); plateNumber.Number = this.txtCarPlateNumber_5.Text.Trim(); car.CarTypeID = Public.ToShort(this.drpCarType.SelectedValue); car.FuelType = Public.ToByte(this.drpFuelType.SelectedValue); car.Model = this.txtCarModel.Text; fuelCard.CardType = (byte)Public.FuelCardType.Ajancy; fuelCard.PAN = this.txtFuelCardPAN.Text.Trim(); car.VIN = this.txtCarVIN.Text.Trim().ToUpper(); // Sets the owner of the car Ajancy.Person ownerPer = null; if (!string.IsNullOrEmpty(this.txtOwnerName.Text.Trim()) && !string.IsNullOrEmpty(this.txtOwnerFamily.Text.Trim()) && !string.IsNullOrEmpty(this.txtOwnerNationalCode.Text.Trim()) && !this.txtOwnerNationalCode.Text.Trim().Equals(person.NationalCode)) { ownerPer = db.Persons.FirstOrDefault<Ajancy.Person>(p => p.NationalCode == this.txtOwnerNationalCode.Text.Trim()); if (ownerPer == null) { ownerPer = new Ajancy.Person { NationalCode = this.txtOwnerNationalCode.Text.Trim(), SubmitDate = DateTime.Now }; Ajancy.User ownerUser = new Ajancy.User { UserName = this.txtOwnerNationalCode.Text.Trim() , ProvinceID = person.User.ProvinceID , CityID = person.User.CityID , SubmitDate = DateTime.Now }; ownerUser.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = (short)Public.Role.CarOwner, MembershipDate = DateTime.Now, LockOutDate = DateTime.Now }); ownerPer.User = ownerUser; db.Persons.InsertOnSubmit(ownerPer); } else if (!ownerPer.User.UsersInRoles.Any<Ajancy.UsersInRole>(ur => ur.RoleID == (short)Public.Role.CarOwner)) { ownerPer.User.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = (short)Public.Role.CarOwner, MembershipDate = DateTime.Now, LockOutDate = DateTime.Now }); } ownerPer.FirstName = this.txtOwnerName.Text.Trim(); ownerPer.LastName = this.txtOwnerFamily.Text.Trim(); person.CarPlateNumbers.Remove(carPlateNumber); ownerPer.CarPlateNumbers.Add(carPlateNumber); } else if (carPlateNumber.OwnerPersonID > 0 && carPlateNumber.OwnerPersonID != person.PersonID) // Set the driver as owner again { carPlateNumber.Person = person; } #endregion if (this.ViewState["Mode"] == null) // Add mode { Ajancy.FuelCardSubstitution formerReq = null; if (fuelCard.FuelCardID > 0) { formerReq = db.FuelCardSubstitutions.FirstOrDefault<Ajancy.FuelCardSubstitution>(fcs => (fcs.AjancyTypeFuelCardID == fuelCard.FuelCardID || fcs.PersonalTypeFuelCardID.GetValueOrDefault() == fuelCard.FuelCardID)); } if (formerReq == null) { db.FuelCardSubstitutions.InsertOnSubmit(new Ajancy.FuelCardSubstitution { FuelCard = fuelCard, UserInRoleID = (short)Public.ActiveUserRole.UserRoleID, SubmitDate = DateTime.Now }); } else { this.lblMessage.Text = "برای کارت سوخت مورد نظر قبلا درخواست ابطال یا جایگزین ثبت شده است"; return; } } db.SubmitChanges(); DisposeContext(); Response.Redirect("~/Message.aspx?mode=10"); } }
private void detach_ZonePlateNumbers(ZonePlateNumber entity) { this.SendPropertyChanging(); entity.City = null; }
partial void DeleteZonePlateNumber(ZonePlateNumber instance);
partial void UpdateZonePlateNumber(ZonePlateNumber instance);
partial void InsertZonePlateNumber(ZonePlateNumber instance);
protected void btnSave_Click(object sender, EventArgs e) { if (Page.IsValid) { DataLoadOptions dlo = new DataLoadOptions(); dlo.LoadWith<Ajancy.Person>(p => p.User); dlo.LoadWith<Ajancy.User>(u => u.UsersInRoles); dlo.LoadWith<Ajancy.Person>(p => p.DriverCertifications); dlo.LoadWith<Ajancy.DriverCertification>(dc => dc.DriverCertificationCars); dlo.LoadWith<Ajancy.DriverCertificationCar>(dcc => dcc.CarPlateNumber); dlo.LoadWith<Ajancy.CarPlateNumber>(cpn => cpn.Car); dlo.LoadWith<Ajancy.Car>(c => c.FuelCards); db.LoadOptions = dlo; #region AjancyType Ajancy.Person person = new Ajancy.Person(); Ajancy.DriverCertification driverCertification = new Ajancy.DriverCertification(); Ajancy.DriverCertificationCar driverCertificationCar = new Ajancy.DriverCertificationCar(); Ajancy.CarPlateNumber carPlateNumber = new Ajancy.CarPlateNumber(); Ajancy.ZonePlateNumber plateNumber = new Ajancy.ZonePlateNumber(); Ajancy.Car car = new Ajancy.Car(); Ajancy.FuelCard fuelCard = new Ajancy.FuelCard(); Ajancy.Car car_db = null; if (this.ViewState["PersonID"] == null) // New Person { car_db = db.Cars.FirstOrDefault<Ajancy.Car>(c => c.EngineNo == this.txtCarEngineNo.Text.Trim() && c.ChassisNo == this.txtCarChassisNo.Text.Trim()); if (car_db != null) { foreach (var cpn in car_db.CarPlateNumbers) { foreach (var dcc in cpn.DriverCertificationCars) { if (dcc.LockOutDate == null) { this.lblMessage.Text = "خودرو مورد نظر با این شماره موتور و شماره شاسی متعلق به شخص دیگری میباشد"; return; } } } car = car_db; } else { db.Cars.InsertOnSubmit(car); } if (db.Cars.Any<Ajancy.Car>(c => c.VIN == this.txtCarVIN.Text.Trim())) { this.lblMessage.Text = "خودرو مورد نظر با این شماره VIN متعلق به شخص دیگری میباشد"; return; } if (db.FuelCards.Any<Ajancy.FuelCard>(fc => fc.PAN == this.txtFuelCardPAN.Text)) { this.lblMessage.Text = "شماره PAN کارت سوخت خودرو قبلا برای خودرو دیگری ثبت شده"; return; } Ajancy.ZonePlateNumber pln = db.ZonePlateNumbers.FirstOrDefault<Ajancy.ZonePlateNumber>( pl => pl.CityID == Public.ToShort(this.drpCarPlateNumberCity.SelectedValue) && pl.Number == this.txtCarPlateNumber_5.Text.Trim()); if (pln != null) { foreach (Ajancy.CarPlateNumber cpn in pln.CarPlateNumbers) { foreach (var dcc in cpn.DriverCertificationCars) { if (dcc.LockOutDate == null) { this.lblMessage.Text = "شماره پلاک وارد شده متعلق به شخص دیگری میباشد"; return; } } } } Ajancy.User user = new Ajancy.User(); user.UserName = this.txtNationalCode.Text.Trim(); user.ProvinceID = Public.ActiveUserRole.User.ProvinceID; user.CityID = Public.ActiveUserRole.User.CityID; user.SubmitDate = DateTime.Now; user.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = (short)Public.Role.TaxiDriver, MembershipDate = DateTime.Now }); person.User = user; person.NationalCode = this.txtNationalCode.Text.Trim(); person.SubmitDate = DateTime.Now; driverCertification.CertificationType = (byte)Public.AjancyType.TaxiAjancy; driverCertification.SubmitDate = DateTime.Now; carPlateNumber = new Ajancy.CarPlateNumber { Car = car, ZonePlateNumber = plateNumber, Person = person }; driverCertificationCar = new Ajancy.DriverCertificationCar { CarPlateNumber = carPlateNumber, SubmitDate = DateTime.Now }; driverCertification.DriverCertificationCars.Add(driverCertificationCar); person.DriverCertifications.Add(driverCertification); fuelCard.SubmitDate = DateTime.Now; car.FuelCards.Add(fuelCard); db.CarPlateNumbers.InsertOnSubmit(carPlateNumber); db.AjancyDrivers.InsertOnSubmit(new Ajancy.AjancyDriver { AjancyID = Public.ToInt(this.drpAjancies.SelectedValue), DriverCertificationCar = driverCertificationCar, MembershipDate = DateTime.Now }); db.Persons.InsertOnSubmit(person); } else // Person Exists { person = db.Persons.FirstOrDefault<Ajancy.Person>(p => p.PersonID == Public.ToInt(this.ViewState["PersonID"])); if (person.User.UsersInRoles.Any<Ajancy.UsersInRole>(ur => ur.RoleID == (short)Public.Role.TaxiDriver)) // Person is a driver { driverCertification = person.DriverCertifications.First<Ajancy.DriverCertification>( dc => dc.CertificationType == (byte)Public.AjancyType.TaxiAjancy); driverCertificationCar = driverCertification.DriverCertificationCars.Last<Ajancy.DriverCertificationCar>(); carPlateNumber = driverCertificationCar.CarPlateNumber; plateNumber = carPlateNumber.ZonePlateNumber; car = carPlateNumber.Car; fuelCard = car.FuelCards.Last<Ajancy.FuelCard>(); //fuelCard = car.FuelCards.First<Ajancy.FuelCard>(fc => fc.DiscardDate == null); car_db = db.Cars.FirstOrDefault<Ajancy.Car>(c => c.EngineNo == this.txtCarEngineNo.Text.Trim() && c.ChassisNo == this.txtCarChassisNo.Text.Trim() && c.CarID != carPlateNumber.Car.CarID); if (car_db != null) { foreach (var cpn in car_db.CarPlateNumbers) { foreach (var dcc in cpn.DriverCertificationCars) { if (dcc.LockOutDate == null) { this.lblMessage.Text = "خودرو مورد نظر با این شماره موتور و شماره شاسی متعلق به شخص دیگری میباشد"; return; } } } carPlateNumber.Car = car_db; } if (db.Cars.Any<Ajancy.Car>(c => c.VIN == this.txtCarVIN.Text.Trim() && c.CarID != car.CarID)) { this.lblMessage.Text = "خودرو مورد نظر با این شماره VIN متعلق به شخص دیگری میباشد"; return; } if ( db.FuelCards.Any<Ajancy.FuelCard>( fc => fc.PAN == this.txtFuelCardPAN.Text && fc.FuelCardID != fuelCard.FuelCardID)) { this.lblMessage.Text = "شماره PAN کارت سوخت خودرو قبلا برای خودرو دیگری ثبت شده"; return; } Ajancy.ZonePlateNumber pln = db.ZonePlateNumbers.FirstOrDefault<Ajancy.ZonePlateNumber>( pl => pl.CityID == Public.ToShort(this.drpCarPlateNumberCity.SelectedValue) && pl.Number == this.txtCarPlateNumber_5.Text.Trim() && pl.ZonePlateNumberID != carPlateNumber.ZonePlateNumberID); if (pln != null) { foreach (Ajancy.CarPlateNumber cpn in pln.CarPlateNumbers) { foreach (var dcc in cpn.DriverCertificationCars) { if (dcc.LockOutDate == null) { this.lblMessage.Text = "شماره پلاک وارد شده متعلق به شخص دیگری میباشد"; return; } } } } //Ajancy.AjancyDriver ajancyDriver = driverCertificationCar.AjancyDrivers.Last<Ajancy.AjancyDriver>(jd => jd.LockOutDate == null); Ajancy.AjancyDriver ajancyDriver = driverCertificationCar.AjancyDrivers.Last<Ajancy.AjancyDriver>(); int ajancyId = Public.ToInt(this.drpAjancies.SelectedValue); if (ajancyDriver.AjancyID != ajancyId) // Driver has moved to one other ajancy { ajancyDriver.LockOutDate = DateTime.Now; driverCertificationCar.AjancyDrivers.Add(new Ajancy.AjancyDriver { AjancyID = ajancyId, MembershipDate = DateTime.Now }); } if (Public.ActiveUserRole.RoleID == (short)Public.Role.ProvinceManager) { person.User.CityID = Public.ToShort(this.drpCity.SelectedValue); } } else // Person is not driver { car_db = db.Cars.FirstOrDefault<Ajancy.Car>(c => c.EngineNo == this.txtCarEngineNo.Text.Trim() && c.ChassisNo == this.txtCarChassisNo.Text.Trim()); if (car_db != null) { foreach (var cpn in car_db.CarPlateNumbers) { foreach (var dcc in cpn.DriverCertificationCars) { if (dcc.LockOutDate == null) { this.lblMessage.Text = "خودرو مورد نظر با این شماره موتور و شماره شاسی متعلق به شخص دیگری میباشد"; return; } } } car = car_db; } else { db.Cars.InsertOnSubmit(car); } if (db.Cars.Any<Ajancy.Car>(c => c.VIN == this.txtCarVIN.Text.Trim())) { this.lblMessage.Text = "خودرو مورد نظر با این شماره VIN متعلق به شخص دیگری میباشد"; return; } if (db.FuelCards.Any<Ajancy.FuelCard>(fc => fc.PAN == this.txtFuelCardPAN.Text)) { this.lblMessage.Text = "شماره PAN کارت سوخت خودرو قبلا برای خودرو دیگری ثبت شده"; return; } Ajancy.ZonePlateNumber pln = db.ZonePlateNumbers.FirstOrDefault<Ajancy.ZonePlateNumber>( pl => pl.CityID == Public.ToShort(this.drpCarPlateNumberCity.SelectedValue) && pl.Number == this.txtCarPlateNumber_5.Text.Trim()); if (pln != null) { foreach (Ajancy.CarPlateNumber cpn in pln.CarPlateNumbers) { foreach (var dcc in cpn.DriverCertificationCars) { if (dcc.LockOutDate == null) { this.lblMessage.Text = "شماره پلاک وارد شده متعلق به شخص دیگری میباشد"; return; } } } } person.User.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = (short)Public.Role.TaxiDriver, MembershipDate = DateTime.Now }); driverCertification.CertificationType = (byte)Public.AjancyType.TaxiAjancy; driverCertification.SubmitDate = DateTime.Now; carPlateNumber = new Ajancy.CarPlateNumber { Car = car, ZonePlateNumber = plateNumber, Person = person }; driverCertificationCar = new Ajancy.DriverCertificationCar { CarPlateNumber = carPlateNumber, SubmitDate = DateTime.Now }; driverCertification.DriverCertificationCars.Add(driverCertificationCar); person.DriverCertifications.Add(driverCertification); fuelCard.SubmitDate = DateTime.Now; car.FuelCards.Add(fuelCard); db.CarPlateNumbers.InsertOnSubmit(carPlateNumber); db.AjancyDrivers.InsertOnSubmit(new Ajancy.AjancyDriver { AjancyID = Public.ToInt(this.drpAjancies.SelectedValue), DriverCertificationCar = driverCertificationCar, MembershipDate = DateTime.Now }); } } // --------- setting other values person.FirstName = this.txtFirstName.Text.Trim(); person.LastName = this.txtLastName.Text.Trim(); person.Father = this.txtFather.Text.Trim(); person.BirthCertificateNo = this.txtBirthCertificateNo.Text.Trim(); person.Gender = Public.ToByte(this.drpGender.SelectedValue); person.Marriage = Public.ToByte(this.drpMarriage.SelectedValue); person.BirthPlace = this.txtBirthPlace.Text.Trim(); person.Phone = this.txtPhone.Text.Trim(); person.Mobile = this.txtMobile.Text.Trim(); person.PostalCode = this.txtPostalCode.Text.Trim(); person.Address = this.txtAddress.Text.Trim(); plateNumber.CityID = Public.ToShort(this.drpCarPlateNumberCity.SelectedValue); plateNumber.Number = this.txtCarPlateNumber_5.Text.Trim(); if (!string.IsNullOrEmpty(this.txtDriverCertificationNo.Text)) { if (person.PersonID == 0) { if ( db.DriverCertifications.Any<Ajancy.DriverCertification>( dc => dc.DriverCertificationNo == this.txtDriverCertificationNo.Text)) { this.lblMessage.Text = "شماره دفترچه صلاحیت تکراری میباشد"; return; } } else { if ( db.DriverCertifications.Any<Ajancy.DriverCertification>( dc => dc.PersonID != person.PersonID && dc.DriverCertificationNo == this.txtDriverCertificationNo.Text)) { this.lblMessage.Text = "شماره دفترچه صلاحیت تکراری میباشد"; return; } } } driverCertification.DriverCertificationNo = string.IsNullOrEmpty(this.txtDriverCertificationNo.Text) ? null : this.txtDriverCertificationNo.Text.Trim(); car.CarTypeID = Public.ToByte(this.drpCarType.SelectedValue); car.Model = this.txtCarModel.Text.Trim(); car.EngineNo = this.txtCarEngineNo.Text.Trim(); car.ChassisNo = this.txtCarChassisNo.Text.Trim(); car.FuelType = Public.ToByte(this.drpFuelType.SelectedValue); fuelCard.CardType = Public.ToByte(this.drpFuelCardType.SelectedValue); fuelCard.PAN = this.txtFuelCardPAN.Text.Trim(); car.VIN = this.txtCarVIN.Text.Trim().ToUpper(); // Sets the owner of the car Ajancy.Person ownerPer = null; if (!string.IsNullOrEmpty(this.txtOwnerName.Text.Trim()) && !string.IsNullOrEmpty(this.txtOwnerFamily.Text.Trim()) && !string.IsNullOrEmpty(this.txtOwnerNationalCode.Text.Trim()) && !this.txtOwnerNationalCode.Text.Trim().Equals(person.NationalCode)) { ownerPer = db.Persons.FirstOrDefault<Ajancy.Person>( p => p.NationalCode == this.txtOwnerNationalCode.Text.Trim()); if (ownerPer == null) { ownerPer = new Ajancy.Person { NationalCode = this.txtOwnerNationalCode.Text.Trim(), SubmitDate = DateTime.Now }; Ajancy.User ownerUser = new Ajancy.User { UserName = this.txtOwnerNationalCode.Text.Trim() , ProvinceID = person.User.ProvinceID , CityID = person.User.CityID , SubmitDate = DateTime.Now }; ownerUser.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = (short)Public.Role.CarOwner, MembershipDate = DateTime.Now, LockOutDate = DateTime.Now }); ownerPer.User = ownerUser; db.Persons.InsertOnSubmit(ownerPer); } else if ( !ownerPer.User.UsersInRoles.Any<Ajancy.UsersInRole>( ur => ur.RoleID == (short)Public.Role.CarOwner)) { ownerPer.User.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = (short)Public.Role.CarOwner, MembershipDate = DateTime.Now, LockOutDate = DateTime.Now }); } ownerPer.FirstName = this.txtOwnerName.Text.Trim(); ownerPer.LastName = this.txtOwnerFamily.Text.Trim(); person.CarPlateNumbers.Remove(carPlateNumber); ownerPer.CarPlateNumbers.Add(carPlateNumber); } else if (carPlateNumber.OwnerPersonID > 0 && carPlateNumber.OwnerPersonID != person.PersonID) // Set the driver as owner again { carPlateNumber.Person = person; } #endregion db.SubmitChanges(); DisposeContext(); Response.Redirect("~/Message.aspx?mode=12"); } }