public bool Save(Rent newRent, string _WHCode) { int TempId = dc.CreateRent(newRent, _WHCode); if (TempId > 0) { return true; } return false; }
public IList<Rent> getByRentsCode(string _rentCode) { IList<Rent> rents = new List<Rent>(); Rent rent = null; try { Conn = db.openConnAccess(); sb = new StringBuilder(); sb.Remove(0, sb.Length); sb.Append(" SELECT ID,RentCode,PRCode,PCompany,PAddress,PPhone,PObjective,DateRent,DateReturn FROM tbRent "); sb.Append(" WHERE (RentCode='" + _rentCode + "')"); string sql; sql = sb.ToString(); com = new OleDbCommand(); com.CommandText = sql; com.CommandType = CommandType.Text; com.Connection = Conn; dr = com.ExecuteReader(); if (dr.HasRows) { DataTable dt = new DataTable(); dt.Load(dr); int index = 1; foreach (DataRow drw in dt.Rows) { rent = new Rent(); rent.ID = Convert.ToInt32(drw["ID"].ToString()); rent.Index = Convert.ToString(index); rent.RentCode = drw["RentCode"].ToString(); rent.PRCode = drw["PRCode"].ToString(); rent.PCompany = drw["PCompany"].ToString(); rent.PAddress = drw["PAddress"].ToString(); rent.PPhone = drw["PPhone"].ToString(); rent.PObjective = drw["PObjective"].ToString(); // IList<RentDetail> rentDetails = this.getRentOrderDetailByRentCode(drw["RentCode"].ToString()); // rent.rentDetails = rentDetails; rent.DateRent = Convert.ToDateTime(drw["DateRent"].ToString()); rent.DateReturn = Convert.ToDateTime(drw["DateReturn"].ToString()); rents.Add(rent); } } dr.Close(); } catch (Exception ex) { dr.Close(); Conn.Close(); return null; throw ex; } finally { Conn.Close(); } return rents; }
public int CreateRent(Rent newRent,string _whCode) { int result = -1; try { Conn = db.openConnAccess(); tr = Conn.BeginTransaction(); sb = new StringBuilder(); sb.Remove(0, sb.Length); sb.Append("INSERT INTO tbRent(RentCode,PRCode,PCompany,PAddress,PPhone,PObjective,DateRent,DateReturn)"); sb.Append(" VALUES (@RentCode,@PRCode,@PCompany,@PAddress,@PPhone,@PObjective,@DateRent,@DateReturn)"); string sqlSave; sqlSave = sb.ToString(); com = new OleDbCommand(); com.Connection = Conn; com.CommandText = sqlSave; com.Transaction = tr; com.Parameters.Clear(); com.Parameters.Add("@RentCode", OleDbType.VarChar).Value = newRent.RentCode; com.Parameters.Add("@PRCode", OleDbType.VarChar).Value = newRent.PRCode; com.Parameters.Add("@PCompany", OleDbType.VarChar).Value = newRent.PCompany; com.Parameters.Add("@PAddress", OleDbType.VarChar).Value = newRent.PAddress; com.Parameters.Add("@PPhone", OleDbType.VarChar).Value = newRent.PPhone; com.Parameters.Add("@PObjective", OleDbType.VarChar).Value = newRent.PObjective; string rentDate = String.Format("{0:dd/MM/yyyy}", newRent.DateRent); string returnDate = String.Format("{0:dd/MM/yyyy}", newRent.DateReturn); com.Parameters.Add("@DateRent", OleDbType.Date).Value = rentDate; com.Parameters.Add("@DateReturn", OleDbType.Date).Value = returnDate; com.ExecuteNonQuery(); foreach (RentDetail r in newRent.rentDetails) { sb.Remove(0, sb.Length); sb.Append("INSERT INTO tbRentDetail(RentCode,PCode,PName,UCode,UName,NumberRent,NumberReturn,Physical,Penalty)"); sb.Append(" VALUES (@RentCode,@PCode,@PName,@UCode,@UName,@NumberRent,@NumberReturn,@Physical,@Penalty)"); string sqlSaveDetail; sqlSaveDetail = sb.ToString(); com = new OleDbCommand(); com.Connection = Conn; com.CommandText = sqlSaveDetail; com.Transaction = tr; com.Parameters.Clear(); com.Parameters.Add("@RentCode", OleDbType.VarChar).Value = r.RentCode; com.Parameters.Add("@PCode", OleDbType.VarChar).Value = r.PCode; com.Parameters.Add("@PName", OleDbType.VarChar).Value = r.PName; com.Parameters.Add("@UCode", OleDbType.VarChar).Value = r.UCode; com.Parameters.Add("@UName", OleDbType.VarChar).Value = r.UName; com.Parameters.Add("@NumberRent", OleDbType.VarChar).Value = r.NumberRent; com.Parameters.Add("@NumberReturn", OleDbType.VarChar).Value = "0"; com.Parameters.Add("@Physical", OleDbType.VarChar).Value = ""; com.Parameters.Add("@Penalty", OleDbType.VarChar).Value = "0"; com.ExecuteNonQuery(); ProductOnWareHouse pOnwarehouse = serviceProductOnWarhose.getByPCodeAndWHCode(r.PCode, _whCode); if (pOnwarehouse != null) { Product p = serviceProduct.getByCode(r.PCode); pOnwarehouse.UCode = p.UCode; Unit u = serviceUnit.getByCode(p.UCode); pOnwarehouse.QtyWithUnit = u.QtyUnit; int oldblance = pOnwarehouse.RealQty; int newbalnce = oldblance - r.NumberRent; pOnwarehouse.RealQty = newbalnce; int tmp = serviceProductOnWarhose.UpdateProductOnWareHouse(pOnwarehouse); if (tmp > 0) { Console.WriteLine("update pOnwarehouse Complate"); } } } tr.Commit(); result = 1; } catch (Exception ex) { tr.Rollback(); Conn.Close(); return result; throw ex; } finally { Conn.Close(); } return result; }
private void cmdSave_Click(object sender, EventArgs e) { if (txtPRCode.Text.Trim() == "") { MessageBox.Show("กรุณากรอกชื่อผู้ยืมด้วย !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information); txtPRCode.Focus(); return; } int count = lsvProductList.Items.Count; if (count == 0) { MessageBox.Show("กรุณาเลือกสินค้าด้วย !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information); txtSearchProduct.Focus(); return; } DateTime start = dtpRent.Value; DateTime end = dtpReturn.Value; if (end < start) { MessageBox.Show("วันคืนสินค้าต้องมากกว่าหรือเท่ากับวันยืมสินค้าด้วย !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information); dtpReturn.Select(); return; } if (MessageBox.Show("คุณต้องการเพิ่มการยืมใหม่ ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { try { Rent rent = serviceRent.getByRentCode(txtRN.Text.Trim()); if (rent == null) { Rent newRent = new Rent(); newRent.RentCode = txtRN.Text.Trim(); newRent.PRCode = txtPRCode.Text.Trim(); newRent.PCompany = txtPCompany.Text.Trim(); newRent.PAddress = txtPAddress.Text.Trim(); newRent.PPhone = txtPPhone.Text.Trim(); newRent.PObjective = txtPObjective.Text.Trim(); newRent.DateRent = dtpRent.Value; newRent.DateReturn = dtpReturn.Value; IList<RentDetail> rentDetails = new List<RentDetail>(); RentDetail rentDetail; int j = 1; for (int i = 0; i <= lsvProductList.Items.Count - 1; i++) { rentDetail = new RentDetail(); rentDetail.Index = Convert.ToString(j); rentDetail.RentCode = txtRN.Text.Trim(); rentDetail.PCode = lsvProductList.Items[i].SubItems[0].Text.Replace(",", ""); rentDetail.PName = lsvProductList.Items[i].SubItems[1].Text.Replace(",", ""); Product product = serviceProduct.getByCode(lsvProductList.Items[i].SubItems[0].Text.Replace(",", "")); rentDetail.UCode = product.UCode; rentDetail.UName = lsvProductList.Items[i].SubItems[2].Text.Replace(",", ""); rentDetail.NumberRent = Convert.ToInt32(lsvProductList.Items[i].SubItems[3].Text.Replace(",", "")); rentDetails.Add(rentDetail); j++; } newRent.rentDetails = rentDetails; bool save = serviceRent.Save(newRent, cboWarehouse.SelectedValue.ToString()); if (save) { MessageBox.Show("เพิ่มการยืมสินค้า เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information); ClearProduct(); txtRN.Text = ""; lsvProductList.Items.Clear(); txtSearchProduct.Focus(); ClearPersonRent(); txtRN.Text = serviceRent.getRentCode(); } else { MessageBox.Show("ไม่สามารถ เพิ่มการยืมสินค้าใหม่ได้!!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { MessageBox.Show("มีเลขที่เอกสารการยืมนี้อยู่แล้ว!!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { MessageBox.Show("ไม่สามารถ เพิ่มการยืมสินค้าได้ เนื่องจาก !!! : " + ex.Message, "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }