设备维修确认发票功能

This commit is contained in:
xiaoshi 2025-06-10 10:56:27 +08:00
parent 21cac3881c
commit 61c34e608e
1 changed files with 319 additions and 0 deletions

View File

@ -0,0 +1,319 @@
package zcgj.zcdev.zcdev.pr.plugin.form;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.metadata.dynamicobject.DynamicObjectType;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.CloseCallBack;
import kd.bos.form.ShowFormHelper;
import kd.bos.form.ShowType;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.operate.FormOperate;
import kd.bos.list.ListShowParameter;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.operation.DeleteServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.ec.basedata.common.invoicecloud.InvoiceDataHandleHelper;
import kd.ec.basedata.common.invoicecloud.bean.InvoiceVO;
import kd.ec.contract.utils.ImportInvoiceUtils;
import kd.fi.ap.piaozone.InvoiceCollectHelper;
import kd.sdk.plugin.Plugin;
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
public class MaintenanceAckBillPlugin extends AbstractBillPlugIn implements Plugin {
private static final Log log = LogFactory.getLog(MaintenanceAckBillPlugin.class);
@Override
public void beforeDoOperation(BeforeDoOperationEventArgs args) {
super.beforeDoOperation(args);
FormOperate operate = (FormOperate)args.getSource();
String key = operate.getOperateKey();
if (StringUtils.equals(key, "newsubentry")) { //选择发票
this.beforeNewSubEntry(args);
} else if (StringUtils.equals(key, "selectinvoice")) { //导入发票
this.showInvoiceImport(args);
}else if (key.equals("deletesubentry")) {
this.beforeDeleteSubEntry(args);
}
}
public void closedCallBack(ClosedCallBackEvent event) {
super.closedCallBack(event);//selectInvoice_2220454955754478592
String actionId = event.getActionId();
Object returnData = event.getReturnData();
if (actionId.toLowerCase().startsWith("zcgj_entryentity") && returnData != null) {
this.invoiceCloseCallBack(event);
} else if (actionId.toLowerCase().startsWith("selectinvoice") && returnData != null) {
this.importInvoiceCallBack(returnData);
}
}
//选择发票 开始
protected void beforeNewSubEntry(BeforeDoOperationEventArgs args) {
String billId = this.getModel().getDataEntity().getPkValue().toString();
if (!billId.equals("0") && "A".equals(this.getModel().getValue("billstatus"))) {
this.showInvoice("zcgj_entryentity");
} else {
this.getView().showMessage(ResManager.loadKDString("单据不是暂存状态,请先保存单据。", "PaymentApplyEditUI_1", "ec-contract-formplugin", new Object[0]));
args.setCancel(true);
}
}
private void showInvoice(String entryType) {
long currencyId = ((DynamicObject)this.getModel().getValue("zcgj_currency")).getLong("id");
long orgId = ((DynamicObject)this.getModel().getValue("zcgj_accountorg")).getLong("id");
QFilter filter = new QFilter("org", "=", orgId);
filter.and(new QFilter("isclaimed", "=", false));
filter.and(new QFilter("billstatus", "=", "C"));
filter.and(new QFilter("currency", "=", currencyId));
filter.and(new QFilter("invoicestatus", "!=", "2"));
filter.and(new QFilter("unapplyamount", ">", BigDecimal.ZERO));
QFilter filter1 = new QFilter("isclaimed", "=", true);
filter1.and(new QFilter("billstatus", "=", "C"));
// DynamicObject contract = (DynamicObject)this.getModel().getValue("zcgj_contract");
// if (contract != null) {
// long contractId = contract.getLong("id");
// filter1.and(new QFilter("contract", "=", contractId));
filter1.and(new QFilter("unapplyamount", ">", BigDecimal.ZERO));
DynamicObjectCollection contInvEntryCol = this.getModel().getEntryEntity("zcgj_entryentity");
if (contInvEntryCol.size() > 0) {
List<Long> selectedInvIds = new ArrayList(8);
for(DynamicObject contInvEntryObj : contInvEntryCol) {
DynamicObject invoice = contInvEntryObj.getDynamicObject("zcgj_invoice");
selectedInvIds.add(invoice.getLong("id"));
}
filter1.and(new QFilter("id", "not in", selectedInvIds));
}
ListShowParameter param = ShowFormHelper.createShowListForm("ec_in_invoice_f7", true);
param.getListFilterParameter().setFilter(filter.or(filter1));
param.setMultiSelect(true);
param.setCloseCallBack(new CloseCallBack(this, entryType));
param.getOpenStyle().setShowType(ShowType.Modal);
this.getView().showForm(param);
// }
}
protected void invoiceCloseCallBack(ClosedCallBackEvent event) {
ListSelectedRowCollection rows = (ListSelectedRowCollection)event.getReturnData();
DynamicObject[] invArr = new DynamicObject[rows.size()];
int index = 0;
// DynamicObject upContract = (DynamicObject)this.getModel().getValue("zcgj_contract");
for(ListSelectedRow row : rows) {
Object invoicePk = row.getPrimaryKeyValue().toString();
int rowIndex = this.getModel().createNewEntryRow("zcgj_entryentity");
this.getModel().setValue("zcgj_invoice", invoicePk.toString(), rowIndex);
DynamicObject invoice = BusinessDataServiceHelper.loadSingle(invoicePk, "ec_in_invoice", "unapplyinvtax,unapplyamount,isClaimed,contract,project,connecttype,currency,totalamount,totaltax,totaloftaxamount");
this.getModel().setValue("zcgj_invoicecurrency", invoice.getDynamicObject("currency") == null ? Long.valueOf("0") : invoice.getDynamicObject("currency").getPkValue(), rowIndex);
this.getModel().setValue("zcgj_invoiceamount", invoice.getBigDecimal("totalamount"), rowIndex);
this.getModel().setValue("zcgj_invoicetax", invoice.getBigDecimal("totaltax"), rowIndex);
this.getModel().setValue("zcgj_oftaxinvoiceamount", invoice.getBigDecimal("totaloftaxamount"), rowIndex);
this.getModel().setValue("zcgj_unapplyinvoftaxamt", invoice.getBigDecimal("unapplyamount"), rowIndex);
this.getModel().setValue("zcgj_unapplyinvtax", invoice.getBigDecimal("unapplyinvtax"), rowIndex);
this.getModel().setValue("zcgj_applyinvoftaxamt", invoice.getBigDecimal("unapplyamount"), rowIndex);
invArr[index++] = invoice;
invoice.set("isclaimed", true);
// invoice.set("contract", upContract);
invoice.set("project", (DynamicObject)this.getModel().getValue("zcgj_project"));
invoice.set("connecttype", "contract");
}
SaveServiceHelper.save(invArr);
this.getView().invokeOperation("invoicesave");
}
protected BigDecimal getSum(String entryId, String columnId) {
int rowCount = this.getModel().getEntryRowCount(entryId);
BigDecimal totalAmount = BigDecimal.ZERO;
for(int i = 0; i < rowCount; ++i) {
BigDecimal amount = (BigDecimal)this.getModel().getValue(columnId, i);
amount = amount == null ? BigDecimal.ZERO : amount;
totalAmount = totalAmount.add(amount);
}
return totalAmount;
}
//选择发票 结束
//导入发票 开始
protected void showInvoiceImport(BeforeDoOperationEventArgs args) {
String billId = this.getModel().getDataEntity().getPkValue().toString();
if (!billId.equals("0") && "A".equals(this.getModel().getValue("billstatus"))) {
if (this.getModel().getValue("zcgj_accountorg") == null) {
this.getView().showTipNotification(ResManager.loadKDString("发票企业名称不能为空", "PaymentApplyEditUI_18", "ec-contract-formplugin", new Object[0]));
args.setCancel(true);
} else {
if (!ImportInvoiceUtils.isXhInvoiceCloud()) {
args.setCancel(true);
ImportInvoiceUtils.showImportView(this);
}else{
args.setCancel(true);
DynamicObject org = (DynamicObject) this.getModel().getValue("zcgj_accountorg");
InvoiceCollectHelper.showSelectInvoice(this, org);
}
}
} else {
this.getView().showMessage(ResManager.loadKDString("单据不是暂存状态,请先保存单据。", "PaymentApplyEditUI_1", "ec-contract-formplugin", new Object[0]));
args.setCancel(true);
}
}
protected void importInvoiceCallBack(Object returnData) {
DynamicObject org = (DynamicObject)this.getModel().getValue("zcgj_accountorg");
//List<InvoiceVO> invoiceVOList = InvoiceDataHandleHelper.parseXhInvoiceCloudReturnData(returnData);
List<InvoiceVO> invoiceVOList = CustomInvoiceDataHandleHelper.parseXhInvoiceCloudReturnData(returnData);
Map<Boolean, Set<DynamicObject>> invoiceMap = InvoiceDataHandleHelper.processInvoiceVO(invoiceVOList, RequestContext.get().getCurrUserId(),
org.getLong("id"), new Date(), "ec_in_invoice", (DynamicObject)this.getModel().getValue("zcgj_currency"), true);
ImportInvoiceUtils invoiceUtils = new ImportInvoiceUtils(this.getView(), this.getPageCache());
this.addInvoiceToEntry(invoiceUtils, invoiceMap);
}
protected void addInvoiceToEntry(ImportInvoiceUtils invoiceUtils, Map<Boolean, Set<DynamicObject>> invoiceMap) {
if (invoiceMap.isEmpty()) {
this.getView().showTipNotification(ResManager.loadKDString("导入发票为空。", "PaymentApplyEditUI_19", "ec-contract-formplugin", new Object[0]));
} else {
long orgId = (Long)((DynamicObject)this.getModel().getValue("zcgj_accountorg")).getPkValue();
Set<DynamicObject> newInvoices = (Set)invoiceMap.get(Boolean.TRUE);
Set<DynamicObject> existInvoices = (Set)invoiceMap.get(Boolean.FALSE);
if (newInvoices != null && !newInvoices.isEmpty()) {
for(DynamicObject newInvoice : newInvoices) {
DynamicObject buyerOrg = newInvoice.getDynamicObject("buyer");
if (buyerOrg != null && buyerOrg.getLong("id") != orgId) {
this.getView().showErrorNotification(ResManager.loadKDString("导入失败:当前发票购买方不为当前财务记账组织,请确认信息。", "PaymentApplyEditUI_20", "ec-contract-formplugin", new Object[0]));
return;
}
}
this.saveInvoiceData(invoiceUtils, newInvoices, existInvoices);
} else if (existInvoices != null) {
DynamicObjectCollection contInvEntryCol = this.getModel().getEntryEntity("zcgj_entryentity");
Map<Object, Object> entryMap = (Map)contInvEntryCol.stream().collect(Collectors.toMap(this::getInvoicePK, this::getInvoicePK));
this.initSubEntryEntity(existInvoices, entryMap);
}
}
}
protected void saveInvoiceData(ImportInvoiceUtils invoiceUtils, Set<DynamicObject> newInvoices, Set<DynamicObject> existInvoices) {
invoiceUtils.saveInvoiceAndGiveTips(newInvoices, existInvoices);
if (newInvoices != null && existInvoices != null) {
newInvoices.addAll(existInvoices);
}
this.initSubEntryEntity(newInvoices, (Map)null);
}
protected void initSubEntryEntity(Set<DynamicObject> invoices, Map<Object, Object> entryMap) {
if (invoices != null) {
List<DynamicObject> invArr = new ArrayList(invoices.size());
for(DynamicObject invoice : invoices) {
if (entryMap != null && entryMap.get(invoice.getPkValue()) != null) {
this.getView().showTipNotification(String.format(ResManager.loadKDString("发票号码%s已存在分录行", "PaymentApplyEditUI_22", "ec-contract-formplugin", new Object[0]), invoice.getString("invoiceno")));
} else {
BigDecimal unApplyAmount = invoice.getBigDecimal("unapplyamount");
if (unApplyAmount != null && unApplyAmount.doubleValue() <= (double)0.0F) {
this.getView().showTipNotification(String.format(ResManager.loadKDString("发票号码%s金额已经被关联完毕不可重复使用", "PaymentApplyEditUI_23", "ec-contract-formplugin", new Object[0]), invoice.getString("invoiceno")));
} else {
// DynamicObject upContract = (DynamicObject)this.getModel().getValue("zcgj_contract");
int rowIndex = this.getModel().createNewEntryRow("zcgj_entryentity");
this.getModel().setValue("zcgj_invoice", invoice.getPkValue(), rowIndex);
this.getModel().setValue("zcgj_invoicecurrency", invoice.getDynamicObject("currency") == null ? Long.valueOf("0") : invoice.getDynamicObject("currency").getPkValue(), rowIndex);
this.getModel().setValue("zcgj_invoiceamount", invoice.getBigDecimal("totalamount"), rowIndex);
this.getModel().setValue("zcgj_invoicetax", invoice.getBigDecimal("totaltax"), rowIndex);
this.getModel().setValue("zcgj_oftaxinvoiceamount", invoice.getBigDecimal("totaloftaxamount"), rowIndex);
this.getModel().setValue("zcgj_unapplyinvoftaxamt", invoice.getBigDecimal("unapplyamount"), rowIndex);
this.getModel().setValue("zcgj_unapplyinvtax", invoice.getBigDecimal("unapplyinvtax"), rowIndex);
this.getModel().setValue("zcgj_applyinvoftaxamt", invoice.getBigDecimal("unapplyamount"), rowIndex);
invoice.set("isclaimed", true);
// invoice.set("contract", upContract);
invoice.set("project", (DynamicObject)this.getModel().getValue("zcgj_project"));
invoice.set("connecttype", "contract");
invArr.add(invoice);
}
}
}
if (invArr.size() > 0) {
SaveServiceHelper.save((DynamicObject[])invArr.toArray(new DynamicObject[invoices.size()]));
}
this.getView().invokeOperation("invoicesave");
}
}
protected Object getInvoicePK(DynamicObject object) {
return object.getDynamicObject("invoice").getPkValue();
}
//导入发票 结束
//删除发票 开始
protected void beforeDeleteSubEntry(BeforeDoOperationEventArgs args) {
EntryGrid subGrid = (EntryGrid)this.getControl("zcgj_entryentity");
int[] selRows = subGrid.getEntryState().getSelectedRows();
if (selRows.length == 0) {
this.getView().showMessage(ResManager.loadKDString("请选择发票。", "PaymentApplyEditUI_3", "ec-contract-formplugin", new Object[0]));
args.setCancel(true);
} else {
Object[] delPks = new Object[selRows.length];
Set<Object> updateInvoicePks = new HashSet(selRows.length);
DynamicObjectCollection subEntryEntityCol = this.getModel().getEntryEntity("zcgj_entryentity");
for(int i = selRows.length - 1; i >= 0; --i) {
int rowIndex = selRows[i];
DynamicObject invoice = (DynamicObject)this.getModel().getValue("zcgj_invoice", rowIndex);
updateInvoicePks.add(invoice.getPkValue());
delPks[i] = ((DynamicObject)subEntryEntityCol.get(selRows[i])).getPkValue();
this.getModel().deleteEntryRow("zcgj_entryentity", rowIndex);
}
// this.setApplyAmtByInvoice(this.getModel().getEntryCurrentRowIndex("entryentity"));
// DynamicObjectType entryentityDT = DynamicObjectTypeUtils.getEntryDynamicObjectType("ec_paymentapply", "entryentity");
DynamicObjectType subDT = this.getModel().getDataEntity().getDynamicObjectCollection("zcgj_entryentity").getDynamicObjectType();
// DynamicObjectType subDT = this.getModel().getDynamicObjectCollection("zcgj_entryentity").getDynamicObjectType();
QFilter filter = new QFilter("zcgj_entryentity.zcgj_invoice", "in", updateInvoicePks);
filter.and("id", "!=", this.getModel().getDataEntity().getPkValue());
DynamicObjectCollection invoiceApplyEntries = QueryServiceHelper.query("zcgj_ec_out_finaceconfirm", "zcgj_entryentity.zcgj_invoice", new QFilter[]{filter});
if (invoiceApplyEntries != null && !invoiceApplyEntries.isEmpty()) {
for(DynamicObject subEntry : invoiceApplyEntries) {
updateInvoicePks.remove(subEntry.get("zcgj_entryentity.zcgj_invoice"));
}
}
if (!updateInvoicePks.isEmpty()) {
DynamicObject[] invoices = BusinessDataServiceHelper.load("ec_in_invoice", "isinvoiceclaim,isclaimed,contract,project,connecttype",
new QFilter[]{new QFilter("id", "in", updateInvoicePks)});
for(DynamicObject invoice : invoices) {
if (!invoice.getBoolean("isinvoiceclaim")) {
invoice.set("isclaimed", false);
invoice.set("contract", (Object)null);
invoice.set("project", (Object)null);
invoice.set("connecttype", "null");
}
}
SaveServiceHelper.save(invoices);
}
DeleteServiceHelper.delete(subDT, delPks);
this.getView().showSuccessNotification("发票删除成功!");
//this.setEnableByInvoice();
}
}
//删除发票 结束
}