private void btnLuuThongTin_Click(object sender, EventArgs e)
        {
            try
            {
                DOTHOC _dotHoc = new DOTHOC()
                {
                    CoSoId    = GlobalSettings.CoSoId,
                    LopHocId  = this.LopHocId,
                    TenDotHoc = txtTenLopHoc.Text,
                    HocPhi    = O2S_Common.TypeConvert.Parse.ToDecimal(numHocPhi.Text),
                    SoBuoiHoc = O2S_Common.TypeConvert.Parse.ToDecimal(numSoBuoiHoc.Text),
                };

                if (DotHocLogic.Insert(_dotHoc))
                {
                    O2S_Common.Utilities.ThongBao.frmThongBao frmthongbao = new O2S_Common.Utilities.ThongBao.frmThongBao(Base.ThongBaoLable.THEM_MOI_THANH_CONG);
                    frmthongbao.Show();
                    this.Close();
                }
                else
                {
                    O2S_Common.Utilities.ThongBao.frmThongBao frmthongbao = new O2S_Common.Utilities.ThongBao.frmThongBao(Base.ThongBaoLable.THEM_MOI_THAT_BAI);
                    frmthongbao.Show();
                }
            }
            catch (Exception ex)
            {
                O2S_Common.Logging.LogSystem.Error(ex);
            }
        }
        private void cboDotHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                DOTHOC _dothoc = DotHocLogic.SelectSingle(O2S_Common.TypeConvert.Parse.ToInt32(cboDotHoc.SelectedValue.ToString()));
                if (_dothoc != null)
                {
                    numHocPhi.Text    = O2S_Common.Number.Convert.NumberToString((_dothoc.HocPhi ?? 0), 0);
                    numSoTietHoc.Text = O2S_Common.Number.Convert.NumberToString((_dothoc.SoBuoiHoc ?? 0), 0);

                    numSoBuoiHVDangKy.Text = O2S_Common.Number.Convert.NumberToString((_dothoc.SoBuoiHoc ?? 0), 0);
                    //
                    decimal _sotiethoc = O2S_Common.TypeConvert.Parse.ToDecimal(numSoTietHoc.Text);
                    if (_sotiethoc != 0)
                    {
                        decimal _thanhtien = O2S_Common.TypeConvert.Parse.ToDecimal(numHocPhi.Text) * (O2S_Common.TypeConvert.Parse.ToDecimal(numSoBuoiHVDangKy.Text) / _sotiethoc);
                        lblThanhTienKhoaHoc.Text = O2S_Common.Number.Convert.NumberToString(_thanhtien, 0);
                    }
                    else
                    {
                        lblThanhTienKhoaHoc.Text = "0";
                    }
                }
                //numDaDong.Maximum = O2S_Common.TypeConvert.Parse.ToDecimal(numHocPhi.Text);

                numTongTien.Text = O2S_Common.Number.Convert.NumberToString(TinhTongTien(), 0);

                numConNo.Text = O2S_Common.Number.Convert.NumberToString((O2S_Common.TypeConvert.Parse.ToDecimal(numTongTien.Text) - O2S_Common.TypeConvert.Parse.ToDecimal(numDaDong.Text.Replace(",", ""))), 0);
            }
            catch (Exception ex)
            {
                O2S_Common.Logging.LogSystem.Warn(ex);
            }
        }
        private void cboDotHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                var    _dothocselect = (DOTHOC)cboDotHoc.SelectedItem;
                DOTHOC _dothoc       = DotHocLogic.SelectSingle(_dothocselect.DotHocId);
                if (_dothoc != null)
                {
                    numHocPhi.Text    = O2S_Common.Number.Convert.NumberToString((_dothoc.HocPhi ?? 0), 0);
                    numSoTietHoc.Text = O2S_Common.Number.Convert.NumberToString((_dothoc.SoBuoiHoc ?? 0), 0);

                    numSoBuoiHVDangKy.Text = O2S_Common.Number.Convert.NumberToString((_dothoc.SoBuoiHoc ?? 0), 0);
                    //
                    //decimal _sotiethoc = O2S_Common.TypeConvert.Parse.ToDecimal(numSoTietHoc.Text);
                    if (_dothoc.SoBuoiHoc != 0)
                    {
                        decimal _thanhtien = _dothoc.HocPhi ?? 0 * (O2S_Common.TypeConvert.Parse.ToDecimal(numSoBuoiHVDangKy.Text) / _dothoc.SoBuoiHoc ?? 1);
                        numTongTien.Text = O2S_Common.Number.Convert.NumberToString(_thanhtien, 0);
                        numDaDong.Text   = numTongTien.Text;
                        numConNo.Text    = "0";
                    }
                    else
                    {
                        numTongTien.Text = "0";
                        numDaDong.Text   = "0";
                        numConNo.Text    = "0";
                    }
                }
            }
            catch (Exception ex)
            {
                O2S_Common.Logging.LogSystem.Warn(ex);
            }
        }
 private void LoadDotHocTheoLopHoc(int _lophocId)
 {
     try
     {
         List <DOTHOC> _lstDotHoc = DotHocLogic.SelectTheoLopHoc(_lophocId);
         gridControlDSDotHoc.DataSource = _lstDotHoc;
     }
     catch (Exception ex)
     {
         O2S_Common.Logging.LogSystem.Error(ex);
     }
 }
        private void gridViewDSHocVien_Click(object sender, EventArgs e)
        {
            try
            {
                var rowHandle = gridViewDSHocVien.FocusedRowHandle;
                this.HocVienId_Select      = O2S_Common.TypeConvert.Parse.ToInt32(gridViewDSHocVien.GetRowCellValue(rowHandle, "HocVienId").ToString());
                this.PhieuGhiDanhId_Select = O2S_Common.TypeConvert.Parse.ToInt32(gridViewDSHocVien.GetRowCellValue(rowHandle, "PhieuGhiDanhId").ToString());

                PHIEUGHIDANH _phieugd = PhieuGhiDanhLogic.SelectSingle(this.PhieuGhiDanhId_Select);

                lblMaHocVien.Text     = _phieugd.HOCVIEN.MaHocVien;
                lblTenHocVien.Text    = _phieugd.HOCVIEN.TenHocVien;
                lblHocPhi.Text        = O2S_Common.Number.Convert.NumberToString(O2S_Common.TypeConvert.Parse.ToDecimal(_phieugd.TongTien.ToString()), 0);
                lblDaDong.Text        = O2S_Common.Number.Convert.NumberToString(O2S_Common.TypeConvert.Parse.ToDecimal(_phieugd.DaDong.ToString()), 0);
                lblConNo.Text         = O2S_Common.Number.Convert.NumberToString(O2S_Common.TypeConvert.Parse.ToDecimal(_phieugd.ConNo.ToString()), 0);
                lblMienGiam_Tien.Text = O2S_Common.Number.Convert.NumberToString(O2S_Common.TypeConvert.Parse.ToDecimal(_phieugd.MienGiam_Tien.ToString()), 0);
                numDaDong.Text        = _phieugd.ConNo.ToString();
                //Load danh sach phieu thu
                PhieuThuFilter _filter = new PhieuThuFilter();
                _filter.HocVienId      = this.HocVienId_Select;
                _filter.PhieuGhiDanhId = this.PhieuGhiDanhId_Select;
                LoadDanhSachPhieuThu(_filter);
                //Load dot hoc theo lop hoc
                if (_phieugd.LopHocId != null && _phieugd.LopHocId != 0)
                {
                    cboDotHoc.DataSource    = DotHocLogic.SelectDotHocChucDongTien(_phieugd.LopHocId ?? 0, this.PhieuGhiDanhId_Select);
                    cboDotHoc.DisplayMember = "TenDotHoc";
                    cboDotHoc.ValueMember   = "DotHocId";
                }
                else
                {
                    cboDotHoc.DataSource    = null;
                    cboDotHoc.DisplayMember = "TenDotHoc";
                    cboDotHoc.ValueMember   = "DotHocId";
                }
                btnInBienLai.Enabled = false;
                //if (_phieugd.ConNo == 0)
                //{
                //    btnLuuLai.Enabled = false;
                //    numDaDong.ReadOnly = true;
                //}
                //else
                //{
                //    btnLuuLai.Enabled = true;
                //    numDaDong.ReadOnly = false;
                //}
            }
            catch (Exception ex)
            {
                O2S_Common.Logging.LogSystem.Error(ex);
            }
        }
 private void LoadDotHocTheoLopHoc()
 {
     try
     {
         DotHocFilter _filter = new DotHocFilter();
         _filter.CoSoId          = GlobalSettings.CoSoId;
         _filter.LopHocId        = O2S_Common.TypeConvert.Parse.ToInt32(cboLopHoc.SelectedValue.ToString());
         cboDotHoc.DataSource    = DotHocLogic.Select(_filter);
         cboDotHoc.DisplayMember = "TenDotHoc";
         cboDotHoc.ValueMember   = "DotHocId";
     }
     catch (Exception ex)
     {
         O2S_Common.Logging.LogSystem.Warn(ex);
     }
 }
 private void repositoryItemButton_Xoa_Click(object sender, EventArgs e)
 {
     try
     {
         if (MessageBox.Show("Bạn có muốn xóa?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
         {
             var rowHandle = gridViewDSDotHoc.FocusedRowHandle;
             int _DotHocId = O2S_Common.TypeConvert.Parse.ToInt32(gridViewDSDotHoc.GetRowCellValue(rowHandle, "DotHocId").ToString());
             if (DotHocLogic.Delete(_DotHocId))
             {
                 O2S_Common.Utilities.ThongBao.frmThongBao frmthongbao = new O2S_Common.Utilities.ThongBao.frmThongBao(Base.ThongBaoLable.XOA_THANH_CONG);
                 frmthongbao.Show();
                 gridViewLopHoc_Click(null, null);
             }
         }
     }
     catch (Exception ex)
     {
         O2S_Common.Logging.LogSystem.Error(ex);
     }
 }
        private void btnLuuPhieu_Click(object sender, EventArgs e)
        {
            try
            {
                ValidateLuuPhieu();
                DOTHOC _dotHoc = null;
                if (cboDotHoc.SelectedValue != null)
                {
                    _dotHoc = DotHocLogic.SelectSingle(O2S_Common.TypeConvert.Parse.ToInt32(cboDotHoc.SelectedValue.ToString()));
                }
                //Insert bang PHIEUGHIDANH
                //insert bang PHIEUTHU; HOCPHIHOCVIEN
                //cap nhat bang HOCVIEN trang thai hoc vien = hoc vien chinh thuc + TAIKHOAN=chinh thuc
                PHIEUGHIDANH _phieughidanh = new PHIEUGHIDANH();
                _phieughidanh.HocVienId   = this.HocVienId_Select;
                _phieughidanh.KhoaHocId   = O2S_Common.TypeConvert.Parse.ToInt32(cboKhoaHoc.SelectedValue.ToString());
                _phieughidanh.LopHocId    = O2S_Common.TypeConvert.Parse.ToInt32(cboLopHoc.SelectedValue.ToString());
                _phieughidanh.NgayGhiDanh = DateTime.ParseExact(dateNgayGhiDanh.Text, "HH:mm:ss dd/MM/yyyy", CultureInfo.InvariantCulture);
                //tong tien cua lop hoc den thoi diem hien tai
                List <DOTHOC> _lstDotHoc = DotHocLogic.SelectTheoLopHoc(_phieughidanh.LopHocId ?? 0);
                _phieughidanh.HocPhiKH = _lstDotHoc != null?_lstDotHoc.Sum(o => o.HocPhi) : 0;

                _phieughidanh.SoTietKH = _lstDotHoc != null?_lstDotHoc.Sum(o => o.SoBuoiHoc) : 0;

                _phieughidanh.HocPhiHocVienKH   = O2S_Common.TypeConvert.Parse.ToDecimal(lblThanhTienKhoaHoc.Text.Replace(",", ""));
                _phieughidanh.SoTietHocVienKH   = O2S_Common.TypeConvert.Parse.ToDecimal(numSoBuoiHVDangKy.Text);
                _phieughidanh.ThuKhoanKhac      = TinhTongTien_ThuThem();
                _phieughidanh.TongTien          = O2S_Common.TypeConvert.Parse.ToDecimal(numTongTien.Text);
                _phieughidanh.DaDong            = O2S_Common.TypeConvert.Parse.ToDecimal(numDaDong.Text.Replace(",", ""));
                _phieughidanh.ConNo             = O2S_Common.TypeConvert.Parse.ToDecimal(numConNo.Text);
                _phieughidanh.MienGiam_PhanTram = O2S_Common.TypeConvert.Parse.ToInt16(numMienGiam_PTram.Value.ToString());
                _phieughidanh.MienGiam_Tien     = O2S_Common.TypeConvert.Parse.ToDecimal(numMienGiam_Tien.Text.Replace(",", ""));
                _phieughidanh.LyDoMienGiam      = txtLyDoMienGiam.Text;
                if (GlobalSettings.UserID != -1)
                {
                    _phieughidanh.NhanVienId = GlobalSettings.UserID;
                }

                //insert bang PHIEUTHU
                PHIEUTHU             _phieuthu = new PHIEUTHU();
                List <HOCPHIHOCVIEN> _lsthphv  = new List <HOCPHIHOCVIEN>();
                if (O2S_Common.TypeConvert.Parse.ToDecimal(numDaDong.Text.Replace(",", "")) > 0)
                {
                    //_phieuthuInsert.PhieuGhiDanhId = this.PhieuGhiDanhId;
                    _phieuthu.CoSoId      = GlobalSettings.CoSoId;
                    _phieuthu.HocVienId   = this.HocVienId_Select;
                    _phieuthu.ThoiGianThu = DateTime.Now;
                    _phieuthu.SoTien      = O2S_Common.TypeConvert.Parse.ToDecimal(numDaDong.Text.Replace(",", ""));
                    _phieuthu.NoiDung     = cboDotHoc.Text;
                    _phieuthu.GhiChu      = "Thu tiền đợt học: " + cboDotHoc.Text;
                    //Tien Dot Hoc
                    if (cboDotHoc.SelectedValue != null)
                    {
                        HOCPHIHOCVIEN _hphv_kh = new HOCPHIHOCVIEN()
                        {
                            Stt        = 1,
                            HocVienId  = this.HocVienId_Select,
                            DmDichVuId = O2S_Common.TypeConvert.Parse.ToInt32(cboDotHoc.SelectedValue.ToString()),
                            TenDichVu  = cboDotHoc.Text,
                            SoTien     = O2S_Common.TypeConvert.Parse.ToDecimal(lblThanhTienKhoaHoc.Text.Replace(",", "")),
                            SoLuong    = O2S_Common.TypeConvert.Parse.ToDecimal(numSoBuoiHVDangKy.Text.Replace(",", "")),
                            DonGia     = O2S_Common.TypeConvert.Parse.ToDecimal(numHocPhi.Text) / O2S_Common.TypeConvert.Parse.ToDecimal(numSoTietHoc.Text),
                            //PhieuThuId = _phieuthuId,
                            GhiChu = "",
                        };
                        _lsthphv.Add(_hphv_kh);
                    }
                    //tien khoan Khac
                    if (gridViewKhoanKhac.RowCount > 0)
                    {
                        _phieuthu.NoiDung += " và khoản khác";
                        _phieuthu.GhiChu  += " và khoản khác";
                        int _stt_thuthem = 2;
                        for (int i = 0; i < gridViewKhoanKhac.RowCount; i++)
                        {
                            object _noidungkt = gridViewKhoanKhac.GetRowCellValue(i, "noidung");
                            if (_noidungkt != null)
                            {
                                HOCPHIHOCVIEN _hphv_khac = new HOCPHIHOCVIEN()
                                {
                                    Stt        = _stt_thuthem,
                                    HocVienId  = this.HocVienId_Select,
                                    DmDichVuId = 0,
                                    TenDichVu  = gridViewKhoanKhac.GetRowCellValue(i, "noidung") == null ? "" : gridViewKhoanKhac.GetRowCellValue(i, "noidung").ToString(),
                                    SoTien     = O2S_Common.TypeConvert.Parse.ToDecimal(gridViewKhoanKhac.GetRowCellValue(i, "sotien").ToString()),
                                    SoLuong    = 1,
                                    DonGia     = O2S_Common.TypeConvert.Parse.ToDecimal(gridViewKhoanKhac.GetRowCellValue(i, "sotien").ToString()),
                                    //PhieuThuId = _phieuthuId,
                                    GhiChu = gridViewKhoanKhac.GetRowCellValue(i, "ghichu") == null ? "" : gridViewKhoanKhac.GetRowCellValue(i, "ghichu").ToString(),
                                };
                                _lsthphv.Add(_hphv_khac);
                                _stt_thuthem += 1;
                            }
                        }
                    }
                }
                //bang diem
                BANGDIEM _bangdiem = new BANGDIEM();
                _bangdiem.HocVienId = this.HocVienId_Select;
                _bangdiem.LopHocId  = O2S_Common.TypeConvert.Parse.ToInt32(cboLopHoc.SelectedValue.ToString());
                // _bangdiem.PhieuGhiDanhId = item.PhieuGhiDanhId ?? 0;
                _bangdiem.KhoaHocId = O2S_Common.TypeConvert.Parse.ToInt32(cboKhoaHoc.SelectedValue.ToString());
                _bangdiem.TrangThai = 0;//=0: xep lop; =1: dang hoc; =3: co diem; =99:ket thu

                if (PhieuGhiDanhLogic.InsertPGDFull_XepLop(_phieughidanh, _phieuthu, _lsthphv, _bangdiem, ref this.PhieuGhiDanhId, ref this.PhieuThuId))
                {
                    HOCVIEN _hv = HocVienLogic.SelectSingle(this.HocVienId_Select);
                    MessageBox.Show(string.Format("Học viên {0} đã được chuyển thành học viên chính thức với tài khoản:{1}Tên đăng nhập: {2}{3}Mật khẩu: {4}",
                                                  _hv.TenHocVien, Environment.NewLine, _hv.MaHocVien, Environment.NewLine, _hv.MaHocVien),
                                    "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    isSave = true;

                    btnLuuPhieu.Enabled = false;
                    LoadPhieuGhiDanh();

                    if (this.PhieuThuId != 0)
                    {
                        btnInBienLai.Enabled = true;
                        if (MessageBox.Show("Bạn có muốn in phiếu ghi danh vừa lưu?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            InBienLaiThuTien_Process(this.PhieuThuId);
                        }
                    }
                }
                else
                {
                    O2S_Common.Utilities.ThongBao.frmThongBao frmthongbao = new O2S_Common.Utilities.ThongBao.frmThongBao(Base.ThongBaoLable.THEM_MOI_THAT_BAI);
                    frmthongbao.Show();
                }
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message, "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void btnLuuLai_Click(object sender, EventArgs e)
        {
            try
            {
                ValidateLuu();
                //Update PHIEUGHIDANH: dadong
                PHIEUGHIDANH _phieughidanh = PhieuGhiDanhLogic.SelectSingle(this.PhieuGhiDanhId_Select);
                _phieughidanh.DaDong = _phieughidanh.DaDong + O2S_Common.TypeConvert.Parse.ToDecimal(numDaDong.Text);
                //
                _phieughidanh.SoTietKH        = _phieughidanh.SoTietKH + O2S_Common.TypeConvert.Parse.ToDecimal(numSoTietHoc.Text);//so tiet tren dot hoc da chon
                _phieughidanh.HocPhiKH        = _phieughidanh.HocPhiKH + O2S_Common.TypeConvert.Parse.ToDecimal(numHocPhi.Text);
                _phieughidanh.HocPhiHocVienKH = _phieughidanh.HocPhiHocVienKH + O2S_Common.TypeConvert.Parse.ToDecimal(numTongTien.Text);
                _phieughidanh.SoTietHocVienKH = _phieughidanh.SoTietHocVienKH + O2S_Common.TypeConvert.Parse.ToDecimal(numSoBuoiHVDangKy.Text);

                decimal _mienggiam = _phieughidanh.MienGiam_Tien ?? 0;
                _phieughidanh.ConNo = _phieughidanh.TongTien - _phieughidanh.DaDong - _mienggiam;


                //Insert Phieu Thu
                var      rowHandle = gridViewDSHocVien.FocusedRowHandle;
                PHIEUTHU _phieuthu = new PHIEUTHU();
                _phieuthu.CoSoId         = GlobalSettings.CoSoId;
                _phieuthu.PhieuGhiDanhId = this.PhieuGhiDanhId_Select;
                _phieuthu.HocVienId      = this.HocVienId_Select;
                _phieuthu.ThoiGianThu    = DateTime.Now;
                _phieuthu.SoTien         = O2S_Common.TypeConvert.Parse.ToDecimal(numDaDong.Text);
                _phieuthu.NoiDung        = cboDotHoc.Text;
                _phieuthu.GhiChu         = "Thu tiền đợt học: " + cboDotHoc.Text;
                //Hoc phi hoc vien
                DOTHOC _dothoc = DotHocLogic.SelectSingle(O2S_Common.TypeConvert.Parse.ToInt32(cboDotHoc.SelectedValue.ToString()));

                decimal _donGia = _dothoc.HocPhi ?? 0;
                if (_dothoc.SoBuoiHoc != null && _dothoc.SoBuoiHoc != 0)
                {
                    _donGia = _dothoc.HocPhi / _dothoc.SoBuoiHoc ?? 1;
                }

                HOCPHIHOCVIEN _hocphiHV = new HOCPHIHOCVIEN()
                {
                    PhieuGhiDanhId = this.PhieuGhiDanhId_Select,
                    HocVienId      = this.HocVienId_Select,
                    Stt            = 1,
                    DmDichVuId     = _dothoc.DotHocId,
                    TenDichVu      = cboDotHoc.Text,
                    SoTien         = O2S_Common.TypeConvert.Parse.ToDecimal(numTongTien.Text),
                    SoLuong        = O2S_Common.TypeConvert.Parse.ToDecimal(numSoBuoiHVDangKy.Text),
                    DonGia         = _donGia,
                    PhieuThuId     = 0,
                    GhiChu         = "",
                };
                if (PhieuGhiDanhLogic.InsertQLHocPhi_Option1(_phieughidanh, _phieuthu, _hocphiHV, ref this.PhieuThu_Insert))
                {
                    O2S_Common.Utilities.ThongBao.frmThongBao frmthongbao = new O2S_Common.Utilities.ThongBao.frmThongBao(Base.ThongBaoLable.LUU_THANH_CONG);
                    frmthongbao.Show();
                    //
                    LoadDanhSachHocVien();
                    gridViewDSHocVien_Click(null, null);

                    PhieuThuFilter _filter = new PhieuThuFilter();
                    _filter.HocVienId      = this.HocVienId_Select;
                    _filter.PhieuGhiDanhId = this.PhieuGhiDanhId_Select;
                    LoadDanhSachPhieuThu(_filter);

                    btnInBienLai.Enabled = true;
                }
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message, "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                O2S_Common.Logging.LogSystem.Error(ex);
            }
        }