sap凭证接口

This commit is contained in:
yuxueliang0813 2025-03-27 18:02:30 +08:00
parent 8cf1ef3ec7
commit abb4ce6f13
4 changed files with 435 additions and 6 deletions

View File

@ -0,0 +1,393 @@
package shjh.jhzj7.fi.fi.plugin.form;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.TypeUtils;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.db.DB;
import kd.bos.db.DBRoute;
import kd.bos.form.control.Button;
import kd.bos.form.control.Control;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.id.ID;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import shjh.jhzj7.fi.fi.utils.EsbUtils;
import shjh.jhzj7.fi.fi.utils.JhzjUtils;
import java.util.Date;
import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;
/**
* 动态表单插件-科目初始化 shjh_init_account
* @author yuxueliang
*/
public class InitAccountFormPlugin extends AbstractFormPlugin {
private final static Log log = LogFactory.getLog(InitAccountFormPlugin.class);
private static final String OK_BUTTON_KEY = "btnok";//确认按钮
private static final String entityName = "bd_accountview";//会计科目实体 财务库 表名 T_BD_Account
private static final String acctTypeName = "bd_accounttype";//科目类型
private static final String hswdName = "bd_asstacttype";//系统库 核算维度 表名t_bas_flex_property
private static final String orgName = "bos_org";//业务单元
private static final String bbName = "bd_currency";//币别
private static final Map<String, DynamicObject> mapObject = new HashMap<>();//公司对象集合
private static final Map<String, DynamicObject> hsxmMaps = new HashMap<>();//核算维度对象集合
private static final String insertSql = "insert into T_BD_Account_U (fdataid,fuseorgid) values (?,?);";
/**
* 按钮监听注册
*/
@Override
public void registerListener(EventObject e) {
super.registerListener(e);
//确认按钮
Button selectedButton = this.getView().getControl(OK_BUTTON_KEY);
selectedButton.addClickListener(this);
}
/**
* 按钮点击实现方法
*/
@Override
public void click(EventObject evt) {
try {
Control source = (Control) evt.getSource();
String key = source.getKey();
if (StringUtils.equals(OK_BUTTON_KEY, key)) {
// 点击确认按钮
String params = (String) this.getModel().getValue("shjh_largetextfield");
JSONObject json_obj = JSONObject.parseObject(params);
JSONArray detailsJson = json_obj.getJSONArray("items");
if(detailsJson == null){
this.getView().showMessage("未识别到items参数");
return ;
}
JSONArray itemsJson = new JSONArray();//返回值明细集合
JSONObject itemInfo;//返回值明细对象
String number;//科目编号
String name;//科目名称
String acctType;//科目类型 资产负债类 损益类
DynamicObject acctInfo;
DynamicObject acctTypeInfo;
JSONObject json_body;
Map<String, Long> acctids = new HashMap<>();//科目编号和ID对应关系
Map<String, DynamicObject> accountMaps = new HashMap<>();//将科目编号与对象关联
//先根据基础信息在集团层面生成科目不考虑核算维度使用逐级分配
//再根据公司信息进行科目分配
//最后处理每个公司下科目的核算维度和禁用状态
for (int i = 0; i < detailsJson.size(); i++) {
json_body = detailsJson.getJSONObject(i);
number = json_body.getString("code");
name = json_body.getString("name");
acctType = json_body.getString("type");
if(EsbUtils.isEmpty(number) || EsbUtils.isEmpty(name) || EsbUtils.isEmpty(acctType)){
log.error(String.format("会计科目接口入参为空异常:%s", json_body.toJSONString()));
itemInfo = new JSONObject();
itemInfo.put("code",number);
itemInfo.put("error","入参值为空");
itemsJson.add(itemInfo);
continue;
}
//根据科目编号和集团ID查找对应科目是否已存在
acctInfo = BusinessDataServiceHelper.loadSingle(entityName,new QFilter[]{new QFilter("number","=",number),
new QFilter("createorg.id","=", JhzjUtils.GROUPID)});
if(acctInfo != null){
//处理科目更新逻辑此时能修改哪些字段名称 是否可用
if("0".equals(json_body.getString("status")) || !name.equals(acctInfo.getString("name"))){
acctInfo.set("enable", 0);//科目禁用处理
acctInfo.set("name", name);
acctInfo.set("fullname", name);
SaveServiceHelper.save(new DynamicObject[]{acctInfo});
}
}else{
acctTypeInfo = BusinessDataServiceHelper.loadSingleFromCache(acctTypeName,new QFilter[]{new QFilter("number","=",acctType)});
if(acctTypeInfo == null){
log.error(String.format("科目类型在金蝶中找不到:%s", acctType));
itemInfo = new JSONObject();
itemInfo.put("code",number);
itemInfo.put("error","科目类型在金蝶中找不到");
itemsJson.add(itemInfo);
continue;
}
//不存在做新增 根据实体名称创建动态对象
acctInfo = BusinessDataServiceHelper.newDynamicObject(entityName);
acctInfo.set("accounttable", EsbUtils.ACCTABLE);//科目表fid上正式时注意此ID
acctInfo.set("number", number);
acctInfo.set("name", name);
//处理父级科目
// parentAcctInfo = getParentAcct(number);
acctInfo.set("longnumber", number);
acctInfo.set("fullname", name);
// acctInfo.set("parent", parentAcctInfo.getLong("id"));
//创建组织
acctInfo.set("createorg", JhzjUtils.GROUPID);//创建组织
acctInfo.set("org", JhzjUtils.GROUPID);//管理组织
//科目类型
acctInfo.set("accounttype", acctTypeInfo);
//币别核算--外币核算类型--集团科目新增时默认为allcurrency
acctInfo.set("acctcurrency", "allcurrency");//不核算外币nocurrency 指定核算币别descurrency 核算所有币别allcurrency
acctInfo.set("ctrlstrategy", "1");//控制策略 5全局共享 7私有 1逐级分配
acctInfo.set("control", "nocontrol");//受控系统 nocontrol 应付 应收 资产
//损益类型
acctInfo.set("pltype", getSY(acctType));
acctInfo.set("dc", 1);//余额方向 默认为借
acctInfo.set("level", 1);//级次 默认为1
acctInfo.set("isleaf", true);//明细科目 默认为是
acctInfo.set("startdate", new Date());//版本化日期
acctInfo.set("enddate", TypeUtils.castToDate("2999-12-31"));//失效日期
acctInfo.set("accrualdirection", "nocontrol");//科目录入方向控制 不控制nocontrol 借方debit 贷方credit
acctInfo.set("orgcontrollevel", 1);//控制级次 默认为1
acctInfo.set("isallowca", false);//允许公司增加下级科目 由接口同步 为false时控制级次可以不设置
acctInfo.set("ismanual", false);//手工录入 由接口同步
acctInfo.set("iscash", false);//现金科目 默认false sap不区分
acctInfo.set("isbank", false);//银行科目 默认false sap不区分
acctInfo.set("iscashequivalent", false);//现金等价物 默认false sap不区分
acctInfo.set("isjournal", false);//登日记账 现金等价物时需要勾选
acctInfo.set("enable", 1);//是否启用
acctInfo.set("status", "C");//单据状态 A保存 B已提交 C已审核
acctInfo.set("creator", RequestContext.get().getCurrUserId());//创建人
//手动指定科目的金蝶id
long kmId = ID.genLongId();
acctInfo.set("id", kmId);
acctInfo.set("masterid", kmId);//主数据内码系统不会根据id自动生成需要手动设置
//保存数据直接保存入库不走操作校验
SaveServiceHelper.save(new DynamicObject[]{acctInfo});
//处理科目使用范围
DB.update(DBRoute.of("fi"), insertSql, new Object[]{kmId,JhzjUtils.GROUPID});
}
acctids.put(number,acctInfo.getLong("id"));
accountMaps.put(number,acctInfo);
}
JSONArray companysJson = json_obj.getJSONArray("companys");
//处理科目在每个公司生成
Map<String, DynamicObject> companyAcctMaps = handleAccountCompany(accountMaps,companysJson);
//处理科目分配和反分配
EsbUtils.handleAssign(companysJson, acctids, entityName);
EsbUtils.handleUnAssign(companysJson, acctids, entityName);
//处理每个公司下科目的核算维度和禁用状态
JSONArray asstacttypesJson = json_obj.getJSONArray("asstacttypes");
if(asstacttypesJson != null){
String hsxm;//核算维度名称 基础资料
DynamicObject hsxmInfo;//核算维度对象
DynamicObjectCollection dochswd;
String companynum;
DynamicObject checkitementryInfo = null;
for (int i = 0; i < asstacttypesJson.size(); i++) {
json_body = asstacttypesJson.getJSONObject(i);
hsxm = json_body.getString("asstactname");
number = json_body.getString("code");
if(EsbUtils.isEmpty(hsxm)){
log.error(String.format("核算维度为空:%s", number));
itemInfo = new JSONObject();
itemInfo.put("code",number);
itemInfo.put("error","核算维度为空"+number);
itemsJson.add(itemInfo);
continue;
}
companynum = json_body.getString("companynum");
//根据科目核算维度中的科目编号和公司编号获取对应科目对象不存在的不处理
acctInfo = companyAcctMaps.get(number+companynum);
if(acctInfo == null){
continue;
}
hsxmInfo = getHsxmInfo(hsxm);
if(hsxmInfo == null){
log.error(String.format("核算维度在金蝶中找不到:%s", hsxm));
itemInfo = new JSONObject();
itemInfo.put("code",number);
itemInfo.put("error","核算维度在金蝶中找不到"+hsxm);
itemsJson.add(itemInfo);
continue;
}
//判断当前科目是否存在此核算维度不存在则新增存在则判断是否必录
dochswd = acctInfo.getDynamicObjectCollection("checkitementry");
for (int j = 0; j < dochswd.size(); j++) {
checkitementryInfo = dochswd.get(j);
if(hsxmInfo.getLong("id") == checkitementryInfo.getDynamicObject("asstactitem").getLong("id")){
break;
}
checkitementryInfo = null;
}
if(checkitementryInfo == null || dochswd.isEmpty()){
checkitementryInfo = dochswd.addNew();
checkitementryInfo.set("asstactitem",hsxmInfo);
checkitementryInfo.set("isdetail", true);//明细
}
if("1".equals(json_body.getString("isneed"))){
checkitementryInfo.set("isrequire", true);//必录
}else{
checkitementryInfo.set("isrequire", false);//必录
}
acctInfo.set("isassist", true);//主表的isassist是否包含核算项目为是
SaveServiceHelper.save(new DynamicObject[]{acctInfo});
companyAcctMaps.put(number+companynum,acctInfo);
}
}
if(!itemsJson.isEmpty()){
this.getView().showMessage("科目数据处理异常"+itemsJson.toJSONString());;
return;
}
this.getView().showMessage("处理成功");
}
} catch (Exception e) {
log.error("按钮处理异常:"+e.getMessage());
this.getView().showMessage("按钮处理异常"+e.getMessage());
}
}
private Map<String, DynamicObject> handleAccountCompany(Map<String, DynamicObject> accountMaps,JSONArray companysJson){
Map<String, DynamicObject> baseMaps = new HashMap<>();
if(companysJson == null){
return baseMaps;
}
JSONObject json_body;
DynamicObject oldAcctInfo;
DynamicObject newAcctInfo;
DynamicObject hsorgInfo;
String compnum;
String acctnum;
for (int i = 0; i < companysJson.size(); i++) {
json_body = companysJson.getJSONObject(i);
acctnum = json_body.getString("code");//科目编号
compnum = json_body.getString("companynum");//公司编号
if(EsbUtils.isEmpty(acctnum) || EsbUtils.isEmpty(compnum)){
continue;
}
oldAcctInfo = accountMaps.get(acctnum);
if(oldAcctInfo == null){
continue;
}
newAcctInfo = BusinessDataServiceHelper.loadSingle(entityName,new QFilter[]{new QFilter("number","=",acctnum),
new QFilter("createorg.number","=", compnum)});
if(newAcctInfo == null){
hsorgInfo = getOrgInfo(compnum);
if(hsorgInfo == null){
continue;
}
//复制对象修改其中的某些属性比如id和组织id新增币别相关属性
newAcctInfo = copydo(oldAcctInfo,hsorgInfo,json_body);
}else{
//修改币别相关属性
newAcctInfo.set("acctcurrency", json_body.getString("acctcurrency"));
//如果是指定核算币别
// if("descurrency".equals(json_body.getString("acctcurrency"))){
// //处理具体币别分录数据根据逗号进行分割
// docbb = newAcctInfo.getDynamicObjectCollection("currencyentry");
// docbb.clear();
// String[] dess = descurrencynum.split(",");
// for (int j = 0; j < dess.length; j++) {
// bbiso = dess[j];
// bbInfo = BusinessDataServiceHelper.loadSingleFromCache(bbName,new QFilter[]{new QFilter("number","=",bbiso)});
// if(bbInfo != null){
// docbb.addNew().set("currency",bbInfo.getLong("id"));
// }
// }
// }
}
baseMaps.put(acctnum+compnum, newAcctInfo);
}
return baseMaps;
}
private DynamicObject copydo(DynamicObject olddo, DynamicObject hsorgInfo, JSONObject json_body){
DynamicObject newAcctInfo = BusinessDataServiceHelper.newDynamicObject(entityName);
newAcctInfo.set("accounttable", EsbUtils.ACCTABLE);//TODO 科目表fid上正式时注意此ID
newAcctInfo.set("number", olddo.getString("number"));
newAcctInfo.set("name", olddo.getString("name"));
newAcctInfo.set("longnumber", olddo.getString("longnumber"));
newAcctInfo.set("fullname", olddo.getString("fullname"));
newAcctInfo.set("createorg", hsorgInfo.getLong("id"));//创建组织
newAcctInfo.set("org", hsorgInfo.getLong("id"));//管理组织
//科目类型
newAcctInfo.set("accounttype", olddo.getDynamicObject("accounttype"));
//币别核算--外币核算类型
newAcctInfo.set("acctcurrency", json_body.getString("acctcurrency"));//不核算外币nocurrency 指定核算币别descurrency 核算所有币别allcurrency
//如果是指定核算币别
if("descurrency".equals(json_body.getString("acctcurrency")) && !EsbUtils.isEmpty(json_body.getString("descurrencynum"))){
//处理具体币别分录数据根据逗号进行分割
DynamicObjectCollection docbb = newAcctInfo.getDynamicObjectCollection("currencyentry");
String[] dess = json_body.getString("descurrencynum").split(",");
String bbiso;
DynamicObject bbInfo;
for (int j = 0; j < dess.length; j++) {
bbiso = dess[j];
bbInfo = BusinessDataServiceHelper.loadSingleFromCache(bbName,new QFilter[]{new QFilter("number","=",bbiso)});
if(bbInfo != null){
docbb.addNew().set("currency",bbInfo.getLong("id"));
}
}
}
//损益类型
newAcctInfo.set("pltype", olddo.getString("pltype"));
newAcctInfo.set("ctrlstrategy", "1");//控制策略 5全局共享 7私有 1逐级分配
newAcctInfo.set("control", "nocontrol");//受控系统 nocontrol 应付 应收 资产
newAcctInfo.set("dc", 1);//余额方向 默认为借
newAcctInfo.set("level", olddo.getInt("level"));//级次 根据源科目来设置
newAcctInfo.set("isleaf", true);//明细科目 默认为是
newAcctInfo.set("startdate", new Date());//版本化日期
newAcctInfo.set("enddate", TypeUtils.castToDate("2999-12-31"));//失效日期
newAcctInfo.set("accrualdirection", "nocontrol");//科目录入方向控制 不控制nocontrol 借方debit 贷方credit
newAcctInfo.set("orgcontrollevel", 1);//控制级次 默认为1
newAcctInfo.set("isallowca", false);//允许公司增加下级科目 由接口同步 为false时控制级次可以不设置
newAcctInfo.set("ismanual", false);//手工录入 由接口同步
newAcctInfo.set("iscash", false);//现金科目 默认false sap不区分
newAcctInfo.set("isbank", false);//银行科目 默认false sap不区分
newAcctInfo.set("iscashequivalent", false);//现金等价物 默认false sap不区分
newAcctInfo.set("isjournal", false);//登日记账 现金等价物时需要勾选
newAcctInfo.set("enable", 1);//是否启用
newAcctInfo.set("status", "C");//单据状态 A保存 B已提交 C已审核
newAcctInfo.set("creator", RequestContext.get().getCurrUserId());//创建人
//手动指定科目的金蝶id
newAcctInfo.set("id", ID.genLongId());
newAcctInfo.set("masterid", olddo.getLong("id"));//主数据内码系统不会根据id自动生成需要手动设置
SaveServiceHelper.save(new DynamicObject[]{newAcctInfo});
//处理科目使用范围
DB.update(DBRoute.of("fi"), insertSql, new Object[]{newAcctInfo.getLong("id"),hsorgInfo.getLong("id")});
return newAcctInfo;
}
private DynamicObject getHsxmInfo(String hsxmname){
//核算维度对象集合
DynamicObject orginfo = hsxmMaps.get(hsxmname);
if(orginfo == null){
orginfo = BusinessDataServiceHelper.loadSingleFromCache(hswdName,new QFilter[]{new QFilter("name","=",hsxmname)});
mapObject.put(hsxmname,orginfo);
}
return orginfo;
}
private DynamicObject getOrgInfo(String companynum){
//公司对象集合
DynamicObject orginfo = mapObject.get(companynum);
if(orginfo == null){
orginfo = BusinessDataServiceHelper.loadSingleFromCache(orgName,new QFilter[]{new QFilter("number","=",companynum)});
mapObject.put(companynum,orginfo);
}
return orginfo;
}
private String getSY(String acctType){
//根据科目类型返回对应损益类型
if("001".equals(acctType)){
//资产负债类返回非损益类科目
return "0";
}else{
//损益类返回其它损益类型
return "6";
}
}
}

