private void btnSave_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         using (CRM_magEntities contextDB = new CRM_magEntities())
         {
             foreach (var item in unsd)
             {
                 int idRA = Convert.ToInt32(contextDB.Request_reception.Find(item.NumberRequest).id_request_execution);
                 Request_attachment ra = contextDB.Request_attachment.Find(idRA);
                 ra.id_mont = item.IdMont;
             }
             contextDB.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString());
     }
 }
        private void btnOKFizL_Click(object sender, RoutedEventArgs e)
        {
            if (IsErrorFizLico())
            {
                MessageBox.Show(sErrorFizLico, "Ошибка!");
            }
            else
            {
                try
                {
                    using (CRM_magEntities contextDB = new CRM_magEntities())
                    {
                        Client cl = new Client();

                        cl.famil       = txtbFamilCl.Text;
                        cl.f_name      = txtbNameCl.Text;
                        cl.s_name      = txtbSurnameCl.Text;
                        cl.number_pasp = txtbPasp.Text;
                        cl.tel         = txtbTel.Text;
                        contextDB.Client.Add(cl);
                        contextDB.SaveChanges();
                        idClient = cl.id_client;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }


            if (!IsErrorFizLico())
            {
                try
                {
                    using (CRM_magEntities contextDB = new CRM_magEntities())
                    {
                        Adr_installation adr = new Adr_installation();
                        if (rbtnMoscow.IsChecked == true)
                        {
                            adr.city_town = "Москва";
                        }
                        else if (rbtnMO.IsChecked == true)
                        {
                            adr.city_town = txtbCityTown.Text;
                        }
                        adr.raion = txtbRajon.Text;
                        if (txtbStreet.Text != null)
                        {
                            adr.street = txtbStreet.Text;
                        }
                        adr.house = Convert.ToInt32(txtbHouse.Text);
                        adr.korp  = txtbKorp.Text;
                        adr.flat  = Convert.ToInt32(txtbFlat.Text);
                        if (cmbMetro.SelectedValue != null)
                        {
                            adr.id_metro = Convert.ToInt32(cmbMetro.SelectedValue);
                        }

                        adr.podezd    = Convert.ToInt32(txtbPodezd.Text);
                        adr.etag      = Convert.ToInt32(txtbEtag.Text);
                        adr.domofon   = txtbDomofon.Text;
                        adr.id_client = idClient;
                        contextDB.Adr_installation.Add(adr);
                        contextDB.SaveChanges();
                        idAdrInst = adr.id_adr_installation;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }


            if (!IsErrorFizLico())
            {
                try
                {
                    using (CRM_magEntities contextDB = new CRM_magEntities())
                    {
                        Request_snab rs = new Request_snab();
                        if (cmbMarkaGor.SelectedValue != null)
                        {
                            rs.id_gor_schetch = Convert.ToInt32(cmbMarkaGor.SelectedValue);
                        }
                        if (cmbMarkaHol.SelectedValue != null)
                        {
                            rs.id_hol_schetch = Convert.ToInt32(cmbMarkaHol.SelectedValue);
                        }
                        if (cmbMarkaTeplo.SelectedValue != null)
                        {
                            rs.id_teplo_schetch = Convert.ToInt32(cmbMarkaTeplo.SelectedValue);
                        }
                        int  numbKolGor;
                        bool resultKolGor = int.TryParse(txtbKolGor.Text, out numbKolGor);
                        if (resultKolGor == true)
                        {
                            rs.kol_gor = numbKolGor;
                        }

                        int  numbKolHol;
                        bool resultKolHol = int.TryParse(txtbKolHol.Text, out numbKolHol);
                        if (resultKolHol == true)
                        {
                            rs.kol_hol = numbKolHol;
                        }

                        int  numbKolTeplo;
                        bool resultKolTeplo = int.TryParse(txtbKolTeplo.Text, out numbKolTeplo);
                        if (resultKolTeplo == true)
                        {
                            rs.kol_teplo = numbKolTeplo;
                        }

                        contextDB.Request_snab.Add(rs);
                        contextDB.SaveChanges();

                        idReqSnab = rs.id_request_snab;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }


            if (!IsErrorFizLico())
            {
                try
                {
                    using (CRM_magEntities contextDB = new CRM_magEntities())
                    {
                        Request_attachment ra = new Request_attachment();
                        ra.datetime_vyezda_montazhnika = Convert
                                                         .ToDateTime(txtbVyezd.Text);

                        if (cmbMont.SelectedValue != null)
                        {
                            ra.id_mont = Convert.ToInt32(cmbMont.SelectedValue);
                        }

                        contextDB.Request_attachment.Add(ra);
                        contextDB.SaveChanges();

                        idReqAtt = ra.id_request_execution;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }



            if (!IsErrorFizLico())
            {
                try
                {
                    using (CRM_magEntities contextDB = new CRM_magEntities())
                    {
                        Request_buhg rb = new Request_buhg();
                        decimal      sum;
                        bool         resultSum = decimal.TryParse(txtbSum.Text, out sum);
                        if (resultSum == true)
                        {
                            rb.sum_contract = sum;
                        }

                        contextDB.Request_buhg.Add(rb);
                        contextDB.SaveChanges();
                        idReqBuhg = rb.id_request_buhg;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }

            if (!IsErrorFizLico())
            {
                try
                {
                    using (CRM_magEntities contextDB = new CRM_magEntities())
                    {
                        Request_reception rr = new Request_reception();
                        rr.id_request_snab      = idReqSnab;
                        rr.id_request_buhg      = idReqBuhg;
                        rr.id_request_execution = idReqAtt;
                        rr.id_adr_installation  = idAdrInst;
                        rr.id_status            = 1;
                        rr.id_kind_of_work      = Convert.ToInt32(cmbKindOfWork.SelectedValue);
                        if (idPerenosReq != 0)
                        {
                            rr.id_perenos_request = idPerenosReq;
                        }
                        rr.dispetcher   = txtbDispR.Text;
                        rr.date_request = Convert.ToDateTime(txtbDateReg.Text);

                        contextDB.Request_reception.Add(rr);
                        contextDB.SaveChanges();
                        txtbNumberRequest.Text = rr.id_request.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                if (evNewClient != null)
                {
                    evNewClient();
                }
            }
        }