public HttpResponseMessage created(HttpRequestMessage request, DuAnViewModel duAnVM)
        {
            return(CreateReponse(request, () =>
            {
                HttpResponseMessage response;

                if (ModelState.IsValid)
                {
                    var newDuan = new DuAn();
                    newDuan.UpdateDuAn(duAnVM);
                    newDuan.Updated_at = null;
                    newDuan.NgayHoanThanh = null;
                    var model = _daService.Add(newDuan);
                    _daService.Save();
                    var responseData = Mapper.Map <DuAn, DuAnViewModel>(model);
                    response = request.CreateResponse(HttpStatusCode.Created, responseData);
                }
                else
                {
                    response = request.CreateResponse(HttpStatusCode.BadRequest, ModelState);
                }
                return response;
            }));
        }
        public HttpResponseMessage Update(HttpRequestMessage request, HangMucViewModel hangMucViewModel)
        {
            return(CreateReponse(request, () =>
            {
                HttpResponseMessage respose = null;
                if (!ModelState.IsValid)
                {
                    respose = request.CreateResponse(HttpStatusCode.BadRequest, ModelState.IsValid);
                }
                else
                {
                    var hangMuc = _hangMucService.getByID(hangMucViewModel.ID);
                    var HeSoNC = _heSoNhanCongService.GetHeSoKcn(hangMucViewModel.ThamGia.Count());
                    hangMuc.UpdateHangMuc(hangMucViewModel);
                    hangMuc.HesoKcn = HeSoNC.HeSoNcKcn;
                    _hangMucService.Update(hangMuc);
                    _hangMucService.save();
                    if (hangMuc != null)
                    {
                        var duan = _duAnService.GetAllInfoById(hangMuc.IdDuAn);
                        var point = _thamGiaService.TotalPoint(hangMuc.IdDuAn, hangMuc.LoaiHangMuc);

                        var hm = _hangMucService.GetHangMucById(hangMuc.ID);
                        decimal?diemHm = 0m;
                        if (hm.HeSoLap != null && hm.HeSoTg != null)
                        {
                            diemHm = hm.DiemDanhGia * hm.HeSoLap.Hesl * hm.HeSoTg.HeSoTgdk * hm.HesoKcn * hm.NhomCongViec.HeSoCV;
                        }

                        decimal?donGiaDiemTT = 0m;
                        decimal?donGiaDiemGT = 0m;
                        if (!point.Equals(0) && hangMucViewModel.ThamGia.Count() > 0)
                        {
                            foreach (var item in hangMucViewModel.ThamGia)
                            {
                                point = point + Math.Round((diemHm * item.HeSoThamGia) / 100 ?? 0);
                            }
                            if (hangMuc.LoaiHangMuc == 0)
                            {
                                //tính đơn giá điểm trục tiếp
                                var q0 = (duan.GiaTriHopDong * duan.TyLeTheoDT) / 100;
                                var q1 = q0 - duan.LuongThueNgoai;
                                var q2 = (q1 * duan.LuongTTQtt) / 100;
                                donGiaDiemTT = q2 / point;
                                duan.TongDiemTT = point;
                                duan.DonGiaDiemTT = donGiaDiemTT;
                                _duAnService.Update(duan);
                            }
                            else if (hangMuc.LoaiHangMuc == 1)
                            {
                                // tính đơn giá điểm gián tiếp
                                var g0 = (duan.GiaTriHopDong * duan.TyLeTheoDT) / 100;
                                var g1 = g0 - duan.LuongThueNgoai;
                                var g2 = (g1 * duan.LuongGTQgt) / 100;
                                var g3 = (g2 * duan.LuongGTV22) / 100;
                                donGiaDiemGT = g3 / point;
                                duan.TongDiemGT = point;
                                duan.DonGiaDiemGT = donGiaDiemGT;
                                _duAnService.Update(duan);
                            }
                            _duAnService.Save();
                        }

                        List <ThamGia> listTG = new List <ThamGia>();
                        if (hm.ThamGia.Count() > 0 || hangMucViewModel.ThamGia.Count() > 0)
                        {
                            foreach (var item in hangMucViewModel.ThamGia)
                            {
                                if (hangMuc.LoaiHangMuc == 0)
                                {
                                    listTG.Add(new ThamGia()
                                    {
                                        IdHangMuc = hangMuc.ID,
                                        IdDuAn = hangMuc.IdDuAn,
                                        IdNhanVien = item.IdNhanVien,
                                        HeSoThamGia = item.HeSoThamGia,
                                        LoaiHangMuc = item.LoaiHangMuc,
                                        DiemThanhVien = Math.Round((diemHm * item.HeSoThamGia) / 100 ?? 0),
                                        ThuNhap = donGiaDiemTT * ((diemHm * item.HeSoThamGia) / 100 ?? 0)
                                    });
                                }
                                else
                                {
                                    listTG.Add(new ThamGia()
                                    {
                                        IdHangMuc = hangMuc.ID,
                                        IdDuAn = hangMuc.IdDuAn,
                                        IdNhanVien = item.IdNhanVien,
                                        HeSoThamGia = item.HeSoThamGia,
                                        LoaiHangMuc = item.LoaiHangMuc,
                                        DiemThanhVien = Math.Round((diemHm * item.HeSoThamGia) / 100 ?? 0),
                                        ThuNhap = donGiaDiemGT * ((diemHm * item.HeSoThamGia) / 100 ?? 0)
                                    });
                                }
                            }
                            _thamGiaService.Add(listTG, hangMuc.ID, hangMuc.LoaiHangMuc);
                        }
                    }
                    _thamGiaService.Save();
                    respose = request.CreateResponse(HttpStatusCode.Accepted, hangMucViewModel);
                }

                return respose;
            }));
        }