protected void btnSave_Click(object sender, EventArgs e)
    {
        if (this.Page.IsValid)
        {
            int ajancyId = 0;
            Ajancy.Person person = null;
            Ajancy.Ajancy ajancy = new Ajancy.Ajancy();
            Ajancy.BusinessLicense businessLicense = new Ajancy.BusinessLicense();
            db = new Ajancy.Kimia_Ajancy(Public.ConnectionString);

            if (HttpContext.Current.User.IsInRole(Public.Role.AjancyManager.ToString())) // Add mode
            {
                if (db.Ajancies.Any<Ajancy.Ajancy>(j => j.PostalCode == this.txtPlacePostalCode.Text.Trim()))
                {
                    this.lblMessage.Text = "کد پستی واحد تجاری تکراری میباشد";// Duplicate PostalCode
                    return;
                }
                else if (db.Ajancies.Any<Ajancy.Ajancy>(j => j.CityID == int.Parse(this.drpCity.SelectedValue) &&
                                                                                       j.AjancyType != (byte)Public.AjancyType.Academy &&
                                                                                       j.AjancyName.Equals(this.txtAjancyName.Text.Trim())))
                {
                    this.lblMessage.Text = string.Format("آژانسی با نام <b>{0}</b> در شهرستان <b>{1}</b> موجود میباشد", ajancy.AjancyName, this.drpCity.SelectedItem.Text);
                    return;
                }

                dlo = new DataLoadOptions();
                dlo.LoadWith<Ajancy.Person>(p => p.User);
                dlo.LoadWith<Ajancy.User>(u => u.UsersInRoles);
                dlo.LoadWith<Ajancy.UsersInRole>(ur => ur.AjancyPartners);
                db.LoadOptions = dlo;

                person = db.Persons.First<Ajancy.Person>(p => p.NationalCode == HttpContext.Current.User.Identity.Name);
                Ajancy.AjancyPartner partner = person.User.UsersInRoles.Single<Ajancy.UsersInRole>(ur => ur.RoleID == (short)Public.Role.AjancyManager && ur.LockOutDate == null).AjancyPartners.Single<Ajancy.AjancyPartner>(jp => jp.AjancyID == null);
                ajancy.AjancyPartners.Add(partner);
                ajancy.BusinessLicenses.Add(businessLicense);
                businessLicense.BusinessLicenseRevivals.Add(new Ajancy.BusinessLicenseRevival { StartDate = this.txtStartDate.GeorgianDate.Value, EndDate = this.txtEndDate.GeorgianDate.Value, SubmitDate = DateTime.Now });
                ajancy.SubmitDate = DateTime.Now;
                db.Ajancies.InsertOnSubmit(ajancy);
            }
            else if (!HttpContext.Current.User.IsInRole(Public.Role.AjancyManager.ToString()) && int.TryParse(this.ViewState["AjancyID"].ToString(), out ajancyId)) // Edit mode
            {
                dlo = new DataLoadOptions();
                dlo.LoadWith<Ajancy.Ajancy>(j => j.AjancyUtilities);
                dlo.LoadWith<Ajancy.Ajancy>(j => j.BusinessLicenses);
                dlo.LoadWith<Ajancy.BusinessLicense>(bl => bl.BusinessLicenseRevivals);
                dlo.LoadWith<Ajancy.Ajancy>(j => j.AjancyPartners);
                dlo.LoadWith<Ajancy.AjancyPartner>(jp => jp.UsersInRole);
                dlo.LoadWith<Ajancy.UsersInRole>(ur => ur.User);
                dlo.LoadWith<Ajancy.User>(u => u.Person);
                db.LoadOptions = dlo;

                ajancy = db.Ajancies.FirstOrDefault<Ajancy.Ajancy>(j => j.AjancyID == ajancyId);
                businessLicense = ajancy.BusinessLicenses.First<Ajancy.BusinessLicense>();
                person = ajancy.AjancyPartners.Single<Ajancy.AjancyPartner>(jp => jp.UsersInRole.RoleID == (short)Public.Role.AjancyManager && jp.LockOutDate == null).UsersInRole.User.Person;
            }

            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();

            ajancy.AjancyType = Public.ToByte(this.drpAjancyType.SelectedValue);
            ajancy.AjancyName = this.txtAjancyName.Text.Trim();
            ajancy.CityID = Public.ToShort(this.drpCity.SelectedValue);
            ajancy.BusinessLicenseType = Public.ToByte(this.drpBusinessLicenseType.SelectedValue);
            ajancy.OfficePosition = Public.ToByte(this.drpOfficePosition.SelectedValue);
            ajancy.OfficeLevel = this.txtOfficeLevel.Text.Trim();
            ajancy.OfficeSpace = this.txtOfficeSpace.Text.Trim();
            ajancy.BalconySpace = this.txtBalconySpace.Text.Trim();
            ajancy.BalconyHeight = this.txtBalconyHeight.Text.Trim();
            ajancy.ParkingSpace = this.txtParkingSpace.Text.Trim();
            ajancy.ParkingState = this.drpParkingState.SelectedIndex == 0 ? true : false;
            ajancy.BusinessScope = this.txtBusinessScope.Text.Trim();
            ajancy.PoliceStation = this.txtPoliceStation.Text.Trim();
            ajancy.Mayor = (byte)this.drpMayor.SelectedIndex;
            ajancy.Address = this.txtPlaceAddress.Text.Trim();
            ajancy.Phone = this.txtBusinessPlacePhone.Text.Trim();
            ajancy.RegisteredPelak = this.txtRegisteredPelak.Text.Trim();
            ajancy.BluePelak = this.txtBluePelak.Text.Trim();
            ajancy.PostalCode = this.txtPlacePostalCode.Text.Trim();
            ajancy.EstateType = Public.ToByte(this.drpEstateType.SelectedValue);
            ajancy.DocumentType = Public.ToByte(this.drpDocumentType.SelectedValue);
            ajancy.PlaceOwner = this.txtPlaceOwner.Text.Trim();

            businessLicense.BusinessLicenseNo = this.txtBusinessLicenseNo.Text.Trim();
            businessLicense.MemberShipCode = this.txtMemberShipCode.Text.Trim();
            businessLicense.NationalCardBarCode = this.txtNationalCardBarCode.Text.Trim();
            businessLicense.SerialNo = this.txtSerialNo.Text.Trim();
            businessLicense.CategoryCode = this.txtCategoryCode.Text.Trim();
            businessLicense.ISIC = this.txtISIC.Text.Trim();
            businessLicense.SubmitDate = DateTime.Now;

            Ajancy.AjancyUtility utility = null;
            foreach (ListItem item in this.lstUtility.Items)
            {
                utility = ajancy.AjancyUtilities.FirstOrDefault<Ajancy.AjancyUtility>(u => u.Utility == Public.ToByte(item.Value));
                if (!item.Selected && utility != null)
                {
                    db.AjancyUtilities.DeleteOnSubmit(utility);
                }
                else if (item.Selected && utility == null)
                {
                    ajancy.AjancyUtilities.Add(new Ajancy.AjancyUtility { Utility = Public.ToByte(item.Value) });
                }
            }

            #region SetPartners

            if (!string.IsNullOrEmpty(this.txtPartnerName1.Text.Trim()) && !string.IsNullOrEmpty(this.txtPartnerFamily1.Text.Trim()) && !string.IsNullOrEmpty(this.txtPartnerNationalCode1.Text.Trim()))
            {
                Ajancy.Person prsn = db.Persons.FirstOrDefault<Ajancy.Person>(p => p.NationalCode == this.txtPartnerNationalCode1.Text.Trim());
                Ajancy.UsersInRole userRole = null;
                if (prsn == null)
                {
                    prsn = new Ajancy.Person { NationalCode = this.txtPartnerNationalCode1.Text.Trim(), FirstName = this.txtPartnerName1.Text.Trim(), LastName = this.txtPartnerFamily1.Text.Trim(), SubmitDate = DateTime.Now };
                    Ajancy.User user = new Ajancy.User();
                    user.UserName = this.txtPartnerNationalCode1.Text.Trim();
                    user.ProvinceID = Public.ToByte(this.drpProvince.SelectedValue);
                    user.CityID = Public.ToShort(this.drpCity.SelectedValue);
                    user.SubmitDate = DateTime.Now;
                    userRole = new Ajancy.UsersInRole { RoleID = (short)Public.Role.AjancyPartner, MembershipDate = DateTime.Now };
                    userRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                    user.UsersInRoles.Add(userRole);
                    prsn.User = user;
                    db.Persons.InsertOnSubmit(prsn);
                }
                else
                {
                    userRole = prsn.User.UsersInRoles.SingleOrDefault<Ajancy.UsersInRole>(ur => ur.RoleID == (short)Public.Role.AjancyPartner);
                    if (userRole == null)
                    {
                        userRole = new Ajancy.UsersInRole { RoleID = (short)Public.Role.AjancyPartner, MembershipDate = DateTime.Now };
                        userRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                        prsn.User.UsersInRoles.Add(userRole);
                    }
                    else if (!userRole.AjancyPartners.Any<Ajancy.AjancyPartner>(jp => jp.AjancyID == ajancy.AjancyID))
                    {
                        userRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                    }
                }
            }
            if (!string.IsNullOrEmpty(this.txtPartnerName2.Text.Trim()) && !string.IsNullOrEmpty(this.txtPartnerFamily2.Text.Trim()) && !string.IsNullOrEmpty(this.txtPartnerNationalCode2.Text.Trim()))
            {
                Ajancy.Person prsn = db.Persons.FirstOrDefault<Ajancy.Person>(p => p.NationalCode == this.txtPartnerNationalCode2.Text.Trim());
                Ajancy.UsersInRole userRole = null;
                if (prsn == null)
                {
                    prsn = new Ajancy.Person { NationalCode = this.txtPartnerNationalCode2.Text.Trim(), FirstName = this.txtPartnerName2.Text.Trim(), LastName = this.txtPartnerFamily2.Text.Trim(), SubmitDate = DateTime.Now };
                    Ajancy.User user = new Ajancy.User();
                    user.UserName = this.txtPartnerNationalCode2.Text.Trim();
                    user.ProvinceID = Public.ToByte(this.drpProvince.SelectedValue);
                    user.CityID = Public.ToShort(this.drpCity.SelectedValue);
                    user.SubmitDate = DateTime.Now;
                    userRole = new Ajancy.UsersInRole { RoleID = (short)Public.Role.AjancyPartner, MembershipDate = DateTime.Now };
                    userRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                    user.UsersInRoles.Add(userRole);
                    prsn.User = user;
                    db.Persons.InsertOnSubmit(prsn);
                }
                else
                {
                    userRole = prsn.User.UsersInRoles.SingleOrDefault<Ajancy.UsersInRole>(ur => ur.RoleID == (short)Public.Role.AjancyPartner);
                    if (userRole == null)
                    {
                        userRole = new Ajancy.UsersInRole { RoleID = (short)Public.Role.AjancyPartner, MembershipDate = DateTime.Now };
                        userRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                        prsn.User.UsersInRoles.Add(userRole);
                    }
                    else if (!userRole.AjancyPartners.Any<Ajancy.AjancyPartner>(jp => jp.AjancyID == ajancy.AjancyID))
                    {
                        userRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                    }
                }
            }
            if (!string.IsNullOrEmpty(this.txtPartnerName3.Text.Trim()) && !string.IsNullOrEmpty(this.txtPartnerFamily3.Text.Trim()) && !string.IsNullOrEmpty(this.txtPartnerNationalCode3.Text.Trim()))
            {
                Ajancy.Person prsn = db.Persons.FirstOrDefault<Ajancy.Person>(p => p.NationalCode == this.txtPartnerNationalCode3.Text.Trim());
                Ajancy.UsersInRole userRole = null;
                if (prsn == null)
                {
                    prsn = new Ajancy.Person { NationalCode = this.txtPartnerNationalCode3.Text.Trim(), FirstName = this.txtPartnerName3.Text.Trim(), LastName = this.txtPartnerFamily3.Text.Trim(), SubmitDate = DateTime.Now };
                    Ajancy.User user = new Ajancy.User();
                    user.UserName = this.txtPartnerNationalCode3.Text.Trim();
                    user.ProvinceID = Public.ToByte(this.drpProvince.SelectedValue);
                    user.CityID = Public.ToShort(this.drpCity.SelectedValue);
                    user.SubmitDate = DateTime.Now;
                    userRole = new Ajancy.UsersInRole { RoleID = (short)Public.Role.AjancyPartner, MembershipDate = DateTime.Now };
                    userRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                    user.UsersInRoles.Add(userRole);
                    prsn.User = user;
                    db.Persons.InsertOnSubmit(prsn);
                }
                else
                {
                    userRole = prsn.User.UsersInRoles.SingleOrDefault<Ajancy.UsersInRole>(ur => ur.RoleID == (short)Public.Role.AjancyPartner);
                    if (userRole == null)
                    {
                        userRole = new Ajancy.UsersInRole { RoleID = (short)Public.Role.AjancyPartner, MembershipDate = DateTime.Now };
                        userRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                        prsn.User.UsersInRoles.Add(userRole);
                    }
                    else if (!userRole.AjancyPartners.Any<Ajancy.AjancyPartner>(jp => jp.AjancyID == ajancy.AjancyID))
                    {
                        userRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                    }
                }
            }

            #endregion

            try
            {
                db.SubmitChanges();
                DisposeContext();
                this.Session["Ajancy"] = new Ajancy.Ajancy { AjancyID = ajancy.AjancyID, AjancyType = ajancy.AjancyType, AjancyName = ajancy.AjancyName };
                Response.Redirect("~/Message.aspx?mode=4");
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("IX_BusinessLicenses"))
                {
                    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");
        }
    }
Exemple #3
0
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            Ajancy.Person person = new Ajancy.Person();
            person.NationalCode = this.txtNationalCode.Text;
            person.FirstName = this.txtFirstName.Text;
            person.LastName = this.txtLastName.Text;
            person.Mobile = this.txtMobile.Text.Trim();
            person.SubmitDate = DateTime.Now;
            Ajancy.User user = new Ajancy.User();
            user.UserName = this.txtNationalCode.Text;
            user.PassWord = FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtPassword.Text, "SHA1");
            user.ProvinceID = Public.ToByte(this.drpProvince.SelectedValue);
            user.CityID = Public.ToShort(this.drpCity.SelectedValue);
            user.UsersInRoles.Add(new Ajancy.UsersInRole { RoleID = Public.ToShort(this.drpRoles.SelectedValue), MembershipDate = DateTime.Now });
            person.User = user;
            db = new Ajancy.Kimia_Ajancy(Public.ConnectionString);

            switch ((Public.Role)byte.Parse(this.drpRoles.SelectedValue))
            {
                case Public.Role.AjancyManager:
                case Public.Role.AjancySupervisor:
                case Public.Role.AjancySecretary:
                case Public.Role.AjancyPartner:
                    user.UsersInRoles[0].AjancyPartners.Add(new Ajancy.AjancyPartner { SubmitDate = DateTime.Now });
                    break;

                case Public.Role.ProvinceManager:
                    var prvManager = from u in db.Users
                                     join ur in db.UsersInRoles on u.UserID equals ur.UserID
                                     where ur.RoleID == (short)Public.Role.ProvinceManager &&
                                                u.ProvinceID == user.ProvinceID
                                     select new { u.UserID };
                    foreach (var item in prvManager)
                    {
                        this.lblMessage.Text = string.Format("برای استان {0} کاربری با سمت مدیر استان موجود میباشد", this.drpProvince.SelectedItem.Text);
                        return;
                    }
                    break;

                case Public.Role.CityManager:
                    var ctyManager = from u in db.Users
                                     join ur in db.UsersInRoles on u.UserID equals ur.UserID
                                     where ur.RoleID == (short)Public.Role.CityManager &&
                                                u.CityID == user.CityID
                                     select new { u.UserID };
                    foreach (var item in ctyManager)
                    {
                        this.lblMessage.Text = string.Format("برای شهرستان {0} کاربری با سمت مدیر شهرستان موجود میباشد", this.drpCity.SelectedItem.Text);
                        return;
                    }
                    break;

                case Public.Role.AcademyProvince:
                    var acdPrv = from u in db.Users
                                 join ur in db.UsersInRoles on u.UserID equals ur.UserID
                                 where ur.RoleID == (short)Public.Role.AcademyProvince &&
                                                u.ProvinceID == user.ProvinceID
                                 select new { u.UserID };
                    foreach (var item in acdPrv)
                    {
                        this.lblMessage.Text = string.Format("برای استان {0} کاربری با سمت مدیر استان موجود میباشد", this.drpProvince.SelectedItem.Text);
                        return;
                    }
                    break;

                case Public.Role.AcademyCity:
                    var acdCty = from u in db.Users
                                 join ur in db.UsersInRoles on u.UserID equals ur.UserID
                                 where ur.RoleID == (short)Public.Role.AcademyCity &&
                                                u.CityID == user.CityID
                                 select new { u.UserID };
                    foreach (var item in acdCty)
                    {
                        this.lblMessage.Text = string.Format("برای شهرستان {0} کاربری با سمت مدیر شهرستان موجود میباشد", this.drpCity.SelectedItem.Text);
                        return;
                    }
                    break;
            }

            try
            {
                db.Persons.InsertOnSubmit(person);
                db.SubmitChanges();
                this.lblMessage.Text = "ثبت کاربر انجام شد";
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("IX_Persons"))
                {
                    this.lblMessage.Text = "کد ملی تکراری میباشد!";
                }
                else if (ex.Message.Contains("IX_Persons_2"))
                {
                    this.lblMessage.Text = "نام کاربری تکراری میباشد!";
                }
            }

            this.txtNationalCode.Text = null;
            this.txtFirstName.Text = null;
            this.txtLastName.Text = null;
            this.txtMobile.Text = null;
            this.txtPassword.Text = null;
            this.txtRePassword.Text = null;
            this.drpRoles.SelectedIndex = 0;
            this.drpProvince.SelectedIndex = 0;
            this.drpCity.SelectedIndex = 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");
    }
