本文我们详细讲解了Biz目录下的将xml报文转换为用友U8凭证格式代码。
Convert目录下的文件:
IConvertor.cs代码:
using NC2U8.Modal;
using System;
using System.Collections.Generic;
using System.Text;
namespace NC2U8.Biz.Convert
{
public interface IConvertor
{
/// <summary>
/// 转成成U8适应的XML
/// </summary>
/// <param name="vcNo">VC编号</param>
/// <param name="outerData">要转化的XML</param>
/// <returns></returns>
string Convert(string outerData);
}
}
VoucherConvertor.cs 代码:代码中最主要代码,将详细讲解怎么把XML报文转换为U8凭证格式
using NC2U8.WebReference;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Serialization;
namespace NC2U8.Biz.Convert
{
public class VoucherConvertor : IConvertor
{
/// <summary>
/// 公司编号和年份返回存放
/// </summary>
public string ReslutInfo { get; set; }
public string Convert(string outerData)
{
//获取nc凭证数据
XmlSerializer sn = new XmlSerializer(typeof(NC2U8.Modal.NC.ufinterface));
NC2U8.Modal.NC.ufinterface nc = sn.Deserialize(new StringReader(outerData)) as NC2U8.Modal.NC.ufinterface;
//转换成u8凭证数据 XML
Modal.U8.ufinterface u8 = NC2U8(nc);
XmlSerializer su = new XmlSerializer(typeof(NC2U8.Modal.U8.ufinterface));
XmlWriterSettings setting = new XmlWriterSettings();
setting.Encoding = new UTF8Encoding(false);
setting.Indent = true;
//Create our own namespaces for the output
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
//Add an empty namespace and empty value
ns.Add("", "");//去除节点中的xmlns:xsi 属性
using (var ms = new MemoryStream())
{
using (var xw = XmlWriter.Create(ms, setting))
{
su.Serialize(xw, u8, ns);
return ReslutInfo + "|" + Encoding.UTF8.GetString(ms.ToArray());
}
}
}
private Modal.U8.ufinterface NC2U8(Modal.NC.ufinterface nc)
{
Modal.U8.ufinterface u8 = new Modal.U8.ufinterface();
//ufinterface
u8.billtype = nc.billtype;
u8.codeexchanged = nc.isexchange;//编码是否已转换
u8.docid = nc.filename;//唯一编号
u8.exportneedexch = "N";//string.Empty;
u8.proc = nc.proc;//操作码 导入用add edit delete ,导出query
u8.receiver = "u8";// nc.receiver; 接收方
u8.renewproofno = "Y";//string.Empty;
u8.roottag = "voucher";// nc.roottag;
u8.sender = "001";//nc.sender;发送方编码 即注册的外部编码
u8.version = "2.0";
ReslutInfo = nc.receiver;
//voucher
u8.voucher = new Modal.U8.ufinterfaceVoucher[nc.voucher.Length];
for (int vIndex = 0; vIndex < nc.voucher.Length; vIndex++)
{
u8.voucher[vIndex] = new Modal.U8.ufinterfaceVoucher();
var u8Voucher = u8.voucher[vIndex];
var ncVoucher = nc.voucher[vIndex];
u8Voucher.id = ncVoucher.id;
//voucher_head
u8Voucher.voucher_head = new Modal.U8.ufinterfaceVoucherVoucher_head[1];
var u8vh = new Modal.U8.ufinterfaceVoucherVoucher_head();
var ncvh = ncVoucher.voucher_head[0];
//公司的名称或编号,U8用户不用填写,给NC用户使用
u8vh.company = "";//ncvh.company;
//凭证类别*不能为空
u8vh.voucher_type = RetuenType(ncvh.voucher_type);
//凭证所属的会计年度 *不能为空
u8vh.fiscal_year = ncvh.fiscal_year;
//所属的会计期间 *不能为空
u8vh.accounting_period = ncvh.accounting_period;
//凭证号 需从系统中取 根据iyear iperiod
Config.U8VoucherTableAdapters.GL_accvouchTableAdapter vid = new Config.U8VoucherTableAdapters.GL_accvouchTableAdapter();
var voucher_id = vid.GetU8VoucherID(int.Parse(ncvh.accounting_period), int.Parse(ncvh.fiscal_year), RetuenType(ncvh.voucher_type));//voucher_type);
//跨月后备查里面没有凭证ID 因此默认凭证ID 1开始
if (voucher_id == null) voucher_id = 1;
u8vh.voucher_id = voucher_id.ToString();
//所附单据的数量
u8vh.attachment_number = ncvh.attachment_number;
//制单日期 *不能为空
u8vh.date = ncvh.prepareddate;
ReslutInfo += "-" + ncvh.prepareddate.Replace('-', '.');
//制单人
u8vh.enter = ReturnEnterName(ncvh.enter);
//出纳签字人
u8vh.cashier = ncvh.cashier;
//是否已签字
u8vh.signature = string.Empty;//ncvh.signature;
//审核人名称
u8vh.checker = ncvh.checker;
//记账日期
u8vh.posting_date = ncvh.posting_date;
//记账人名称
u8vh.posting_person = ncvh.posting_person;
//制单系统标示,导入时必须填GL,即总帐系统单据
u8vh.voucher_making_system = string.Empty;// ncvh.voucher_making_system;
//凭证头自定义项1 2
u8vh.memo1 = "结算平台";//ncvh.memo1;
u8vh.memo2 = ncvh.memo2;
//保留(外部凭证业务类型)
u8vh.reserve1 = ncvh.reserve1;
//保留(外部凭证业务号)
u8vh.reserve2 = ncVoucher.id;//ncvh.reserve2;
//作废凭证此字段填1,正常凭证不填,有错凭证填2
u8vh.revokeflag = string.Empty;//ncvh.revokeflag;
u8Voucher.voucher_head[0] = u8vh;
//voucher_body
u8Voucher.voucher_body = new Modal.U8.ufinterfaceVoucherVoucher_bodyEntry[nc.voucher[vIndex].voucher_body.Length];
//判断客商是否存在
Config.U8VoucherTableAdapters.CustomerTableAdapter customer = new Config.U8VoucherTableAdapters.CustomerTableAdapter();
Config.U8VoucherTableAdapters.VendorTableAdapter vendor = new Config.U8VoucherTableAdapters.VendorTableAdapter();
//保存下客商辅助核算 代码下面判断 销项税 22210105用
var cust_id = string.Empty;
for (int vbIndex = 0; vbIndex < u8Voucher.voucher_body.Length; vbIndex++)
{
u8Voucher.voucher_body[vbIndex] = new Modal.U8.ufinterfaceVoucherVoucher_bodyEntry();
u8Voucher.voucher_body[vbIndex].entry_id = ncVoucher.voucher_body[vbIndex].entry_id;
u8Voucher.voucher_body[vbIndex].account_code = ncVoucher.voucher_body[vbIndex].account_code;
var vtype = ncVoucher.voucher_body[vbIndex].@abstract.Contains("付") ? " 付款No:" : " 核销No:";
u8Voucher.voucher_body[vbIndex].@abstract = ncVoucher.voucher_body[vbIndex].@abstract + vtype + ncVoucher.voucher_body[vbIndex].freeitem1;
//默认备注为3
u8Voucher.voucher_body[vbIndex].settlement = "3";// ncVoucher.voucher_body[vbIndex].settlement;
u8Voucher.voucher_body[vbIndex].document_id = ncVoucher.voucher_body[vbIndex].document_id;
//票据日期改成 默认成制定制单日期
u8Voucher.voucher_body[vbIndex].document_date = ncvh.prepareddate; //ncVoucher.voucher_body[vbIndex].document_date;
u8Voucher.voucher_body[vbIndex].currency = ReturnMoneyType(ncVoucher.voucher_body[vbIndex].currency);
u8Voucher.voucher_body[vbIndex].unit_price = ncVoucher.voucher_body[vbIndex].unit_price;
u8Voucher.voucher_body[vbIndex].exchange_rate1 = ncVoucher.voucher_body[vbIndex].exchange_rate1;
u8Voucher.voucher_body[vbIndex].exchange_rate2 = ncVoucher.voucher_body[vbIndex].exchange_rate2;
u8Voucher.voucher_body[vbIndex].debit_quantity = ncVoucher.voucher_body[vbIndex].debit_quantity;
u8Voucher.voucher_body[vbIndex].primary_debit_amount = ncVoucher.voucher_body[vbIndex].primary_debit_amount;
u8Voucher.voucher_body[vbIndex].secondary_debit_amount = ncVoucher.voucher_body[vbIndex].secondary_debit_amount;
u8Voucher.voucher_body[vbIndex].natural_debit_currency = ncVoucher.voucher_body[vbIndex].natural_debit_currency;
u8Voucher.voucher_body[vbIndex].credit_quantity = ncVoucher.voucher_body[vbIndex].credit_quantity;
u8Voucher.voucher_body[vbIndex].primary_credit_amount = ncVoucher.voucher_body[vbIndex].primary_credit_amount;
u8Voucher.voucher_body[vbIndex].secondary_credit_amount = ncVoucher.voucher_body[vbIndex].secondary_credit_amount;
u8Voucher.voucher_body[vbIndex].natural_credit_currency = ncVoucher.voucher_body[vbIndex].natural_credit_currency;
u8Voucher.voucher_body[vbIndex].bill_type = ncVoucher.voucher_body[vbIndex].bill_type;
u8Voucher.voucher_body[vbIndex].bill_id = ncVoucher.voucher_body[vbIndex].bill_id;
u8Voucher.voucher_body[vbIndex].bill_date = ncVoucher.voucher_body[vbIndex].bill_date;
if (ncVoucher.voucher_body[vbIndex].auxiliary_accounting != null)
{
var u8AuxList = new List<Modal.U8.item>();
for (int aIndex = 0; aIndex < ncVoucher.voucher_body[vbIndex].auxiliary_accounting.Length; aIndex++)
{
if (ncVoucher.voucher_body[vbIndex].auxiliary_accounting[aIndex].name.Equals("客商辅助核算"))
{
#region//判断客商代码是否在U8中存在,不存在则自动新增 20170328 add
customer = new Config.U8VoucherTableAdapters.CustomerTableAdapter();
vendor = new Config.U8VoucherTableAdapters.VendorTableAdapter();
var code = ncVoucher.voucher_body[vbIndex].auxiliary_accounting[aIndex].Value;
if (!string.IsNullOrEmpty(code) && code.Length > 4)
{
//去接口中取得客商代码详细信息写入U8表中
PsnService psnService = new PsnService();
var getCustomerInfo = psnService.GetAllCustomerInfoByCode(code);
if (!string.IsNullOrEmpty(getCustomerInfo))
{
string[] array2 = getCustomerInfo.Split(new char[] { '|' });
//客户
if (customer.GetExistsCustomerCode(code) == 0)
{
customer.Insert(array2[0], array2[1], array2[1], "01", DateTime.Now, 0, 0, 0, array2[0], 0, 0, 0, 0, DateTime.Now, array2[0], false,
false, false, false, false, false, false, false, true, 0, "人民币", 0, false, false, array2[0], DateTime.Now, false, false, false);
}
if (vendor.GetExistVendorCode(code) == 0)
{
//供应商 array2[1].Replace("有限公司", "") 精典名称时报错改的
vendor.Insert(array2[0], array2[1], array2[1].Replace("有限公司", ""), "01", DateTime.Now, 0, 0, 0, array2[0], 0, 0, 0, 0, false, DateTime.Now, false,
false, false, false, false, false, true, "人民币", 0, false, false, false, DateTime.Now);
}
}
}
#endregion
var u8Aux = new Modal.U8.item();
u8Aux.name = "cust_id";
u8AuxList.Add(new Modal.U8.item() { name = "item_class", Value = "09" });
u8AuxList.Add(new Modal.U8.item() { name = "supplier_id", Value = ncVoucher.voucher_body[vbIndex].@abstract.Contains("付") ? ncVoucher.voucher_body[vbIndex].auxiliary_accounting[aIndex].Value : "" });
//收款信息
u8Aux.Value = ncVoucher.voucher_body[vbIndex].@abstract.Contains("付") ? "" : ncVoucher.voucher_body[vbIndex].auxiliary_accounting[aIndex].Value;
u8AuxList.Add(u8Aux);
cust_id = ncVoucher.voucher_body[vbIndex].auxiliary_accounting[aIndex].Value;
}
}
//没有客商辅助核算情况下插入XML
if (u8AuxList.Count == 0)
{
//客户编号 销项税 22210105
u8AuxList.Add(new Modal.U8.item() { name = "cust_id", Value = ncVoucher.voucher_body[vbIndex].account_code== "22210105" ? cust_id : "" });
//项目大类
u8AuxList.Add(new Modal.U8.item() { name = "item_class", Value = "" });
//供应商编号
u8AuxList.Add(new Modal.U8.item() { name = "supplier_id", Value = "" });
}
//部门编号
u8AuxList.Add(new Modal.U8.item() { name = "dept_id", Value = "1" });
//人员编号
u8AuxList.Add(new Modal.U8.item() { name = "personnel_id", Value = "" });
//项目编号
//20170406del 由于出现项目非法而删除改为财务建立的项目大类
//u8AuxList.Add(new Modal.U8.item() { name = "item_id", Value = System.Convert.ToDateTime(ncvh.prepareddate).ToString("yyyy") + "结算平台中(货代/检品/物流)发送U8的业务往来" });
//u8AuxList.Add(new Modal.U8.item() { name = "item_id", Value = System.Convert.ToDateTime(ncvh.prepareddate).ToString("yyyy") + ncvh.accounting_period });//20180125 由于2018年的科目大类有问题先切换2017年12月的
u8AuxList.Add(new Modal.U8.item() { name = "item_id", Value = "201712" });
u8AuxList.Add(new Modal.U8.item() { name = "operator", Value = "-" });
u8AuxList.Add(new Modal.U8.item() { name = "self_define1", Value = "-" });
if (u8AuxList.Count > 0)
{
u8Voucher.voucher_body[vbIndex].auxiliary_accounting = u8AuxList.ToArray();
}
}
//新增银行备查用 判断科目是否银行科目
if (ReturnBank(ncVoucher.voucher_body[vbIndex].account_code) == true)
{
//凭证号 需从系统中取 根据iyear iperiod
Config.U8VoucherTableAdapters.GL_CodeRemarkTableAdapter getInoID = new Config.U8VoucherTableAdapters.GL_CodeRemarkTableAdapter();
var ino_id = getInoID.GetCodeRemarkIno_ID(int.Parse(ncvh.accounting_period), int.Parse(ncvh.fiscal_year), RetuenType(ncvh.voucher_type));//, int.Parse(DateTime.Now.ToString("yyyyMM")));//voucher_type);
//跨月后备查里面没有凭证ID 因此默认凭证ID 1开始
if (ino_id == null) ino_id = 1;
//默认银行备注为0
var iYperiod = ncvh.fiscal_year + ncvh.accounting_period;
Config.U8VoucherTableAdapters.GL_CodeRemarkTableAdapter insertcode =
new Config.U8VoucherTableAdapters.GL_CodeRemarkTableAdapter();
insertcode.InsertData(int.Parse(u8vh.accounting_period), RetuenType(ncvh.voucher_type), int.Parse(ino_id.ToString()),
vbIndex + 1, int.Parse(u8vh.fiscal_year), int.Parse(iYperiod), "0", null, null);
}
}
}
return u8;
}
/// <summary>
/// 根据币制转换为币制名称
/// </summary>
/// <param name="moneyType"></param>
/// <returns></returns>
public string ReturnMoneyType(string moneyType)
{
var currency = string.Empty;
switch (moneyType.Trim())
{
case "CNY":
currency = "人民币";
break;
case "JPY":
currency = "日圆";
break;
case "USD":
currency = "美金";
break;
}
return currency;
}
/// <summary>
/// 判断科目代码是不是银行代码
/// </summary>
/// <param name="vcode"></param>
/// <returns></returns>
public bool ReturnBank(string vcode)
{
bool ifbank = false;
switch (vcode)
{
case "10020101":
ifbank = true;
break;
case "10020202":
ifbank = true;
break;
case "10020303":
ifbank = true;
break;
}
return ifbank;
}
/// 返回收/付等类型
/// </summary>
/// <param name="vtype"></param>
/// <returns></returns>
public string RetuenType(string vtype)
{
string voucher_type = string.Empty;
switch (vtype)
{
case "A":
voucher_type = "收";
break;
case "C":
voucher_type = "收";
break;
case "I":
//结算中的对账,U8相对应的是转 类型 20170330
//voucher_type = "对账";
voucher_type = "转";
break;
case "D":
voucher_type = "付";
break;
case "F":
voucher_type = "付";
break;
case "Z":
voucher_type = "转";
break;
}
return voucher_type;
}
/// <summary>
/// 根据提交OP转换为提交者姓名
/// </summary>
/// <param name="userid"></param>
/// <returns></returns>
public string ReturnEnterName(string userid)
{
List<EnterInfo> lstinfo = new List<EnterInfo>();
lstinfo.Add(new EnterInfo() { UserID = "888", UserName = "xxx" });
var name = string.Empty;
foreach (var item in lstinfo)
{
if (item.UserID.Equals(userid))
{
name = item.UserName;
break;
}
}
return name;
}
public class EnterInfo
{
public string UserID { get; set; }
public string UserName { get; set; }
}
}
}
EaiAccepter.cs 代码:
http://localhost/u8eai/import.asp 用友U8中默认 eai接口 导入
using NC2U8.Biz.Convert;
using NC2U8.Common;
using NC2U8.Modal;
using System;
using System.Collections.Generic;
using System.Net;
using System.Web;
namespace NC2U8.Biz
{
public class EaiAccepter
{
private IConvertor convertor;
private string url;
private string encoding;
public EaiAccepter(IConvertor convertor)
{
this.convertor = convertor;
}
public string Send(string outerData)
{
url = System.Configuration.ConfigurationManager.AppSettings["url"];
encoding = System.Configuration.ConfigurationManager.AppSettings["encoding"];
if (string.IsNullOrEmpty(url)) url = "http://localhost/u8eai/import.asp";
if (string.IsNullOrEmpty(encoding)) encoding = "utf-8";
string convertedData = convertor.Convert(outerData);
var getFormatU8Xml = convertedData.Split('|');
//HttpWebResponse response = HttpHelper.Post(url, convertedData);20161013del
//return HttpHelper.GetResponseContent(response, encoding);
HttpWebResponse response = HttpHelper.Post(url, getFormatU8Xml[1]);
return getFormatU8Xml[0]+"|"+HttpHelper.GetResponseContent(response, encoding);
}
}
}
至此,本系列文章结束了,我们从基础配置到中间代码的编写,最终xml报文转换为U8凭证格式。
最终效果如下: