发票优化

This commit is contained in:
zengweihai 2024-10-29 14:41:32 +08:00
parent 6976568dbb
commit 67537c5b52
2 changed files with 372 additions and 100 deletions

View File

@ -50,6 +50,7 @@ import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.AttachmentDto; import kd.bos.servicehelper.AttachmentDto;
import kd.bos.servicehelper.AttachmentServiceHelper; import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.bos.servicehelper.BusinessDataServiceHelper; import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.botp.BFTrackerServiceHelper;
import kd.bos.servicehelper.coderule.CodeRuleServiceHelper; import kd.bos.servicehelper.coderule.CodeRuleServiceHelper;
import kd.bos.servicehelper.operation.DeleteServiceHelper; import kd.bos.servicehelper.operation.DeleteServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper; import kd.bos.servicehelper.operation.OperationServiceHelper;
@ -74,6 +75,7 @@ import javax.xml.bind.DatatypeConverter;
import java.io.*; import java.io.*;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.net.URL; import java.net.URL;
import java.sql.Timestamp;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -428,8 +430,6 @@ public class ReconPayreqFormPlugin extends AbstractFormPlugin implements BeforeF
}else { }else {
this.getView().showTipNotification("请先选择用款单位!"); this.getView().showTipNotification("请先选择用款单位!");
} }
break; break;
//发票验真验重 //发票验真验重
case "qeug_refinvoice": case "qeug_refinvoice":
@ -538,7 +538,6 @@ public class ReconPayreqFormPlugin extends AbstractFormPlugin implements BeforeF
invoiceEntry.set("inventry_invoicebill", invoiceBill); invoiceEntry.set("inventry_invoicebill", invoiceBill);
invoiceEntry.set("qeug_invoiceno",invoiceBill.getString("invoiceno")); invoiceEntry.set("qeug_invoiceno",invoiceBill.getString("invoiceno"));
invoiceEntry.set("inventry_haswriteoffamt", invoiceBill.getBigDecimal("writeoffamount")); invoiceEntry.set("inventry_haswriteoffamt", invoiceBill.getBigDecimal("writeoffamount"));
invoiceAmt = invoiceAmt.add(invoiceBill.getBigDecimal("invoiceamount")); invoiceAmt = invoiceAmt.add(invoiceBill.getBigDecimal("invoiceamount"));
} }
@ -555,6 +554,8 @@ public class ReconPayreqFormPlugin extends AbstractFormPlugin implements BeforeF
FormOperate source = (FormOperate) args.getSource(); FormOperate source = (FormOperate) args.getSource();
String operateKey = source.getOperateKey(); String operateKey = source.getOperateKey();
DynamicObject dataEntity = this.getModel().getDataEntity();//单据数据
Object pk = this.getModel().getDataEntity().getPkValue();//当前单据主键
switch (operateKey) { switch (operateKey) {
//删行 //删行
case "deleteinvoiceentry": case "deleteinvoiceentry":
@ -604,6 +605,66 @@ public class ReconPayreqFormPlugin extends AbstractFormPlugin implements BeforeF
} }
break; break;
case "confirmchange":
//确认核销付款申请/费用登记关联发票
String billtype =this.getModel().getDataEntityType().getName();//单据标识
//todo:若单据为费用登记单
boolean billFlag = true;
if ("recon_connotextbill".equals(billtype)){
Map<String, HashSet<Long>> tarBills = BFTrackerServiceHelper.findTargetBills("recon_connotextbill", new Long[]{(Long) pk});//查询费用登记的下游单据
HashSet<Long> longs = new HashSet<>();
if (tarBills.containsKey("recon_payreqbill")){
longs = tarBills.get("recon_payreqbill");
}
if (longs.size() > 0){//当下游付款申请单存在时
Object[] objects = longs.toArray();
Object payPk= objects[0];//更新主键为付款申请单据
DynamicObject recon_payreqbill = BusinessDataServiceHelper.loadSingle(payPk, "recon_payreqbill");
if (recon_payreqbill != null){
String billstatus = recon_payreqbill.getString("billstatus");
if (!"C".equals(billstatus)){
billFlag = false;
args.setCancel(true);//取消操作
this.getView().showTipNotification("下游关联付款申请单据状态异常,无法核销");
}
}else {
billFlag = false;
args.setCancel(true);//取消操作
this.getView().showTipNotification("该费用登记单据无下游关联付款申请单据,无法核销");
}
}else {
billFlag = false;
args.setCancel(true);//取消操作
this.getView().showTipNotification("该费用登记单据无下游关联付款申请单据,无法核销");
}
}
if (dataEntity.getBoolean("qeug_isticket") && billFlag){//确定付款是否需要补票且当单据为费用登记时需要判断关联的付款申请是否异常
BigDecimal applyamt = dataEntity.getBigDecimal("applyamt");//本期申请金额
BigDecimal totalAmount = BigDecimal.ZERO;
DynamicObjectCollection invoiceentry = dataEntity.getDynamicObjectCollection("invoiceentry");//发票信息分录
for (DynamicObject invoice : invoiceentry) {
totalAmount = totalAmount.add(invoice.getBigDecimal("inventry_haswriteoffamt"));//遍历计算总发票核销金额
}
if (applyamt.compareTo(totalAmount) >= 0){
if (applyamt.compareTo(totalAmount) == 0){//完全核销付款申请金额等于发票金额
boolean bool = VerificationInvoice(dataEntity, pk);
if(bool){
//todo:4根据当前付款申请的发票分录所有数据修改数据后取消补单状态
this.getModel().setValue("qeug_isticket",false);
}else {
args.setCancel(true);//取消操作
}
}else if(applyamt.compareTo(totalAmount) > 0){//付款申请金额大于发票金额,此时不允许核销
args.setCancel(true);//取消操作
this.getView().showTipNotification("本次申请金额大于发票核销金额之和,请修改补录发票");
}
} else {
args.setCancel(true);//取消操作
this.getView().showTipNotification("总发票核销金额大于本次申请金额,请修改补录发票");
}
}
break;
default: default:
break; break;
} }
@ -789,5 +850,190 @@ public class ReconPayreqFormPlugin extends AbstractFormPlugin implements BeforeF
} }
} }
} }
/**
* 校验发票分录金额是否存在差异之前已经核销的发票需要返回已核销金额后再进行比较
* @param invoiceentry 付款申请发票分录数据
* @param pk 付款申请单主键
*/
public boolean checkInvoiceAmount(DynamicObjectCollection invoiceentry,Object pk){
boolean flag = true;
//todo:1查询当前申请的是否存在已经核销的发票
QFilter qFilter = new QFilter("writeoffinfoentry.wentry_paybill.id", QCP.equals,pk);
DynamicObject[] invoiceBills = BusinessDataServiceHelper.load("recon_invoicebill", "id,invoiceno,invoiceamount,haswriteoffamount,writeoffamount,writeoffinfoentry.wentry_paybill.number,writeoffinfoentry.wentry_writroffamount,writeoffinfoentry.wentry_bizdate", new QFilter[]{qFilter});
ArrayList<String> alreadyInvoices = new ArrayList<>();
for (DynamicObject invoiceBill : invoiceBills) {//添加该申请单已核销的发票号码
alreadyInvoices.add(invoiceBill.getString("invoiceno"));
}
for (int i = 0; i < invoiceentry.size();i++){
DynamicObject invoice = invoiceentry.get(i);
String qeug_invoiceno = invoice.getString("qeug_invoiceno");
QFilter no_qf = new QFilter("invoiceno", QCP.equals, qeug_invoiceno);
DynamicObject invoiceBill = BusinessDataServiceHelper.loadSingle("recon_invoicebill", no_qf.toArray());
BigDecimal inventry_haswriteoffamt = invoice.getBigDecimal("inventry_haswriteoffamt");//发票申请核销金额
if (invoiceBill != null){
Object invoicePk = invoiceBill.getPkValue();
DynamicObject recon_invoicebill = BusinessDataServiceHelper.loadSingle(invoicePk, "recon_invoicebill");//查询实际发票数据
if (recon_invoicebill != null){
String invoiceno = recon_invoicebill.getString("invoiceno");
DynamicObjectCollection writeoffinfoentry = recon_invoicebill.getDynamicObjectCollection("writeoffinfoentry");//获取核销信息分录
BigDecimal writeoffamount = recon_invoicebill.getBigDecimal("writeoffamount");//获取未核销金额
if (alreadyInvoices.contains(invoiceno)){
for (DynamicObject dynamicObject : writeoffinfoentry) {
Object payBillPk = dynamicObject.getDynamicObject("wentry_paybill").getPkValue();//核销付款单id
if (pk.equals(payBillPk)){
BigDecimal wentry_writroffamount = dynamicObject.getBigDecimal("wentry_writroffamount");//获取分录核销金额
writeoffamount = writeoffamount.add(wentry_writroffamount);//还原未核销金额
}
}
}
if (writeoffamount.compareTo(inventry_haswriteoffamt) < 0){
this.getView().showTipNotification(""+(i+1)+"发票未核销金额不足,请重新关联发票");
flag = false;
}
}else {
this.getView().showTipNotification(""+(i+1)+"发票行数据异常,请重新关联发票");
flag = false;
}
}else {
this.getView().showTipNotification(""+(i+1)+"发票行数据异常,请重新关联发票");
flag = false;
}
}
return flag;
}
/**
* 发票金额核销数据更新根据校验未核销金额结果返回判断值正确则更新发票核销数据
* @param dataEntity
* @param pk
* @return
*/
public boolean VerificationInvoice(DynamicObject dataEntity,Object pk){
String billtype =this.getModel().getDataEntityType().getName();//单据标识
DynamicObjectCollection invoiceentry = dataEntity.getDynamicObjectCollection("invoiceentry");//发票信息分录
BigDecimal applyamt = dataEntity.getBigDecimal("applyamt");//本期申请金额
if ("recon_connotextbill".equals(billtype)) {//当单据为费用登记时
Map<String, HashSet<Long>> tarBills = BFTrackerServiceHelper.findTargetBills("recon_connotextbill", new Long[]{(Long) pk});//查询费用登记的下游单据
HashSet<Long> longs = new HashSet<>();
if (tarBills.containsKey("recon_payreqbill")){
longs = tarBills.get("recon_payreqbill");
}
if (longs.size() > 0){//当下游付款申请单存在时
Object[] objects = longs.toArray();
pk= objects[0];//更新主键为付款申请单据
}
}
boolean flag = checkInvoiceAmount(invoiceentry,pk);
if (flag){//校验发票未核销金额是否足额
//todo:0,若单据类型为费用登记则同步费用登记发票分录数据到付款申请上
if ("recon_connotextbill".equals(billtype)) {//当单据为费用登记时
DynamicObject recon_payreqbill = BusinessDataServiceHelper.loadSingle(pk, "recon_payreqbill");//获取下游付款申请单完整数据
DynamicObjectCollection text_invoiceentry = dataEntity.getDynamicObjectCollection("invoiceentry");
DynamicObjectCollection pay_invoiceentry = recon_payreqbill.getDynamicObjectCollection("invoiceentry");
if (text_invoiceentry.size() > 0){
pay_invoiceentry.clear();
for (DynamicObject dynamicObject : text_invoiceentry) {
DynamicObject addNew = pay_invoiceentry.addNew();
addNew.set("inventry_invoicebill",dynamicObject.getDynamicObject("inventry_invoicebill").getPkValue());
addNew.set("qeug_invoiceno",dynamicObject.getString("qeug_invoiceno"));
addNew.set("inventry_haswriteoffamt",dynamicObject.getBigDecimal("inventry_haswriteoffamt"));
}
SaveServiceHelper.save(new DynamicObject[]{recon_payreqbill});//保存同步发票分录后的付款申请
}
}
//todo:1使用pk付款申请单id查询当前申请的是否存在已经核销的发票
QFilter qFilter = new QFilter("writeoffinfoentry.wentry_paybill.id", QCP.equals,pk);
DynamicObject[] invoiceBills = BusinessDataServiceHelper.load("recon_invoicebill", "id,invoiceno,invoiceamount,haswriteoffamount,writeoffamount,writeoffinfoentry.wentry_paybill.number,writeoffinfoentry.wentry_writroffamount,writeoffinfoentry.wentry_bizdate", new QFilter[]{qFilter});
ArrayList<String> alreadyInvoices = new ArrayList<>();
for (DynamicObject invoiceBill : invoiceBills) {//添加该申请单已核销的发票号码
alreadyInvoices.add(invoiceBill.getString("invoiceno"));
}
//todo:2遍历当前单据的发票分录数据比较已关联发票数据,修改数据
for (DynamicObject invoice : invoiceentry) {
String qeug_invoiceno = invoice.getString("qeug_invoiceno");
QFilter no_qf = new QFilter("invoiceno", QCP.equals, qeug_invoiceno);
DynamicObject invoiceBill = BusinessDataServiceHelper.loadSingle("recon_invoicebill", no_qf.toArray());
BigDecimal inventry_haswriteoffamt = invoice.getBigDecimal("inventry_haswriteoffamt");//发票申请核销金额
if (invoiceBill != null){
Object invoicePk = invoiceBill.getPkValue();//获取发票主键
DynamicObject recon_invoicebill = BusinessDataServiceHelper.loadSingle(invoicePk, "recon_invoicebill");//查询实际发票数据
if (recon_invoicebill != null){
String invoiceno = recon_invoicebill.getString("invoiceno");//发票号码
if (!Strings.isEmpty(invoiceno)){
boolean isContains = alreadyInvoices.contains(invoiceno);//判断当前行发票是否已经被关联核销
if (isContains){//当此时关联发票已经被核销的情况
DynamicObjectCollection writeoffinfoentry = recon_invoicebill.getDynamicObjectCollection("writeoffinfoentry");//获取核销信息分录
for (DynamicObject dynamicObject : writeoffinfoentry) {
Object payBillPk = dynamicObject.getDynamicObject("wentry_paybill").getPkValue();//核销付款单id
if (pk.equals(payBillPk)){
BigDecimal wentry_writroffamount = dynamicObject.getBigDecimal("wentry_writroffamount");//获取分录核销金额
if (wentry_writroffamount.compareTo(inventry_haswriteoffamt) != 0){//当付款单发票核销金额同已核销数据不一致时
BigDecimal haswriteoffamount = recon_invoicebill.getBigDecimal("haswriteoffamount");//目前已核销金额
haswriteoffamount = haswriteoffamount.subtract(wentry_writroffamount);//还原已核销金额
haswriteoffamount = haswriteoffamount.add(inventry_haswriteoffamt);//重新添加出已核销金额
recon_invoicebill.set("haswriteoffamount",haswriteoffamount);//赋值已核销金额
BigDecimal writeoffamount = recon_invoicebill.getBigDecimal("writeoffamount");//目前未核销金额
writeoffamount = writeoffamount.add(wentry_writroffamount);//还原未核销金额
writeoffamount = writeoffamount.subtract(inventry_haswriteoffamt);
recon_invoicebill.set("writeoffamount",writeoffamount);//赋值未核销金额
dynamicObject.set("wentry_writroffamount",inventry_haswriteoffamt);//更新核销金额
SaveServiceHelper.save(new DynamicObject[]{recon_invoicebill});//更新发票数据
alreadyInvoices.remove(invoiceno);
break;
}
}
}
}else {
DynamicObjectCollection writeoffinfoentry = recon_invoicebill.getDynamicObjectCollection("writeoffinfoentry");//获取核销信息分录
DynamicObject dynamicObject = writeoffinfoentry.addNew();
dynamicObject.set("wentry_paybill",pk);//付款申请单编号
dynamicObject.set("wentry_payamount",applyamt);//付款金额
dynamicObject.set("wentry_writroffamount",inventry_haswriteoffamt);//核销金额
BigDecimal haswriteoffamount = recon_invoicebill.getBigDecimal("haswriteoffamount");//目前已核销金额
haswriteoffamount = haswriteoffamount.add(inventry_haswriteoffamt);//增加已核销金额
recon_invoicebill.set("haswriteoffamount",haswriteoffamount);//赋值已核销金额
BigDecimal writeoffamount = recon_invoicebill.getBigDecimal("writeoffamount");//目前未核销金额
writeoffamount = writeoffamount.subtract(inventry_haswriteoffamt);
recon_invoicebill.set("writeoffamount",writeoffamount);//赋值未核销金额
SaveServiceHelper.save(new DynamicObject[]{recon_invoicebill});//更新发票数据
}
}
}
}
}
//todo:3根据当前付款申请的发票分录数据修改数据后剩下的发票为废弃关联需返回核销金额
if(alreadyInvoices.size()>0){//当已核销发票存在时返还核销金额
for (String alreadyInvoice : alreadyInvoices) {
QFilter alreadyInvoice_qf = new QFilter("invoiceno", QCP.equals, alreadyInvoice);//设置发票登记过滤条件
DynamicObject recon_invoicebill = BusinessDataServiceHelper.loadSingle("recon_invoicebill", alreadyInvoice_qf.toArray());//查询发票数据
if (recon_invoicebill != null){//当发票不为空时
DynamicObjectCollection writeoffinfoentry = recon_invoicebill.getDynamicObjectCollection("writeoffinfoentry");//获取核销信息分录
for (int i = 0; i < writeoffinfoentry.size();i++) {
DynamicObject dynamicObject = writeoffinfoentry.get(i);
Object payBillPk = dynamicObject.getDynamicObject("wentry_paybill").getPkValue();//核销付款单id
if (pk.equals(payBillPk)){
BigDecimal wentry_writroffamount = dynamicObject.getBigDecimal("wentry_writroffamount");//获取分录核销金额
BigDecimal haswriteoffamount = recon_invoicebill.getBigDecimal("haswriteoffamount");//目前已核销金额
haswriteoffamount = haswriteoffamount.subtract(wentry_writroffamount);//还原已核销金额
recon_invoicebill.set("haswriteoffamount",haswriteoffamount);//赋值已核销金额
BigDecimal writeoffamount = recon_invoicebill.getBigDecimal("writeoffamount");//目前未核销金额
writeoffamount = writeoffamount.add(wentry_writroffamount);//还原未核销金额
recon_invoicebill.set("writeoffamount",writeoffamount);//赋值未核销金额
SaveServiceHelper.save(new DynamicObject[]{recon_invoicebill});//
break;
}
}
}
}
}
return true;
}else {
this.getView().showTipNotification("发票未核销金额异常,请检查后重新补录发票");
return false;
}
}
} }
// CodeRuleInfo codeRule = CodeRuleServiceHelper.getCodeRule("recon_invoicebill", recon_invoicebill, null); // CodeRuleInfo codeRule = CodeRuleServiceHelper.getCodeRule("recon_invoicebill", recon_invoicebill, null);

