lc/lc123/cloud/app/plugin/operate/im/OtherInYdthPlugin.java

193 lines
11 KiB
Java
Raw Normal View History

2025-11-18 09:52:34 +00:00
package tqq9.lc123.cloud.app.plugin.operate.im;
import com.alibaba.fastjson.JSONObject;
2025-11-20 07:09:28 +00:00
import kd.bos.dataentity.OperateOption;
2025-11-18 09:52:34 +00:00
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.botp.runtime.ConvertOperationResult;
import kd.bos.entity.botp.runtime.PushArgs;
import kd.bos.entity.botp.runtime.SourceBillReport;
2025-11-20 07:09:28 +00:00
import kd.bos.entity.operate.result.OperationResult;
2025-11-18 09:52:34 +00:00
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.botp.ConvertServiceHelper;
2025-11-20 07:09:28 +00:00
import kd.bos.servicehelper.operation.OperationServiceHelper;
2025-11-18 09:52:34 +00:00
import kd.bos.util.StringUtils;
import kd.sdk.plugin.Plugin;
2025-11-20 07:09:28 +00:00
import tqq9.lc123.cloud.app.api.utils.Constants;
2025-11-18 09:52:34 +00:00
import tqq9.lc123.cloud.app.plugin.utils.BotpParamUtils;
import java.math.BigDecimal;
import java.util.*;
/**
2025-11-19 09:24:07 +00:00
* 其他入库单审核时采购退货申请下推红字采购入库单审核
2025-11-18 09:52:34 +00:00
*/
public class OtherInYdthPlugin extends AbstractOperationServicePlugIn implements Plugin {
private final static Log logger = LogFactory.getLog(OtherInYdthPlugin.class);
private static String PM_PURREFUNDAPPLYBILL = "pm_purrefundapplybill";//采购退货申请单
private static String IM_PURINBILL = "im_purinbill";//采购入库单
private static String BILLENTRY = "billentry";//收货通知单分录,采购退货申请单分录
@Override
public void onPreparePropertys(PreparePropertysEventArgs e) {
super.onPreparePropertys(e);
e.getFieldKeys().add("billentry.tqq9_cgthflid");
2025-11-20 03:27:59 +00:00
e.getFieldKeys().add("billentry.tqq9_ydthflid");//异地退货分录id
2025-11-18 09:52:34 +00:00
}
@Override
public void afterExecuteOperationTransaction(AfterOperationArgs e) {
super.afterExecuteOperationTransaction(e);
DynamicObject[] dataEntities1 = e.getDataEntities();
for (DynamicObject dynamicObject : dataEntities1) {
DynamicObjectCollection billentry = dynamicObject.getDynamicObjectCollection("billentry");
List<Long> cgthflidList = new ArrayList<>();
for (DynamicObject object : billentry) {
String tqq9_cgthflid = object.getString("tqq9_cgthflid");
if (StringUtils.isNotEmpty(tqq9_cgthflid) && !"0".equals(tqq9_cgthflid)) {
cgthflidList.add(Long.valueOf(tqq9_cgthflid));
}
}
2025-11-19 09:24:07 +00:00
2025-11-18 09:52:34 +00:00
//根据采购退货分录id找到采购退货申请单下推-红字采购入库单
2025-11-19 09:24:07 +00:00
//其他入库单有几行明细 生成的采购入库单就几行明细
2025-11-18 09:52:34 +00:00
DynamicObject pm_purrefundapplybill = BusinessDataServiceHelper.loadSingle("pm_purrefundapplybill",
new QFilter[]{new QFilter("billentry.id", QCP.in, cgthflidList)});
2025-11-19 09:24:07 +00:00
if (null != pm_purrefundapplybill) {
// 2. 创建列表收集红字入库单明细数据
List<Map<String, Object>> redEntryDataList = new ArrayList<>();
// 3. 遍历其他入库单明细收集数据不创建DynamicObject
for (DynamicObject inEntry : billentry) {
// 获取当前行的信息
String returnEntryId = inEntry.getString("tqq9_cgthflid");
BigDecimal qty = inEntry.getBigDecimal("qty") == null ? BigDecimal.ZERO : inEntry.getBigDecimal("qty"); // 入库数量
String materialId = inEntry.getDynamicObject("material").getDynamicObject("masterid").getString("id");
2025-11-20 03:27:59 +00:00
long tqq9_ydthflid = inEntry.getLong("tqq9_ydthflid");
2025-11-19 09:24:07 +00:00
if (returnEntryId != null && qty != null && materialId != null) {
// 创建数据Map收集明细信息
Map<String, Object> redEntryData = new HashMap<>();
redEntryData.put("materialId", materialId);
redEntryData.put("qty", qty); // 红字用负数
redEntryData.put("returnEntryId", returnEntryId);
2025-11-20 03:27:59 +00:00
redEntryData.put("tqq9_ydthflid", tqq9_ydthflid);
2025-11-19 09:24:07 +00:00
redEntryDataList.add(redEntryData);
2025-11-18 09:52:34 +00:00
2025-11-19 09:24:07 +00:00
}
2025-11-18 09:52:34 +00:00
}
2025-11-20 03:27:59 +00:00
DynamicObjectCollection tqq9_entryentity = pm_purrefundapplybill.getDynamicObjectCollection("tqq9_entryentity");//异地退货明细
for (Map<String, Object> map : redEntryDataList) {
for (DynamicObject object : tqq9_entryentity) {
long id = object.getLong("id");
long tqq9_ydthflid = (long) map.get("tqq9_ydthflid");
if (id == tqq9_ydthflid) {
DynamicObject tqq9_taxrateid_ydth = object.getDynamicObject("tqq9_taxrateid_ydth");
if (null != tqq9_taxrateid_ydth) {
map.put("tqq9_taxrateid_ydth", tqq9_taxrateid_ydth.getLong("id"));//税率
}
map.put("tqq9_price_ydth", object.get("tqq9_price_ydth"));//不含税单价
map.put("tqq9_taxamount_ydth", object.get("tqq9_taxamount_ydth"));//税额
map.put("tqq9_priceandtax_ydth", object.get("tqq9_priceandtax_ydth"));//含税单价
map.put("tqq9_amountandtax_ydth", object.get("tqq9_amountandtax_ydth"));//折扣后价税合计
map.put("tqq9_amount_ydth", object.get("tqq9_amount_ydth"));//折扣后不含税金额
break;
}
}
}
2025-11-18 09:52:34 +00:00
Long id = pm_purrefundapplybill.getLong("id");
2025-11-19 09:24:07 +00:00
2025-11-18 09:52:34 +00:00
Map<String, Set<JSONObject>> param = new HashMap<>();//传入转换规则处理参数
2025-11-19 09:24:07 +00:00
for (Map<String, Object> map : redEntryDataList) {
2025-11-18 09:52:34 +00:00
JSONObject detailObject = new JSONObject();
2025-11-19 09:24:07 +00:00
Object materialId = map.get("materialId");
Object qty = map.get("qty");
Object returnEntryId = map.get("returnEntryId");
2025-11-20 03:27:59 +00:00
Object tqq9_ydthflid = map.get("tqq9_ydthflid");
detailObject.put("materialId", materialId);//物料id
detailObject.put("qty", qty);//数量
detailObject.put("returnEntryId", returnEntryId);//采购退货明细id
detailObject.put("tqq9_ydthflid", tqq9_ydthflid);//异地退货分录id
detailObject.put("tqq9_taxrateid_ydth", map.get("tqq9_taxrateid_ydth"));//税率
detailObject.put("tqq9_price_ydth", map.get("tqq9_price_ydth"));//不含税单价
detailObject.put("tqq9_taxamount_ydth", map.get("tqq9_taxamount_ydth"));//税额
detailObject.put("tqq9_priceandtax_ydth", map.get("tqq9_priceandtax_ydth"));//含税单价
detailObject.put("tqq9_amountandtax_ydth", map.get("tqq9_amountandtax_ydth"));//折扣后价税合计
detailObject.put("tqq9_amount_ydth", map.get("tqq9_amount_ydth"));//折扣后不含税金额
2025-11-19 09:24:07 +00:00
param.computeIfAbsent((String) returnEntryId, k -> new HashSet<>()).add(detailObject);
2025-11-18 09:52:34 +00:00
}
2025-11-19 09:24:07 +00:00
HashMap<Long, Set<Long>> entitypkMap = new HashMap<>();//上下游id映射Map
for (Map<String, Object> map : redEntryDataList) {
Object returnEntryId = map.get("returnEntryId");
entitypkMap.computeIfAbsent(id, k -> new HashSet<>()).add(Long.valueOf((String) returnEntryId));
}
PushArgs pushArgs1 = BotpParamUtils.getPushArgs(PM_PURREFUNDAPPLYBILL, IM_PURINBILL, BILLENTRY, param, entitypkMap, "2304405848245415936");
ConvertOperationResult pushResult = ConvertServiceHelper.pushAndSave(pushArgs1);
2025-11-18 09:52:34 +00:00
List<SourceBillReport> billReports = pushResult.getBillReports();
2025-11-19 09:24:07 +00:00
//下推失败直接返回
if (!pushResult.isSuccess()) {
for (SourceBillReport billReport : billReports) {
String billMessage = billReport.getFailMessage();
logger.info("采购退货申请:" + pm_purrefundapplybill.getString("billno") + "下推红字采购入库单失败:" + billMessage);
}
}
2025-11-20 07:09:28 +00:00
//获取下推单据id准备提交审核操作
Set<Object> targetBillIds = pushResult.getTargetBillIds();
DynamicObject[] load = new DynamicObject[targetBillIds.size()];
Iterator<Object> iterator = targetBillIds.iterator();
for (int i = 0; iterator.hasNext(); i++) {
Object next = iterator.next();
DynamicObject dynamicObject1 = BusinessDataServiceHelper.loadSingle(IM_PURINBILL, new QFilter[]{new QFilter("id", QCP.equals, next)});
load[i] = dynamicObject1;
}
//开始提交
OperateOption operateOption = OperateOption.create();
OperationResult sumbitResult = OperationServiceHelper.executeOperate(Constants.TYPE_SUBMIT, IM_PURINBILL, load, operateOption);
if (!sumbitResult.isSuccess()) {
if (Objects.nonNull(sumbitResult.getAllErrorOrValidateInfo())) {
logger.info("采购入库单提交失败: " + sumbitResult.getAllErrorOrValidateInfo().toString());
}
OperationResult deleteResult = OperationServiceHelper.executeOperate(Constants.TYPE_DELETE, IM_PURINBILL, load, operateOption);
} else {
//开始审核
OperationResult auditResult = OperationServiceHelper.executeOperate(Constants.TYPE_AUDIT, IM_PURINBILL, load, operateOption);
if (!auditResult.isSuccess()) {
if (Objects.nonNull(auditResult.getAllErrorOrValidateInfo())) {
logger.info("采购入库单审核失败: " + auditResult.getAllErrorOrValidateInfo().toString());
}
OperationResult unsubmitResult = OperationServiceHelper.executeOperate(Constants.TYPE_UNSUBMIT, IM_PURINBILL, load, operateOption);
OperationResult deleteResult = OperationServiceHelper.executeOperate(Constants.TYPE_DELETE, IM_PURINBILL, load, operateOption);
}
}
2025-11-18 09:52:34 +00:00
}
2025-11-19 09:24:07 +00:00
2025-11-18 09:52:34 +00:00
}
}
}