发票名识别校验
This commit is contained in:
		
							parent
							
								
									c467e552df
								
							
						
					
					
						commit
						96151fbc55
					
				|  | @ -0,0 +1,188 @@ | |||
| package zcgj.zcdev.zcdev.pr.plugin.form; | ||||
| 
 | ||||
| import kd.bos.coderule.api.CodeRuleInfo; | ||||
| 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.dataentity.utils.StringUtils; | ||||
| import kd.bos.entity.MainEntityType; | ||||
| import kd.bos.exception.KDBizException; | ||||
| 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.coderule.CodeRuleServiceHelper; | ||||
| import kd.ec.basedata.common.enums.BillStatusEnum; | ||||
| import kd.ec.basedata.common.enums.DefaultEnum; | ||||
| import kd.ec.basedata.common.invoicecloud.InvoiceDataHandleHelper; | ||||
| import kd.ec.basedata.common.invoicecloud.bean.InvoiceItemVO; | ||||
| import kd.ec.basedata.common.invoicecloud.bean.InvoiceVO; | ||||
| import kd.ec.basedata.common.invoicecloud.enumeration.InvoiceTypeEnum; | ||||
| import kd.ec.basedata.common.utils.TextHelper; | ||||
| 
 | ||||
| import java.math.BigDecimal; | ||||
| import java.text.ParseException; | ||||
| import java.text.SimpleDateFormat; | ||||
| import java.util.Date; | ||||
| import java.util.List; | ||||
| 
 | ||||