View File

@ -6,10 +6,12 @@ import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.ListSelectedRow; import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection; import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.entity.operate.Save; import kd.bos.entity.operate.Save;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter; import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType; import kd.bos.form.ShowType;
import kd.bos.form.control.Toolbar; import kd.bos.form.control.Toolbar;
import kd.bos.form.events.AfterDoOperationEventArgs; import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.list.plugin.AbstractListPlugin; import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QCP; import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter; import kd.bos.orm.query.QFilter;
@ -26,6 +28,7 @@ import java.util.*;
*/ */
public class PeconPayreqBillListPlugin extends AbstractListPlugin implements Plugin { public class PeconPayreqBillListPlugin extends AbstractListPlugin implements Plugin {
String billname;
@Override @Override
public void afterDoOperation(AfterDoOperationEventArgs afterDoOperationEventArgs) { public void afterDoOperation(AfterDoOperationEventArgs afterDoOperationEventArgs) {
@ -37,13 +40,14 @@ public class PeconPayreqBillListPlugin extends AbstractListPlugin implements Plu
Object pk = row.getPrimaryKeyValue(); Object pk = row.getPrimaryKeyValue();
if (checkAddInvoice(pk)){ if (checkAddInvoice(pk)){
BillShowParameter showParameter = new BillShowParameter(); BillShowParameter showParameter = new BillShowParameter();
showParameter.setFormId("recon_payreqbill"); showParameter.setFormId(billname);
showParameter.setPkId(pk);//设置单据主键 showParameter.setPkId(pk);//设置单据主键
showParameter.setCustomParam("isticket","true"); showParameter.setCustomParam("isticket","true");
showParameter.getOpenStyle().setShowType(ShowType.Modal);//打开方式 showParameter.getOpenStyle().setShowType(ShowType.Modal);//打开方式
showParameter.setCloseCallBack(new CloseCallBack(this,"ticket"));
this.getView().showForm(showParameter); this.getView().showForm(showParameter);
}else { }else {
this.getView().showTipNotification("付款申请单据不符合补录条件"); this.getView().showTipNotification("单据不符合补录条件");
} }
} }
@ -51,7 +55,8 @@ public class PeconPayreqBillListPlugin extends AbstractListPlugin implements Plu
ListSelectedRowCollection rows = this.getSelectedRows(); ListSelectedRowCollection rows = this.getSelectedRows();
for (ListSelectedRow row : rows) { for (ListSelectedRow row : rows) {
Object pk = row.getPrimaryKeyValue(); Object pk = row.getPrimaryKeyValue();
DynamicObject payreqbill = BusinessDataServiceHelper.loadSingle(pk, "recon_payreqbill");//付款申请完整数据 if (checkAddInvoice(pk)){
DynamicObject payreqbill = BusinessDataServiceHelper.loadSingle(pk, billname);//付款申请完整数据
if (payreqbill.getBoolean("qeug_isticket")){//确定付款是否需要补票 if (payreqbill.getBoolean("qeug_isticket")){//确定付款是否需要补票
BigDecimal applyamt = payreqbill.getBigDecimal("applyamt");//本期申请金额 BigDecimal applyamt = payreqbill.getBigDecimal("applyamt");//本期申请金额
BigDecimal totalAmount = BigDecimal.ZERO; BigDecimal totalAmount = BigDecimal.ZERO;
@ -161,6 +166,17 @@ public class PeconPayreqBillListPlugin extends AbstractListPlugin implements Plu
} }
} }
} }
}
}
}
@Override
public void closedCallBack(ClosedCallBackEvent closedCallBackEvent) {
super.closedCallBack(closedCallBackEvent);
String actionId = closedCallBackEvent.getActionId();
if ("ticket".equals(actionId)) {
this.getView().invokeOperation("refresh");
} }
} }
@ -170,8 +186,18 @@ public class PeconPayreqBillListPlugin extends AbstractListPlugin implements Plu
* @return * @return
*/ */
public boolean checkAddInvoice(Object pk){ public boolean checkAddInvoice(Object pk){
DynamicObject payreqbill = BusinessDataServiceHelper.loadSingle(pk, "recon_payreqbill");//付款申请完整数据 DynamicObject bill;
return payreqbill.getBoolean("qeug_isticket"); QFilter qFilter = new QFilter("id", QCP.equals, pk);
DynamicObject payreqbill = BusinessDataServiceHelper.loadSingle("recon_payreqbill",qFilter.toArray());//付款申请完整数据
if (payreqbill != null){
billname = "recon_payreqbill";
bill = payreqbill;
}else {
DynamicObject connotextbill = BusinessDataServiceHelper.loadSingle("recon_connotextbill",qFilter.toArray());//付款申请完整数据
billname = "recon_connotextbill";
bill = connotextbill;
}
return bill.getBoolean("qeug_isticket");
} }
/** /**