Esempio n. 1
0
        private void dgvHoaDon_DoubleClick(object sender, EventArgs e)
        {
            string MaHoaDon = txtMaHDBan.Text;
            string MaHangXoa;
            float  ThanhTienXoa, SLXoa, SL, SLCon;
            double Tong, TongMoi;

            if (tblCTHDB.Rows.Count == 0)
            {
                MessageBox.Show("Không có dữ liệu!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if ((MessageBox.Show("Bạn có chắc chắn muốn xóa không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes))
            {
                //Xóa hàng và cập nhật lại số lượng hàng
                MaHangXoa    = dgvHDBanHang.CurrentRow.Cells["MaHang"].Value.ToString().Trim();
                SLXoa        = Convert.ToInt32(dgvHDBanHang.CurrentRow.Cells["SoLuong"].Value.ToString().Trim());
                ThanhTienXoa = float.Parse(dgvHDBanHang.CurrentRow.Cells["ThanhTien"].Value.ToString().Trim());
                DB.DeleteHoaDonChiTiet(txtMaHDBan.Text.Trim(), MaHangXoa);

                // Cập nhật lại số lượng cho các mặt hàng
                SL    = float.Parse(DB.GetDataFromHang(MaHangXoa, "GetSLHang") + string.Empty);
                SLCon = SL + SLXoa; // cộng số lượng hàng ban đầu vào kho
                DB.UpdateSoLuong(SLCon, MaHangXoa);

                Tong    = float.Parse(DB.GetDataFromHoaDon(txtMaHDBan.Text, "GetTongTienHD") + string.Empty);
                TongMoi = Tong - ThanhTienXoa;
                DB.UpdateTongTien(TongMoi, txtMaHDBan.Text);
                txtTongTien.Text = TongMoi.ToString();

                if (tblCTHDB.Rows.Count - 1 == 0)
                {
                    if ((MessageBox.Show("Hoá đơn trống, bạn có muốn xóa hóa đơn này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes))
                    {
                        DB.DeleteHoaDon(MaHoaDon);
                    }
                }
            }
            LoadDataGridView();
        }
Esempio n. 2
0
        private void btnLuu_Click(object sender, EventArgs e)
        {
            float  SLConLai, sl;
            double Tong, TongMoi;

            if (!CheckMaHoaDon())
            {
                // Mã hóa đơn chưa có, tiến hành lưu các thông tin chung
                // Mã HDBan được sinh tự động do đó không có trường hợp trùng khóa

                if (cboMaNhanVien.Text.Length == 0)
                {
                    MessageBox.Show("Bạn phải nhập nhân viên", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    cboMaNhanVien.Focus();
                    return;
                }
                if (cboMaKhachHang.Text.Length == 0)
                {
                    MessageBox.Show("Bạn phải nhập khách hàng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    cboMaKhachHang.Focus();
                    return;
                }
                HoaDon newHD = new HoaDon {
                    MaHoaDon   = txtMaHDBan.Text.Trim(),
                    MaNhanVien = cboMaNhanVien.Text.Trim(),
                    NgayBan    = dtpNgayBan.Value,
                    MaKhach    = cboMaKhachHang.Text.Trim(),
                    TongTien   = float.Parse(txtTongTien.Text.Trim())
                };
                DB.InsertHoaDon(newHD);
            }
            // Lưu thông tin của các mặt hàng
            if (cboMaHang.Text.Trim().Length == 0)
            {
                MessageBox.Show("Bạn phải nhập mã hàng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                cboMaHang.Focus();
                return;
            }
            if ((txtSoLuong.Text.Trim().Length == 0) || (txtSoLuong.Text == "0"))
            {
                MessageBox.Show("Bạn phải nhập số lượng", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSoLuong.Text = string.Empty;
                txtSoLuong.Focus();
                return;
            }
            if (txtGiamGia.Text.Trim().Length == 0)
            {
                MessageBox.Show("Bạn phải nhập giảm giá", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtGiamGia.Focus();
                return;
            }
            if (CheckMaHangHD())
            {
                MessageBox.Show("Mã hàng này đã có, bạn phải nhập mã khác", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ResetValuesHang();
                cboMaHang.Focus();
                return;
            }
            // Kiểm tra xem số lượng hàng trong kho còn đủ để cung cấp không?
            sl = float.Parse(DB.GetSoLuongBaseOnMaHangHD(cboMaHang.Text.Trim()));
            if (float.Parse(txtSoLuong.Text) > sl)
            {
                MessageBox.Show("Số lượng mặt hàng này chỉ còn " + sl, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSoLuong.Text = string.Empty;
                txtSoLuong.Focus();
                return;
            }
            HDChiTiet hdct = new HDChiTiet {
                MaHoaDon  = txtMaHDBan.Text.Trim(),
                MaHang    = cboMaHang.Text.Trim(),
                SoLuong   = float.Parse(txtSoLuong.Text.Trim()),
                DonGiaBan = float.Parse(txtDonGia.Text.Trim()),
                GiamGia   = float.Parse(txtGiamGia.Text.Trim()),
                ThanhTien = float.Parse(txtThanhTien.Text.Trim())
            };

            DB.InsertHDCT(hdct);
            LoadDataGridView();

            // Cập nhật lại số lượng của mặt hàng vào bảng tblHang
            SLConLai = sl - float.Parse(txtSoLuong.Text);
            DB.UpdateSoLuong(SLConLai, cboMaHang.Text);

            // Cập nhật lại tổng tiền cho hóa đơn bán//
            Tong    = float.Parse(DB.GetDataFromHoaDon(txtMaHDBan.Text, "GetTongTienHD") + string.Empty);
            TongMoi = Tong + float.Parse(txtThanhTien.Text);
            DB.UpdateTongTien(TongMoi, txtMaHDBan.Text);
            txtTongTien.Text = TongMoi.ToString();

            ResetValuesHang();
            btnXoa.Enabled        = true;
            btnThemHoaDon.Enabled = true;
        }
Esempio n. 3
0
        private void btnLuu_Click(object sender, EventArgs e)
        {
            string sql = "SELECT Sohoadonnhap FROM tblHDN WHERE Sohoadonnhap=N'" + txtMaHD.Text + "'";
            double sl, SLcon, tong, TongMoi;

            if (!Data.Checkey(sql))
            {
                //Mã hóa đơn chưa có, tiến hành lưu các thông tin chung
                //Mã HDN đc sinh ra tự động nên ko có trường hợp trùng khóa
                if (txtNgay.Text.Length == 0)
                {
                    MessageBox.Show("Bạn phải nhập ngày nhập hàng!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtNgay.Focus();
                    return;
                }

                if (cboManhanvien.Text.Length == 0)
                {
                    MessageBox.Show("Bạn phải nhập nhân viên", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    cboManhanvien.Focus();
                    return;
                }
                if (cboManhacungcap.Text.Length == 0)
                {
                    MessageBox.Show("Bạn phải nhập nhà cung cấp!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    cboManhacungcap.Focus();
                    return;
                }
                sql = "INSERT INTO HoaDonNhap(Sohoadonnhap,Manhanvien,NgayNhap,Manhacungcap,Tongtien)VALUES (N'" + txtMaHD.Text.Trim() + "',N'" + cboManhanvien.Text.ToString() + "','" + Data.ConvertDateTime(txtNgay.Text.Trim()) + "',N'" + cboManhacungcap.Text.Trim() + "'," + txtTongtien.Text + ")";
                Data.RunSql(sql);
            }
            //luu thong tin cac mat hang

            if (txtTennhacungcap.Text.Trim().Length == 0)
            {
                MessageBox.Show("Bạn phải nhập mã hàng!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtTennhacungcap.Focus();
                return;
            }
            if (txtSoluong.Text.Trim().Length == 0)
            {
                MessageBox.Show("Bạn phải nhập số lượng!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSoluong.Text = "";
                txtSoluong.Focus();
                return;
            }
            if (txtGiamgia.Text.Trim().Length == 0)
            {
                MessageBox.Show("Bạn phải nhập giảm giá!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtGiamgia.Focus();
                return;
            }

            sql = "SELECT Tennhacungcap FROM tblChiTietHDN WHERE Tennhacungcap =N'" + txtTennhacungcap.Text + "' AND Sohoadonnhap=N'" + txtMaHD.Text.Trim() + "'";

            if (Data.Checkey(sql))
            {
                MessageBox.Show("Mã hàng này đã có,bạn phải nhập mã khác!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ResetValuesHang();
                txtTennhacungcap.Focus();
                return;
            }

            sl  = Convert.ToDouble(Data.GetFieldValues("SELECT Soluong FROM tblDMHangHoa WHERE Tennhacungcap=N'" + txtTennhacungcap.Text + "'"));
            sql = "INSERT INTO tblChiTietHDN(Sohoadonnhap,Tennhacungcap,Soluong,Giamgia,Thanhtien)VALUES (N'" + txtMaHD.Text.Trim() + "',N'" + txtTennhacungcap.Text.ToString() + "',N'" + txtSoluong.Text + "," + txtGiamgia.Text + "," + txtThanhtien.Text + ")";
            Data.RunSql(sql);
            Load_DataGridView();

            //Cập nhật lại số lượng của mặt hàng vào bảng HangHoa

            SLcon = sl - Convert.ToDouble(txtSoluong.Text);
            sql   = "UPDATE tblDMHangHoa SET Soluong=" + SLcon + "WHERE Tennhacungcap=N'" + txtTennhacungcap.Text + "'";
            Data.RunSql(sql);

            //Cập nhật tổng tiền cho HDN

            tong    = Convert.ToDouble(Data.GetFieldValues("SELECT Tongtien FROM tblHDN WHERE Sohoadonnhap=N'" + txtMaHD.Text + "'"));
            TongMoi = tong + Convert.ToDouble(txtThanhtien.Text);
            sql     = "UPDATE tblHDN SET Tongtien=" + TongMoi + "WHERE Sohoadonnhap=N'" + txtMaHD.Text + "'";
            Data.RunSql(sql);
            txtTongtien.Text = TongMoi.ToString();
            lblBangchu.Text  = "Bằng chữ: " + Data.ChuyenSoSangChu(TongMoi.ToString());
            ResetValuesHang();
            btnXoa.Enabled  = true;
            btnIn.Enabled   = true;
            btnThem.Enabled = true;
        }