public static bool XoaTienTamung(NoitruTamung objTamung, bool VanConTamung, string lydohuy)
 {
     try
     {
         using (var scope = new TransactionScope())
         {
             using (var sh = new SharedDbConnectionScope())
             {
                 KcbLoghuy objHuy = new KcbLoghuy();
                 objHuy.IdBenhnhan   = objTamung.IdBenhnhan;
                 objHuy.MaLuotkham   = objTamung.MaLuotkham;
                 objHuy.LoaiphieuHuy = 2;//0= hủy thanh toán;1= hủy phiếu chi;2= hủy tạm ứng
                 objHuy.LydoHuy      = lydohuy;
                 objHuy.SotienHuy    = Utility.DecimaltoDbnull(objTamung.SoTien, 0);
                 objHuy.IdNhanvien   = globalVariables.gv_intIDNhanvien;
                 objHuy.NgayHuy      = globalVariables.SysDate;
                 objHuy.NguoiTao     = globalVariables.UserName;
                 objHuy.NgayTao      = globalVariables.SysDate;
                 objHuy.IsNew        = true;
                 objHuy.Save();
                 new Delete().From(NoitruTamung.Schema).Where(NoitruTamung.Columns.Id).IsEqualTo(objTamung.Id).Execute();
                 NoitruPhieudieutri objNoitruPhieudieutri = Utility.getNoitruPhieudieutri(objTamung.IdBenhnhan, objTamung.MaLuotkham);
                 KcbLuotkham        objKcbLuotkham        = Utility.getKcbLuotkham(objTamung.IdBenhnhan, objTamung.MaLuotkham);
                 if (Utility.Byte2Bool(objKcbLuotkham.TrangthaiNoitru) && objNoitruPhieudieutri == null && !VanConTamung)//Chỉ update nếu là nội trú
                 {
                     objKcbLuotkham.IsNew           = false;
                     objKcbLuotkham.TrangthaiNoitru = 1;
                     objKcbLuotkham.MarkOld();
                     objKcbLuotkham.Save();
                 }
             }
             scope.Complete();
         }
         return(true);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
 public static bool XoaTienTamung(NoitruTamung objTamung,bool VanConTamung,string lydohuy)
 {
     try
     {
         using (var scope = new TransactionScope())
         {
             using (var sh = new SharedDbConnectionScope())
             {
                 KcbLoghuy objHuy = new KcbLoghuy();
                 objHuy.IdBenhnhan = objTamung.IdBenhnhan;
                 objHuy.MaLuotkham = objTamung.MaLuotkham;
                 objHuy.LoaiphieuHuy = 2;//0= hủy thanh toán;1= hủy phiếu chi;2= hủy tạm ứng
                 objHuy.LydoHuy = lydohuy;
                 objHuy.SotienHuy =Utility.DecimaltoDbnull( objTamung.SoTien,0);
                 objHuy.IdNhanvien = globalVariables.gv_intIDNhanvien;
                 objHuy.NgayHuy=globalVariables.SysDate;
                 objHuy.NguoiTao=globalVariables.UserName;
                 objHuy.NgayTao=globalVariables.SysDate;
                 objHuy.IsNew=true;
                 objHuy.Save();
                 new Delete().From(NoitruTamung.Schema).Where(NoitruTamung.Columns.Id).IsEqualTo(objTamung.Id).Execute();
                 NoitruPhieudieutri objNoitruPhieudieutri = Utility.getNoitruPhieudieutri(objTamung.IdBenhnhan, objTamung.MaLuotkham);
                 KcbLuotkham objKcbLuotkham = Utility.getKcbLuotkham(objTamung.IdBenhnhan, objTamung.MaLuotkham);
                 if (Utility.Byte2Bool(objKcbLuotkham.TrangthaiNoitru) && objNoitruPhieudieutri == null && !VanConTamung)//Chỉ update nếu là nội trú
                 {
                     objKcbLuotkham.IsNew = false;
                     objKcbLuotkham.TrangthaiNoitru = 1;
                     objKcbLuotkham.MarkOld();
                     objKcbLuotkham.Save();
                 }
             }
             scope.Complete();
         }
         return true;
     }
     catch (Exception ex)
     {
         return false;
     }
 }
 public ActionResult HuyThongTinLanThanhToan(int id_thanhtoan, KcbLuotkham objLuotkham, string lydohuy, int IdHdonLog,bool HuyBienlai)
 {
     try
     {
         decimal v_TotalPaymentDetail = 0;
         decimal v_DiscountRate = 0;
         using (var scope = new TransactionScope())
         {
             using (var dbscope = new SharedDbConnectionScope())
             {
                 if (Utility.Byte2Bool(objLuotkham.TrangthaiNoitru))
                 {
                     SPs.NoitruHuyhoanung(objLuotkham.MaLuotkham, objLuotkham.IdBenhnhan).Execute();
                 }
                 if (IdHdonLog > 0)
                     if (HuyBienlai)
                         new Update(HoadonLog.Schema).Set(HoadonLog.Columns.TrangThai).EqualTo(1)
                             .Where(HoadonLog.Columns.IdHdonLog).IsEqualTo(IdHdonLog).Execute();
                     else
                         new Delete().From(HoadonLog.Schema)
                             .Where(HoadonLog.Columns.IdHdonLog).IsEqualTo(IdHdonLog).Execute();
                 SqlQuery sqlQuery =
                     new Select().From(KcbThanhtoanChitiet.Schema).Where(KcbThanhtoanChitiet.Columns.IdThanhtoan).IsEqualTo(
                         id_thanhtoan);
                 KcbThanhtoanChitietCollection arrPaymentDetails = sqlQuery.ExecuteAsCollection<KcbThanhtoanChitietCollection>();
                 KcbThanhtoan objThanhtoan = KcbThanhtoan.FetchByID(id_thanhtoan);
                 KcbLoghuy objKcbLoghuy = new KcbLoghuy();
                 objKcbLoghuy.IdBenhnhan = objThanhtoan.IdBenhnhan;
                 objKcbLoghuy.MaLuotkham = objThanhtoan.MaLuotkham;
                 objKcbLoghuy.IdNhanvien = globalVariables.gv_intIDNhanvien;
                 objKcbLoghuy.SotienHuy = objThanhtoan.TongTien;
                 objKcbLoghuy.LydoHuy = lydohuy;
                 objKcbLoghuy.NgayHuy = DateTime.Now;
                 objKcbLoghuy.NgayTao = DateTime.Now;
                 objKcbLoghuy.NguoiTao = globalVariables.UserName;
                 objKcbLoghuy.IsNew = true;
                 objKcbLoghuy.LoaiphieuHuy = Utility.ByteDbnull(objThanhtoan.TrangThai, 0);
                 objKcbLoghuy.Save();
                 if (objThanhtoan != null)
                     HUYTHONGTIN_THANHTOAN(arrPaymentDetails, objThanhtoan);
                 new Delete().From(KcbPhieuDct.Schema)
                     .Where(KcbPhieuDct.Columns.MaLuotkham).IsEqualTo(objThanhtoan.MaLuotkham)
                     .And(KcbPhieuDct.Columns.IdBenhnhan).IsEqualTo(objThanhtoan.IdBenhnhan)
                     .And(KcbPhieuDct.Columns.LoaiThanhtoan).IsEqualTo(objThanhtoan.KieuThanhtoan).Execute();
                 if (objLuotkham != null)
                 {
                     byte locked = (byte)(objLuotkham.MaDoituongKcb == "DV" ? objLuotkham.Locked : 0);
                     byte TthaiThanhtoannoitru = (byte)(Utility.Byte2Bool(objThanhtoan.NoiTru) ? 0 : 1);
                     new Update(KcbLuotkham.Schema)
                         .Set(KcbLuotkham.Columns.NgayKetthuc).EqualTo(null)
                         .Set(KcbLuotkham.Columns.NguoiKetthuc).EqualTo(string.Empty)
                         .Set(KcbLuotkham.Columns.Locked).EqualTo(locked)
                         .Set(KcbLuotkham.Columns.TrangthaiNgoaitru).EqualTo(locked)
                         .Set(KcbLuotkham.Columns.TthaiThanhtoannoitru).EqualTo(TthaiThanhtoannoitru)
                         .Set(KcbLuotkham.Columns.LydoKetthuc).EqualTo("")
                         .Where(KcbLuotkham.Columns.MaLuotkham).IsEqualTo(objLuotkham.MaLuotkham)
                         .And(KcbLuotkham.Columns.IdBenhnhan).IsEqualTo(objLuotkham.IdBenhnhan).Execute();
                 }
                 KcbThanhtoan.Delete(id_thanhtoan);
                 if (objLuotkham != null) log.Info(string.Format("Phiếu thanh toán ID: {0} của bệnh nhân: {1} - ID Bệnh nhân: {2} đã được hủy bởi :{3} với lý do hủy :{4}", id_thanhtoan.ToString(), objLuotkham.MaLuotkham, objLuotkham.IdBenhnhan, globalVariables.UserName, lydohuy));
             }
             scope.Complete();
             return ActionResult.Success;
         }
     }
     catch (Exception exception)
     {
         log.Error("Loi trong qua trinh huy thong tin {0}", exception.ToString());
         return ActionResult.Error;
     }
 }
        public ActionResult HuyPhieuchi(KcbThanhtoan objThanhtoan, KcbLuotkham objLuotkham, string lydohuy)
        {
            try
            {
                decimal PtramBHYT = 0;
                ///tổng tiền hiện tại truyền vào của lần payment đang thực hiện
                decimal v_dblTongtienHuy = 0;
                ///tổng tiền đã thanh toán
                decimal v_TotalPaymentDetail = 0;
                using (var scope = new TransactionScope())
                {
                    using (var dbscope = new SharedDbConnectionScope())
                    {
                        if (objThanhtoan != null)
                        {
                            if (THU_VIEN_CHUNG.Laygiatrithamsohethong("KCB_THANHTOAN_KHOIPHUCLAIDULIEU_KHIHUYPHIEUCHI", "0", false) == "1")
                            {
                                KcbThanhtoanCollection lstKcbThanhtoanCollection =
                            new KcbThanhtoanController()
                           .FetchByQuery(
                               KcbThanhtoan.CreateQuery()
                               .AddWhere(KcbThanhtoan.Columns.MaLuotkham, Comparison.Equals, objThanhtoan.MaLuotkham)
                               .AND(KcbThanhtoan.Columns.IdBenhnhan, Comparison.Equals, objThanhtoan.IdBenhnhan)
                               .AND(KcbThanhtoan.Columns.KieuThanhtoan, Comparison.Equals, objThanhtoan.KieuThanhtoan)
                               .AND(KcbThanhtoan.Columns.TrangThai, Comparison.Equals, 0));//Chỉ lấy về các bản ghi thanh toán thường(0= thường;1= thanh toán hủy(trả lại tiền))
                                //Lấy tổng tiền của các lần thanh toán trước
                                List<KcbThanhtoanChitiet> lstKcbThanhtoanChitiet_KhoiphucChitra = new List<KcbThanhtoanChitiet>();

                                foreach (KcbThanhtoan Payment in lstKcbThanhtoanCollection)
                                {
                                    KcbThanhtoanChitietCollection lstKcbThanhtoanChitietCollection = new Select().From(KcbThanhtoanChitiet.Schema)
                                        .Where(KcbThanhtoanChitiet.Columns.IdThanhtoan).IsEqualTo(Payment.IdThanhtoan)
                                        .ExecuteAsCollection<KcbThanhtoanChitietCollection>();

                                    foreach (KcbThanhtoanChitiet _KcbThanhtoanChitiet in lstKcbThanhtoanChitietCollection)
                                    {
                                        //Tính các khoản chi tiết đồng chi trả<->Tự túc=0
                                        if (_KcbThanhtoanChitiet.TuTuc == 0)
                                        {

                                            lstKcbThanhtoanChitiet_KhoiphucChitra.Add(_KcbThanhtoanChitiet);
                                            _KcbThanhtoanChitiet.IsNew = false;
                                            _KcbThanhtoanChitiet.MarkOld();
                                            //Tính tiền các khoản có BHYT chi trả
                                            if (!Utility.Byte2Bool(_KcbThanhtoanChitiet.TrangthaiHuy))
                                                v_TotalPaymentDetail += Utility.Int32Dbnull(_KcbThanhtoanChitiet.SoLuong) *
                                                                        Utility.DecimaltoDbnull(_KcbThanhtoanChitiet.DonGia);
                                        }

                                    }
                                }

                                KcbThanhtoanChitietCollection lstKcbThanhtoanChitiet_Phieuchi = new Select().From(KcbThanhtoanChitiet.Schema).Where(KcbThanhtoanChitiet.Columns.IdThanhtoan).IsEqualTo(objThanhtoan.IdThanhtoan).ExecuteAsCollection<KcbThanhtoanChitietCollection>();

                                List<long> lstIDChitiethuy = (from p in lstKcbThanhtoanChitiet_Phieuchi
                                                              select Utility.Int64Dbnull(p.IdChitiethuy, -1)).ToList<long>();
                                List<KcbThanhtoanChitiet> lstKcbThanhtoanChitiet_Huy = new Select().From(KcbThanhtoanChitiet.Schema).Where(KcbThanhtoanChitiet.Columns.IdChitiet).In(lstIDChitiethuy).ExecuteAsCollection<KcbThanhtoanChitietCollection>().ToList<KcbThanhtoanChitiet>();

                                v_dblTongtienHuy = TongtienKhongTutuc(lstKcbThanhtoanChitiet_Huy);
                                LayThongtinPtramBHYT(v_dblTongtienHuy + v_TotalPaymentDetail, objLuotkham, ref PtramBHYT);
                                //Tính lại thông tin BHYT,BN chi trả cho toàn bộ các chi tiết của BN đã thanh toán mà không bị hủy
                                THU_VIEN_CHUNG.TinhPhamTramBHYT(objLuotkham, ref lstKcbThanhtoanChitiet_Huy, ref lstKcbThanhtoanChitiet_KhoiphucChitra, PtramBHYT);

                                //Tính lại tổng tiền cho tất cả các lần thanh toán cũ
                                List<long> lstIdThanhtoanCu = (from q in lstKcbThanhtoanChitiet_KhoiphucChitra
                                                               select Utility.Int64Dbnull(q.IdThanhtoan, -1)).Distinct().ToList<long>();
                                decimal TT_BN = 0m;
                                decimal TT_BHYT = 0m;
                                decimal TT_Chietkhau_Chitiet = 0m;
                                foreach (int IdThanhtoan in lstIdThanhtoanCu)
                                {
                                    TT_BN = 0m;
                                    TT_BHYT = 0m;
                                    TT_Chietkhau_Chitiet = 0m;
                                    List<KcbThanhtoanChitiet> _LstChitiet = (from q in lstKcbThanhtoanChitiet_KhoiphucChitra
                                                                             where q.IdThanhtoan == IdThanhtoan
                                                                             select q).ToList<KcbThanhtoanChitiet>();

                                    if (_LstChitiet.Count > 0)
                                    {
                                        foreach (KcbThanhtoanChitiet objThanhtoanDetail in _LstChitiet)
                                        {
                                            TT_BN += (objThanhtoanDetail.BnhanChitra + objThanhtoanDetail.PhuThu) * objThanhtoanDetail.SoLuong;
                                            if (!Utility.Byte2Bool(objThanhtoanDetail.TuTuc))
                                                TT_BHYT += objThanhtoanDetail.BhytChitra * objThanhtoanDetail.SoLuong;
                                            TT_Chietkhau_Chitiet += Utility.DecimaltoDbnull(objThanhtoanDetail.TienChietkhau, 0);
                                            //Lưu lại các thông tin tiền đã được tính toán lại ở thủ tục THU_VIEN_CHUNG.TinhPhamTramBHYT(...)
                                            objThanhtoanDetail.IsNew = false;
                                            objThanhtoanDetail.MarkOld();
                                            objThanhtoanDetail.Save();
                                        }
                                        //Update lại tiền thanh toán
                                        new Update(KcbThanhtoan.Schema)
                          .Set(KcbThanhtoan.Columns.TongTien).EqualTo(TT_BHYT + TT_BN)
                          .Set(KcbThanhtoan.Columns.BnhanChitra).EqualTo(TT_BN)
                          .Set(KcbThanhtoan.Columns.BhytChitra).EqualTo(TT_BHYT)
                          .Set(KcbThanhtoan.Columns.MaDoituongKcb).EqualTo(objLuotkham.MaDoituongKcb)
                          .Set(KcbThanhtoan.Columns.IdDoituongKcb).EqualTo(objLuotkham.IdDoituongKcb)
                          .Set(KcbThanhtoan.Columns.PtramBhyt).EqualTo(objLuotkham.PtramBhyt)
                          .Where(KcbThanhtoan.Columns.IdThanhtoan).IsEqualTo(IdThanhtoan).Execute();
                                        //Update phiếu thu
                                        new Update(KcbPhieuthu.Schema)
                         .Set(KcbPhieuthu.Columns.SoTien).EqualTo(TT_BN - TT_Chietkhau_Chitiet)
                         .Set(KcbPhieuthu.Columns.SotienGoc).EqualTo(TT_BN)
                         .Where(KcbPhieuthu.Columns.IdThanhtoan).IsEqualTo(IdThanhtoan).Execute();
                                    }

                                }
                                //Khôi phục lại trạng thái hủy
                                foreach (KcbThanhtoanChitiet objKcbThanhtoanChitiet in lstKcbThanhtoanChitiet_Huy)
                                {
                                    objKcbThanhtoanChitiet.IsNew = false;
                                    objKcbThanhtoanChitiet.MarkOld();
                                    objKcbThanhtoanChitiet.TrangthaiHuy = 0;
                                    objKcbThanhtoanChitiet.NgayHuy = null;
                                    objKcbThanhtoanChitiet.NguoiHuy = null;
                                    objKcbThanhtoanChitiet.Save();

                                    ///thanh toán khám chữa bệnh))
                                    if (objKcbThanhtoanChitiet.IdLoaithanhtoan == 1)
                                    {

                                        new Update(KcbDangkyKcb.Schema)
                                            .Set(KcbDangkyKcb.Columns.TrangthaiHuy).EqualTo(0)
                                            .Where(KcbDangkyKcb.Columns.IdKham).IsEqualTo(objKcbThanhtoanChitiet.IdPhieu).Execute();
                                    }
                                    ///thah toán phần dịch vụ cận lâm sàng
                                    if (objKcbThanhtoanChitiet.IdLoaithanhtoan == 2)
                                    {
                                        KcbChidinhclsChitiet objKcbChidinhclsChitiet = KcbChidinhclsChitiet.FetchByID(objKcbThanhtoanChitiet.IdPhieuChitiet);
                                        if (globalVariables.UserName != "ADMIN")
                                        {
                                            if (objKcbChidinhclsChitiet != null && objKcbChidinhclsChitiet.TrangThai >= 3)//Đã có kết quả
                                            {
                                                return ActionResult.AssignIsConfirmed;
                                            }
                                        }
                                        new Update(KcbChidinhclsChitiet.Schema)
                                            .Set(KcbChidinhclsChitiet.Columns.TrangthaiHuy).EqualTo(0)
                                            .Where(KcbChidinhclsChitiet.Columns.IdChitietchidinh).IsEqualTo(objKcbThanhtoanChitiet.IdPhieuChitiet)
                                            .Execute();
                                    }
                                    ///thanh toán phần thuốc
                                    if (objKcbThanhtoanChitiet.IdLoaithanhtoan == 3)
                                    {
                                        KcbDonthuocChitiet objKcbDonthuocChitiet = KcbDonthuocChitiet.FetchByID(objKcbThanhtoanChitiet.IdPhieuChitiet);

                                        if (globalVariables.UserName != "ADMIN")
                                        {
                                            if (objKcbDonthuocChitiet != null && Utility.Byte2Bool(objKcbDonthuocChitiet.TrangThai))
                                            {
                                                return ActionResult.PresIsConfirmed;
                                            }
                                        }
                                        new Update(KcbDonthuocChitiet.Schema)
                                            .Set(KcbDonthuocChitiet.Columns.TrangthaiHuy).EqualTo(0)
                                            .Where(KcbDonthuocChitiet.Columns.IdChitietdonthuoc).IsEqualTo(objKcbThanhtoanChitiet.IdPhieuChitiet)
                                            .Execute();
                                    }

                                }

                            }
                            //Ghi lại log hủy
                            KcbLoghuy objKcbLoghuy = new KcbLoghuy();
                            objKcbLoghuy.IdBenhnhan = objThanhtoan.IdBenhnhan;
                            objKcbLoghuy.MaLuotkham = objThanhtoan.MaLuotkham;
                            objKcbLoghuy.IdNhanvien = globalVariables.gv_intIDNhanvien;
                            objKcbLoghuy.SotienHuy = objThanhtoan.TongTien;
                            objKcbLoghuy.LydoHuy = lydohuy;
                            objKcbLoghuy.NgayHuy = DateTime.Now;
                            objKcbLoghuy.NgayTao = DateTime.Now;
                            objKcbLoghuy.NguoiTao = globalVariables.UserName;
                            objKcbLoghuy.IsNew = true;
                            objKcbLoghuy.LoaiphieuHuy = Utility.ByteDbnull(objThanhtoan.TrangThai, 0);
                            objKcbLoghuy.Save();
                            //Xóa các thông tin phiếu chi
                            new Delete().From(KcbThanhtoan.Schema)
                                .Where(KcbThanhtoan.Columns.IdThanhtoan).IsEqualTo(objThanhtoan.IdThanhtoan).Execute();
                            new Delete().From(KcbThanhtoanChitiet.Schema)
                                .Where(KcbThanhtoanChitiet.Columns.IdThanhtoan).IsEqualTo(objThanhtoan.IdThanhtoan).Execute();
                            new Delete().From(KcbPhieuthu.Schema)
                                .Where(KcbPhieuthu.Columns.IdThanhtoan).IsEqualTo(objThanhtoan.IdThanhtoan)
                                .And(KcbPhieuthu.Columns.LoaiPhieuthu).IsEqualTo(1).Execute();

                        }
                        else
                        {
                            return ActionResult.Error;
                        }
                    }
                    scope.Complete();
                    return ActionResult.Success;
                }
            }
            catch (Exception exception)
            {
                log.Error("Ban ra loi Exception={0}", exception);
                return ActionResult.Error;
            }
        }
        public ActionResult HuyPhieuchi(KcbThanhtoan objPhieuchi, KcbLuotkham objLuotkham, string lydohuy)
        {
            try
            {
                decimal PtramBHYT = 0;
                ///tổng tiền hiện tại truyền vào của lần payment đang thực hiện
                decimal v_dblTongtienHuy = 0;
                ///tổng tiền đã thanh toán
                decimal v_TotalPaymentDetail = 0;
                using (var scope = new TransactionScope())
                {
                    using (var dbscope = new SharedDbConnectionScope())
                    {
                        if (objPhieuchi != null)
                        {
                            KcbThanhtoanChitietCollection lstKcbThanhtoanChitiet_Phieuchi =
                                new Select().From(KcbThanhtoanChitiet.Schema)
                                .Where(KcbThanhtoanChitiet.Columns.IdThanhtoanhuy).IsEqualTo(objPhieuchi.IdThanhtoan)
                                .ExecuteAsCollection<KcbThanhtoanChitietCollection>();

                            List<long> lstIDThanhtoanKhoiphuc = (from p in lstKcbThanhtoanChitiet_Phieuchi
                                                                 select Utility.Int64Dbnull(p.IdThanhtoan, -1)).ToList<long>();

                            //Khôi phục lại trạng thái hủy
                            foreach (KcbThanhtoanChitiet objKcbThanhtoanChitiet in lstKcbThanhtoanChitiet_Phieuchi)
                            {
                                objKcbThanhtoanChitiet.IsNew = false;
                                objKcbThanhtoanChitiet.MarkOld();
                                objKcbThanhtoanChitiet.TrangthaiHuy = 0;
                                objKcbThanhtoanChitiet.IdThanhtoanhuy = -1;
                                objKcbThanhtoanChitiet.Save();

                                ///thanh toán khám chữa bệnh))
                                if (objKcbThanhtoanChitiet.IdLoaithanhtoan == 1)
                                {

                                    new Update(KcbDangkyKcb.Schema)
                                        .Set(KcbDangkyKcb.Columns.TrangthaiHuy).EqualTo(0)
                                        .Where(KcbDangkyKcb.Columns.IdKham).IsEqualTo(objKcbThanhtoanChitiet.IdPhieu).Execute();
                                }
                                ///thah toán phần dịch vụ cận lâm sàng
                                if (objKcbThanhtoanChitiet.IdLoaithanhtoan == 2)
                                {
                                    KcbChidinhclsChitiet objKcbChidinhclsChitiet = KcbChidinhclsChitiet.FetchByID(objKcbThanhtoanChitiet.IdPhieuChitiet);
                                    if (objKcbChidinhclsChitiet != null && objKcbChidinhclsChitiet.TrangThai >= 3)//Đã có kết quả
                                    {
                                        return ActionResult.AssignIsConfirmed;
                                    }

                                    new Update(KcbChidinhclsChitiet.Schema)
                                        .Set(KcbChidinhclsChitiet.Columns.TrangthaiHuy).EqualTo(0)
                                        .Where(KcbChidinhclsChitiet.Columns.IdChitietchidinh).IsEqualTo(objKcbThanhtoanChitiet.IdPhieuChitiet)
                                        .Execute();
                                }
                                ///thanh toán phần thuốc
                                if (objKcbThanhtoanChitiet.IdLoaithanhtoan == 3)
                                {
                                    KcbDonthuocChitiet objKcbDonthuocChitiet = KcbDonthuocChitiet.FetchByID(objKcbThanhtoanChitiet.IdPhieuChitiet);

                                    if (objKcbDonthuocChitiet != null && Utility.Byte2Bool(objKcbDonthuocChitiet.TrangThai))
                                    {
                                        return ActionResult.PresIsConfirmed;
                                    }

                                    new Update(KcbDonthuocChitiet.Schema)
                                        .Set(KcbDonthuocChitiet.Columns.TrangthaiHuy).EqualTo(0)
                                        .Where(KcbDonthuocChitiet.Columns.IdChitietdonthuoc).IsEqualTo(objKcbThanhtoanChitiet.IdPhieuChitiet)
                                        .Execute();
                                }
                            }
                            //Ghi lại log hủy
                            KcbLoghuy objKcbLoghuy = new KcbLoghuy();
                            objKcbLoghuy.IdBenhnhan = objPhieuchi.IdBenhnhan;
                            objKcbLoghuy.MaLuotkham = objPhieuchi.MaLuotkham;
                            objKcbLoghuy.IdNhanvien = globalVariables.gv_intIDNhanvien;
                            objKcbLoghuy.SotienHuy = objPhieuchi.TongTien;
                            objKcbLoghuy.LydoHuy = lydohuy;
                            objKcbLoghuy.NgayHuy = DateTime.Now;
                            objKcbLoghuy.NgayTao = DateTime.Now;
                            objKcbLoghuy.NguoiTao = globalVariables.UserName;
                            objKcbLoghuy.IsNew = true;
                            objKcbLoghuy.LoaiphieuHuy = Utility.ByteDbnull(objPhieuchi.KieuThanhtoan, 0);
                            objKcbLoghuy.Save();
                            //Xóa các thông tin phiếu chi
                            new Delete().From(KcbThanhtoan.Schema)
                                .Where(KcbThanhtoan.Columns.IdThanhtoan).IsEqualTo(objPhieuchi.IdThanhtoan).Execute();
                            new Delete().From(KcbPhieuthu.Schema)
                                .Where(KcbPhieuthu.Columns.IdThanhtoan).IsEqualTo(objPhieuchi.IdThanhtoan)
                                .And(KcbPhieuthu.Columns.LoaiPhieuthu).IsEqualTo(1).Execute();
                            //Đối tượng Dịch vụ không cần khôi phục dữ liệu
                            //Đối tượng BHYT cần tính toán lại % BHYT để xác định có cần trả lại số tiền >Số tiền hủy cho BN hay không?

                            if (THU_VIEN_CHUNG.IsBaoHiem(objLuotkham.IdLoaidoituongKcb))
                            {
                                decimal TT_BN = 0m;
                                decimal TT_BNCT = 0m;
                                decimal TT_PT = 0m;
                                decimal TT_TT = 0m;
                                decimal TT_BHYT = 0m;
                                decimal TT_Chietkhau_Chitiet = 0m;

                                //Thường chỉ trả về 1 bản ghi thanh toán duy nhất vì là đối tượng BHYT
                                KcbThanhtoanCollection lstKcbThanhtoanCollection =
                                   new KcbThanhtoanController()
                                   .FetchByQuery(
                                       KcbThanhtoan.CreateQuery()
                                       .AddWhere(KcbThanhtoan.Columns.MaLuotkham, Comparison.Equals, objLuotkham.MaLuotkham)
                                       .AND(KcbThanhtoan.Columns.IdBenhnhan, Comparison.Equals, objLuotkham.IdBenhnhan)
                                       .AND(KcbThanhtoan.Columns.KieuThanhtoan, Comparison.Equals, 0)
                                       );
                                List<long> lstIdThanhtoanAll = (from q in lstKcbThanhtoanCollection
                                                                select q.IdThanhtoan).Distinct().ToList<long>();
                                //Biến chứa danh sách tất cả các chi tiết dùng để tính lại tổng tiền thanh toán cho thanh toán có bản ghi bị hủy
                                List<KcbThanhtoanChitiet> lstKcbThanhtoanChitiet_Tatca = new List<KcbThanhtoanChitiet>();
                                if (lstIdThanhtoanAll.Count>0)
                                    lstKcbThanhtoanChitiet_Tatca=
                                     new Select().From(KcbThanhtoanChitiet.Schema)
                                    .Where(KcbThanhtoanChitiet.Columns.IdThanhtoan).In(lstIdThanhtoanAll)
                                    .ExecuteAsCollection<KcbThanhtoanChitietCollection>().ToList<KcbThanhtoanChitiet>();

                                v_TotalPaymentDetail = (from p in lstKcbThanhtoanChitiet_Tatca
                                                        where p.TuTuc == 0
                                                        select p).Sum(c => c.SoLuong * c.DonGia);

                                //Tính lại % BHYT mới sau khi đã trả lại tiền một số dịch vụ
                                LayThongtinPtramBHYT(v_TotalPaymentDetail, objLuotkham, ref PtramBHYT);

                                //Tính lại thông tin BHYT,BN chi trả cho toàn bộ các chi tiết của BN đã thanh toán mà không bị hủy
                                List<KcbThanhtoanChitiet> lsttemp = new List<KcbThanhtoanChitiet>();
                                THU_VIEN_CHUNG.TinhPhamTramBHYT(objLuotkham, ref lsttemp, ref lstKcbThanhtoanChitiet_Tatca, PtramBHYT);
                                if (THU_VIEN_CHUNG.Laygiatrithamsohethong("KCB_THANHTOAN_TINHLAITONGTIEN_CACTHANHTOAN_BITRALAITIEN", "1", false) == "1")
                                {
                                    foreach (int IdThanhtoan in lstIdThanhtoanAll)//Chỉ thực hiện tính lại thanh toán có chứa các chi tiết bị thay đổi
                                    {
                                        TT_BN = 0m;
                                        TT_BNCT = 0m;
                                        TT_PT = 0m;
                                        TT_TT = 0m;
                                        TT_BHYT = 0m;
                                        TT_Chietkhau_Chitiet = 0;
                                        //Lấy lại từ CSDL
                                        List<KcbThanhtoanChitiet> _LstChitiet = (from p in lstKcbThanhtoanChitiet_Tatca
                                                                                 where p.IdThanhtoan == IdThanhtoan
                                                                                 select p).ToList<KcbThanhtoanChitiet>();

                                        if (_LstChitiet.Count > 0)
                                        {
                                            foreach (KcbThanhtoanChitiet objChitietThanhtoan in _LstChitiet)
                                            {
                                                if (!Utility.Byte2Bool(objChitietThanhtoan.TrangthaiHuy))//Bỏ qua các bản ghi đã bị hủy
                                                {
                                                    if (!Utility.Byte2Bool(objChitietThanhtoan.TuTuc))
                                                        TT_BHYT += objChitietThanhtoan.BhytChitra * objChitietThanhtoan.SoLuong;
                                                    TT_Chietkhau_Chitiet += Utility.DecimaltoDbnull(objChitietThanhtoan.TienChietkhau, 0);
                                                    TT_PT += objChitietThanhtoan.PhuThu * objChitietThanhtoan.SoLuong;
                                                    if (Utility.Byte2Bool(objChitietThanhtoan.TuTuc))
                                                        TT_TT += objChitietThanhtoan.BnhanChitra * objChitietThanhtoan.SoLuong;
                                                    else
                                                        TT_BNCT += objChitietThanhtoan.BnhanChitra * objChitietThanhtoan.SoLuong;
                                                }
                                            }
                                            TT_BN += TT_PT + TT_BNCT + TT_TT;
                                            //Update lại tiền thanh toán
                                            new Update(KcbThanhtoan.Schema)
                                          .Set(KcbThanhtoan.Columns.TongTien).EqualTo(TT_BHYT + TT_BN)
                                          .Set(KcbThanhtoan.Columns.BnhanChitra).EqualTo(TT_BNCT)
                                          .Set(KcbThanhtoan.Columns.BhytChitra).EqualTo(TT_BHYT)
                                          .Set(KcbThanhtoan.Columns.PhuThu).EqualTo(TT_PT)
                                          .Set(KcbThanhtoan.Columns.TuTuc).EqualTo(TT_TT)
                                          .Where(KcbThanhtoan.Columns.IdThanhtoan).IsEqualTo(IdThanhtoan).Execute();
                                            //Update phiếu thu
                                            new Update(KcbPhieuthu.Schema)
                                          .Set(KcbPhieuthu.Columns.SoTien).EqualTo(TT_BN - TT_Chietkhau_Chitiet)
                                          .Set(KcbPhieuthu.Columns.SotienGoc).EqualTo(TT_BN)
                                          .Where(KcbPhieuthu.Columns.IdThanhtoan).IsEqualTo(IdThanhtoan).Execute();
                                        }

                                    }
                                }
                            }
                        }
                    }
                    scope.Complete();
                    return ActionResult.Success;
                }
            }
            catch (Exception exception)
            {
                log.Error("Ban ra loi Exception={0}", exception);
                return ActionResult.Error;
            }
        }
        public void Insert(short IdNhanvien,long IdBenhnhan,string MaLuotkham,DateTime NgayHuy,decimal SotienHuy,byte LoaiphieuHuy,string LydoHuy,DateTime NgayTao,string NguoiTao)
        {
            KcbLoghuy item = new KcbLoghuy();

            item.IdNhanvien = IdNhanvien;

            item.IdBenhnhan = IdBenhnhan;

            item.MaLuotkham = MaLuotkham;

            item.NgayHuy = NgayHuy;

            item.SotienHuy = SotienHuy;

            item.LoaiphieuHuy = LoaiphieuHuy;

            item.LydoHuy = LydoHuy;

            item.NgayTao = NgayTao;

            item.NguoiTao = NguoiTao;

            item.Save(UserName);
        }