Exemplo n.º 1
0
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (this.Page.IsValid && this.ViewState["DCID"] != null)
        {
            DataLoadOptions dlo = new DataLoadOptions();
            dlo.LoadWith<Ajancy.PlateNumber>(pl => pl.CarPlateNumbers);
            dlo.LoadWith<Ajancy.CarPlateNumber>(cpn => cpn.DriverCertificationCars);
            db = new Ajancy.Kimia_Ajancy(Public.ConnectionString);
            db.LoadOptions = dlo;

            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;
            }

            int driverCertificationId = Public.ToInt(this.ViewState["DCID"]);
            Ajancy.DriverCertification driverCertification = db.DriverCertifications.First<Ajancy.DriverCertification>(dc => dc.DriverCertificationID == driverCertificationId);
            Ajancy.DriverCertificationCar lastDCC = driverCertification.DriverCertificationCars.Last<Ajancy.DriverCertificationCar>();
            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 && pln.ZonePlateNumberID != lastDCC.CarPlateNumber.ZonePlateNumberID)
            {
                foreach (Ajancy.CarPlateNumber cpn in pln.CarPlateNumbers)
                {
                    foreach (Ajancy.DriverCertificationCar dcc in cpn.DriverCertificationCars)
                    {
                        if (dcc.LockOutDate == null)
                        {
                            this.lblMessage.Text = "شماره پلاک وارد شده متعلق به شخص دیگری میباشد";
                            return;
                        }
                    }
                }
            }

            Ajancy.Car car = new Ajancy.Car
            {
                CarTypeID = Public.ToShort(this.drpCarType.SelectedValue),
                FuelType = Public.ToByte(this.drpFuelType.SelectedValue),
                Model = this.txtCarModel.Text,
                VIN = this.txtCarVIN.Text.Trim().ToUpper()
            };
            car.FuelCards.Add(new Ajancy.FuelCard
            {
                CardType = (byte)Public.FuelCardType.Ajancy,
                PAN = this.txtFuelCardPAN.Text.Trim(),
                SubmitDate = DateTime.Now
            });

            Ajancy.CarPlateNumber carPlateNumber = new Ajancy.CarPlateNumber
            {
                OwnerPersonID = driverCertification.PersonID,
                Car = car,
                ZonePlateNumber = pln != null ? pln : new Ajancy.ZonePlateNumber
                {
                    CityID = Public.ToShort(this.drpCarPlateNumberCity.SelectedValue),
                    Number = this.txtCarPlateNumber_5.Text.Trim()
                }
            };

            if (lastDCC.LockOutDate == null)
            {
                lastDCC.LockOutDate = DateTime.Now;
                lastDCC.AjancyDrivers.Last<Ajancy.AjancyDriver>().LockOutDate = DateTime.Now;
                lastDCC.CarPlateNumber.Car.FuelCards.Last<Ajancy.FuelCard>().DiscardDate = DateTime.Now;
            }

            Ajancy.DriverCertificationCar 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);
            db.SubmitChanges();
            this.lblMessage.Text = Public.SAVEMESSAGE;
            this.ClearControls(false);
            this.LoadDriverCars(driverCertificationId, db);
        }
    }
