public HttpResponseMessage AddEPToGiuong(int id, [FromBody] KTX0020 EP)
 {
     using (var db = new DB())
     {
         result <KTX0020> rel = new result <KTX0020>();
         if (EP.KTX0002_ID == null)
         {
             var check = (from temp in db.KTX0020 where temp.KTX0020_ID == EP.KTX0020_ID select temp).FirstOrDefault();
             if (check != null)
             {
                 var ktx02 = db.KTX0002.SingleOrDefault(p => p.KTX0002_ID == id);
                 if (ktx02.trangthai == false)
                 {
                     var ktx1 = db.KTX0001.SingleOrDefault(p => p.KTX0001_ID == ktx02.KTX0001_ID);
                     check.khoaphong  = ktx1 != null ? ktx1.makhoa : "";
                     check.KTX0002_ID = id;
                     check.KTX0001_ID = ktx02.KTX0001_ID;
                     try
                     {
                         db.SaveChanges();
                         ktx02.trangthai = true;
                         db.SaveChanges();
                         check.MKV9999 = EP.MKV9999;
                         rel.set("OK", check, "Thành công");
                     }
                     catch (Exception d)
                     {
                         rel.set("ERR", EP, "Thất bại: " + d.Message);
                     }
                 }
                 else
                 {
                     rel.set("ERR", EP, "Thất bại: Giường này đã có người ở.");
                 }
             }
         }
         return(rel.ToHttpResponseMessage());
     }
 }
        public HttpResponseMessage edit([FromBody] KTX0020 values)
        {
            result <KTX0020> rel = new result <KTX0020>();

            using (DB db = new DB())
            {
                if (values.trangthai == true)
                {
                    rel.set("ERR", values, "Thất bại: Đơn đã duyệt, không thể sửa");
                }
                else
                {
                    var check = db.KTX0020.SingleOrDefault(p => p.KTX0020_ID == values.KTX0020_ID);
                    if (check != null)
                    {
                        try
                        {
                            values.ngaytaodon            = DateTime.Now;
                            values.trangthai             = false;
                            check.bengiao                = values.bengiao;
                            check.bennhan                = values.bennhan;
                            check.bietngoaingu           = values.bietngoaingu;
                            check.biettiengdantocitnguoi = values.biettiengdantocitnguoi;
                            check.bqlktx             = values.bqlktx;
                            check.choohiennay        = values.choohiennay;
                            check.chunhiemnoilamviec = values.chunhiemnoilamviec;
                            check.cmtnd_ngaycap      = values.cmtnd_ngaycap;
                            check.cmtnd_noicap       = values.cmtnd_noicap;
                            check.cmtnd_so           = values.cmtnd_so;
                            check.dantoc             = values.dantoc;
                            check.didong             = values.didong;
                            check.ghichu             = values.ghichu;
                            check.gioitinh           = values.gioitinh;
                            check.hotenkhac          = values.hotenkhac;
                            check.hotenkhaisinh      = values.hotenkhaisinh;
                            check.khoaphong          = values.khoaphong;
                            check.KTX0001_ID         = values.KTX0001_ID;
                            check.KTX0002_ID         = values.KTX0002_ID;
                            check.KTX0003_ID         = values.KTX0003_ID;
                            check.lamgiodautu14tuoi  = values.lamgiodautu14tuoi;
                            check.lydo                   = values.lydo;
                            check.lydodangkyoktx         = values.lydodangkyoktx;
                            check.lydonguyenvong         = values.lydonguyenvong;
                            check.MKV9999_ID             = values.MKV9999_ID;
                            check.ngaycohieuluc          = values.ngaycohieuluc;
                            check.ngayduyetdon           = values.ngayduyetdon;
                            check.ngayokitucxa           = values.ngayokitucxa;
                            check.ngayquaylaikytucxa     = values.ngayquaylaikytucxa;
                            check.ngaysinh               = values.ngaysinh;
                            check.ngaytaodon             = values.ngaytaodon;
                            check.nghenghiepchucvunoilam = values.nghenghiepchucvunoilam;
                            check.nguyenvongophongso     = values.nguyenvongophongso;
                            check.nharieng               = values.nharieng;
                            check.noidung                = values.noidung;
                            check.noisinh                = values.noisinh;
                            check.noithuongtru           = values.noithuongtru;
                            check.okitucxa               = values.okitucxa;
                            check.quaylaikytucxa         = values.quaylaikytucxa;
                            check.quequan                = values.quequan;
                            check.sokhoatu               = values.sokhoatu;
                            check.somayle                = values.somayle;
                            check.sotu                   = values.sotu;
                            check.thoigiantralantruoc    = values.thoigiantralantruoc;
                            check.tongiao                = values.tongiao;
                            check.trangthai              = values.trangthai;
                            check.trinhdchuyenmon        = values.trinhdchuyenmon;
                            check.trinhdohocvan          = values.trinhdohocvan;
                            check.capbac                 = values.capbac;
                            check.truongphongGA          = values.truongphongGA;
                            check.tienantoidanhhinhphat  = values.tienantoidanhhinhphat;
                            check.truongphongnoilamviec  = values.truongphongnoilamviec;
                            db.SaveChanges();
                            rel.set("OK", values, "Thành công");
                            db.KTX0021.RemoveRange(db.KTX0021.Where(p => p.KTX0020_ID == values.KTX0020_ID).ToArray());
                            db.SaveChanges();
                            foreach (var k in values.KTX0021)
                            {
                                db.KTX0021.Add(new KTX0021()
                                {
                                    batdau = k.batdau, choo = k.choo, ketthuc = k.ketthuc, KTX0020_ID = check.KTX0020_ID, nghenghiepnoilam = k.nghenghiepnoilam
                                });
                                db.SaveChanges();
                            }
                            db.KTX0022.RemoveRange(db.KTX0022.Where(p => p.KTX0020_ID == values.KTX0020_ID).ToArray());
                            db.SaveChanges();
                            foreach (var k in values.KTX0022)
                            {
                                db.KTX0022.Add(new KTX0022()
                                {
                                    ChoOHienNay = k.ChoOHienNay, HoTen = k.HoTen, KTX0020_ID = check.KTX0020_ID, NamSinh = k.NamSinh, NgheNghiep = k.NgheNghiep, QuanHe = k.QuanHe
                                });
                                db.SaveChanges();
                            }
                        }
                        catch (ContextMarshalException rr)
                        {
                            rel.set("ERR", values, "Thất bại: " + rr.Message);
                        }
                    }
                }
            }
            return(rel.ToHttpResponseMessage());
        }
 public HttpResponseMessage upload([FromBody] KTX0020[] valuess)
 {
     if (valuess == null)
     {
         return(null);
     }
     using (DB db = new DB())
     {
         var taisan = db.KTX0010.Where(p => p.trangthai == true && p.loai == 2 && p.soluongmacdinh > 0).Select(p => new
         {
             p.ghichu,
             p.giatien,
             p.KTX0010_ID,
             p.loai,
             p.soluongmacdinh,
             p.soluongfull,
             p.ten,
             p.thutu,
             p.trangthai,
             p.WH0007_ID,
             p.maSanPham,
         });
         results <KTX0020> list = new results <KTX0020>();
         valuess.ToList().ForEach(values =>
         {
             result <KTX0020> rel = new result <KTX0020>();
             try
             {
                 KTX0020 newktx0020 = new KTX0020()
                 {
                     KTX0020_ID             = values.KTX0020_ID,
                     MKV9999_ID             = values.MKV9999_ID,
                     ngaytaodon             = values.ngaytaodon,
                     trangthai              = values.trangthai,
                     trangthai2             = values.trangthai2,
                     bengiao                = values.bengiao,
                     bennhan                = values.bennhan,
                     bietngoaingu           = values.bietngoaingu,
                     biettiengdantocitnguoi = values.biettiengdantocitnguoi,
                     bqlktx             = values.bqlktx,
                     choohiennay        = values.choohiennay,
                     chunhiemnoilamviec = values.chunhiemnoilamviec,
                     cmtnd_ngaycap      = values.cmtnd_ngaycap,
                     cmtnd_noicap       = values.cmtnd_noicap,
                     cmtnd_so           = values.cmtnd_so,
                     dantoc             = values.dantoc,
                     didong             = values.didong,
                     ghichu             = values.ghichu,
                     ghichu2            = values.ghichu2,
                     gioitinh           = values.gioitinh,
                     hotenkhac          = values.hotenkhac,
                     hotenkhaisinh      = values.hotenkhaisinh,
                     khoaphong          = db.KTX0001.Where(p => p.ten == values.ghichu2).Select(p => p.makhoa).FirstOrDefault(),
                     KTX0001_ID         = db.KTX0001.Where(p => p.ten == values.ghichu2).Select(p => p.KTX0001_ID).FirstOrDefault(),
                     KTX0002_ID         = db.KTX0002.Where(p => p.KTX0001_ID == db.KTX0001.Where(pp => pp.ten == values.ghichu2).Select(pp => pp.KTX0001_ID).FirstOrDefault() && p.trangthai == false).Select(p => p.KTX0002_ID).FirstOrDefault(),
                     KTX0003_ID         = db.KTX0003.Where(p => p.MaKhoa == values.sokhoatu).Select(p => p.KTX0003_ID).FirstOrDefault(),
                     lamgiodautu14tuoi  = values.lamgiodautu14tuoi,
                     lydo                   = values.lydo,
                     lydodangkyoktx         = values.lydodangkyoktx,
                     lydonguyenvong         = values.lydonguyenvong,
                     ngaycohieuluc          = values.ngaycohieuluc,
                     ngayduyetdon           = values.ngayduyetdon,
                     ngayokitucxa           = values.ngayokitucxa,
                     ngayquaylaikytucxa     = values.ngayquaylaikytucxa,
                     ngaysinh               = values.ngaysinh,
                     nghenghiepchucvunoilam = values.nghenghiepchucvunoilam,
                     nguyenvongophongso     = values.nguyenvongophongso,
                     nharieng               = values.nharieng,
                     noidung                = values.noidung,
                     noisinh                = values.noisinh,
                     noithuongtru           = values.noithuongtru,
                     okitucxa               = values.okitucxa,
                     quaylaikytucxa         = values.quaylaikytucxa,
                     quequan                = values.quequan,
                     sokhoatu               = db.KTX0003.Where(p => p.KTX0001_ID == db.KTX0001.Where(pp => pp.ten == values.ghichu2).Select(pp => pp.KTX0001_ID).FirstOrDefault() && p.trangthai == false).Select(p => p.SoTu).FirstOrDefault(),
                     somayle                = values.somayle,
                     sotu                   = db.KTX0003.Where(p => p.KTX0001_ID == db.KTX0001.Where(pp => pp.ten == values.ghichu2).Select(pp => pp.KTX0001_ID).FirstOrDefault() && p.trangthai == false).Select(p => p.MaKhoa).FirstOrDefault(),
                     thoigiantralantruoc    = values.thoigiantralantruoc,
                     tongiao                = values.tongiao,
                     trinhdchuyenmon        = values.trinhdchuyenmon,
                     trinhdohocvan          = values.trinhdohocvan,
                     tienantoidanhhinhphat  = values.tienantoidanhhinhphat,
                     truongphongGA          = values.truongphongGA,
                     truongphongnoilamviec  = values.truongphongnoilamviec,
                     KTX0021                = values.KTX0021,
                     KTX0022                = values.KTX0022,
                     capbac                 = values.capbac
                 };
                 newktx0020.trangthai2 = false;
                 newktx0020.MKV9999    = null;
                 db.KTX0020.Add(newktx0020);
                 db.SaveChanges();
                 db.KTX0003.Where(p => p.KTX0003_ID == newktx0020.KTX0003_ID).ToList().ForEach(k =>
                 {
                     k.trangthai = true;
                 });
                 db.KTX0002.Where(p => p.KTX0002_ID == newktx0020.KTX0002_ID).ToList().ForEach(k =>
                 {
                     k.trangthai = true;
                 });
                 db.SaveChanges();
                 taisan.ToList().ForEach(val =>
                 {
                     var check = db.KTX0031.FirstOrDefault(p => p.KTX0010_ID == val.KTX0010_ID && p.MKV9999_ID == newktx0020.MKV9999_ID && p.trangthai == true);
                     if (check == null)
                     {
                         db.KTX0031.Add(new KTX0031()
                         {
                             ghichu     = "Thêm từ chức năng upload file excel",
                             KTX0010_ID = val.KTX0010_ID,
                             MKV9999_ID = newktx0020.MKV9999_ID,
                             ngaycap    = newktx0020.ngayduyetdon,
                             soluongcap = val.soluongmacdinh,
                             trangthai  = true,
                         });
                         try
                         {
                             //db.SaveChanges();
                         }
                         catch
                         {
                         }
                     }
                 });
                 values.KTX0020_ID = newktx0020.KTX0020_ID;
                 rel.set("OK", values, "Thành công");
             }
             catch (ContextMarshalException rr)
             {
                 rel.set("ERR", values, "Thất bại: " + rr.Message);
             }
             list.add(rel);
         }); db.SaveChanges();
         return(list.ToHttpResponseMessage());
     }
 }
        public HttpResponseMessage add([FromBody] KTX0020 values)
        {
            result <KTX0020> rel = new result <KTX0020>();

            using (DB db = new DB())
            {
                try
                {
                    KTX0020 newktx0020 = new KTX0020()
                    {
                        KTX0020_ID             = values.KTX0020_ID,
                        MKV9999_ID             = values.MKV9999_ID,
                        ngaytaodon             = values.ngaytaodon,
                        trangthai              = values.trangthai,
                        trangthai2             = values.trangthai2,
                        bengiao                = values.bengiao,
                        bennhan                = values.bennhan,
                        bietngoaingu           = values.bietngoaingu,
                        biettiengdantocitnguoi = values.biettiengdantocitnguoi,
                        bqlktx             = values.bqlktx,
                        choohiennay        = values.choohiennay,
                        chunhiemnoilamviec = values.chunhiemnoilamviec,
                        cmtnd_ngaycap      = values.cmtnd_ngaycap,
                        cmtnd_noicap       = values.cmtnd_noicap,
                        cmtnd_so           = values.cmtnd_so,
                        dantoc             = values.dantoc,
                        didong             = values.didong,
                        ghichu             = values.ghichu,
                        gioitinh           = values.gioitinh,
                        hotenkhac          = values.hotenkhac,
                        hotenkhaisinh      = values.hotenkhaisinh,
                        khoaphong          = values.khoaphong,
                        KTX0001_ID         = values.KTX0001_ID,
                        KTX0002_ID         = values.KTX0002_ID,
                        KTX0003_ID         = values.KTX0003_ID,
                        lamgiodautu14tuoi  = values.lamgiodautu14tuoi,
                        lydo                   = values.lydo,
                        lydodangkyoktx         = values.lydodangkyoktx,
                        lydonguyenvong         = values.lydonguyenvong,
                        ngaycohieuluc          = values.ngaycohieuluc,
                        ngayduyetdon           = values.ngayduyetdon,
                        ngayokitucxa           = values.ngayokitucxa,
                        ngayquaylaikytucxa     = values.ngayquaylaikytucxa,
                        ngaysinh               = values.ngaysinh,
                        nghenghiepchucvunoilam = values.nghenghiepchucvunoilam,
                        nguyenvongophongso     = values.nguyenvongophongso,
                        nharieng               = values.nharieng,
                        noidung                = values.noidung,
                        noisinh                = values.noisinh,
                        noithuongtru           = values.noithuongtru,
                        okitucxa               = values.okitucxa,
                        quaylaikytucxa         = values.quaylaikytucxa,
                        quequan                = values.quequan,
                        sokhoatu               = values.sokhoatu,
                        somayle                = values.somayle,
                        sotu                   = values.sotu,
                        thoigiantralantruoc    = values.thoigiantralantruoc,
                        tongiao                = values.tongiao,
                        trinhdchuyenmon        = values.trinhdchuyenmon,
                        trinhdohocvan          = values.trinhdohocvan,
                        tienantoidanhhinhphat  = values.tienantoidanhhinhphat,
                        truongphongGA          = values.truongphongGA,
                        truongphongnoilamviec  = values.truongphongnoilamviec,
                        KTX0021                = values.KTX0021,
                        KTX0022                = values.KTX0022,
                        capbac                 = values.capbac
                    };
                    newktx0020.ngaytaodon = DateTime.Now;
                    newktx0020.trangthai  = false;
                    newktx0020.trangthai2 = false;
                    newktx0020.MKV9999    = null;
                    db.KTX0020.Add(newktx0020);
                    db.SaveChanges();
                    values.KTX0020_ID = newktx0020.KTX0020_ID;
                    rel.set("OK", values, "Thành công");
                }
                catch (ContextMarshalException rr)
                {
                    rel.set("ERR", values, "Thất bại: " + rr.Message);
                }
            }
            return(rel.ToHttpResponseMessage());
        }