发票识别验真

This commit is contained in:
zengweihai 2024-09-30 14:40:27 +08:00
parent 7792be805b
commit a3b8cce54d
1 changed files with 358 additions and 79 deletions

View File

@ -1,13 +1,22 @@
package shkd.repc.recon.formplugin;
import cfca.svs.api.util.IOUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.grapecity.documents.excel.B;
import com.grapecity.documents.excel.D;
import com.huawei.shade.org.apache.http.protocol.RequestContent;
import com.tongtech.jms.ra.util.Str;
import kd.bos.bill.BillShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.cache.CacheFactory;
import kd.bos.cache.TempFileCache;
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.fileservice.FileItem;
import kd.bos.fileservice.FileService;
import kd.bos.fileservice.FileServiceFactory;
import kd.bos.form.CloseCallBack;
import kd.bos.form.ShowType;
@ -20,6 +29,9 @@ import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.imageplatform.axis.IScanWebServiceImplServiceStub;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.AttachmentDto;
@ -28,13 +40,25 @@ import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.coderule.CodeRuleServiceHelper;
import kd.bos.servicehelper.operation.DeleteServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.url.UrlService;
import kd.bos.util.FileNameUtils;
import kd.bos.util.HttpClientUtils;
import kd.bos.util.StringUtils;
import org.agrona.Strings;
import org.apache.commons.io.IOUtils;
import org.apache.kafka.common.Uuid;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import shkd.utils.OAUtils;
import javax.xml.bind.DatatypeConverter;
import java.io.*;
import java.math.BigDecimal;
import java.net.URL;
import java.util.*;
/**
@ -44,11 +68,11 @@ import java.util.*;
*/
public class ReconPayreqFormPlugin extends AbstractFormPlugin {
private static final Log logger = LogFactory.getLog(OAUtils.class);
List<String> fileExtendList = new ArrayList<String>(Arrays.asList("png","jpg","jpeg","ofd","pdf","xml"));//发票文件后缀集合
@Override
public void afterDoOperation(AfterDoOperationEventArgs args) {
super.afterDoOperation(args);
DynamicObject dataEntity = this.getView().getModel().getDataEntity();
String billtype = dataEntity.getDynamicObjectType().getName();
String operateKey = args.getOperateKey();
@ -65,31 +89,246 @@ public class ReconPayreqFormPlugin extends AbstractFormPlugin {
//发票信息分录
DynamicObjectCollection invoiceentry = this.getView().getModel().getEntryEntity("invoiceentry");
if (invoiceentry.size() > 0) {
this.getView().showTipNotification("请先删除发票信息数据后,再进行发票识别");
return;
}
// TODO: 调取OA发票识别接口
boolean b = true;
//成功--新增分录行
if (b) {
//发票信息分录
for (int i = 0; i < attachments.size(); i++) {
invoiceentry.addNew();
//todo:模仿标品新增字段,为接口返回数据反写分录做准备
this.getModel().setValue("qeug_invoicecode", "FKHM-0904-0001",i);//发票代码
this.getModel().setValue("qeug_invoiceno", "FKHM-0904-0001",i);//发票号码
this.getModel().setValue("qeug_amount", 10000,i);//价税合计
this.getModel().setValue("qeug_attainformation", "发票识别:"+attachments.get(i).get("name"),i);
this.getModel().setValue("qeug_invoicestatus", 0,i);//发票验真/重状态
for (DynamicObject invoice : invoiceentry) {
String qeug_invoiceno = invoice.getString("qeug_invoiceno");//分录中的发票号码
QFilter q = new QFilter("invoiceno", QCP.equals, qeug_invoiceno);
DynamicObject recon_invoicebill = BusinessDataServiceHelper.loadSingle("recon_invoicebill", new QFilter[]{q});
if (null != recon_invoicebill) {
int recon_invoicebill1 = DeleteServiceHelper.delete("recon_invoicebill", new QFilter[]{q});
//失败
if (1 != recon_invoicebill1) {
this.getView().showTipNotification("删除失败:请检查发票登记单的单据状态");
}
}
}
this.getView().updateView("invoiceentry");
this.getView().showSuccessNotification("发票识别成功");
SaveServiceHelper.save(new DynamicObject[]{this.getView().getModel().getDataEntity()});
}else {
//失败--提示失败信息
this.getView().showTipNotification("发票识别失败:请上传有效发票");
invoiceentry.clear();
}
// ToDo: 获取token
String oaToken = getOaToken();
if(!Strings.isEmpty(oaToken)){//判断是否获取到token
for (int i = 0; i < attachments.size(); i++) {
Map<String, Object> attachment = attachments.get(i);
String type = attachment.get("type").toString();
String name = attachment.get("name").toString();
if (fileExtendList.contains(type)){//规范附件类型超出范围外不调用接口
// TODO: 根据发票路径转换出图像数据
String fileBase64 = getFileBase64StrByUrl(attachment);//转换为Base64数据
// TODO: 调取OA发票识别接口
JSONObject ocr_jsonObject = parseOCRData("91310106054599952W", type, fileBase64, oaToken);
String ocr_success = ocr_jsonObject.getString("success");
if ("true".equals(ocr_success)) {
//TODO: 发票识别成功调用OA发票识别接口
JSONArray ocrDatas = ocr_jsonObject.getJSONArray("data");
String fileId = ocr_jsonObject.getString("fileId");
JSONObject saveCheckJson = saveCheck("91310106054599952W", fileId, ocrDatas,oaToken);//返回数据
String save_success = saveCheckJson.getString("success");
if ("true".equals(save_success) || "false".equals(save_success)){
//TODO: 生成发票登记
JSONArray dataList = ocr_jsonObject.getJSONArray("data");//data数据类型为集合表示
JSONObject data = dataList.getJSONObject(0);//data数据
String invoiceType = data.getString("type"); //票据类型
JSONObject details = data.getJSONObject("details");//票据数据内容
String description = data.getString("description");//发票名称
String qeug_invoicecode = "";//发票代码初始定义
String qeug_invoiceno = "";//发票号码
BigDecimal qeug_notaxamt = BigDecimal.ZERO;//不含税金额
BigDecimal qeug_tax = BigDecimal.ZERO;//税额
BigDecimal qeug_amount = BigDecimal.ZERO;//价税合计
Date qeug_bizdate = null;//开票日期
String qeug_saleorg = "";//开票单位,只有文字该字段不用从oa携带和还原
JSONArray items = null;
boolean parseFlag = true;
if ("10100".equals(invoiceType) || "10101".equals(invoiceType) || "10102".equals(invoiceType) ){
//当票据类型为10100增值税专用发票10101增值税普通发票10102增值税电子普通发票
qeug_invoicecode = details.getString("code");//印刷发票代码
qeug_invoiceno = details.getString("number");//印刷发票号码
qeug_notaxamt = details.getBigDecimal("pretax_amount");//不含税金额
qeug_tax = details.getBigDecimal("tax");//税额
qeug_amount = details.getBigDecimal("total");////价税合计
qeug_bizdate = details.getDate("date");//开票日期
qeug_saleorg = details.getString("seller");//开票单位
items = details.getJSONArray("items");//增值税发票购买项明细
}else if ("10103".equals(invoiceType)){
//当票据类型为10100增值税普通发票(卷票)
qeug_invoicecode = details.getString("code");//印刷发票代码
qeug_invoiceno = details.getString("number");//印刷发票号码
qeug_amount = details.getBigDecimal("total");////价税合计
qeug_bizdate = details.getDate("date");//开票日期
qeug_saleorg = details.getString("seller");//开票单位
items = details.getJSONArray("items");//增值税发票卷票购买项
}else if ("10108".equals(invoiceType) || "10107".equals(invoiceType)){
//当票据类型为10107电子发票(增值税专用发票)10108电子发票(普通发票)
qeug_invoiceno = details.getString("number");//印刷发票号码
qeug_notaxamt = details.getBigDecimal("pretax_amount");//不含税金额
qeug_tax = details.getBigDecimal("tax");//税额
qeug_amount = details.getBigDecimal("total");////价税合计
qeug_bizdate = details.getDate("date");//开票日期
qeug_saleorg = details.getString("seller");//开票单位
items = details.getJSONArray("items");//电子发票购买项
} else if ("10200".equals(invoiceType)){
//当票据类型为10200定额发票
qeug_invoicecode = details.getString("code");//印刷发票代码
qeug_invoiceno = details.getString("number");//印刷发票号码
qeug_amount = details.getBigDecimal("total");////价税合计
}else if ("10400".equals(invoiceType)){
//当票据类型为10400定额发票
qeug_invoicecode = details.getString("code");//印刷发票代码
qeug_invoiceno = details.getString("number");//印刷发票号码
qeug_notaxamt = details.getBigDecimal("pretax_amount");//不含税金额
qeug_amount = details.getBigDecimal("total");////价税合计
qeug_bizdate = details.getDate("date");//开票日期
qeug_saleorg = details.getString("seller");//开票单位
items = details.getJSONArray("items");//机打发票购买项明细
}else if ("10500".equals(invoiceType)){
//当票据类型为10500出租车发票
qeug_invoicecode = details.getString("code");//发票代码
qeug_invoiceno = details.getString("number");//发票号码
qeug_amount = details.getBigDecimal("total");////总金额
qeug_bizdate = details.getDate("date");//开票日期
}else if ("10503".equals(invoiceType)){
//当票据类型为10503火车票
qeug_invoiceno = details.getString("number");//火车票号码
qeug_amount = details.getBigDecimal("total");////总金额
qeug_bizdate = details.getDate("date");//开票日期
}else if ("10505".equals(invoiceType)){
//当票据类型为10505客运汽车
qeug_invoicecode = details.getString("code");//发票代码
qeug_invoiceno = details.getString("number");//发票号码
qeug_amount = details.getBigDecimal("total");////总金额
qeug_bizdate = details.getDate("date");//开票日期
}else if ("10506".equals(invoiceType)){
//当票据类型为10506航空运输电子客票行程单
qeug_invoiceno = details.getString("number");//发票号码
qeug_tax = details.getBigDecimal("tax");//税额
qeug_amount = details.getBigDecimal("total");////总额
qeug_bizdate = details.getDate("date");//填开日期
qeug_saleorg = details.getString("issue_by");//填开单位
}else if ("10507".equals(invoiceType)){
//当票据类型为10507过路费发票
qeug_invoicecode = details.getString("code");//发票代码
qeug_invoiceno = details.getString("number");//发票号码
qeug_amount = details.getBigDecimal("total");//总金额
qeug_bizdate = details.getDate("date");//填开日期
}else{
parseFlag = false;
this.getView().showTipNotification(name +":发票识别失败:请上传有效发票");
}
boolean saveResultFlag = false;
JSONObject save_data = saveCheckJson.getJSONObject("data");
JSONArray billDatas = save_data.getJSONArray("billDatas");
JSONObject billData = billDatas.getJSONObject(0);
String invoiceState = billData.getString("invoiceState");
if ("0".equals(invoiceState)||"3".equals(invoiceState)){
saveResultFlag = true;
}else {
this.getView().showTipNotification(name +":发票保存校验失败:发票状态异常");
return;
}
//todo若为有效发票则生成发票等级并构建发票信息分录数据
if (parseFlag && saveResultFlag){
DynamicObject recon_invoicebill = BusinessDataServiceHelper.newDynamicObject("recon_invoicebill");
// DynamicObject recon_invoicebill1 = BusinessDataServiceHelper.loadSingle("2031274535679975424", "recon_invoicebill");
recon_invoicebill.set("billstatus","A");//单据状态
//所属组织
DynamicObject org = BusinessDataServiceHelper.loadSingle("bos_org", new QFilter[]{new QFilter("number", QCP.equals, "DOBE")});
if (null != org) {
recon_invoicebill.set("org",org);
}
DynamicObject project = dataEntity.getDynamicObject("project");
recon_invoicebill.set("project",project);//设置项目名称
recon_invoicebill.set("collectionway","NORMAL");//设置征收方式为默认普通征税
if ("0".equals(invoiceState)){
recon_invoicebill.set("invoicetype","posinvoice"); //设置为正数发票
}else {
recon_invoicebill.set("invoicetype","roffinvoice"); //设置为红冲发票
}
QFilter invoicetype_qf = new QFilter("number",QCP.equals,type);
DynamicObject bd_invoicetype = BusinessDataServiceHelper.loadSingle("bd_invoicetype", invoicetype_qf.toArray());
recon_invoicebill.set("invoice",bd_invoicetype);//设置发票类型
//发票号码 invoiceno
// recon_invoicebill.set("invoiceno",System.currentTimeMillis());
recon_invoicebill.set("invoicecode",qeug_invoicecode);//发票代码
recon_invoicebill.set("invoiceno",qeug_invoiceno);//分录中的发票号码
recon_invoicebill.set("notaxamt",qeug_notaxamt);//不含税金额
recon_invoicebill.set("tax",qeug_tax);//税额
recon_invoicebill.set("amount",qeug_amount);//价税合计
recon_invoicebill.set("bizdate",qeug_bizdate);//开票日期
// todo----编码规则待维护
//发票编码
recon_invoicebill.set("billno","测试数据勿删!!");//----编码规则待维护
//付款申请单 payreqentrys
DynamicObjectCollection payreqentrys = recon_invoicebill.getDynamicObjectCollection("payreqentrys");
payreqentrys.clear();
DynamicObject payreqentry = payreqentrys.addNew();
payreqentry.set("fbasedataid", this.getView().getModel().getDataEntity());
//todo:若接口返回汇总
//发票_明细
DynamicObjectCollection invoiceentrs = recon_invoicebill.getDynamicObjectCollection("invoiceentry");
invoiceentrs.clear();
if (items != null){
for (int x = 0; x < items.size(); x++ ){
JSONObject item = items.getJSONObject(x);
if (item != null){
DynamicObject invoiceentr = invoiceentrs.addNew();
invoiceentr.set("entry_content",item.getString("name"));//项目名称
invoiceentr.set("entry_qty",item.getString("quantity"));//数量
invoiceentr.set("entry_price",item.getString("price"));//单价
invoiceentr.set("entry_notaxamt",item.getString("total"));//金额
if (item.containsKey("tax_rate")){
String tax_rate = item.getString("tax_rate");
String substring = tax_rate.substring(0, tax_rate.length() - 1);
int value = Integer.parseInt(substring);
QFilter bd_taxrate_qf = new QFilter("taxrate",QCP.equals,value);
DynamicObject bd_taxrate = BusinessDataServiceHelper.loadSingle("bd_taxrate", bd_taxrate_qf.toArray());
invoiceentr.set("entry_taxrate",bd_taxrate);//税率
}
if (item.containsKey("tax")){
invoiceentr.set("entry_tax",item.getString("tax"));//税额
}
}
}
}else {
DynamicObject invoiceentr = invoiceentrs.addNew();
invoiceentr.set("entry_content",description);//项目名称
invoiceentr.set("entry_notaxamt",qeug_amount);
}
//todo:若接口返回明细--遍历新增发票明细分录
Object[] save = SaveServiceHelper.save(new DynamicObject[]{recon_invoicebill});
//todo:添加发票信息分录
DynamicObject dynamicObject = invoiceentry.addNew();//新增分录行
//todo:模仿标品新增字段,为接口返回数据反写分录做准备
DynamicObject dynamicObject1 = BusinessDataServiceHelper.loadSingle(recon_invoicebill.getLong("id"), "recon_invoicebill_f7");
dynamicObject.set("qeug_invoiceno",qeug_invoiceno);
dynamicObject.set("inventry_invoicebill",dynamicObject1);
dynamicObject.set("qeug_textfield", "发票识别:"+name);
this.getView().updateView("invoiceentry");
this.getView().showSuccessNotification("发票识别成功");
SaveServiceHelper.save(new DynamicObject[]{this.getView().getModel().getDataEntity()});
}
}else {//
//验真保存失败--提示失败信息
this.getView().showTipNotification(name +":发票保存验真:请上传有效发票"+saveCheckJson.getString("message"));
}
}else {
//发票识别失败--提示失败信息
this.getView().showTipNotification(name +":发票识别失败:请上传有效发票"+ocr_jsonObject.getString("message"));
}
}else {//附件类型不符合规范
this.getView().showTipNotification("文件:"+name+",不符合规范请重新上传");
}
}
}
break;
//发票验真验重
@ -108,60 +347,7 @@ public class ReconPayreqFormPlugin extends AbstractFormPlugin {
this.getView().showSuccessNotification("发票验真验重接口");
//验真验重接口调通--生成发票登记单据(recon_invoicebill)
//todo 遍历发票信息明细,批量生成发票登记单(再次点击发票验真验重,只选择未完成验真验重的数据)
for (DynamicObject invoiceen : invoiceens) {
String qeug_invoicestatus = invoiceen.getString("qeug_invoicestatus");//验真/--状态
//未完成
if ("0".equals(qeug_invoicestatus)) {
DynamicObject recon_invoicebill = BusinessDataServiceHelper.newDynamicObject("recon_invoicebill");
// DynamicObject recon_invoicebill1 = BusinessDataServiceHelper.loadSingle("2031274535679975424", "recon_invoicebill");
recon_invoicebill.set("billstatus","A");//单据状态
//所属组织
DynamicObject org = BusinessDataServiceHelper.loadSingle("bos_org", new QFilter[]{new QFilter("number", QCP.equals, "DOBE")});
if (null != org) {
recon_invoicebill.set("org",org);
}
//发票号码 invoiceno
// recon_invoicebill.set("invoiceno",System.currentTimeMillis());
recon_invoicebill.set("invoiceno",invoiceen.getString("qeug_invoiceno"));//分录中的发票号码//todo----编码规则待维护
//发票编码
recon_invoicebill.set("billno","测试数据勿删!!");//----编码规则待维护
//付款申请单 payreqentrys
DynamicObjectCollection payreqentrys = recon_invoicebill.getDynamicObjectCollection("payreqentrys");
payreqentrys.clear();
DynamicObject payreqentry = payreqentrys.addNew();
payreqentry.set("fbasedataid", this.getView().getModel().getDataEntity());
//todo:若接口返回汇总
//发票_明细
DynamicObjectCollection invoiceentrs = recon_invoicebill.getDynamicObjectCollection("invoiceentry");
invoiceentrs.clear();
DynamicObject invoiceentr = invoiceentrs.addNew();
invoiceentr.set("entry_notaxamt",100000);
//todo:若接口返回明细--遍历新增发票明细分录
Object[] save = SaveServiceHelper.save(new DynamicObject[]{recon_invoicebill});
//如果 成功生成发票登记的单子,反写分录状态为完成
if (save.length > 0) {
//发票信息分录
DynamicObjectCollection invoiceentrys = this.getView().getModel().getEntryEntity("invoiceentry");
if (invoiceentrys.size() > 0) {
for (DynamicObject invoiceent : invoiceentrys) {
//根据发票号码进行过滤
String qeug_invoiceno = invoiceent.getString("qeug_invoiceno");//分录中的发票号码
//todo:付款申请单发票信息分录行的发票号码 == 发票登记单头发票号码
if (qeug_invoiceno.equals(recon_invoicebill.getString("invoiceno"))) {
System.out.println();
invoiceent.set("qeug_invoicestatus",1);//发票状态(验真/)--------已完成
}
}
this.getView().updateView("invoiceentry");
// SaveServiceHelper.save(new DynamicObject[]{this.getView().getModel().getDataEntity()});
}
}
}
}
}else {
//失败--提示失败信息
this.getView().showTipNotification("发票验真验重失败:请检查发票明细信息是否正确");
@ -214,5 +400,98 @@ public class ReconPayreqFormPlugin extends AbstractFormPlugin {
break;
}
}
public String getOaToken(){
String userName = "lexmiscivrest";
String password = "lexmisciv2019";
String loginName = "test";
String linkUrl = "http://172.31.254.240:9090/seeyon/rest/token";
//接口请求体
JSONObject linkBody = new JSONObject();
linkBody.put("userName", userName);
linkBody.put("password", password);
linkBody.put("loginName",loginName);
String token = "";
try {
String linkPostjson = HttpClientUtils.postjson(linkUrl, null, linkBody.toJSONString());
logger.info("获取OaToken接口返回结果\n{}", linkPostjson);
if (StringUtils.isNotEmpty(linkPostjson)) {
JSONObject jsonObject = JSONObject.parseObject(linkPostjson);
token = jsonObject.getString("id");
}
return token;
} catch (Exception e) {
logger.info(String.format("获取OaToken接口异常%s", e.getMessage()));
throw new RuntimeException(e);
}
}
public JSONObject parseOCRData(String taxno,String fileExtend,String base64File,String token){
String linkUrl = "http://172.31.254.240:9090/seeyon/rest/lexmisciv/kekai/parseOCRData";
//接口请求体
JSONObject linkBody = new JSONObject();
linkBody.put("taxno", taxno);
linkBody.put("fileExtend", fileExtend);
linkBody.put("base64File", base64File);
String strJson = linkBody.toJSONString();
Map<String, String> base64Header = new HashMap<>();
base64Header.put("Content-Type", "application/json");
base64Header.put("token", token);
try {
String linkPostjson = HttpClientUtils.postjson(linkUrl, base64Header,strJson);
logger.info("获取parseOCRData接口返回结果\n{}", linkPostjson);
if (StringUtils.isNotEmpty(linkPostjson)) {
JSONObject jsonObject = JSONObject.parseObject(linkPostjson);
return jsonObject;
}
} catch (Exception e) {
logger.info(String.format("获取OaToken接口异常%s", e.getMessage()));
throw new RuntimeException(e);
}
return null;
}
public JSONObject saveCheck(String taxno,String fileId,JSONArray ocrDatas,String token) {
String linkUrl = "http://172.31.254.240:9090/seeyon/rest/lexmisciv/kekai/saveOutInvoice";
//接口请求体
JSONObject linkBody = new JSONObject();
linkBody.put("taxno", taxno);
linkBody.put("fileId", fileId);
linkBody.put("ocrDatas", ocrDatas);
String strJson = linkBody.toJSONString();
Map<String, String> saveCheckHeader = new HashMap<>();
saveCheckHeader.put("Content-Type", "application/json");
saveCheckHeader.put("token", token);
try {
String linkPostjson = HttpClientUtils.postjson(linkUrl, saveCheckHeader,strJson);
logger.info("获取parseOCRData接口返回结果\n{}", linkPostjson);
if (StringUtils.isNotEmpty(linkPostjson)) {
JSONObject jsonObject = JSONObject.parseObject(linkPostjson);
return jsonObject;
}
}catch (Exception e){
logger.info(String.format("saveOutInvoice接口异常%s", e.getMessage()));
throw new RuntimeException(e);
}
return null;
}
public String getFileBase64StrByUrl(Map<String, Object> attachment){
Object attPKId = attachment.get("attPkId");
AttachmentDto attachmentDto = AttachmentServiceHelper.getAttachmentInfoByAttPk(attPKId);
String resourcePath = attachmentDto.getResourcePath();
ByteArrayOutputStream out = new ByteArrayOutputStream();
FileServiceFactory.getAttachmentFileService().download(resourcePath,out,null);
byte[] bytes = out.toByteArray();
String baseStr = new String(Base64.getEncoder().encode(bytes));
return baseStr;
}
}
// CodeRuleInfo codeRule = CodeRuleServiceHelper.getCodeRule("recon_invoicebill", recon_invoicebill, null);