// Cập nhập thông tin lô hàng và chi tiết hóa đơn public bool CapNhatThongTinLoHang(List<string> dsMaLh, string slBan, string maHd) { double soLuongBan = Double.Parse(slBan); if (dsMaLh.Count == 1) { connection.ThucHienCauLenhSQL("UPDATE LoHang SET SLBan=SLBan + " + soLuongBan + " WHERE LoHang.MaLH='" + dsMaLh[0] + "'"); DTO_CTHoaDon temp_cthd = new DTO_CTHoaDon(maHd, dsMaLh[0], soLuongBan.ToString()); this._cthd.ThemCTHoaDon(temp_cthd); } if (dsMaLh.Count > 1) { for (int i = 0; i < dsMaLh.Count; i++) { if (i < (dsMaLh.Count - 1)) { DataTable hangTon = connection.GetDataTable("SELECT SLNhap, SLBan FROM LoHang WHERE MaLH=N'" + dsMaLh[i] + "')"); soLuongBan -= Double.Parse(hangTon.Rows[0]["SLNhap"].ToString()) - Double.Parse(hangTon.Rows[0]["SLBan"].ToString()); connection.ThucHienCauLenhSQL("UPDATE LoHang SET SLBan=SLNhap WHERE LoHang.MaLH='" + dsMaLh[i] + "'"); DTO_CTHoaDon temp_cthd = new DTO_CTHoaDon(maHd, dsMaLh[i], (Double.Parse(hangTon.Rows[0]["SLNhap"].ToString()) - Double.Parse(hangTon.Rows[0]["SLBan"].ToString())).ToString()); this._cthd.ThemCTHoaDon(temp_cthd); } else { connection.ThucHienCauLenhSQL("UPDATE LoHang SET SLBan=SLBan + " + soLuongBan + " WHERE LoHang.MaLH='" + dsMaLh[i] + "'"); DTO_CTHoaDon temp_cthd = new DTO_CTHoaDon(maHd, dsMaLh[0], soLuongBan.ToString()); this._cthd.ThemCTHoaDon(temp_cthd); } } } return true; #region old algorithsm //DataTable dttenMh = connection.GetDataTable("SELECT TenMH FROM MatHang WHERE MaMH=N'" + maMh + "')"); //string tenMh = dttenMh.Rows[0]["TenMH"].ToString(); //DataTable hangTon = LayMotMatHangTon(maMh); //double soLuongTon = Double.Parse(hangTon.Rows[0]["SLNhap"].ToString()) - Double.Parse(hangTon.Rows[0]["SLBan"].ToString()); //while (soLuongBan > 0) //{ // if (soLuongTon >= soLuongBan) // { // double tempNum = soLuongBan + Double.Parse(hangTon.Rows[0]["SLBan"].ToString()); // connection.ThucHienCauLenhSQL("UPDATE LoHang SET SLBan=" + tempNum + // "WHERE LoHang.MaLH='" + hangTon.Rows[0]["MaLH"] + "'"); // soLuongBan = 0; // dsMaLh.Add(hangTon.Rows[0]["MaLH"].ToString()); // break; // } // else // { // connection.ThucHienCauLenhSQL("UPDATE LoHang SET SLBan=" + hangTon.Rows[0]["SLNhap"] + // "WHERE LoHang.MaLH='" + hangTon.Rows[0]["MaLH"] + "'"); // soLuongBan -= soLuongTon; // dsMaLh.Add(hangTon.Rows[0]["MaLH"].ToString()); // hangTon = LayMotMatHangTon(maMh); // } //} //return true; #endregion }
/** * Thêm mới chi tiết hóa đơn bán hàng * @DTO_CTHoaDon: ctHoaDon */ public bool ThemCTHoaDon(DTO_CTHoaDon ctHoaDon) { connection.ThucHienCauLenhSQL("INSERT INTO ChiTietHoaDon (MaHD, MaLH, SLBan) " + "VALUES (N'" + ctHoaDon.MaHd + "',N'" + ctHoaDon.MaLh + "', " + ctHoaDon.SlBan + ")"); return true; }
// Cập nhật thông tin lô hàng và chi tiết hóa đơn public bool CapNhatThongTinLoHangVaCthd(string maMh, string slBan, string maHd) { //List<string> dsMaLh = new List<string>(); double soLuongBan = Double.Parse(slBan); DataTable dttenMh = connection.GetDataTable("SELECT TenMH FROM MatHang WHERE MaMH=N'" + maMh + "'"); string tenMh = dttenMh.Rows[0]["TenMH"].ToString(); DataTable hangTon = LayMotMatHangTon(maMh); double soLuongTon = Double.Parse(hangTon.Rows[0]["SLNhap"].ToString()) - Double.Parse(hangTon.Rows[0]["SLBan"].ToString()); while (soLuongBan > 0) { if (soLuongTon >= soLuongBan) { connection.ThucHienCauLenhSQL("UPDATE LoHang SET SLBan=SLBan + " + soLuongBan + " WHERE LoHang.MaLH='" + hangTon.Rows[0]["MaLH"].ToString() + "'"); DTO_CTHoaDon temp_cthd = new DTO_CTHoaDon(maHd, hangTon.Rows[0]["MaLH"].ToString(), soLuongBan.ToString()); this._cthd.ThemCTHoaDon(temp_cthd); soLuongBan = 0; //dsMaLh.Add(hangTon.Rows[0]["MaLH"].ToString()); break; } else { connection.ThucHienCauLenhSQL("UPDATE LoHang SET SLBan=SLNhap" + " WHERE LoHang.MaLH='" + hangTon.Rows[0]["MaLH"].ToString() + "'"); DTO_CTHoaDon temp_cthd = new DTO_CTHoaDon(maHd, hangTon.Rows[0]["MaLH"].ToString(), (Double.Parse(hangTon.Rows[0]["SLNhap"].ToString()) - Double.Parse(hangTon.Rows[0]["SLBan"].ToString())).ToString()); this._cthd.ThemCTHoaDon(temp_cthd); soLuongBan -= soLuongTon; //dsMaLh.Add(hangTon.Rows[0]["MaLH"].ToString()); hangTon = LayMotMatHangTon(maMh); soLuongTon = Double.Parse(hangTon.Rows[0]["SLNhap"].ToString()) - Double.Parse(hangTon.Rows[0]["SLBan"].ToString()); } } return true; }