View File

@ -47,6 +47,7 @@ public class ClearAccountBillOperation extends AbstractOperationServicePlugIn im
private static final String userName = "bos_user";//用户
private static final String recbillName = "cas_recbill";//收款单
private static final String pzbName = "shjh_jgqzcust";//结构性清账客户映射表
private static final String voucherName = "gl_voucher";//凭证
/**
* 操作校验通过之后开启事务之前触发此事件
@ -481,7 +482,7 @@ public class ClearAccountBillOperation extends AbstractOperationServicePlugIn im
clear.put("BELNR",recebill.getString("shjh_vouchernum"));//收款sap凭证编号
clear.put("GJAHR",recebill.getString("shjh_sapfiscalyear"));//收款sap会计年度
clear.put("BUZEI",recebill.getString("shjh_sapline"));//收款sap凭证行号
// clear.put("HKONT",receentry.getString(""));//TODO 总账科目-如何取值
clear.put("HKONT",getAccountNumber(entry.getValue()));//TODO 总账科目-如何取值
clear.put("DMBTR1",receentry.getString("e_actamt"));//清账金额-收款单分录的实收金额
IT_CLEAR.add(clear);
break;
@ -501,6 +502,30 @@ public class ClearAccountBillOperation extends AbstractOperationServicePlugIn im
return null;
}
private String getAccountNumber(Long recebillid){
//根据收款单ID获取对应金蝶凭证的贷方科目编号
DynamicObject gl_voucher = BusinessDataServiceHelper.loadSingle(voucherName,
"id,sourcebill,entries,entries.account,entries.debitlocal,entries.creditlocal,entries.entrydc",
new QFilter("sourcebill", QCP.equals, recebillid).toArray());
if (null != gl_voucher) {
DynamicObjectCollection entries = gl_voucher.getDynamicObjectCollection("entries");
if (!entries.isEmpty()) {
DynamicObject account;
BigDecimal creditlocal;
for (DynamicObject entry : entries) {
if ("-1".equals(entry.getString("entrydc"))) {
//分录方向(1.借方,-1.贷方)
account = entry.getDynamicObject("account");//科目
if (null != account) {
return account.getString("number");
}
}
}
}
}
return null;
}
private void handleAudit(AfterOperationArgs e) {
//审核具体实现
DynamicObject[] dos = e.getDataEntities();

View File

@ -6,8 +6,6 @@ import com.sap.db.jdbc.packet.ErrorLevel;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.db.DB;
import kd.bos.db.DBRoute;
import kd.bos.entity.operate.result.OperateErrorInfo;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.AfterOperationArgs;
@ -69,7 +67,7 @@ public class RevenueBillOperation extends AbstractOperationServicePlugIn impleme
//如果预提记账处理单需要生成凭证且未推送sap的才推送sap
sapReturnData = sendVoucher(prinfo);
if(sapReturnData != null && "0".equals(sapReturnData.getString("code"))){
//推送sap成功后反写已推送标记
//TODO 推送sap成功后反写已推送标记
// DB.update(DBRoute.of("fi"), updateVoucherFlag, new Object[]{sapReturnData.getString(""),prinfo.getPkValue()});
this.operationResult.addSuccessPkId(prinfo.getPkValue());
}else if(sapReturnData != null){

View File

@ -21,6 +21,7 @@ import kd.tmc.fpm.business.dataproc.save.domain.ReportDataBatchSaveParam;
import shjh.jhzj7.fi.fi.utils.SapUtils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;
@ -45,6 +46,10 @@ public class FundPlanPaymentTask extends AbstractTask implements Plugin {
qFilter.and("reportplantype", QCP.equals, "reportplan");//报表类型-计划编制
DynamicObject[] collection = BusinessDataServiceHelper.load(entityName, "id", qFilter.toArray());
if(collection.length > 0){
JSONObject sapresult = getSapAR();
if(sapresult == null){
return;
}
//调用SAP应付接口按照公司月份计划科目汇总金额
ReportDataSDKService reportService = new ReportDataSDKService();//报表服务用于写入或者查询报表数据
ReportDataBatchSaveParam pdsp;//报表批量保存的参数
@ -108,11 +113,19 @@ public class FundPlanPaymentTask extends AbstractTask implements Plugin {
}
private JSONObject getSapAR(){
//获取当前日期
Calendar cal = Calendar.getInstance();
//设置日期为本月1号
cal.set(Calendar.DAY_OF_MONTH, 1);
//将日期加一月份得到下月1号
cal.add(Calendar.MONTH, 1);
//日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
JSONArray IT_LIST = new JSONArray();
String duedate_starts = sdf.format(null);
String duedate_ends = sdf.format(null);
String duedate_starts = sdf.format(cal.getTime());//下月1号为开始日期
//将日期加三月份得到一个季度后的1号
cal.add(Calendar.MONTH, 1);
String duedate_ends = sdf.format(cal.getTime());
addFilterCondition(IT_LIST, "FAEDT", duedate_starts, duedate_ends);//到期日
JSONObject sapresult = SapUtils.vouchers_payable(IT_LIST, "FundPlanPaymentTask");
if(sapresult != null){