Exemple #5
0
		private void attach_Users(User entity)
		{
			this.SendPropertyChanging();
			entity.Province = this;
		}
Exemple #6
0
		private void detach_Users(User entity)
		{
			this.SendPropertyChanging();
			entity.Province = null;
		}
Exemple #7
0
 partial void UpdateUser(User instance);
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (this.Page.IsValid)
        {
            Ajancy.Person person = null;
            Ajancy.Ajancy ajancy = new Ajancy.Ajancy();
            Ajancy.FormerBusiness formerBusiness = new Ajancy.FormerBusiness();
            Ajancy.UsersInRole usersInRole = new Ajancy.UsersInRole { RoleID = (short)Public.Role.AjancyManager, MembershipDate = DateTime.Now, LockOutDate = DateTime.Now };
            db = new Ajancy.Kimia_Ajancy(Public.ConnectionString);

            if (this.ViewState["AjancyID"] == null) // Add mode
            {
                ajancy.SubmitDate = DateTime.Now;
                db.Ajancies.InsertOnSubmit(ajancy);

                if (this.ViewState["PersonID"] == null) // Add mode
                {
                    person = new Ajancy.Person();
                    person.NationalCode = this.txtNationalCode.Text;
                    person.SubmitDate = DateTime.Now;
                    Ajancy.User user = new Ajancy.User();
                    user.UserName = this.txtNationalCode.Text;
                    user.ProvinceID = Public.ToByte(this.drpProvince.SelectedValue);
                    user.CityID = Public.ToShort(this.drpCity.SelectedValue);
                    user.SubmitDate = DateTime.Now;
                    usersInRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                    user.UsersInRoles.Add(usersInRole);
                    person.User = user;
                    db.Persons.InsertOnSubmit(person);
                }
                else // Edit mode
                {
                    dlo = new DataLoadOptions();
                    dlo.LoadWith<Ajancy.Person>(p => p.User);
                    dlo.LoadWith<Ajancy.User>(u => u.UsersInRoles);
                    dlo.LoadWith<Ajancy.UsersInRole>(ur => ur.AjancyPartners);
                    db.LoadOptions = dlo;
                    person = db.Persons.FirstOrDefault<Ajancy.Person>(p => p.PersonID == Public.ToInt(this.ViewState["PersonID"]));
                    usersInRole = person.User.UsersInRoles.SingleOrDefault<Ajancy.UsersInRole>(ur => ur.RoleID == (short)Public.Role.AjancyManager);
                    if (usersInRole == null)
                    {
                        usersInRole = new Ajancy.UsersInRole { RoleID = (short)Public.Role.AjancyManager, MembershipDate = DateTime.Now, LockOutDate = DateTime.Now };
                        person.User.UsersInRoles.Add(usersInRole);
                    }
                    usersInRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now, LockOutDate = DateTime.Now });
                }
            }
            else // Edit mode
            {
                dlo = new DataLoadOptions();
                dlo.LoadWith<Ajancy.Ajancy>(j => j.AjancyUtilities);
                dlo.LoadWith<Ajancy.Ajancy>(j => j.BusinessLicenses);
                dlo.LoadWith<Ajancy.Ajancy>(j => j.AjancyPartners);
                dlo.LoadWith<Ajancy.AjancyPartner>(jp => jp.UsersInRole);
                dlo.LoadWith<Ajancy.UsersInRole>(ur => ur.User);
                dlo.LoadWith<Ajancy.User>(u => u.Person);
                db.LoadOptions = dlo;
                ajancy = db.Ajancies.FirstOrDefault<Ajancy.Ajancy>(j => j.AjancyID == Public.ToInt(this.ViewState["AjancyID"]));
                person = ajancy.AjancyPartners.Single<Ajancy.AjancyPartner>(jp => jp.UsersInRole.RoleID == (short)Public.Role.AjancyManager && jp.LockOutDate != null).UsersInRole.User.Person;
            }

            if (ajancy.FormerBusiness == null && !string.IsNullOrEmpty(this.txtFormerBusinessLicenseNo.Text))
            {
                formerBusiness.BusinessLicenseNo = this.txtFormerBusinessLicenseNo.Text;
                formerBusiness.BusinessLicenseDate = this.txtFormerBusinessLicenseDate.GeorgianDate != null ? this.txtFormerBusinessLicenseDate.GeorgianDate : null;
                formerBusiness.BusinessStartDate = this.txtFormerBusinessStartDate.GeorgianDate != null ? this.txtFormerBusinessStartDate.GeorgianDate : null;
                formerBusiness.Board = this.txtFormerBusinessBoard.Text;
                ajancy.FormerBusiness = formerBusiness;
            }
            else if (!string.IsNullOrEmpty(this.txtFormerBusinessLicenseNo.Text))
            {
                ajancy.FormerBusiness.BusinessLicenseNo = this.txtFormerBusinessLicenseNo.Text;
                ajancy.FormerBusiness.BusinessLicenseDate = this.txtFormerBusinessLicenseDate.GeorgianDate != null ? this.txtFormerBusinessLicenseDate.GeorgianDate : null;
                ajancy.FormerBusiness.BusinessStartDate = this.txtFormerBusinessStartDate.GeorgianDate != null ? this.txtFormerBusinessStartDate.GeorgianDate : null;
                ajancy.FormerBusiness.Board = this.txtFormerBusinessBoard.Text;
            }

            person.FirstName = this.txtFirstName.Text;
            person.LastName = this.txtLastName.Text;
            person.Father = this.txtFather.Text;
            person.BirthCertificateNo = this.txtBirthCertificateNo.Text;
            person.BirthCertificateSerial = this.txtBirthCertificateSerial.Text;
            person.BirthCertificateSerie = this.txtBirthCertificateSerie.Text;
            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;
            person.BirthCertificatePlace = this.txtBirthCertificatePlace.Text;
            person.FamilyMembersCount = this.txtFamilyMembersCount.Text;
            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;
            person.JobStatus = Public.ToByte(this.drpJobStatus.SelectedValue);
            person.Phone = this.txtPhone.Text;
            person.Mobile = this.txtMobile.Text;
            person.PostalCode = this.txtPostalCode.Text;
            person.Address = this.txtAddress.Text;

            ajancy.AjancyType = Public.ToByte(this.drpAjancyType.SelectedValue);
            ajancy.CityID = Public.ToShort(this.drpCity.SelectedValue);
            ajancy.BusinessLicenseType = Public.ToByte(this.drpBusinessLicenseType.SelectedValue);
            ajancy.OfficePosition = Public.ToByte(this.drpOfficePosition.SelectedValue);
            ajancy.OfficeLevel = this.txtOfficeLevel.Text;
            ajancy.OfficeSpace = this.txtOfficeSpace.Text;
            ajancy.BalconySpace = this.txtBalconySpace.Text;
            ajancy.BalconyHeight = this.txtBalconyHeight.Text;
            ajancy.ParkingSpace = this.txtParkingSpace.Text;
            ajancy.ParkingState = this.drpParkingState.SelectedIndex == 0 ? true : false;
            ajancy.BusinessScope = this.txtBusinessScope.Text;
            ajancy.PoliceStation = this.txtPoliceStation.Text;
            ajancy.Mayor = Public.ToByte(this.drpMayor.SelectedValue);
            ajancy.Address = this.txtPlaceAddress.Text;
            ajancy.Phone = this.txtBusinessPlacePhone.Text;
            ajancy.RegisteredPelak = this.txtRegisteredPelak.Text;
            ajancy.BluePelak = this.txtBluePelak.Text;
            ajancy.PostalCode = this.txtPlacePostalCode.Text;
            ajancy.EstateType = Public.ToByte(this.drpEstateType.SelectedValue);
            ajancy.DocumentType = Public.ToByte(this.drpDocumentType.SelectedValue);
            ajancy.PlaceOwner = this.txtPlaceOwner.Text;
            ajancy.WaterBillSerial = this.txtWaterBillSerial.Text;
            ajancy.ElectricityBillSerial = this.txtElectricityBillSerial.Text;
            ajancy.GasBillSerial = this.txtGasBillSerial.Text;

            Ajancy.AjancyUtility utility = null;
            foreach (ListItem item in this.lstUtility.Items)
            {
                utility = ajancy.AjancyUtilities.FirstOrDefault<Ajancy.AjancyUtility>(u => u.Utility == Public.ToByte(item.Value));
                if (!item.Selected && utility != null)
                {
                    db.AjancyUtilities.DeleteOnSubmit(utility);
                }
                else if (item.Selected && utility == null)
                {
                    ajancy.AjancyUtilities.Add(new Ajancy.AjancyUtility { Utility = Public.ToByte(item.Value) });
                }
            }

            #region SetPartners

            if (!string.IsNullOrEmpty(this.txtPartnerName1.Text) && !string.IsNullOrEmpty(this.txtPartnerFamily1.Text) && !string.IsNullOrEmpty(this.txtPartnerNationalCode1.Text))
            {
                Ajancy.Person prsn = db.Persons.FirstOrDefault<Ajancy.Person>(p => p.NationalCode == this.txtPartnerNationalCode1.Text);
                Ajancy.UsersInRole userRole = null;
                if (prsn == null)
                {
                    prsn = new Ajancy.Person { NationalCode = this.txtPartnerNationalCode1.Text, FirstName = this.txtPartnerName1.Text, LastName = this.txtPartnerFamily1.Text, SubmitDate = DateTime.Now };
                    Ajancy.User user = new Ajancy.User();
                    user.UserName = this.txtPartnerNationalCode1.Text;
                    user.ProvinceID = Public.ToByte(this.drpProvince.SelectedValue);
                    user.CityID = Public.ToShort(this.drpCity.SelectedValue);
                    user.SubmitDate = DateTime.Now;
                    userRole = new Ajancy.UsersInRole { RoleID = (short)Public.Role.AjancyPartner, MembershipDate = DateTime.Now };
                    userRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                    user.UsersInRoles.Add(userRole);
                    prsn.User = user;
                    db.Persons.InsertOnSubmit(prsn);
                }
                else
                {
                    userRole = prsn.User.UsersInRoles.SingleOrDefault<Ajancy.UsersInRole>(ur => ur.RoleID == (short)Public.Role.AjancyPartner);
                    if (userRole == null)
                    {
                        userRole = new Ajancy.UsersInRole { RoleID = (short)Public.Role.AjancyPartner, MembershipDate = DateTime.Now };
                        userRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                        prsn.User.UsersInRoles.Add(userRole);
                    }
                    else if (!userRole.AjancyPartners.Any<Ajancy.AjancyPartner>(jp => jp.AjancyID == ajancy.AjancyID))
                    {
                        userRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                    }
                }
            }
            if (!string.IsNullOrEmpty(this.txtPartnerName2.Text) && !string.IsNullOrEmpty(this.txtPartnerFamily2.Text) && !string.IsNullOrEmpty(this.txtPartnerNationalCode2.Text))
            {
                Ajancy.Person prsn = db.Persons.FirstOrDefault<Ajancy.Person>(p => p.NationalCode == this.txtPartnerNationalCode2.Text);
                Ajancy.UsersInRole userRole = null;
                if (prsn == null)
                {
                    prsn = new Ajancy.Person { NationalCode = this.txtPartnerNationalCode2.Text, FirstName = this.txtPartnerName2.Text, LastName = this.txtPartnerFamily2.Text, SubmitDate = DateTime.Now };
                    Ajancy.User user = new Ajancy.User();
                    user.UserName = this.txtPartnerNationalCode2.Text;
                    user.ProvinceID = Public.ToByte(this.drpProvince.SelectedValue);
                    user.CityID = Public.ToShort(this.drpCity.SelectedValue);
                    user.SubmitDate = DateTime.Now;
                    userRole = new Ajancy.UsersInRole { RoleID = (short)Public.Role.AjancyPartner, MembershipDate = DateTime.Now };
                    userRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                    user.UsersInRoles.Add(userRole);
                    prsn.User = user;
                    db.Persons.InsertOnSubmit(prsn);
                }
                else
                {
                    userRole = prsn.User.UsersInRoles.SingleOrDefault<Ajancy.UsersInRole>(ur => ur.RoleID == (short)Public.Role.AjancyPartner);
                    if (userRole == null)
                    {
                        userRole = new Ajancy.UsersInRole { RoleID = (short)Public.Role.AjancyPartner, MembershipDate = DateTime.Now };
                        userRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                        prsn.User.UsersInRoles.Add(userRole);
                    }
                    else if (!userRole.AjancyPartners.Any<Ajancy.AjancyPartner>(jp => jp.AjancyID == ajancy.AjancyID))
                    {
                        userRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                    }
                }
            }
            if (!string.IsNullOrEmpty(this.txtPartnerName3.Text) && !string.IsNullOrEmpty(this.txtPartnerFamily3.Text) && !string.IsNullOrEmpty(this.txtPartnerNationalCode3.Text))
            {
                Ajancy.Person prsn = db.Persons.FirstOrDefault<Ajancy.Person>(p => p.NationalCode == this.txtPartnerNationalCode3.Text);
                Ajancy.UsersInRole userRole = null;
                if (prsn == null)
                {
                    prsn = new Ajancy.Person { NationalCode = this.txtPartnerNationalCode3.Text, FirstName = this.txtPartnerName3.Text, LastName = this.txtPartnerFamily3.Text, SubmitDate = DateTime.Now };
                    Ajancy.User user = new Ajancy.User();
                    user.UserName = this.txtPartnerNationalCode3.Text;
                    user.ProvinceID = Public.ToByte(this.drpProvince.SelectedValue);
                    user.CityID = Public.ToShort(this.drpCity.SelectedValue);
                    user.SubmitDate = DateTime.Now;
                    userRole = new Ajancy.UsersInRole { RoleID = (short)Public.Role.AjancyPartner, MembershipDate = DateTime.Now };
                    userRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                    user.UsersInRoles.Add(userRole);
                    prsn.User = user;
                    db.Persons.InsertOnSubmit(prsn);
                }
                else
                {
                    userRole = prsn.User.UsersInRoles.SingleOrDefault<Ajancy.UsersInRole>(ur => ur.RoleID == (short)Public.Role.AjancyPartner);
                    if (userRole == null)
                    {
                        userRole = new Ajancy.UsersInRole { RoleID = (short)Public.Role.AjancyPartner, MembershipDate = DateTime.Now };
                        userRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                        prsn.User.UsersInRoles.Add(userRole);
                    }
                    else if (!userRole.AjancyPartners.Any<Ajancy.AjancyPartner>(jp => jp.AjancyID == ajancy.AjancyID))
                    {
                        userRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                    }
                }
            }

            #endregion

            string url = null;
            try
            {
                db.SubmitChanges();
                url = string.Format("~/Message.aspx?mode=1&c={0}", null);
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("IX_Ajancies")) // Duplicate BusinessLicense Request for one place
                {
                    url = "~/Message.aspx?mode=3";
                }
                else if (ex.Message.Contains("Native"))
                {
                    url = "~/Message.aspx?mode=1";
                }
            }
            DisposeContext();
            Response.Redirect(url);
        }
    }
