private void btnHoanThanh_Click(object sender, EventArgs e) { string _maChiTietSanPham; string _mauSac; string _soLuong; string _hinhAnh; string _maSanPham; if (DialogResult.Yes == MessageBox.Show("Bạn có muốn lưu hóa đơn không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information)) { try { //Lưu vào PhieuNhapKho string _maPhieuNhapKho = Helper.GetTimestamp(DateTime.Now); clsPhieuNhapKho_DTO phieuNhapKho = new clsPhieuNhapKho_DTO(_maPhieuNhapKho, Validation.LayMaNhanVien(), txtGhiChu.Text, DateTime.Now, 1, cboNCC.SelectedValue.ToString()); object resultPhieuNhapKho = clsPhieuNhapKho_BUS.ThemPhieuNhapKho(phieuNhapKho); if (resultPhieuNhapKho is bool) { if ((bool)(resultPhieuNhapKho)) { //Thành công //Vòng lặp thêm các sản phẩm trong dgvHDSanPham for (int i = 0; i < dgvHDSanPham.Rows.Count; i++) { _maChiTietSanPham = dgvHDSanPham.Rows[i].Cells["colMaChiTiet"].Value.ToString(); _mauSac = dgvHDSanPham.Rows[i].Cells["colMauSac"].Value.ToString(); _soLuong = dgvHDSanPham.Rows[i].Cells["colSoLuong"].Value.ToString(); _hinhAnh = dgvHDSanPham.Rows[i].Cells["colHinhAnh"].Value.ToString(); _maSanPham = dgvHDSanPham.Rows[i].Cells["colMaSanPham"].Value.ToString(); //kiem tra ton tai san pham ==> chua ton tai ==> thêm mới sản phẩm ==> thêm từng chi tiết ==> lưu hóa đơn nhập if (clsSanPham_BUS.KiemTonTaiSanPham(_maSanPham)) { /* sản phẩm đã tồn tại */ //kiem tra ton tai chi tiet trong db ==> update if (clsChiTietSanPham_BUS.KiemTraTonTaiMaCT(_maChiTietSanPham)) { /* chi tiết đã tồn tại */ //Lấy số lượng cũ để cập nhật int SoLuongSanPhamCu = 0; SqlDataReader dr = clsChiTietSanPham_BUS.LayChiTiet(_maSanPham, _mauSac.Trim()); if (dr != null) { while (dr.Read()) { SoLuongSanPhamCu = int.Parse(dr["SoLuong"].ToString()); } } SoLuongSanPhamCu += int.Parse(_soLuong); //Lưu chi tiết sản phẩm clsChiTietSP_DTO chiTietSanPham = new clsChiTietSP_DTO(_maChiTietSanPham, _mauSac, SoLuongSanPhamCu); object resultChiTietSP = clsChiTietSanPham_BUS.CapNhatSoLuong(chiTietSanPham); if (resultChiTietSP is bool) { if ((bool)resultChiTietSP) { } else { MessageBox.Show("Thêm chi tiết thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } else { MessageBox.Show((string)resultChiTietSP); return; } } else { /* không tồn tại chi tiết */ //tạo mới chi tiết ==> lưu ảnh vào database clsHinhAnh_DTO hinhAnh = new clsHinhAnh_DTO(_hinhAnh, clsHinhAnh_DTO.LoaiHinhAnh.Product); object resultHinhAnh = clsHinhAnh_BUS.ThemHinhAnh(hinhAnh); if (resultHinhAnh is bool) { //Lưu chi tiết sản phẩm mới clsChiTietSP_DTO chiTietSanPham = new clsChiTietSP_DTO(_maSanPham, _maChiTietSanPham, _mauSac, int.Parse(_soLuong), hinhAnh.MaHinhAnh, 1); object resultChiTietSP = clsChiTietSanPham_BUS.ThemChiTietSanPham(chiTietSanPham); if (resultChiTietSP is bool) { if ((bool)resultChiTietSP) { // lưu chi tiết thành công ==> copy hình string fileName = Path.GetFileName(_hinhAnh); string destPath = Directory.GetCurrentDirectory() + "\\data\\product\\" + fileName; File.Copy(_hinhAnh, destPath, true); } else { MessageBox.Show("Thêm chi tiết mới thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } else { MessageBox.Show((string)resultChiTietSP); return; } } else { MessageBox.Show((string)resultHinhAnh); return; } } } else { /* không tồn tại sản phẩm */ string _maSP = dgvHDSanPham.Rows[i].Cells["colMaSanPham"].Value.ToString(); string _tenSP = dgvHDSanPham.Rows[i].Cells["colTenCTSP"].Value.ToString(); string _thuongHieu = dgvHDSanPham.Rows[i].Cells["colThuongHieuu"].Value.ToString(); string _chatLieu = dgvHDSanPham.Rows[i].Cells["colChatLieuu"].Value.ToString(); int _giaVon = int.Parse(dgvHDSanPham.Rows[i].Cells["colGiaVonNhap"].Value.ToString().Replace(",", "")); int _giaBanLe = int.Parse(dgvHDSanPham.Rows[i].Cells["colGiaBanLe"].Value.ToString().Replace(",", "")); bool _chongNuoc = bool.Parse(dgvHDSanPham.Rows[i].Cells["colChongNuoc"].Value.ToString()); float _trongLuong = float.Parse(dgvHDSanPham.Rows[i].Cells["colKichCo"].Value.ToString()); string _maDanhMuc = dgvHDSanPham.Rows[i].Cells["colMauMaa"].Value.ToString(); int _namBH = int.Parse(dgvHDSanPham.Rows[i].Cells["colNamBaoHanh"].Value.ToString()); //Thêm sản phẩm clsSanPham_DTO sanPham = new clsSanPham_DTO(_maSP, _tenSP, _thuongHieu, _chatLieu, _giaVon, _giaBanLe, _chongNuoc, _trongLuong, _maDanhMuc, _namBH); object resultSanPham = clsSanPham_BUS.ThemSanPham(sanPham); if (resultSanPham is bool) { if ((bool)resultSanPham) { //thêm chi tiết sản phẩm vừa thêm clsHinhAnh_DTO hinhAnh = new clsHinhAnh_DTO(_hinhAnh, clsHinhAnh_DTO.LoaiHinhAnh.Product); object resultHinhAnh = clsHinhAnh_BUS.ThemHinhAnh(hinhAnh); if (resultHinhAnh is bool) { if ((bool)(resultHinhAnh)) { //Lưu chi tiết sản phẩm mới clsChiTietSP_DTO chiTietSanPham = new clsChiTietSP_DTO(_maSanPham, _maChiTietSanPham, _mauSac, int.Parse(_soLuong), hinhAnh.MaHinhAnh, 1); object resultChiTietSP = clsChiTietSanPham_BUS.ThemChiTietSanPham(chiTietSanPham); if (resultChiTietSP is bool) { if ((bool)resultChiTietSP) { // Copy image file vào folder data/product string fileName = Path.GetFileName(_hinhAnh); string destPath = Directory.GetCurrentDirectory() + "\\data\\product\\" + fileName; File.Copy(_hinhAnh, destPath, true); } else { MessageBox.Show("Thêm chi tiết mới thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } else { MessageBox.Show((string)resultChiTietSP); return; } } else { MessageBox.Show("Thêm hình ảnh mới thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } else { MessageBox.Show((string)resultHinhAnh); return; } } else { MessageBox.Show("Thêm sản phẩm thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } else { MessageBox.Show((string)resultSanPham); return; } } //Lưu ChiTietNhapKho clsChiTietPhieuNhapKho_DTO chiTiet = new clsChiTietPhieuNhapKho_DTO(Helper.GetTimestamp(DateTime.Now), _maChiTietSanPham, int.Parse(_soLuong), _maPhieuNhapKho); object resultChiTiet = clsChiTietPhieuNhapKho_BUS.ThemChiTietPhieuNhapKho(chiTiet); if (resultChiTiet is bool) { if ((bool)(resultChiTiet)) { //MessageBox.Show("Đã lưu vào chi tiết hóa đơn", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); _KiemTraThemChiTiet = true; } else { _KiemTraThemChiTiet = false; MessageBox.Show("Thêm chi tiết thất bại, vui lòng kiểm tra lại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } else { MessageBox.Show((string)resultChiTiet); return; } }//end for } else { MessageBox.Show("Thêm thất bại"); return; } } else { MessageBox.Show((string)resultPhieuNhapKho); return; } //lưu thành công if (_KiemTraThemChiTiet) { loadSanPham(); lblSoLuongSP.Text = "0"; lblTongTien.Text = "0"; txtCTMaSP.Text = null; txtCTTenSP.Text = null; btnThemMoi.Enabled = false; EnableChiTiet(false); EnableSanPham(true); EnableThongTinHD(false); RefreshChiTietSanPham(); RefreshHoaDon(); RefreshSanPham(); dgvHDSanPham.Rows.Clear(); MessageBox.Show("Lưu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Lưu thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (IOException msg) { MessageBox.Show(msg.Message); } catch (Exception msg) { MessageBox.Show(msg.Message); } } }
private void btnXacNhan_Click(object sender, EventArgs e) { if (khachHang == null) { // Khách hàng mới khachHang = new clsKhachHang_DTO(txtTenKH.Text, txtSDT.Text, Helper.GetTimestamp(DateTime.Now)); clsKhachHang_BUS.Them(khachHang); } hoaDonHienTai = new clsHoaDon_DTO( Helper.GetTimestamp(DateTime.Now), Validation.LayMaNhanVien(), string.IsNullOrWhiteSpace(txtGhiChu.Text) ? "Không" : txtGhiChu.Text, double.Parse(lblGiamTru.Text), double.Parse(lblThanhTien.Text), khachHang, khuyenMai); if (clsHoaDon_BUS.Them(hoaDonHienTai)) { // Chi tiet hoa don foreach (DataRow row in dtSanPham.Rows) { clsKhuyenMai_DTO km = new clsKhuyenMai_DTO ( row["TenKhuyenMai"].ToString(), row["GiamTru"].ToString(), false, null, null, 1, int.Parse(row["MaKhuyenMai"].ToString()) ); clsChiTietHD_DTO cthd = new clsChiTietHD_DTO ( Helper.GetTimestamp(DateTime.Now), row["MaCTSP"].ToString(), double.Parse(row["GiamTru"].ToString()), double.Parse(row["TongTien"].ToString()), int.Parse(row["SoLuong"].ToString()), km, hoaDonHienTai ); if (!clsChiTietHD_BUS.Them(cthd)) { MessageBox.Show("Đã xảy ra lỗi khi tạo chi tiết hóa đơn, vui lòng thử lại hoặc liên hệ Admin để xử lý."); // TODO: Xóa hóa đơn đã tạo ở trên return; } else { // TODO: Giam so luong ton kho trong database if (!clsChiTietSanPham_BUS.CapNhatSoLuong(cthd.MaCTSP, cthd.SoLuong)) { MessageBox.Show("Đã xảy ra lỗi khi cập nhật số lượng, vui lòng thử lại hoặc liên hệ Admin để xử lý."); return; } } int milliseconds = 100; Thread.Sleep(milliseconds); } DialogResult result = MessageBox.Show("Tạo hóa đơn thành công, bạn có muốn In ngay bây giờ?", "Thông báo", MessageBoxButtons.OKCancel); if (result == DialogResult.OK) { In(); } btnIn.Enabled = true; XacNhanHoaDon(false); } else { MessageBox.Show("Thêm hóa đơn thất bại."); } }