设备成本分摊插件

This commit is contained in:
zhangzhiguo 2025-11-24 18:06:25 +08:00
parent 6072b60549
commit 27c717e76a
2 changed files with 288 additions and 146 deletions

View File

@ -1,146 +0,0 @@
package zcgj.zcdev.zcdev.pr.plugin.form;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.entity.LocaleString;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.ConfirmCallBackListener;
import kd.bos.form.MessageBoxOptions;
import kd.bos.form.control.Control;
import kd.bos.form.control.events.BeforeClickEvent;
import kd.bos.form.control.events.BeforeItemClickEvent;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.EventObject;
import java.util.HashMap;
import java.util.Map;
import static kd.ai.gai.core.Constant.RepoInfo.number;
/**
* 设备成本核算自动取数逻辑
*/
public class EquipmentCostAutoGetDataFormPlugin extends AbstractBillPlugIn {
@Override
public void registerListener(EventObject e) {
super.registerListener(e);
//监听工具栏按钮点击事件
this.addItemClickListeners("advcontoolbarap");
}
@Override
public void beforeDoOperation(BeforeDoOperationEventArgs args) {
super.beforeDoOperation(args);
FormOperate operate = (FormOperate) args.getSource();
String opKey = operate.getOperateKey();
if (opKey.equals("autosplit")) {
Long currentUserId = UserServiceHelper.getCurrentUserId();
// this.getView().showMessage("自动取数 beforeItemClick");
DynamicObject org = (DynamicObject)this.getModel().getValue("org");//所属组织
DynamicObject project = (DynamicObject)this.getModel().getValue("project");//工程项目
DynamicObject splitperiod = (DynamicObject)this.getModel().getValue("splitperiod");//期间
if(org!=null){
long orgId = org.getLong("id");
QFilter filterOrgId = new QFilter("zcgj_entryentity.zcgj_zjuseorg", QCP.equals,orgId);
filterOrgId.and(new QFilter("billstatus", QCP.equals,"C"));
DynamicObject[] equipinfoArray = BusinessDataServiceHelper.load("eceq_equipinfo",
"creator,zcgj_entryentity.zcgj_assperiod,zcgj_entryentity.zcgj_headusedept,zcgj_entryentity.zcgj_entrybillno,zcgj_entryentity.zcgj_shareamount", new QFilter[]{filterOrgId});
DynamicObject[] loadsettle = BusinessDataServiceHelper.load("eceq_settle", "",
new QFilter[]{new QFilter("org", "=", org.getLong("id"))
, new QFilter("period", "=", splitperiod.getLong("id")),
new QFilter("billstatus", "=", "C").or(new QFilter("billstatus", "=", "B"))});
if(loadsettle.length == 0){
//eceq_settle
DynamicObject eceqsettle =BusinessDataServiceHelper.newDynamicObject("eceq_settle");//设备费用结算
eceqsettle.set("org",org);
eceqsettle.set("objecttype","1");
eceqsettle.set("internalorg",org);
eceqsettle.set("project",project);
eceqsettle.set("period",splitperiod);
String periodNumber = splitperiod.getString("number");
Map<String, Date> monthStartAndEnd = getMonthStartAndEnd(periodNumber);
eceqsettle.set("begindate",monthStartAndEnd.get("start"));
eceqsettle.set("enddate",monthStartAndEnd.get("end"));
eceqsettle.set("creator",currentUserId);
eceqsettle.set("billname",org.getString("name")+"设备结算");
eceqsettle.set("billstatus","C");
int i=0;
DynamicObjectCollection entryentity = eceqsettle.getDynamicObjectCollection("entryentity");//设备结算分录
BigDecimal allAmount = BigDecimal.ZERO;
for (DynamicObject equipinfo : equipinfoArray) {//设备详情
DynamicObject eceqequipmentinfo = BusinessDataServiceHelper.loadSingle(equipinfo.getLong("id"), "eceq_equipment_info");
DynamicObjectCollection dynamicObjectCollection = equipinfo.getDynamicObjectCollection("zcgj_entryentity");//设备详情折旧信息分录
for (DynamicObject dynamicObject : dynamicObjectCollection) {
DynamicObject zcgjAssperiod = dynamicObject.getDynamicObject("zcgj_assperiod");
if(zcgjAssperiod!=null && zcgjAssperiod.getString("number").equals(periodNumber)){
BigDecimal zcgjShareamount = dynamicObject.getBigDecimal("zcgj_shareamount");
DynamicObject entryentityentity = entryentity.addNew();
allAmount=allAmount.add(zcgjShareamount);
entryentityentity.set("usestartdate",monthStartAndEnd.get("start"));//单价
entryentityentity.set("useenddate",monthStartAndEnd.get("end"));//单价
entryentityentity.set("unitprice",zcgjShareamount);//单价
entryentityentity.set("amount",zcgjShareamount);//金额
entryentityentity.set("taxprice",zcgjShareamount);//含税单价
entryentityentity.set("taxamount",zcgjShareamount);//含税金额
entryentityentity.set("accountnum",1);//核算数量
entryentityentity.set("equipment",eceqequipmentinfo);//设备名称
i++;
}
}
}
eceqsettle.set("curamount",allAmount);//本期结算金额
eceqsettle.set("curoftaxamount",allAmount);//curoftaxamount
if(i>0){
SaveServiceHelper.saveOperate("eceq_settle", new DynamicObject[]{eceqsettle}, null);//设备结算
//OperateOption option= OperateOption.create();
//OperationResult resultsubmit = OperationServiceHelper.executeOperate("submit", "eceq_settle", new DynamicObject[]{eceqsettle}, option);
//OperationResult resultaudit = OperationServiceHelper.executeOperate("audit", "eceq_settle", new DynamicObject[]{eceqsettle}, option);
}
this.getView().showLoading(new LocaleString("拉取数据中,请稍后!"), 2000);
// this.getView().showMessage("拉取数据中,请稍后!");
}
}
}
}
public static Map<String, Date> getMonthStartAndEnd(String yearMonthStr) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
YearMonth yearMonth = YearMonth.parse(yearMonthStr, formatter);
// LocalDate 转换为 Date
ZoneId zone = ZoneId.systemDefault();
Date start = Date.from(yearMonth.atDay(1).atStartOfDay(zone).toInstant());
Date end = Date.from(yearMonth.atEndOfMonth().atTime(23, 59, 59).atZone(zone).toInstant());
Map<String, Date> result = new HashMap<>();
result.put("start", start);
result.put("end", end);
return result;
}
}

