C# 发送xml报文到用友U8生成凭证系列一(配置信息)
C# 发送xml报文到用友U8生成凭证系列二(基础代码)
C# 发送xml报文到用友U8生成凭证系列三(Modal代码)
C# 发送xml报文到用友U8生成凭证系列四(Biz代码)最终代码

本文我们详细讲解了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凭证数据 XMLModal.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 outputXmlSerializerNamespaces ns = new XmlSerializerNamespaces();//Add an empty namespace and empty valuens.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();//ufinterfaceu8.billtype = nc.billtype;u8.codeexchanged = nc.isexchange;//编码是否已转换u8.docid = nc.filename;//唯一编号u8.exportneedexch = "N";//string.Empty;u8.proc = nc.proc;//操作码 导入用add edit delete ,导出queryu8.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;//voucheru8.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_headu8Voucher.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 iperiodConfig.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 2u8vh.memo1 = "结算平台";//ncvh.memo1;u8vh.memo2 = ncvh.memo2;//保留(外部凭证业务类型) u8vh.reserve1 = ncvh.reserve1;//保留(外部凭证业务号)  u8vh.reserve2 = ncVoucher.id;//ncvh.reserve2;//作废凭证此字段填1,正常凭证不填,有错凭证填2u8vh.revokeflag = string.Empty;//ncvh.revokeflag;u8Voucher.voucher_head[0] = u8vh;//voucher_bodyu8Voucher.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;//默认备注为3u8Voucher.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 addcustomer = 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);}}}#endregionvar 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;}}//没有客商辅助核算情况下插入XMLif (u8AuxList.Count == 0){//客户编号                                                                                                       销项税      22210105u8AuxList.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 iperiodConfig.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;//默认银行备注为0var 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凭证格式。

最终效果如下:

C# 发送xml报文到用友U8生成凭证系列四(Biz代码)最终代码相关推荐

  1. C# 发送xml报文到用友U8生成凭证系列二(基础代码)

    C# 发送xml报文到用友U8生成凭证系列一(配置信息) C# 发送xml报文到用友U8生成凭证系列二(基础代码) C# 发送xml报文到用友U8生成凭证系列三(Modal代码) C# 发送xml报文 ...

  2. python发送xml报文_python通过tcp发送xml报文的方法

    如下所示: # -*- coding: utf-8 -*- import socket # 使用tcp发送请求报文 def tcpsend(ip, port, xmlbw): address = (i ...

  3. 用友U8审核凭证出现列前缀tempdb无效未指定表名的对话框的解决方法

    用友U8审核凭证出现列前缀tempdb无效未指定表名,U8出现这个问题,是因为你的计算机名出现非法符号,如"-"等,这时就会出现上述问题.更改计算机名的方法,去掉"-&q ...

  4. webservice发送xml报文_海关总署公告2020年第120号(关于发布进出境公路运输工具货运舱单电子传输报文格式V1.1的公告)...

    点击上方蓝色字体,关注我们 为切实加强公路进出境运输工具及货运舱单管理,进一步规范相关电子数据申报传输要求,根据海关总署2020年第113号公告,我署对进出境公路运输工具货运舱单电子传输报文中部分数据 ...

  5. 使用axis发送xml报文,返回并解析报文实例

    前段时间刚好学了一点webservice,因此想和大家分享一下我的成果,因为能力原因,只能做个小实例,望大家见谅! 此实例的思路是:(1)用String类型构造好需要发送的报文;(2)使用axis调用 ...

  6. java调用百望税控NISEC_SKSC.dll发送xml报文

    1.第一个问题:Java调用dll文件 JNative方式调用dll JNative是一种能够使Java语言使调用DLL的一种技术,对JNI进行了封装,可能有些读者会有这样一个问题,JNative对J ...

  7. u8转完看不到菜单_用友U8操作中的常见问题及解决方案

    目前,会计电算化课程是会计工作者的最为核心课程,而用友ERP-U8也是应用最为广泛的课程这一,然而在学生实践过程中往往会出现许多的问题,本人通过在这几年的教学过程中学生遇到的难点疑点及常见问题进行归纳 ...

  8. vba xml 怎么设置父节点_熊二做了一个xml报文处理的开源库easyxml

    ❝ 自信.冷静.专注.-- TM 熊的自我勉励 ❞ 1. 前言 熊二从去年开始,因项目需求接触到xml报文的处理,也是我第一次学习用C/C++的方式处理基于DOM模型的xml报文.因为本人比较懒hhh ...

  9. java 传送解析8583报文_java发送ISO8583报文接口案例

    [实例简介] java发送ISO8583报文实现平安银行支付接口 [实例截图] [核心代码] pingAnBank └── pingAnBank ├── build.xml ├── client │  ...

最新文章

  1. Java锁详解:“独享锁/共享锁+公平锁/非公平锁+乐观锁/悲观锁+线程锁”
  2. AgilePoint模型驱动BPM(业务流程管理)介绍
  3. Android源码打包命令
  4. 网络编程懒人入门(六):史上最通俗的集线器、交换机、路由器功能原理入门
  5. margin塌陷问题
  6. modbus4-232 485 单机 多机
  7. Jenkins安装入门
  8. 跨平台加密版 SQLite 3 - wxSQLite3
  9. 01.神经网络和深度学习 W1.深度学习概论
  10. centos sftp客户端 c 源码_Redis第3课:如何使用 Redis客户端
  11. 对象失去焦点时自己动提交数据
  12. 【数据结构笔记29】最小生成树问题:Prim算法与Kruskal算法
  13. 开课吧python怎么样-找工作得有个大杀招,你看AI换脸这个技能怎么样?
  14. Odoo免费开源MES功能应用简介
  15. 烧录工具Android Tool的使用
  16. 记录进行Uniprot转化为Entrez ID的过程
  17. C语言的数学运算函数
  18. 运维安全:微软远程桌面修改默认远程端口
  19. 浙江电信网速测试皮肤软件,浙江电信网速测试(进入中国电信官方测速网站)
  20. 专利申请流程及费用及时间?

热门文章

  1. FPGA给DSP6678复位
  2. Windows 7下读写Ext2/Ext3/Ext4文件系统
  3. Kafka遇到报错:The group member‘s supported protocols are incompatible with those of existing members
  4. SCDL--稀疏编码(sparse code)与字典学习(dictionary learning)
  5. 网页悬浮在线人工客服代码.
  6. Office frontpage 2003 SKU017.CAB
  7. Ubuntu配置Nginx部署Vue SPA项目
  8. 浅议企业信息化队伍建设
  9. Java导出自定义Excel表格,一套组合拳解决
  10. windows配置c语言开发环境