入库单优化费用汇总明细分录逻辑

This commit is contained in:
xuhaihui 2025-10-30 17:25:44 +08:00
parent 1aef4df699
commit beade8ba3d
2 changed files with 165 additions and 45 deletions

View File

@ -1609,7 +1609,7 @@ public class MaterialInBillEditPluginExt extends AbstractEcmaBillPlugin implemen
recordTransamount = transamount.divide(new BigDecimal(entryRowCount3), 10, RoundingMode.CEILING);
recordTransoftaxamount = transoftaxamount.divide(new BigDecimal(entryRowCount3), 10, RoundingMode.CEILING);
zcgj_transtaxamounts = transtaxamount.divide(new BigDecimal(entryRowCount3), 10, RoundingMode.CEILING);
this.getModel().beginInit();
// this.getModel().beginInit();//二开注释掉的
lastTransamount = new BigDecimal(0);
lastTransoftaxamount = new BigDecimal(0);
zcgj_transtaxamounts2 = new BigDecimal(0);
@ -1633,9 +1633,9 @@ public class MaterialInBillEditPluginExt extends AbstractEcmaBillPlugin implemen
this.getModel().setValue("taxtransamount", splitTaxAmount, entryRowCount3 - 1);//含税运费
this.getModel().setValue("zcgj_transtaxamount", zcgj_transtaxamounts3, entryRowCount3 - 1);//运费税额
this.getModel().setValue("amount", EcNumberHelper.add(this.getModel().getValue("ftransamount", entryRowCount3 - 1), amount1), entryRowCount3 - 1);//含运费金额不含税
this.getModel().endInit();
// this.getModel().endInit();//二开注释掉的
} else if ("2".equals(splitType)) {
this.getModel().beginInit();
// this.getModel().beginInit();//二开注释掉的
recordTransamount = new BigDecimal(0);
recordTransoftaxamount = new BigDecimal(0);
zcgj_transtaxamounts = new BigDecimal(0);
@ -1662,14 +1662,14 @@ public class MaterialInBillEditPluginExt extends AbstractEcmaBillPlugin implemen
this.getModel().setValue("taxtransamount", lastTransoftaxamount, entryRowCount3 - 1);//含税运费
this.getModel().setValue("zcgj_transtaxamount", zcgj_transtaxamounts2, entryRowCount3 - 1);//含税运费
this.getModel().setValue("amount", EcNumberHelper.add(this.getModel().getValue("ftransamount", entryRowCount3 - 1), amount), entryRowCount3 - 1);//含运费金额不含税
this.getModel().endInit();
} else {
// this.getModel().endInit();//二开注释掉的
}/* else {
for (int i = 0; i < entryRowCount3; ++i) {
this.getModel().setValue("ftransamount", 0, i);//运费
this.getModel().setValue("taxtransamount", 0, i);//含税运费
this.getModel().setValue("zcgj_transtaxamount", 0, i);//运费税额
}
}
}*///二开注释掉的
this.getView().updateView("entryentity");
this.fieldLockLogic();
@ -1899,9 +1899,10 @@ public class MaterialInBillEditPluginExt extends AbstractEcmaBillPlugin implemen
this.getView().getModel().setValue("transamount", transamount);
this.getView().getModel().setValue("transoftaxamount", transoftaxamount);
this.getView().getModel().setValue("transtaxamount", transtaxamount);
this.getView().getModel().setValue("splittype", splittype);
// this.getView().getModel().setValue("splittype", splittype);//二开注释掉的
this.getView().getModel().setValue("taxrate", cbsInfo);
this.getModel().endInit();
this.getView().getModel().setValue("splittype", splittype);//二开添加的
this.transamountChanged(transamount);
this.transoftaxamountChanged(transoftaxamount);
this.transtaxamountChanged(transtaxamount);

View File