View File

@ -0,0 +1,288 @@
package zcgj.zcdev.zcdev.pr.plugin.form;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.entity.LocaleString;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.orm.ORM;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;
import kd.ec.eceq.business.model.BaseConstant;
import java.math.BigDecimal;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
/**
* 设备成本分摊插件
*/
public class EquipmentCostSplitFormPluginExt extends EquipmentCardListPlugin{
@Override
public void afterDoOperation(AfterDoOperationEventArgs afterDoOperationEventArgs) {
//super.afterDoOperation(afterDoOperationEventArgs);
switch (afterDoOperationEventArgs.getOperateKey()) {
case "autosplit":
this.autoGenSettData();
this.autoSplitCost();
break;
case "deletesubentry":
int subEntryIndex = this.getModel().getEntryCurrentRowIndex("costsplitentity");
int subRowCount = this.getModel().getEntryRowCount("costsplitentity");
if (subEntryIndex >= 0 && subRowCount > 0) {
int parentRowIndex = this.getModel().getEntryCurrentRowIndex("settlesplitentity");
BigDecimal curamount = (BigDecimal)this.getModel().getValue("curamount", parentRowIndex);
BigDecimal oldsplitamount = (BigDecimal)this.getModel().getValue("oldsplitamount", parentRowIndex);
BigDecimal curSplitamount = this.sumAmount("costsplitentity", "splitamount");
this.getModel().setValue("cursplitamount", curSplitamount, parentRowIndex);
BigDecimal restsplitamount = curamount.subtract(oldsplitamount).subtract(curSplitamount);
this.getModel().setValue("restsplitamount", restsplitamount, parentRowIndex);
this.getView().updateView("settlesplitentity");
this.refreshCostamount();
}
}
}
private void autoSplitCost() {
ORM orm = ORM.create();
DynamicObjectCollection costSplitList = this.getModel().getEntryEntity("settlesplitentity");
costSplitList.clear();
DynamicObject settleSplit = null;
if (this.getModel().getValue("project") == null) {
this.getView().showTipNotification(ResManager.loadKDString("工程项目不能为空。", "EquipmentCostSplitFormPlugin_2", "ec-eceq-formplugin", new Object[0]));
} else {
DynamicObject project = (DynamicObject)this.getModel().getValue("project");
DynamicObject period = (DynamicObject)this.getModel().getValue("splitperiod");
QFilter qFilter = new QFilter("project", "=", project.getPkValue());
QFilter qFilter1 = new QFilter("id", "<>", this.getModel().getDataEntity().getPkValue());
DynamicObject[] equipCostSpiltList = BusinessDataServiceHelper.load("eceq_costsplit", "settlesplitentity,settlesplitentity.settleid", new QFilter[]{qFilter, qFilter1});
List<Object> settleIds = new ArrayList();
for(DynamicObject equipCostSpilt : equipCostSpiltList) {
for(DynamicObject settleSplitEntry : equipCostSpilt.getDynamicObjectCollection("settlesplitentity")) {
settleIds.add(settleSplitEntry.get("settleid"));
}
}
QFilter settleIdsInEquipCostSpiltFilter;
if (settleIds.isEmpty()) {
settleIdsInEquipCostSpiltFilter = new QFilter("1", "=", 1);
} else {
settleIdsInEquipCostSpiltFilter = new QFilter("id", "not in", settleIds);
}
QFilter projectFilter = new QFilter("project", "=", project.getPkValue());
QFilter periodFilter = new QFilter("period.enddate", "<=", period.getDate("enddate"));
QFilter isallsplitFilter = new QFilter("isallsplit", "=", "0");
QFilter iscostFilter = new QFilter("iscost", "=", false);
QFilter billStatusFilter = new QFilter("billstatus", "=", "C");
DynamicObject[] settleList = BusinessDataServiceHelper.load("eceq_settle", "billno, billstatus, creator, modifier, auditor, auditdate, modifytime, createtime, org, billname, project, objecttype, internalorg, externalunit, period, begindate, enddate, contract, taxrate, curoftaxamount, totaltax, curamount, description, isallsplit, objecttypeshowname, currency, iscost, issettle, unitproject,entryentity.equipment,entryentity.procbs,entryentity.ca,entryentity.proboq,entryentity.issplit,entryentity.amount,entryentity.zcgj_sectype1", new QFilter[]{projectFilter, isallsplitFilter, billStatusFilter, periodFilter, iscostFilter, settleIdsInEquipCostSpiltFilter});
if (settleList.length == 0) {
this.getView().showTipNotification(ResManager.loadKDString("没有需要分摊的数据。", "EquipmentCostSplitFormPlugin_4", "ec-eceq-formplugin", new Object[0]));
}
long[] entryIds = orm.genLongIds(costSplitList.getDynamicObjectType(), settleList.length);
for(int i = 0; i < settleList.length; ++i) {
settleSplit = new DynamicObject(costSplitList.getDynamicObjectType());
settleSplit.set(BaseConstant.ID_ENTITY_PK, entryIds[i]);
settleSplit.set("settleno", settleList[i].get("billno"));
if ("1".equals(settleList[i].getString("objecttype"))) {
settleSplit.set("externalunit", settleList[i].getDynamicObject("internalorg").get("name"));
} else {
settleSplit.set("externalunit", settleList[i].getDynamicObject("externalunit").get("name"));
}
settleSplit.set("contract", settleList[i].getDynamicObject("contract"));
settleSplit.set("period", settleList[i].getDynamicObject("period"));
settleSplit.set("curamount", settleList[i].get("curamount"));
settleSplit.set("settleid", settleList[i].getPkValue());
QFilter settleFilter = new QFilter("settlesplitentity.settleno", "=", settleList[i].get("billno"));
QFilter statusFilter = new QFilter("billstatus", "=", "C");
DynamicObject[] settleSplits = BusinessDataServiceHelper.load("eceq_costsplit", "settlesplitentity.id,settlesplitentity.cursplitamount,settlesplitentity.settleid", new QFilter[]{settleFilter, statusFilter});
BigDecimal oldAmount = new BigDecimal((double)0.0F);
new BigDecimal((double)0.0F);
BigDecimal curamount = settleList[i].getBigDecimal("curamount");
BigDecimal cursplitamount;
if (settleSplits.length <= 0) {
oldAmount = new BigDecimal((double)0.0F);
cursplitamount = settleList[i].getBigDecimal("curamount");
} else {
for(int j = 0; j < settleSplits.length; ++j) {
DynamicObjectCollection settlesplitentitys = settleSplits[j].getDynamicObjectCollection("settlesplitentity");
for(int k = 0; k < settlesplitentitys.size(); ++k) {
if ((Long)settleList[i].getPkValue() == ((DynamicObject)settlesplitentitys.get(k)).getLong("settleid")) {
oldAmount = oldAmount.add(((DynamicObject)settlesplitentitys.get(k)).getBigDecimal("cursplitamount"));
break;
}
}
}
cursplitamount = new BigDecimal((double)0.0F);
}
settleSplit.set("oldsplitamount", oldAmount);
settleSplit.set("cursplitamount", cursplitamount);
BigDecimal restsplitamount = curamount.subtract(oldAmount).subtract(cursplitamount);
settleSplit.set("restsplitamount", restsplitamount);
DynamicObjectCollection entryentityList = settleList[i].getDynamicObjectCollection("entryentity");
DynamicObject unitProject = settleList[i].getDynamicObject("unitproject");
DynamicObjectCollection costSplitDetailList = settleSplit.getDynamicObjectCollection("costsplitentity");
DynamicObject costSplitDetail = null;
long[] subEntryIds = orm.genLongIds(costSplitDetailList.getDynamicObjectType(), entryentityList.size());
for(int j = 0; j < entryentityList.size(); ++j) {
costSplitDetail = new DynamicObject(costSplitDetailList.getDynamicObjectType());
costSplitDetail.set(BaseConstant.ID_ENTITY_PK, subEntryIds[j]);
if (unitProject != null) {
costSplitDetail.set("unitproject", unitProject);
}
costSplitDetail.set("equipment", ((DynamicObject)entryentityList.get(j)).getDynamicObject("equipment"));
costSplitDetail.set("proboq", ((DynamicObject)entryentityList.get(j)).getDynamicObject("proboq"));
costSplitDetail.set("procbs", ((DynamicObject)entryentityList.get(j)).getDynamicObject("procbs"));
costSplitDetail.set("ca", ((DynamicObject)entryentityList.get(j)).getDynamicObject("ca"));
costSplitDetail.set("settleentryid", ((DynamicObject)entryentityList.get(j)).getPkValue());
costSplitDetail.set("zcgj_sectype1", ((DynamicObject)entryentityList.get(j)).getString("zcgj_sectype1"));
costSplitDetail.set("costtype", "equfee");
if (settleSplits.length > 0) {
costSplitDetail.set("splitamount", new BigDecimal((double)0.0F));
} else {
costSplitDetail.set("splitamount", ((DynamicObject)entryentityList.get(j)).getBigDecimal("amount"));
}
costSplitDetailList.add(costSplitDetail);
}
settleSplit.set("costsplitentity", costSplitDetailList);
costSplitList.add(settleSplit);
}
this.getModel().updateEntryCache(costSplitList);
this.getView().updateView("settlesplitentity");
this.getView().updateView("costsplitentity");
this.refreshCostamount();
}
}
protected void refreshCostamount() {
BigDecimal cursplitamount = this.sumAmount("settlesplitentity", "cursplitamount");
this.getModel().setValue("costamount", cursplitamount);
}
protected BigDecimal sumAmount(String entryKey, String col) {
BigDecimal totalAmt = BigDecimal.ZERO;
int index = this.getModel().getEntryRowCount(entryKey);
for(int i = 0; i < index; ++i) {
Object splitAmt = this.getModel().getValue(col, i);
if (splitAmt != null) {
totalAmt = totalAmt.add((BigDecimal)splitAmt);
}
}
return totalAmt;
}
/**
* 自动生成设备结算单数据
*/
private void autoGenSettData(){
Long currentUserId = UserServiceHelper.getCurrentUserId();
// this.getView().showMessage("自动取数 beforeItemClick");
DynamicObject org = (DynamicObject)this.getModel().getValue("org");//所属组织
DynamicObject project = (DynamicObject)this.getModel().getValue("project");//工程项目
DynamicObject splitperiod = (DynamicObject)this.getModel().getValue("splitperiod");//期间
if(org!=null){
long orgId = org.getLong("id");
QFilter filterOrgId = new QFilter("zcgj_entryentity.zcgj_zjuseorg", QCP.equals,orgId);
filterOrgId.and(new QFilter("billstatus", QCP.equals,"C"));
DynamicObject[] equipinfoArray = BusinessDataServiceHelper.load("eceq_equipinfo",
"creator,zcgj_entryentity.zcgj_assperiod,zcgj_entryentity.zcgj_headusedept,zcgj_entryentity.zcgj_entrybillno,zcgj_entryentity.zcgj_shareamount", new QFilter[]{filterOrgId});
DynamicObject[] loadsettle = BusinessDataServiceHelper.load("eceq_settle", "",
new QFilter[]{new QFilter("org", "=", org.getLong("id"))
, new QFilter("period", "=", splitperiod.getLong("id")),
new QFilter("billstatus", "=", "C").or(new QFilter("billstatus", "=", "B"))});
if(loadsettle.length == 0){
//eceq_settle
DynamicObject eceqsettle =BusinessDataServiceHelper.newDynamicObject("eceq_settle");//设备费用结算
eceqsettle.set("org",org);
eceqsettle.set("objecttype","1");
eceqsettle.set("internalorg",org);
eceqsettle.set("project",project);
eceqsettle.set("period",splitperiod);
String periodNumber = splitperiod.getString("number");
Map<String, Date> monthStartAndEnd = getMonthStartAndEnd(periodNumber);
eceqsettle.set("begindate",monthStartAndEnd.get("start"));
eceqsettle.set("enddate",monthStartAndEnd.get("end"));
eceqsettle.set("creator",currentUserId);
eceqsettle.set("billname",org.getString("name")+"设备结算");
eceqsettle.set("billstatus","C");
int i=0;
DynamicObjectCollection entryentity = eceqsettle.getDynamicObjectCollection("entryentity");//设备结算分录
BigDecimal allAmount = BigDecimal.ZERO;
for (DynamicObject equipinfo : equipinfoArray) {//设备详情
DynamicObject eceqequipmentinfo = BusinessDataServiceHelper.loadSingle(equipinfo.getLong("id"), "eceq_equipment_info");
DynamicObjectCollection dynamicObjectCollection = equipinfo.getDynamicObjectCollection("zcgj_entryentity");//设备详情折旧信息分录
for (DynamicObject dynamicObject : dynamicObjectCollection) {
DynamicObject zcgjAssperiod = dynamicObject.getDynamicObject("zcgj_assperiod");
if(zcgjAssperiod!=null && zcgjAssperiod.getString("number").equals(periodNumber)){
BigDecimal zcgjShareamount = dynamicObject.getBigDecimal("zcgj_shareamount");
DynamicObject entryentityentity = entryentity.addNew();
allAmount=allAmount.add(zcgjShareamount);
entryentityentity.set("usestartdate",monthStartAndEnd.get("start"));//单价
entryentityentity.set("useenddate",monthStartAndEnd.get("end"));//单价
entryentityentity.set("unitprice",zcgjShareamount);//单价
entryentityentity.set("amount",zcgjShareamount);//金额
entryentityentity.set("taxprice",zcgjShareamount);//含税单价
entryentityentity.set("taxamount",zcgjShareamount);//含税金额
entryentityentity.set("accountnum",1);//核算数量
entryentityentity.set("equipment",eceqequipmentinfo);//设备名称
entryentityentity.set("zcgj_sectype1","30.20");//设备名称
i++;
}
}
}
eceqsettle.set("curamount",allAmount);//本期结算金额
eceqsettle.set("curoftaxamount",allAmount);//curoftaxamount
if(i>0){
SaveServiceHelper.saveOperate("eceq_settle", new DynamicObject[]{eceqsettle}, null);//设备结算
//OperateOption option= OperateOption.create();
//OperationResult resultsubmit = OperationServiceHelper.executeOperate("submit", "eceq_settle", new DynamicObject[]{eceqsettle}, option);
//OperationResult resultaudit = OperationServiceHelper.executeOperate("audit", "eceq_settle", new DynamicObject[]{eceqsettle}, option);
}
this.getView().showLoading(new LocaleString("拉取数据中,请稍后!"), 2000);
// this.getView().showMessage("拉取数据中,请稍后!");
}
}
}
public static Map<String, Date> getMonthStartAndEnd(String yearMonthStr) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
YearMonth yearMonth = YearMonth.parse(yearMonthStr, formatter);
// LocalDate 转换为 Date
ZoneId zone = ZoneId.systemDefault();
Date start = Date.from(yearMonth.atDay(1).atStartOfDay(zone).toInstant());
Date end = Date.from(yearMonth.atEndOfMonth().atTime(23, 59, 59).atZone(zone).toInstant());
Map<String, Date> result = new HashMap<>();
result.put("start", start);
result.put("end", end);
return result;
}
}