private void bBinding_Click(object sender, System.EventArgs e)
 {
     ObjectList<ServiceOld> selectedServices = this.serviceBenefitSearchView1.SelectedServices;
     ObjectList<LocalAddress> selectedAddresses = ((OperationGroupForm) base.Parent.Parent.Parent.Parent.Parent).GetSelectedAddresses();
     Benefit selectedBenefit = this.serviceBenefitSearchView1.SelectedBenefit;
     ApartmentResidentBenefit benefit2 = new ApartmentResidentBenefit();
     int num = 0;
     ObjectList<LocalAddress> list3 = new ObjectList<LocalAddress>();
     if (selectedBenefit != null)
     {
         foreach (LocalAddress address in selectedAddresses)
         {
             if (address.Level == AddressLevel.MaxAddress)
             {
                 list3.Add(address);
             }
             if (address.Level == AddressLevel.Дом)
             {
                 if (Mappers.LocalAddressMapper.GetChildren(address).get_Count() != 0)
                 {
                     foreach (LocalAddress address2 in Mappers.LocalAddressMapper.GetChildren(address))
                     {
                         list3.Add(address2);
                     }
                 }
                 else
                 {
                     list3.Add(address);
                 }
             }
             if (address.Level == AddressLevel.Улица)
             {
                 ObjectList<LocalAddress> list4 = new ObjectList<LocalAddress>();
                 foreach (LocalAddress address3 in address.GetChildren())
                 {
                     if (Mappers.LocalAddressMapper.GetChildren(address3).get_Count() != 0)
                     {
                         foreach (LocalAddress address4 in Mappers.LocalAddressMapper.GetChildren(address3))
                         {
                             list3.Add(address4);
                         }
                     }
                     else
                     {
                         list3.Add(address3);
                     }
                 }
             }
             if (address.Level == AddressLevel.MinAddress)
             {
                 ObjectList<LocalAddress> list5 = new ObjectList<LocalAddress>();
                 foreach (LocalAddress address5 in address.GetChildren())
                 {
                     foreach (LocalAddress address6 in address5.GetChildren())
                     {
                         if (Mappers.LocalAddressMapper.GetChildren(address6).get_Count() != 0)
                         {
                             foreach (LocalAddress address7 in Mappers.LocalAddressMapper.GetChildren(address6))
                             {
                                 list3.Add(address7);
                             }
                         }
                         else
                         {
                             list3.Add(address6);
                         }
                     }
                 }
             }
         }
         foreach (LocalAddress address8 in list3)
         {
             if (Mappers.ApartmentMapper.FindByAdrId(address8.Id).get_Count() == 0)
             {
                 return;
             }
             benefit2.ApartmentId = Mappers.ApartmentMapper.FindByAdrId(address8.Id).get_Item(0).Id;
             benefit2.BenefitId = selectedBenefit.Id;
             foreach (ServiceOld old in selectedServices)
             {
                 benefit2.ServiceTypeId = old.ServiceTypeId;
                 if (this.dbxOpenDate.get_MaskFull())
                 {
                     benefit2.FromDate = System.Convert.ToDateTime(this.dbxOpenDate.get_Text());
                     benefit2.ToDate = this.dbxCloseDate.get_MaskFull() ? System.Convert.ToDateTime(this.dbxCloseDate.get_Text()) : System.Convert.ToDateTime((string) null);
                     foreach (PersonBenefit benefit3 in Mappers.PersonBenefitMapper.FindByAllParametrs(old.ServiceTypeId, benefit2.ApartmentId, selectedBenefit.Id, benefit2.FromDate, benefit2.ToDate))
                     {
                         benefit2.PersonId = benefit3.PersonId;
                         benefit2.PersonBenefitId = benefit3.Id;
                         benefit2.Created = System.DateTime.Now;
                         num = (int) (num + 1);
                         benefit2.Insert();
                     }
                 }
                 else
                 {
                     BalloonWindow.Show(this.dbxOpenDate, "Не была введена дата начала для привязки");
                 }
             }
         }
     }
     else
     {
         BalloonWindow.Show(this.bBinding, "Выберите наименование льготы для привязки");
     }
     BalloonWindow.Show(this.bBinding, "Привязано " + ((int) num) + " записей");
     num = (int) (num + 1);
 }
 private void btnSave_Click(object sender, System.EventArgs e)
 {
     if (this.dbxBegin.IsNull)
     {
         new BalloonWindow("Введите дату начала").Show(this.btnSave);
     }
     else if ((this.ap.ToDate != Constants.NullDate) && (this.dbxEnd.IsNull || (!this.dbxEnd.IsNull && (this.dbxEnd.Value > this.ap.ToDate))))
     {
         new BalloonWindow("Дата окончания не должна быть больше даты выписки проживающего").Show(this.btnSave);
     }
     else if (!this.dbxEnd.IsNull && (this.dbxEnd.Value < this.dbxBegin.Value))
     {
         new BalloonWindow("Дата начала должна быть меньше даты окончания").Show(this.btnSave);
     }
     else
     {
         ObjectList<ApartmentResidentBenefitsServiceTypesView> selectRowsGrid = this.GetSelectRowsGrid();
         if (selectRowsGrid == null)
         {
             new BalloonWindow("Выберите типы услуг").Show(this.btnSave);
         }
         else
         {
             ApartmentPersonBenefitsView apartmentPersonBenefitsView = this.GetApartmentPersonBenefitsView();
             if (apartmentPersonBenefitsView != ApartmentPersonBenefitsView.Null)
             {
                 PersonBenefit benefit = PersonBenefit.FindByPersonIdBenefitId(apartmentPersonBenefitsView.PersonId, apartmentPersonBenefitsView.BenefitId);
                 if ((benefit != PersonBenefit.Null) && this.Verify(benefit.Id))
                 {
                     foreach (ApartmentResidentBenefitsServiceTypesView view2 in selectRowsGrid)
                     {
                         ApartmentResidentBenefit benefit2 = new ApartmentResidentBenefit {
                             ApartmentId = apartmentPersonBenefitsView.ApartmentId,
                             PersonId = this.ap.PersonId,
                             PersonBenefitId = benefit.Id,
                             ServiceTypeId = view2.ServiceTypeId,
                             FromDate = this.dbxBegin.Value
                         };
                         if (!this.dbxEnd.IsNull)
                         {
                             benefit2.ToDate = this.dbxEnd.Value;
                         }
                         benefit2.SaveChanges();
                     }
                     base.Close();
                 }
             }
         }
     }
 }
 private void bUnBinding_Click(object sender, System.EventArgs e)
 {
     ObjectList<ServiceOld> selectedServices = this.serviceBenefitSearchView1.SelectedServices;
     ObjectList<LocalAddress> selectedAddresses = ((OperationGroupForm) base.Parent.Parent.Parent.Parent.Parent).GetSelectedAddresses();
     Benefit selectedBenefit = this.serviceBenefitSearchView1.SelectedBenefit;
     ApartmentResidentBenefit benefit2 = new ApartmentResidentBenefit();
     ObjectList<LocalAddress> list3 = new ObjectList<LocalAddress>();
     int num = 0;
     if (selectedBenefit != null)
     {
         foreach (LocalAddress address in selectedAddresses)
         {
             if (address.Level == AddressLevel.MaxAddress)
             {
                 list3.Add(address);
             }
             if (address.Level == AddressLevel.Дом)
             {
                 if (Mappers.LocalAddressMapper.GetChildren(address).get_Count() != 0)
                 {
                     foreach (LocalAddress address2 in Mappers.LocalAddressMapper.GetChildren(address))
                     {
                         list3.Add(address2);
                     }
                 }
                 else
                 {
                     list3.Add(address);
                 }
             }
             if (address.Level == AddressLevel.Улица)
             {
                 ObjectList<LocalAddress> list4 = new ObjectList<LocalAddress>();
                 foreach (LocalAddress address3 in address.GetChildren())
                 {
                     if (Mappers.LocalAddressMapper.GetChildren(address3).get_Count() != 0)
                     {
                         foreach (LocalAddress address4 in Mappers.LocalAddressMapper.GetChildren(address3))
                         {
                             list3.Add(address4);
                         }
                     }
                     else
                     {
                         list3.Add(address3);
                     }
                 }
             }
             if (address.Level == AddressLevel.MinAddress)
             {
                 ObjectList<LocalAddress> list5 = new ObjectList<LocalAddress>();
                 foreach (LocalAddress address5 in address.GetChildren())
                 {
                     foreach (LocalAddress address6 in address5.GetChildren())
                     {
                         if (Mappers.LocalAddressMapper.GetChildren(address6).get_Count() != 0)
                         {
                             foreach (LocalAddress address7 in Mappers.LocalAddressMapper.GetChildren(address6))
                             {
                                 list3.Add(address7);
                             }
                         }
                         else
                         {
                             list3.Add(address6);
                         }
                     }
                 }
             }
         }
         foreach (LocalAddress address8 in list3)
         {
             if (Mappers.ApartmentMapper.FindByAdrId(address8.Id).get_Count() == 0)
             {
                 return;
             }
             benefit2.ApartmentId = Mappers.ApartmentMapper.FindByAdrId(address8.Id).get_Item(0).Id;
             benefit2.BenefitId = selectedBenefit.Id;
             foreach (ServiceOld old in selectedServices)
             {
                 benefit2.ServiceTypeId = old.ServiceTypeId;
                 foreach (ApartmentResidentBenefit benefit3 in Mappers.ApartmentResidentBenefitMapper.FindByAllParametrs(old.ServiceTypeId, benefit2.ApartmentId, selectedBenefit.Id, false))
                 {
                     benefit3.Delete();
                     num = (int) (num + 1);
                 }
             }
         }
     }
     else
     {
         BalloonWindow.Show(this.bBinding, "Выберите наименование льготы для привязки");
     }
     BalloonWindow.Show(this.bUnBinding, "Отвязано " + ((int) num) + " записей");
 }