Exemplo n.º 2
0
    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.DrivingLicense drivingLicense = new Ajancy.DrivingLicense();
            Ajancy.DriverCertification driverCertification = new Ajancy.DriverCertification();
            Ajancy.DriverCertificationCar driverCertificationCar = new Ajancy.DriverCertificationCar();
            Ajancy.CarPlateNumber carPlateNumber = new Ajancy.CarPlateNumber();
            Ajancy.PlateNumber plateNumber = new Ajancy.PlateNumber();
            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.PlateNumber pln = db.PlateNumbers.FirstOrDefault<Ajancy.PlateNumber>(pl => pl.TwoDigits == this.txtCarPlateNumber_1.Text.Trim() &&
                                                                                                                                                          pl.Alphabet == this.drpCarPlateNumber.SelectedValue &&
                                                                                                                                                          pl.ThreeDigits == this.txtCarPlateNumber_2.Text.Trim() &&
                                                                                                                                                          pl.RegionIdentifier == this.txtCarPlateNumber_3.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, PlateNumber = plateNumber, Person = person };
                driverCertificationCar = new Ajancy.DriverCertificationCar { CarPlateNumber = carPlateNumber, SubmitDate = DateTime.Now };
                driverCertification.DriverCertificationCars.Add(driverCertificationCar);
                person.DrivingLicenses.Add(drivingLicense);
                person.DriverCertifications.Add(driverCertification);
                fuelCard.SubmitDate = DateTime.Now;
                car.FuelCards.Add(fuelCard);
                db.CarPlateNumbers.InsertOnSubmit(carPlateNumber);
                db.AjancyDrivers.InsertOnSubmit(new Ajancy.AjancyDriver { AjancyID = Public.ActiveAjancy.AjancyID, 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.PlateNumber;
                    car = carPlateNumber.Car;
                    //fuelCard = car.FuelCards.First<Ajancy.FuelCard>(fc => fc.DiscardDate == null);
                    fuelCard = car.FuelCards.Last<Ajancy.FuelCard>();
                    drivingLicense = person.DrivingLicenses.LastOrDefault<Ajancy.DrivingLicense>();
                    if (drivingLicense == null)
                    {
                        drivingLicense = new Ajancy.DrivingLicense();
                        person.DrivingLicenses.Add(drivingLicense);
                    }

                    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.PlateNumber pln = db.PlateNumbers.FirstOrDefault<Ajancy.PlateNumber>(pl => pl.TwoDigits == this.txtCarPlateNumber_1.Text.Trim() &&
                                                                                                                                                              pl.Alphabet == this.drpCarPlateNumber.SelectedValue &&
                                                                                                                                                              pl.ThreeDigits == this.txtCarPlateNumber_2.Text.Trim() &&
                                                                                                                                                              pl.RegionIdentifier == this.txtCarPlateNumber_3.Text.Trim() &&
                                                                                                                                                              pl.PlateNumberID != carPlateNumber.PlateNumber.PlateNumberID);
                    if (pln != null)
                    {
                        foreach (Ajancy.CarPlateNumber cpn in pln.CarPlateNumbers)
                        {
                            foreach (var dcc in cpn.DriverCertificationCars)
                            {
                                if (dcc.LockOutDate == null)
                                {
                                    this.lblMessage.Text = "شماره پلاک وارد شده متعلق به شخص دیگری میباشد";
                                    return;
                                }
                            }
                        }
                    }
                }
                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.PlateNumber pln = db.PlateNumbers.FirstOrDefault<Ajancy.PlateNumber>(pl => pl.TwoDigits == this.txtCarPlateNumber_1.Text.Trim() &&
                                                                                                                                                              pl.Alphabet == this.drpCarPlateNumber.SelectedValue &&
                                                                                                                                                              pl.ThreeDigits == this.txtCarPlateNumber_2.Text.Trim() &&
                                                                                                                                                              pl.RegionIdentifier == this.txtCarPlateNumber_3.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, PlateNumber = plateNumber, Person = person };
                    driverCertificationCar = new Ajancy.DriverCertificationCar { CarPlateNumber = carPlateNumber, SubmitDate = DateTime.Now };
                    driverCertification.DriverCertificationCars.Add(driverCertificationCar);
                    person.DriverCertifications.Add(driverCertification);
                    person.DrivingLicenses.Add(drivingLicense);
                    fuelCard.SubmitDate = DateTime.Now;
                    car.FuelCards.Add(fuelCard);
                    db.CarPlateNumbers.InsertOnSubmit(carPlateNumber);
                    db.AjancyDrivers.InsertOnSubmit(new Ajancy.AjancyDriver { AjancyID = Public.ActiveAjancy.AjancyID, DriverCertificationCar = driverCertificationCar, MembershipDate = DateTime.Now });
                }
            }

            // --------- setting other values

            person.SubmitDate = DateTime.Now;
            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.BirthCertificateSerial = this.txtBirthCertificateSerial.Text.Trim();
            person.BirthCertificateSerie = this.txtBirthCertificateSerie.Text.Trim();
            person.BirthCertificateAlfa = this.drpBirthCertificateAlfa.SelectedValue;
            person.Gender = Public.ToByte(this.drpGender.SelectedValue);
            person.Marriage = Public.ToByte(this.drpMarriage.SelectedValue);
            person.BirthDate = this.txtBirthDate.GeorgianDate.Value;
            person.BirthPlace = this.txtBirthPlace.Text.Trim();
            person.BirthCertificatePlace = this.txtBirthCertificatePlace.Text.Trim();
            person.FamilyMembersCount = this.txtFamilyMembersCount.Text.Trim();
            person.Education = Public.ToByte(this.drpEducation.SelectedValue);
            person.MilitaryService = Public.ToByte(this.drpMilitaryService.SelectedValue);
            person.Religion = Public.ToByte(this.drpReligion.SelectedValue);
            person.Subreligion = this.txtSubreligion.Text.Trim();
            person.JobStatus = Public.ToByte(this.drpJobStatus.SelectedValue);
            person.Phone = this.txtPhone.Text.Trim();
            person.Mobile = this.txtMobile.Text.Trim();
            person.PostalCode = this.txtPostalCode.Text.Trim();
            person.Address = this.txtAddress.Text.Trim();
            //person.FormerJob = this.txtFormerJob.Text.Trim();
            //person.FormerJobType = Public.ToByte(this.drpFormerJobType.SelectedValue);
            plateNumber.TwoDigits = this.txtCarPlateNumber_1.Text.Trim();
            plateNumber.Alphabet = this.drpCarPlateNumber.SelectedValue;
            plateNumber.ThreeDigits = this.txtCarPlateNumber_2.Text.Trim();
            plateNumber.RegionIdentifier = this.txtCarPlateNumber_3.Text.Trim();

            drivingLicense.DrivingLicenseNo = this.txtDrivingLicenseNo.Text.Trim();
            drivingLicense.ExportDate = this.txtDrivingLicenseDate.GeorgianDate.Value;
            drivingLicense.ExportPlace = this.txtDrivingLicensePlace.Text.Trim();
            drivingLicense.Type = Public.ToByte(this.drpDrivingLicenseType.SelectedValue);

            //if (!string.IsNullOrEmpty(this.txtDriverCertificationNo.Text))
            //{
            //    if (db.DriverCertifications.Any<Ajancy.DriverCertification>(dc => 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.Color = this.txtCarColor.Text.Trim();
            car.EngineNo = this.txtCarEngineNo.Text.Trim();
            car.ChassisNo = this.txtCarChassisNo.Text.Trim();
            car.GASProvider = Public.ToByte(this.drpGASProvider.SelectedValue);
            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

            try
            {
                db.SubmitChanges();

                if (Request.QueryString["id"] == null)
                {
                    DisposeContext();
                    Response.Redirect("~/Message.aspx?mode=12");
                }
                else // edit mode
                {
                    DisposeContext();
                    Response.Redirect("~/Message.aspx?mode=7");
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("DrivingLicenseNo"))
                {
                    this.lblMessage.Text = "شماره گواهینامه رانندگی تکراری میباشد";
                }
                else
                {
                    throw ex;
                }
            }
        }
    }
    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);
            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.PlateNumber plateNumber = new Ajancy.PlateNumber();

            plateNumber = db.PlateNumbers.FirstOrDefault<Ajancy.PlateNumber>(pl => pl.TwoDigits == this.txtCarPlateNumber_1.Text.Trim() &&
                                                                                                                pl.Alphabet == this.drpCarPlateNumber.SelectedValue &&
                                                                                                                pl.ThreeDigits == this.txtCarPlateNumber_2.Text.Trim() &&
                                                                                                                pl.RegionIdentifier == this.txtCarPlateNumber_3.Text.Trim());
            if (plateNumber != null)
            {
                foreach (Ajancy.CarPlateNumber cpn in plateNumber.CarPlateNumbers)
                {
                    foreach (var dcc in cpn.DriverCertificationCars)
                    {
                        if (dcc.LockOutDate == null)
                        {
                            this.lblMessage.Text = "شماره پلاک وارد شده متعلق به شخص دیگری میباشد";
                            return;
                        }
                    }
                }
            }
            else
            {
                plateNumber = new Ajancy.PlateNumber();
                plateNumber.TwoDigits = this.txtCarPlateNumber_1.Text.Trim();
                plateNumber.Alphabet = this.drpCarPlateNumber.SelectedValue;
                plateNumber.ThreeDigits = this.txtCarPlateNumber_2.Text.Trim();
                plateNumber.RegionIdentifier = this.txtCarPlateNumber_3.Text.Trim();
            }

            if (this.ViewState["PersonID"] == null) // New Person
            {
                Ajancy.User user = new Ajancy.User();
                user.UserName = this.txtNationalCode.Text.Trim();
                user.ProvinceID = Public.ActiveUserRole.User.ProvinceID;
                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 };
                person.DriverCertifications.Add(driverCertification);
                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);
                    Ajancy.DriverCertificationCar current_dcc = driverCertification.DriverCertificationCars.Last<Ajancy.DriverCertificationCar>();
                    current_dcc.LockOutDate = DateTime.Now;
                    current_dcc.AjancyDrivers.Last<Ajancy.AjancyDriver>().LockOutDate = DateTime.Now;
                }
                else // Person is not driver
                {
                    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 };
                    person.DriverCertifications.Add(driverCertification);
                }
            }

            // --------------- setting values

            string[] values = this.hdnValues.Value.Split('|');
            db.DriverCertificationCars.First<Ajancy.DriverCertificationCar>(dcc => dcc.DriverCertificationCarID == Public.ToInt(values[1])).LockOutDate = DateTime.Now;
            db.AjancyDrivers.First<Ajancy.AjancyDriver>(jd => jd.AjancyDriverID == Public.ToInt(values[2])).LockOutDate = DateTime.Now;

            carPlateNumber = new Ajancy.CarPlateNumber { CarID = Public.ToInt(values[0]), PlateNumber = 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.FirstName = this.txtFirstName.Text.Trim();
            person.LastName = this.txtLastName.Text.Trim();

            // 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=25");
        }
    }
