//thêm sản phẩm vào hóa đơn bán
        public void ThemSanPhamHoaDonBan(ChiTietHDBan cthd)
        {
            db.ChiTietHDBans.InsertOnSubmit(cthd);
               db.SubmitChanges();
              /*
            //string mahd= cthd.MaHD.ToString().Trim();

             string manv = cthd.MaNV.ToString().Trim();
            string masp=cthd.MaSP.ToString().Trim();
               //  int soluong =Convert.ToInt32( cthd.SoLuongBan);
            string soluong = cthd.SoLuongBan.ToString();
               //  float cuocphivc = Convert.ToInt64(cthd.CuocPhiVanChuyen);
            string cuocphivc = cthd.CuocPhiVanChuyen.ToString();
              // float cuocphivc = cthd.CuocPhiVanChuyen;
            string makh = cthd.MaKH.ToString().Trim();
              // float thanhtien = Convert.ToInt64(cthd.ThanhTien);
            string thanhtien = cthd.ThanhTien.ToString();
              //  MessageBox.Show(""+soluong);
              // db.ExecuteQuery<ChiTietHDBan>("Insert into ChiTietHDBan values('HD005','SP001','15','NV002','0','KH004','15000')");
            //  MessageBox.Show("Ma HD "+cthd.MaHD+"/t Ma SP "+cthd.MaSP+"Thanh tiên "+cthd.ThanhTien+"\t Ma NV "+cthd.MaNV+"Ma KH "+cthd.MaKH);
            db.ExecuteQuery<ChiTietHDBan>("Insert into ChiTietHDBan values('mahd','masp','soluong','manv','cuocphivc','makh','thanhtien')");
               db.SubmitChanges();*/
        }
 partial void DeleteChiTietHDBan(ChiTietHDBan instance);
 partial void UpdateChiTietHDBan(ChiTietHDBan instance);
 partial void InsertChiTietHDBan(ChiTietHDBan instance);
		private void detach_ChiTietHDBans(ChiTietHDBan entity)
		{
			this.SendPropertyChanging();
			entity.SanPham = null;
		}
		private void attach_ChiTietHDBans(ChiTietHDBan entity)
		{
			this.SendPropertyChanging();
			entity.NhanVien = this;
		}
Esempio n. 7
0
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (dgvChiTietHD.RowCount <= 0)
            {
                MessageBox.Show("Không có dữ liệu để lưu!!!", "Thông báo");
            }
            else
            {

              _KHBLL=new KhachHangBLL();
              _HDBLL=new HoaDonBLL();
              // MessageBox.Show("Kdasdas    " + _KHBLL.KiemTraKhachHang(cbMaKH.Text.Trim()));
                //nếu khách hàng không tồn tại thì thêm khách hàng
                if (_KHBLL.KiemTraKhachHang(cbMaKH.Text.Trim()) == false)
                {
                    KhachHang kh = new KhachHang();
                    kh.MaKH = cbMaKH.Text.Trim();
                    kh.TenKH = txtTenKH.Text.Trim();
                    kh.SoDTKH = txtSoDTKH.Text.Trim();
                    kh.DiaChiKH = txtDiaChiKH.Text.Trim();
                    _KHBLL.ThemKhachHang(kh);
                    //MessageBox.Show("Thêm khách hàng thành công","Thông báo");
                    HienThiCBMaKH();

                }

                HoaDon hd = new HoaDon();
                kiemtra = 0;
                hd.MaHD = txtMaHD.Text.Trim();
                hd.LoaiHD = "HDB";
                double TongTien = 0;
                for (int j = 0; j < i; j++)
                {
                  TongTien+= Convert.ToDouble( dgvChiTietHD.Rows[j].Cells["ThanhTien"].Value.ToString().Trim());
                }
                hd.TongTien = TongTien +Convert.ToInt64( txtCuocPhiVC.Text.ToString().Trim());
                hd.NgayLapPhieu = DateTime.Now.ToString("dd-MM-yyyy hh:mm");
                _HDBLL.ThemHoaDonBan(hd);

                _CTHDBBLL = new ChiTietHDBanBLL();
                 //thêm chi tiết hóa đơn vào cơ sở dữ liệu

               // List< ChiTietHDBan> lstcthdban=new List<ChiTietHDBan>();
                ChiTietHDBan cthdban;
                _CTHDNBLL = new ChiTietHDNhapBLL();
                    for (int j = 0; j < i; j++)
                    {

                        cthdban = new ChiTietHDBan();
                        cthdban.STT = _CTHDBBLL.DemChiTietHDB();
                        cthdban.MaHD = txtMaHD.Text.Trim();
                      //  MessageBox.Show("ffffff " + j);

                        cthdban.MaNV = txtMaNV.Text.Trim();
                        cthdban.MaKH = cbMaKH.Text.Trim();
                        cthdban.MaSP = dgvChiTietHD.Rows[j].Cells["MaSP"].Value.ToString().Trim();

                        cthdban.SoLuongBan =Convert.ToInt32(dgvChiTietHD.Rows[j].Cells["SoLuongBan"].Value.ToString().Trim());
                       // cthdban.SoLuongBan =Convert.ToString( dgvChiTietHD.Rows[j].Cells["SoLuongBan"].Value.ToString().Trim());
                        cthdban.CuocPhiVanChuyen = Convert.ToInt64(dgvChiTietHD.Rows[j].Cells["CuocPhiVanChuyen"].Value.ToString().Trim());
                        //cthdban.MaKH = dgvChiTietHD.Rows[j].Cells["MaKH"].Value.ToString().Trim();

                        cthdban.ThanhTien = Convert.ToInt64(dgvChiTietHD.Rows[j].Cells["ThanhTien"].Value.ToString().Trim());
                        //lstcthdban.Add(cthdban);
                        //TongTienHDB +=Convert.ToDouble( cthdban.ThanhTien);
                        if (cthdban.SoLuongBan <= _CTHDNBLL.LaySoLuongTonKho(cthdban.MaSP))
                        {
                            _CTHDBBLL.ThemSanPhamHoaDonBan(cthdban);
                            _CTHDNBLL.CapNhapTonKho(cthdban.MaSP, cthdban.SoLuongBan);
                        }
                        else
                        {
                            MessageBox.Show(cthdban.MaSP.ToString() + " Số lượng không đủ","Thông báo");
                            kiemtra++;
                        }

                     }
                   // _CTHDBBLL.ThemSanPhamHoaDonBan(lstcthdban);

                    //MessageBox.Show("Tong tien HD "+txtMaHD.ToString()+"= "+TongTienHDB);

                    if(kiemtra == 0)
                        MessageBox.Show("Lưu thành công", "Thông báo");
                    LayMaHoaDonHienTai();
                    grThonTinKhachHang.Enabled = true;
                    dgvChiTietHD.Rows.Clear();
                    i = 0;
                    lbTongTien.Text = "Tổng Tiền: ";
                    HienThiDGVSanPhamBan();
            }
        }