Exemple #9
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");
        }
    }
Exemple #10
0
 partial void InsertUser(User instance);
    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");
    }
Exemple #12
0
    private void LogIn(Ajancy.UsersInRole userInRole, string[] roleParts)
    {
        Ajancy.User user = new Ajancy.User
        {
            UserID = userInRole.UserID,
            CityID = userInRole.User.Person.User.CityID.GetValueOrDefault(),
            ProvinceID = userInRole.User.Person.User.ProvinceID,
            Person = new Ajancy.Person { PersonID = userInRole.User.PersonID, FirstName = userInRole.User.Person.FirstName, LastName = userInRole.User.Person.LastName },
            City = new Ajancy.City { CityID = userInRole.User.Person.User.CityID.GetValueOrDefault(), ProvinceID = userInRole.User.Person.User.ProvinceID, Name = userInRole.User.Person.User.City.Name },
            Province = new Ajancy.Province { ProvinceID = userInRole.User.Person.User.ProvinceID, Name = userInRole.User.Person.User.Province.Name }
        };

        this.DisposeContext();
        this.Session["UserRole"] = new Ajancy.UsersInRole { UserRoleID = userInRole.UserRoleID, RoleID = userInRole.RoleID, User = user };
        if (userInRole.RoleID == (short)Public.Role.AjancyManager)
        {
            this.Session["Ajancy"] = new Ajancy.Ajancy { AjancyID = int.Parse(roleParts[1]), AjancyType = byte.Parse(roleParts[2]), AjancyName = roleParts[3] };
        }
        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, this.txtUserName.Text, DateTime.Now, DateTime.Now.AddMinutes(30), true, userInRole.RoleID.ToString());
        Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket)));
        Response.Redirect(FormsAuthentication.GetRedirectUrl(this.txtUserName.Text, true));
    }
    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");
        }
    }
