private void UpdateData() { try { Cursor.Current = Cursors.WaitCursor; var db = new CkpEntities(); var bc = (from b in db.BaoCaoNgay where b.Id == BcId && b.TrangThai == 1 select b).FirstOrDefault(); var vtbc = (from v in db.ViTriBom_BaoCao where v.BaoCaoId == BcId select v); var nlbc = (from n in db.NhanLuc_BaoCao where n.BaoCaoId == BcId select n); var cul = CultureInfo.InvariantCulture; //Bao cao bc.NgayBaoCao = DateTime.ParseExact(NgayBcTb.Text, "ddMMyyyy", cul); bc.NguoiBaoCaoId = NvIds[NgBcCb.SelectedIndex]; bc.MayBomId = BomIds[BomCb.SelectedIndex]; bc.CongTrinhId = CtrIds[CtrCb.SelectedIndex]; bc.KhachHangId = KhIds[KhCb.SelectedIndex]; bc.GhiChu = GcRtb.Text; bc.NgaySuaCuoi = DateTime.Now; foreach (var vt in vtbc) { db.ViTriBom_BaoCao.Remove(vt); } foreach (var nl in nlbc) { db.NhanLuc_BaoCao.Remove(nl); } //Vi tri if (VtCb1.SelectedIndex > -1) { double dgv = 0; double.TryParse(DgStTb1.Text.Replace(",", ""), out dgv); double dg = 0; double.TryParse(DgTtTb1.Text.Replace(",", ""), out dg); var newVt = new ViTriBom_BaoCao { BaoCaoId = bc.Id, ViTriId = VtIds[VtCb1.SelectedIndex], Tang = int.Parse(TangTb1.Text), SoLuong = double.Parse(SlTb1.Text), DonVi = DvCb1.Text, DonGiaVat = dgv, DonGia = dg, GhiChu = GcTb1.Text, DonGiaId = DgCb1.SelectedIndex != -1 ? DgIds[DgCb1.SelectedIndex] : (int?)null }; db.ViTriBom_BaoCao.Add(newVt); } if (VtCb2.SelectedIndex > -1) { double dgv = 0; double.TryParse(DgStTb2.Text.Replace(",", ""), out dgv); double dg = 0; double.TryParse(DgTtTb2.Text.Replace(",", ""), out dg); var newVt = new ViTriBom_BaoCao { BaoCaoId = bc.Id, ViTriId = VtIds[VtCb2.SelectedIndex], Tang = int.Parse(TangTb2.Text), SoLuong = double.Parse(SlTb2.Text), DonVi = DvCb2.Text, DonGiaVat = dgv, DonGia = dg, GhiChu = GcTb2.Text, DonGiaId = DgCb2.SelectedIndex != -1 ? DgIds[DgCb2.SelectedIndex] : (int?)null }; db.ViTriBom_BaoCao.Add(newVt); } if (VtCb3.SelectedIndex > -1) { double dgv = 0; double.TryParse(DgStTb3.Text.Replace(",", ""), out dgv); double dg = 0; double.TryParse(DgTtTb3.Text.Replace(",", ""), out dg); var newVt = new ViTriBom_BaoCao { BaoCaoId = bc.Id, ViTriId = VtIds[VtCb3.SelectedIndex], Tang = int.Parse(TangTb3.Text), SoLuong = double.Parse(SlTb3.Text), DonVi = DvCb3.Text, DonGiaVat = dgv, DonGia = dg, GhiChu = GcTb3.Text, DonGiaId = DgCb3.SelectedIndex != -1 ? DgIds[DgCb3.SelectedIndex] : (int?)null }; db.ViTriBom_BaoCao.Add(newVt); } //Nhan luc for (int i = 0; i < NhanLucGrid.Rows.Count; i++) { var row = NhanLucGrid.Rows[i]; if (row.Cells[0].Value == null) { continue; } var nvid = row.Cells[0].Value.ToString().Split('.')[0]; var bcnl = new NhanLuc_BaoCao { BaoCaoId = bc.Id, NhanVienId = int.Parse(nvid), KhoiLuongBom = row.Cells[1].Value == null ? (double?)null : double.Parse(row.Cells[1].Value.ToString()), DonGiaKhoi = row.Cells[2].Value == null ? (int?)null : int.Parse(row.Cells[2].Value.ToString()) }; db.NhanLuc_BaoCao.Add(bcnl); } db.SaveChanges(); Cursor.Current = Cursors.Default; MessageBox.Show("Cập nhật dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); parentForm.UpdateSearchResult(); Close(); } catch (Exception ex) { Cursor.Current = Cursors.Default; MessageBox.Show(ex.GetBaseException().Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void OkBtn_Click(object sender, EventArgs e) { try { double ftemp = 0; int temp = 0; DateTime dt = DateTime.Now; if ((!int.TryParse(TangTb1.Text, out temp) && TangTb1.Text.Length > 0) || (!int.TryParse(TangTb2.Text, out temp) && TangTb2.Text.Length > 0) || (!int.TryParse(TangTb3.Text, out temp) && TangTb3.Text.Length > 0)) { MessageBox.Show("Tầng phải là số", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if ((!double.TryParse(SlTb1.Text, out ftemp) && SlTb1.Text.Length > 0) || (!double.TryParse(SlTb2.Text, out ftemp) && SlTb2.Text.Length > 0) || (!double.TryParse(SlTb3.Text, out ftemp) && SlTb3.Text.Length > 0)) { MessageBox.Show("Số khối/số ca phải là số", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if ((!double.TryParse(DgTtTb1.Text.Replace(",", ""), out ftemp) && DgTtTb1.Text.Length > 0) || (!double.TryParse(DgTtTb2.Text.Replace(",", ""), out ftemp) && DgTtTb2.Text.Length > 0) || (!double.TryParse(DgTtTb3.Text.Replace(",", ""), out ftemp) && DgTtTb3.Text.Length > 0)) { MessageBox.Show("Đơn giá trước thuế phải là số", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if ((!double.TryParse(DgStTb1.Text.Replace(",", ""), out ftemp) && DgStTb1.Text.Length > 0) || (!double.TryParse(DgStTb2.Text.Replace(",", ""), out ftemp) && DgStTb2.Text.Length > 0) || (!double.TryParse(DgStTb3.Text.Replace(",", ""), out ftemp) && DgStTb3.Text.Length > 0)) { MessageBox.Show("Đơn giá sau thuế phải là số", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (!DateTime.TryParseExact(NgayBcTb.Text, "ddMMyyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt)) { MessageBox.Show("Ngày báo cáo không đúng format ddMMyyyy", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } foreach (DataGridViewRow row in NhanLucGrid.Rows) { if (row.Cells[1].Value != null && !double.TryParse(row.Cells[1].Value.ToString(), out ftemp)) { MessageBox.Show("Số khối bơm phải là số", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (row.Cells[2].Value != null && !int.TryParse(row.Cells[2].Value.ToString(), out temp)) { MessageBox.Show("Lương/khối phải là số", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } if (BcId != 0) // if this is edit mode { UpdateData(); return; } if (NgayBcTb.Text.Length == 0 || BomCb.SelectedIndex == -1 || NgBcCb.SelectedIndex == -1 || CtrCb.SelectedIndex == -1) { if (NgayBcTb.Text.Length == 0) { NgayBcLbl.ForeColor = Color.Red; } else { NgayBcLbl.ForeColor = Color.Black; } if (BomCb.SelectedIndex == -1) { BomLbl.ForeColor = Color.Red; } else { BomLbl.ForeColor = Color.Black; } if (NgBcCb.SelectedIndex == -1) { NgBcLbl.ForeColor = Color.Red; } else { NgBcLbl.ForeColor = Color.Black; } if (CtrCb.SelectedIndex == -1) { CtrLbl.ForeColor = Color.Red; } else { CtrLbl.ForeColor = Color.Black; } } else { Cursor.Current = Cursors.WaitCursor; var db = new CkpEntities(); var cul = CultureInfo.InvariantCulture; //Bao cao var newReport = new BaoCaoNgay { NgayBaoCao = DateTime.ParseExact(NgayBcTb.Text, "ddMMyyyy", cul), NguoiBaoCaoId = NvIds[NgBcCb.SelectedIndex], MayBomId = BomIds[BomCb.SelectedIndex], CongTrinhId = CtrIds[CtrCb.SelectedIndex], KhachHangId = KhIds[KhCb.SelectedIndex], TrangThai = 1, GhiChu = GcRtb.Text, NgayTao = DateTime.Now, NgaySuaCuoi = DateTime.Now }; db.BaoCaoNgay.Add(newReport); //Vi tri if (VtCb1.SelectedIndex > -1) { double dgv = 0; double.TryParse(DgStTb1.Text.Replace(",", ""), out dgv); double dg = 0; double.TryParse(DgTtTb1.Text.Replace(",", ""), out dg); var newVt = new ViTriBom_BaoCao { BaoCaoId = newReport.Id, ViTriId = VtIds[VtCb1.SelectedIndex], Tang = TangTb1.Text.Length > 0 ? int.Parse(TangTb1.Text) : (int?)null, SoLuong = SlTb1.Text.Length > 0 ? double.Parse(SlTb1.Text) : (double?)null, DonVi = DvCb1.Text, DonGiaVat = dgv, DonGia = dg, GhiChu = GcTb1.Text, DonGiaId = DgCb1.SelectedIndex != -1 ? DgIds[DgCb1.SelectedIndex] : (int?)null }; db.ViTriBom_BaoCao.Add(newVt); } if (VtCb2.SelectedIndex > -1) { double dgv = 0; double.TryParse(DgStTb2.Text.Replace(",", ""), out dgv); double dg = 0; double.TryParse(DgTtTb2.Text.Replace(",", ""), out dg); var newVt = new ViTriBom_BaoCao { BaoCaoId = newReport.Id, ViTriId = VtIds[VtCb2.SelectedIndex], Tang = TangTb2.Text.Length > 0 ? int.Parse(TangTb2.Text) : (int?)null, SoLuong = SlTb2.Text.Length > 0 ? double.Parse(SlTb2.Text) : (double?)null, DonVi = DvCb2.Text, DonGiaVat = dgv, DonGia = dg, GhiChu = GcTb2.Text, DonGiaId = DgCb2.SelectedIndex != -1 ? DgIds[DgCb2.SelectedIndex] : (int?)null }; db.ViTriBom_BaoCao.Add(newVt); } if (VtCb3.SelectedIndex > -1) { double dgv = 0; double.TryParse(DgStTb3.Text.Replace(",", ""), out dgv); double dg = 0; double.TryParse(DgTtTb3.Text.Replace(",", ""), out dg); var newVt = new ViTriBom_BaoCao { BaoCaoId = newReport.Id, ViTriId = VtIds[VtCb3.SelectedIndex], Tang = TangTb3.Text.Length > 0 ? int.Parse(TangTb3.Text) : (int?)null, SoLuong = SlTb3.Text.Length > 0 ? double.Parse(SlTb3.Text) : (double?)null, DonVi = DvCb3.Text, DonGiaVat = dgv, DonGia = dg, GhiChu = GcTb3.Text, DonGiaId = DgCb3.SelectedIndex != -1 ? DgIds[DgCb3.SelectedIndex] : (int?)null }; db.ViTriBom_BaoCao.Add(newVt); } //Nhan luc for (int i = 0; i < NhanLucGrid.Rows.Count; i++) { var row = NhanLucGrid.Rows[i]; if (row.Cells[0].Value == null) { continue; } var nvid = row.Cells[0].Value.ToString().Split('.')[0]; var bcnl = new NhanLuc_BaoCao { BaoCaoId = newReport.Id, NhanVienId = int.Parse(nvid), KhoiLuongBom = row.Cells[1].Value == null ? (double?)null : double.Parse(row.Cells[1].Value.ToString()), DonGiaKhoi = row.Cells[2].Value == null ? (int?)null : int.Parse(row.Cells[2].Value.ToString()) }; db.NhanLuc_BaoCao.Add(bcnl); } db.SaveChanges(); } Cursor.Current = Cursors.Default; MessageBox.Show("Tạo báo cáo mới thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); Close(); } catch (Exception ex) { Cursor.Current = Cursors.Default; MessageBox.Show(ex.GetBaseException().Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); } }