public JsonResult Edit(ApplyListModel viewBill) { BaseJsonData json = new BaseJsonData(); if (!User.Identity.IsAuthenticated) { json.msg_code = "nologin"; goto next; } int user = Common.PageValidate.FilterParam(User.Identity.Name); SetSelect(0); if (ModelState.IsValid) { Reimbursement bill = db.Reimbursement.Find(viewBill.reimbursementCode); if (bill == null) { json.msg_code = "error"; json.msg_text = "没有主申请单,更新失败。"; goto next; } if (viewBill.next == 0) { json.msg_code = "error"; json.msg_text = "必需选择审核用户。"; goto next; } StringBuilder sbmsg = new StringBuilder(); if (bill.r_bill_state == 1) { json.msg_code = "error"; json.msg_text = "该报销单已经批复完成,不允许修改。"; goto next; } else { bill.r_bill_amount = viewBill.amount; bill.r_fact_amount = viewBill.amount; bill.r_bill_state = 0; bill.r_add_date = DateTime.Now; bill.reimbursement_info = viewBill.info; db.Entry(bill).State = EntityState.Modified; //录入报销事由 Reimbursement_Content content = null; foreach (ViewContentModel citem in viewBill.contents) { if (citem.contentId != null && citem.contentId != 0) { content = db.Reimbursement_Content.Find(citem.contentId); if (content != null) { content.c_amount = citem.amount; db.Entry(content).State = EntityState.Modified; } } else { content = new Reimbursement_Content(); content.c_reimbursement_code = bill.reimbursement_code; content.c_dic_id = citem.selectId; content.c_amount = citem.amount; db.Reimbursement_Content.Add(content); } try { db.SaveChanges(); } catch (Exception e) { ErrorUnit.WriteErrorLog(e.ToString(), this.GetType().ToString()); sbmsg.Append("报销内容录入失败<br />"); continue; } //录入明细 if (citem.details != null && citem.details.Count() > 0) { Reimbursement_Detail detail = null; foreach (ViewDetailContent item in citem.details) { if (item.detailId != null && item.detailId != 0) { detail = db.Reimbursement_Detail.Find(item.detailId); } else { detail = new Reimbursement_Detail(); detail.detail_content_id = content.content_id; } detail.detail_amount = item.amount; detail.detail_date = DateTime.Parse(item.strDate + " 00:00"); detail.detail_info = item.detailInfo; detail.detail_content_id = content.content_id; if (item.detailId != null && item.detailId != 0) { db.Entry(detail).State = EntityState.Modified; } else { db.Reimbursement_Detail.Add(detail); } } } //录入附件 if (citem.attachments != null && citem.attachments.Count() > 0) { string attachment_path = string.Format("{0}\\{1}\\{2}\\", MyConfiguration.GetAttachmentPath(), bill.reimbursement_code, DateTime.Now.ToString("yyyyMMdd")); string attachment_temp_path = MyConfiguration.GetAttachmentTempPath();; if (!Directory.Exists(attachment_path)) { Directory.CreateDirectory(attachment_path); } string filePath, tempFile, saveFileName = "", storeFileName; foreach (ViewAttachment item in citem.attachments) { if (item.id > 0) { continue; } try { saveFileName = Path.GetFileName(item.fileName); storeFileName = string.Format("{0}/{1}", DateTime.Now.ToString("yyyyMMdd"), saveFileName); tempFile = attachment_temp_path + item.fileName; filePath = string.Format("{0}{1}", attachment_path, saveFileName); if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } System.IO.File.Move(tempFile, filePath); } catch (Exception e) { ErrorUnit.WriteErrorLog(e.ToString(), this.GetType().ToString()); sbmsg.Append("文件【").Append(item.fileName).Append("】保存失败,请重新上传"); continue; } Reimbursement_Attachment attachment = new Reimbursement_Attachment { attachment_path = storeFileName, atta_detail_id = (int)citem.contentId, atta_reimbursement_code = bill.reimbursement_code }; db.Reimbursement_Attachment.Add(attachment); } } try { db.SaveChanges(); } catch (Exception e) { ErrorUnit.WriteErrorLog(e.ToString(), this.GetType().ToString()); sbmsg.Append("报销明细录入失败<br />"); continue; } } //录入附件 if (viewBill.attachments != null && viewBill.attachments.Count() > 0) { string attachment_path = string.Format("{0}\\{1}\\{2}\\", MyConfiguration.GetAttachmentPath(), bill.reimbursement_code, DateTime.Now.ToString("yyyyMMdd")); string attachment_temp_path = MyConfiguration.GetAttachmentTempPath();; if (!Directory.Exists(attachment_path)) { Directory.CreateDirectory(attachment_path); } string filePath, tempFile, saveFileName = "", storeFileName; foreach (ViewAttachment item in viewBill.attachments) { if (item.id > 0) { continue; } try { saveFileName = Path.GetFileName(item.fileName); storeFileName = string.Format("{0}/{1}", DateTime.Now.ToString("yyyyMMdd"), saveFileName); tempFile = attachment_temp_path + item.fileName; filePath = string.Format("{0}{1}", attachment_path, saveFileName); if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } System.IO.File.Move(tempFile, filePath); } catch (Exception e) { ErrorUnit.WriteErrorLog(e.ToString(), this.GetType().ToString()); sbmsg.Append("文件【").Append(item.fileName).Append("】保存失败,请重新上传"); continue; } Reimbursement_Attachment attachment = new Reimbursement_Attachment { attachment_path = storeFileName, atta_detail_id = 0, atta_reimbursement_code = bill.reimbursement_code }; db.Reimbursement_Attachment.Add(attachment); } try { //干脆都先提交得了 db.SaveChanges(); } catch (Exception e) { ErrorUnit.WriteErrorLog(e.ToString(), this.GetType().ToString()); Delete(bill.reimbursement_code); json.msg_code = "error"; json.msg_text = "报销单附件提交失败。"; goto next; } } //录入批复流程 db.Process_Respond.RemoveRange(db.Process_Respond.Where(x => x.pr_reimbursement_code == bill.reimbursement_code)); //添加批复人 Process_Respond pr = new Process_Respond(); pr.pr_reimbursement_code = bill.reimbursement_code; pr.pr_user_id = viewBill.next; pr.pr_number = 1; db.Process_Respond.Add(pr); try { db.SaveChanges(); SysLog.WriteLog(user, string.Format("修改报帐单[{0}]", bill.reimbursement_code), IpHelper.GetIP(), bill.reimbursement_code, 4, "", db); } catch (Exception e) { ErrorUnit.WriteErrorLog(e.ToString(), this.GetType().ToString()); Delete(bill.reimbursement_code); json.msg_code = "error"; json.msg_text = "报销单提交失败。"; goto next; } } json.state = 1; json.msg_code = bill.reimbursement_code; json.msg_text = sbmsg.ToString(); } next: return(Json(json, JsonRequestBehavior.AllowGet)); }
public JsonResult Create(ApplyListModel _sbill) { BaseJsonData json = new BaseJsonData(); if (!User.Identity.IsAuthenticated) { json.msg_code = "nologin"; goto next; } int user = Common.PageValidate.FilterParam(User.Identity.Name); SetSelect(0); if (ModelState.IsValid) { Reimbursement bill = new Reimbursement(); bill.r_bill_amount = _sbill.amount; bill.r_bill_state = 0; bill.r_add_date = DateTime.Now; bill.r_add_user_id = user; bill.reimbursement_info = _sbill.info; var maxfa = db.Reimbursement.OrderByDescending(x => x.reimbursement_code).FirstOrDefault(); //apply_number:年份+10001自增 if (maxfa == null) { bill.reimbursement_code = DateTime.Now.Year.ToString() + "10001"; } else { bill.reimbursement_code = DateTime.Now.Year.ToString() + (int.Parse(maxfa.reimbursement_code.Substring(4)) + 1); } db.Reimbursement.Add(bill); try { db.SaveChanges(); } catch (Exception e) { ErrorUnit.WriteErrorLog(e.ToString(), this.GetType().ToString()); json.msg_code = "error"; json.msg_text = "报销单提交失败。"; goto next; } StringBuilder sbErr = new StringBuilder(); //添加报销内容 foreach (ViewContentModel citem in _sbill.contents) { Reimbursement_Content content = new Reimbursement_Content(); content.c_reimbursement_code = bill.reimbursement_code; content.c_amount = citem.amount; content.c_dic_id = citem.selectId; db.Reimbursement_Content.Add(content); try { //必需先提交更改,因为下面添加明细需要用到自动生成的ID。 db.SaveChanges(); } catch (Exception e) { ErrorUnit.WriteErrorLog(e.ToString(), this.GetType().ToString()); Delete(bill.reimbursement_code); json.msg_code = "error"; json.msg_text = "报销单提交失败。"; goto next; } //添加明细 if (citem.details != null && citem.details.Count() > 0) { foreach (ViewDetailContent viewDetail in citem.details) { Reimbursement_Detail detail = new Reimbursement_Detail() { detail_amount = viewDetail.amount, detail_content_id = content.content_id, detail_date = DateTime.Parse(viewDetail.strDate + " 00:00"), detail_info = viewDetail.detailInfo }; db.Reimbursement_Detail.Add(detail); } } //添加附件 if (citem.attachments != null && citem.attachments.Count() > 0) { string attachment_path = string.Format("{0}\\{1}\\{2}\\", MyConfiguration.GetAttachmentPath(), bill.reimbursement_code, DateTime.Now.ToString("yyyyMMdd")); string attachment_temp_path = MyConfiguration.GetAttachmentTempPath();; if (!Directory.Exists(attachment_path)) { Directory.CreateDirectory(attachment_path); } string filePath, tempFile, saveFileName = "", storeFileName; foreach (ViewAttachment item in citem.attachments) { try { saveFileName = Path.GetFileName(item.fileName); storeFileName = string.Format("{0}/{1}", DateTime.Now.ToString("yyyyMMdd"), saveFileName); tempFile = attachment_temp_path + item.fileName; filePath = string.Format("{0}{1}", attachment_path, saveFileName); if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } System.IO.File.Move(tempFile, filePath); } catch (Exception e) { ErrorUnit.WriteErrorLog(e.ToString(), GetType().ToString()); sbErr.Append("文件【").Append(saveFileName).Append("】保存失败,请重新上传。\r\n"); continue; } Reimbursement_Attachment attachment = new Reimbursement_Attachment { attachment_path = storeFileName, atta_detail_id = content.content_id, atta_reimbursement_code = bill.reimbursement_code }; db.Reimbursement_Attachment.Add(attachment); } } try { db.SaveChanges(); } catch (Exception e) { ErrorUnit.WriteErrorLog(e.ToString(), this.GetType().ToString()); Delete(bill.reimbursement_code); json.msg_code = "error"; json.msg_text = "报销单提交失败。"; goto next; } } //添加其他附件 if (_sbill.attachments != null && _sbill.attachments.Count() > 0) { string attachment_path = string.Format("{0}\\{1}\\{2}\\", MyConfiguration.GetAttachmentPath(), bill.reimbursement_code, DateTime.Now.ToString("yyyyMMdd")); string attachment_temp_path = MyConfiguration.GetAttachmentTempPath();; if (!Directory.Exists(attachment_path)) { Directory.CreateDirectory(attachment_path); } string filePath, tempFile, saveFileName = "", storeFileName; foreach (ViewAttachment item in _sbill.attachments) { try { saveFileName = Path.GetFileName(item.fileName); storeFileName = string.Format("{0}/{1}", DateTime.Now.ToString("yyyyMMdd"), saveFileName); tempFile = attachment_temp_path + item.fileName; filePath = string.Format("{0}{1}", attachment_path, saveFileName); if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } System.IO.File.Move(tempFile, filePath); } catch (Exception e) { ErrorUnit.WriteErrorLog(e.ToString(), GetType().ToString()); sbErr.Append("文件【").Append(saveFileName).Append("】保存失败,请重新上传。\r\n"); continue; } Reimbursement_Attachment attachment = new Reimbursement_Attachment { attachment_path = storeFileName, atta_detail_id = 0, atta_reimbursement_code = bill.reimbursement_code }; db.Reimbursement_Attachment.Add(attachment); } try { db.SaveChanges(); } catch (Exception e) { ErrorUnit.WriteErrorLog(e.ToString(), this.GetType().ToString()); Delete(bill.reimbursement_code); json.msg_code = "error"; json.msg_text = "报销单提交失败。"; goto next; } } //添加批复人 Process_Respond pr = new Process_Respond(); pr.pr_reimbursement_code = bill.reimbursement_code; pr.pr_user_id = _sbill.next; pr.pr_number = 1; db.Process_Respond.Add(pr); try { db.SaveChanges(); SysLog.WriteLog(user, string.Format("添加报帐单[{0}]", bill.reimbursement_code), IpHelper.GetIP(), bill.reimbursement_code, 4, "", db); } catch (Exception e) { ErrorUnit.WriteErrorLog(e.ToString(), this.GetType().ToString()); Delete(bill.reimbursement_code); json.msg_code = "error"; json.msg_text = "报销单提交失败。"; goto next; } json.state = 1; json.msg_code = bill.reimbursement_code; json.msg_text = sbErr.ToString(); } next: return(Json(json, JsonRequestBehavior.AllowGet)); }
public JsonResult SetAgree(Respond respond) { BaseJsonData json = new BaseJsonData(); if (!User.Identity.IsAuthenticated) { json.msg_text = "没有登陆或登陆失效,请重新登陆后操作。"; json.msg_code = "notLogin"; return(Json(json, JsonRequestBehavior.AllowGet)); } int user = PageValidate.FilterParam(User.Identity.Name); if (!RoleCheck.CheckHasAuthority(user, db, "批复管理", "批复")) { json.msg_text = "没有权限。"; json.msg_code = "paramErr"; return(Json(json, JsonRequestBehavior.AllowGet)); } if (respond.id == null || respond.id == 0) { json.msg_text = "参数传递失败,请重试。"; json.msg_code = "paramErr"; return(Json(json, JsonRequestBehavior.AllowGet)); } Process_Respond model = db.Process_Respond.Find(respond.id); if (model == null) { json.msg_text = "没找到该流程,可能已经撤销,请重试。"; json.msg_code = "None"; return(Json(json, JsonRequestBehavior.AllowGet)); } if (user != model.pr_user_id) { json.msg_text = "非该流程的当前批复人。"; json.msg_code = "paramErr"; return(Json(json, JsonRequestBehavior.AllowGet)); } var exists = db.Process_Respond.Where(x => x.pr_reimbursement_code == model.pr_reimbursement_code && x.pr_user_id == respond.next); if (exists.Count() > 0) { json.msg_text = "该审核人已存在审批列表中。"; json.msg_code = "Exists"; return(Json(json, JsonRequestBehavior.AllowGet)); } //批复当前流程 int state = respond.state; model.pr_state = state; model.pr_time = DateTime.Now; model.pr_content = PageValidate.InputText(Server.UrlDecode(respond.reason), 2000); db.Entry(model).State = System.Data.Entity.EntityState.Modified; //是否为批复不通过 Reimbursement bill = db.Reimbursement.Find(model.pr_reimbursement_code); if (bill == null) { json.msg_text = "操作失败,该报销单已被删除。"; json.msg_code = "Error"; return(Json(json, JsonRequestBehavior.AllowGet)); } if (state == 1) { Funds fmodel = db.Funds.Find(bill.r_funds_id); if (fmodel == null) { json.msg_text = "所申请的经费已不存在,无法继续。"; json.msg_code = "applyError"; json.state = 0; return(Json(json, JsonRequestBehavior.AllowGet)); } //是否有next if (respond.next != null && respond.next != 0) { Process_Respond pr = new Process_Respond(); pr.pr_reimbursement_code = model.pr_reimbursement_code; pr.pr_user_id = (int)respond.next; pr.pr_number = model.pr_number + 1; db.Process_Respond.Add(pr); model.next = pr.pr_id; db.Entry(model).State = System.Data.Entity.EntityState.Modified; } else { if (fmodel.f_balance < bill.r_bill_amount) { //经费不足,回退批复 json.msg_text = "当前经费余额不足,无法继续。"; json.msg_code = "applyError"; json.state = 0; return(Json(json, JsonRequestBehavior.AllowGet)); } else { fmodel.f_balance = fmodel.f_balance - bill.r_bill_amount; db.Entry(fmodel).State = System.Data.Entity.EntityState.Modified; bill.r_bill_state = state; bill.r_fact_amount = bill.r_bill_amount; db.Entry(bill).State = System.Data.Entity.EntityState.Modified; } } } else { bill.r_bill_state = state; db.Entry(bill).State = System.Data.Entity.EntityState.Modified; } try { db.SaveChanges(); SysLog.WriteLog(user, string.Format("批复报帐单[{0}],结果为[{1}]", bill.reimbursement_code, state), IpHelper.GetIP(), bill.reimbursement_code, 3, "", db); } catch (DbEntityValidationException et) { StringBuilder errors = new StringBuilder(); IEnumerable <DbEntityValidationResult> validationResult = et.EntityValidationErrors; foreach (DbEntityValidationResult result in validationResult) { ICollection <DbValidationError> validationError = result.ValidationErrors; foreach (DbValidationError err in validationError) { errors.Append(err.PropertyName + ":" + err.ErrorMessage + "\r\n"); } } ErrorUnit.WriteErrorLog(errors.ToString(), this.GetType().Name); json.msg_text = "审核失败。"; json.msg_code = "respondError"; json.state = 0; return(Json(json, JsonRequestBehavior.AllowGet)); } json.msg_text = "操作成功。"; json.msg_code = "success"; json.state = 1; return(Json(json, JsonRequestBehavior.AllowGet)); }