差旅报销单差旅申请审批日期和行程日期校验

This commit is contained in:
zhangzhiguo 2025-11-11 14:34:32 +08:00
parent 1863fdc0cc
commit be2a1315d4
1 changed files with 138 additions and 0 deletions

View File

@ -0,0 +1,138 @@
package zcgj.zcdev.zcdev.fs.plugin.operate;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;
import kd.bos.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import zcgj.zcdev.zcdev.fs.utils.OrgCheckUtils;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 差旅报销单差旅申请审批日期和行程日期校验
*/
public class TripreimbursebillIsWriteoffapplyCheckOp extends AbstractOperationServicePlugIn {
private static final Logger log = LoggerFactory.getLogger(TripreimbursebillIsWriteoffapplyCheckOp.class);
@Override
public void onPreparePropertys(PreparePropertysEventArgs e) {
super.onPreparePropertys(e);
e.getFieldKeys().add("costcompany");//费用承担公司
e.getFieldKeys().add("tripentry");//行程信息分录
e.getFieldKeys().add("startdate");//行程开始时间
e.getFieldKeys().add("enddate");//行程结束时间
e.getFieldKeys().add("invoiceentry");//发票信息
e.getFieldKeys().add("writeoffapply");//关联申请
e.getFieldKeys().add("zcgjInvoiceremark");//特殊说明
}
@Override
public void onAddValidators(AddValidatorsEventArgs e) {
super.onAddValidators(e);
Long currentUserId = UserServiceHelper.getCurrentUserId();
// 当前用户所属组织
Long mainOrgId = UserServiceHelper.getUserMainOrgId(currentUserId);
//当前切换选择的组织
Long currentOrgId = RequestContext.get().getOrgId();
//当前所在的组织是属于矿山下的
//if(OrgCheckUtils.isKS(currentOrgId)){
e.getValidators().add(new ValidatorExt());
//}
}
class ValidatorExt extends AbstractValidator {
@Override
public void validate() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
ExtendedDataEntity[] extendedDataEntities = this.getDataEntities();
for (ExtendedDataEntity extendedDataEntity : extendedDataEntities) {
DynamicObject dataEntity = extendedDataEntity.getDataEntity();
Object costcompanyObj = dataEntity.get("costcompany");
Object costdeptObj = dataEntity.get("costdept");
if(costcompanyObj!=null && costdeptObj != null){
DynamicObject costcompany = (DynamicObject)costcompanyObj;
long costcompanyId = costcompany.getLong("id");
String number = costcompany.getString("number");
if(OrgCheckUtils.isKS(costcompanyId)){
List<String> errorList = new ArrayList<>();
String zcgjInvoiceremark = dataEntity.getString("zcgj_invoiceremark");//特殊说明
//行程明细会有多个明细
DynamicObjectCollection tripentry = dataEntity.getDynamicObjectCollection("tripentry");
//发票明细
DynamicObjectCollection writeoffapply = dataEntity.getDynamicObjectCollection("writeoffapply");//关联的出差申请
LocalDate earliestAuditDate = null;//最小审批日期
int j=0;
for (DynamicObject dynamicObject : writeoffapply) {
j++;
//er_tripreqbill
long sourceapplybillid = dynamicObject.getLong("sourceapplybillid");
DynamicObject tripreqbill = BusinessDataServiceHelper.loadSingle(sourceapplybillid, "er_tripreqbill");
boolean ischange = tripreqbill.getBoolean("ischange");//出差申请单发生过变更后不进行校验
LocalDate auditDate = dateToLocalDate(dynamicObject.getDate("zcgj_glsq_auditdate"));
if (auditDate != null && !ischange) {
if (earliestAuditDate == null || auditDate.isBefore(earliestAuditDate)) {
earliestAuditDate = auditDate; // 保留最小审批日期
}
}
}
int i= 0;
// 校验1: 是否在行程时间范围内
boolean notInTripRange = false;
for (DynamicObject trip : tripentry) {
i++;
LocalDate startDate = dateToLocalDate(trip.getDate("startdate"));
if (earliestAuditDate != null && startDate != null) {
if (!earliestAuditDate.isEqual(startDate) && !earliestAuditDate.isBefore(startDate)) {
notInTripRange = true;
break;
}
}
}
if (notInTripRange /* && StringUtils.isEmpty(zcgjInvoiceremark)*/) {
//throw new RuntimeException("乘车日期【" + carrierDate + "】不在任何行程时间范围内!");
this.addFatalErrorMessage(extendedDataEntity, String.format("第%d条出差申请单的审批日期晚于第%d段行程的开始日期",j,i));
}
}
}
}
}
}
/**
* java.util.Date 转换为 java.time.LocalDate
* @param date java.util.Date
* @return java.time.LocalDate
*/
public static LocalDate dateToLocalDate(Date date) {
if(date!=null){
return date.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();
}
else{
return null;
}
}
}