采购计划F7超额校验器

This commit is contained in:
李贵强 2025-03-15 14:28:26 +08:00
parent d5b7ae9610
commit 0ec2c7e885
2 changed files with 120 additions and 27 deletions

View File

@ -13,13 +13,11 @@ import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
import java.util.*;
/**
* 动态表单插件
* 费用登记-
* 费用登记-采购计划F7过滤
*/
public class ConNoTextBillFromPlugin extends AbstractFormPlugin implements Plugin, BeforeF7SelectListener {
@ -34,37 +32,50 @@ public class ConNoTextBillFromPlugin extends AbstractFormPlugin implements Plugi
@Override
public void beforeF7Select(BeforeF7SelectEvent evt) {
ListShowParameter formShowParameter = (ListShowParameter) evt.getFormShowParameter();
ArrayList<Long> canSeeDataId = new ArrayList<>();
//查找所有已审核采购需求数据
List<Long> canSeeDataId = new ArrayList<>();
// 获取当前组织
DynamicObject org = (DynamicObject) this.getModel().getValue("org");
QFilter qFilter = new QFilter("billstatus", QCP.equals, "C");
qFilter.and(new QFilter("org.name", QCP.equals, org.getString("name")));
DynamicObject[] settlePlanBills = BusinessDataServiceHelper.load("recon_settleplanbill", "id,bill,qeug_applyamount", qFilter.toArray());
//判断申请金额是否被费用登记用完
for (int i = 0; i < settlePlanBills.length; i++) {
DynamicObject settlePlanBill = settlePlanBills[i];
String orgName = org.getString("name");
// 查找所有已审核的采购需求数据单据状态 = "C"
QFilter statusFilter = new QFilter("billstatus", QCP.equals, "C");
QFilter orgFilter = new QFilter("org.name", QCP.equals, orgName);
DynamicObject[] settlePlanBills = BusinessDataServiceHelper.load("recon_settleplanbill", "id,bill,qeug_applyamount", statusFilter.and(orgFilter).toArray());
// 若无数据直接返回
if (settlePlanBills == null || settlePlanBills.length == 0) {
return;
}
// 遍历所有采购需求数据
for (DynamicObject settlePlanBill : settlePlanBills) {
long id = settlePlanBill.getLong("id");
BigDecimal applyAmount = settlePlanBill.getBigDecimal("qeug_applyamount");
BigDecimal allOriAmt=BigDecimal.ZERO;
QFilter qFilter1 = new QFilter("qeug_refbillfield.id", QCP.equals, id);
DynamicObject[] conNoTextBills = BusinessDataServiceHelper.load("recon_connotextbill", "id,oriamt", qFilter1.toArray());
if (conNoTextBills.length!=0){
for (int j = 0; j < conNoTextBills.length; j++) {
DynamicObject conNoTextBill = conNoTextBills[j];
BigDecimal oriAmt = conNoTextBill.getBigDecimal("oriamt");
allOriAmt=allOriAmt.add(oriAmt);
}
}
//还有余额
if (applyAmount.compareTo(allOriAmt)>0){
// 查询当前采购需求的所有相关费用登记
QFilter refFilter = new QFilter("qeug_refbillfield.id", QCP.equals, id);
DynamicObject[] conNoTextBills = BusinessDataServiceHelper.load("recon_connotextbill", "id,oriamt", refFilter.toArray());
// 计算所有已使用金额
BigDecimal totalUsedAmount = Arrays.stream(conNoTextBills)
.map(bill -> bill.getBigDecimal("oriamt"))
.reduce(BigDecimal.ZERO, BigDecimal::add);
// 若申请金额大于已使用金额则允许选择
if (applyAmount.compareTo(totalUsedAmount) > 0) {
canSeeDataId.add(id);
}
}
List<QFilter> qFilters = new ArrayList<>();
qFilters.add(new QFilter("id", QCP.in, canSeeDataId));
formShowParameter.getListFilterParameter().setQFilters(qFilters);
// 仅当有可用数据时才设置过滤条件
if (!canSeeDataId.isEmpty()) {
List<QFilter> qFilters = Collections.singletonList(new QFilter("id", QCP.in, canSeeDataId));
formShowParameter.getListFilterParameter().setQFilters(qFilters);
}
}
}

View File

@ -0,0 +1,82 @@
package shkd.repc.recon.validator;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;
import java.math.BigDecimal;
/**
* 动态表单插件
*/
public class ConNoTextValidator extends AbstractValidator {
@Override
public void validate() {
ExtendedDataEntity[] dataEntities = this.getDataEntities();
if (dataEntities == null || dataEntities.length == 0) {
return;
}
for (ExtendedDataEntity dataEntity : dataEntities) {
if (dataEntity == null) {
continue;
}
DynamicObject bill = dataEntity.getDataEntity();
DynamicObject currentBill = BusinessDataServiceHelper.loadSingle(bill.getLong("id"), "recon_connotextbill");
if (currentBill == null) {
continue;
}
DynamicObject refBill = currentBill.getDynamicObject("qeug_refbillfield");
if (refBill == null) {
continue;
}
DynamicObject settlePlanBill = BusinessDataServiceHelper.loadSingle(refBill.getLong("id"), "recon_settleplanbill");
// 获取申请总额
BigDecimal applyAmount = settlePlanBill.getBigDecimal("qeug_applyamount");
// 查找已占用金额的其他单据
QFilter filter = new QFilter("qeug_refbillfield.id", QCP.equals, refBill.getLong("id"));
DynamicObject[] relatedBills = BusinessDataServiceHelper.load("recon_connotextbill", "id, oriamt, billno", filter.toArray());
// 计算占用金额并收集提示信息
BigDecimal occupationAmount = BigDecimal.ZERO;
StringBuilder message = new StringBuilder();
for (DynamicObject otherBill : relatedBills) {
if (otherBill.getLong("id") == currentBill.getLong("id")) {
continue; // 跳过当前单据
}
BigDecimal otherOriAmt = otherBill.getBigDecimal("oriamt");
occupationAmount = occupationAmount.add(otherOriAmt);
message.append("")
.append(otherBill.getString("billno"))
.append("】已占用该采购计划申请金额: ")
.append(otherOriAmt.setScale(2).toPlainString())
.append("\n");
}
// 计算超额情况
BigDecimal currentOriAmt = currentBill.getBigDecimal("oriamt");
BigDecimal excessAmount = currentOriAmt.add(occupationAmount).subtract(applyAmount);
if (excessAmount.compareTo(BigDecimal.ZERO) > 0) {
message.append("当前单据金额: ").append(currentOriAmt.setScale(2).toPlainString())
.append(",累计占用金额: ").append(occupationAmount.setScale(2).toPlainString())
.append(",超出申请总额: ").append(applyAmount.setScale(2).toPlainString())
.append("\n");
this.addErrorMessage(dataEntity, message.toString());
}
}
}
}