企业成本核算表单插件取数逻辑添加成本金额汇总去重逻辑
This commit is contained in:
		
							parent
							
								
									a4dc2091a3
								
							
						
					
					
						commit
						b783eca764
					
				| 
						 | 
				
			
			@ -12,7 +12,10 @@ import kd.bos.orm.query.QFilter;
 | 
			
		|||
import kd.bos.servicehelper.BusinessDataServiceHelper;
 | 
			
		||||
import kd.bos.servicehelper.QueryServiceHelper;
 | 
			
		||||
 | 
			
		||||
import java.math.BigDecimal;
 | 
			
		||||
import java.util.EventObject;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 企业成本核算表单插件
 | 
			
		||||
| 
						 | 
				
			
			@ -51,11 +54,14 @@ public class EntCostSplitBillPlugin extends AbstractBillPlugIn {
 | 
			
		|||
            filter.and(new QFilter("zcgj_period", QCP.equals, period1.getPkValue()));//期间
 | 
			
		||||
            filter.and(new QFilter("zcgj_isnew", QCP.equals, true));//是否最新
 | 
			
		||||
            DynamicObjectCollection rptAssistBalanceGxCollection = QueryServiceHelper.query("zcgj_rpt_assistbalancegx",
 | 
			
		||||
                    "id,zcgj_account,zcgj_processname",
 | 
			
		||||
                    "id,zcgj_account,zcgj_processname,zcgj_debitlocal,zcgj_costcompanyname",
 | 
			
		||||
                    new QFilter[]{filter});//"核算维度余额取数表(矿山工序维度)"
 | 
			
		||||
 | 
			
		||||
            // 在循环开始前定义汇总Map
 | 
			
		||||
            Map<String, DynamicObject> summaryMap = new HashMap<>();
 | 
			
		||||
 | 
			
		||||
            for (DynamicObject rptAssistBalanceGx : rptAssistBalanceGxCollection) {
 | 
			
		||||
                QFilter[] qFilter = new QFilter[]{new QFilter("id", QCP.equals, rptAssistBalanceGx.get("zcgj_account"))};
 | 
			
		||||
                QFilter[] qFilter = new QFilter[]{new QFilter("id", QCP.equals, rptAssistBalanceGx.get("zcgj_account"))};//科目ID
 | 
			
		||||
                DynamicObject bd_accountview = BusinessDataServiceHelper.loadSingle("bd_accountview",
 | 
			
		||||
                        "id,number,name", qFilter);//"会计科目"
 | 
			
		||||
                if (bd_accountview == null) {
 | 
			
		||||
| 
						 | 
				
			
			@ -74,42 +80,84 @@ public class EntCostSplitBillPlugin extends AbstractBillPlugIn {
 | 
			
		|||
                if (ec_ecbd_pro_cbs == null && zcgj_accountcost == null && ec_projects.length == 0) {
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
                DynamicObject newEntry = new DynamicObject(entryType);
 | 
			
		||||
                DynamicObjectCollection subEntryEntityCollection = newEntry.getDynamicObjectCollection("zcgj_subentryentity");//成本核算维度明细
 | 
			
		||||
                DynamicObjectType subEntryEntityType = subEntryEntityCollection.getDynamicObjectType();
 | 
			
		||||
                DynamicObject newEntrySubEntryEntity = new DynamicObject(subEntryEntityType);
 | 
			
		||||
                if (ec_projects.length != 0) {
 | 
			
		||||
                    for (DynamicObject ec_project : ec_projects) {
 | 
			
		||||
                        boolean zcgj_init = ec_project.getBoolean("zcgj_init");//默认项目
 | 
			
		||||
                        if (zcgj_init) {
 | 
			
		||||
 | 
			
		||||
                // 构建去重key - 根据工序或成本中心分别处理
 | 
			
		||||
                String processName = rptAssistBalanceGx.getString("zcgj_processname"); // 工序名称
 | 
			
		||||
                String costCompanyName = rptAssistBalanceGx.getString("zcgj_costcompanyname"); // 成本中心名称
 | 
			
		||||
                Object costType = zcgj_accountcost != null ? zcgj_accountcost.get("zcgj_acccostentry.zcgj_costtype") : null; // 成本项
 | 
			
		||||
                Object secType = zcgj_accountcost != null ? zcgj_accountcost.get("zcgj_acccostentry.zcgj_sectype") : null; // 二级分类
 | 
			
		||||
 | 
			
		||||
                String uniqueKey = "";
 | 
			
		||||
                if (processName != null && !processName.isEmpty()) {
 | 
			
		||||
                    // 存在工序时,按照工序+成本项+二级分类去重
 | 
			
		||||
                    uniqueKey = "PROCESS_" + processName + "_" + (costType != null ? costType.toString() : "") + "_" + (secType != null ? secType.toString() : "");
 | 
			
		||||
                } else if (costCompanyName != null && !costCompanyName.isEmpty()) {
 | 
			
		||||
                    // 存在成本中心时,按照成本中心+成本项+二级分类去重
 | 
			
		||||
                    uniqueKey = "COSTCENTER_" + costCompanyName + "_" + (costType != null ? costType.toString() : "") + "_" + (secType != null ? secType.toString() : "");
 | 
			
		||||
                } else {
 | 
			
		||||
                    // 如果既没有工序也没有成本中心,则不进行汇总,使用唯一ID
 | 
			
		||||
                    uniqueKey = "NONE_" + rptAssistBalanceGx.getString("id");
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                DynamicObject existingEntry = summaryMap.get(uniqueKey);
 | 
			
		||||
                if (existingEntry != null) {
 | 
			
		||||
                    // 如果已存在相同key的记录,则累加成本金额
 | 
			
		||||
                    Object existingAmount = existingEntry.get("costamount");
 | 
			
		||||
                    Object newAmount = rptAssistBalanceGx.get("zcgj_debitlocal");
 | 
			
		||||
 | 
			
		||||
                    // 进行金额累加
 | 
			
		||||
                    BigDecimal totalAmount = new BigDecimal(existingAmount != null ? existingAmount.toString() : "0")
 | 
			
		||||
                            .add(new BigDecimal(newAmount != null ? newAmount.toString() : "0"));
 | 
			
		||||
                    existingEntry.set("costamount", totalAmount);
 | 
			
		||||
                } else {
 | 
			
		||||
                    // 如果不存在相同key的记录,则创建新记录
 | 
			
		||||
                    DynamicObject newEntry = new DynamicObject(entryType);
 | 
			
		||||
                    DynamicObjectCollection subEntryEntityCollection = newEntry.getDynamicObjectCollection("zcgj_subentryentity");//成本核算维度明细
 | 
			
		||||
                    DynamicObjectType subEntryEntityType = subEntryEntityCollection.getDynamicObjectType();
 | 
			
		||||
                    DynamicObject newEntrySubEntryEntity = new DynamicObject(subEntryEntityType);
 | 
			
		||||
 | 
			
		||||
                    if (ec_projects.length != 0) {
 | 
			
		||||
                        for (DynamicObject ec_project : ec_projects) {
 | 
			
		||||
                            boolean zcgj_init = ec_project.getBoolean("zcgj_init");//默认项目
 | 
			
		||||
                            if (zcgj_init) {
 | 
			
		||||
                                DynamicObject ec_project_f7 = BusinessDataServiceHelper.loadSingle("ec_project_f7", "id",
 | 
			
		||||
                                        new QFilter[]{new QFilter("id", QCP.equals, ec_project.getPkValue())});//项目
 | 
			
		||||
                                newEntry.set("project", ec_project_f7);//"项目"
 | 
			
		||||
                                newEntrySubEntryEntity.set("zcgj_project", ec_project_f7);//项目
 | 
			
		||||
                                break;
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                        Object project = newEntry.get("project");
 | 
			
		||||
                        if (project == null) {
 | 
			
		||||
                            DynamicObject ec_project_f7 = BusinessDataServiceHelper.loadSingle("ec_project_f7", "id",
 | 
			
		||||
                                    new QFilter[]{new QFilter("id", QCP.equals, ec_project.getPkValue())});//项目
 | 
			
		||||
                            newEntry.set("project", ec_project_f7);//"项目"
 | 
			
		||||
                                    new QFilter[]{new QFilter("id", QCP.equals, ec_projects[0].getPkValue())});//项目
 | 
			
		||||
                            newEntry.set("project", ec_project_f7);//项目
 | 
			
		||||
                            newEntrySubEntryEntity.set("zcgj_project", ec_project_f7);//项目
 | 
			
		||||
                            break;
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                    Object project = newEntry.get("project");
 | 
			
		||||
                    if (project == null) {
 | 
			
		||||
                        DynamicObject ec_project_f7 = BusinessDataServiceHelper.loadSingle("ec_project_f7", "id",
 | 
			
		||||
                                new QFilter[]{new QFilter("id", QCP.equals, ec_projects[0].getPkValue())});//项目
 | 
			
		||||
                        newEntry.set("project", ec_project_f7);//项目
 | 
			
		||||
                        newEntrySubEntryEntity.set("zcgj_project", ec_project_f7);//项目
 | 
			
		||||
                    if (ec_ecbd_pro_cbs != null) {
 | 
			
		||||
                        newEntry.set("cbs", ec_ecbd_pro_cbs);//成本分解结构
 | 
			
		||||
                        newEntrySubEntryEntity.set("zcgj_cbs", ec_ecbd_pro_cbs);//成本分解结构
 | 
			
		||||
                    }
 | 
			
		||||
                    if (zcgj_accountcost != null) {
 | 
			
		||||
                        newEntry.set("costtype", zcgj_accountcost.get("zcgj_acccostentry.zcgj_costtype"));//成本项
 | 
			
		||||
                        newEntry.set("zcgj_sectype", zcgj_accountcost.get("zcgj_acccostentry.zcgj_sectype"));//二级分类
 | 
			
		||||
                        newEntrySubEntryEntity.set("zcgj_costtype", zcgj_accountcost.get("zcgj_acccostentry.zcgj_costtype"));//成本项
 | 
			
		||||
                        newEntrySubEntryEntity.set("zcgj_sectype1", zcgj_accountcost.get("zcgj_acccostentry.zcgj_sectype"));//二级分类
 | 
			
		||||
                    }
 | 
			
		||||
                    newEntry.set("costamount", rptAssistBalanceGx.get("zcgj_debitlocal"));//成本金额
 | 
			
		||||
 | 
			
		||||
                    summaryMap.put(uniqueKey, newEntry);
 | 
			
		||||
                    subEntryEntityCollection.add(newEntrySubEntryEntity);
 | 
			
		||||
                }
 | 
			
		||||
                if (ec_ecbd_pro_cbs != null) {
 | 
			
		||||
                    newEntry.set("cbs", ec_ecbd_pro_cbs);//成本分解结构
 | 
			
		||||
                    newEntrySubEntryEntity.set("zcgj_cbs", ec_ecbd_pro_cbs);//成本分解结构
 | 
			
		||||
                }
 | 
			
		||||
                if (zcgj_accountcost != null) {
 | 
			
		||||
                    newEntry.set("costtype", zcgj_accountcost.get("zcgj_acccostentry.zcgj_costtype"));//成本项
 | 
			
		||||
                    newEntry.set("zcgj_sectype", zcgj_accountcost.get("zcgj_acccostentry.zcgj_sectype"));//二级分类
 | 
			
		||||
                    newEntrySubEntryEntity.set("zcgj_costtype", zcgj_accountcost.get("zcgj_acccostentry.zcgj_costtype"));//成本项
 | 
			
		||||
                    newEntrySubEntryEntity.set("zcgj_sectype1", zcgj_accountcost.get("zcgj_acccostentry.zcgj_sectype"));//二级分类
 | 
			
		||||
                }
 | 
			
		||||
                entryCollection.add(newEntry);
 | 
			
		||||
                subEntryEntityCollection.add(newEntrySubEntryEntity);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // 清空原集合并添加汇总后的数据
 | 
			
		||||
            entryCollection.clear();
 | 
			
		||||
            for (DynamicObject entry : summaryMap.values()) {
 | 
			
		||||
                entryCollection.add(entry);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            this.getView().updateView("entryentity");//刷新分录
 | 
			
		||||
            this.getView().updateView("zcgj_subentryentity");//刷新分录
 | 
			
		||||
            this.getModel().endInit();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue