1.清账接口

--s
This commit is contained in:
weiyunlong 2025-03-21 16:42:25 +08:00
parent ba6dffb339
commit 7ccbc805f9
2 changed files with 242 additions and 6 deletions

View File

@ -0,0 +1,198 @@
package shjh.jhzj7.fi.fi.plugin.operate;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;
import shjh.jhzj7.fi.fi.utils.JhzjUtils;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import static shjh.jhzj7.fi.fi.utils.SapUtils.sap_accounVoucher;
/**
* 付款单推送SAP凭证操作插件
* shjh_cas_paybill_ext
*/
public class PaybillPushSapOperation extends AbstractOperationServicePlugIn implements Plugin {
@Override
public void afterExecuteOperationTransaction(AfterOperationArgs e) {
super.afterExecuteOperationTransaction(e);
String operationKey = e.getOperationKey();
if ("pushsap".equals(operationKey)) {
DynamicObject[] dataEntities = e.getDataEntities();
for (DynamicObject bill : dataEntities) {
bill = BusinessDataServiceHelper.loadSingle(bill.getPkValue(), "cas_paybill");
//主动:{1.fk:不清帐,直传付款凭证给sap;2.SAP:要清账,都传} 被动:{不用传清账} FKZJ2025030040
JSONObject data = new JSONObject();
data.put("IS_HEADER", getIS_HEADER(bill));//抬头
data.put("IT_ITEM", getIT_ITEM(bill));
//只有SAP需要传清账数据
String shjhSourcesystem = bill.getString("shjh_sourcesystem");//来源系统
if ("A".equals(shjhSourcesystem)) {
JSONArray IT_CLEAR = new JSONArray();//清账数据参考IT_CLEAR仅清账需要输入
data.put("IT_CLEAR", IT_CLEAR);
}
JSONArray IS_CUSTOMERCPD = new JSONArray();//一次性供应商或客户信息仅需要填写
data.put("IS_CUSTOMERCPD", IS_CUSTOMERCPD);
sap_accounVoucher(data,bill.getString("billno"));
}
}
}
private JSONObject getIS_HEADER(DynamicObject bill){
JSONObject IS_HEADER = new JSONObject();//抬头
String BUKRS = "";
if (null != bill.getDynamicObject("openorg")) {
BUKRS = bill.getDynamicObject("openorg").getString("number");
}
IS_HEADER.put("BUKRS",BUKRS);//公司代码_核算组织(付款单)
IS_HEADER.put("BLART","KZ");//凭证类型
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date bizdate = bill.getDate("bizdate");
String bizdate1 = "";
if (null != bizdate) {
bizdate1 = format.format(bizdate);
}
IS_HEADER.put("BLDAT",bizdate1);//凭证日期_业务日期(付款单bizdate)
Date acttradedate = bill.getDate("acttradedate");
String acttradedate1 = "";
if (null != acttradedate) {
acttradedate1 = format.format(acttradedate);
}
IS_HEADER.put("BUDAT",acttradedate1);//过账日期_实际交易日期(付款单acttradedate)
String isoCode = bill.getDynamicObject("currency").getString("number");
IS_HEADER.put("WAERS", JhzjUtils.getCurrencyCode(isoCode));//币种_付款币种(付款单currency)
IS_HEADER.put("XBLNR",bill.getString("billno"));//参考凭证号_单据编号(付款单billno)
IS_HEADER.put("BKTXT",bill.getString("payeename"));//凭证抬头文本_收款人名称(付款单payeename)
IS_HEADER.put("USNAM","资金系统");//用户名
return IS_HEADER;
}
private JSONArray getIT_ITEM(DynamicObject bill){
//客户(bd_customer)||供应商(bd_supplier) 收款人id:payee
String payeetype = bill.getString("payeetype");//收款人类型
String customerOrsupnum = getCustomerOrSupplierNumber(payeetype, bill);
//原因码(付款明细)
String RSTGR = "";
String SGTXT = "";
DynamicObjectCollection entrys = bill.getDynamicObjectCollection("entry");
if (!entrys.isEmpty()) {
DynamicObject entry = entrys.get(0);
DynamicObject shjhYym = entry.getDynamicObject("shjh_yym");
if (null != shjhYym) {
RSTGR = shjhYym.getString("number");
}
SGTXT = entry.getString("shjh_voucherentrynum");//SAP会计科目行项目号
}
//成本中心
String KOSTL = "";
DynamicObject shjhCostcenter = bill.getDynamicObject("shjh_costcenter");
if (null != shjhCostcenter) {
KOSTL = shjhCostcenter.getString("number");//成本中心
}
//利润中心
String PRCTR = "";
DynamicObject shjhPc = bill.getDynamicObject("shjh_pc");
if (null != shjhPc) {
PRCTR = shjhPc.getString("number");//利润中心
}
JSONArray IT_ITEM = new JSONArray();
DynamicObject gl_voucher = BusinessDataServiceHelper.loadSingle("gl_voucher",
"id,sourcebill,entries,entries.account,entries.debitlocal,entries.creditlocal,entries.entrydc",
new QFilter("sourcebill", QCP.equals, bill.getPkValue()).toArray());
if (null != gl_voucher) {
DynamicObjectCollection entries = gl_voucher.getDynamicObjectCollection("entries");
if (!entries.isEmpty()) {
for (DynamicObject entry : entries) {
JSONObject IT_ITEMS = new JSONObject();
switch (payeetype) {
case "bd_customer":
IT_ITEMS.put("KUNNR", customerOrsupnum); // 供应商编号_收款人id(付款单payee)
break;
case "bd_supplier":
IT_ITEMS.put("LIFNR", customerOrsupnum); // 客户编号_收款人id(付款单payee)
break;
default:
break;
}
DynamicObject account = entry.getDynamicObject("account");//科目
if (null != account) {
IT_ITEMS.put("HKONT", account.getString("number")); // 总账科目_科目(凭证分录account)
}
// 获取借方金额
BigDecimal debitlocal = (BigDecimal) entry.get("debitlocal");//借方
// 获取贷方金额
BigDecimal creditlocal = (BigDecimal) entry.get("creditlocal");//贷方
// 获取分录方向
String entrydc = (String) entry.get("entrydc");//分录方向(1.借方,-1.贷方)
// 进行空值检查
if (debitlocal != null && creditlocal != null && entrydc != null) {
BigDecimal amountToPut = BigDecimal.ZERO;
if ("1".equals(entrydc)) {
amountToPut = debitlocal;
} else if ("-1".equals(entrydc)) {
amountToPut = creditlocal.negate();
} else {
// 处理分录方向不符合预期的情况
System.err.println("Invalid entry direction: " + entrydc);
}
IT_ITEMS.put("DMBTR", String.valueOf(amountToPut));
} else {
// 处理存在空值的情况
System.err.println("One or more values are null.");
}
IT_ITEMS.put("RSTGR", "014");//原因代码_原因码(付款明细shjh_yym)_必填
IT_ITEMS.put("SGTXT", SGTXT);//行项目文本_SAP会计科目行项目号(付款明细shjh_voucherentrynum)
IT_ITEMS.put("KOSTL",KOSTL);//成本中心_成本中心(付款单shjh_costcenter)
IT_ITEMS.put("PRCTR",PRCTR);//利润中心_利润中心(付款单shjh_pc)
IT_ITEM.add(IT_ITEMS);
}
}
}
return IT_ITEM;
}
/**
* 根据收款人类型获取收款人编号
* @param payeetype
* @param bill
* @return
*/
private String getCustomerOrSupplierNumber(String payeetype, DynamicObject bill) {
String customerOrsupnum = "";
String objectType = null;
switch (payeetype) {
case "bd_customer":
objectType = "bd_customer";
break;
case "bd_supplier":
objectType = "bd_supplier";
break;
}
if (objectType != null) {
DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(Long.valueOf(bill.getString("payee")), objectType);
if (dynamicObject != null) {
customerOrsupnum = dynamicObject.getString("number");
}
}
return customerOrsupnum;
}
}

