public void ThemHangVaoChiTietHoaDon(DataTable tbThongTin) { txtKhachThanhToan.Text = "0"; string MaHang = tbThongTin.Rows[0]["MaHang"].ToString(); int IDHangHoa = Int32.Parse(tbThongTin.Rows[0]["ID"].ToString()); int MaHoaDon = tabControlSoHoaDon.ActiveTabIndex; var exitHang = DanhSachHoaDon[MaHoaDon].ListChiTietHoaDon.FirstOrDefault(item => item.IDHangHoa == IDHangHoa); if (exitHang != null) { int SoLuong = exitHang.SoLuong + int.Parse(txtSoLuong.Text); double ThanhTienOld = exitHang.ThanhTien; exitHang.SoLuong = SoLuong; exitHang.HinhAnh = tbThongTin.Rows[0]["HinhAnh"].ToString(); exitHang.DonGia = double.Parse(tbThongTin.Rows[0]["GiaBan"].ToString()); exitHang.GiaKyThuat = double.Parse(tbThongTin.Rows[0]["GiaBan"].ToString()); exitHang.TonKho = dtCapNhatTonKho.SoLuong_TonKho(IDHangHoa.ToString(), Session["IDKho"].ToString()); exitHang.ThanhTien = SoLuong * exitHang.GiaKyThuat; DanhSachHoaDon[MaHoaDon].TongTien += SoLuong * exitHang.DonGia - ThanhTienOld; DanhSachHoaDon[MaHoaDon].KhachCanTra = DanhSachHoaDon[MaHoaDon].TongTien + DanhSachHoaDon[MaHoaDon].TienSuaXe; //- DanhSachHoaDon[MaHoaDon].GiamGia; } else { ChiTietHoaDon cthd = new ChiTietHoaDon(); cthd.IDHangHoa = IDHangHoa; cthd.MaHang = MaHang; cthd.TonKho = dtCapNhatTonKho.SoLuong_TonKho(IDHangHoa.ToString(), Session["IDKho"].ToString()); cthd.TenHang = tbThongTin.Rows[0]["TenHangHoa"].ToString(); cthd.SoLuong = int.Parse(txtSoLuong.Text); cthd.DonViTinh = tbThongTin.Rows[0]["TenDonViTinh"].ToString(); cthd.DonGia = double.Parse(tbThongTin.Rows[0]["GiaBan"].ToString()); cthd.GiaKyThuat = double.Parse(tbThongTin.Rows[0]["GiaBan"].ToString()); cthd.GiaMua = double.Parse(tbThongTin.Rows[0]["GiaMua"].ToString()); cthd.HinhAnh = tbThongTin.Rows[0]["HinhAnh"].ToString(); cthd.ThanhTien = int.Parse(txtSoLuong.Text) * double.Parse(cthd.GiaKyThuat.ToString()); DanhSachHoaDon[MaHoaDon].ListChiTietHoaDon.Add(cthd); DanhSachHoaDon[MaHoaDon].SoLuongHang++; DanhSachHoaDon[MaHoaDon].TongTien += cthd.ThanhTien; DanhSachHoaDon[MaHoaDon].KhachCanTra = DanhSachHoaDon[MaHoaDon].TongTien + DanhSachHoaDon[MaHoaDon].TienSuaXe; } }
/// <summary> /// Thêm mới và cập nhật hàng hóa trong hóa đơn /// </summary> /// <param name="tbThongTin"></param> /// <param name="TrangThaiGiaSiHayLe"></param> public void ThemHangVaoChiTietHoaDon(DataTable tbThongTin, int TrangThaiGiaSiHayLe) { txtKhachThanhToan.Text = "0"; string MaHang = tbThongTin.Rows[0]["MaHang"].ToString(); int IDHangHoa = Int32.Parse(tbThongTin.Rows[0]["ID"].ToString()); int MaHoaDon = tabControlSoHoaDon.ActiveTabIndex; double DoDay = double.Parse(tbThongTin.Rows[0]["DoDay"].ToString()); var exitHang = DanhSachHoaDon[MaHoaDon].ListChiTietHoaDon.FirstOrDefault(item => item.IDHangHoa == IDHangHoa && item.TrangThaiGiaSiHayLe == TrangThaiGiaSiHayLe && item.DoDay == DoDay); if (exitHang != null) { // kiểm tra đơn giá double SoLuong = exitHang.SoLuong + double.Parse(txtSoLuong.Text); double ThanhTienOld = exitHang.ThanhTien; exitHang.SoLuong = SoLuong; if (TrangThaiGiaSiHayLe == 1) { // giá lẻ exitHang.DonGia = double.Parse(tbThongTin.Rows[0]["GiaBanLe"].ToString()); exitHang.TrangThaiGiaSiHayLe = 1; } else { // giá sỉ exitHang.DonGia = double.Parse(tbThongTin.Rows[0]["GiaBanSi"].ToString()); exitHang.TrangThaiGiaSiHayLe = 0; } exitHang.DoDay = double.Parse(tbThongTin.Rows[0]["DoDay"].ToString()); exitHang.TonKho = dtCapNhatTonKho.SoLuong_TonKho(IDHangHoa.ToString(), Session["IDKho"].ToString()); exitHang.ThanhTien = SoLuong * exitHang.DonGia; DanhSachHoaDon[MaHoaDon].TongTien += SoLuong * exitHang.DonGia - ThanhTienOld; DanhSachHoaDon[MaHoaDon].KhachCanTra = DanhSachHoaDon[MaHoaDon].TongTien; // -DanhSachHoaDon[MaHoaDon].GiamGia; DanhSachHoaDon[MaHoaDon].KhachThanhToan = DanhSachHoaDon[MaHoaDon].TongTien; // -DanhSachHoaDon[MaHoaDon].GiamGia; } else { ChiTietHoaDon cthd = new ChiTietHoaDon(); cthd.IDHangHoa = IDHangHoa; cthd.MaHang = MaHang; cthd.TonKho = dtCapNhatTonKho.SoLuong_TonKho(IDHangHoa.ToString(), Session["IDKho"].ToString()); cthd.TenHang = tbThongTin.Rows[0]["TenHangHoa"].ToString(); cthd.SoLuong = int.Parse(txtSoLuong.Text); if (TrangThaiGiaSiHayLe == 1) { // giá lẻ cthd.DonGia = double.Parse(tbThongTin.Rows[0]["GiaBanLe"].ToString()); cthd.TrangThaiGiaSiHayLe = 1; cthd.DonViTinh = tbThongTin.Rows[0]["TenDonViTinhLe"].ToString(); } else { // giá sỉ cthd.DonGia = double.Parse(tbThongTin.Rows[0]["GiaBanSi"].ToString()); cthd.TrangThaiGiaSiHayLe = 0; cthd.DonViTinh = tbThongTin.Rows[0]["TenDonViTinhSi"].ToString(); } cthd.ThanhTien = int.Parse(txtSoLuong.Text) * double.Parse(cthd.DonGia.ToString()); cthd.DoDay = double.Parse(tbThongTin.Rows[0]["DoDay"].ToString()); cthd.HeSo = double.Parse(tbThongTin.Rows[0]["HeSo"].ToString()); DanhSachHoaDon[MaHoaDon].ListChiTietHoaDon.Add(cthd); DanhSachHoaDon[MaHoaDon].SoLuongHang++; DanhSachHoaDon[MaHoaDon].TongTien += cthd.ThanhTien; DanhSachHoaDon[MaHoaDon].KhachCanTra = DanhSachHoaDon[MaHoaDon].TongTien; // -DanhSachHoaDon[MaHoaDon].GiamGia; DanhSachHoaDon[MaHoaDon].KhachThanhToan = DanhSachHoaDon[MaHoaDon].TongTien; // -DanhSachHoaDon[MaHoaDon].GiamGia; } }