internal static CODE_RESULT_RETURN Add(GiamGia sp)
 {
     using (var context = DBContext.GetContext())
     {
         try
         {
             //var trung = context.GiamGia.FirstOrDefault(ca => (ca.MaGiamGia != sp.MaGiamGia)
             //&&
             //    ((ca.NgayBatDau <= sp.NgayBatDau && ca.NgayKetThuc >= sp.NgayBatDau)
             //        || (ca.NgayBatDau <= sp.NgayKetThuc && ca.NgayKetThuc >= sp.NgayKetThuc)));
             //if (trung != null)
             //{
             //    return CODE_RESULT_RETURN.MaTrung;
             //}
             var n1 = context.GiamGia.FirstOrDefault(n => n.MaGiamGia == sp.MaGiamGia);
             if (n1 == null)
             {
                 context.GiamGia.Add(sp);
                 context.SaveChanges();
                 return CODE_RESULT_RETURN.ThanhCong;
             }
             return CODE_RESULT_RETURN.MaTrung;
         }
         catch
         {
             return CODE_RESULT_RETURN.ThatBai;
         }
     }
 }
 public static bool IsKhongRangBuoc(GiamGia g)
 {
     using (var db = DBContext.GetContext())
     {
         //var phieuThus = db.PhieuThu.FirstOrDefault(p => p.GiamGia.MaGiamGia == g.MaGiamGia);
         //if (phieuThus != null)
         //    return false;
         var khachLe = db.KhachLe.Include(p => p.GiamGia).FirstOrDefault(p => p.GiamGia.MaGiamGia == g.MaGiamGia);
         if (khachLe != null)
             return false;
         return true;
     }
 }
 internal static CODE_RESULT_RETURN Update(GiamGia hv)
 {
     using (var db = DBContext.GetContext())
     {
         var hvc = db.GiamGia.FirstOrDefault(h => h.MaGiamGia  == hv.MaGiamGia);
         if (hvc != null)
         {
             hvc.NgayCuoi = hv.NgayCuoi;
             hvc.NgayDau = hv.NgayDau;
             hvc.PhanTramGiam = hv.PhanTramGiam;
             hvc.TenGiamGia = hv.TenGiamGia;
             db.SaveChanges();
             return CODE_RESULT_RETURN.ThanhCong;
         }
         return CODE_RESULT_RETURN.ThatBai;
     }
 }
 internal static CODE_RESULT_RETURN Add(GiamGia sp)
 {
     using (var context = DBContext.GetContext())
     {
         try
         {
             var n1 = context.GiamGia.FirstOrDefault(n => n.MaGiamGia == sp.MaGiamGia);
             if (n1 == null)
             {
                 context.GiamGia.Add(sp);
                 context.SaveChanges();
                 return CODE_RESULT_RETURN.ThanhCong;
             }
             return CODE_RESULT_RETURN.MaTrung;
         }
         catch
         {
             return CODE_RESULT_RETURN.ThatBai;
         }
     }
 }
 public FrmGiamGiaEdit(GiamGia c = null)
 {
     InitializeComponent();
     ct = c;
     //txtTenGiamGia.Text = ct.TenGiamGia;
     //dtpNgayBatDau.Value = c.NgayBatDau;
     //dtpNgayKetThuc.Value = c.NgayKetThuc;
     if (ct != null)
     {
         txtMaGiamGia.Enabled = false;
         txtTenGiamGia.Text = ct.TenGiamGia;
         dtpNgayBatDau.Value = ct.NgayBatDau;
         dtpNgayKetThuc.Value = ct.NgayKetThuc;
         txtMaGiamGia.Text = ct.MaGiamGia;
         txtMaGiamGia.Enabled = false;
         txtPhanTram.Text = ct.PhanTramGiam.ToString();
         if (ct.NgayKetThuc.Date < DateTime.Now.Date)
             btnCapNhat.Enabled = false;
     }
     else
     {
         dtpNgayBatDau.Value = dtpNgayKetThuc.Value = new DateTime(1900, 1, 1);
     }
 }
        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();

            #region validate
                if (txtMaGiamGia.Text == "")
                {
                    errorProvider1.SetError(txtMaGiamGia, "Chưa nhập mã giảm giá");
                    txtMaGiamGia.Focus();
                    return;
                }

                if (txtTenGiamGia.Text == "")
                {
                    errorProvider1.SetError(txtTenGiamGia, "Chưa nhập tên giảm giá");
                    txtTenGiamGia.Focus();
                    return;
                }
                DateTime ngayBatDau = dtpNgayBatDau.Value, ngayKetThuc = dtpNgayKetThuc.Value;

                if (ngayBatDau.Year == 1900)
                {
                    errorProvider1.SetError(dtpNgayBatDau, "Ngày bắt đầu không hợp lệ");
                    dtpNgayBatDau.Focus();
                    return;
                }
                if (ngayKetThuc.Year == 1900)
                {
                    errorProvider1.SetError(dtpNgayKetThuc, "Ngày kết thúc không hợp lệ");
                    dtpNgayKetThuc.Focus();
                    return;
                }

                if (ngayKetThuc.Date < ngayBatDau.Date)
                {
                    errorProvider1.SetError(dtpNgayKetThuc, "Ngày kết thúc không được sớm hơn ngày bắt đầu");
                    dtpNgayKetThuc.Focus();
                    return;
                }
                if (ct == null)
                {
                    if (ngayBatDau.Date < DateTime.Now.Date)
                    {
                        errorProvider1.SetError(dtpNgayBatDau, "Ngày bắt đầu không được sớm hơn ngày hiện tại");
                        dtpNgayBatDau.Focus();
                        return;
                    }
                }
                else
                    if (ngayKetThuc.Date < DateTime.Now.Date)
                    {
                        errorProvider1.SetError(dtpNgayKetThuc, "Ngày kết thúc không được nhỏ hơn ngày hiện tại");
                        dtpNgayKetThuc.Focus();
                        return;
                    }

                if (txtPhanTram.Text == "")
                {
                    errorProvider1.SetError(txtPhanTram, "Chưa nhập phần trăm giảm giá");
                    txtPhanTram.Focus();
                    return;
                }
                int phanTram;
                if (!int.TryParse(txtPhanTram.Text, out phanTram) ||  phanTram <0 || phanTram > 100)
                {
                    errorProvider1.SetError(txtPhanTram, "Mã giảm giá phải là số từ 0 đến 100");
                    txtPhanTram.Focus();
                    return;
                }
                #endregion validate
            try{
                if (ct == null)
                {
                    var gg = new GiamGia()
                    {
                        MaGiamGia = txtMaGiamGia.Text.Trim(),
                        NgayBatDau = dtpNgayBatDau.Value,
                        NgayKetThuc = dtpNgayKetThuc.Value,
                        PhanTramGiam = phanTram,
                        TenGiamGia = txtTenGiamGia.Text.Trim()
                    };
                    var r = GiamGiaController.Add(gg);
                    switch (r)
                    {
                        case CODE_RESULT_RETURN.MaTrung:
                            errorProvider1.SetError(txtMaGiamGia, "Mã giảm giá này đã tồn tại");
                            txtMaGiamGia.Focus();
                            return;
                        case CODE_RESULT_RETURN.ThanhCong:
                            DialogResult = DialogResult.OK;
                            break;
                        case CODE_RESULT_RETURN.ThatBai:
                            DialogUtils.ShowError("Có lỗi khi tạo giảm giá");
                            return;
                    }
                }
                else
                {
                    //ct.MaGiamGia = txtMaGiamGia.Text.Trim();
                    ct.NgayBatDau = dtpNgayBatDau.Value;
                    ct.NgayKetThuc = dtpNgayKetThuc.Value;
                    ct.PhanTramGiam = phanTram;
                    ct.TenGiamGia = txtTenGiamGia.Text.Trim();
                    var r = GiamGiaController.Update(ct);
                    switch (r)
                    {

                        case CODE_RESULT_RETURN.ThanhCong:
                            DialogResult = DialogResult.OK;
                            break;
                        case CODE_RESULT_RETURN.ThatBai:
                            DialogUtils.ShowError("Có lỗi khi cập nhật giảm giá");
                            return;
                    }
                }
                Close();
            }
            catch
            {
                MessageBox.Show("Có lỗi xảy ra, vui lòng kiểm tra dữ liệu nhập vào");
            }
        }
 internal static CODE_RESULT_RETURN Update(GiamGia hv)
 {
     using (var db = DBContext.GetContext())
     {
         //var trung = db.GiamGia.FirstOrDefault(ca => (ca.MaGiamGia != sp.MaGiamGia)
         //  &&
         //      ((ca.NgayBatDau <= sp.NgayBatDau && ca.NgayKetThuc >= sp.NgayBatDau)
         //          || (ca.NgayBatDau <= sp.NgayKetThuc && ca.NgayKetThuc >= sp.NgayKetThuc)));
         //if (trung != null)
         //{
         //    return CODE_RESULT_RETURN.MaTrung;
         //}
         var hvc = db.GiamGia.FirstOrDefault(h => h.MaGiamGia == hv.MaGiamGia);
         if (hvc != null)
         {
             hvc.NgayKetThuc = hv.NgayKetThuc;
             hvc.NgayBatDau = hv.NgayBatDau;
             hvc.PhanTramGiam = hv.PhanTramGiam;
             hvc.TenGiamGia = hv.TenGiamGia;
             db.SaveChanges();
             return CODE_RESULT_RETURN.ThanhCong;
         }
         return CODE_RESULT_RETURN.ThatBai;
     }
 }