@ -221,8 +221,8 @@ public class MaterialInbPurchaseApplyPlugin extends AbstractBillPlugIn implement
} else {
this.getModel().setValue("zcgj_applidepart", null);//清空申请人部门字段
}
} else if ("totaloftaxamount".equals(key) || "taxamount".equals(key)) {
//入库含税总金额,税额
} else if ("totaloftaxamount".equals(key) || "taxamount".equals(key) || "splittype".equals(key)) {
//入库含税总金额,税额,分摊类型
if ("taxamount".equals(key)) {
//税额
Boolean adjustamount = (Boolean) this.getModel().getValue("adjustamount");//微调金额
@ -244,7 +244,6 @@ public class MaterialInbPurchaseApplyPlugin extends AbstractBillPlugIn implement
BigDecimal calculatedTaxIncludedFreight = totalFreightTaxAmount.add(totalFreightAmount); // 含税运费 = 运费税额 + 运费
BigDecimal calculatedTotalTaxIncluded = totalFreightAmount.add(totalFreightTaxAmount).add(totalNoTaxAmount).add(totalTaxAmount); // 入库含税总金额
this.getModel().beginInit();
this.getModel().setValue("totaltaxamount", calculatedTotalTax); // 总税额
this.getModel().setValue("transtaxamount", totalFreightTaxAmount); // 运费总税额
this.getModel().setValue("transoftaxamount", calculatedTaxIncludedFreight); // 含税总运费
@ -254,7 +253,6 @@ public class MaterialInbPurchaseApplyPlugin extends AbstractBillPlugIn implement
this.getView().updateView("transtaxamount");
this.getView().updateView("transoftaxamount");
this.getView().updateView("totaloftaxamount");
this.getModel().endInit();
setNewExpenseSummary();
} else if ("ftransamount".equals(key)) {
// 入库单明细-运费
@ -269,7 +267,6 @@ public class MaterialInbPurchaseApplyPlugin extends AbstractBillPlugIn implement
BigDecimal calculatedTaxIncludedFreight = totalFreightTaxAmount.add(totalFreightAmount); // 含税运费 = 运费税额 + 运费
BigDecimal calculatedTotalTaxIncluded = totalFreightAmount.add(totalFreightTaxAmount).add(totalNoTaxAmount).add(totalTaxAmount); // 入库含税总金额
this.getModel().beginInit();
this.getModel().setValue("transamount", totalFreightAmount); // 总运费
this.getModel().setValue("totalamount", calculatedTotalAmount); // 入库总金额
this.getModel().setValue("transoftaxamount", calculatedTaxIncludedFreight); // 含税总运费
@ -279,7 +276,6 @@ public class MaterialInbPurchaseApplyPlugin extends AbstractBillPlugIn implement
this.getView().updateView("totalamount");
this.getView().updateView("transoftaxamount");
this.getView().updateView("totaloftaxamount");
this.getModel().endInit();
setNewExpenseSummary();
} else if ("oftaxamount".equals(key) || "taxprice".equals(key)) {
//入库单明细-含税金额入库单明细-入库含税单价
@ -552,11 +548,11 @@ public class MaterialInbPurchaseApplyPlugin extends AbstractBillPlugIn implement
DynamicObjectCollection entryEntityCollection = this.getModel().getDataEntity(true).getDynamicObjectCollection("entryentity");//入库单分录
DynamicObjectCollection expenseSummaryCollection = this.getModel().getDataEntity(true).getDynamicObjectCollection("zcgj_expensesummary");//费用汇总分录
// 查找现有的运费行如果没有则创建新的
DynamicObject freightLine = findOrCreateFreightLine(expenseSummaryCollection);
// 获取分摊类型
String splitType = (String) this.getModel().getValue("splittype");
// 清空费用汇总分录保留运费行
clearNonFreightLines(expenseSummaryCollection);
// 清空费用汇总分录
expenseSummaryCollection.clear();
// 根据入库单分录创建费用行
DynamicObjectType expenseSummaryType = expenseSummaryCollection.getDynamicObjectType();
@ -586,10 +582,13 @@ public class MaterialInbPurchaseApplyPlugin extends AbstractBillPlugIn implement
expenseSummaryCollection.add(newExpenseSummaryEntity);
}
// 更新运费行数据并确保其存在于集合中
updateFreightLineData(freightLine);
if (!expenseSummaryCollection.contains(freightLine)) {
expenseSummaryCollection.add(freightLine);
// 根据分摊类型处理运费行
if ("3".equals(splitType)) {
// 不分摊 - 创建一条固定的运费行使用汇总值
handleUnsplitFreightLine(expenseSummaryCollection);
} else if ("1".equals(splitType) || "2".equals(splitType)) {
// 平分分摊或比例分摊 - 为每条入库单明细创建运费行
handleSplitFreightLines(entryEntityCollection, expenseSummaryCollection);
}
// 处理发票号码汇总逻辑
@ -599,6 +598,134 @@ public class MaterialInbPurchaseApplyPlugin extends AbstractBillPlugIn implement
this.getView().updateView("zcgj_expensesummary");//刷新分录
}
/**
* 处理不分摊情况下的运费行
*
* @param expenseSummaryCollection 费用汇总分录集合
*/
private void handleUnsplitFreightLine(DynamicObjectCollection expenseSummaryCollection) {
DynamicObject newFreightLine = new DynamicObject(expenseSummaryCollection.getDynamicObjectType());
// 设置运费行标识
newFreightLine.set("zcgj_isfreightline", true);
// 使用单据头上的字段汇总值
BigDecimal transSoftTaxAmount = (BigDecimal) getModel().getValue("transoftaxamount"); // 含税运费
BigDecimal transAmount = (BigDecimal) getModel().getValue("transamount"); // 总运费
BigDecimal transtaxAmount = (BigDecimal) getModel().getValue("transtaxamount"); // 运费总税额
DynamicObject taxRate = (DynamicObject) getModel().getValue("taxrate"); // 运费税率
// 设置运费行数据
newFreightLine.set("zcgj_pa_amount", transSoftTaxAmount); // 价税合计-含税总运费
newFreightLine.set("zcgj_amountnotax", transAmount); // 不含税金额-总运费
newFreightLine.set("zcgj_taxamt", transtaxAmount); // 税额-运费总税额
if (taxRate != null) {
newFreightLine.set("zcgj_rateval", taxRate.get("taxrate")); // 税率
}
// 设置固定的运费费用项目
DynamicObject ecbd_resource = BusinessDataServiceHelper.loadSingle("ecbd_resource",
"id,zcgj_expenseitem", new QFilter[]{new QFilter("number", QCP.equals, "ZCKS07")});//清单分类-固定运费
if (ecbd_resource != null) {
DynamicObject expenseItem = ecbd_resource.getDynamicObject("zcgj_expenseitem");
newFreightLine.set("zcgj_expenseitem", expenseItem);
newFreightLine.set("zcgj_shippingcostitem", expenseItem); // 运费费用项目
}
// 添加到费用汇总分录
expenseSummaryCollection.add(newFreightLine);
}
/**
* 处理平分分摊或比例分摊情况下的运费行
*
* @param entryEntityCollection 入库单明细集合
* @param expenseSummaryCollection 费用汇总分录集合
*/
private void handleSplitFreightLines(DynamicObjectCollection entryEntityCollection, DynamicObjectCollection expenseSummaryCollection) {
// 获取单据头上的税率
DynamicObject taxRate = (DynamicObject) getModel().getValue("taxrate");
BigDecimal rateValue = taxRate != null ? (BigDecimal) taxRate.get("taxrate") : BigDecimal.ZERO;
// 遍历入库单明细为每条记录创建一条运费行
for (DynamicObject entryEntity : entryEntityCollection) {
DynamicObject newFreightLine = new DynamicObject(expenseSummaryCollection.getDynamicObjectType());
// 设置运费行标识
newFreightLine.set("zcgj_isfreightline", true);
// 获取运费相关字段
BigDecimal ftransamount = (BigDecimal) entryEntity.get("ftransamount"); // 运费
BigDecimal zcgj_transtaxamount = (BigDecimal) entryEntity.get("zcgj_transtaxamount"); // 运费税额
BigDecimal taxtransamount = (BigDecimal) entryEntity.get("taxtransamount"); // 含税运费
// 计算含税运费运费 + 运费税额
BigDecimal calculatedTaxTransAmount = ftransamount.add(zcgj_transtaxamount);
// 价税合计-含税运费
newFreightLine.set("zcgj_pa_amount", calculatedTaxTransAmount);
// 不含税金额-运费
newFreightLine.set("zcgj_amountnotax", ftransamount);
// 税额-运费税额
newFreightLine.set("zcgj_taxamt", zcgj_transtaxamount);
// 设置税率
newFreightLine.set("zcgj_rateval", rateValue);
// 设置费用项目 - 根据入库单明细中的资源编码获取对应的费用项目
DynamicObject material = entryEntity.getDynamicObject("material");
if (material != null) {
DynamicObject resource = material.getDynamicObject("resource");
if (resource != null) {
String resourceNumber = resource.getString("number");
DynamicObject ecbd_resource = BusinessDataServiceHelper.loadSingle("ecbd_resource",
"id,zcgj_expenseitem", new QFilter[]{new QFilter("number", QCP.equals, resourceNumber)});
if (ecbd_resource != null) {
DynamicObject expenseItem = ecbd_resource.getDynamicObject("zcgj_expenseitem");
newFreightLine.set("zcgj_expenseitem", expenseItem);
}
}
}
// 添加到费用汇总分录
expenseSummaryCollection.add(newFreightLine);
}
}
/**
* 获取运费对应的费用项目
*
* @param entryEntity 入库单明细实体
* @return 费用项目对象
*/
private DynamicObject getExpenseItemForFreight(DynamicObject entryEntity) {
DynamicObject expenseItem = null;
DynamicObject material = entryEntity.getDynamicObject("material");//入库单分录-资源编码
if (material != null) {
DynamicObject resource = material.getDynamicObject("resource");//入库单分录-资源编码-清单分类
if (resource != null) {
String resourceNumber = resource.getString("number");//资源编码-清单分类-编码
DynamicObject ecbd_resource = BusinessDataServiceHelper.loadSingle("ecbd_resource",
"id,zcgj_expenseitem", new QFilter[]{new QFilter("number", QCP.equals, resourceNumber)});//清单分类
expenseItem = ecbd_resource.getDynamicObject("zcgj_expenseitem");//费用项目
}
}
// 如果没有找到对应的费用项目使用默认的运费费用项目
if (expenseItem == null) {
DynamicObject ecbd_resource = BusinessDataServiceHelper.loadSingle("ecbd_resource",
"id,zcgj_expenseitem", new QFilter[]{new QFilter("number", QCP.equals, "ZCKS07")});//清单分类-固定运费
if (ecbd_resource != null) {
expenseItem = ecbd_resource.getDynamicObject("zcgj_expenseitem");
}
}
return expenseItem;
}
/**
* 处理发票号码汇总逻辑
*
@ -636,7 +763,7 @@ public class MaterialInbPurchaseApplyPlugin extends AbstractBillPlugIn implement
invoiceTypeMap.put(invoiceNumber, invoice);
}
// 更新运费行的发票号码和专票/抵扣字段
// 更新所有运费行的发票号码和专票/抵扣字段
for (DynamicObject line : expenseSummaryCollection) {
if (Boolean.TRUE.equals(line.getBoolean("zcgj_isfreightline"))) {
// 运费行 - 设置运费发票号码
@ -653,33 +780,26 @@ public class MaterialInbPurchaseApplyPlugin extends AbstractBillPlugIn implement
line.set("zcgj_isspecialinvoice", false);
line.set("zcgj_offset", false);
}
break;
}
}
// 更新所有非运费行的发票号码和专票/抵扣字段
for (DynamicObject line : expenseSummaryCollection) {
if (Boolean.TRUE.equals(line.getBoolean("zcgj_isfreightline"))) {
continue; // 跳过运费行
}
// 非运费行 - 设置非运费发票号码
if (!nonFreightInvoiceNumbers.isEmpty()) {
String invoiceNumbersStr = String.join(",", nonFreightInvoiceNumbers);
line.set("zcgj_invoicenoentry", invoiceNumbersStr);
// 设置专票和抵扣字段
boolean isSpecialInvoice = checkIfSpecialInvoice(nonFreightInvoiceNumbers, invoiceTypeMap);
line.set("zcgj_isspecialinvoice", isSpecialInvoice);
line.set("zcgj_offset", isSpecialInvoice);
} else {
line.set("zcgj_invoicenoentry", null);
line.set("zcgj_isspecialinvoice", false);
line.set("zcgj_offset", false);
// 非运费行 - 设置非运费发票号码
if (!nonFreightInvoiceNumbers.isEmpty()) {
String invoiceNumbersStr = String.join(",", nonFreightInvoiceNumbers);
line.set("zcgj_invoicenoentry", invoiceNumbersStr);
// 设置专票和抵扣字段
boolean isSpecialInvoice = checkIfSpecialInvoice(nonFreightInvoiceNumbers, invoiceTypeMap);
line.set("zcgj_isspecialinvoice", isSpecialInvoice);
line.set("zcgj_offset", isSpecialInvoice);
} else {
line.set("zcgj_invoicenoentry", null);
line.set("zcgj_isspecialinvoice", false);
line.set("zcgj_offset", false);
}
}
}
}
/**
* 检查发票列表中是否包含专票类型的发票
*
@ -705,7 +825,6 @@ public class MaterialInbPurchaseApplyPlugin extends AbstractBillPlugIn implement
}
/**
* 查找或创建运费行
*