此处以预付款申请单推动付款结算单到NC系统为例
目录
四、 FcVoucherUtil 的 settlementXML 方法(json 转 xml 的方法)
五、 FcVoucherUtil 的 doPost 方法(推送 NC 的方法)
一、Action
public ActionForm getZhiwu(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception {
String fdId = request.getParameter("fdId");
String fdName = request.getParameter("fdModel");
JSONObject rtn = null;
try {
String hql = "from "+fdName + " model where model.fdId=:fdId";
List list = getServiceImp(request).getBaseDao().getHibernateSession().createQuery(hql).setParameter("fdId", fdId).list();
if (!ArrayUtil.isEmpty(list)) {
rtn = getServiceImp(request).paymentStatement(list.get(0));
}
response.setCharacterEncoding("UTF-8");
response.getWriter().write(rtn.toString());
} catch (Exception e) {
response.getWriter().write("");
e.printStackTrace();
}
return null;
}
前端点击推送调用此方法把查出来的单据信息传到 service 层处理后把返回的结果写出去
返回的 Json 由 succse 和 text 组成,succse 表示是否成功,text 表示返回的响应信息
二、Service
public abstract JSONObject paymentStatement(Object object) throws Exception;
三、ServiceImpl
JSONObject rtn = new JSONObject();
rtn.put("succse", false);
String doPost = null;
if (mainModel == null) {
return rtn;
}
String id = null;
String name = null;
try {
JSONObject json = new JSONObject();
List<FcBaseIntegration> getfdKey = null;
getfdKey = getFcBaseIntegrationService().getfdKey(ContractManageYfk.class.getName());
if (ArrayUtil.isEmpty(getfdKey)) {
throw new RuntimeException("未配置NC付款结算,集成失败!");
}
List<FcBaseNcExpense> findByFdNc = getFcBaseNcExpenseService().findByFdNc(getfdKey.get(0));
ContractManageYfk contractManageYfk = (ContractManageYfk) mainModel;
if (StringUtil.isNotNull(contractManageYfk.getFdVoucherNumber())) {
rtn.put("succse", true);
return rtn;
}
id = contractManageYfk.getFdId();
name = ContractManageYfk.class.getName();
TraderManageYzgl fdUnit = contractManageYfk.getFdUnit();
json.put("pk_org", fdUnit.getFdUnitNo());
json.put("pk_group", "001");
json.put("trade_type", findByFdNc.get(0).getFdSettlementType());
json.put("bill_date", DateUtil.convertDateToString(new Date(), DateUtil.PATTERN_DATE));
json.put("creator", "zhglxt");
json.put("primal_money", contractManageYfk.getFdSqje());
json.put("local_money", contractManageYfk.getFdSqje());
json.put("memo", StringUtil.isNotNull(contractManageYfk.getFdSm()) ? contractManageYfk.getFdSm() : contractManageYfk.getDocSubject());
json.put("voucher", contractManageYfk.getFdVoucherNumber());
//修改
json.put("objecttype", "1");
json.put("pk_dept", (fdUnit.getFdIsOrg() != null && fdUnit.getFdIsOrg()) ? contractManageYfk.getFdSqbm().getFdNo() : "001");
//修改
json.put("pk_supplier", contractManageYfk.getFdSkdw().getFdNo());
json.put("pk_busiman", contractManageYfk.getDocCreator().getFdNo());
json.put("pk_account", contractManageYfk.getFdYhzh());
json.put("pk_oppaccount", contractManageYfk.getFdFkyhzh1().getFdBankAccount());
json.put("cash_item", contractManageYfk.getFdCashProject().getFdItmeCode());
json.put("lydjh", contractManageYfk.getDocNumber());
json.put("suppliername", contractManageYfk.getFdSkdw().getFdName());
json.put("pzlb", contractManageYfk.getFdVoucherType() != null ? contractManageYfk.getFdVoucherType().getFdCode() : "01");
json.put("fjds", 1);
JSONArray items = new JSONArray();
JSONObject item = new JSONObject();
item.put("pk_org", fdUnit.getFdUnitNo());
item.put("pk_account", contractManageYfk.getFdYhzh());
item.put("accounttype", "0");
item.put("accountname", contractManageYfk.getFdSkyhzh().getFdBankName());
item.put("accountopenbank", contractManageYfk.getFdSkyhzh().getFdOpenBank());
item.put("accountcode", contractManageYfk.getFdYhzh());
item.put("pk_oppaccount", contractManageYfk.getFdFkyhzh1().getFdBankAccount());
item.put("pk_supplier", contractManageYfk.getFdSkdw().getFdNo());
item.put("memo", StringUtil.isNotNull(contractManageYfk.getFdSm()) ? contractManageYfk.getFdSm() : contractManageYfk.getDocSubject());
//修改
item.put("pk_dept", (fdUnit.getFdIsOrg() != null && fdUnit.getFdIsOrg()) ? contractManageYfk.getFdSqbm().getFdNo() : "001");
item.put("pk_busiman", contractManageYfk.getDocCreator().getFdNo());
item.put("pay_primal", contractManageYfk.getFdSqje());
item.put("pay_local", contractManageYfk.getFdSqje());
item.put("cash_item", contractManageYfk.getFdCashProject().getFdItmeCode());
item.put("objecttype", "1");
item.put("creator", "zhglxt");
item.put("suppliername", contractManageYfk.getFdSkdw().getFdName());
item.put("hetong", contractManageYfk.getFdSrht().getFdHtbh());
item.put("gcxm", contractManageYfk.getFdXmbhName());
item.put("gzr", contractManageYfk.getFdAccountsPeople() != null ? contractManageYfk.getFdAccountsPeople().getFdNo() : "");
items.add(item);
json.put("items", items);
String voucherXML = null;
Boolean isTrue = false;
TransactionStatus ts = null;
try {
voucherXML = FcVoucherUtil.settlementXML(json);
doPost = FcVoucherUtil.doPost(getfdKey.get(0).getFdUrl(), getfdKey.get(0).getFdLoganName(), getfdKey.get(0).getFdPassword(), voucherXML);
Element element = DocumentHelper.parseText(doPost).getRootElement().element("sendresult");
ts = TransactionUtils.beginNewTransaction();
if ("1".equals(element.element("resultcode").getTextTrim())) {
isTrue = true;
rtn.put("succse", true);
rtn.put("text", "付款结算单:" + element.element("bdocid").getTextTrim());
if (mainModel instanceof ContractManageYfk) {
IContractManageYfkService contractManageYfkService = (IContractManageYfkService) SpringBeanUtil.getBean("contractManageYfkService");
List<ContractManageYfk> list = contractManageYfkService.getBaseDao().getHibernateSession().createQuery("from ContractManageYfk contractManageYfk where contractManageYfk.fdId = :fdId").setParameter("fdId", ((ContractManageYfk) mainModel).getFdI
for (ContractManageYfk contractManageYfk : list) {
contractManageYfk.setFdVoucherNumber(element.element("bdocid").getTextTrim());
contractManageYfkService.update(contractManageYfk);
}
}
TransactionUtils.commit(ts);
} else {
rtn.put("text", "生成付款结算单失败:" + doPost);
}
} catch (Exception e) {
voucherXML = json.toString();
e.printStackTrace();
doPost = e.toString();
rtn.put("text", "生成付款结算单失败:" + e.toString());
if (ts != null)
TransactionUtils.rollback(ts);
}
1、rth 的 JSONObject 返回的是单据是否推送成功以及 NC 返回的信息
2、json 的 JSONObject 是拼接的单据信息,并把这个 json 传给 settlementXML 方法转成一个String 类型的 XML
3、调用 FcVoucherUtil.doPost 方法推送 NC,解析NC 返回的信息修改原单据为已推动和存日志
那 json 是怎么转成 XML 的呢?让我们接着往下看
四、 FcVoucherUtil 的 settlementXML 方法(json 转 xml 的方法)
public static String settlementXML(JSONObject json){
String result = "";
// 创建一个Document实例
Document isHeaderDoc = DocumentHelper.createDocument();
// 1、添加根节点(基本信息)
Element billhead = isHeaderDoc.addElement("billhead");
billhead.addElement("pk_org").addText(json.containsKey("pk_org")?json.getString("pk_org"):"");//<!--结算财务组织,最大长度为20,类型为:String,非空 组织编码-->
billhead.addElement("pk_pcorg").addText("");//<!--利润中心,最大长度为20,类型为:String,可空-->
billhead.addElement("pk_group").addText(json.getString("pk_group"));//<!--所属集团,最大长度为20,类型为:String,非空,默认001-->
billhead.addElement("pk_paybill").addText("");
billhead.addElement("bill_no").addText("");//<!--单据编号,最大长度为40,类型为:String,可空-->
billhead.addElement("trade_type").addText(json.getString("trade_type"));//<!--付款结算类型,最大长度为50,类型为:String,非空,参考集成档案2.28-->
billhead.addElement("bill_date").addText(DateUtil.convertDateToString(new Date(),DateUtil.PATTERN_DATE));//<!--单据日期,最大长度为19,类型为:UFDate,非空,参考OA单据创建日期-->
billhead.addElement("source_flag").addText("2");//<!--来源系统,最大长度为20,类型为:String,非空,默认2-->
billhead.addElement("bill_status").addText("-1");//<!--单据状态,最大长度为0,类型为:Integer,非空,默认-1-->
billhead.addElement("creator").addText(json.getString("creator"));//<!--创建人,最大长度为20,类型为:String,非空,默认zhglxt-->
billhead.addElement("creationtime").addText(DateUtil.convertDateToString(new Date(),"yyyy-MM-dd HH:mm:ss"));//<!--创建时间,最大长度为19,类型为:UFDateTime,非空,参考单据推送时间-->
billhead.addElement("approver").addText("");//<!--审核人,最大长度为20,类型为:String,可空-->
billhead.addElement("approve_date").addText("");//<!--审核日期,最大长度为19,类型为:UFDate,可空-->
billhead.addElement("approve_time").addText("");//<!--审核时间,最大长度为19,类型为:UFDateTime,可空-->
billhead.addElement("billmaker").addText(json.getString("creator"));//<!--制单人,最大长度为20,类型为:String,可空-->
billhead.addElement("billmaker_date").addText(DateUtil.convertDateToString(new Date(),"yyyy-MM-dd HH:mm:ss"));//<!--制单日期,最大长度为19,类型为:UFDate,可空-->
billhead.addElement("primal_money").addText(json.getString("primal_money"));//<!--付款原币金额,最大长度为28,类型为:UFDouble,非空,表体付款原币金额合计-->
billhead.addElement("local_money").addText(json.getString("local_money"));//<!--付款组织本币金额,最大长度为28,类型为:UFDouble,非空,表体付款组织本币金额合计-->
billhead.addElement("pk_signer").addText("");//<!--签字人,最大长度为20,类型为:String,可空-->
billhead.addElement("pk_signdate").addText("");//<!--签字日期,最大长度为19,类型为:UFDate,可空-->
billhead.addElement("memo").addText(json.getString("memo"));//<!--备注,最大长度为181,类型为:String,非空,取备注或事由-->
billhead.addElement("pk_officialprinter").addText("");//<!--正式打印人,最大长度为20,类型为:String,可空-->
billhead.addElement("officialprint_date").addText("");//<!--正式打印日期,最大长度为19,类型为:UFDate,可空-->
billhead.addElement("pk_comfirmer").addText("");//<!--确认人,最大长度为20,类型为:String,可空-->
billhead.addElement("pk_effect_user").addText("");//<!--生效人,最大长度为20,类型为:String,可空-->
billhead.addElement("effect_flag").addText("0");//<!--生效状态,最大长度为0,类型为:Integer,非空,默认0-->
billhead.addElement("effect_year").addText("");//<!--生效年度,最大长度为4,类型为:String,可空-->
billhead.addElement("effect_period").addText("");//<!--生效期间,最大长度为2,类型为:String,可空-->
billhead.addElement("effect_date").addText("");//<!--生效日期,最大长度为19,类型为:UFDate,可空-->
billhead.addElement("payman").addText("");//<!--支付人,最大长度为20,类型为:String,可空-->
billhead.addElement("payway").addText("0");//<!--支付方式,最大长度为0,类型为:Integer,非空,默认0-->
billhead.addElement("paystatus").addText("");//<!--支付状态,最大长度为0,类型为:Integer,可空-->
billhead.addElement("paydate").addText("");//<!--支付日期,最大长度为19,类型为:UFDate,可空-->
billhead.addElement("settlenum").addText("");//<!--结算号,最大长度为30,类型为:String,可空-->
billhead.addElement("isnetready").addText("N");//<!--是否已经补录,最大长度为1,类型为:UFBoolean,可空-->
billhead.addElement("isreded").addText("N");//<!--是否已红冲,最大长度为1,类型为:UFBoolean,可空-->
billhead.addElement("pk_org_v").addText("");//<!--业务单元版本,最大长度为20,类型为:String,可空-->
billhead.addElement("pk_fiorg_v").addText("");//<!--财务组织版本,最大长度为20,类型为:String,可空-->
billhead.addElement("pk_pcorg_v").addText("");//<!--利润中心版本,最大长度为20,类型为:String,可空-->
billhead.addElement("group_local").addText("");//<!--付款集团本币金额,最大长度为28,类型为:UFDouble,可空-->
billhead.addElement("global_local").addText("");//<!--付款全局本币金额,最大长度为28,类型为:UFDouble,可空-->
billhead.addElement("group_rate").addText("");//<!--集团本币汇率,最大长度为15,类型为:UFDouble,可空-->
billhead.addElement("global_rate").addText("");//<!--全局本币汇率,最大长度为15,类型为:UFDouble,可空-->
billhead.addElement("voucher").addText(json.containsKey("voucher")?json.getString("voucher"):"");//<!--凭证号,最大长度为50,类型为:String,可空-->
billhead.addElement("objecttype").addText(json.getString("objecttype"));//<!--交易对象类型,最大长度为0,类型为:Integer,非空,0=客户,1=供应商,3=人员-->
billhead.addElement("pk_subjct").addText(json.containsKey("pk_subjct")?json.getString("pk_subjct"):"");//<!--科目,最大长度为20,类型为:String,可空-->
billhead.addElement("pk_dept").addText(json.getString("pk_dept"));//<!--部门,最大长度为20,类型为:String,非空,部门编码-->
billhead.addElement("pk_customer").addText("0".equals(json.getString("objecttype"))?json.getString("pk_customer"):"");//<!--客户,最大长度为20,类型为:String,交易对象为0非空,业主编码-->
billhead.addElement("pk_supplier").addText("1".equals(json.getString("objecttype"))?json.getString("pk_supplier"):"");//<!--供应商,最大长度为20,类型为:String,交易对象为1非空,供应商编码-->
billhead.addElement("pk_busiman").addText(json.getString("pk_busiman"));//<!--业务员,最大长度为20,类型为:String,非空,人员编码-->
billhead.addElement("pk_balatype").addText("3");//<!--结算方式,最大长度为20,类型为:String,非空,默认3-->
billhead.addElement("pk_account").addText(json.containsKey("pk_account")?json.getString("pk_account"):"");//<!--收款银行账户,最大长度为20,类型为:String,非空,取收款银行账号-->
billhead.addElement("pk_oppaccount").addText(json.getString("pk_oppaccount"));//<!--付款银行账户,最大长度为20,类型为:String,非空,取付款银行账号-->
billhead.addElement("pk_currtype").addText("CNY");//<!--币种,最大长度为20,类型为:String,非空,默认CNY-->
billhead.addElement("local_rate").addText("1.00000000");//<!--组织本币汇率,最大长度为15,类型为:UFDouble,非空,默认1.00000000-->
billhead.addElement("dz_date").addText("");//<!--对账日期,最大长度为19,类型为:UFDateTime,可空-->
billhead.addElement("payperson").addText("");//<!--付款人,最大长度为20,类型为:String,可空-->
billhead.addElement("res_issue").addText("");//<!--核算要素,最大长度为20,类型为:String,可空-->
billhead.addElement("note_type").addText("");//<!--票据类型,最大长度为20,类型为:String,可空-->
billhead.addElement("note_no").addText("");//<!--票据号,最大长度为20,类型为:String,可空-->
billhead.addElement("blanknote_no").addText("");//<!--空白票据编号,最大长度为100,类型为:String,可空-->
billhead.addElement("pk_recproject").addText("");//<!--收支项目,最大长度为20,类型为:String,可空-->
billhead.addElement("cash_item").addText(json.getString("cash_item"));//<!--现金流量项目,最大长度为20,类型为:String,非空,现金流量项目编码-->
billhead.addElement("bankroll_projet").addText("");//<!--资金计划项目,最大长度为20,类型为:String,可空-->
billhead.addElement("isrefused").addText("N");//<!--是否被退回,最大长度为1,类型为:UFBoolean,可空-->
billhead.addElement("jz_date").addText("");//<!--记账日期,最大长度为19,类型为:UFDate,可空-->
billhead.addElement("ys_period").addText("");//<!--预算期间,最大长度为2,类型为:String,可空-->
billhead.addElement("savedate").addText("");//<!--保存日期,最大长度为19,类型为:UFDate,可空-->
billhead.addElement("is_cf").addText("");//<!--是否需承付,最大长度为1,类型为:UFBoolean,可空-->
billhead.addElement("pk_jobid").addText("");//<!--项目,最大长度为20,类型为:String,可空-->
billhead.addElement("pk_jobobjpha").addText("");//<!--任务,最大长度为50,类型为:String,可空-->
billhead.addElement("mon_account").addText("");//<!--现金账户,最大长度为20,类型为:String,可空-->
billhead.addElement("cf_begindate").addText("");//<!--承付开始日期,最大长度为19,类型为:UFDate,可空-->
billhead.addElement("cf_enddate").addText("");//<!--承付结束日期,最大长度为19,类型为:UFDate,可空-->
billhead.addElement("lydjh").addText(json.getString("lydjh"));//<!--来源单据号,最大长度为20,类型为:String,非空,取OA单号-->
billhead.addElement("pzlb").addText(json.containsKey("pzlb")?json.getString("pzlb"):"01");//<!--凭证类别,最大长度为20,类型为:String,非空,取凭证类别-->
billhead.addElement("fjds").addText(json.containsKey("fjds")?json.getString("fjds"):"1");//<!--附件单数-->
Element items = billhead.addElement("items");
JSONArray itemsar = json.getJSONArray("items");
for (Object object : itemsar) {
JSONObject itemjs = (JSONObject) object;
Element item = items.addElement("item");
item.addElement("pk_org").addText(itemjs.getString("pk_org"));//<!--结算财务组织,最大长度为20,类型为:String,非空,组织编码-->
item.addElement("pk_pcorg").addText("");//<!--利润中心,最大长度为20,类型为:String,可空-->
item.addElement("pk_account").addText(itemjs.getString("pk_account"));//<!--收款银行账户,最大长度为20,类型为:String,非空,收款银行账号-->
item.addElement("accounttype").addText(itemjs.getString("accounttype"));//<!--收款银行账户性质,最大长度为1,类型为:String,非空,0=公司,1=个人-->
item.addElement("accountname").addText(itemjs.getString("accountname"));//<!--收款银行账户名称,最大长度为200,类型为:String,非空,收款银行户名-->
item.addElement("accountopenbank").addText(itemjs.getString("accountopenbank"));//<!--收款银行名称,最大长度为200,类型为:String,非空,收款银行开户行-->
item.addElement("accountcode").addText(itemjs.getString("accountcode"));//<!--收款银行账户编码,最大长度为50,类型为:String,非空,收款银行账号-->
item.addElement("pk_oppaccount").addText(itemjs.getString("pk_oppaccount"));//<!--付款银行账户,最大长度为20,类型为:String,非空,付款银行账号-->
item.addElement("pk_paybill_detail").addText("");//
item.addElement("freeitemid").addText("");//
item.addElement("teade_type").addText("D5");//<!--单据类型pk冗余,最大长度为20,类型为:String,非空,默认D5-->
item.addElement("billdetail_no").addText("0");//<!--单据分录编号,最大长度为30,类型为:Integer,非空,默认0-->
item.addElement("direction").addText("-1");//<!--方向,最大长度为0,类型为:Integer,非空,默认-1-->
item.addElement("memo").addText(itemjs.getString("memo"));//<!--摘要,最大长度为181,类型为:String,非空,备注或事由-->
item.addElement("pk_subjct").addText(itemjs.containsKey("pk_subjct")?itemjs.getString("pk_subjct"):"");//<!--科目,最大长度为20,类型为:String,可空-->
item.addElement("pk_customer").addText("0".equals(json.getString("objecttype"))?itemjs.getString("pk_customer"):"");//<!--客户,最大长度为20,类型为:String,交易对象为0非空,业主编码-->
item.addElement("pk_supplier").addText("1".equals(json.getString("objecttype"))?itemjs.getString("pk_supplier"):"");//<!--供应商,最大长度为20,类型为:String,交易对象为1非空,供应商编码-->
item.addElement("pk_currtype").addText("CNY");//<!--币种,最大长度为20,类型为:String,非空,默认CNY-->
item.addElement("local_rate").addText("1.00000000");//<!--组织本币汇率,最大长度为15,类型为:UFDouble,非空,默认1.00000000-->
item.addElement("price").addText("");//<!--单价,最大长度为28,类型为:UFDouble,可空-->
item.addElement("pk_jobid").addText("");//<!--项目,最大长度为20,类型为:String,可空-->
item.addElement("pk_dept").addText(itemjs.getString("pk_dept"));//<!--部门,最大长度为20,类型为:String,非空,部门编码-->
item.addElement("pk_busiman").addText(itemjs.getString("pk_busiman"));//<!--业务员,最大长度为20,类型为:String,非空,创建人编码-->
item.addElement("pay_primal").addText(itemjs.getString("pay_primal"));//<!--付款原币金额,最大长度为28,类型为:UFDouble,非空,付款金额-->
item.addElement("pay_local").addText(itemjs.getString("pay_local"));//<!--付款组织本币金额,最大长度为28,类型为:UFDouble,非空,付款金额-->
item.addElement("pay_count").addText("");//<!--数量,最大长度为20,类型为:UFDouble,可空-->
item.addElement("cinventoryid").addText("");//<!--物料基本信息,最大长度为20,类型为:String,可空-->
item.addElement("openbill_date").addText("");//<!--开票日期,最大长度为19,类型为:UFDate,可空-->
item.addElement("note_type").addText("");//<!--票据类型,最大长度为20,类型为:String,可空-->
item.addElement("note_no").addText("");//<!--票据号,最大长度为20,类型为:String,可空-->
item.addElement("blanknote_no").addText("");//<!--空白票据编号,最大长度为100,类型为:String,可空-->
item.addElement("pk_balatype").addText("3");//<!--结算方式,最大长度为20,类型为:String,非空,默认3-->
item.addElement("sanhu").addText("");//<!--散户,最大长度为20,类型为:String,可空-->
item.addElement("facard_no").addText("");//<!--固定资产号,最大长度为50,类型为:String,可空-->
item.addElement("cash_item").addText(itemjs.getString("cash_item"));//<!--现金流量项目,最大长度为20,类型为:String,非空,现金流量项目编码-->
item.addElement("payman").addText("");//<!--支付人,最大长度为20,类型为:String,可空-->
item.addElement("paystatus").addText("");//<!--支付状态,最大长度为0,类型为:Integer,可空-->
item.addElement("paydate").addText("");//<!--支付日期,最大长度为19,类型为:UFDate,可空-->
item.addElement("bill_date").addText(DateUtil.convertDateToString(new Date(),DateUtil.PATTERN_DATE));//<!--单据日期,最大长度为19,类型为:UFDate,非空,OA创建日期-->
item.addElement("pk_jobobjpha").addText("");//<!--任务,最大长度为50,类型为:String,可空-->
item.addElement("djxtflag").addText("N");//<!--单据协同状态,最大长度为1,类型为:UFBoolean,可空-->
item.addElement("objecttype").addText(itemjs.getString("objecttype"));//<!--交易对象类型,最大长度为0,类型为:Integer,非空,0=客户,1=供应商,3=人员-->
item.addElement("pk_trader").addText("");//
item.addElement("bankroll_projet").addText("");//<!--资金计划项目,最大长度为20,类型为:String,可空-->
item.addElement("trade_type").addText("D5");//<!--单据类型编码,最大长度为20,类型为:String,非空,默认D5-->
item.addElement("pk_group").addText("001");//<!--所属集团,最大长度为20,类型为:String,非空,默认001-->
item.addElement("creator").addText(itemjs.getString("creator"));//<!--创建人,最大长度为20,类型为:String,非空,默认zhglxt-->
item.addElement("creationtime").addText(DateUtil.convertDateToString(new Date(),"yyyy-MM-dd HH:mm:ss"));//<!--创建时间,最大长度为19,类型为:UFDateTime,非空,推送时间-->
item.addElement("modifier").addText("");//<!--修改人,最大长度为20,类型为:String,可空-->
item.addElement("modifiedtime").addText("");//<!--修改时间,最大长度为19,类型为:UFDateTime,可空-->
item.addElement("pk_org_v").addText("");//<!--业务单元版本,最大长度为20,类型为:String,可空-->
item.addElement("pk_fiorg_v").addText("");//<!--财务组织版本,最大长度为20,类型为:String,可空-->
item.addElement("pk_pcorg_v").addText("");//<!--利润中心版本,最大长度为20,类型为:String,可空-->
item.addElement("group_local_pay").addText("");//<!--付款集团本币金额,最大长度为28,类型为:UFDouble,可空-->
item.addElement("global_local_pay").addText("");//<!--付款全局本币金额,最大长度为28,类型为:UFDouble,可空-->
item.addElement("group_rate").addText("");//<!--集团本币汇率,最大长度为15,类型为:UFDouble,可空-->
item.addElement("global_rate").addText("");//<!--全局本币汇率,最大长度为15,类型为:UFDouble,可空-->
item.addElement("ts_primal").addText("");//<!--托收原币金额,最大长度为28,类型为:UFDouble,可空-->
item.addElement("ts_local").addText("");//<!--托收组织本币金额,最大长度为28,类型为:UFDouble,可空-->
item.addElement("group_local_ts").addText("");//<!--托收集团本币金额,最大长度为28,类型为:UFDouble,可空-->
item.addElement("global_local_ts").addText("");//<!--托收全局本币金额,最大长度为28,类型为:UFDouble,可空-->
item.addElement("rec_flag").addText("");//<!--收入标志,最大长度为1,类型为:UFBoolean,可空-->
item.addElement("res_issue").addText("");//<!--核算要素,最大长度为20,类型为:String,可空-->
item.addElement("mon_account").addText("");//<!--现金账户,最大长度为20,类型为:String,可空-->
item.addElement("cf_man").addText("");//<!--承付人,最大长度为20,类型为:String,可空-->
item.addElement("cf_type").addText("");//<!--承付类型,最大长度为0,类型为:Integer,可空-->
item.addElement("cf_status").addText("");//<!--承付状态,最大长度为0,类型为:Integer,可空-->
item.addElement("refusenote").addText("");//<!--拒付理由,最大长度为181,类型为:String,可空-->
item.addElement("is_refuse").addText("N");//<!--是否被拒付,最大长度为1,类型为:UFBoolean,非空,默认N-->
item.addElement("pk_recproject").addText("");//<!--收支项目,最大长度为20,类型为:String,可空-->
item.addElement("bill_type").addText("F5");//<!--单据类型,最大长度为2,类型为:String,非空,默认F5-->
item.addElement("billclass").addText("fj");//<!--单据大类,最大长度为2,类型为:String,非空,默认fj-->
item.addElement("pk_billtypeid").addText("");//<!--单据类型PK,最大长度为20,类型为:String,可空-->
item.addElement("pk_tradetypeid").addText("");//<!--单据交易类型PK,最大长度为20,类型为:String,可空-->
item.addElement("suppliername").addText(itemjs.containsKey("suppliername")?itemjs.getString("suppliername"):"");//<!--供应商名称,最大长度为200,类型为:String,可空,供应商名称-->交易对象控制
item.addElement("customername").addText(itemjs.containsKey("customername")?itemjs.getString("customername"):"");//<!--客户名称,最大长度为200,类型为:String,可空,客户名称-->交易对象控制
item.addElement("bankrelated_code").addText("");//<!--对账标识码,最大长度为100,类型为:String,可空-->
item.addElement("duty_issue").addText("");//<!--责任要素,最大长度为20,类型为:String,可空-->
item.addElement("hetong").addText(itemjs.containsKey("hetong")?itemjs.getString("hetong"):"");//<!--合同,最大长度为20,类型为:String,可空,合同编码-->
item.addElement("gcxm").addText(itemjs.containsKey("gcxm")?itemjs.getString("gcxm"):"");//<!--工程项目,最大长度为20,类型为:String,可空,项目编码-->
item.addElement("bklx").addText(itemjs.containsKey("bklx")?itemjs.getString("bklx"):"");//<!--板块类型,最大长度为20,类型为:String,可空,板块类型编码-->
item.addElement("ywlx").addText(itemjs.containsKey("ywlx")?itemjs.getString("ywlx"):"");//<!--业务类型,最大长度为20,类型为:String,可空,业务类型编码-->
item.addElement("yfxm").addText(itemjs.containsKey("yfxm")?itemjs.getString("yfxm"):"");//<!--研发项目,最大长度为20,类型为:String,可空,研发项目编码-->
item.addElement("jieduan").addText(itemjs.containsKey("jieduan")?itemjs.getString("jieduan"):"");//<!--阶段,最大长度为20,类型为:String,可空,阶段编码-->
item.addElement("zcmx").addText(itemjs.containsKey("zcmx")?itemjs.getString("zcmx"):"");//<!--支出明细,最大长度为20,类型为:String,可空,支出明细编码-->
item.addElement("sylx").addText(itemjs.containsKey("sylx")?itemjs.getString("sylx"):"");//<!--收益类型,最大长度为20,类型为:String,可空,收益类型编码-->
item.addElement("pjlx").addText(itemjs.containsKey("pjlx")?itemjs.getString("pjlx"):"");//<!--票据类型,最大长度为20,类型为:String,可空,发票类型编码-->
item.addElement("shuilv").addText(itemjs.containsKey("shuilv")?itemjs.getString("shuilv"):"");//<!--税率,最大长度为20,类型为:String,可空,税率编码-->
item.addElement("hsje").addText(itemjs.containsKey("hsje")?itemjs.getString("hsje"):"");//<!--含税金额,最大长度为20,类型为:String,可空,含税金额或发票金额-->
item.addElement("shuie").addText(itemjs.containsKey("shuie")?itemjs.getString("shuie"):"");//<!--税额,最大长度为20,类型为:String,可空,税额-->
item.addElement("bhsje").addText(itemjs.containsKey("bhsje")?itemjs.getString("bhsje"):"");//<!--不含税金额,最大长度为20,类型为:String,可空,不含税金额-->
item.addElement("cxjkje").addText(itemjs.containsKey("cxjkje")?itemjs.getString("cxjkje"):"");//<!--冲销借款金额,最大长度为20,类型为:String,可空,冲销借款金额-->
item.addElement("fylx").addText(itemjs.containsKey("fylx")?itemjs.getString("fylx"):"");//<!--费用类型,最大长度为20,类型为:String,可空,费用类别、付款类别等编码-->
item.addElement("cjyfk").addText(itemjs.containsKey("cjyfk")?itemjs.getString("cjyfk"):"");//<!--冲减预付款,最大长度为20,类型为:String,可空,冲销预付款金额-->
item.addElement("gzr").addText(itemjs.containsKey("gzr")?itemjs.getString("gzr"):"");//<!--挂账人,最大长度为20,类型为:String,非空,挂账人编码-->
}
billhead.addElement("billclass").addText("fj");//<!--单据大类,最大长度为2,类型为:String,非空,默认fj-->
billhead.addElement("bill_type").addText("F5");//<!--单据类型,最大长度为2,类型为:String,非空,默认F5-->
billhead.addElement("suppliername").addText(json.containsKey("suppliername")?json.getString("suppliername"):"");//<!--供应商名称,最大长度为200,类型为:String,非空,供应商名称-->根据交易对象
billhead.addElement("customername").addText(json.containsKey("customername")?json.getString("customername"):"");//<!--客户名称,最大长度为200,类型为:String,非空,客户名称-->
billhead.addElement("duty_issue").addText("");//<!--责任要素,最大长度为20,类型为:String,可空-->
billhead.addElement("ntberrmsg").addText("");//<!--预算出错提示,最大长度为200,类型为:String,可空-->
billhead.addElement("haspassedntbcheck").addText("");//<!--是否通过预算检查,最大长度为1,类型为:UFBoolean,非空,默认N-->
result = headXML(json.containsKey("pk_org")?json.getString("pk_org"):"",json.containsKey("pk_group")?json.getString("pk_group"):"")+footXML(isHeaderDoc);
return "<?xml version='1.0' encoding='UTF-8'?>"+result;
}
public static String headXML(String pk_org,String pk_group){
String result = "<ufinterface account='develop' billtype='cmppaybill'"
+ " businessunitcode='' filename='' groupcode='"+pk_group+"' "
+ "isexchange='Y' orgcode='"+pk_org+"' receiver='1101' replace='Y' roottag='' sender='01'>"
+ "<bill id=''>";
return result;
}
public static String footXML(Document document){
String re = "<\\?xml version=\"1.0\" encoding=\"UTF-8\"\\?>";
String replaceAll = document.asXML().replaceAll(re,"");
return replaceAll+"</bill></ufinterface>";
}
调用 headXML 方法拼接 XML 的头部,调用 footXML 把 document 对象传入拼接XML 的身体
拿着这个拼接好的 XML、url、userName、pwd 调用 FcVoucherUtil.doPost 方法推送NC
那这个 doPost 方法又是怎么做推送到nc的呢?让我们接着往下看
五、 FcVoucherUtil 的 doPost 方法(推送 NC 的方法)
public static String doPost(String url,String userName,String pwd,String xml){
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
// 创建Http Post请求
HttpPost httpPost = new HttpPost(url);
// 创建请求内容
httpPost.setHeader("HTTP Method", "POST");
httpPost.setHeader("Connection", "Keep-Alive");
httpPost.setHeader("Content-Type", "text/xml; charset=UTF-8");
httpPost.setHeader("Authorization", getHeader(userName, pwd));
httpPost.setHeader("Username",userName);
httpPost.setHeader("Password",pwd);
StringEntity entity = new StringEntity(xml, "UTF-8");
entity.setContentType("text/xml;charset=UTF-8");
httpPost.setEntity(entity);
// 执行http请求
response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
} else {
String tmp = EntityUtils.toString(response.getEntity(), "UTF-8");
throw new RuntimeException(tmp);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if(response!=null){
response.close();
}
} catch (Exception e) {
e.printStackTrace();
}
if(httpClient!=null){
try{
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return resultString;
}
private static String getHeader(String userName,String pwd) {
String auth = userName + ":" + pwd;
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
String authHeader = "Basic " + new String(encodedAuth);
return authHeader;
}
1、创建一个 http 对象,在请求头中设置 token 等信息
token:把用户名和密码 转成数组后用 字符集 进行编码(调用 Base64的 encodeBase64 方法进行编码 ),编码完成后再转成 String
2、请求体中放入组装好的 XML
3、执行请求后再解析响应结果