public HttpResponseMessage GetInfoById(HttpRequestMessage request, int id)
 {
     return(CreateReponse(request, () =>
     {
         HttpResponseMessage response;
         var data = _daService.GetAllInfoById(id);
         response = request.CreateResponse(HttpStatusCode.OK, data);
         return response;
     }));
 }
        public HttpResponseMessage Created(HttpRequestMessage request, HangMucViewModel hangMucViewModel)
        {
            return(CreateReponse(request, () =>
            {
                if (!ModelState.IsValid)
                {
                    return request.CreateResponse(HttpStatusCode.BadRequest, ModelState.IsValid);
                }
                else
                {
                    var hangMuc = new HangMuc();
                    var HeSoNC = _heSoNhanCongService.GetHeSoKcn(hangMucViewModel.SoNguoiThucHien);
                    hangMuc.UpdateHangMuc(hangMucViewModel);
                    hangMuc.HesoKcn = HeSoNC.HeSoNcKcn;
                    var hangMucResponse = _hangMucService.Add(hangMuc);
                    _hangMucService.save();
                    if (hangMucResponse != null)
                    {
                        var donGiaDiemTT = 0;
                        var donGiaDiemGT = 0;
                        var hm = _hangMucService.GetHangMucById(hangMucResponse.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;
                        }

                        if (hangMucViewModel.ThamGia.Count() > 0)
                        {
                            List <ThamGia> listTG = new List <ThamGia>();
                            foreach (var item in hangMucViewModel.ThamGia)
                            {
                                if (hangMucResponse.LoaiHangMuc == 0)
                                {
                                    listTG.Add(new ThamGia()
                                    {
                                        IdHangMuc = hangMucResponse.ID,
                                        IdDuAn = hangMucResponse.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 = hangMucResponse.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, hangMucResponse.ID, hangMucResponse.LoaiHangMuc);
                            _thamGiaService.Save();

                            var duan = _duAnService.GetAllInfoById(hangMuc.IdDuAn);
                            var point = _thamGiaService.TotalPoint(hangMuc.IdDuAn, hangMuc.LoaiHangMuc);

                            //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;

                            if (point != 0)
                            {
                                donGiaDiemTT = Convert.ToInt32(q2 / point);
                            }

                            // 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;

                            if (point != 0)
                            {
                                donGiaDiemGT = Convert.ToInt32(g3 / point);
                            }

                            if (hangMuc.LoaiHangMuc == 0)
                            {
                                duan.TongDiemTT = point;
                                duan.DonGiaDiemTT = donGiaDiemTT;
                                _duAnService.Update(duan);
                            }
                            else
                            {
                                duan.TongDiemGT = point;
                                duan.DonGiaDiemGT = donGiaDiemGT;
                                _duAnService.Update(duan);
                            }

                            var Tg = _thamGiaService.GetByIdHm(hangMucResponse.ID, hangMucResponse.LoaiHangMuc);
                            List <ThamGia> listTGUp = new List <ThamGia>();
                            foreach (var item in Tg)
                            {
                                if (hangMucResponse.LoaiHangMuc == 0)
                                {
                                    listTGUp.Add(new ThamGia()
                                    {
                                        IdHangMuc = hangMucResponse.ID,
                                        IdDuAn = hangMucResponse.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
                                {
                                    listTGUp.Add(new ThamGia()
                                    {
                                        IdHangMuc = hangMucResponse.ID,
                                        IdDuAn = hangMucResponse.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(listTGUp, hangMucResponse.ID, hangMucResponse.LoaiHangMuc);
                            _thamGiaService.Save();
                        }
                    }
                    return request.CreateResponse(HttpStatusCode.Created, hangMucResponse);
                }
            }));
        }