Exemplo n.º 4
0
    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");
        }
    }
Exemplo n.º 5
0
 partial void DeleteDriverCertificationCar(DriverCertificationCar instance);
Exemplo n.º 6
0
    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");
    }
Exemplo n.º 7
0
 partial void InsertDriverCertificationCar(DriverCertificationCar instance);
Exemplo n.º 8
0
 partial void UpdateDriverCertificationCar(DriverCertificationCar instance);
Exemplo n.º 9
0
		private void detach_DriverCertificationCars(DriverCertificationCar entity)
		{
			this.SendPropertyChanging();
			entity.DriverCertification = null;
		}
Exemplo n.º 10
0
		private void attach_DriverCertificationCars(DriverCertificationCar entity)
		{
			this.SendPropertyChanging();
			entity.CarPlateNumber = this;
		}
Exemplo n.º 11
0
    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.PlateNumber plateNumber = new Ajancy.PlateNumber();
            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.PlateNumber pln = db.PlateNumbers.FirstOrDefault<Ajancy.PlateNumber>(pl => pl.TwoDigits == this.txtCarPlateNumber_1.Text.Trim() &&
                                                                                                                                                          pl.Alphabet == this.drpCarPlateNumber.SelectedValue &&
                                                                                                                                                          pl.ThreeDigits == this.txtCarPlateNumber_2.Text.Trim() &&
                                                                                                                                                          pl.RegionIdentifier == this.txtCarPlateNumber_3.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();

                driverCertification.CertificationType = (byte)Public.AjancyType.TaxiAjancy;
                driverCertification.SubmitDate = DateTime.Now;

                carPlateNumber = new Ajancy.CarPlateNumber { Car = car, PlateNumber = 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.PlateNumber;
                    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.PlateNumber pln = db.PlateNumbers.FirstOrDefault<Ajancy.PlateNumber>(pl => pl.TwoDigits == this.txtCarPlateNumber_1.Text.Trim() &&
                                                                                                                                                              pl.Alphabet == this.drpCarPlateNumber.SelectedValue &&
                                                                                                                                                              pl.ThreeDigits == this.txtCarPlateNumber_2.Text.Trim() &&
                                                                                                                                                              pl.RegionIdentifier == this.txtCarPlateNumber_3.Text.Trim() &&
                                                                                                                                                              pl.PlateNumberID != carPlateNumber.PlateNumber.PlateNumberID);
                    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.PlateNumber pln = db.PlateNumbers.FirstOrDefault<Ajancy.PlateNumber>(pl => pl.TwoDigits == this.txtCarPlateNumber_1.Text.Trim() &&
                                                                                                                                                              pl.Alphabet == this.drpCarPlateNumber.SelectedValue &&
                                                                                                                                                              pl.ThreeDigits == this.txtCarPlateNumber_2.Text.Trim() &&
                                                                                                                                                              pl.RegionIdentifier == this.txtCarPlateNumber_3.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, PlateNumber = 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.SubmitDate = DateTime.Now;
            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.TwoDigits = this.txtCarPlateNumber_1.Text.Trim();
            plateNumber.Alphabet = this.drpCarPlateNumber.SelectedValue;
            plateNumber.ThreeDigits = this.txtCarPlateNumber_2.Text.Trim();
            plateNumber.RegionIdentifier = this.txtCarPlateNumber_3.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");
        }
    }
Exemplo n.º 12
0
    private void SelfReplacement_Save()
    {
        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.PlateNumber plateNumber = new Ajancy.PlateNumber();
        Ajancy.Car car = new Ajancy.Car();
        Ajancy.FuelCard fuelCard = new Ajancy.FuelCard();

        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.PlateNumber pln = db.PlateNumbers.FirstOrDefault<Ajancy.PlateNumber>(pl => pl.TwoDigits == this.txtCarPlateNumber_1.Text.Trim() &&
                                                                                                                            pl.Alphabet == this.drpCarPlateNumber.SelectedValue &&
                                                                                                                            pl.ThreeDigits == this.txtCarPlateNumber_2.Text.Trim() &&
                                                                                                                            pl.RegionIdentifier == this.txtCarPlateNumber_3.Text.Trim());
        if (pln != null)
        {
            this.lblMessage.Text = "شماره پلاک وارد شده در  بخش کارت سوخت آژانسی متعلق به شخص دیگری میباشد";
            return;
        }

        if (this.ViewState["PersonID"] == null && this.ViewState["PersonID_2"] == null) // New Person
        {
            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.AcademyTeacher, MembershipDate = DateTime.Now });
            person.User = user;
            person.NationalCode = this.txtNationalCode.Text.Trim();
            person.SubmitDate = DateTime.Now;

            driverCertification = new Ajancy.DriverCertification
            {
                CertificationType = (byte)Public.AjancyType.Academy,
                SubmitDate = DateTime.Now
            };
            carPlateNumber = new Ajancy.CarPlateNumber { Car = car, PlateNumber = plateNumber };
            driverCertificationCar = new Ajancy.DriverCertificationCar { CarPlateNumber = carPlateNumber, SubmitDate = DateTime.Now, LockOutDate = DateTime.Now };
            driverCertificationCar.AjancyDrivers.Add(new Ajancy.AjancyDriver { AjancyID = Public.ToInt(this.drpAjancies.SelectedValue), MembershipDate = DateTime.Now, LockOutDate = DateTime.Now });
            driverCertification.DriverCertificationCars.Add(driverCertificationCar);
            person.DriverCertifications.Add(driverCertification);
            fuelCard.SubmitDate = DateTime.Now;
            fuelCard.DiscardDate = DateTime.Now;
            car.FuelCards.Add(fuelCard);
            db.Persons.InsertOnSubmit(person);
        }
        else // Person Exists
        {
            if (Public.ActiveUserRole.RoleID == (short)Public.Role.AcademyProvince)
            {
                person.User.CityID = Public.ToShort(this.drpCity_2.SelectedValue);
            }

            person = db.Persons.FirstOrDefault<Ajancy.Person>(p => p.PersonID == Public.ToInt(this.ViewState["PersonID"]));
            person.User.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = (short)Public.Role.AcademyTeacher, MembershipDate = DateTime.Now });
            driverCertification = new Ajancy.DriverCertification
            {
                CertificationType = (byte)Public.AjancyType.Academy,
                SubmitDate = DateTime.Now
            };
            carPlateNumber = new Ajancy.CarPlateNumber { Car = car, PlateNumber = plateNumber };
            driverCertificationCar = new Ajancy.DriverCertificationCar { CarPlateNumber = carPlateNumber, SubmitDate = DateTime.Now, LockOutDate = DateTime.Now };
            driverCertificationCar.AjancyDrivers.Add(new Ajancy.AjancyDriver { AjancyID = Public.ToInt(this.drpAjancies.SelectedValue), DriverCertificationCar = driverCertificationCar, MembershipDate = DateTime.Now, LockOutDate = DateTime.Now });
            driverCertification.DriverCertificationCars.Add(driverCertificationCar);
            person.DriverCertifications.Add(driverCertification);
            fuelCard.SubmitDate = DateTime.Now;
            fuelCard.DiscardDate = DateTime.Now;
            car.FuelCards.Add(fuelCard);

            switch ((Public.Role)Public.ActiveUserRole.RoleID)
            {
                case Public.Role.AcademyProvince:
                    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.TwoDigits = this.txtCarPlateNumber_1.Text.Trim();
        plateNumber.Alphabet = this.drpCarPlateNumber.SelectedValue;
        plateNumber.ThreeDigits = this.txtCarPlateNumber_2.Text.Trim();
        plateNumber.RegionIdentifier = this.txtCarPlateNumber_3.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();

        #endregion

        #region PersonalType

        Ajancy.DriverCertificationCar driverCertificationCar_2 = new Ajancy.DriverCertificationCar { SubmitDate = DateTime.Now };
        Ajancy.CarPlateNumber carPlateNumber_2 = new Ajancy.CarPlateNumber();
        Ajancy.PlateNumber plateNumber_2 = new Ajancy.PlateNumber();
        Ajancy.Car car_2 = new Ajancy.Car();
        Ajancy.FuelCard fuelCard_2 = new Ajancy.FuelCard();

        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.PlateNumber pnl = db.PlateNumbers.FirstOrDefault<Ajancy.PlateNumber>(pl => pl.TwoDigits == this.txtCarPlateNumber_1_2.Text.Trim() &&
                                                                                                                           pl.Alphabet == this.drpCarPlateNumber_2.SelectedValue &&
                                                                                                                           pl.ThreeDigits == this.txtCarPlateNumber_2_2.Text.Trim() &&
                                                                                                                           pl.RegionIdentifier == this.txtCarPlateNumber_3_2.Text.Trim());
        if (pnl != null)
        {
            this.lblMessage.Text = "شماره پلاک وارد شده در  بخش کارت سوخت شخصی متعلق به شخص دیگری میباشد";
            return;
        }

        if (this.ViewState["PersonID"] == null && this.ViewState["PersonID_2"] == null) // New Person
        {
            carPlateNumber_2 = new Ajancy.CarPlateNumber { Car = car_2, PlateNumber = 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);
            fuelCard_2.SubmitDate = DateTime.Now;
            car_2.FuelCards.Add(fuelCard_2);
        }
        else // Person Exists
        {
            carPlateNumber_2 = new Ajancy.CarPlateNumber { Car = car_2, PlateNumber = 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);
            fuelCard_2.SubmitDate = DateTime.Now;
            car_2.FuelCards.Add(fuelCard_2);
        }

        // --------------- setting values

        if (this.txtCarPlateNumber_1.Text.Trim().Equals(this.txtCarPlateNumber_1_2.Text.Trim()) &&
            this.drpCarPlateNumber.SelectedValue.Equals(this.drpCarPlateNumber_2.SelectedValue) &&
            this.txtCarPlateNumber_2.Text.Trim().Equals(this.txtCarPlateNumber_2_2.Text.Trim()) &&
            this.txtCarPlateNumber_3.Text.Trim().Equals(this.txtCarPlateNumber_3_2.Text.Trim()))
        {
            carPlateNumber_2.PlateNumber = plateNumber;
        }
        else
        {
            plateNumber_2.TwoDigits = this.txtCarPlateNumber_1_2.Text.Trim();
            plateNumber_2.Alphabet = this.drpCarPlateNumber_2.SelectedValue;
            plateNumber_2.ThreeDigits = this.txtCarPlateNumber_2_2.Text.Trim();
            plateNumber_2.RegionIdentifier = this.txtCarPlateNumber_3_2.Text.Trim();
        }

        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

        #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

        Ajancy.FuelCardSubstitution fcsReq = new Ajancy.FuelCardSubstitution
        {
            FuelCard = fuelCard,
            UserInRoleID = Public.ActiveUserRole.UserRoleID,
            SubmitDate = DateTime.Now
        };

        db.FuelCardSubstitutions.InsertOnSubmit(fcsReq);
        db.SubmitChanges();
        fcsReq.PersonalTypeFuelCardID = fuelCard_2.FuelCardID;
        db.SubmitChanges();
        DisposeContext();
        Response.Redirect("~/Message.aspx?mode=11");
    }