| public class InvoiceNameRecognitionBillPlugin  extends InvoiceDataHandleHelper { | ||||
|     private static final Log log = LogFactory.getLog(InvoiceNameRecognitionBillPlugin.class); | ||||
|     public static DynamicObject generateInvoiceByVO(InvoiceVO invoiceVO, MainEntityType dt, long id, long userID, long orgID, Date date, DynamicObject currency) { | ||||
|         DynamicObject invoice = new DynamicObject(dt); | ||||
|         invoice.set("id", id); | ||||
|         invoice.set("serialno", invoiceVO.getSerialNo()); | ||||
|         InvoiceTypeEnum invoiceTypeEnum = InvoiceTypeEnum.getEnumByValue(invoiceVO.getInvoiceType()); //根据值 返回对应的发票类型 | ||||
|         if (invoiceTypeEnum != null) { | ||||
|             QFilter invoiceTypeFilter = new QFilter("name", "=", invoiceTypeEnum.getName()); | ||||
|             DynamicObject invoiceType = BusinessDataServiceHelper.loadSingle("ec_invoice_type", "id", new QFilter[]{invoiceTypeFilter});//搜索指定的发票类型 | ||||
|             invoice.set("invoicetypeid", invoiceType);//设置发票类型 | ||||
|         } | ||||
| 
 | ||||
|         invoice.set("invoicecode", invoiceVO.getInvoiceCode());//设置发票代码 | ||||
|         invoice.set("invoiceno", invoiceVO.getInvoiceNo());//发票号码 | ||||
|         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); | ||||
| 
 | ||||
|         try { | ||||
|             Date invoiceDate = format.parse(invoiceVO.getInvoiceDate()); | ||||
|             invoice.set("invoicedate", invoiceDate);//设置发票日期 | ||||
|         } catch (ParseException var32) { | ||||
|             log.info("parse invoice date error."); | ||||
|         } | ||||
|         //设置发票的货币、含税总金额、未核销金额、总税额、未核销税额和总金额。 | ||||
|         invoice.set("currency", currency); | ||||
|         invoice.set("totaloftaxamount", invoiceVO.getTotalAmount()); | ||||
|         invoice.set("unapplyamount", invoiceVO.getTotalAmount()); | ||||
|         invoice.set("totaltax", invoiceVO.getTaxAmount()); | ||||
|         invoice.set("unapplyinvtax", invoiceVO.getTaxAmount()); | ||||
|         invoice.set("totalamount", invoiceVO.getAmount()); | ||||
|         //设置购买方的税号、银行名称、银行账号、电话号码和地址。 | ||||
|         invoice.set("buyertaxno", invoiceVO.getBuyerTaxNo()); | ||||
|         invoice.set("buyerbank", TextHelper.getBankName(invoiceVO.getBuyerAccount())); | ||||
|         invoice.set("buyeraccount", TextHelper.getBankNum(invoiceVO.getBuyerAccount())); | ||||
|         invoice.set("buyerphone", TextHelper.getPhoneNum(invoiceVO.getBuyerAddressPhone())); | ||||
|         invoice.set("buyeraddress", TextHelper.getAddress(invoiceVO.getBuyerAddressPhone())); | ||||
|         if (StringUtils.equals(dt.getName(), "ec_in_invoice")) { | ||||
|             invoice.set("isreceived", "1"); | ||||
|         } | ||||
| 
 | ||||
|         invoice.set("source", "03"); | ||||
|         invoice.set("buyername", invoiceVO.getBuyerName());//设置购买方 | ||||
|         //原来是name 改为ffirmname | ||||
|         QFilter buyerFilter = new QFilter("ffirmname", "=", invoiceVO.getBuyerName()); | ||||
|         //如果是进项发票,在业务单元里搜索 指定买方信息 否则在客户里搜索 | ||||
|         DynamicObject buyer = StringUtils.equals(dt.getName(), "ec_in_invoice") ? BusinessDataServiceHelper.loadSingle("bos_org", "", new QFilter[]{buyerFilter}) : BusinessDataServiceHelper.loadSingle("bd_customer", "", new QFilter[]{new QFilter("name", "=", invoiceVO.getBuyerName())}); | ||||
|         if (buyer == null) { | ||||
|             log.info("buyer: "+invoiceVO.getBuyerName()+"is null"); | ||||
|             throw new KDBizException(String.format(ResManager.loadKDString("发票【%1$s】:购买方【%2$s】不存在。", "InvoiceDataHandleHelper_5", "ec-ecbd-common", new Object[0]), invoiceVO.getInvoiceCode(), invoiceVO.getBuyerName())); | ||||
|         } else { | ||||
|             invoice.set("buyer", buyer); | ||||
|             invoice.set("sellertaxno", invoiceVO.getSalerTaxNo()); | ||||
|             invoice.set("sellerbank", TextHelper.getBankName(invoiceVO.getSalerAccount())); | ||||
|             invoice.set("selleraccount", TextHelper.getBankNum(invoiceVO.getSalerAccount())); | ||||
|             invoice.set("sellerphone", TextHelper.getPhoneNum(invoiceVO.getSalerAddressPhone())); | ||||
|             invoice.set("selleraddress", TextHelper.getAddress(invoiceVO.getSalerAddressPhone())); | ||||
|             String sellerName = invoiceVO.getSalerName(); | ||||
|             invoice.set("sellername", sellerName); | ||||
|             /** | ||||
|              * 设置销售方的税号、银行名称、银行账号、电话号码和地址。 | ||||
|              * 根据销售方名称加载销售方信息(可能是供应商或组织),如果销售方不存在,抛出异常。 | ||||
|              */ | ||||
|             QFilter sellerFilter = new QFilter("name", "=", sellerName); | ||||
|             if (!sellerName.contains("(") && !sellerName.contains(")")) { | ||||
|                 if (sellerName.contains("(") || sellerName.contains(")")) { | ||||
|                     sellerName.replace('(', '('); | ||||
|                     sellerName.replace(')', ')'); | ||||
|                     sellerFilter.or("name", "=", sellerName); | ||||
|                 } | ||||
|             } else { | ||||
|                 sellerName.replace('(', '('); | ||||
|                 sellerName.replace(')', ')'); | ||||
|                 sellerFilter.or("name", "=", sellerName); | ||||
|             } | ||||
| 
 | ||||
|             DynamicObject seller = StringUtils.equals(dt.getName(), "ec_in_invoice") ? BusinessDataServiceHelper.loadSingle("bd_supplier", "", new QFilter[]{sellerFilter}) : BusinessDataServiceHelper.loadSingle("bos_org", "", new QFilter[]{sellerFilter}); | ||||
|             if (seller == null) { | ||||
|                 throw new KDBizException(String.format(ResManager.loadKDString("发票【%1$s】:销售方【%2$s】不存在。", "InvoiceDataHandleHelper_6", "ec-ecbd-common", new Object[0]), invoiceVO.getInvoiceCode(), sellerName)); | ||||
|             } else { | ||||
|                 invoice.set("seller", seller); | ||||
|                 invoice.set("invoicestatus", invoiceVO.getInvoiceStatus()); | ||||
|                 invoice.set("billstatus", BillStatusEnum.AUDIT.getValue()); | ||||
|                 invoice.set("org_id", orgID); | ||||
|                 //处理发票明细 | ||||
|                 List<InvoiceItemVO> detailEntries = invoiceVO.getItems(); | ||||
|                 if (detailEntries != null && !detailEntries.isEmpty()) { | ||||
|                     DynamicObjectCollection invoiceEntries = invoice.getDynamicObjectCollection("entryentity"); | ||||
|                     DynamicObjectType entryDt = (new DynamicObject(dt)).getDynamicObjectCollection("entryentity").getDynamicObjectType(); | ||||
| 
 | ||||
|                     for(InvoiceItemVO item : detailEntries) { | ||||
|                         DynamicObject entry = new DynamicObject(entryDt); | ||||
|                         entry.set("name", item.getGoodsName()); | ||||
|                         entry.set("model", item.getSpecModel()); | ||||
|                         String unitName = item.getUnit(); | ||||
|                         if (unitName != null && !unitName.isEmpty()) { | ||||
|                             QFilter unitFilter = (new QFilter("name", "=", unitName)).or("number", "=", unitName).or("number", "=", unitName.toLowerCase()).or("number", "=", unitName.toUpperCase()); | ||||
|                             QFilter validFilter = (new QFilter("enable", "=", DefaultEnum.YES.getValue().charAt(0))).and("status", "=", BillStatusEnum.AUDIT.getValue().charAt(0)); | ||||
|                             DynamicObject unit = BusinessDataServiceHelper.loadSingle("bd_measureunits", "name", new QFilter[]{unitFilter, validFilter}); | ||||
|                             if (unit != null) { | ||||
|                                 entry.set("unit", unit); | ||||
|                             } | ||||
|                         } | ||||
| 
 | ||||
|                         entry.set("qty", item.getNum()); | ||||
|                         BigDecimal price = item.getUnitPrice(); | ||||
|                         entry.set("price", item.getUnitPrice()); | ||||
|                         entry.set("amount", item.getDetailAmount()); | ||||
|                         BigDecimal ofTaxPrice = price; | ||||
|                         BigDecimal tax = item.getTaxRate(); | ||||
|                         if (tax != null) { | ||||
|                             BigDecimal taxRateHundred = tax.multiply(BigDecimal.TEN.multiply(BigDecimal.TEN)); | ||||
|                             QFilter taxFilter = new QFilter("taxrate", "=", taxRateHundred); | ||||
|                             DynamicObject[] taxRate = BusinessDataServiceHelper.load("bd_taxrate", "name", new QFilter[]{taxFilter}); | ||||
|                             if (taxRate != null && taxRate.length > 0) { | ||||
|                                 entry.set("taxrate", taxRate[0]); | ||||
|                             } | ||||
| 
 | ||||
|                             if (price != null) { | ||||
|                                 ofTaxPrice = price.multiply(BigDecimal.ONE.add(tax)); | ||||
|                             } | ||||
|                         } | ||||
| 
 | ||||
|                         entry.set("oftaxprice", ofTaxPrice); | ||||
|                         entry.set("tax", item.getTaxAmount()); | ||||
|                         entry.set("oftaxamount", item.getDetailAmount().add(item.getTaxAmount() == null ? BigDecimal.ZERO : item.getTaxAmount())); | ||||
|                         invoiceEntries.add(entry); | ||||
|                     } | ||||
|                 } | ||||
| 
 | ||||
|                 String snapshotUrl = invoiceVO.getSnapshotUrl(); | ||||
|                 if (!StringUtils.isBlank(snapshotUrl)) { | ||||
|                     DynamicObjectCollection pictureEntries = invoice.getDynamicObjectCollection("pictureentry"); | ||||
|                     DynamicObjectType pictureEntryDt = (new DynamicObject(dt)).getDynamicObjectCollection("pictureentry").getDynamicObjectType(); | ||||
|                     DynamicObject item = new DynamicObject(pictureEntryDt); | ||||
|                     item.set("filename", invoiceVO.getInvoiceNo()); | ||||
|                     item.set("imageurl", snapshotUrl); | ||||
|                     item.set("uploaddate", date); | ||||
|                     pictureEntries.add(item); | ||||
|                 } | ||||
| 
 | ||||
|                 invoice.set("creator_id", userID); | ||||
|                 invoice.set("modifier_id", userID); | ||||
|                 invoice.set("auditor_id", userID); | ||||
|                 invoice.set("createtime", date); | ||||
|                 invoice.set("modifytime", date); | ||||
|                 invoice.set("auditdate", date); | ||||
|                 CodeRuleInfo codeRuleInfo = CodeRuleServiceHelper.getCodeRule(dt.getName(), invoice, String.valueOf(RequestContext.get().getOrgId())); | ||||
|                 if (null != codeRuleInfo) { | ||||
|                     invoice.set("billno", CodeRuleServiceHelper.getNumber(codeRuleInfo, invoice)); | ||||
|                 } else { | ||||
|                     invoice.set("billno", invoiceVO.getSerialNo()); | ||||
|                 } | ||||
| 
 | ||||
|                 return invoice; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Loading…
	
		Reference in New Issue