发票识别验真

This commit is contained in:
zengweihai 2024-10-14 10:03:45 +08:00
parent 8af813c869
commit eb3636588b
1 changed files with 350 additions and 251 deletions

View File

@ -3,6 +3,8 @@ package shkd.repc.recon.formplugin;
import cfca.svs.api.util.IOUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.grapecity.documents.excel.B;
import com.grapecity.documents.excel.D;
import com.huawei.shade.org.apache.http.protocol.RequestContent;
@ -40,6 +42,7 @@ 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.servicehelper.user.UserServiceHelper;
import kd.bos.url.UrlService;
import kd.bos.util.FileNameUtils;
import kd.bos.util.HttpClientUtils;
@ -73,24 +76,45 @@ public class ReconPayreqFormPlugin extends AbstractFormPlugin {
@Override
public void afterDoOperation(AfterDoOperationEventArgs args) {
super.afterDoOperation(args);
DynamicObject dataEntity = this.getView().getModel().getDataEntity();
DynamicObject dataEntity = this.getView().getModel().getDataEntity();//单据对象
String billtype = dataEntity.getDynamicObjectType().getName();
String operateKey = args.getOperateKey();
switch (operateKey){
//发票识别test
case "qeug_invoicedistinguish":
DynamicObject usedepart = dataEntity.getDynamicObject("usedepart");//获取用款单位
if (usedepart != null){//用款单位不为空
String usedepart_number = usedepart.getString("number");//获取用款单位编码
String companyTaxNo = getCompanyTaxNo(usedepart_number);
if (Strings.isEmpty(companyTaxNo)){
this.getView().showTipNotification("请维护财务公司税务登记号");
return;
}
List<Map<String, Object>> attachments = AttachmentServiceHelper.getAttachments(billtype,
dataEntity.getPkValue(), "qeug_attachmentpanelap");
if (attachments.size() <= 0) {
this.getView().showTipNotification("请先上传发票附件");
return;
}
// ToDo: 获取token
String oaToken = getOaToken();
//发票信息分录
DynamicObjectCollection invoiceentry = this.getView().getModel().getEntryEntity("invoiceentry");
if (invoiceentry.size() > 0) {
for (DynamicObject invoice : invoiceentry) {
String qeug_invoiceno = invoice.getString("qeug_invoiceno");//分录中的发票号码
String qeug_invoicekey = invoice.getString("qeug_invoicekey");//分录中的发票保存key
Map<String, Object> map = new HashMap<>();
// 创建一个数组并将其作为值放入Map中
map.put("pi_keys", Arrays.asList(qeug_invoicekey));
ObjectMapper objectMapper = new ObjectMapper();
try {
String jsonString = objectMapper.writeValueAsString(map);
deleteInvoice(jsonString,oaToken);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
QFilter q = new QFilter("invoiceno", QCP.equals, qeug_invoiceno);
DynamicObject recon_invoicebill = BusinessDataServiceHelper.loadSingle("recon_invoicebill", new QFilter[]{q});
if (null != recon_invoicebill) {
@ -103,8 +127,6 @@ public class ReconPayreqFormPlugin extends AbstractFormPlugin {
}
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);
@ -114,15 +136,15 @@ public class ReconPayreqFormPlugin extends AbstractFormPlugin {
// TODO: 根据发票路径转换出图像数据
String fileBase64 = getFileBase64StrByUrl(attachment);//转换为Base64数据
// TODO: 调取OA发票识别接口
JSONObject ocr_jsonObject = parseOCRData("91310106054599952W", type, fileBase64, oaToken);
JSONObject ocr_jsonObject = parseOCRData(companyTaxNo, 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);//返回数据
JSONObject saveCheckJson = saveCheck(companyTaxNo, fileId, ocrDatas,oaToken);//返回数据
String save_success = saveCheckJson.getString("success");
if ("true".equals(save_success) || "false".equals(save_success)){
if ("true".equals(save_success)){
//TODO: 生成发票登记
JSONArray dataList = ocr_jsonObject.getJSONArray("data");//data数据类型为集合表示
@ -226,18 +248,21 @@ public class ReconPayreqFormPlugin extends AbstractFormPlugin {
return;
}
//todo若为有效发票则生成发票等级并构建发票信息分录数据
if (parseFlag && saveResultFlag){
JSONArray resultList = saveCheckJson.getJSONArray("resultList");
JSONObject result = resultList.getJSONObject(0);//保存返回结果
String pi_key = result.getString("pi_key");//发票结果key
DynamicObject recon_invoicebill = BusinessDataServiceHelper.newDynamicObject("recon_invoicebill");
// DynamicObject recon_invoicebill1 = BusinessDataServiceHelper.loadSingle("2031274535679975424", "recon_invoicebill");
recon_invoicebill.set("billstatus","A");//单据状态
recon_invoicebill.set("billstatus","C");//单据状态
//所属组织
DynamicObject org = BusinessDataServiceHelper.loadSingle("bos_org", new QFilter[]{new QFilter("number", QCP.equals, "DOBE")});
if (null != org) {
recon_invoicebill.set("org",org);
}
recon_invoicebill.set("contractbill",dataEntity.getDynamicObject("contractbill"));
recon_invoicebill.set("biztype","CONTRACT");//发票所属业务
DynamicObject project = dataEntity.getDynamicObject("project");
recon_invoicebill.set("project",project);//设置项目名称
recon_invoicebill.set("collectionway","NORMAL");//设置征收方式为默认普通征税
@ -246,14 +271,18 @@ public class ReconPayreqFormPlugin extends AbstractFormPlugin {
}else {
recon_invoicebill.set("invoicetype","roffinvoice"); //设置为红冲发票
}
QFilter invoicetype_qf = new QFilter("number",QCP.equals,type);
QFilter invoicetype_qf = new QFilter("number",QCP.equals,invoiceType);
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("invoicecode",qeug_invoicecode == ""?"0":qeug_invoicecode);//发票代码
recon_invoicebill.set("invoiceno",qeug_invoiceno);//分录中的发票号码
recon_invoicebill.set("invoiceamount",qeug_amount);//发票金额
recon_invoicebill.set("haswriteoffamount",BigDecimal.ZERO);//已核销金额
recon_invoicebill.set("writeoffamount",qeug_amount);//未核销金额
recon_invoicebill.set("notaxamt",qeug_notaxamt);//不含税金额
recon_invoicebill.set("tax",qeug_tax);//税额
recon_invoicebill.set("amount",qeug_amount);//价税合计
@ -312,6 +341,7 @@ public class ReconPayreqFormPlugin extends AbstractFormPlugin {
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_invoicekey",pi_key);
dynamicObject.set("qeug_textfield", "发票识别:"+name);
this.getView().updateView("invoiceentry");
this.getView().showSuccessNotification("发票识别成功");
@ -330,6 +360,12 @@ public class ReconPayreqFormPlugin extends AbstractFormPlugin {
}
}
}
this.getView().updateView("invoiceentry");
}else {
this.getView().showTipNotification("请先选择用款单位!");
}
break;
//发票验真验重
case "qeug_invoicecheck":
@ -379,6 +415,26 @@ public class ReconPayreqFormPlugin extends AbstractFormPlugin {
EntryGrid assetsentry = (EntryGrid)this.getView().getControl("invoiceentry");
int[] selectRows = assetsentry.getSelectRows();
if (selectRows.length > 0) {
Map<String, Object> map = new HashMap<>();
List<String> noList = new ArrayList<>();
for (int i = 0; i < selectRows.length; i++) {
int selectRow = selectRows[i];
String qeug_invoicekey = (String)this.getModel().getValue("qeug_invoicekey",selectRow);//分录中的发票保存key
noList.add(qeug_invoicekey);
}
map.put("pi_keys", noList);
String keys_str = "";
ObjectMapper objectMapper = new ObjectMapper();
try {
keys_str = objectMapper.writeValueAsString(map);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
String oaToken = this.getOaToken();
JSONObject deleteResult = deleteInvoice(keys_str, oaToken);
String del_result = deleteResult.getString("success");
if ("true".equals(del_result)){
for (int i = 0; i < selectRows.length; i++) {
int selectRow = selectRows[i];
String qeug_invoiceno = (String)this.getModel().getValue("qeug_invoiceno",selectRow);//分录中的发票号码
@ -394,6 +450,10 @@ public class ReconPayreqFormPlugin extends AbstractFormPlugin {
}
}
}
}else {
args.setCancel(true);//取消操作
this.getView().showTipNotification("oa删除接口调用失败"+deleteResult.getString("message"));
}
}
break;
default:
@ -404,7 +464,13 @@ public class ReconPayreqFormPlugin extends AbstractFormPlugin {
public String getOaToken(){
String userName = "lexmiscivrest";
String password = "lexmisciv2019";
String loginName = "test";
String loginName = "test";//测试使用test用户
//正式环境使用
// Long userId = UserServiceHelper.getCurrentUserId();
// DynamicObject bos_user = BusinessDataServiceHelper.loadSingle(userId, "bos_user");
// if (bos_user != null){
// loginName = bos_user.getString("number");
// }
String linkUrl = "http://172.31.254.240:9090/seeyon/rest/token";
//接口请求体
@ -469,7 +535,7 @@ public class ReconPayreqFormPlugin extends AbstractFormPlugin {
try {
String linkPostjson = HttpClientUtils.postjson(linkUrl, saveCheckHeader,strJson);
logger.info("获取parseOCRData接口返回结果:\n{}", linkPostjson);
logger.info("获取saveOutInvoice接口返回结果:\n{}", linkPostjson);
if (StringUtils.isNotEmpty(linkPostjson)) {
JSONObject jsonObject = JSONObject.parseObject(linkPostjson);
return jsonObject;
@ -481,6 +547,39 @@ public class ReconPayreqFormPlugin extends AbstractFormPlugin {
return null;
}
public JSONObject deleteInvoice(String pi_keys,String token) {
String linkUrl = "http://172.31.254.240:9090/seeyon/rest/lexmisciv/kekai/delInvoice";
//接口请求体
Map<String, String> saveCheckHeader = new HashMap<>();
saveCheckHeader.put("Content-Type", "application/json");
saveCheckHeader.put("token", token);
try {
String linkPostjson = HttpClientUtils.postjson(linkUrl, saveCheckHeader,pi_keys);
logger.info("获取delInvoice接口返回结果\n{}", linkPostjson);
if (StringUtils.isNotEmpty(linkPostjson)) {
JSONObject jsonObject = JSONObject.parseObject(linkPostjson);
return jsonObject;
}
}catch (Exception e){
logger.info(String.format("delInvoice接口异常%s", e.getMessage()));
throw new RuntimeException(e);
}
return null;
}
public String getCompanyTaxNo(String bizDept){
//根据用款部门的编号获得对应关系表中的财务公司编号和部门编号
DynamicObject orginfo = BusinessDataServiceHelper.loadSingle("qeug_recon_orgrelation",new QFilter[]{new QFilter("number","=",bizDept)});
String qeug_taxno = "";
if(orginfo != null){
qeug_taxno = orginfo.getString("qeug_taxno");//财务公司税务登记号
}
return qeug_taxno;
}
public String getFileBase64StrByUrl(Map<String, Object> attachment){