Exemple #14
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");
        }
    }
Exemple #15
0
 partial void DeleteUser(User instance);
Exemple #16
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 (this.Page.IsValid)
        {
            int personId = 0;
            int ajancyId = 0;
            Ajancy.Person person = new Ajancy.Person();
            Ajancy.UsersInRole usersInRole = null;
            Ajancy.Ajancy ajancy = new Ajancy.Ajancy();
            Ajancy.BusinessLicense businessLicense = new Ajancy.BusinessLicense();
            db = new Ajancy.Kimia_Ajancy(Public.ConnectionString);

            if (this.ViewState["PersonID"] == null) // Add mode
            {
                Ajancy.User user = new Ajancy.User();
                user.UserName = this.txtNationalCode.Text;
                user.PassWord = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.txtNationalCode.Text.Trim(), "SHA1");
                user.ProvinceID = Public.ToByte(this.drpProvince.SelectedValue);
                user.CityID = Public.ToShort(this.drpCity.SelectedValue);
                user.SubmitDate = DateTime.Now;
                usersInRole = new Ajancy.UsersInRole { RoleID = (short)Public.Role.AjancyManager, MembershipDate = DateTime.Now };
                usersInRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                user.UsersInRoles.Add(usersInRole);
                person.SubmitDate = DateTime.Now;
                person.User = user;
                db.Persons.InsertOnSubmit(person);
            }
            else if (int.TryParse(this.ViewState["PersonID"].ToString(), out personId)) // Edit person mode
            {
                dlo = new DataLoadOptions();
                dlo.LoadWith<Ajancy.Person>(p => p.User);
                dlo.LoadWith<Ajancy.User>(u => u.UsersInRoles);
                db.LoadOptions = dlo;

                person = db.Persons.First<Ajancy.Person>(p => p.PersonID == personId);
                usersInRole = person.User.UsersInRoles.SingleOrDefault<Ajancy.UsersInRole>(ur => ur.RoleID == (short)Public.Role.AjancyManager);
                if (usersInRole == null)
                {
                    usersInRole = new Ajancy.UsersInRole { RoleID = (short)Public.Role.AjancyManager, MembershipDate = DateTime.Now };
                    person.User.UsersInRoles.Add(usersInRole);
                }

                if (this.ViewState["AjancyID"] == null)
                {
                    usersInRole.AjancyPartners.Add(new Ajancy.AjancyPartner { Ajancy = ajancy, SubmitDate = DateTime.Now });
                }
            }

            if (this.ViewState["AjancyID"] == null)
            {
                if (db.Ajancies.Any<Ajancy.Ajancy>(j => j.CityID == short.Parse(this.drpCity.SelectedValue) &&
                                                                                j.AjancyType != (byte)Public.AjancyType.Academy &&
                                                                                j.AjancyName.Equals(this.txtAjancyName.Text.Trim())))
                {
                    this.lblMessage.Text = string.Format("آژانسی با نام <b>{0}</b> در شهرستان <b>{1}</b> موجود میباشد", ajancy.AjancyName, this.drpCity.SelectedItem.Text);
                    return;
                }

                var buslic = from j in db.Ajancies
                             join bl in db.BusinessLicenses on j.AjancyID equals bl.AjancyID
                             where bl.BusinessLicenseNo == this.txtBusinessLicenseNo.Text.Trim() &&
                                        j.CityID == Public.ToShort(this.drpCity.SelectedValue)
                             select new { j.AjancyID };
                foreach (var item in buslic)
                {
                    this.lblMessage.Text = string.Format("آژانسی با شماره پروانه کسب <b>{0}</b> در شهرستان <b>{1}</b> موجود میباشد", this.txtBusinessLicenseNo.Text, this.drpCity.SelectedItem.Text);
                    return;
                }

                businessLicense.SubmitDate = DateTime.Now;
                ajancy.BusinessLicenses.Add(businessLicense);
            }
            else if (int.TryParse(this.ViewState["AjancyID"].ToString(), out ajancyId)) // Edit ajancy mode
            {
                if (db.Ajancies.Any<Ajancy.Ajancy>(j => j.CityID == int.Parse(this.drpCity.SelectedValue) && j.AjancyID != ajancyId && j.AjancyName.Equals(this.txtAjancyName.Text.Trim())))
                {
                    this.lblMessage.Text = string.Format("آژانسی با نام <b>{0}</b> در شهرستان <b>{1}</b> موجود میباشد", this.txtAjancyName.Text, this.drpCity.SelectedItem.Text);
                    return;
                }

                var buslic = from j in db.Ajancies
                             join bl in db.BusinessLicenses on j.AjancyID equals bl.AjancyID
                             where bl.BusinessLicenseNo == this.txtBusinessLicenseNo.Text.Trim() &&
                                        j.CityID == Public.ToShort(this.drpCity.SelectedValue) &&
                                        j.AjancyID != ajancyId
                             select new { j.AjancyID };
                foreach (var item in buslic)
                {
                    this.lblMessage.Text = string.Format("آژانسی با شماره پروانه کسب <b>{0}</b> در شهرستان <b>{1}</b> موجود میباشد", this.txtBusinessLicenseNo.Text, this.drpCity.SelectedItem.Text);
                    return;
                }

                ajancy = db.Ajancies.FirstOrDefault<Ajancy.Ajancy>(j => j.AjancyID == ajancyId);
                businessLicense = ajancy.BusinessLicenses.First<Ajancy.BusinessLicense>();
            }

            person.NationalCode = this.txtNationalCode.Text.Trim();
            person.FirstName = this.txtFirstName.Text.Trim();
            person.LastName = this.txtLastName.Text.Trim();
            person.Mobile = this.txtMobile.Text.Trim();
            person.Gender = Public.ToByte(this.drpGender.SelectedValue);

            ajancy.SubmitDate = DateTime.Now;
            ajancy.AjancyType = Public.ToByte(this.drpAjancyType.SelectedValue);
            ajancy.AjancyName = this.txtAjancyName.Text.Trim();
            ajancy.CityID = Public.ToShort(this.drpCity.SelectedValue);
            ajancy.BusinessLicenseType = Public.ToByte(this.drpBusinessLicenseType.SelectedValue);
            ajancy.PostalCode = this.txtPlacePostalCode.Text.Trim();
            ajancy.Address = this.txtPlaceAddress.Text.Trim();
            ajancy.Phone = this.txtBusinessPlacePhone.Text.Trim();
            businessLicense.BusinessLicenseNo = this.txtBusinessLicenseNo.Text.Trim();

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