Exemplo n.º 13
0
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (this.txtCarVIN.Text.ToUpper().Trim().Equals(this.txtCarVIN_2.Text.ToUpper().Trim()))
            {
                this.lblMessage.Text = "VIN خودرو بخش آژانسی با VIN خودرو بخش  شخصی نباید یکسان باشد";
                return;
            }

            if (this.txtFuelCardPAN.Text.ToUpper().Trim().Equals(this.txtFuelCardPAN_2.Text.ToUpper().Trim()))
            {
                this.lblMessage.Text = "PAN کارت سوخت بخش آژانسی با PAN کارت سوخت بخش  شخصی نباید یکسان باشد";
                return;
            }

            if (this.txtNationalCode.Text.Trim() == this.txtNationalCode_2.Text.Trim() && this.ViewState["Mode"] == null)
            {
                SelfReplacement_Save();
            }

            if (this.txtCarPlateNumber_1.Text.Trim().Equals(this.txtCarPlateNumber_1_2.Text.Trim()) &&
                this.drpCarPlateNumber.SelectedValue.Equals(this.drpCarPlateNumber_2.SelectedValue) &&
                this.txtCarPlateNumber_2.Text.Trim().Equals(this.txtCarPlateNumber_2_2.Text.Trim()) &&
                this.txtCarPlateNumber_3.Text.Trim().Equals(this.txtCarPlateNumber_3_2.Text.Trim()))
            {
                this.lblMessage.Text = "پلاک خودرو بخش آژانسی با پلاک خودرو بخش شخصی نباید یکسان باشد";
                return;
            }

            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.PlateNumber plateNumber = new Ajancy.PlateNumber();
            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.PlateNumber pln = db.PlateNumbers.FirstOrDefault<Ajancy.PlateNumber>(pl => pl.TwoDigits == this.txtCarPlateNumber_1.Text.Trim() &&
                                                                                                                                    pl.Alphabet == this.drpCarPlateNumber.SelectedValue &&
                                                                                                                                    pl.ThreeDigits == this.txtCarPlateNumber_2.Text.Trim() &&
                                                                                                                                    pl.RegionIdentifier == this.txtCarPlateNumber_3.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, PlateNumber = plateNumber };
                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.PlateNumber;
                    car = carPlateNumber.Car;
                    fuelCard = car.FuelCards.Last<Ajancy.FuelCard>();
                    fuelCard.DiscardDate = null;

                    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.PlateNumber pln = db.PlateNumbers.FirstOrDefault<Ajancy.PlateNumber>(pl => pl.TwoDigits == this.txtCarPlateNumber_1.Text.Trim() &&
                                                                                                                                        pl.Alphabet == this.drpCarPlateNumber.SelectedValue &&
                                                                                                                                        pl.ThreeDigits == this.txtCarPlateNumber_2.Text.Trim() &&
                                                                                                                                        pl.RegionIdentifier == this.txtCarPlateNumber_3.Text.Trim() &&
                                                                                                                                        pl.PlateNumberID != carPlateNumber.PlateNumber.PlateNumberID);
                    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.PlateNumber pln = db.PlateNumbers.FirstOrDefault<Ajancy.PlateNumber>(pl => pl.TwoDigits == this.txtCarPlateNumber_1.Text.Trim() &&
                                                                                                                                        pl.Alphabet == this.drpCarPlateNumber.SelectedValue &&
                                                                                                                                        pl.ThreeDigits == this.txtCarPlateNumber_2.Text.Trim() &&
                                                                                                                                        pl.RegionIdentifier == this.txtCarPlateNumber_3.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, PlateNumber = plateNumber };
                    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.TwoDigits = this.txtCarPlateNumber_1.Text.Trim();
            plateNumber.Alphabet = this.drpCarPlateNumber.SelectedValue;
            plateNumber.ThreeDigits = this.txtCarPlateNumber_2.Text.Trim();
            plateNumber.RegionIdentifier = this.txtCarPlateNumber_3.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();

            #endregion

            #region PersonalType

            Ajancy.Person person_2 = new Ajancy.Person();
            Ajancy.DriverCertification driverCertification_2 = new Ajancy.DriverCertification();
            Ajancy.DriverCertificationCar driverCertificationCar_2 = new Ajancy.DriverCertificationCar();
            Ajancy.CarPlateNumber carPlateNumber_2 = new Ajancy.CarPlateNumber();
            Ajancy.PlateNumber plateNumber_2 = new Ajancy.PlateNumber();
            Ajancy.Car car_2 = new Ajancy.Car();
            Ajancy.FuelCard fuelCard_2 = new Ajancy.FuelCard();

            if (this.ViewState["PersonID_2"] == null) // New Person
            {
                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.PlateNumber pnl = db.PlateNumbers.FirstOrDefault<Ajancy.PlateNumber>(pl => pl.TwoDigits == this.txtCarPlateNumber_1_2.Text.Trim() &&
                                                                                                                                   pl.Alphabet == this.drpCarPlateNumber_2.SelectedValue &&
                                                                                                                                   pl.ThreeDigits == this.txtCarPlateNumber_2_2.Text.Trim() &&
                                                                                                                                   pl.RegionIdentifier == this.txtCarPlateNumber_3_2.Text.Trim());
                if (pnl != null)
                {
                    this.lblMessage.Text = "شماره پلاک وارد شده در  بخش کارت سوخت شخصی متعلق به شخص دیگری میباشد";
                    return;
                }

                Ajancy.User user = new Ajancy.User();
                user.UserName = this.txtNationalCode_2.Text.Trim();
                user.ProvinceID = Public.ToByte(this.drpProvince_2.SelectedValue);
                user.CityID = Public.ToShort(this.drpCity_2.SelectedValue);
                user.SubmitDate = DateTime.Now;
                user.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = (short)Public.Role.TaxiDriver, MembershipDate = DateTime.Now });
                person_2.User = user;
                person_2.NationalCode = this.txtNationalCode_2.Text.Trim();
                person_2.SubmitDate = DateTime.Now;

                driverCertification_2 = new Ajancy.DriverCertification
                {
                    CertificationType = (byte)Public.AjancyType.TaxiAjancy,
                    SubmitDate = DateTime.Now
                };
                carPlateNumber_2 = new Ajancy.CarPlateNumber { Car = car_2, PlateNumber = 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_2.DriverCertificationCars.Add(driverCertificationCar_2);
                person_2.DriverCertifications.Add(driverCertification_2);
                fuelCard_2.SubmitDate = DateTime.Now;
                car_2.FuelCards.Add(fuelCard_2);
                db.Persons.InsertOnSubmit(person_2);
            }
            else // Person Exists
            {
                person_2 = db.Persons.FirstOrDefault<Ajancy.Person>(p => p.PersonID == Public.ToInt(this.ViewState["PersonID_2"]));
                if (person_2.User.UsersInRoles.Any<Ajancy.UsersInRole>(ur => ur.RoleID == (short)Public.Role.TaxiDriver))  // Person is a driver
                {
                    driverCertification_2 = person_2.DriverCertifications.First<Ajancy.DriverCertification>(dc => dc.CertificationType == (byte)Public.AjancyType.TaxiAjancy);
                    driverCertificationCar_2 = driverCertification_2.DriverCertificationCars.Last<Ajancy.DriverCertificationCar>(dcc => dcc.LockOutDate == null);
                    carPlateNumber_2 = driverCertificationCar_2.CarPlateNumber;
                    plateNumber_2 = carPlateNumber_2.PlateNumber;
                    car_2 = carPlateNumber_2.Car;
                    fuelCard_2 = car_2.FuelCards.Last<Ajancy.FuelCard>();
                    fuelCard_2.DiscardDate = null;

                    if (db.Cars.Any<Ajancy.Car>(c => c.VIN == this.txtCarVIN_2.Text.Trim() && c.CarID != car_2.CarID))
                    {
                        this.lblMessage.Text = "خودرو بخش کارت سوخت شخصی با این شماره VIN متعلق به شخص دیگری میباشد";
                        return;
                    }

                    if (db.FuelCards.Any<Ajancy.FuelCard>(fc => fc.PAN == this.txtFuelCardPAN_2.Text && fc.FuelCardID != fuelCard_2.FuelCardID))
                    {
                        this.lblMessage.Text = "شماره PAN کارت سوخت خودرو بخش کارت سوخت شخصی قبلا برای خودرو دیگری ثبت شده";
                        return;
                    }

                    Ajancy.PlateNumber pnl = db.PlateNumbers.FirstOrDefault<Ajancy.PlateNumber>(pl => pl.TwoDigits == this.txtCarPlateNumber_1_2.Text.Trim() &&
                                                                                                                                       pl.Alphabet == this.drpCarPlateNumber_2.SelectedValue &&
                                                                                                                                       pl.ThreeDigits == this.txtCarPlateNumber_2_2.Text.Trim() &&
                                                                                                                                       pl.RegionIdentifier == this.txtCarPlateNumber_3_2.Text.Trim() &&
                                                                                                                                       pl.PlateNumberID != carPlateNumber_2.PlateNumber.PlateNumberID);
                    if (pnl != null)
                    {
                        this.lblMessage.Text = "شماره پلاک وارد شده در  بخش کارت سوخت شخصی متعلق به شخص دیگری میباشد";
                        return;
                    }

                    Ajancy.AjancyDriver ajancyDriver_2 = driverCertificationCar_2.AjancyDrivers.Last<Ajancy.AjancyDriver>(jd => jd.LockOutDate == null);
                    int ajancyId_2 = Public.ToInt(this.drpAjancies_2.SelectedValue);
                    if (ajancyDriver_2.AjancyID != ajancyId_2) // Driver has moved to one other ajancy
                    {
                        ajancyDriver_2.LockOutDate = DateTime.Now;
                        driverCertificationCar_2.AjancyDrivers.Add(new Ajancy.AjancyDriver { AjancyID = ajancyId_2, MembershipDate = DateTime.Now });
                    }

                    //if (Public.ActiveUserRole.RoleID == (short)Public.Role.ProvinceManager)
                    //{
                    //    person_2.User.CityID = Public.ToShort(this.drpCity_2.SelectedValue);
                    //}
                }
                else // Person is not driver
                {
                    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.PlateNumber pnl = db.PlateNumbers.FirstOrDefault<Ajancy.PlateNumber>(pl => pl.TwoDigits == this.txtCarPlateNumber_1_2.Text.Trim() &&
                                                                                                                                       pl.Alphabet == this.drpCarPlateNumber_2.SelectedValue &&
                                                                                                                                       pl.ThreeDigits == this.txtCarPlateNumber_2_2.Text.Trim() &&
                                                                                                                                       pl.RegionIdentifier == this.txtCarPlateNumber_3_2.Text.Trim());
                    if (pnl != null)
                    {
                        this.lblMessage.Text = "شماره پلاک وارد شده در  بخش کارت سوخت شخصی متعلق به شخص دیگری میباشد";
                        return;
                    }

                    person_2.User.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = (short)Public.Role.TaxiDriver, MembershipDate = DateTime.Now });
                    driverCertification_2 = new Ajancy.DriverCertification
                    {
                        CertificationType = (byte)Public.AjancyType.TaxiAjancy,
                        SubmitDate = DateTime.Now
                    };
                    carPlateNumber_2 = new Ajancy.CarPlateNumber { Car = car_2, PlateNumber = 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), DriverCertificationCar = driverCertificationCar_2, MembershipDate = DateTime.Now });
                    driverCertification_2.DriverCertificationCars.Add(driverCertificationCar_2);
                    person_2.DriverCertifications.Add(driverCertification_2);
                    fuelCard_2.SubmitDate = DateTime.Now;
                    car_2.FuelCards.Add(fuelCard_2);
                }

                switch ((Public.Role)Public.ActiveUserRole.RoleID)
                {
                    case Public.Role.ProvinceManager:
                        person_2.User.CityID = Public.ToShort(this.drpCity_2.SelectedValue);
                        break;

                    case Public.Role.Admin:
                        person_2.User.ProvinceID = Public.ToByte(this.drpProvince_2.SelectedValue);
                        person_2.User.CityID = Public.ToShort(this.drpCity_2.SelectedValue);
                        break;
                }
            }

            // --------------- setting values

            person_2.FirstName = this.txtFirstName_2.Text.Trim();
            person_2.LastName = this.txtLastName_2.Text.Trim();

            plateNumber_2.TwoDigits = this.txtCarPlateNumber_1_2.Text.Trim();
            plateNumber_2.Alphabet = this.drpCarPlateNumber_2.SelectedValue;
            plateNumber_2.ThreeDigits = this.txtCarPlateNumber_2_2.Text.Trim();
            plateNumber_2.RegionIdentifier = this.txtCarPlateNumber_3_2.Text.Trim();

            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

            #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_2.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_2.User.ProvinceID
                                ,
                                CityID = person_2.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_2.CarPlateNumbers.Add(carPlateNumber_2);
                        if (carPlateNumber_2.OwnerPersonID > 0 && carPlateNumber_2.OwnerPersonID != person_2.PersonID) // Set driver as owner again
                        {
                            carPlateNumber_2.Person = person_2;
                        }
                    }
                }

                if (ajancyTypeOwner && this.txtNationalCode_2.Text == this.txtOwnerNationalCode.Text) // Cross \
                {
                    if (!person_2.User.UsersInRoles.Any<Ajancy.UsersInRole>(ur => ur.RoleID == (short)Public.Role.CarOwner))
                    {
                        person_2.User.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = (short)Public.Role.CarOwner, MembershipDate = DateTime.Now, LockOutDate = DateTime.Now });
                    }
                    person_2.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 (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)
                    {
                        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;
                    }
                }

                Ajancy.FuelCardSubstitution fcsReq = new Ajancy.FuelCardSubstitution
                {
                    FuelCard = fuelCard,
                    UserInRoleID = Public.ActiveUserRole.UserRoleID,
                    SubmitDate = DateTime.Now
                };

                db.FuelCardSubstitutions.InsertOnSubmit(fcsReq);
                db.SubmitChanges();
                fcsReq.PersonalTypeFuelCardID = fuelCard_2.FuelCardID;
            }
            else // Edit mode
            {
                db.FuelCardSubstitutions.FirstOrDefault<Ajancy.FuelCardSubstitution>(fcs => (fcs.AjancyTypeFuelCardID == fuelCard.FuelCardID && fcs.PersonalTypeFuelCardID.GetValueOrDefault() == fuelCard_2.FuelCardID)).SubmitDate = DateTime.Now;
            }

            db.SubmitChanges();
            DisposeContext();
            Response.Redirect("~/Message.aspx?mode=11");
        }
    }