/// <summary> /// 生成单据根据 /// </summary> /// <param name="templeteFile">模板文件</param> /// <param name="saveToFile"></param> /// <param name="mainDT"></param> /// <param name="dtls"></param> public void MakeDoc(string templeteFile, string saveToFile, DataTable mainDT, DataSet dtlsDS) { this.mainDT = mainDT; this.dtlsDS = dtlsDS; #region 生成参数。 string str = Cash.GetBillStr(templeteFile, false).Substring(0); string error = ""; string[] paras = null; DataSet dsTemp = new DataSet(); dsTemp.Tables.Add(mainDT); foreach (DataTable dttemp in dsTemp.Tables) { dsTemp.Tables.Add(dttemp); } Attrs attrs = new Attrs(); foreach (DataTable dt in dsTemp.Tables) { foreach (DataColumn dc in dt.Columns) { Attr attr = new Attr(); attr.Field = dc.ColumnName; attr.Key = dc.ColumnName; switch (dc.DataType.ToString()) { case "int": attr.MyDataType = DataType.AppInt; break; case "decimal": case "float": attr.MyDataType = DataType.AppFloat; break; case "string": default: attr.MyDataType = DataType.AppString; break; } } } paras = Cash.GetBillParas_Gener(templeteFile, attrs); #endregion try { string key = ""; string ss = ""; #region 主表 foreach (string para in paras) { if (para == null || para == "") { continue; } try { if (para.Contains("ImgAth")) { str = str.Replace("<" + para + ">", this.GetValueByKey(para)); } else if (para.Contains("Siganture")) { str = str.Replace("<" + para + ">", this.GetValueByKey(para)); } else if (para.Contains("Img@AppPath")) { str = str.Replace("<" + para + ">", this.GetValueImgStrs(para)); } else if (para.Contains(".BPPaint")) { str = str.Replace("<" + para + ">", this.GetValueBPPaintStrs(para)); } else if (para.Contains(".M2M")) { str = str.Replace("<" + para + ">", this.GetValueM2MStrs(para)); } else { str = str.Replace("<" + para + ">", this.GetCode(this.GetValueByKey(para))); } } catch (Exception ex) { error += "取参数[" + para + "]出现错误:有以下情况导致此错误;1你用Text取值时间,此属性不是外键。2,类无此属性。<br>更详细的信息:<br>" + ex.Message; if (SystemConfig.IsDebug) { throw new Exception(error); } Log.DebugWriteError(error); } } #endregion #region 从表 string shortName = ""; ArrayList al = this.EnsDataDtls; foreach (Entities dtls in al) { // shortName = dtls.GetNewEntity.ToString().Substring(dtls.GetNewEntity.ToString().LastIndexOf(".") + 1); Entity dtl = dtls.GetNewEntity; string dtlEnName = dtl.ToString(); shortName = dtlEnName.Substring(dtlEnName.LastIndexOf(".") + 1); if (str.IndexOf(shortName) == -1) { continue; } int pos_rowKey = str.IndexOf(shortName); int row_start = -1, row_end = -1; if (pos_rowKey != -1) { row_start = str.Substring(0, pos_rowKey).LastIndexOf("\\row"); row_end = str.Substring(pos_rowKey).IndexOf("\\row"); } if (row_start != -1 && row_end != -1) { string row = str.Substring(row_start, (pos_rowKey - row_start) + row_end); str = str.Replace(row, ""); Map map = dtls.GetNewEntity.EnMap; int i = dtls.Count; while (i > 0) { i--; string rowData = row.Clone() as string; dtl = dtls[i]; foreach (Attr attr in map.Attrs) { switch (attr.MyDataType) { case DataType.AppDouble: case DataType.AppFloat: case DataType.AppRate: rowData = rowData.Replace("<" + shortName + "." + attr.Key + ">", dtl.GetValStringByKey(attr.Key)); break; case DataType.AppMoney: rowData = rowData.Replace("<" + shortName + "." + attr.Key + ">", dtl.GetValDecimalByKey(attr.Key).ToString("0.00")); break; case DataType.AppInt: if (attr.MyDataType == DataType.AppBoolean) { rowData = rowData.Replace("<" + shortName + "." + attr.Key + ">", dtl.GetValStrByKey(attr.Key)); int v = dtl.GetValIntByKey(attr.Key); if (v == 1) { rowData = rowData.Replace("<" + shortName + "." + attr.Key + "Text>", "是"); } else { rowData = rowData.Replace("<" + shortName + "." + attr.Key + "Text>", "否"); } } else { rowData = rowData.Replace("<" + shortName + "." + attr.Key + ">", dtl.GetValStrByKey(attr.Key)); } break; default: rowData = rowData.Replace("<" + shortName + "." + attr.Key + ">", GetCode(dtl.GetValStrByKey(attr.Key))); break; } } str = str.Insert(row_start, rowData); } } } #endregion 从表 #region 明细 合计信息。 al = this.EnsDataDtls; foreach (Entities dtls in al) { Entity dtl = dtls.GetNewEntity; string dtlEnName = dtl.ToString(); shortName = dtlEnName.Substring(dtlEnName.LastIndexOf(".") + 1); //shortName = dtls.ToString().Substring(dtls.ToString().LastIndexOf(".") + 1); Map map = dtl.EnMap; foreach (Attr attr in map.Attrs) { switch (attr.MyDataType) { case DataType.AppDouble: case DataType.AppFloat: case DataType.AppMoney: case DataType.AppRate: key = "<" + shortName + "." + attr.Key + ".SUM>"; if (str.IndexOf(key) != -1) { str = str.Replace(key, dtls.GetSumFloatByKey(attr.Key).ToString()); } key = "<" + shortName + "." + attr.Key + ".SUM.RMB>"; if (str.IndexOf(key) != -1) { str = str.Replace(key, dtls.GetSumFloatByKey(attr.Key).ToString("0.00")); } key = "<" + shortName + "." + attr.Key + ".SUM.RMBDX>"; if (str.IndexOf(key) != -1) { str = str.Replace(key, GetCode(DA.DataType.ParseFloatToCash(dtls.GetSumFloatByKey(attr.Key)))); } break; case DataType.AppInt: key = "<" + shortName + "." + attr.Key + ".SUM>"; if (str.IndexOf(key) != -1) { str = str.Replace(key, dtls.GetSumIntByKey(attr.Key).ToString()); } break; default: break; } } } #endregion 从表合计 StreamWriter wr = new StreamWriter(this.TempFilePath, false, Encoding.ASCII); str = str.Replace("<", ""); str = str.Replace(">", ""); wr.Write(str); wr.Close(); } catch (Exception ex) { string msg = ""; if (SystemConfig.IsDebug) { // 异常可能与单据的配置有关系。 try { this.CyclostyleFilePath = SystemConfig.PathOfDataUser + "\\CyclostyleFile\\" + templeteFile; str = Cash.GetBillStr(templeteFile, false); string s = RepBill.RepairBill(this.CyclostyleFilePath); msg = "@已经成功的执行修复线 RepairLineV2,您重新发送一次或者,退后重新在发送一次,是否可以解决此问题。@" + s; } catch (Exception ex1) { msg = "执行修复线失败. RepairLineV2 " + ex1.Message; } } throw new Exception("生成文档失败:单据名称[" + this.CyclostyleFilePath + "] 异常信息:" + ex.Message + " @自动修复单据信息:" + msg); } }