View File

@ -279,7 +279,6 @@ public class SapUtils {
/**
* 生成代扣款记账单接口组装请求体:
*
* @return 请求体的 JSON 对象
*/
private static JSONObject withholdingBody(JSONObject data) {
@ -322,7 +321,6 @@ public class SapUtils {
/**
* 生成代扣款记账单接口组装请求体:
*
* @return 请求体的 JSON 对象
*/
private static JSONObject backpaymentBody(JSONObject data) {
@ -334,12 +332,55 @@ public class SapUtils {
String dates = date.format(formatter);
//组装请求体
JSONObject pendingsBody = new JSONObject();
pendingsBody.put("rootContextID", "" + System.currentTimeMillis());//todo:唯一事务ID采用UUID或其他强唯一性ID
pendingsBody.put("rootContextID", "" + System.currentTimeMillis());//唯一事务ID采用UUID或其他强唯一性ID
pendingsBody.put("requestTime", dates);//请求时间,格式为yyyy-MM-dd HH:mm:ss.SSS
pendingsBody.put("data", data);
return pendingsBody;
}
/**
* SAP会计凭证
*/
public static JSONObject sap_accounVoucher(JSONObject data, String billno) {
Map<String, Object> thirdPartyMap = new HashMap<>();
thirdPartyMap.put("interfaceID", interfaceID7);
thirdPartyMap.put("receiverID", receiverID1);
thirdPartyMap.put("URL", sapArUrl);
thirdPartyMap.put("billno", billno);
// 获取当前日期和时间
LocalDateTime date = LocalDateTime.now();
// 定义日期时间格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
// 格式化当前日期和时间
String dates = date.format(formatter);
thirdPartyMap.put("createdatetime", dates);
// 组装请求体
JSONObject sapAccounVoucherBody = sapAccounVoucherBody(data);
// 发送请求并处理响应
return processRequest(sapAccounVoucherBody, thirdPartyMap, Apimenthod7);
}
/**
* SAP会计凭证组装请求体:
* @return 请求体的 JSON 对象
*/
private static JSONObject sapAccounVoucherBody(JSONObject data) {
// 获取当前日期和时间
LocalDateTime date = LocalDateTime.now();
// 定义日期时间格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
// 格式化当前日期和时间
String dates = date.format(formatter);
//组装请求体
JSONObject pendingsBody = new JSONObject();
pendingsBody.put("rootContextID", "" + System.currentTimeMillis());//唯一事务ID采用UUID或其他强唯一性ID
pendingsBody.put("requestTime", dates);//请求时间,格式为yyyy-MM-dd HH:mm:ss.SSS
pendingsBody.put("data", data);
return pendingsBody;
}
/**
* 发送请求并处理响应
*
@ -405,7 +446,6 @@ public class SapUtils {
/**
* 解析响应 JSON 字符串
*
* @param response 响应的 JSON 字符串
* @param apiName 接口名称
* @return 解析后的 JSON 对象
@ -421,7 +461,6 @@ public class SapUtils {
/**
* 处理响应结果
*
* @param jsonObject 响应的 JSON 对象
* @param pendingsBody 请求体
* @param thirdPartyMap 第三方数据映射
@ -445,7 +484,6 @@ public class SapUtils {
/**
* 处理异常情况
*
* @param e 捕获的异常
* @param pendingsBody 请求体
* @param thirdPartyMap 第三方数据映射