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

157 lines
8.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package tqq9.lc123.cloud.app.plugin.operate.im;
import com.alibaba.fastjson.JSONObject;
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;
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;
import kd.bos.util.StringUtils;
import kd.sdk.plugin.Plugin;
import tqq9.lc123.cloud.app.plugin.utils.BotpParamUtils;
import java.math.BigDecimal;
import java.util.*;
/**
* 其他入库单审核时采购退货申请下推红字采购入库单(审核)
*/
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");
e.getFieldKeys().add("billentry.tqq9_ydthflid");//异地退货分录id
}
@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));
}
}
//根据采购退货分录id找到采购退货申请单下推-红字采购入库单
//其他入库单有几行明细 生成的采购入库单就几行明细
DynamicObject pm_purrefundapplybill = BusinessDataServiceHelper.loadSingle("pm_purrefundapplybill",
new QFilter[]{new QFilter("billentry.id", QCP.in, cgthflidList)});
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");
long tqq9_ydthflid = inEntry.getLong("tqq9_ydthflid");
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);
redEntryData.put("tqq9_ydthflid", tqq9_ydthflid);
redEntryDataList.add(redEntryData);
}
}
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;
}
}
}
Long id = pm_purrefundapplybill.getLong("id");
Map<String, Set<JSONObject>> param = new HashMap<>();//传入转换规则处理参数
for (Map<String, Object> map : redEntryDataList) {
JSONObject detailObject = new JSONObject();
Object materialId = map.get("materialId");
Object qty = map.get("qty");
Object returnEntryId = map.get("returnEntryId");
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"));//折扣后不含税金额
param.computeIfAbsent((String) returnEntryId, k -> new HashSet<>()).add(detailObject);
}
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);
List<SourceBillReport> billReports = pushResult.getBillReports();
//下推失败直接返回
if (!pushResult.isSuccess()) {
for (SourceBillReport billReport : billReports) {
String billMessage = billReport.getFailMessage();
logger.info("采购退货申请:" + pm_purrefundapplybill.getString("billno") + "下推红字采购入库单失败:" + billMessage);
}
}
}
}
}
}