partial void UpdateDangKyHoatDong(DangKyHoatDong instance);
 partial void DeleteDangKyHoatDong(DangKyHoatDong instance);
		private void detach_DangKyHoatDongs(DangKyHoatDong entity)
		{
			this.SendPropertyChanging();
			entity.HoSoGiayPhep = null;
		}
 partial void InsertDangKyHoatDong(DangKyHoatDong instance);
		private void attach_DangKyHoatDongs(DangKyHoatDong entity)
		{
			this.SendPropertyChanging();
			entity.HoatDong = this;
		}
        public ActionResult BoSungGiayPhep(ToChucBoSungViewModel model, int save)
        {
            TaiKhoan tk = ((EnhancedPrincipal)HttpContext.User).Data;
            int msg = 0;

            if (save == 1)
            {
                //using (TransactionScope scope = new TransactionScope())
                {
                    try
                    {
                        ThongTinChung ttc = new ThongTinChung();
                        //using (TransactionScope s1 = new TransactionScope())
                        {
                            ttc.TenToChuc = model.TenToChuc;
                            ttc.GiayPhepKinhDoanh = model.GiayPhepKinhDoanh;
                            ttc.HangDoanhNghiep = model.HangDoanhNghiep;
                            ttc.VonLuuDong = model.VonLuuDong;
                            ttc.VonPhapDinh = model.VonPhapDinh;
                            ttc.SoTaiKhoan = model.SoTaiKhoan;
                            ttc.TongSoCanBo = model.TongSoCanBo;
                            ttc.MaLoaiHinhToChuc = model.MaLoaiHinhToChuc;
                            ttc.TruSoChinh = model.TruSoChinh;
                            ttc.NguoiDaiDien = model.NguoiDaiDien;
                            ttc.DienThoai = model.DienThoai;
                            ttc.Email = model.Email;
                            ttc.Fax = model.Fax;
                            _ttcRespository.Add(ttc);

                            // Luu vao to chuc neu chua co hoac update
                            //ToChuc t = _tochucRespository.GetToChucByID(model.MaToChuc);
                            List<ToChuc> tcs = tk.ToChucs.ToList();
                            if (tcs.Count == 0)
                            {
                                ToChuc tc1 = new ToChuc();
                                tc1.TenToChuc = model.TenToChuc;
                                tc1.GiayPhepKinhDoanh = model.GiayPhepKinhDoanh;
                                tc1.HangDoanhNghiep = model.HangDoanhNghiep;
                                tc1.VonLuuDong = model.VonLuuDong;
                                tc1.VonPhapDinh = model.VonPhapDinh;
                                tc1.SoTaiKhoan = model.SoTaiKhoan;
                                tc1.TongSoCanBo = model.TongSoCanBo;
                                tc1.MaLoaiHinhToChuc = model.MaLoaiHinhToChuc;
                                tc1.TruSoChinh = model.TruSoChinh;
                                tc1.NguoiDaiDien = model.NguoiDaiDien;
                                tc1.DienThoai = model.DienThoai;
                                tc1.Email = model.Email;
                                tc1.Fax = model.Fax;
                                tc1.MaTaiKhoan = tk.MaTaiKhoan;
                                _tochucRespository.Add(tc1);
                            }
                            else
                            {
                                ToChuc t = tcs[0];
                                t.TenToChuc = model.TenToChuc;
                                t.GiayPhepKinhDoanh = model.GiayPhepKinhDoanh;
                                t.HangDoanhNghiep = model.HangDoanhNghiep;
                                t.VonLuuDong = model.VonLuuDong;
                                t.VonPhapDinh = model.VonPhapDinh;
                                t.SoTaiKhoan = model.SoTaiKhoan;
                                t.TongSoCanBo = model.TongSoCanBo;
                                t.MaLoaiHinhToChuc = model.MaLoaiHinhToChuc;
                                t.TruSoChinh = model.TruSoChinh;
                                t.NguoiDaiDien = model.NguoiDaiDien;
                                t.DienThoai = model.DienThoai;
                                t.Email = model.Email;
                                t.Fax = model.Fax;
                                _tochucRespository.Update(t);
                            }
                           // s1.Complete();
                        }
                        // xoa nhung nangluckekhai co MaToChuc va them vao nangluckekhai co MaToChuc moi
                        // them những nangluckekhai co MaThongTinChung

                        ToChuc tochuc = _tochucRespository.GetToChucByTaiKhoan(tk.MaTaiKhoan);
                        List<NangLucKeKhai> listNangLucCu = _nanglucRespository.GetNangLucByTCID(tochuc.MaToChuc);
                        foreach (var nlCu in listNangLucCu)
                        {
                            _nanglucRespository.Delete(nlCu);
                        }
                        IEnumerable<NangLucVM> listNangLuc = model.NangLucRow;
                        if (listNangLuc != null)
                        {
                            foreach (var i in listNangLuc)
                            {
                                if (i.NganhNghe != "" && i.NganhNghe != null)
                                {
                                    NangLucKeKhai nl = new NangLucKeKhai();
                                    nl.MaToChuc = tochuc.MaToChuc;
                                    nl.NganhNghe = i.NganhNghe;
                                    nl.SoTrenDaiHoc = i.Daihoc.GetValueOrDefault(0);
                                    nl.SoTrungCap = i.TrungCap.GetValueOrDefault(0);
                                    nl.SoCongNhanKyThuat = i.CongNhan.GetValueOrDefault(0);
                                    nl.LoaiKhac = i.LoaiKhac.GetValueOrDefault(0);
                                    _nanglucRespository.Add(nl);
                                    NangLucKeKhai nl1 = new NangLucKeKhai();
                                    nl1.MaThongTinChung = ttc.MaThongTinChung;
                                    nl1.NganhNghe = i.NganhNghe;
                                    nl1.SoTrenDaiHoc = i.Daihoc.GetValueOrDefault(0);
                                    nl1.SoTrungCap = i.TrungCap.GetValueOrDefault(0);
                                    nl1.SoCongNhanKyThuat = i.CongNhan.GetValueOrDefault(0);
                                    nl1.LoaiKhac = i.LoaiKhac.GetValueOrDefault(0);
                                    _nanglucRespository.Add(nl1);
                                }
                            }
                        }
                        // xoa nhung ThietBi co MaToChuc va them vao ThietBi co MaToChuc moi
                        // them những ThietBi co MaThongTinChung
                        List<ThietBi> listThietBiCu = _thietbiRespository.GetThietBiByTCID(tochuc.MaToChuc);
                        foreach (var tbCu in listThietBiCu)
                        {
                            _thietbiRespository.Delete(tbCu);
                        }
                        IEnumerable<ThietBiVM> listThietBi = model.ThietBiRow;
                        if (listThietBi != null)
                        {
                            foreach (var i in listThietBi)
                            {
                                if (i.TenThietBi_CongNghe != "" && i.TenThietBi_CongNghe != null)
                                {
                                    ThietBi tb = new ThietBi();
                                    tb.MaToChuc = tochuc.MaToChuc;
                                    tb.TenThietBi_CongNghe = i.TenThietBi_CongNghe;
                                    tb.SoLuong = i.SoLuong.GetValueOrDefault(0);
                                    tb.TinhTrang = i.TinhTrang;
                                    tb.GhiChu = i.GhiChu;
                                    _thietbiRespository.Add(tb);
                                    ThietBi tb1 = new ThietBi();
                                    tb1.MaThongTinChung = ttc.MaThongTinChung;
                                    tb1.TenThietBi_CongNghe = i.TenThietBi_CongNghe;
                                    tb1.SoLuong = i.SoLuong.GetValueOrDefault(0);
                                    tb1.TinhTrang = i.TinhTrang;
                                    tb1.GhiChu = i.GhiChu;
                                    _thietbiRespository.Add(tb1);
                                }
                            }
                        }

                        // xoa nhung ThietBi co MaToChuc va them vao ThietBi co MaToChuc moi
                        // them những ThietBi co MaThongTinChung

                        List<NhanLuc> listNhanLucCu = _nhanlucRespository.GetNhanLucByTCID(tochuc.MaToChuc);
                        foreach (var nlCu in listNhanLucCu)
                        {
                            _nhanlucRespository.Delete(nlCu);
                        }
                        IEnumerable<NhanLucVM> listNhanLuc = model.NhanLucRow;
                        if (listNhanLuc != null)
                        {
                            foreach (var i in listNhanLuc)
                            {
                                if (i.HoTen != "" && i.HoTen != null)
                                {
                                    NhanLuc nl = new NhanLuc();
                                    nl.MaToChuc = tochuc.MaToChuc;
                                    nl.HoTen = i.HoTen;
                                    nl.ChucVu = i.ChucVu;
                                    nl.TrinhDoHocVan = i.TrinhDoHocVan;
                                    nl.ThamNien = i.ThamNien;
                                    _nhanlucRespository.Add(nl);
                                    NhanLuc nl1 = new NhanLuc();
                                    nl1.MaThongTinChung = ttc.MaThongTinChung;
                                    nl1.HoTen = i.HoTen;
                                    nl1.ChucVu = i.ChucVu;
                                    nl1.TrinhDoHocVan = i.TrinhDoHocVan;
                                    nl1.ThamNien = i.ThamNien;
                                    _nhanlucRespository.Add(nl1);
                                }
                            }
                        }
                        ///xóa báo cáo hoạt động có mã tổ chức
                        ///xóa báo cáo hoạt động có mã báo cáo
                        //thêm báo cáo hoạt động mới(1 cho tổ chức + 1 cho thông tin chung)
                        //thêm những row báo cáo công trình mới

                        BaoCaoHoatDong bchd1 = _bchdRespository.GetBaoCaoHoatDongByTC(tochuc.MaToChuc);
                        // bchd1.MaToChuc = tochuc.MaToChuc;
                        bchd1.TuNam = model.TuNam;
                        bchd1.DenNam = model.DenNam;
                        bchd1.DoanhThu = model.DoanhThuNam.GetValueOrDefault(0);
                        bchd1.NopNganSach = model.NopNganSach.GetValueOrDefault(0);
                        _bchdRespository.Update(bchd1);
                        BaoCaoHoatDong bchd2 = new BaoCaoHoatDong();
                        bchd2.MaThongTinChung = ttc.MaThongTinChung;
                        bchd2.TuNam = model.TuNam;
                        bchd2.DenNam = model.DenNam;
                        bchd2.DoanhThu = model.DoanhThuNam.GetValueOrDefault(0);
                        bchd2.NopNganSach = model.NopNganSach.GetValueOrDefault(0);
                        _bchdRespository.Add(bchd2);
                        List<BaoCaoCongTrinh> congtrinhListCu = _bcctRespository.GetBCCTByBCHD(bchd1.MaBaoCao);
                        foreach (var bc in congtrinhListCu)
                        {
                            _bcctRespository.Delete(bc);
                        }

                        IEnumerable<CongTrinhVM> congtrinhList = model.CongTrinhRow;
                        if (congtrinhList != null)
                        {
                            foreach (var i in congtrinhList)
                            {
                                if (i.TenCongTrinh != "" && i.TenCongTrinh != null)
                                {
                                    BaoCaoCongTrinh bc = new BaoCaoCongTrinh();
                                    bc.MaBaoCao = bchd1.MaBaoCao;
                                    bc.TenCongTrinh = i.TenCongTrinh;
                                    bc.ChuDauTu = i.ChuDauTu;
                                    bc.ThoiGianThucHien = i.ThoiGianThucHien;
                                    bc.GiaTriDaThucHien = i.GiaTriThucHien;
                                    bc.GhiChu = i.GhiChu;
                                    _bcctRespository.Add(bc);
                                    BaoCaoCongTrinh bc1 = new BaoCaoCongTrinh();
                                    bc1.MaBaoCao = bchd2.MaBaoCao;
                                    bc1.TenCongTrinh = i.TenCongTrinh;
                                    bc1.ChuDauTu = i.ChuDauTu;
                                    bc1.ThoiGianThucHien = i.ThoiGianThucHien;
                                    bc1.GiaTriDaThucHien = i.GiaTriThucHien;
                                    bc1.GhiChu = i.GhiChu;
                                    _bcctRespository.Add(bc1);
                                }
                            }
                        }

                        HoSoGiayPhep hs = new HoSoGiayPhep();
                        hs.MaToChuc = tochuc.MaToChuc;
                        hs.SoGiayPhep = model.SoGiayPhep;
                        hs.MaThongTinChung = ttc.MaThongTinChung;
                        hs.NgayXinPhep = DateTime.Today;
                        hs.TinhTrang = 6;
                        hs.LoaiGiayPhep = 2;
                        hs.CamKet = model.Camket;
                        hs.LyDo = model.LyDo;
                        if (Request.Files["tepdinhkem"] != null) // If uploaded synchronously
                        {
                            var tep_guid = _fileStore.SaveUploadedFile(Request.Files["tepdinhkem"]);
                            var tep_filename = Request.Files["tepdinhkem"].FileName;
                            hs.TepDinhKem = tep_guid.ToString();
                        }
                        else
                        {
                            hs.TepDinhKem = Request["tepdinhkem_guid"].ToString();
                        }
                        _hsgpRepository.Add(hs);

                        ///add dang ky hoat dong
                        IList<int> listDK = model.DSHoatDongSelecteds;
                        if (listDK != null)
                        {
                            foreach (var i in listDK)
                            {
                                DangKyHoatDong dk = new DangKyHoatDong();
                                dk.MaHoSo = hs.MaHoSo;
                                dk.MaHoatDong = i;
                                dk.LanBoSung = _dkhdRespository.getSoLanBoSung(tochuc.MaToChuc);
                                _dkhdRespository.Add(dk);
                            }
                        }

                        //scope.Complete();
                        msg = 20;
                    }
                    catch
                    {
                        //MessageHelper.CreateMessage(MessageType.Error, "", new List<string> { "Gửi hồ sơ không thành công" }, HttpContext.Response);
                        msg = 21;
                    }
                }
            }
            else
            {
                try
                {
                   // using (TransactionScope s2 = new TransactionScope())
                    //{
                        List<ToChuc> tcs = tk.ToChucs.ToList();
                        if (tcs.Count == 0)
                        {
                            ToChuc tc = new ToChuc();
                            tc.TenToChuc = model.TenToChuc;
                            tc.GiayPhepKinhDoanh = model.GiayPhepKinhDoanh;
                            tc.HangDoanhNghiep = model.HangDoanhNghiep;
                            tc.VonLuuDong = model.VonLuuDong;
                            tc.VonPhapDinh = model.VonPhapDinh;
                            tc.SoTaiKhoan = model.SoTaiKhoan;
                            tc.TongSoCanBo = model.TongSoCanBo;
                            tc.MaLoaiHinhToChuc = model.MaLoaiHinhToChuc;
                            tc.TruSoChinh = model.TruSoChinh;
                            tc.NguoiDaiDien = model.NguoiDaiDien;
                            tc.DienThoai = model.DienThoai;
                            tc.Email = model.Email;
                            tc.Fax = model.Fax;
                            tc.MaTaiKhoan = tk.MaTaiKhoan;
                            _tochucRespository.Add(tc);
                        }
                        else
                        {
                            ToChuc tc = tcs[0];
                            tc.TenToChuc = model.TenToChuc;
                            tc.GiayPhepKinhDoanh = model.GiayPhepKinhDoanh;
                            tc.HangDoanhNghiep = model.HangDoanhNghiep;
                            tc.VonLuuDong = model.VonLuuDong;
                            tc.VonPhapDinh = model.VonPhapDinh;
                            tc.SoTaiKhoan = model.SoTaiKhoan;
                            tc.TongSoCanBo = model.TongSoCanBo;
                            tc.MaLoaiHinhToChuc = model.MaLoaiHinhToChuc;
                            tc.TruSoChinh = model.TruSoChinh;
                            tc.NguoiDaiDien = model.NguoiDaiDien;
                            tc.DienThoai = model.DienThoai;
                            tc.Email = model.Email;
                            tc.Fax = model.Fax;
                            _tochucRespository.Update(tc);
                        }

                        ToChuc tochuc = _tochucRespository.GetToChucByTaiKhoan(tk.MaTaiKhoan);
                        List<NhanLuc> listNhanLucCu = _nhanlucRespository.GetNhanLucByTCID(tochuc.MaToChuc);
                        foreach (var nlCu in listNhanLucCu)
                        {
                            _nhanlucRespository.Delete(nlCu);
                        }
                        IEnumerable<NhanLucVM> listNhanLuc = model.NhanLucRow;
                        if (listNhanLuc != null)
                        {
                            foreach (var i in listNhanLuc)
                            {
                                NhanLuc nl = new NhanLuc();
                                nl.MaToChuc = tochuc.MaToChuc;
                                nl.HoTen = i.HoTen;
                                nl.ChucVu = i.ChucVu;
                                nl.ThamNien = i.ThamNien;
                                _nhanlucRespository.Add(nl);
                            }
                        }

                        List<NangLucKeKhai> listNangLucCu = _nanglucRespository.GetNangLucByTCID(tochuc.MaToChuc);
                        foreach (var nlCu in listNangLucCu)
                        {
                            _nanglucRespository.Delete(nlCu);
                        }
                        IEnumerable<NangLucVM> listNangLuc = model.NangLucRow;
                        if (listNangLuc != null)
                        {
                            foreach (var i in listNangLuc)
                            {
                                NangLucKeKhai nl = new NangLucKeKhai();
                                nl.MaToChuc = tochuc.MaToChuc;
                                nl.NganhNghe = i.NganhNghe;
                                nl.SoTrenDaiHoc = i.Daihoc.GetValueOrDefault(0);
                                nl.SoTrungCap = i.TrungCap.GetValueOrDefault(0);
                                nl.SoCongNhanKyThuat = i.CongNhan.GetValueOrDefault(0);
                                nl.LoaiKhac = i.LoaiKhac.GetValueOrDefault(0);
                                _nanglucRespository.Add(nl);
                            }
                        }

                        List<ThietBi> listThietBiCu = _thietbiRespository.GetThietBiByTCID(tochuc.MaToChuc);
                        foreach (var tbCu in listThietBiCu)
                        {
                            _thietbiRespository.Delete(tbCu);
                        }
                        IEnumerable<ThietBiVM> listThietBi = model.ThietBiRow;
                        if (listThietBi != null)
                        {
                            foreach (var i in listThietBi)
                            {
                                ThietBi tb = new ThietBi();
                                tb.MaToChuc = tochuc.MaToChuc;
                                tb.TenThietBi_CongNghe = i.TenThietBi_CongNghe;
                                tb.SoLuong = i.SoLuong.GetValueOrDefault(0);
                                tb.TinhTrang = i.TinhTrang;
                                tb.GhiChu = i.GhiChu;
                                _thietbiRespository.Add(tb);
                            }
                        }

                        /// cập nhật báo cáo hoạt động có mã tổ chức, xóa hết báo cáo công trình có mã báo cáo vừa update
                        /// thêm bc công trình với mã báo cáo cũ
                        BaoCaoHoatDong bchd1 = _bchdRespository.GetBaoCaoHoatDongByTC(tochuc.MaToChuc);
                        bchd1.MaToChuc = tochuc.MaToChuc;
                        bchd1.TuNam = model.TuNam;
                        bchd1.DenNam = model.DenNam;
                        bchd1.DoanhThu = model.DoanhThuNam.GetValueOrDefault(0);
                        bchd1.NopNganSach = model.NopNganSach.GetValueOrDefault(0);
                        _bchdRespository.Update(bchd1);

                        List<BaoCaoCongTrinh> congtrinhListCu = _bcctRespository.GetBCCTByBCHD(bchd1.MaBaoCao);
                        foreach (var bc in congtrinhListCu)
                        {
                            _bcctRespository.Delete(bc);
                        }

                        IEnumerable<CongTrinhVM> congtrinhList = model.CongTrinhRow;
                        if (congtrinhList != null)
                        {
                            foreach (var i in congtrinhList)
                            {
                                BaoCaoCongTrinh bc = new BaoCaoCongTrinh();
                                bc.MaBaoCao = bchd1.MaBaoCao;
                                bc.TenCongTrinh = i.TenCongTrinh;
                                bc.ChuDauTu = i.ChuDauTu;
                                bc.ThoiGianThucHien = i.ThoiGianThucHien;
                                bc.GiaTriDaThucHien = i.GiaTriThucHien;
                                bc.GhiChu = i.GhiChu;
                                _bcctRespository.Add(bc);
                            }
                        }

                       // s2.Complete();
                        msg = 22;
                   // }
                }
                catch
                {
                    msg = 23;
                }
            }
            return RedirectToAction("ThongBao", new { iMsg = msg });

            //HoSoGiayPhep hsgp = new HoSoGiayPhep();
        }
        public ActionResult TaoMoi(ToChucDetailViewModel model, int save)
        {
            TaiKhoan tk = ((EnhancedPrincipal)HttpContext.User).Data;
            int msg = 0;

            if (save == 1)
            {
                //using (TransactionScope scope = new TransactionScope())
               // {
                    try
                    {
                        ThongTinChung ttc = new ThongTinChung();
                        //using (TransactionScope s1 = new TransactionScope())
                        //{
                            ttc.TenToChuc = model.TenToChuc;
                            ttc.GiayPhepKinhDoanh = model.GiayPhepKinhDoanh;
                            ttc.HangDoanhNghiep = model.HangDoanhNghiep;
                            ttc.VonLuuDong = model.VonLuuDong;
                            ttc.VonPhapDinh = model.VonPhapDinh;
                            ttc.SoTaiKhoan = model.SoTaiKhoan;
                            ttc.TongSoCanBo = model.TongSoCanBo;
                            ttc.MaLoaiHinhToChuc = model.MaLoaiHinhToChuc;
                            ttc.TruSoChinh = model.TruSoChinh;
                            ttc.NguoiDaiDien = model.NguoiDaiDien;
                            ttc.DienThoai = model.DienThoai;
                            ttc.Email = model.Email;
                            ttc.Fax = model.Fax;
                            _ttcRespository.Add(ttc);

                            // Luu vao to chuc neu chua co hoac update
                            //ToChuc t = _tochucRespository.GetToChucByID(model.MaToChuc);
                            ToChuc tc = _tochucRespository.GetToChucByTaiKhoan(tk.MaTaiKhoan);
                            if (tc == null)
                            {
                                ToChuc tc1 = new ToChuc();
                                tc1.TenToChuc = model.TenToChuc;
                                tc1.GiayPhepKinhDoanh = model.GiayPhepKinhDoanh;
                                tc1.HangDoanhNghiep = model.HangDoanhNghiep;
                                tc1.VonLuuDong = model.VonLuuDong;
                                tc1.VonPhapDinh = model.VonPhapDinh;
                                tc1.SoTaiKhoan = model.SoTaiKhoan;
                                tc1.TongSoCanBo = model.TongSoCanBo;
                                tc1.MaLoaiHinhToChuc = model.MaLoaiHinhToChuc;
                                tc1.TruSoChinh = model.TruSoChinh;
                                tc1.NguoiDaiDien = model.NguoiDaiDien;
                                tc1.DienThoai = model.DienThoai;
                                tc1.Email = model.Email;
                                tc1.Fax = model.Fax;
                                tc1.MaTaiKhoan = tk.MaTaiKhoan;
                                _tochucRespository.Add(tc1);
                            }
                            else
                            {
                                ToChuc t = tc;
                                t.TenToChuc = model.TenToChuc;
                                t.GiayPhepKinhDoanh = model.GiayPhepKinhDoanh;
                                t.HangDoanhNghiep = model.HangDoanhNghiep;
                                t.VonLuuDong = model.VonLuuDong;
                                t.VonPhapDinh = model.VonPhapDinh;
                                t.SoTaiKhoan = model.SoTaiKhoan;
                                t.TongSoCanBo = model.TongSoCanBo;
                                t.MaLoaiHinhToChuc = model.MaLoaiHinhToChuc;
                                t.TruSoChinh = model.TruSoChinh;
                                t.NguoiDaiDien = model.NguoiDaiDien;
                                t.DienThoai = model.DienThoai;
                                t.Email = model.Email;
                                t.Fax = model.Fax;
                                _tochucRespository.Update(t);
                            }
                            //s1.Complete();
                            //}
                        // xoa nhung nangluckekhai co MaToChuc va them vao nangluckekhai co MaToChuc moi
                        // them những nangluckekhai co MaThongTinChung

                        //List<ToChuc> tcs1 = tk.ToChucs.ToList();
                        //ToChuc tochuc = tcs1[0];
                        ToChuc tochuc = _tochucRespository.GetToChucByTaiKhoan(tk.MaTaiKhoan);
                        List<NangLucKeKhai> listNangLucCu = _nanglucRespository.GetNangLucByTCID(tochuc.MaToChuc);
                        foreach (var nlCu in listNangLucCu)
                        {
                            _nanglucRespository.Delete(nlCu);
                        }
                        IEnumerable<NangLucVM> listNangLuc = model.NangLucRow;
                        if (listNangLuc != null)
                        {
                            foreach (var i in listNangLuc)
                            {
                                NangLucKeKhai nl = new NangLucKeKhai();
                                nl.MaToChuc = tochuc.MaToChuc;
                                nl.NganhNghe = i.NganhNghe;
                                nl.SoTrenDaiHoc = i.Daihoc.GetValueOrDefault(0);
                                nl.SoTrungCap = i.TrungCap.GetValueOrDefault(0);
                                nl.SoCongNhanKyThuat = i.CongNhan.GetValueOrDefault(0);
                                nl.LoaiKhac = i.LoaiKhac.GetValueOrDefault(0);
                                _nanglucRespository.Add(nl);
                                NangLucKeKhai nl1 = new NangLucKeKhai();
                                nl1.MaThongTinChung = ttc.MaThongTinChung;
                                nl1.NganhNghe = i.NganhNghe;
                                nl1.SoTrenDaiHoc = i.Daihoc.GetValueOrDefault(0);
                                nl1.SoTrungCap = i.TrungCap.GetValueOrDefault(0);
                                nl1.SoCongNhanKyThuat = i.CongNhan.GetValueOrDefault(0);
                                nl1.LoaiKhac = i.LoaiKhac.GetValueOrDefault(0);
                                _nanglucRespository.Add(nl1);
                            }
                        }
                        // xoa nhung ThietBi co MaToChuc va them vao ThietBi co MaToChuc moi
                        // them những ThietBi co MaThongTinChung
                        List<ThietBi> listThietBiCu = _thietbiRespository.GetThietBiByTCID(tochuc.MaToChuc);
                        foreach (var tbCu in listThietBiCu)
                        {
                            _thietbiRespository.Delete(tbCu);
                        }
                        IEnumerable<ThietBiVM> listThietBi = model.ThietBiRow;
                        if (listThietBi != null)
                        {
                            foreach (var i in listThietBi)
                            {
                                ThietBi tb = new ThietBi();
                                tb.MaToChuc = tochuc.MaToChuc;
                                tb.TenThietBi_CongNghe = i.TenThietBi_CongNghe;
                                tb.SoLuong = i.SoLuong.GetValueOrDefault(0);
                                tb.TinhTrang = i.TinhTrang;
                                tb.GhiChu = i.GhiChu;
                                _thietbiRespository.Add(tb);
                                ThietBi tb1 = new ThietBi();
                                tb1.MaThongTinChung = ttc.MaThongTinChung;
                                tb1.TenThietBi_CongNghe = i.TenThietBi_CongNghe;
                                tb1.SoLuong = i.SoLuong.GetValueOrDefault(0);
                                tb1.TinhTrang = i.TinhTrang;
                                tb1.GhiChu = i.GhiChu;
                                _thietbiRespository.Add(tb1);
                            }
                        }

                        // xoa nhung ThietBi co MaToChuc va them vao ThietBi co MaToChuc moi
                        // them những ThietBi co MaThongTinChung

                        List<NhanLuc> listNhanLucCu = _nhanlucRespository.GetNhanLucByTCID(tochuc.MaToChuc);
                        foreach (var nlCu in listNhanLucCu)
                        {
                            _nhanlucRespository.Delete(nlCu);
                        }
                        IEnumerable<NhanLucVM> listNhanLuc = model.NhanLucRow;
                        if (listNhanLuc != null)
                        {
                            foreach (var i in listNhanLuc)
                            {
                                NhanLuc nl = new NhanLuc();
                                nl.MaToChuc = tochuc.MaToChuc;
                                nl.HoTen = i.HoTen;
                                nl.ChucVu = i.ChucVu;
                                nl.TrinhDoHocVan = i.TrinhDoHocVan;
                                nl.ThamNien = i.ThamNien;
                                _nhanlucRespository.Add(nl);
                                NhanLuc nl1 = new NhanLuc();
                                nl1.MaThongTinChung = ttc.MaThongTinChung;
                                nl1.HoTen = i.HoTen;
                                nl1.ChucVu = i.ChucVu;
                                nl1.TrinhDoHocVan = i.TrinhDoHocVan;
                                nl1.ThamNien = i.ThamNien;
                                _nhanlucRespository.Add(nl1);
                            }
                        }

                        HoSoGiayPhep hs = new HoSoGiayPhep();
                        hs.MaToChuc = tochuc.MaToChuc;
                        hs.MaThongTinChung = ttc.MaThongTinChung;
                        hs.NgayXinPhep = DateTime.Today;
                        hs.TinhTrang = 1;
                        hs.LoaiGiayPhep = 1;
                        hs.CamKet = model.Camket;
                        hs.LyDo = model.LyDo;
                        if (Request.Files["tepdinhkem"] != null) // If uploaded synchronously
                        {
                            var tep_guid = _fileStore.SaveUploadedFile(Request.Files["tepdinhkem"]);
                            var tep_filename = Request.Files["tepdinhkem"].FileName;
                            hs.TepDinhKem = tep_guid.ToString();
                        }
                        else
                        {
                            hs.TepDinhKem = Request["tepdinhkem_guid"].ToString();

                        }
                        _hsgpRepository.Add(hs);

                        ///add dang ky hoat dong
                        IList<int> listDK = model.DSHoatDongSelecteds;
                        if (listDK != null)
                        {
                            foreach (var i in listDK)
                            {
                                DangKyHoatDong dk = new DangKyHoatDong();
                                dk.MaHoSo = hs.MaHoSo;
                                dk.MaHoatDong = i;
                                dk.LanBoSung = 0;
                                _dkhdRespository.Add(dk);
                            }
                        }
                        msg = 20;
                    }
                       // scope.Complete();
                    //msg = 20;
                   // }
                    catch
                    {
                        MessageHelper.CreateMessage(MessageType.Error, "", new List<string> { "Gửi hồ sơ không thành công" }, HttpContext.Response);
                        model.loaiHinh = _loaihinhtochucRepository.GetLoaiHinhToChucs().ToList();
                        return View(model);
                        //msg = 21;
                    }
            }
            else
            {
                try
                {
                    //using (TransactionScope s2 = new TransactionScope())
                   // {
                        List<ToChuc> tcs = tk.ToChucs.ToList();
                        if (tcs.Count == 0)
                        {
                            ToChuc tc = new ToChuc();
                            tc.TenToChuc = model.TenToChuc;
                            tc.GiayPhepKinhDoanh = model.GiayPhepKinhDoanh;
                            tc.HangDoanhNghiep = model.HangDoanhNghiep;
                            tc.VonLuuDong = model.VonLuuDong;
                            tc.VonPhapDinh = model.VonPhapDinh;
                            tc.SoTaiKhoan = model.SoTaiKhoan;
                            tc.TongSoCanBo = model.TongSoCanBo;
                            tc.MaLoaiHinhToChuc = model.MaLoaiHinhToChuc;
                            tc.TruSoChinh = model.TruSoChinh;
                            tc.NguoiDaiDien = model.NguoiDaiDien;
                            tc.DienThoai = model.DienThoai;
                            tc.Email = model.Email;
                            tc.Fax = model.Fax;
                            tc.MaTaiKhoan = tk.MaTaiKhoan;
                            _tochucRespository.Add(tc);
                        }
                        else
                        {
                            ToChuc tc = tcs[0];
                            tc.TenToChuc = model.TenToChuc;
                            tc.GiayPhepKinhDoanh = model.GiayPhepKinhDoanh;
                            tc.HangDoanhNghiep = model.HangDoanhNghiep;
                            tc.VonLuuDong = model.VonLuuDong;
                            tc.VonPhapDinh = model.VonPhapDinh;
                            tc.SoTaiKhoan = model.SoTaiKhoan;
                            tc.TongSoCanBo = model.TongSoCanBo;
                            tc.MaLoaiHinhToChuc = model.MaLoaiHinhToChuc;
                            tc.TruSoChinh = model.TruSoChinh;
                            tc.NguoiDaiDien = model.NguoiDaiDien;
                            tc.DienThoai = model.DienThoai;
                            tc.Email = model.Email;
                            tc.Fax = model.Fax;
                            _tochucRespository.Update(tc);
                        }

                        ToChuc tochuc = _tochucRespository.GetToChucByTaiKhoan(tk.MaTaiKhoan);
                        List<NhanLuc> listNhanLucCu = _nhanlucRespository.GetNhanLucByTCID(tochuc.MaToChuc);
                        foreach (var nlCu in listNhanLucCu)
                        {
                            _nhanlucRespository.Delete(nlCu);
                        }
                        IEnumerable<NhanLucVM> listNhanLuc = model.NhanLucRow;
                        if (listNhanLuc != null)
                        {
                            foreach (var i in listNhanLuc)
                            {
                                NhanLuc nl = new NhanLuc();
                                nl.MaToChuc = tochuc.MaToChuc;
                                nl.HoTen = i.HoTen;
                                nl.ChucVu = i.ChucVu;
                                nl.TrinhDoHocVan = i.TrinhDoHocVan;
                                nl.ThamNien = i.ThamNien;
                                _nhanlucRespository.Add(nl);
                            }
                        }

                        List<NangLucKeKhai> listNangLucCu = _nanglucRespository.GetNangLucByTCID(tochuc.MaToChuc);
                        foreach (var nlCu in listNangLucCu)
                        {
                            _nanglucRespository.Delete(nlCu);
                        }
                        IEnumerable<NangLucVM> listNangLuc = model.NangLucRow;
                        if (listNangLuc != null)
                        {
                            foreach (var i in listNangLuc)
                            {
                                NangLucKeKhai nl = new NangLucKeKhai();
                                nl.MaToChuc = tochuc.MaToChuc;
                                nl.NganhNghe = i.NganhNghe;
                                nl.SoTrenDaiHoc = i.Daihoc.GetValueOrDefault(0);
                                nl.SoTrungCap = i.TrungCap.GetValueOrDefault(0);
                                nl.SoCongNhanKyThuat = i.CongNhan.GetValueOrDefault(0);
                                nl.LoaiKhac = i.LoaiKhac.GetValueOrDefault(0);
                                _nanglucRespository.Add(nl);
                            }
                        }

                        List<ThietBi> listThietBiCu = _thietbiRespository.GetThietBiByTCID(tochuc.MaToChuc);
                        foreach (var tbCu in listThietBiCu)
                        {
                            _thietbiRespository.Delete(tbCu);
                        }
                        IEnumerable<ThietBiVM> listThietBi = model.ThietBiRow;
                        if (listThietBi != null)
                        {
                            foreach (var i in listThietBi)
                            {
                                ThietBi tb = new ThietBi();
                                tb.MaToChuc = tochuc.MaToChuc;
                                tb.TenThietBi_CongNghe = i.TenThietBi_CongNghe;
                                tb.SoLuong = i.SoLuong.GetValueOrDefault(0);
                                tb.TinhTrang = i.TinhTrang;
                                tb.GhiChu = i.GhiChu;
                                _thietbiRespository.Add(tb);
                            }
                        }
                       // s2.Complete();
                        msg = 22;
                    //}
                }
                catch
                {
                    MessageHelper.CreateMessage(MessageType.Error, "", new List<string> { "Lỗi khi lưu hồ sơ đăng ký" }, HttpContext.Response);
                    model.loaiHinh = _loaihinhtochucRepository.GetLoaiHinhToChucs().ToList();
                    return View(model);
                }
            }
            return RedirectToAction("ThongBao", new { iMsg = msg });
        }
 public void Delete(DangKyHoatDong DangKy)
 {
     db.DangKyHoatDongs.DeleteOnSubmit(DangKy);
     db.SubmitChanges();
 }
 /*  public  IEnumerable<HoatDong> GetHoatDongByDangKy(List<DangKyHoatDong> dk)
 {
     List<int> listMaHD = new List<int>();
     foreach (DangKyHoatDong d in dk)
     {
         listMaHD.Add((int)d.MaHoatDong);
     }
    // return from d in db.HoatDongs
      //      where d.MaHoatDong in dk
 }
 */
 public void Add(DangKyHoatDong DangKy)
 {
     db.DangKyHoatDongs.InsertOnSubmit(DangKy);
     db.SubmitChanges();
 }