发票识别验真

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,263 +76,296 @@ 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);
}
List<Map<String, Object>> attachments = AttachmentServiceHelper.getAttachments(billtype,
dataEntity.getPkValue(), "qeug_attachmentpanelap");
if (attachments.size() <= 0) {
this.getView().showTipNotification("请先上传发票附件");
return;
}
//发票信息分录
DynamicObjectCollection invoiceentry = this.getView().getModel().getEntryEntity("invoiceentry");
if (invoiceentry.size() > 0) {
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("删除失败:请检查发票登记单的单据状态");
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("删除失败:请检查发票登记单的单据状态");
}
}
}
invoiceentry.clear();
}
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: 生成发票登记
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(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(companyTaxNo, fileId, ocrDatas,oaToken);//返回数据
String save_success = saveCheckJson.getString("success");
if ("true".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"));//税额
}
}
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;
}
}else {
DynamicObject invoiceentr = invoiceentrs.addNew();
invoiceentr.set("entry_content",description);//项目名称
invoiceentr.set("entry_notaxamt",qeug_amount);
//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","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");//设置征收方式为默认普通征税
if ("0".equals(invoiceState)){
recon_invoicebill.set("invoicetype","posinvoice"); //设置为正数发票
}else {
recon_invoicebill.set("invoicetype","roffinvoice"); //设置为红冲发票
}
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 == ""?"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);//价税合计
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_invoicekey",pi_key);
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"));
}
//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 +":发票识别失败:请上传有效发票"+ocr_jsonObject.getString("message"));
}
}else {//
//验真保存失败--提示失败信息
this.getView().showTipNotification(name +":发票保存验真:请上传有效发票"+saveCheckJson.getString("message"));
}else {//附件类型不符合规范
this.getView().showTipNotification("文件:"+name+",不符合规范请重新上传");
}
}else {
//发票识别失败--提示失败信息
this.getView().showTipNotification(name +":发票识别失败:请上传有效发票"+ocr_jsonObject.getString("message"));
}
}else {//附件类型不符合规范
this.getView().showTipNotification("文件:"+name+",不符合规范请重新上传");
}
this.getView().updateView("invoiceentry");
}else {
this.getView().showTipNotification("请先选择用款单位!");
}
}
break;
//发票验真验重
case "qeug_invoicecheck":
@ -379,20 +415,44 @@ 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_invoiceno = (String)this.getModel().getValue("qeug_invoiceno",selectRow);//分录中的发票号码
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("删除失败:请检查发票登记单的单据状态");
args.setCancel(true);
return;
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);//分录中的发票号码
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("删除失败:请检查发票登记单的单据状态");
args.setCancel(true);
return;
}
}
}
}else {
args.setCancel(true);//取消操作
this.getView().showTipNotification("oa删除接口调用失败"+deleteResult.getString("message"));
}
}
break;
@ -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){