合同成本核算自动取数逻辑优化
This commit is contained in:
parent
ddf8cb028a
commit
c81122c3ab
|
|
@ -1,6 +1,7 @@
|
|||
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;
|
||||
|
|
@ -11,8 +12,15 @@ import kd.bos.entity.MainEntityType;
|
|||
import kd.bos.entity.ValueMapItem;
|
||||
import kd.bos.entity.datamodel.events.ChangeData;
|
||||
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
|
||||
import kd.bos.entity.operate.OperateOptionConst;
|
||||
import kd.bos.entity.operate.result.OperationResult;
|
||||
import kd.bos.entity.property.ComboProp;
|
||||
import kd.bos.form.ConfirmCallBackListener;
|
||||
import kd.bos.form.MessageBoxOptions;
|
||||
import kd.bos.form.MessageBoxResult;
|
||||
import kd.bos.form.control.events.BeforeItemClickEvent;
|
||||
import kd.bos.form.events.BeforeDoOperationEventArgs;
|
||||
import kd.bos.form.events.MessageBoxClosedEvent;
|
||||
import kd.bos.form.field.BasedataEdit;
|
||||
import kd.bos.form.field.ComboEdit;
|
||||
import kd.bos.form.field.ComboItem;
|
||||
|
|
@ -24,6 +32,8 @@ import kd.bos.orm.query.QCP;
|
|||
import kd.bos.orm.query.QFilter;
|
||||
import kd.bos.servicehelper.BusinessDataServiceHelper;
|
||||
import kd.bos.servicehelper.QueryServiceHelper;
|
||||
import kd.bos.servicehelper.operation.OperationServiceHelper;
|
||||
import kd.sdk.plugin.Plugin;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
|
|
@ -35,12 +45,13 @@ import java.util.*;
|
|||
* 3:子分录成本分解结构过滤逻辑
|
||||
* 工序即为成本分解结构
|
||||
*/
|
||||
public class EntCostSplitBillPlugin extends AbstractBillPlugIn implements BeforeF7SelectListener {
|
||||
public class EntCostSplitBillPlugin extends AbstractBillPlugIn implements BeforeF7SelectListener, Plugin {
|
||||
@Override
|
||||
public void registerListener(EventObject e) {
|
||||
super.registerListener(e);
|
||||
BasedataEdit cbs = (BasedataEdit) this.getControl("zcgj_cbs");
|
||||
cbs.addBeforeF7SelectListener(this);
|
||||
this.addItemClickListeners("advcontoolbarap");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -114,231 +125,29 @@ public class EntCostSplitBillPlugin extends AbstractBillPlugIn implements Before
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeItemClick(BeforeItemClickEvent evt) {
|
||||
super.beforeItemClick(evt);
|
||||
if (evt.getItemKey().equals("zcgj_autofetch")) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void confirmCallBack(MessageBoxClosedEvent messageBoxClosedEvent) {
|
||||
super.confirmCallBack(messageBoxClosedEvent);
|
||||
//判断是否是对应确认框的点击回调事件
|
||||
if (("autogetdata").equals(messageBoxClosedEvent.getCallBackId())) {
|
||||
if (MessageBoxResult.Yes.equals(messageBoxClosedEvent.getResult())) {
|
||||
autoFetchData();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void beforeDoOperation(BeforeDoOperationEventArgs args) {
|
||||
super.beforeDoOperation(args);
|
||||
FormOperate formOperate = (FormOperate) args.getSource();
|
||||
if ("autogetdata".equals(formOperate.getOperateKey())) {
|
||||
//自动取数
|
||||
Object org = this.getModel().getValue("org");//所属组织
|
||||
if (org == null) {
|
||||
this.getView().showTipNotification("请选择组织");
|
||||
return;
|
||||
}
|
||||
Object period = this.getModel().getValue("period");//期间
|
||||
if (period == null) {
|
||||
this.getView().showTipNotification("请选择期间");
|
||||
return;
|
||||
}
|
||||
this.getModel().beginInit();
|
||||
DynamicObject org1 = (DynamicObject) org;//所属组织
|
||||
DynamicObject period1 = (DynamicObject) period;//期间
|
||||
DynamicObjectCollection entryCollection = this.getModel().getDataEntity(true).getDynamicObjectCollection("entryentity");//企业费用成本分摊明细分录
|
||||
DynamicObjectType entryType = entryCollection.getDynamicObjectType();
|
||||
if (entryCollection.size() > 0) {
|
||||
entryCollection.clear();
|
||||
}
|
||||
|
||||
QFilter filter = new QFilter("zcgj_org", QCP.equals, org1.getPkValue());//核算组织
|
||||
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,zcgj_debitlocal,zcgj_costcompanyname",
|
||||
new QFilter[]{filter});//"核算维度余额取数表(矿山工序维度)"
|
||||
|
||||
// 在循环开始前定义汇总Map
|
||||
Map<String, DynamicObject> summaryMap = new HashMap<>();
|
||||
|
||||
for (DynamicObject rptAssistBalanceGx : rptAssistBalanceGxCollection) {
|
||||
Object debitLocal = rptAssistBalanceGx.get("zcgj_debitlocal");//本期发生借方
|
||||
if (debitLocal == null ||
|
||||
(debitLocal instanceof BigDecimal && ((BigDecimal) debitLocal).compareTo(BigDecimal.ZERO) <= 0) ||
|
||||
(debitLocal instanceof String && ((String) debitLocal).equals("0"))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
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) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QFilter filter3 = new QFilter("zcgj_companyblentry.zcgj_org", QCP.equals, org1.getPkValue());//组织
|
||||
DynamicObject zcgj_companybelong = QueryServiceHelper.queryOne("zcgj_companybelong", "id",
|
||||
new QFilter[]{filter3});//"公司归属区域"
|
||||
Long id = 0L;
|
||||
if (zcgj_companybelong != null) {
|
||||
id = (Long) zcgj_companybelong.get("id");
|
||||
}
|
||||
|
||||
QFilter filter1 = new QFilter("zcgj_acccostentry.zcgj_accountnumber", QCP.equals, bd_accountview.getString("number"));//科目编码
|
||||
filter1.and(new QFilter("zcgj_belongcompany", QCP.equals, id));
|
||||
DynamicObject zcgj_accountcost = QueryServiceHelper.queryOne("zcgj_accountcost",
|
||||
"id,zcgj_acccostentry.zcgj_costtype,zcgj_acccostentry.zcgj_sectype",
|
||||
new QFilter[]{filter1});//"会计科目与成本项目"
|
||||
|
||||
QFilter[] qFilters = new QFilter[]{new QFilter("name", QCP.equals, rptAssistBalanceGx.get("zcgj_processname"))};//工序名称
|
||||
DynamicObject ec_ecbd_pro_cbs = BusinessDataServiceHelper.loadSingle("ec_ecbd_pro_cbs", "id", qFilters);//"项目工序"
|
||||
|
||||
QFilter filter2 = new QFilter("fiaccountorg", QCP.equals, org1.getPkValue());//财务记账组织
|
||||
filter2.and(new QFilter("billstatus", QCP.equals, "C"));//单据状态
|
||||
DynamicObject[] ec_projects = BusinessDataServiceHelper.load("ec_project", "id,zcgj_init", new QFilter[]{filter2});// "项目"
|
||||
|
||||
if (ec_ecbd_pro_cbs == null && zcgj_accountcost == null && ec_projects.length == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 构建去重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; // 二级分类
|
||||
|
||||
if (costType == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String secTypeName = "";
|
||||
if (secType != null) {
|
||||
secTypeName = secType.toString();
|
||||
}
|
||||
String costTypeName = costType.toString();
|
||||
|
||||
// 如果二级分类为"70.55"且成本项为"70",则对该行的成本金额进行扣减
|
||||
if ("70.55".equals(secTypeName) && "70.".equals(costTypeName)) {
|
||||
QFilter filter5 = new QFilter("billstatus", QCP.equals, "C");//单据状态
|
||||
filter5.and(new QFilter("zcgj_entryentity.zcgj_assperiod", QCP.equals, period1.getPkValue()));//折旧期间
|
||||
filter5.and(new QFilter("zcgj_entryentity.zcgj_zjuseorg", QCP.equals, org1.getPkValue()));//使用组织
|
||||
DynamicObjectCollection eceq_equipinfoCollection = QueryServiceHelper.query("eceq_equipinfo",
|
||||
"id,zcgj_entryentity.zcgj_shareamount",
|
||||
new QFilter[]{filter5});//"设备详情"
|
||||
if (eceq_equipinfoCollection.size() > 0) {
|
||||
BigDecimal zcgj_shareamounts = BigDecimal.ZERO;
|
||||
for (DynamicObject eceq_equipinfo : eceq_equipinfoCollection) {
|
||||
BigDecimal shareAmount = eceq_equipinfo.getBigDecimal("zcgj_entryentity.zcgj_shareamount");
|
||||
if (shareAmount != null) {
|
||||
zcgj_shareamounts = zcgj_shareamounts.add(shareAmount);
|
||||
}
|
||||
}
|
||||
|
||||
// 扣减成本金额
|
||||
Object currentAmount = rptAssistBalanceGx.get("zcgj_debitlocal");
|
||||
BigDecimal newAmount = new BigDecimal(currentAmount != null ? currentAmount.toString() : "0")
|
||||
.subtract(zcgj_shareamounts);
|
||||
|
||||
// 确保不会出现负数
|
||||
if (newAmount.compareTo(BigDecimal.ZERO) < 0) {
|
||||
newAmount = BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
// 更新rptAssistBalanceGx中的金额
|
||||
rptAssistBalanceGx.set("zcgj_debitlocal", newAmount);
|
||||
}
|
||||
}
|
||||
|
||||
String uniqueKey = "";
|
||||
if (processName != null && !processName.isEmpty()) {
|
||||
// 存在工序时,按照工序+成本项+二级分类去重
|
||||
uniqueKey = "PROCESS_" + processName + "_" + (costType != null ? costType.toString() : "") + "_" + (secType != null ? secType.toString() : "");
|
||||
} else {
|
||||
// 如果没有工序,则按照成本项+二级分类去重
|
||||
uniqueKey = "NONE_" + (costType != null ? costType.toString() : "") + "_" + (secType != null ? secType.toString() : "");
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// 同时更新子分录的金额
|
||||
DynamicObject subEntry = existingEntry.getDynamicObjectCollection("zcgj_subentryentity").get(0);
|
||||
if (subEntry != null) {
|
||||
subEntry.set("zcgj_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_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"));//成本金额
|
||||
newEntrySubEntryEntity.set("zcgj_costamount", rptAssistBalanceGx.get("zcgj_debitlocal"));//成本金额
|
||||
|
||||
summaryMap.put(uniqueKey, newEntry);
|
||||
subEntryEntityCollection.add(newEntrySubEntryEntity);
|
||||
}
|
||||
}
|
||||
|
||||
// 在添加数据前进行排序
|
||||
// 将summaryMap转换为List并按成本项数值排序
|
||||
List<DynamicObject> sortedEntries = new ArrayList<>(summaryMap.values());
|
||||
sortedEntries.sort((entry1, entry2) -> {
|
||||
Object costType1 = entry1.get("costtype");
|
||||
Object costType2 = entry2.get("costtype");
|
||||
|
||||
if (costType1 == null && costType2 == null) return 0;
|
||||
if (costType1 == null) return -1;
|
||||
if (costType2 == null) return 1;
|
||||
|
||||
try {
|
||||
Double num1 = Double.parseDouble(costType1.toString());
|
||||
Double num2 = Double.parseDouble(costType2.toString());
|
||||
return num1.compareTo(num2);
|
||||
} catch (NumberFormatException e) {
|
||||
return costType1.toString().compareTo(costType2.toString());
|
||||
}
|
||||
});
|
||||
|
||||
entryCollection.clear();
|
||||
// 按排序后的顺序添加数据
|
||||
entryCollection.addAll(sortedEntries);
|
||||
|
||||
this.getView().updateView("entryentity");//刷新分录
|
||||
this.getView().updateView("zcgj_subentryentity");//刷新分录
|
||||
this.getModel().endInit();
|
||||
this.getView().invokeOperation("save");//保存用于刷新
|
||||
this.getView().invokeOperation("refresh");//刷新全局页面
|
||||
} else if ("newentry1".equals(formOperate.getOperateKey())) {
|
||||
if ("newentry1".equals(formOperate.getOperateKey())) {
|
||||
//成本核算维度明细子分录增行按钮
|
||||
int rowIndex = this.getModel().getEntryCurrentRowIndex("entryentity");//企业费用成本分摊明细分录行索引
|
||||
int rowCount = this.getModel().getEntryRowCount("entryentity");//企业费用成本分摊明细分录总行数
|
||||
|
|
@ -353,9 +162,258 @@ public class EntCostSplitBillPlugin extends AbstractBillPlugIn implements Before
|
|||
this.getView().showTipNotification(ResManager.loadKDString("请先选中1行企业费用成本分摊明细分录行数据", "MaterialCostBillPlugin_1", "ec-ecco-formplugin", new Object[0]));
|
||||
args.setCancel(true);
|
||||
}
|
||||
} else if ("autogetdata".equals(formOperate.getOperateKey())) {
|
||||
DynamicObjectCollection entryEntityCollection = this.getModel().getDataEntity(true).getDynamicObjectCollection("entryentity");//企业费用成本分摊明细分录
|
||||
if (entryEntityCollection.size() > 0) {
|
||||
args.setCancel(true);
|
||||
ConfirmCallBackListener confirmCallBackListener = new ConfirmCallBackListener("autogetdata", this);
|
||||
//设置页面确认框,参数为:标题,选项框类型,回调监听
|
||||
this.getView().showConfirm("自动取数将清空列表数据,您确认执行吗?", MessageBoxOptions.YesNo, confirmCallBackListener);
|
||||
} else {
|
||||
autoFetchData();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动取数处理方法
|
||||
*/
|
||||
private void autoFetchData() {
|
||||
//自动取数
|
||||
Object org = this.getModel().getValue("org");//所属组织
|
||||
if (org == null) {
|
||||
this.getView().showTipNotification("请选择组织");
|
||||
return;
|
||||
}
|
||||
Object period = this.getModel().getValue("period");//期间
|
||||
if (period == null) {
|
||||
this.getView().showTipNotification("请选择期间");
|
||||
return;
|
||||
}
|
||||
this.getModel().beginInit();
|
||||
DynamicObject org1 = (DynamicObject) org;//所属组织
|
||||
DynamicObject period1 = (DynamicObject) period;//期间
|
||||
DynamicObjectCollection entryCollection = this.getModel().getDataEntity(true).getDynamicObjectCollection("entryentity");//企业费用成本分摊明细分录
|
||||
DynamicObjectType entryType = entryCollection.getDynamicObjectType();
|
||||
if (entryCollection.size() > 0) {
|
||||
entryCollection.clear();
|
||||
}
|
||||
|
||||
QFilter filter = new QFilter("zcgj_org", QCP.equals, org1.getPkValue());//核算组织
|
||||
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,zcgj_debitlocal,zcgj_costcompanyname",
|
||||
new QFilter[]{filter});//"核算维度余额取数表(矿山工序维度)"
|
||||
|
||||
// 在循环开始前定义汇总Map
|
||||
Map<String, DynamicObject> summaryMap = new HashMap<>();
|
||||
|
||||
for (DynamicObject rptAssistBalanceGx : rptAssistBalanceGxCollection) {
|
||||
Object debitLocal = rptAssistBalanceGx.get("zcgj_debitlocal");//本期发生借方
|
||||
if (debitLocal == null ||
|
||||
(debitLocal instanceof BigDecimal && ((BigDecimal) debitLocal).compareTo(BigDecimal.ZERO) <= 0) ||
|
||||
(debitLocal instanceof String && ((String) debitLocal).equals("0"))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
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) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QFilter filter3 = new QFilter("zcgj_companyblentry.zcgj_org", QCP.equals, org1.getPkValue());//组织
|
||||
DynamicObject zcgj_companybelong = QueryServiceHelper.queryOne("zcgj_companybelong", "id",
|
||||
new QFilter[]{filter3});//"公司归属区域"
|
||||
Long id = 0L;
|
||||
if (zcgj_companybelong != null) {
|
||||
id = (Long) zcgj_companybelong.get("id");
|
||||
}
|
||||
|
||||
QFilter filter1 = new QFilter("zcgj_acccostentry.zcgj_accountnumber", QCP.equals, bd_accountview.getString("number"));//科目编码
|
||||
filter1.and(new QFilter("zcgj_belongcompany", QCP.equals, id));
|
||||
DynamicObject zcgj_accountcost = QueryServiceHelper.queryOne("zcgj_accountcost",
|
||||
"id,zcgj_acccostentry.zcgj_costtype,zcgj_acccostentry.zcgj_sectype",
|
||||
new QFilter[]{filter1});//"会计科目与成本项目"
|
||||
|
||||
QFilter[] qFilters = new QFilter[]{new QFilter("name", QCP.equals, rptAssistBalanceGx.get("zcgj_processname"))};//工序名称
|
||||
DynamicObject ec_ecbd_pro_cbs = BusinessDataServiceHelper.loadSingle("ec_ecbd_pro_cbs", "id", qFilters);//"项目工序"
|
||||
|
||||
QFilter filter2 = new QFilter("fiaccountorg", QCP.equals, org1.getPkValue());//财务记账组织
|
||||
filter2.and(new QFilter("billstatus", QCP.equals, "C"));//单据状态
|
||||
DynamicObject[] ec_projects = BusinessDataServiceHelper.load("ec_project", "id,zcgj_init", new QFilter[]{filter2});// "项目"
|
||||
|
||||
if (ec_ecbd_pro_cbs == null && zcgj_accountcost == null && ec_projects.length == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 构建去重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; // 二级分类
|
||||
|
||||
if (costType == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String secTypeName = "";
|
||||
if (secType != null) {
|
||||
secTypeName = secType.toString();
|
||||
}
|
||||
String costTypeName = costType.toString();
|
||||
|
||||
// 如果二级分类为"70.55"且成本项为"70",则对该行的成本金额进行扣减
|
||||
if ("70.55".equals(secTypeName) && "70.".equals(costTypeName)) {
|
||||
QFilter filter5 = new QFilter("billstatus", QCP.equals, "C");//单据状态
|
||||
filter5.and(new QFilter("zcgj_entryentity.zcgj_assperiod", QCP.equals, period1.getPkValue()));//折旧期间
|
||||
filter5.and(new QFilter("zcgj_entryentity.zcgj_zjuseorg", QCP.equals, org1.getPkValue()));//使用组织
|
||||
DynamicObjectCollection eceq_equipinfoCollection = QueryServiceHelper.query("eceq_equipinfo",
|
||||
"id,zcgj_entryentity.zcgj_shareamount",
|
||||
new QFilter[]{filter5});//"设备详情"
|
||||
if (eceq_equipinfoCollection.size() > 0) {
|
||||
BigDecimal zcgj_shareamounts = BigDecimal.ZERO;
|
||||
for (DynamicObject eceq_equipinfo : eceq_equipinfoCollection) {
|
||||
BigDecimal shareAmount = eceq_equipinfo.getBigDecimal("zcgj_entryentity.zcgj_shareamount");
|
||||
if (shareAmount != null) {
|
||||
zcgj_shareamounts = zcgj_shareamounts.add(shareAmount);
|
||||
}
|
||||
}
|
||||
|
||||
// 扣减成本金额
|
||||
Object currentAmount = rptAssistBalanceGx.get("zcgj_debitlocal");
|
||||
BigDecimal newAmount = new BigDecimal(currentAmount != null ? currentAmount.toString() : "0")
|
||||
.subtract(zcgj_shareamounts);
|
||||
|
||||
// 确保不会出现负数
|
||||
if (newAmount.compareTo(BigDecimal.ZERO) < 0) {
|
||||
newAmount = BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
// 更新rptAssistBalanceGx中的金额
|
||||
rptAssistBalanceGx.set("zcgj_debitlocal", newAmount);
|
||||
}
|
||||
}
|
||||
|
||||
String uniqueKey = "";
|
||||
if (processName != null && !processName.isEmpty()) {
|
||||
// 存在工序时,按照工序+成本项+二级分类去重
|
||||
uniqueKey = "PROCESS_" + processName + "_" + (costType != null ? costType.toString() : "") + "_" + (secType != null ? secType.toString() : "");
|
||||
} else {
|
||||
// 如果没有工序,则按照成本项+二级分类去重
|
||||
uniqueKey = "NONE_" + (costType != null ? costType.toString() : "") + "_" + (secType != null ? secType.toString() : "");
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// 同时更新子分录的金额
|
||||
DynamicObject subEntry = existingEntry.getDynamicObjectCollection("zcgj_subentryentity").get(0);
|
||||
if (subEntry != null) {
|
||||
subEntry.set("zcgj_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_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"));//成本金额
|
||||
newEntrySubEntryEntity.set("zcgj_costamount", rptAssistBalanceGx.get("zcgj_debitlocal"));//成本金额
|
||||
|
||||
summaryMap.put(uniqueKey, newEntry);
|
||||
subEntryEntityCollection.add(newEntrySubEntryEntity);
|
||||
}
|
||||
}
|
||||
|
||||
// 在添加数据前进行排序
|
||||
// 将summaryMap转换为List并按成本项数值排序
|
||||
List<DynamicObject> sortedEntries = new ArrayList<>(summaryMap.values());
|
||||
sortedEntries.sort((entry1, entry2) -> {
|
||||
Object costType1 = entry1.get("costtype");
|
||||
Object costType2 = entry2.get("costtype");
|
||||
|
||||
if (costType1 == null && costType2 == null) return 0;
|
||||
if (costType1 == null) return -1;
|
||||
if (costType2 == null) return 1;
|
||||
|
||||
try {
|
||||
Double num1 = Double.parseDouble(costType1.toString());
|
||||
Double num2 = Double.parseDouble(costType2.toString());
|
||||
return num1.compareTo(num2);
|
||||
} catch (NumberFormatException e) {
|
||||
return costType1.toString().compareTo(costType2.toString());
|
||||
}
|
||||
});
|
||||
|
||||
entryCollection.clear();
|
||||
StringBuilder costTypeBuilder = new StringBuilder();
|
||||
for (int i = 0; i < sortedEntries.size(); i++) {
|
||||
DynamicObject dynamicObject = sortedEntries.get(i);
|
||||
String costtype = dynamicObject.getString("costtype");
|
||||
|
||||
if (i > 0) {
|
||||
costTypeBuilder.append("+");
|
||||
}
|
||||
costTypeBuilder.append(costtype);
|
||||
}
|
||||
String mergedCostTypes = costTypeBuilder.toString();
|
||||
this.getModel().setValue("description", mergedCostTypes);
|
||||
// 按排序后的顺序添加数据
|
||||
entryCollection.addAll(sortedEntries);
|
||||
|
||||
this.getModel().endInit();
|
||||
OperateOption option = OperateOption.create();
|
||||
option.setVariableValue(OperateOptionConst.ISSHOWMESSAGE, "false");
|
||||
OperationServiceHelper.executeOperate("save", this.getView().getEntityId(),
|
||||
new DynamicObject[]{this.getModel().getDataEntity()}, option);
|
||||
this.getView().showSuccessNotification("已完成取数");
|
||||
this.getView().invokeOperation("refresh");//刷新全局页面
|
||||
}
|
||||
|
||||
private static Integer getSortValue(String type) {
|
||||
if ("10.".equals(type)) return 10;
|
||||
|
|
|
|||
Loading…
Reference in New Issue