public string Save(CheckBill CheckBill, string userName) { //主表id为0 新增一条记录 此时子表数据会自动插入 if (CheckBill.Id == Guid.Parse("00000000-0000-0000-0000-000000000000")) { CheckBill.Id = Guid.NewGuid(); //生成一个id CheckBill.BillCode = GetBillCode("PD"); //生成单号 if (CheckBill.BillCode == "no") { CheckBill.BillCode = GetBillCode("PD"); //再次生成单号 } CheckBill.BillState = 1; //保存状态 CheckBill.MakePerson = userName; CheckBill.CreateDate = DateTime.Now; foreach (var item in CheckBill.CheckDetail) //生成子表id { item.Id = Guid.NewGuid(); //重置子表Id item.CreateDate = DateTime.Now; item.State = 1; item.MainTableId = CheckBill.Id; } CurrentDal.AddEntity(CheckBill); } //主表有id 说明是修改的 else { //删除原子表数据 List <CheckDetail> list = CurrentDBSession.CheckDetailDal.LoadEntities(a => a.MainTableId == CheckBill.Id).ToList(); foreach (CheckDetail item in list) { CurrentDBSession.CheckDetailDal.DeleteEntity(item); } //添加子表数据 foreach (CheckDetail item in CheckBill.CheckDetail) { item.Id = Guid.NewGuid(); //设置子表id item.MainTableId = CheckBill.Id; //设置主表id item.CreateDate = DateTime.Now; item.State = 1; CurrentDBSession.CheckDetailDal.AddEntity(item); } //修改主表数据 CheckBill.MakePerson = userName; CheckBill.CreateDate = DateTime.Now; CurrentDal.EditEntity(CheckBill); } //保存成功返回单据Id return(CurrentDBSession.SaveChanges() ? CheckBill.Id.ToString() : "no"); }
//获取表单数据 public ActionResult GetData() { string str = Request.Params["CheckBillId"];//单号 //如果新单据 没有数据 if (string.IsNullOrEmpty(str)) { return(Json(new CheckBill()));//返回一个新建的空对象 } //如果有数据 Guid CheckBillId = new Guid(Request["CheckBillId"]); //单据编号 CheckBill bill = CheckBillService.LoadEntities(t => t.Id == CheckBillId).FirstOrDefault(); //获取表单 return(Json(bill)); }
//保存数据 public ActionResult Save(CheckBill CheckBill) { string res = CheckBillService.Save(CheckBill, Session["UserName"].ToString()); return(Content(res)); }
//保存 private void tsbSave_Click(object sender, EventArgs e) { gridView1.UpdateCurrentRow(); if (dateTimePicker1.Text == "") { this.ShowAlertMessage("请选择日期!"); return; } if (cboDepot.Text == "") { this.ShowAlertMessage("必须选择仓库!"); return; } if (cboHandlePerson.Text == "") { this.ShowAlertMessage("必须选择经手人!"); return; } if (gridView1.RowCount <= 0) { this.ShowAlertMessage("必须增加货品明细数据!"); return; } bool boolvalue = false; for (int i = 0; i < gridView1.RowCount; i++) { DataRowView dr = (DataRowView)(gridView1.GetRow(i)); if (dr[6].ToString().Trim() != "" && dr[7].ToString().Trim() != "") { if (decimal.Parse(dr[6].ToString()) > 0 && decimal.Parse(dr[7].ToString()) > 0) { boolvalue = true; break; } } } if (boolvalue == true) { this.ShowAlertMessage("同一行数据盘盈或盘亏只能填一项,另一项必须为空或0!"); return; } CheckBill CheckBill = new CheckBill(); CheckBill.CheckBillGuid = txtGuid.Text; CheckBill.BillDate = DateTime.Parse(dateTimePicker1.Text); CheckBill.BillID = txtBillID.Text; CheckBill.Depot = cboDepot.Text; if (txtCreateDate.Text.Trim() == "") { CheckBill.CreateDate = DateTime.Now; } else { CheckBill.CreateDate = DateTime.Parse(txtCreateDate.Text); } CheckBill.CreatePerson = SysParams.UserName; CheckBill.CheckDate = DateTime.Parse("1900-01-01"); CheckBill.CheckPerson = ""; CheckBill.Remark = txtRemark.Text; CheckBill.BillAutoID = txtAutoBillID.Text; CheckBill.HandlePerson = cboHandlePerson.Text; List <CheckBillDetail> list = new List <CheckBillDetail>(); CheckBillDetail CheckBillDetail = new CheckBillDetail(); for (int i = 0; i < gridView1.RowCount; i++) { DataRowView dr = (DataRowView)(gridView1.GetRow(i)); CheckBillDetail = new CheckBillDetail(); CheckBillDetail.CheckBillDetailGuid = Guid.NewGuid().ToString(); CheckBillDetail.CheckBillGuid = CheckBill.CheckBillGuid; CheckBillDetail.MaterialGuid = dr[0].ToString(); //gridView1.GetRowCellValue(i, gridMaterialGuid).ToString(); CheckBillDetail.MaterialID = dr[1].ToString(); //gridView1.GetRowCellValue(i, gridMaterialGuid).ToString(); CheckBillDetail.MaterialName = dr[2].ToString(); //gridView1.GetRowCellValue(i, gridMaterialName).ToString(); CheckBillDetail.BarNo = dr[3].ToString(); //gridView1.GetRowCellValue(i, gridMaterialName).ToString(); CheckBillDetail.Spec = dr[4].ToString(); // gridView1.GetRowCellValue(i, gridSpec).ToString(); CheckBillDetail.Unit = dr[5].ToString(); //gridView1.GetRowCellValue(i, gridUnit).ToString(); if (dr[6].ToString().Trim() != "") { CheckBillDetail.SurplusQty = decimal.Parse(dr[6].ToString());//int.Parse(gridView1.GetRowCellValue(i, gridQty).ToString()); } else { CheckBillDetail.SurplusQty = 0; } if (dr[7].ToString().Trim() != "") { CheckBillDetail.DeficientQty = decimal.Parse(dr[7].ToString());//int.Parse(gridView1.GetRowCellValue(i, gridQty).ToString()); } else { CheckBillDetail.DeficientQty = 0; } if (dr[8].ToString().Trim() != "") { CheckBillDetail.Price = decimal.Parse(dr[8].ToString()); //decimal.Parse(gridView1.GetRowCellValue(i, gridPrice).ToString()); } else { CheckBillDetail.Price = 0; } if (dr[9].ToString().Trim() != "") { CheckBillDetail.Total = decimal.Parse(dr[9].ToString()); //decimal.Parse(gridView1.GetRowCellValue(i, gridTotal ).ToString()); } else { CheckBillDetail.Total = 0; } CheckBillDetail.Remark = dr[10].ToString();//gridView1.GetRowCellValue(i, gridRemark).ToString(); list.Add(CheckBillDetail); } //保存 rbm.SaveCheckBill(CheckBill, list); //刷新 if (SendFlag == 0) { frmCheckBill.frmcheckbill.LoadBill(); } tsbCheckPass.Enabled = true; this.ShowMessage("保存成功"); }