public BCCongTrinhController(BaoCaoCongTrinhRespository bcctRespository, BaoCaoHoatDongRespository bchdRespository)
 {
     this._bchdRepository = bchdRespository;
     this._bcctRepository = bcctRespository;
 }
        public ActionResult DownloadBaoCao(int id)
        {
            HoSoGiayPhep hs = _gphdRepository.GetHoSoGiayPhepByID(id);
            if (hs.MaThongTinChung == null || !hs.MaThongTinChung.HasValue)
            {
                return RedirectToAction("ChiTiet", new { id = id });
            }

            IBaoCaoHoatDongRepository bchdRepo = new BaoCaoHoatDongRespository();
            IBaoCaoCongTrinhRespository bcctRepo = new BaoCaoCongTrinhRespository();
            BaoCaoHoatDong bchd = bchdRepo.GetBaoCaoHoatDongByTC(hs.MaThongTinChung.Value);
            if (bchd == null) {
                return RedirectToAction("ChiTiet", new { id = id });
            }

            List<BaoCaoCongTrinh> bccts = bcctRepo.GetBCCTByBCHD(bchd.MaBaoCao);

            String outputName = HttpContext.Session.SessionID + DateTime.Now.Ticks + ".docx";
            String tmpPath = Path.Combine(HostingEnvironment.MapPath("~/App_Data/Templates"), "PM32-2010-TT-BTNMT7.docx");
            String outPath = Path.Combine(HostingEnvironment.MapPath("~/App_Data/Download"), outputName);
            DocumentHandling.DocumentHandling handling = new PM32_2010_TT_BTNMT7Handling();

            DateTime ngayBaoCao = DateTime.Now;
            List<List<string>> congTrinhs = new List<List<string>>();
            for (int i = 0; i < bccts.Count; i++)
            {
                List<string> congTrinh = new List<string>();
                BaoCaoCongTrinh bcct = bccts[i];

                congTrinh.Add((i + 1).ToString());
                congTrinh.Add(bcct.TenCongTrinh);
                congTrinh.Add(bcct.ChuDauTu);
                congTrinh.Add(bcct.CongDoanDaThiCong);
                congTrinh.Add(bcct.GiaTriDaThucHien);
                congTrinh.Add(bcct.ThoiGianThucHien);
                congTrinh.Add(bcct.GhiChu);

                congTrinhs.Add(congTrinh);
            }

            handling.SettingParam(PM32_2010_TT_BTNMT7Handling.DIADIEM, "Hồ Chí Minh");
            handling.SettingParam(PM32_2010_TT_BTNMT7Handling.NGAY, ngayBaoCao.Day);
            handling.SettingParam(PM32_2010_TT_BTNMT7Handling.THANG, ngayBaoCao.Month);
            handling.SettingParam(PM32_2010_TT_BTNMT7Handling.NAM, ngayBaoCao.Year);
            if (bchd.TuNam != null && bchd.TuNam.HasValue)
            {
                handling.SettingParam(PM32_2010_TT_BTNMT7Handling.TUNAM, bchd.TuNam.Value);
            }
            if (bchd.DenNam != null && bchd.DenNam.HasValue)
            {
                handling.SettingParam(PM32_2010_TT_BTNMT7Handling.DENNAM, bchd.DenNam.Value);
            }
            if (bchd.ToChuc != null)
            {
                handling.SettingParam(PM32_2010_TT_BTNMT7Handling.TOCHUCDUOCCAPGIAYPHEP, bchd.ToChuc.TenToChuc);
                handling.SettingParam(PM32_2010_TT_BTNMT7Handling.TENTOCHUC, bchd.ToChuc.TenToChuc);
                if (bchd.ToChuc.GiayPhepKinhDoanh != null)
                {
                    handling.SettingParam(PM32_2010_TT_BTNMT7Handling.GIAYPHEPHOATDONGSO, bchd.ToChuc.GiayPhepKinhDoanh);
                    handling.SettingParam(PM32_2010_TT_BTNMT7Handling.GIAYPHEPHOATDONGCAPNGAY, "......");
                }
            }
            if (bchd.DoanhThu != null && bchd.DoanhThu.HasValue)
            {
                handling.SettingParam(PM32_2010_TT_BTNMT7Handling.DOANHTHUNAM, bchd.DoanhThu.Value);
            }
            if (bchd.NopNganSach != null && bchd.NopNganSach.HasValue)
            {
                handling.SettingParam(PM32_2010_TT_BTNMT7Handling.NOPNGANSACHNAM, bchd.NopNganSach.Value);
            }
            handling.SettingParam(PM32_2010_TT_BTNMT7Handling.CONGTRINHDODACBANDO, congTrinhs);

            handling.Generate(tmpPath, outPath);

            // Download
            // Open the file.
            Stream iStream = null;
            try
            {
                iStream = new FileStream(outPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                // Total bytes to read:
                long dataToRead = iStream.Length;

                // Buffer to read 10K bytes in chunk:
                byte[] buffer = new Byte[10000];

                Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + "PM32-2010-TT-BTNMT.docx");

                // Read the bytes.
                while (dataToRead > 0)
                {
                    // Verify that the client is connected.
                    if (Response.IsClientConnected)
                    {
                        // Read the data in buffer.
                        int length = iStream.Read(buffer, 0, 10000);

                        // Write the data to the current output stream.
                        Response.OutputStream.Write(buffer, 0, length);

                        // Flush the data to the HTML output.
                        Response.Flush();

                        buffer = new Byte[10000];
                        dataToRead = dataToRead - length;
                    }
                    else
                    {
                        //prevent infinite loop if user disconnects
                        dataToRead = -1;
                    }
                }
            }
            catch (Exception ex)
            {
                // Trap the error, if any.
                Response.Write("Error : " + ex.Message);
            }
            finally
            {
                Response.Close();
                if (iStream != null)
                {
                    //Close the file.
                    iStream.Close();
                }
                if (System.IO.File.Exists(outPath))
                {
                    System.IO.File.Delete(outPath);
                }
            }
            return RedirectToAction("ChiTiet", new { id = id });
        }