package com.dxhy.erp.controller; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.dxhy.common.aspect.SysLog; import com.dxhy.common.constant.CommonConstants; import com.dxhy.common.constant.ConfigureConstant; import com.dxhy.common.controller.AbstractController; import com.dxhy.common.enums.SNFplxEnum; import com.dxhy.common.exception.BaseException; import com.dxhy.common.util.InvoiceUtil; import com.dxhy.common.utils.Base64Encoding; import com.dxhy.common.utils.R; import com.dxhy.erp.entity.*; import com.dxhy.erp.entity.sdny.*; import com.dxhy.erp.model.InvoiceDetailInfo; import com.dxhy.erp.service.*; import com.dxhy.erp.utils.Base64; import com.dxhy.erp.utils.HmacSHA1Util; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.redisson.api.RLock; import org.redisson.api.RedissonClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.scheduling.annotation.Async; import org.springframework.util.Base64Utils; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; /** * 山东能源查验主流程控制器 * * @author ariesy * @date 2023-03-15 */ @SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaMethodTooLong"}) @RestController @RequestMapping("/sn") @Slf4j public class SDNYMainProcessController extends AbstractController { @Value("${sdny.environment}") private String environment; @Value("${sdny.isPresure}") private String isPresure; @Resource private InvoiceQueryService invoiceQueryService; @Resource private SNPushCheckRecordService snPushCheckRecordService; @Resource private SignSaveInvoiceInfoService signSaveInvoiceInfoService; @Resource private SignCheckRecordService signCheckRecordService; @Resource private TDxInvoiceReimburseService tDxInvoiceReimburseService; @Resource private TdxPlatformCheckLogService platfromCheckLogService; @Resource private MryxInterfaceService mryxInterfaceService; @Resource private GatherService gatherService; @Resource private RequestRecordService requestRecordService; @Autowired private ImageSubmitService imageSubmitService; @Resource private RedissonClient redisson; /** * 查验发票 */ @PostMapping("/singleInvoiceCheck2") @ResponseBody @SysLog("发票查验") public ResponseEntity singleInvoiceCheck2(@RequestBody Map pramsMap) throws Exception { log.info("传入的参数为:{}", pramsMap); String userid = "101833"; String dbName = "business"; JSONObject checkInvoiceResult = null; RequestRecord singleInvoiceCheckRecord = requestRecordService.getBaseRequestRecord(JSONObject.toJSONString(pramsMap), "/singleInvoiceCheck2", pramsMap.get("invoiceCode"), pramsMap.get("invoiceNumber"), "singleInvoiceCheck"); //挡板开关 if ("1".equals(isPresure)) { String base64 = "ewoJImNvZGUiOiAxMDAwLAoJImRhdGEiOiB7CgkJImRyYXdlZU5hbWUiOiAiIiwKCQkiYnVzaW5lc3NVbml0IjogIiIsCgkJImxlbW9uTWFya2V0VGF4Tm8iOiAiOTEzNzAxMjVNQTNRQUtDMjhBIiwKCQkiYnJhbmRNb2RlbCI6ICLliKvlhYvniYxTR002NTIxVUJBMSIsCgkJInZlaGljbGVJZGVudGlmaWNhdGlvbk5vIjogIkxTR1VBODJMN01HMDgzODcwIiwKCQkiaWRjYXJkTm8iOiAiIiwKCQkic291cmNlIjogIiIsCgkJInRyYW5zcG9ydFJvdXRlIjogIiIsCgkJInRvdGFsVGF4IjogIiIsCgkJImJ1c2luZXNzVW5pdEFkZHJlc3MiOiAiIiwKCQkibGljZW5zZVBsYXRlIjogIuS6kUFFODVTNSIsCgkJInNlbGxlclVuaXRPckluZGl2aWR1YWwiOiAi5rWO5Y2X5YyX5p6B5pif5rG96L2m5pyN5Yqh5pyJ6ZmQ5YWs5Y+45LqR5Y2X5YiG5YWs5Y+4IiwKCQkiYWRtaW5pc3RyYXRpdmVEaXZpc2lvbk5vIjogIiIsCgkJImNhcnJpZXJOYW1lIjogIiIsCgkJInRheFJlY29yZHMiOiAiIiwKCQkic2FsZXNCYW5rIjogIiIsCgkJImxlbW9uTWFya2V0IjogIuWxseS4nOa2pumAmuS6jOaJi+i9puW4guWcuuacjeWKoeaciemZkOWFrOWPuCIsCgkJImludm9pY2VUeXBlIjogIjE1IiwKCQkibGltaXRQZW9wbGUiOiAiIiwKCQkibWFjaGluZU5vIjogIjU4OTkxNjQyNDc3MiIsCgkJInB1cmNoYXNlckFkZHJlc3NQaG9uZSI6ICIiLAoJCSJzdGF0ZSI6ICIwIiwKCQkiaW52b2ljZU5vIjogIjAwMzI3OTYyIiwKCQkidmVoaWNsZVR5cGUiOiAi5bCP5Z6L5pmu6YCa5a6i6L2mIiwKCQkiZW5naW5lTm8iOiAiIiwKCQkiaW5zcGVjdGlvbk51bSI6ICIiLAoJCSJtYWNoaW5lQ29kZSI6ICIiLAoJCSJidXNpbmVzc1VuaXRQaG9uZSI6ICIiLAoJCSJzZWxsZXJQaG9uZSI6ICIxNTkxMDA4NDM4NCIsCgkJInplcm9UYXhSYXRlU2lnbiI6ICIiLAoJCSJpbnZvaWNlQ29kZSI6ICIwMzcwMDIxMDA0MTciLAoJCSJjZXJ0aWZpY2F0ZU5vIjogIiIsCgkJInB1cmNoYXNlck5hbWUiOiAiIiwKCQkiY2hlY2tDb2RlIjogIiIsCgkJImJpbGxpbmdEYXRlIjogIjIwMjMtMDMtMTciLAoJCSJ0b3RhbEFtb3VudCI6ICIiLAoJCSJ2ZWhpY2xlTm8iOiAiTFNHVUE4Mkw3TUcwODM4NzAiLAoJCSJzYWxlc0FkZHJlc3NQaG9uZSI6ICIiLAoJCSJidXllclBob25lIjogIjE1Njg4ODg1MjA1IiwKCQkic2FsZXNOYW1lIjogIiIsCgkJInB1cmNoYXNlclRheE5vIjogIiIsCgkJInNlbGxlclVuaXRDb2RlT3JJZE5vIjogIjkxNTMwMTAyTUE3SzcyWVQ1QyIsCgkJImNhcnJpZXJUYXhObyI6ICIiLAoJCSJ0YXhBdXRob3JpdHlOYW1lIjogIiIsCgkJImluc3BlY3Rpb25MaXN0Tm8iOiAiIiwKCQkiYW1vdW50VGF4IjogIiIsCgkJIm9yaWdpblBsYWNlIjogIiIsCgkJImNlcnRpZmljYXRlIjogIiIsCgkJImJ1eWVySUROdW0iOiAiIiwKCQkicGF5bWVudFZvdWNoZXJObyI6ICIiLAoJCSJsZW1vbk1hcmtldEFkZHJlc3MiOiAi5bGx5Lic55yB5rWO5Y2X5biC5rWO5YyX5byA5Y+R5Yy65Lit5bCP5LyB5Lia5a215YyW5ZutIiwKCQkiZmFjdG9yeU1vZGVsIjogIuWIq+WFi+eJjFNHTTY1MjFVQkExIiwKCQkiYnV5ZXJVbml0T3JJbmRpdmlkdWFsIjogIua1juWNl+WMl+aegeaYn+axvei9puacjeWKoeaciemZkOWFrOWPuCIsCgkJImludm9pY2VOdW1iZXIiOiAiMDAzMjc5NjIiLAoJCSJ0b2xsU2lnbiI6ICIwNyIsCgkJInRheEJ1cmVhdUNvZGUiOiAiIiwKCQkidG9ubmFnZSI6ICIiLAoJCSJ0YXhBdXRob3JpdHlObyI6ICIiLAoJCSJjb21wYW55IjogIkJJR0IiLAoJCSJsZW1vbk1hcmtldFBob25lIjogIjEzODU0MTkzMDI1IiwKCQkicHVyY2hhc2VyQmFuayI6ICIiLAoJCSJhZG1pbmlzdHJhdGl2ZURpdmlzaW9uTmFtZSI6ICIiLAoJCSJwYXNzZW5nZXJzTGltaXRlZCI6ICIiLAoJCSJidXllclVuaXRPckluZGl2aWR1YWxBZGRyZXNzIjogIuWxseS4nOecgea1juWNl+W4guWkqeahpeWMuuWMl+WFs+WMl+i3rzblj7fmuZblhYnlsbHoibLlrrblm600LTMtMTAyIiwKCQkiaW52b2ljZUxpbmVMaXN0IjogW3sKCQkJInVuaXRQcmljZSI6ICIiLAoJCQkicm93Tm8iOiAiMSIsCgkJCSJhbW91bnQiOiAiIiwKCQkJInNwZWNpZmljYXRpb25Nb2RlbCI6ICIiLAoJCQkicXVhbnRpdHkiOiAiIiwKCQkJInRheCI6ICIiLAoJCQkidHlwZSI6ICIiLAoJCQkidW5pdCI6ICIiLAoJCQkibGljZW5zZVBsYXRlTnVtIjogIiIsCgkJCSJjdXJyZW50RGF0ZUVuZCI6ICIiLAoJCQkiY29tbW9kaXR5Q29kZSI6ICIiLAoJCQkiY3VycmVudERhdGVTdGFydCI6ICIiLAoJCQkiY29tbW9kaXR5TmFtZSI6ICIiCgkJfV0sCgkJImxlbW9uTWFya2V0QmFua0FuZEFjY291bnQiOiAi5Lit5Zu96ZO26KGM5rWO6Ziz5pSv6KGM6JCl5Lia6YOoMjMyNTQwNzQ2NjQ5IiwKCQkic2FsZXNUYXhObyI6ICIiLAoJCSJzZWxsZXJVbml0T3JJbmRpdmlkdWFsQWRkcmVzcyI6ICLkupHljZfnnIHmmIbmmI7luILkupTljY7ljLrpu5Hmnpfpk7rooZfpgZPlip7kuovlpITkurrmsJHopb/ot683MjDlj7dN5qCLTS0xMOWPtyIsCgkJImJ1c2luZXNzVW5pdEJhbmtBbmRBY2NvdW50IjogIiIsCgkJInZlaGljbGVBZG1pbk9mZmljZSI6ICLmtY7ljZfluIIiLAoJCSJhbW91bnRUYXhDTiI6ICIiLAoJCSJyZWdpc3RyYXRpb25ObyI6ICIzNzAwMzc1ODUzNTciLAoJCSJwcm9kdWN0UGxhY2UiOiAiIiwKCQkicmVtYXJrcyI6ICIiLAoJCSJ0YXhCdXJlYXVOYW1lIjogIiIKCX0sCgkibWVzc2FnZSI6ICJzdWNjZXNzIgp9"; String result = Base64Encoding.decodeToString(base64); checkInvoiceResult = JSONObject.parseObject(result); R data = new R(1000, "default success"); data.put("message", "success"); data.put("data", checkInvoiceResult); try { requestRecordService.saveSuccessfulRequestRecord(singleInvoiceCheckRecord, JSONObject.toJSONString(data)); } catch (Exception e) { log.error("发票查验接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(data)); } InvoiceUtil iu = new InvoiceUtil(pramsMap.get("invoiceCode")); String fplxdm = iu.getFplxdm(); if (fplxdm != null || "10107".equals(pramsMap.get("invoiceType")) || "10108".equals(pramsMap.get("invoiceType")) || pramsMap.get("invoiceCode").startsWith("50000")) { //查验类发票,执行查验操作后入库 log.info("查验类发票,执行查验操作后入库"); //权限校验 if (StringUtils.isBlank(pramsMap.get("invoiceNumber"))) { try { requestRecordService.saveFailedRequestRecord(singleInvoiceCheckRecord, 0, JSONObject.toJSONString(R.error("发票号码不能为空!")), "发票号码不能为空!"); } catch (Exception e) { log.error("发票查验接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(R.error("发票号码不能为空!"))); } pramsMap.put("invoiceNo", pramsMap.get("invoiceNumber")); if (StringUtils.isNotBlank(pramsMap.get("billingDate"))) { String billingDate = pramsMap.get("billingDate").replaceAll("-", ""); iu.setKprq(pramsMap.get("billingDate")); pramsMap.put("invoiceDate", billingDate); pramsMap.put("billingDate", billingDate); } else { try { requestRecordService.saveFailedRequestRecord(singleInvoiceCheckRecord, 0, JSONObject.toJSONString(R.error("开票日期不能为空!")), "开票日期不能为空!"); } catch (Exception e) { log.error("发票查验接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(R.error("开票日期不能为空!"))); } if (StringUtils.isBlank(pramsMap.get("invoiceType"))) { try { requestRecordService.saveFailedRequestRecord(singleInvoiceCheckRecord, 0, JSONObject.toJSONString(R.error("发票类型不能为空!")), "发票类型不能为空!"); } catch (BaseException e) { log.error("发票查验接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(R.error("发票类型不能为空!"))); } if (SNFplxEnum.ZP.getSnFplxDm().equals(pramsMap.get("invoiceType")) || SNFplxEnum.JDC.getSnFplxDm().equals(pramsMap.get("invoiceType")) || SNFplxEnum.DZZP.getSnFplxDm().equals(pramsMap.get("invoiceType")) || SNFplxEnum.ESC.getSnFplxDm().equals(pramsMap.get("invoiceType")) || SNFplxEnum.QDZP.getSnFplxDm().equals(pramsMap.get("invoiceType")) || SNFplxEnum.QDPP.getSnFplxDm().equals(pramsMap.get("invoiceType"))) { if (StringUtils.isBlank(pramsMap.get("totalAmount"))) { try { requestRecordService.saveFailedRequestRecord(singleInvoiceCheckRecord, 0, JSONObject.toJSONString(R.error("金额不能为空!")), "金额不能为空!"); } catch (Exception e) { log.error("发票查验接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(R.error("金额不能为空!"))); } } else { if (StringUtils.isBlank(pramsMap.get("checkCode"))) { try { requestRecordService.saveFailedRequestRecord(singleInvoiceCheckRecord, 0, JSONObject.toJSONString(R.error("校验码不能为空!")), "校验码不能为空!"); } catch (Exception e) { log.error("发票查验接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(R.error("校验码不能为空!"))); } } if ("10107".equals(pramsMap.get("invoiceType")) || "10108".equals(pramsMap.get("invoiceType"))) { pramsMap.put("invoiceType", "21"); } log.info("发票类型为:{}", pramsMap.get("invoiceType")); String uuid = (StringUtils.isBlank(pramsMap.get("invoiceCode")) ? "" : pramsMap.get("invoiceCode")) + pramsMap.get("invoiceNo"); log.info("uuid={}", uuid); String notes = invoiceQueryService.checkingInvoiceInfo("business", uuid, getUserInfo()); if (notes != null) { try { requestRecordService.saveFailedRequestRecord(singleInvoiceCheckRecord, 0, JSONObject.toJSONString(R.error(notes)), notes); } catch (Exception e) { log.error("发票查验接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(R.error(notes))); } //非通行费类发票 先判断底账库中是否已存在 if (!"32000".equals(pramsMap.get("invoiceType"))) { //查询底账库是否有对应发票,有则直接返回 QueryWrapper recordWrapper = new QueryWrapper<>(); recordWrapper.eq("uuid", uuid); BaseTDxRecordInvoice checkInvoiceByUuid = snPushCheckRecordService.selectRecordByWrapper(recordWrapper); if (checkInvoiceByUuid != null) { log.info("进项票池已存在发票.直接返回票池信息"); R data = new R(1000, "default success"); //如果购方名称为空,则查验之后补充购销方相关信息 String gfName = checkInvoiceByUuid.getGfName(); String gfAddressAndPhone = checkInvoiceByUuid.getGfAddressAndPhone(); if(StringUtils.isEmpty(gfName) || StringUtils.isEmpty(gfAddressAndPhone)){ log.info("购方名称为空,查验补充信息"); checkInvoiceResult = snPushCheckRecordService.singleCheckInvoice(pramsMap, null); if (!checkInvoiceResult.containsKey("code")) { if (!checkInvoiceResult.containsKey("cyjg")) { //先删除原有主数据,再查验更新主数据和明细数据 int delete = snPushCheckRecordService.deleteRecordByWrapper(recordWrapper); if (delete > 0) { checkInvoiceResult.put("invoiceNo", checkInvoiceResult.getString("invoiceNumber")); signSaveInvoiceInfoService.saveQueryInvoiceResult(checkInvoiceResult, userid, "1", dbName); } } else if (checkInvoiceResult.containsKey("cyjg") && !"0001".equals(checkInvoiceResult.getString("cyjg"))) { data = errorInfo(checkInvoiceResult, data); try { requestRecordService.saveFailedRequestRecord(singleInvoiceCheckRecord, 0, JSONObject.toJSONString(data), (String) checkInvoiceResult.get("cyjgxx")); } catch (Exception e) { log.error("发票查验接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(data)); } } checkInvoiceByUuid = snPushCheckRecordService.selectRecordByWrapper(recordWrapper); } String invoiceNo = checkInvoiceByUuid.getInvoiceNo(); QueryWrapper detailWrapper = new QueryWrapper<>(); detailWrapper.eq("uuid", uuid); List baseTDxRecordInvoiceDetail = null; List vehicleList = null; if ("10104".equals(pramsMap.get("invoiceType")) || "10105".equals(pramsMap.get("invoiceType"))) { vehicleList = snPushCheckRecordService.vihicleList(uuid); } else { baseTDxRecordInvoiceDetail = snPushCheckRecordService.selectRecordDetailListByWrapper(detailWrapper); } if ((baseTDxRecordInvoiceDetail == null || baseTDxRecordInvoiceDetail.size() == 0) && (vehicleList == null || vehicleList.size() == 0)) { checkInvoiceResult = snPushCheckRecordService.singleCheckInvoice(pramsMap, null); List activeCallRequest = new ArrayList<>(); activeCallRequest.add(JSONObject.toJSONString(pramsMap)); activeCallRequest.add(JSONObject.toJSONString(null)); singleInvoiceCheckRecord.setActiveCallRequestDetail(JSONObject.toJSONString(activeCallRequest)); singleInvoiceCheckRecord.setActiveCall("singleCheckInvoice"); singleInvoiceCheckRecord.setActiveCallResponse(JSONObject.toJSONString(checkInvoiceResult)); if (!checkInvoiceResult.containsKey("code")) { if (!checkInvoiceResult.containsKey("cyjg")) { //先删除原有主数据,再查验更新主数据和明细数据 int delete = snPushCheckRecordService.deleteRecordByWrapper(recordWrapper); if (delete > 0) { checkInvoiceResult.put("invoiceNo", checkInvoiceResult.getString("invoiceNumber")); signSaveInvoiceInfoService.saveQueryInvoiceResult(checkInvoiceResult, userid, "1", dbName); if ("10104".equals(pramsMap.get("invoiceType")) || "10105".equals(pramsMap.get("invoiceType"))) { vehicleList = snPushCheckRecordService.vihicleList(uuid); } else { baseTDxRecordInvoiceDetail = snPushCheckRecordService.selectRecordDetailListByWrapper(detailWrapper); } } } else if (checkInvoiceResult.containsKey("cyjg") && !"0001".equals(checkInvoiceResult.getString("cyjg"))) { data = errorInfo(checkInvoiceResult, data); try { requestRecordService.saveFailedRequestRecord(singleInvoiceCheckRecord, 0, JSONObject.toJSONString(data), (String) checkInvoiceResult.get("cyjgxx")); } catch (Exception e) { log.error("发票查验接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(data)); } } } JSONObject checkInvoice = (JSONObject) JSONObject.toJSON(checkInvoiceByUuid); JSONArray checkInvoiceDetail = (JSONArray) JSONObject.toJSON(baseTDxRecordInvoiceDetail); JSONObject vehicleDetail = new JSONObject(); if(vehicleList != null && vehicleList.size() > 0) { vehicleDetail = (JSONObject) JSONObject.toJSON(vehicleList.get(0)); } convertToResult(checkInvoice, checkInvoiceDetail, vehicleDetail); data.put("data", checkInvoice); //查验结果保存 // saveCheckLog(pramsMap, checkInvoice); try { if (checkInvoiceResult != null) { if (checkInvoiceResult.containsKey("code")) { requestRecordService.saveFailedRequestRecord(singleInvoiceCheckRecord, 0, JSONObject.toJSONString(data), (String) checkInvoiceResult.get("message")); } } else { requestRecordService.saveSuccessfulRequestRecord(singleInvoiceCheckRecord, JSONObject.toJSONString(data)); } } catch (Exception e) { log.error("发票查验接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(data)); } else { TdxSaleRecordInvoice tdxSaleRecordInvoice = snPushCheckRecordService.selectSaleByUuid(uuid); log.info("tdxSaleRecordInvoice={}", tdxSaleRecordInvoice); if (tdxSaleRecordInvoice != null) { log.info("销项票池已存在发票.直接返回票池信息"); R data = new R(1000, "default success"); String invoiceNo = tdxSaleRecordInvoice.getInvoiceNo(); QueryWrapper saleDetailWrapper = new QueryWrapper<>(); saleDetailWrapper.eq("invoice_no", invoiceNo); List recordInvoiceSaleDetailModel = snPushCheckRecordService.selectSaleListByWrapper(saleDetailWrapper); JSONObject checkInvoice = (JSONObject) JSONObject.toJSON(tdxSaleRecordInvoice); JSONArray saleDetail = (JSONArray) JSONObject.toJSON(recordInvoiceSaleDetailModel); convertToResult(checkInvoice, saleDetail); data.put("data", checkInvoice); //查验结果保存 // saveCheckLog(pramsMap,checkInvoice); try { requestRecordService.saveSuccessfulRequestRecord(singleInvoiceCheckRecord, JSONObject.toJSONString(data)); } catch (Exception e) { log.error("发票查验接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(data)); } } } try { checkInvoiceResult = snPushCheckRecordService.singleCheckInvoice(pramsMap, "1"); List activeCallRequest = new ArrayList<>(); activeCallRequest.add(JSONObject.toJSONString(pramsMap)); activeCallRequest.add(JSONObject.toJSONString("1")); singleInvoiceCheckRecord.setActiveCallRequestDetail(JSONObject.toJSONString(activeCallRequest)); singleInvoiceCheckRecord.setActiveCall("singleCheckInvoice"); singleInvoiceCheckRecord.setActiveCallResponse(JSONObject.toJSONString(checkInvoiceResult)); log.info("查验结果为:{}", checkInvoiceResult); } catch (Exception e) { e.printStackTrace(); log.error("查验失败,errormsg:{}", e.getMessage()); try { requestRecordService.saveFailedRequestRecord(singleInvoiceCheckRecord, 1, JSONObject.toJSONString(R.error("查验失败," + e.getMessage())), "查验失败,异常信息:{}" + JSONObject.toJSONString(e.getMessage())); } catch (Exception exception) { log.error("发票查验接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(R.error("查验失败," + e.getMessage()))); } if (!checkInvoiceResult.containsKey("code")) { if (checkInvoiceResult.containsKey("cyjg") && !"0001".equals(checkInvoiceResult.getString("cyjg"))) { R data = new R(); data = errorInfo(checkInvoiceResult, data); try { requestRecordService.saveFailedRequestRecord(singleInvoiceCheckRecord, 0, JSONObject.toJSONString(data), (String) checkInvoiceResult.get("cyjgxx")); } catch (Exception e) { log.error("发票查验接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(data)); } R data = new R(1000, "default success"); data.put("message", "success"); if (!checkInvoiceResult.containsKey("cyjg")) { checkInvoiceResult.put("invoiceNo", checkInvoiceResult.getString("invoiceNumber")); log.info("返回给影像系统的结果为:{}", checkInvoiceResult); signSaveInvoiceInfoService.saveQueryInvoiceResult(checkInvoiceResult, userid, "1", dbName); try { requestRecordService.saveSuccessfulRequestRecord(singleInvoiceCheckRecord, JSONObject.toJSONString(data)); } catch (Exception e) { log.error("发票查验接口记录保存失败,errormsg:{}", e); } } Object o = checkInvoiceResult.get("invoiceLineList"); JSONArray jsonArray = JSONObject.parseArray(JSONObject.toJSONString(o)); for (Object object : jsonArray) { Map map = JSONObject.parseObject(JSONObject.toJSONString(object), Map.class); String taxRate = (String) map.get("taxRate"); if ("0".equals(taxRate) || "*".equals(taxRate) || "**".equals(taxRate) || "***".equals(taxRate) || "不征税".equals(taxRate) || "免税".equals(taxRate)) { map.put("taxRate", "0.00"); } } JSONArray invoiceLineList = checkInvoiceResult.getJSONArray("invoiceLineList"); for (int i = 0; i < invoiceLineList.size(); i++) { JSONObject detail = invoiceLineList.getJSONObject(i); String commodityName = detail.getString("commodityName"); log.info("查验货物名称{}", commodityName); if (StringUtils.isNotBlank(commodityName) && commodityName.contains("详见销货清单")){ invoiceLineList.remove(i); } } data.put("data", checkInvoiceResult); return ResponseEntity.ok(JSONObject.toJSONString(data)); } } else { //其他类发票,直接入库 R data = new R(1000, "default success"); if (!checkInvoiceResult.isEmpty()) { log.info("其他类发票,不做查验,直接入库"); data.put("message", "success"); checkInvoiceResult.put("invoiceNo", checkInvoiceResult.getString("invoiceNumber")); checkInvoiceResult.put("message", "入库成功"); log.info("返回给影像系统的结果为:{}", checkInvoiceResult); data.put("data", checkInvoiceResult); } else { data.put("message", "error 其他类发票不支持查验"); } try { requestRecordService.saveSuccessfulRequestRecord(singleInvoiceCheckRecord, JSONObject.toJSONString(data)); } catch (Exception e) { log.error("发票查验接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(data)); } try { requestRecordService.saveFailedRequestRecord(singleInvoiceCheckRecord, 0, JSONObject.toJSONString(R.error().put("data", checkInvoiceResult)), "未知异常,请联系管理员"); } catch (Exception e) { log.error("发票查验接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(R.error().put("data", checkInvoiceResult))); } /** * 查验发票 */ @PostMapping("/singleInvoiceCheck") @ResponseBody @SysLog("发票查验") public ResponseEntity singleInvoiceCheck(@RequestBody Map pramsMap) throws Exception { log.info("传入的参数为:{}", pramsMap); String param = JSONObject.toJSONString(pramsMap); //加分布式锁 String lockKey = Base64Utils.encodeToString(param.getBytes()); boolean isLock = false; RLock lock = redisson.getLock(lockKey); try { isLock = lock.tryLock(600, TimeUnit.SECONDS); if (!isLock) { log.info("当前已有线程获取到锁"); } else { String dbName = "business"; JSONObject checkInvoiceResult = null; InvoiceUtil iu = new InvoiceUtil(pramsMap.get("invoiceCode")); String fplxdm = iu.getFplxdm(); if (fplxdm != null || "10107".equals(pramsMap.get("invoiceType")) || "10108".equals(pramsMap.get("invoiceType")) || pramsMap.get("invoiceCode").startsWith("50000")) { //查验类发票,执行查验操作后入库 log.info("查验类发票,执行查验操作后入库"); //权限校验 if (StringUtils.isBlank(pramsMap.get("invoiceNumber"))) { return ResponseEntity.ok(JSONObject.toJSONString(R.error("发票号码不能为空!"))); } pramsMap.put("invoiceNo", pramsMap.get("invoiceNumber")); if (StringUtils.isNotBlank(pramsMap.get("billingDate"))) { String billingDate = pramsMap.get("billingDate").replaceAll("-", ""); iu.setKprq(pramsMap.get("billingDate")); pramsMap.put("invoiceDate", billingDate); pramsMap.put("billingDate", billingDate); } else { return ResponseEntity.ok(JSONObject.toJSONString(R.error("开票日期不能为空!"))); } if (StringUtils.isBlank(pramsMap.get("invoiceType"))) { return ResponseEntity.ok(JSONObject.toJSONString(R.error("发票类型不能为空!"))); } if (SNFplxEnum.ZP.getSnFplxDm().equals(pramsMap.get("invoiceType")) || SNFplxEnum.JDC.getSnFplxDm().equals(pramsMap.get("invoiceType")) || SNFplxEnum.DZZP.getSnFplxDm().equals(pramsMap.get("invoiceType")) || SNFplxEnum.ESC.getSnFplxDm().equals(pramsMap.get("invoiceType")) || SNFplxEnum.QDZP.getSnFplxDm().equals(pramsMap.get("invoiceType")) || SNFplxEnum.QDPP.getSnFplxDm().equals(pramsMap.get("invoiceType"))) { if (StringUtils.isBlank(pramsMap.get("totalAmount"))) { return ResponseEntity.ok(JSONObject.toJSONString(R.error("金额不能为空!"))); } } else { if (StringUtils.isBlank(pramsMap.get("checkCode"))) { return ResponseEntity.ok(JSONObject.toJSONString(R.error("校验码不能为空!"))); } } if ("10107".equals(pramsMap.get("invoiceType")) || "10108".equals(pramsMap.get("invoiceType"))) { pramsMap.put("invoiceType", "21"); } log.info("发票类型为:{}", pramsMap.get("invoiceType")); String uuid = (StringUtils.isBlank(pramsMap.get("invoiceCode")) ? "" : pramsMap.get("invoiceCode")) + pramsMap.get("invoiceNo"); log.info("uuid={}", uuid); String notes = invoiceQueryService.checkingInvoiceInfo("business", uuid, getUserInfo()); if (notes != null) { return ResponseEntity.ok(JSONObject.toJSONString(R.error(notes))); } //非通行费类发票 先判断底账库中是否已存在 if (!"32000".equals(pramsMap.get("invoiceType"))) { //查询底账库是否有对应发票,有则直接返回 QueryWrapper recordWrapper = new QueryWrapper<>(); recordWrapper.eq("uuid", uuid); BaseTDxRecordInvoice checkInvoiceByUuid = snPushCheckRecordService.selectRecordByWrapper(recordWrapper); if (checkInvoiceByUuid != null) { log.info("进项票池已存在发票.直接返回票池信息"); R data = new R(1000, "default success"); //如果购方名称为空,则查验之后补充购销方相关信息 String gfName = checkInvoiceByUuid.getGfName(); String gfAddressAndPhone = checkInvoiceByUuid.getGfAddressAndPhone(); if (StringUtils.isEmpty(gfName) || StringUtils.isEmpty(gfAddressAndPhone)) { log.info("购方名称为空,查验补充信息"); checkInvoiceResult = snPushCheckRecordService.singleCheckInvoice(pramsMap, null); if (!checkInvoiceResult.containsKey("code")) { if (!checkInvoiceResult.containsKey("cyjg")) { snPushCheckRecordService.updateInvoice(dbName, checkInvoiceResult, recordWrapper); } else if (checkInvoiceResult.containsKey("cyjg") && !"0001".equals(checkInvoiceResult.getString("cyjg"))) { data = errorInfo(checkInvoiceResult, data); return ResponseEntity.ok(JSONObject.toJSONString(data)); } } checkInvoiceByUuid = snPushCheckRecordService.selectRecordByWrapper(recordWrapper); } String invoiceNo = checkInvoiceByUuid.getInvoiceNo(); QueryWrapper detailWrapper = new QueryWrapper<>(); detailWrapper.eq("uuid", uuid); List baseTDxRecordInvoiceDetail = null; List vehicleList = null; if ("10104".equals(pramsMap.get("invoiceType")) || "10105".equals(pramsMap.get("invoiceType"))) { vehicleList = snPushCheckRecordService.vihicleList(uuid); } else { baseTDxRecordInvoiceDetail = snPushCheckRecordService.selectRecordDetailListByWrapper(detailWrapper); } if ((baseTDxRecordInvoiceDetail == null || baseTDxRecordInvoiceDetail.size() == 0) && (vehicleList == null || vehicleList.size() == 0)) { checkInvoiceResult = snPushCheckRecordService.singleCheckInvoice(pramsMap, null); if (!checkInvoiceResult.containsKey("code")) { if (!checkInvoiceResult.containsKey("cyjg")) { //先删除原有主数据,再查验更新主数据和明细数据 snPushCheckRecordService.updateInvoice(dbName, checkInvoiceResult, recordWrapper); if ("10104".equals(pramsMap.get("invoiceType")) || "10105".equals(pramsMap.get("invoiceType"))) { vehicleList = snPushCheckRecordService.vihicleList(uuid); } else { baseTDxRecordInvoiceDetail = snPushCheckRecordService.selectRecordDetailListByWrapper(detailWrapper); } } else if (checkInvoiceResult.containsKey("cyjg") && !"0001".equals(checkInvoiceResult.getString("cyjg"))) { data = errorInfo(checkInvoiceResult, data); return ResponseEntity.ok(JSONObject.toJSONString(data)); } } } decimalFormat(checkInvoiceByUuid); JSONObject checkInvoice = (JSONObject) JSONObject.toJSON(checkInvoiceByUuid); log.info("checkInvoice:{}",checkInvoice); JSONArray checkInvoiceDetail = (JSONArray) JSONObject.toJSON(baseTDxRecordInvoiceDetail); JSONObject vehicleDetail = new JSONObject(); if (vehicleList != null && vehicleList.size() > 0) { vehicleDetail = (JSONObject) JSONObject.toJSON(vehicleList.get(0)); } convertToResult(checkInvoice, checkInvoiceDetail, vehicleDetail); data.put("data", checkInvoice); //查验结果保存 //saveCheckLog(pramsMap, checkInvoice); return ResponseEntity.ok(JSONObject.toJSONString(data)); } else { TdxSaleRecordInvoice tdxSaleRecordInvoice = snPushCheckRecordService.selectSaleByUuid(uuid); log.info("tdxSaleRecordInvoice={}", tdxSaleRecordInvoice); if (tdxSaleRecordInvoice != null) { log.info("销项票池已存在发票.直接返回票池信息"); R data = new R(1000, "default success"); String invoiceNo = tdxSaleRecordInvoice.getInvoiceNo(); QueryWrapper saleDetailWrapper = new QueryWrapper<>(); saleDetailWrapper.eq("invoice_no", invoiceNo); List recordInvoiceSaleDetailModel = snPushCheckRecordService.selectSaleListByWrapper(saleDetailWrapper); List baseTDxRecordInvoiceDetail = null; if(recordInvoiceSaleDetailModel == null || recordInvoiceSaleDetailModel.size() == 0){ log.info("明细行为空,查验补充信息"); checkInvoiceResult = snPushCheckRecordService.singleCheckInvoice(pramsMap, null); if (!checkInvoiceResult.containsKey("code")) { if (!checkInvoiceResult.containsKey("cyjg")) { snPushCheckRecordService.updateInvoice(dbName, checkInvoiceResult, recordWrapper); QueryWrapper detailWrapper = new QueryWrapper<>(); detailWrapper.eq("uuid", uuid); baseTDxRecordInvoiceDetail = snPushCheckRecordService.selectRecordDetailListByWrapper(detailWrapper); } else if (checkInvoiceResult.containsKey("cyjg") && !"0001".equals(checkInvoiceResult.getString("cyjg"))) { data = errorInfo(checkInvoiceResult, data); return ResponseEntity.ok(JSONObject.toJSONString(data)); } } checkInvoiceByUuid = snPushCheckRecordService.selectRecordByWrapper(recordWrapper); } decimalFormat(tdxSaleRecordInvoice); JSONObject checkInvoice = (JSONObject) JSONObject.toJSON(tdxSaleRecordInvoice); JSONArray saleDetail = (JSONArray) JSONObject.toJSON(recordInvoiceSaleDetailModel); if(baseTDxRecordInvoiceDetail!= null && baseTDxRecordInvoiceDetail.size() > 0){ saleDetail = (JSONArray) JSONObject.toJSON(baseTDxRecordInvoiceDetail); } convertToResult(checkInvoice, saleDetail); data.put("data", checkInvoice); //查验结果保存 // saveCheckLog(pramsMap,checkInvoice); return ResponseEntity.ok(JSONObject.toJSONString(data)); } } } try { checkInvoiceResult = snPushCheckRecordService.singleCheckInvoice(pramsMap, "1"); log.info("查验结果为:{}", checkInvoiceResult); } catch (Exception e) { e.printStackTrace(); log.error("查验失败,errormsg:{}", e.getMessage()); return ResponseEntity.ok(JSONObject.toJSONString(R.error("查验失败," + e.getMessage()))); } if (!checkInvoiceResult.containsKey("code")) { if (checkInvoiceResult.containsKey("cyjg") && !"0001".equals(checkInvoiceResult.getString("cyjg"))) { R data = new R(); data = errorInfo(checkInvoiceResult, data); return ResponseEntity.ok(JSONObject.toJSONString(data)); } R data = new R(1000, "default success"); data.put("message", "success"); if (!checkInvoiceResult.containsKey("cyjg")) { checkInvoiceResult.put("invoiceNo", checkInvoiceResult.getString("invoiceNumber")); log.info("返回给影像系统的结果为:{}", checkInvoiceResult); signSaveInvoiceInfoService.saveQueryInvoiceResult(checkInvoiceResult, "", "1", dbName); } Object o = checkInvoiceResult.get("invoiceLineList"); JSONArray jsonArray = JSONObject.parseArray(JSONObject.toJSONString(o)); for (Object object : jsonArray) { Map map = JSONObject.parseObject(JSONObject.toJSONString(object), Map.class); String taxRate = (String) map.get("taxRate"); if ("0".equals(taxRate) || "*".equals(taxRate) || "**".equals(taxRate) || "***".equals(taxRate) || "不征税".equals(taxRate) || "免税".equals(taxRate)) { map.put("taxRate", "0.00"); } } JSONArray invoiceLineList = checkInvoiceResult.getJSONArray("invoiceLineList"); for (int i = 0; i < invoiceLineList.size(); i++) { JSONObject detail = invoiceLineList.getJSONObject(i); String commodityName = detail.getString("commodityName"); log.info("查验货物名称{}", commodityName); if (StringUtils.isNotBlank(commodityName) && commodityName.contains("详见销货清单")){ invoiceLineList.remove(i); } } data.put("data", checkInvoiceResult); return ResponseEntity.ok(JSONObject.toJSONString(data)); } } else { //其他类发票,直接入库 R data = new R(500, "default success"); if (checkInvoiceResult != null && !checkInvoiceResult.isEmpty()) { log.info("其他类发票,不做查验,直接入库"); data.put("message", "success"); checkInvoiceResult.put("invoiceNo", checkInvoiceResult.getString("invoiceNumber")); checkInvoiceResult.put("message", "入库成功"); log.info("返回给影像系统的结果为:{}", checkInvoiceResult); data.put("data", checkInvoiceResult); } else { //根据刘振要求进行提示修改 data.put("message", "请检查发票代码或者发票类型是否正确!"); } return ResponseEntity.ok(JSONObject.toJSONString(data)); } return ResponseEntity.ok(JSONObject.toJSONString(R.error().put("data", checkInvoiceResult))); } }catch (Exception e) { log.error("接口查验异常{}",e); return ResponseEntity.ok(JSONObject.toJSONString(R.error().put("data", ""))); } finally { //关锁 if (isLock) { lock.unlock(); } } return ResponseEntity.ok(JSONObject.toJSONString(R.error().put("data", "请求正在处理,不要重复提交"))); } private void decimalFormat(TdxSaleRecordInvoice tdxSaleRecordInvoice) { if (tdxSaleRecordInvoice != null){ if (tdxSaleRecordInvoice.getInvoiceAmount() != null){ tdxSaleRecordInvoice.setInvoiceAmount(tdxSaleRecordInvoice.getInvoiceAmount().setScale(2)); } if (tdxSaleRecordInvoice.getTaxAmount() != null){ tdxSaleRecordInvoice.setTaxAmount(tdxSaleRecordInvoice.getTaxAmount().setScale(2)); } if (tdxSaleRecordInvoice.getTotalAmount() != null){ tdxSaleRecordInvoice.setTotalAmount(tdxSaleRecordInvoice.getTotalAmount().setScale(2)); } } } public static void decimalFormat(BaseTDxRecordInvoice checkInvoiceByUuid) { if (checkInvoiceByUuid != null){ if (checkInvoiceByUuid.getInvoiceAmount() != null){ checkInvoiceByUuid.setInvoiceAmount(checkInvoiceByUuid.getInvoiceAmount().setScale(2)); } if (checkInvoiceByUuid.getTaxAmount() != null){ checkInvoiceByUuid.setTaxAmount(checkInvoiceByUuid.getTaxAmount().setScale(2)); } if (checkInvoiceByUuid.getTotalAmount() != null){ checkInvoiceByUuid.setTotalAmount(checkInvoiceByUuid.getTotalAmount().setScale(2)); } } } @Async void saveCheckLog(Map pramsMap, JSONObject checkInvoice) { TdxPlatformCheckLog checkLog = new TdxPlatformCheckLog(); checkLog.setCreateDate(new Date()); checkLog.setInvoiceNo(pramsMap.get("invoiceNo")); checkLog.setInvoiceCode(pramsMap.get("invoiceCode")); checkLog.setCheckType("1"); //将参数转化为json字符串 String params = JSONObject.toJSONString(pramsMap); //将json字符串转化为base64格式 String paramsEncode = Base64.encode(params.getBytes()); checkLog.setRequesPrams(paramsEncode); String returnResult = checkInvoice.toJSONString(); String returnResultEncode = Base64.encode(returnResult.getBytes()); checkLog.setReturnResult(returnResultEncode); //结果入库 platfromCheckLogService.save(checkLog); } private R errorInfo(JSONObject checkInvoiceResult, R data) { if ("0002".equals(checkInvoiceResult.getString("cyjg"))) { data = new R(1002, checkInvoiceResult.getString("cyjgxx")); } if ("0005".equals(checkInvoiceResult.getString("cyjg"))) { data = new R(1003, checkInvoiceResult.getString("cyjgxx")); } if ("0006".equals(checkInvoiceResult.getString("cyjg"))) { data = new R(1004, checkInvoiceResult.getString("cyjgxx")); } if ("0009".equals(checkInvoiceResult.getString("cyjg"))) { data = new R(1005, checkInvoiceResult.getString("cyjgxx")); } if ("1004".equals(checkInvoiceResult.getString("cyjg"))) { data = new R(1006, checkInvoiceResult.getString("cyjgxx")); } if ("1005".equals(checkInvoiceResult.getString("cyjg"))) { data = new R(1007, checkInvoiceResult.getString("cyjgxx")); } if ("1006".equals(checkInvoiceResult.getString("cyjg"))) { data = new R(1008, checkInvoiceResult.getString("cyjgxx")); } if ("1008".equals(checkInvoiceResult.getString("cyjg"))) { data = new R(1009, checkInvoiceResult.getString("cyjgxx")); } if ("1009".equals(checkInvoiceResult.getString("cyjg"))) { data = new R(1010, checkInvoiceResult.getString("cyjgxx")); } if ("1010".equals(checkInvoiceResult.getString("cyjg"))) { data = new R(1011, checkInvoiceResult.getString("cyjgxx")); } if ("1014".equals(checkInvoiceResult.getString("cyjg"))) { data = new R(1012, checkInvoiceResult.getString("cyjgxx")); } if ("1015".equals(checkInvoiceResult.getString("cyjg"))) { data = new R(1013, checkInvoiceResult.getString("cyjgxx")); } if ("1020".equals(checkInvoiceResult.getString("cyjg"))) { data = new R(1014, checkInvoiceResult.getString("cyjgxx")); } if ("1021".equals(checkInvoiceResult.getString("cyjg"))) { data = new R(1015, checkInvoiceResult.getString("cyjgxx")); } return data; } private void convertToResult(JSONObject checkInvoice, JSONArray detailArray) throws ParseException { checkInvoice.put("totalTax", checkInvoice.getString("taxAmount")); checkInvoice.put("amountTax", checkInvoice.getString("totalAmount")); checkInvoice.put("totalAmount", checkInvoice.getString("invoiceAmount")); // 增加备注字段 checkInvoice.put("remarks", checkInvoice.getString("remark")); //添加非空判断 String invoiceCode = null; if (checkInvoice.get("invoiceCode") != null) { invoiceCode = checkInvoice.get("invoiceCode").toString(); } String invoiceType = checkInvoice.get("invoiceType").toString(); if ("22".equals(invoiceType)) { invoiceType = "33"; } if ("21".equals(invoiceType)) { invoiceType = "31"; } checkInvoice.put("invoiceType", invoiceType); checkInvoice.put("administrativeDivisionNo", ""); checkInvoice.put("administrativeDivisionName", ""); if ("31".equals(invoiceType) || "32".equals(invoiceType) || "185".equals(invoiceType) || "186".equals(invoiceType)) { checkInvoice.put("invoiceCode", ""); checkInvoice.put("invoiceNumber", invoiceCode == null ? checkInvoice.get("invoiceNo").toString() : invoiceCode + checkInvoice.get("invoiceNo").toString()); } else { checkInvoice.put("invoiceCode", invoiceCode); checkInvoice.put("invoiceNumber", checkInvoice.get("invoiceNo").toString()); } //全电纸专发票类型185返回给影像01 if ("185".equals(checkInvoice.getString("invoiceType"))){ checkInvoice.put("invoiceType","01"); } String uuid = ""; if (invoiceCode != null && StringUtils.isNoneBlank(invoiceCode)) { uuid = invoiceCode + checkInvoice.get("invoiceNo").toString(); checkInvoice.put("uuid", uuid); } else { uuid = checkInvoice.get("invoiceNo").toString(); checkInvoice.put("uuid", uuid); } String kprq = checkInvoice.get("invoiceDate").toString(); SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US); Date date = sdf.parse(kprq); String formatStr = new SimpleDateFormat("yyyy-MM-dd").format(date); log.info("开票日期为:{} ", formatStr); checkInvoice.put("billingDate", formatStr); checkInvoice.put("purchaserName", checkInvoice.get("gfName")); checkInvoice.put("purchaserTaxNo", checkInvoice.get("gfTaxNo")); checkInvoice.put("purchaserBank", checkInvoice.get("gfBankAndNo")); checkInvoice.put("purchaserAddressPhone", checkInvoice.get("gfAddressAndPhone")); checkInvoice.put("salesName", checkInvoice.get("xfName")); checkInvoice.put("salesTaxNo", checkInvoice.get("xfTaxNo")); checkInvoice.put("salesAddressPhone", checkInvoice.get("gfAddressAndPhone")); checkInvoice.put("salesBank", checkInvoice.get("gfBankAndNo")); checkInvoice.put("checkCode", checkInvoice.get("checkCode")); checkInvoice.put("state", "0"); checkInvoice.put("tollSign", "07"); String taxRate = ""; List> returnDetailList = new ArrayList<>(); JSONArray detailList = detailArray; for (int i = 0; i < detailList.size(); i++) { String taxRateDetail = ""; JSONObject detail = (JSONObject) detailList.get(i); if(StringUtils.isNotEmpty(detail.getString("goodsName")) && (detail.getString("goodsName").equals("(详见销货清单)") || detail.getString("goodsName").equals("(详见销货清单)"))){ continue; } Map snDetailMap = new HashMap(); snDetailMap.put("rowNo", i + 1 + ""); snDetailMap.put("commodityCode", detail.getString("goodsNum")); snDetailMap.put("commodityName", detail.getString("goodsName")); snDetailMap.put("specificationModel", detail.getString("model")); snDetailMap.put("unit", detail.getString("unit")); String quantity = detail.getString("num"); snDetailMap.put("quantity", quantity); String unitPrice = detail.getString("unitPrice"); snDetailMap.put("unitPrice", unitPrice); if ("11".equals(invoiceType)) { snDetailMap.put("tax", "0"); //如果是卷票计算合计金额 String hjje = null; if (detail.getString("detailAmount") != null && detail.getString("taxAmount") != null) { BigDecimal je = new BigDecimal(detail.getString("detailAmount")); if (detail.getString("taxAmount") != null) { hjje = je.add(new BigDecimal(detail.getString("taxAmount"))).toPlainString(); } else { hjje = je.toPlainString(); } } if (StringUtils.isNotBlank(hjje)) snDetailMap.put("amount", hjje); } else { snDetailMap.put("tax", "***".equals(detail.getString("taxAmount")) ? "0.00" : detail.getString("taxAmount")); snDetailMap.put("amount", detail.getString("detailAmount")); } if ("免税".equals(detail.getString("taxRate")) || "不征税".equals(detail.getString("taxRate")) || "***".equals(detail.getString("taxRate"))) { taxRateDetail = "0"; snDetailMap.put("taxRate", taxRateDetail); } else { if (detail.get("taxRate") != null && StringUtils.isNoneBlank(detail.get("taxRate").toString())) { try { taxRateDetail = detail.get("taxRate").toString(); if (!"0".equals(taxRateDetail)) { if (StringUtils.isNoneBlank(taxRateDetail)) { if (taxRateDetail.contains("%")) { taxRateDetail = taxRateDetail.replace("%",""); } BigDecimal tax = new BigDecimal(taxRateDetail); BigDecimal tax1 = tax.divide(new BigDecimal("100")); if (tax.compareTo(new BigDecimal("0.99")) < 1) { taxRateDetail = tax.toPlainString(); }else { taxRateDetail = tax1.toPlainString(); } } } }catch (Exception e){ log.error("报错的原因为:{}",e); taxRateDetail = "0.00"; } if ("11".equals(invoiceType)) { taxRateDetail = "0.000"; } log.info("明细-发票税率为:{}", taxRateDetail); snDetailMap.put("taxRate", taxRateDetail); } } log.info("明细-tax={}", detail.get("taxAmount")); snDetailMap.put("currentDateEnd", detail.getString("txrqz")); snDetailMap.put("currentDateStart", detail.getString("txrqq")); snDetailMap.put("licensePlateNum", detail.getString("cph")); snDetailMap.put("type", detail.getString("lx")); returnDetailList.add(snDetailMap); } checkInvoice.put("invoiceLineList", returnDetailList); } private void convertToResult(JSONObject checkInvoice, JSONArray detailArray, JSONObject vehicleList) throws ParseException { checkInvoice.put("totalTax", checkInvoice.getString("taxAmount")); checkInvoice.put("amountTax", checkInvoice.getString("totalAmount")); checkInvoice.put("totalAmount", checkInvoice.getString("invoiceAmount")); // 增加备注字段 checkInvoice.put("remarks", checkInvoice.getString("remark")); //添加非空判断 String invoiceCode = null; if (checkInvoice.get("invoiceCode") != null) { invoiceCode = checkInvoice.get("invoiceCode").toString(); } String invoiceType = checkInvoice.get("invoiceType").toString(); if ("22".equals(invoiceType)) { invoiceType = "33"; } if ("21".equals(invoiceType)) { invoiceType = "31"; } checkInvoice.put("invoiceType", invoiceType); checkInvoice.put("administrativeDivisionNo", ""); checkInvoice.put("administrativeDivisionName", ""); if ("31".equals(invoiceType) || "32".equals(invoiceType) || "185".equals(invoiceType) || "186".equals(invoiceType)) { checkInvoice.put("invoiceCode", ""); checkInvoice.put("invoiceNumber", invoiceCode == null ? checkInvoice.get("invoiceNo").toString() : invoiceCode + checkInvoice.get("invoiceNo").toString()); } else { checkInvoice.put("invoiceCode", invoiceCode); checkInvoice.put("invoiceNumber", checkInvoice.get("invoiceNo").toString()); } if("185".equals(checkInvoice.getString("invoiceType"))){ checkInvoice.put("invoiceType", "01"); } String uuid = ""; if (invoiceCode != null && StringUtils.isNoneBlank(invoiceCode)) { uuid = invoiceCode + checkInvoice.get("invoiceNo").toString(); checkInvoice.put("uuid", uuid); } else { uuid = checkInvoice.get("invoiceNo").toString(); checkInvoice.put("uuid", uuid); } String kprq = checkInvoice.get("invoiceDate").toString(); SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US); Date date = sdf.parse(kprq); String formatStr = new SimpleDateFormat("yyyy-MM-dd").format(date); log.info("开票日期为:{} ", formatStr); checkInvoice.put("billingDate", formatStr); checkInvoice.put("purchaserName", checkInvoice.get("gfName")); checkInvoice.put("purchaserTaxNo", checkInvoice.get("gfTaxNo")); checkInvoice.put("purchaserBank", checkInvoice.get("gfBankAndNo")); checkInvoice.put("purchaserAddressPhone", checkInvoice.get("gfAddressAndPhone")); checkInvoice.put("salesName", checkInvoice.get("xfName")); checkInvoice.put("salesTaxNo", checkInvoice.get("xfTaxNo")); checkInvoice.put("salesAddressPhone", checkInvoice.get("xfAddressAndPhone")); checkInvoice.put("salesBank", checkInvoice.get("xfBankAndNo")); log.info(" salesBank={}",checkInvoice.getString("salesBank")); String xfAddressAndPhone = checkInvoice.getString("xfAddressAndPhone"); log.info("xfAddressAndPhone={}",xfAddressAndPhone); if (StringUtils.isNotBlank(xfAddressAndPhone)){ String[] split = xfAddressAndPhone.split("\t"); if (split.length == 2) { checkInvoice.put("sellerPhone", split[1]); }else { checkInvoice.put("sellerPhone", xfAddressAndPhone); } } log.info("sellerPhone={}",checkInvoice.getString("sellerPhone")); checkInvoice.put("checkCode", checkInvoice.get("checkCode")); if("1".equals(checkInvoice.get("invoiceStatus"))){ checkInvoice.put("state", "3"); }else if("2".equals(checkInvoice.get("invoiceStatus"))){ checkInvoice.put("state", "1"); }else if("3".equals(checkInvoice.get("invoiceStatus")) || "7".equals(checkInvoice.get("invoiceStatus")) || "8".equals(checkInvoice.get("invoiceStatus"))){ checkInvoice.put("state", "2"); }else if("4".equals(checkInvoice.get("invoiceStatus"))){ checkInvoice.put("state", "4"); }else if("0".equals(checkInvoice.get("invoiceStatus"))){ checkInvoice.put("state", "0"); } //机动车添加税率 if (vehicleList != null && StringUtils.isNotBlank(vehicleList.getString("taxRate"))) { String taxRate = vehicleList.getString("taxRate"); BigDecimal tax = new BigDecimal(taxRate); BigDecimal rate = tax.divide(new BigDecimal(100)); checkInvoice.put("taxRate", rate); } log.info("taxRate={}", checkInvoice.get("taxRate")); checkInvoice.put("tollSign", "07"); //机动车 if ("03".equals(invoiceType)) { checkInvoice.put("vehicleType", vehicleList.getString("vehicleType")); checkInvoice.put("brandModel", vehicleList.getString("factoryModel")); checkInvoice.put("originPlace", vehicleList.getString("productPlace")); checkInvoice.put("certificateNo", vehicleList.getString("certificate")); checkInvoice.put("inspectionListNo", vehicleList.getString("inspectionNum")); checkInvoice.put("engineNo", vehicleList.getString("engineNo")); checkInvoice.put("vehicleNo", vehicleList.getString("vehicleNo")); checkInvoice.put("paymentVoucherNo", vehicleList.getString("taxRecords")); checkInvoice.put("passengersLimited", vehicleList.getString("limitPeople")); checkInvoice.put("taxAuthorityNo", vehicleList.getString("taxBureauCode")); checkInvoice.put("taxAuthorityName", vehicleList.getString("taxBureauName")); checkInvoice.put("idcardNo", vehicleList.getString("buyerIdNum")); // checkInvoice.put("amountTax", vehicleList.getString("carPrice")); } //二手车 if ("15".equals(invoiceType)) { checkInvoice.put("vehicleType", vehicleList.getString("vehicleType")); checkInvoice.put("vehicleNo", vehicleList.getString("vehicleNo")); checkInvoice.put("brandModel", vehicleList.getString("factoryModel")); checkInvoice.put("licensePlate", vehicleList.getString("plateNo")); checkInvoice.put("buyerUnitOrIndividual", vehicleList.getString("buyerUnit")); checkInvoice.put("buyerUnitCodeOrIdNo", vehicleList.getString("buyerUnitCode")); checkInvoice.put("buyerUnitOrIndividualAddress", vehicleList.getString("buyerUnitAddress")); checkInvoice.put("sellerUnitOrIndividual", vehicleList.getString("sellerUnit")); checkInvoice.put("sellerUnitCodeOrIdNo", vehicleList.getString("sellerUnitCode")); checkInvoice.put("sellerUnitOrIndividualAddress", vehicleList.getString("sellerUnitAddress")); checkInvoice.put("sellerPhone", vehicleList.getString("sellerPhone")); checkInvoice.put("lemonMarket", vehicleList.getString("carMarket")); checkInvoice.put("lemonMarketAddress", vehicleList.getString("carMarketAddress")); checkInvoice.put("lemonMarketPhone", vehicleList.getString("carMarketPhone")); checkInvoice.put("lemonMarketBankAndAccount", vehicleList.getString("carMarketBank")); checkInvoice.put("vehicleAdminOffice", vehicleList.getString("vehicleAdminOffice")); checkInvoice.put("registrationNo", vehicleList.getString("registrationNo")); checkInvoice.put("lemonMarketTaxNo", vehicleList.getString("carMarketTaxNo")); checkInvoice.put("idcardNo", vehicleList.getString("buyerIdNum")); checkInvoice.put("amountTax", vehicleList.getString("carPrice")); } String taxRate = ""; List> returnDetailList = new ArrayList<>(); JSONArray detailList = detailArray; if (detailList != null) { for (int i = 0; i < detailList.size(); i++) { String taxRateDetail = ""; JSONObject detail = (JSONObject) detailList.get(i); if (StringUtils.isNotEmpty(detail.getString("goodsName")) && (detail.getString("goodsName").equals("(详见销货清单)") || detail.getString("goodsName").equals("(详见销货清单)"))) { continue; } Map snDetailMap = new HashMap(); snDetailMap.put("rowNo", i + 1 + ""); snDetailMap.put("commodityCode", detail.getString("goodsNum")); snDetailMap.put("commodityName", detail.getString("goodsName")); snDetailMap.put("specificationModel", detail.getString("model")); snDetailMap.put("unit", detail.getString("unit")); String quantity = detail.getString("num"); snDetailMap.put("quantity", quantity); String unitPrice = detail.getString("unitPrice"); snDetailMap.put("unitPrice", unitPrice); if ("11".equals(invoiceType)) { snDetailMap.put("tax", "0"); //如果是卷票计算合计金额 String hjje = null; if (detail.getString("detailAmount") != null && detail.getString("taxAmount") != null) { BigDecimal je = new BigDecimal(detail.getString("detailAmount")); if (detail.getString("taxAmount") != null) { hjje = je.add(new BigDecimal(detail.getString("taxAmount"))).toPlainString(); } else { hjje = je.toPlainString(); } } if (StringUtils.isNotBlank(hjje)) snDetailMap.put("amount", hjje); } else { snDetailMap.put("tax", "***".equals(detail.getString("taxAmount")) ? "0.00" : detail.getString("taxAmount")); snDetailMap.put("amount", detail.getString("detailAmount")); } if ("免税".equals(detail.getString("taxRate")) || "不征税".equals(detail.getString("taxRate")) || "***".equals(detail.getString("taxRate"))) { taxRateDetail = "0"; snDetailMap.put("taxRate", taxRateDetail); } else { if (detail.get("taxRate") != null && StringUtils.isNoneBlank(detail.get("taxRate").toString())) { try { taxRateDetail = detail.get("taxRate").toString(); if (!"0".equals(taxRateDetail)) { if (StringUtils.isNoneBlank(taxRateDetail)) { if (taxRateDetail.contains("%")) { taxRateDetail = taxRateDetail.replace("%", ""); } BigDecimal tax = new BigDecimal(taxRateDetail); BigDecimal tax1 = tax.divide(new BigDecimal("100")); if (tax.compareTo(new BigDecimal("0.99")) < 1) { taxRateDetail = tax.toPlainString(); } else { taxRateDetail = tax1.toPlainString(); } } } } catch (Exception e) { log.error("报错的原因为:{}", e); taxRateDetail = "0.00"; } if ("11".equals(invoiceType)) { taxRateDetail = "0.000"; } log.info("明细-发票税率为:{}", taxRateDetail); snDetailMap.put("taxRate", taxRateDetail); } } log.info("明细-tax={}", detail.get("taxAmount")); snDetailMap.put("currentDateEnd", detail.getString("txrqz")); snDetailMap.put("currentDateStart", detail.getString("txrqq")); snDetailMap.put("licensePlateNum", detail.getString("cph")); snDetailMap.put("type", detail.getString("lx")); returnDetailList.add(snDetailMap); } } checkInvoice.put("invoiceLineList", returnDetailList); } /** * 查验发票 */ @PostMapping("/syncInvoiceInfo") @ResponseBody @SysLog("同步发票信息") public ResponseEntity syncInvoiceInfo(@RequestBody SNRequestObject snRequestObject) { log.info("影像系统同步数据:{}", JSONObject.toJSONString(snRequestObject)); List invoiceCodes = new ArrayList<>(); List invoiceNos = new ArrayList<>(); RequestRecord record = null; if (snRequestObject.getInvoiceInfoList() != null && snRequestObject.getInvoiceInfoList().size() > 0) { snRequestObject.getInvoiceInfoList().stream().forEach(invoiceInfo -> { invoiceCodes.add(invoiceInfo.getInvoiceCode()); invoiceNos.add(invoiceInfo.getInvoiceNumber()); }); log.info("影像系统同步数据的发票号码为:{}", JSONObject.toJSONString(invoiceNos)); log.info("影像系统同步数据的发票代码为:{}", JSONObject.toJSONString(invoiceCodes)); if (invoiceCodes.size() > 0) { record = requestRecordService.getBaseRequestRecord(JSONObject.toJSONString(snRequestObject), "/syncInvoiceInfo", JSONObject.toJSONString(invoiceCodes), JSONObject.toJSONString(invoiceNos), "syncInvoiceInfo"); } else { record = requestRecordService.getBaseRequestRecord(JSONObject.toJSONString(snRequestObject), "/syncInvoiceInfo", JSONObject.toJSONString(invoiceNos), "syncInvoiceInfo"); } } else { record = requestRecordService.getBaseRequestRecord(JSONObject.toJSONString(snRequestObject), "/syncInvoiceInfo", "syncInvoiceInfo"); } record.setImageId(snRequestObject.getImageId()); String dbName = "business"; // 入参统一在入口处理 String orgCode = snRequestObject.getCompCode(); SysDeptEntity orgByCompCode = snPushCheckRecordService.getOrgByCompCode(orgCode); log.info("查询的组织信息为:{}", JSONObject.toJSONString(orgByCompCode)); String company = ""; String taxno = ""; String taxname = ""; if (orgByCompCode != null) { company = orgByCompCode.getCompany(); taxno = orgByCompCode.getTaxno(); taxname = orgByCompCode.getTaxname(); } String result = ""; try { List invoiceInfoList = snRequestObject.getInvoiceInfoList(); String imageId = snRequestObject.getImageId(); String compCode = snRequestObject.getCompCode(); TDxInvoiceReimburse reimburse = new TDxInvoiceReimburse(); reimburse.setImageId(imageId); reimburse.setCompCode(compCode); reimburse.setGfName(taxname); SNSAPObject object = new SNSAPObject(); object.setSYSID("FPXT"); object.setIFYWID("FI845"); object.setBSKEY(UUID.randomUUID().toString().replace("-", "")); object.setSAPKEY(""); object.setZORG(compCode); object.setZFILED1(""); object.setZFILED2(""); object.setZFILED3(""); object.setZFILED4(""); List gsClients = snPushCheckRecordService.selectByGsdm(orgCode); if (gsClients != null && gsClients.size() > 0) { object.setZFILED5(gsClients.get(0).getClient()); } else { log.error("未能查到 "+orgCode+" 公司对应的推送client"); return ResponseEntity.ok(JSONObject.toJSONString(R.error("未能查到 "+orgCode+" 公司对应的推送client"))); } List sapInvoiceInfos = new ArrayList<>(); if (invoiceInfoList != null && invoiceInfoList.size() > 0) { for (SNInvoice invoice : invoiceInfoList) { String fplxdm = ""; String snFplx = ""; result = snPushCheckRecordService.updateInvoicePoll(company, taxno, reimburse, invoice, fplxdm, imageId, compCode); //价税分离判断 if (!"ok".equals(result) && !"0000".equals(result)) { R data = new R(1000, "default success"); data.put("data", result); try { requestRecordService.saveFailedRequestRecord(record, 0, JSONObject.toJSONString(R.error(result)), result); } catch (Exception e) { log.error("发票同步接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(R.error(result))); } SAPInvoiceInfo info = convertToSapOnject(invoice, imageId, compCode); sapInvoiceInfos.add(info); } object.setZDATA(JSONObject.toJSONString(sapInvoiceInfos)); //推送到SAP log.info("推送山能数据:{}", JSONObject.toJSONString(object)); JSONObject request = new JSONObject(); request.put("IS_INPUT", object); try { List requestList = new ArrayList<>(); requestList.add(request.toJSONString()); record.setActiveCallRequestDetail(JSONObject.toJSONString(requestList)); record.setActiveCall("pushCheckResultToSAP"); //将信息推送到影像系统 result = snPushCheckRecordService.pushCheckResultToSAP(request.toJSONString()); record.setActiveCallResponse(JSONObject.toJSONString(result)); R data = new R(); JSONObject sapResult = JSONObject.parseObject(result); String es_output = sapResult.getString("ES_OUTPUT"); if(StringUtils.isNotEmpty(es_output)) { JSONObject outputResult = JSONObject.parseObject(es_output); if ("S".equals(outputResult.getString("ZTYPE"))) { data = new R(1000, "default success"); data.put("data", result); } else if ("E".equals(outputResult.getString("ZTYPE"))) { data = new R(1001, "request error"); data.put("data", result); } else { data = new R(1002, "unknown error"); data.put("data", result); } }else{ data = new R(1002, "unknown error"); data.put("data", result); } try { requestRecordService.saveSuccessfulRequestRecord(record, JSONObject.toJSONString(data)); } catch (Exception e) { log.error("发票同步接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(data)); } catch (Exception e) { e.printStackTrace(); log.error("", e); // requestRecordService.saveRecord(pushCheckResultToSAPRecord,1, 1, JSONObject.toJSONString(R.error(CommonConstants.MSG_ERR_PUSH_SDNY)), 1); try { requestRecordService.saveFailedRequestRecord(record, 1, JSONObject.toJSONString(R.error(CommonConstants.MSG_ERR_PUSH_SDNY)), "推送sap失败,失败原因:" + "{" + JSONObject.toJSONString(e) + "}"); } catch (Exception exception) { log.error("发票同步接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(R.error(CommonConstants.MSG_ERR_PUSH_SDNY))); } } else { //删除对应的影像号 QueryWrapper recordWrapper = new QueryWrapper<>(); recordWrapper.eq("image_id", imageId); List baseTDxRecordInvoices = snPushCheckRecordService.selectRecordListByWrapper(recordWrapper); // if(baseTDxRecordInvoice != null){ if (baseTDxRecordInvoices != null && baseTDxRecordInvoices.size() > 0) { log.info("TDxRecordInvoice表中匹配到数据"); BaseTDxRecordInvoice recordInvoice = new BaseTDxRecordInvoice(); recordInvoice.setImageId(""); recordInvoice.setCompCode(""); snPushCheckRecordService.updateRecordByWrapper(recordInvoice, recordWrapper); R data = new R(1000, "default success"); data.put("data", ""); try { requestRecordService.saveSuccessfulRequestRecord(record, JSONObject.toJSONString(data)); } catch (Exception e) { log.error("发票同步接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(data)); } else { //删除对应的影像号 QueryWrapper reimburseWrapper = new QueryWrapper<>(); reimburseWrapper.eq("image_id", imageId); List tDxInvoiceReimburses = snPushCheckRecordService.selectReimburseListByWrapper(reimburseWrapper); if (tDxInvoiceReimburses != null && tDxInvoiceReimburses.size() > 0) { log.info("TDxInvoiceReimburse表中匹配到数据"); TDxInvoiceReimburse tDxInvoiceReimburse = new TDxInvoiceReimburse(); tDxInvoiceReimburse.setImageId(""); tDxInvoiceReimburse.setCompCode(""); snPushCheckRecordService.updateReimburseByWrapper(tDxInvoiceReimburse, reimburseWrapper); } R data = new R(1000, "default success"); data.put("data", ""); try { requestRecordService.saveSuccessfulRequestRecord(record, JSONObject.toJSONString(data)); } catch (Exception e) { log.error("发票同步接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(data)); } } } catch (TimeoutException exception) { log.error("同步SAP失败,报错信息为:{}", exception); exception.printStackTrace(); try { requestRecordService.saveFailedRequestRecord(record, 1, JSONObject.toJSONString(R.error(CommonConstants.CONNECT_TIMEOUT)), "同步SAP失败,报错信息为:" + "{" + JSONObject.toJSONString(exception) + "}"); } catch (Exception e) { log.error("发票同步接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(R.error(CommonConstants.CONNECT_TIMEOUT))); } catch (NullPointerException exception) { log.error("同步SAP失败,报错信息为:{}", exception); exception.printStackTrace(); try { requestRecordService.saveFailedRequestRecord(record, 1, JSONObject.toJSONString(R.error(CommonConstants.NULL_POINTER_TIMEOUT)), "同步SAP失败,报错信息为:" + "{" + JSONObject.toJSONString(exception) + "}"); } catch (Exception e) { log.error("发票同步接口记录保存失败,errormsg:{}", e); } return ResponseEntity.ok(JSONObject.toJSONString(R.error(CommonConstants.NULL_POINTER_TIMEOUT))); } catch (Exception exception) { log.error("同步SAP失败,报错信息为:{}", exception); exception.printStackTrace(); try { requestRecordService.saveFailedRequestRecord(record, 1, JSONObject.toJSONString(R.error(CommonConstants.MSG_ERR_DEFAULT)), "同步SAP失败,报错信息为:" + "{" + JSONObject.toJSONString(exception) + "}"); } catch (Exception e) { log.error("发票同步接口记录保存失败,errormsg:{}", e); } return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(JSONObject.toJSONString(R.error(CommonConstants.MSG_ERR_DEFAULT))); } } /** * 影像系统提交销项发票 * @param params * @return */ @PostMapping("/imageSubmitSalesInvoice") @ResponseBody @SysLog("影像系统提交销项发票") public ResponseEntity imageSubmitSalesInvoice(@RequestBody SNRequestObject params) { log.info("影像系统提交销项发票 params:{}", JSONObject.toJSONString(params)); if (StringUtils.isBlank(params.getImageId())) return ResponseEntity.ok(JSONObject.toJSONString(R.error(500, "影像ID不能为空"))); if (StringUtils.isBlank(params.getCompCode())) return ResponseEntity.ok(JSONObject.toJSONString(R.error(500, "公司代码不能为空"))); if (StringUtils.isBlank(params.getJbrUserName())) return ResponseEntity.ok(JSONObject.toJSONString(R.error(500, "经办人昵称不能为空"))); if (StringUtils.isBlank(params.getJbrUserNum())) return ResponseEntity.ok(JSONObject.toJSONString(R.error(500, "经办人编号不能为空"))); if (StringUtils.isBlank(params.getClientNum())) return ResponseEntity.ok(JSONObject.toJSONString(R.error(500, "集团客户端编号不能为空"))); try { R rs = null; if (params.getInvoiceInfoList() == null || params.getInvoiceInfoList().size() == 0) { // 撤回 rs = imageSubmitService.salesInvoiceRollback(params); } else { // 提交 rs = imageSubmitService.salesInvoiceSubmit(params); } return ResponseEntity.ok(JSONObject.toJSONString(rs)); }catch (Exception e) { log.error("影像系统提交销项发票失败", e); return ResponseEntity.ok(JSONObject.toJSONString(R.error(500, e.getMessage()))); } } /** * 查验发票 */ @PostMapping("/sn/signature") @ResponseBody @SysLog("获取接口签名值") public String signature(HttpServletRequest request, @RequestBody Map paramMap) { String ip = paramMap.get("ip"); String apiCode = paramMap.get("apiCode"); String secretId = paramMap.get("secretId"); String secretKey = paramMap.get("secretKey"); String content = paramMap.get("content"); String dataExchangeId = paramMap.get("dataExchangeId"); TreeMap sortMap = new TreeMap<>(); sortMap.put("SecretId", secretId); sortMap.put("dataExchangeId", dataExchangeId); sortMap.put("encryptCode", "0"); sortMap.put("zipCode", "0"); sortMap.put("content", content); String reqUrl = request.getRequestURL().toString(); if (reqUrl.contains(ConfigureConstant.STRING_WH)) { reqUrl = reqUrl.substring(ConfigureConstant.INT_0, reqUrl.indexOf(ConfigureConstant.STRING_WH)); } //获取鉴权URL String signUrl = getAuthUrl(reqUrl, request.getMethod()); String signature = HmacSHA1Util.genSign(signUrl, sortMap, secretKey); return signature; } private static String getAuthUrl(String reqUrl, String method) { if (reqUrl.split(ConfigureConstant.STRING_COLON).length > ConfigureConstant.INT_2) { String one = reqUrl.split(ConfigureConstant.STRING_COLON)[1]; String two = reqUrl.split(ConfigureConstant.STRING_COLON)[2]; //支持带端口数据的返回 if (two.indexOf(ConfigureConstant.STRING_SLASH_LINE) > 0) { reqUrl = one.replaceAll("//", "") + ConfigureConstant.STRING_COLON + two; } else { reqUrl = one.replaceAll("//", "") + two.substring(two.indexOf(ConfigureConstant.STRING_SLASH_LINE)); } } else if (reqUrl.split(ConfigureConstant.STRING_COLON).length == ConfigureConstant.INT_2) { String one = reqUrl.split(ConfigureConstant.STRING_COLON)[1]; reqUrl = one.contains("//") ? one.replaceAll("//", "") : reqUrl; } return method + reqUrl + "?"; } private SAPInvoiceInfo convertToSapOnject(SNInvoice snInvoice, String imageId, String compCode) { //转换时间格式 Date date = new Date(); SimpleDateFormat formatterTime = new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat formatterDate = new SimpleDateFormat("yyyyMMddHHmmss"); String timeString = formatterTime.format(date); String dateString = formatterDate.format(date); SAPInvoiceInfo info = new SAPInvoiceInfo(); String snFplx = convertToSapType(snInvoice.getInvoiceType()); info.setZBAZT("0"); info.setZXFSH(snInvoice.getSalesTaxNo()); info.setZFPSZD(snInvoice.getPlace()); info.setZSPMC(snInvoice.getDraweeName()); info.setZSPSBH(snInvoice.getDraweeTaxNo()); info.setZCYJG(snInvoice.getCheckResultMessage()); info.setZJKZM(snInvoice.getImportCertificateNo()); info.setZDJZH(snInvoice.getCertificateNo()); info.setZGFMC(snInvoice.getPurchaserName()); info.setSORT(snInvoice.getSort()); // 文件名 info.setZWJMC(snInvoice.getFileName()); //机打代码 info.setZJDDM(snInvoice.getMachineCode()); if (snInvoice.getTotalAmount() != null) { BigDecimal total = new BigDecimal(snInvoice.getTotalAmount()); // BigDecimal tax = new BigDecimal(snInvoice.getTotalTax()); // BigDecimal subtract = total.subtract(tax); info.setZJK(total.toString()); } else { info.setZJK("0.00"); } info.setZSCSJ("000000"); info.setZYSHW(snInvoice.getCargoInformation()); info.setZFPLX(snFplx); info.setZQYD(snInvoice.getTransportRoute()); info.setZCCRXM(snInvoice.getRiderName()); if (snInvoice.getBillingDate() != null) { String billingDate = snInvoice.getBillingDate(); String replace = billingDate.replace("-", ""); info.setZKPRQ(replace); } info.setZSE(snInvoice.getTotalTax()); info.setZTXBZ(snInvoice.getTollSign()); // info.setZSKPH(snInvoice.getMachineCode()); info.setZGFZH(snInvoice.getPurchaserBank()); info.setZBZ(snInvoice.getRemarks()); List invoiceLineList = snInvoice.getInvoiceLineList(); List sapInvoiceDetailList = new ArrayList<>(); //查询明细 String uuid = snInvoice.getInvoiceCode() + snInvoice.getInvoiceNumber(); if (StringUtils.isBlank(snInvoice.getInvoiceCode())) { uuid = snInvoice.getInvoiceNumber(); } List detailInfo = mryxInterfaceService.getDetailInfo(uuid); if (invoiceLineList != null && invoiceLineList.size() > 0) { for (SNInvoiceDetail snInvoiceDetail : invoiceLineList) { SAPInvoiceDetail sapInvoiceDetail = new SAPInvoiceDetail(); //设置id sapInvoiceDetail.setZHID(UUID.randomUUID().toString().replace("-", "")); // sapInvoiceDetail.setZZWDJ(""); sapInvoiceDetail.setIMG_ID(imageId); sapInvoiceDetail.setZCPH(snInvoiceDetail.getLicensePlateNum()); // sapInvoiceDetail.setZCJSJ(); // sapInvoiceDetail.setZCJRQ(); sapInvoiceDetail.setFPYXBH(snInvoice.getFileId()); sapInvoiceDetail.setZCFZ(snInvoice.getStationGetOn()); sapInvoiceDetail.setZTXZ(snInvoiceDetail.getCurrentDateEnd()); sapInvoiceDetail.setZTXQ(snInvoiceDetail.getCurrentDateStart()); if (snInvoiceDetail.getAmount() != null) sapInvoiceDetail.setZJK(snInvoiceDetail.getAmount()); sapInvoiceDetail.setZHH(snInvoiceDetail.getRowNo()); //将类似于0.03数据格式转化为3.00 if (snInvoiceDetail.getTaxRate() != null && snInvoiceDetail.getTaxRate() != "") { String taxRate = snInvoiceDetail.getTaxRate(); sapInvoiceDetail.setZTAX(taxRate); if (taxRate.contains("%")) { taxRate = taxRate.replace("%", ""); } if ("0".equals(taxRate) || "*".equals(taxRate) || "**".equals(taxRate) || "***".equals(taxRate) || "不征税".equals(taxRate) || "免税".equals(taxRate)) { sapInvoiceDetail.setZTAX("0.00"); sapInvoiceDetail.setZSM(getZSM(sapInvoiceDetail.getZTAX())); } else { //传给SAP税率为整数 BigDecimal tax = new BigDecimal(taxRate); sapInvoiceDetail.setZSM(getZSM(tax.toString())); BigDecimal tax1 = tax.multiply(new BigDecimal("100")); sapInvoiceDetail.setZTAX(tax1.toString()); if (tax1.compareTo(new BigDecimal("99")) > 0) { sapInvoiceDetail.setZTAX(taxRate); } else { DecimalFormat decimalFormat = new DecimalFormat("#.00"); String formattedDecimal = decimalFormat.format(tax1); sapInvoiceDetail.setZTAX(formattedDecimal); } } } // if (invoiceLineList.size() == 1 && snInvoice.getTaxRate() == null) { // info.setZTAX(sapInvoiceDetail.getZTAX()); // } // sapInvoiceDetail.setZHBH(""); sapInvoiceDetail.setZDJ(snInvoiceDetail.getUnitPrice()); // sapInvoiceDetail.setZHID(""); sapInvoiceDetail.setZDDZ(snInvoice.getStationGetOff()); sapInvoiceDetail.setZHWMC(snInvoiceDetail.getCommodityName()); sapInvoiceDetail.setZCYR(info.getZCYRMC()); sapInvoiceDetail.setZDW(snInvoiceDetail.getUnit()); sapInvoiceDetail.setZGSJBM(snInvoiceDetail.getCommodityCode()); if (StringUtils.isNotBlank(snInvoiceDetail.getTax())) { sapInvoiceDetail.setZSE(snInvoiceDetail.getTax()); } else { if (detailInfo != null && !detailInfo.isEmpty()) { for (InvoiceDetailInfo invoiceDetailInfo : detailInfo) { if (StringUtils.isNotBlank(invoiceDetailInfo.getDetailNo()) && invoiceDetailInfo.getDetailNo().equals(snInvoiceDetail.getRowNo())) { sapInvoiceDetail.setZSE(invoiceDetailInfo.getTaxAmount()); } } } } sapInvoiceDetail.setZSL(snInvoiceDetail.getQuantity()); // String zsmTax = snInvoiceDetail.getTaxRate(); // if (zsmTax != null && zsmTax != "" && zsmTax.length() > 4) { // zsmTax = zsmTax.substring(0, 4); // } // if (zsmTax != null && zsmTax.contains("%") && !zsmTax.contains(".")) { // zsmTax = (new BigDecimal(zsmTax.replace("%", ""))).divide(new BigDecimal("100")).toString(); // } //// sapInvoiceDetail.setZSM(getZSM(snInvoiceDetail.getTaxRate())); // sapInvoiceDetail.setZSM(getZSM(zsmTax)); sapInvoiceDetail.setZGGXH(snInvoiceDetail.getSpecificationModel()); if (snInvoice.getInvoiceCode() != null) { sapInvoiceDetail.setZTID(snInvoice.getInvoiceCode() + snInvoice.getInvoiceNumber() + snFplx); } else { sapInvoiceDetail.setZTID(snInvoice.getInvoiceNumber() + snFplx); } sapInvoiceDetailList.add(sapInvoiceDetail); } } info.setIT_ITEM(sapInvoiceDetailList); //获取税率 String taxRate = snInvoice.getTaxRate(); if (taxRate != null && !taxRate.contains("%")) { BigDecimal bigDecimal = new BigDecimal(taxRate); BigDecimal multiply = bigDecimal.multiply(new BigDecimal("100")); info.setZTAX(multiply.toPlainString()); info.setZSM(getZSM(taxRate)); } if (taxRate != null && taxRate.contains("%")) { String replace = taxRate.replace("%", ""); BigDecimal bigDecimal = new BigDecimal(replace); BigDecimal divide = bigDecimal.divide(new BigDecimal(100)); // taxRate = divide.toString(); // info.setZTAX(bigDecimal.multiply(new BigDecimal("0.01")).toString()); info.setZTAX(replace + ".00"); info.setZSM(getZSM(divide.toString())); } if (taxRate != null && taxRate.contains(".") && !taxRate.contains("%")) { BigDecimal tax = new BigDecimal(taxRate); info.setZSM(getZSM(tax.toPlainString())); BigDecimal tax1 = tax.multiply(new BigDecimal("100")); info.setZTAX(tax1.setScale(2, RoundingMode.FLOOR).toPlainString()); } if (taxRate != null && "0".equals(taxRate)) { info.setZTAX("0.00"); info.setZSM(getZSM(info.getZTAX())); } if (invoiceLineList == null || invoiceLineList.size() == 0) { if (info.getZTAX() == null || info.getZTAX() == "") { info.setZTAX("0.00"); info.setZSM(getZSM(info.getZTAX())); } } info.setZFPDM(snInvoice.getInvoiceCode()); info.setZCD(snInvoice.getOriginPlace()); info.setZSCCZ(snInvoice.getStationGetOn()); info.setZCC(snInvoice.getTrainNumber()); info.setZFPHM(snInvoice.getInvoiceNumber()); info.setZBXF(snInvoice.getInsurance()); info.setZRYFJ(snInvoice.getFuelSurcharge()); info.setZCLSB(snInvoice.getVehicleNo()); info.setZKPSJ("000000"); info.setZXCSJ("000000"); info.setZXFMC(snInvoice.getSalesName()); info.setZCYRSBH(snInvoice.getCarrierName()); info.setIMG_ID(imageId); info.setZCPH(snInvoice.getLicensePlate()); info.setZLSLBZ(snInvoice.getZeroTaxRateSign()); info.setFPYXBH(snInvoice.getFileId()); //转换日期 info.setZCJSJ(dateString); info.setZFPZT(snInvoice.getState()); if (snInvoice.getState() == null) { info.setZFPZT("0"); } else { info.setZFPZT(snInvoice.getState()); } // info.setZESCSC(""); info.setZFDJM(snInvoice.getEngineNo()); info.setZGXR(""); //认证相关 String zrzjg = getZRZJG(snInvoice.getInvoiceCode(), snInvoice.getInvoiceNumber(), info.getZFPLX()); info.setZRZJG(zrzjg); info.setZRZSSQ(""); // info.setZRZMS(""); // info.setZRZRQ(""); info.setZLC(snInvoice.getMileage()); info.setZHGZH(snInvoice.getCertificateNo()); info.setZXFDH(snInvoice.getSalesAddressPhone()); // info.setZESCSH(""); info.setZGSDM(compCode); info.setZGFSH(snInvoice.getPurchaserTaxNo()); info.setZXCRS(snInvoice.getPassengersLimited()); info.setZWSPZ(snInvoice.getPaymentVoucherNo()); info.setZCCDW(snInvoice.getTonnage()); info.setZSWJG(snInvoice.getTaxAuthorityName()); // info.setZYWDJ(""); info.setZXCCZ(snInvoice.getStationGetOff()); info.setZCCRZH(snInvoice.getIdcardNo()); info.setZCPXH(snInvoice.getBrandModel()); info.setZMHFZJJ(snInvoice.getCaacDevelopmentFund()); if (!"nullnull".equals(snInvoice.getPurchaserAddressPhone())) { info.setZGFDH(snInvoice.getPurchaserAddressPhone()); // info.setZGFDH(""); } info.setZXFZH(snInvoice.getSalesBank()); info.setZJYM(snInvoice.getCheckCode()); info.setZSJDH(snInvoice.getInspectionListNo()); // info.setZYGC(""); info.setZJSHJ(snInvoice.getAmountTax()); if (snInvoice.getAmountTax() == null && ("17".equals(info.getZFPLX()) || "18".equals(info.getZFPLX()))) { info.setZJSHJ(snInvoice.getTotalAmount()); } //添加二手车返回价税合计 if (SNFplxEnum.ESC.getFplxDm().equals(info.getZFPLX())){ info.setZJSHJ(snInvoice.getTotalAmount()); } info.setZCYRMC(snInvoice.getCarrierName()); info.setZJGDM(snInvoice.getTaxAuthorityNo()); //时间转换 info.setZGXSJ(dateString); info.setZPJZT("1"); info.setZZWLX(snInvoice.getSeat()); info.setZPJLY(snInvoice.getSource()); if (snInvoice.getSource() == null) { info.setZPJLY("1"); } if ("01".equals(snInvoice.getInvoiceType()) || "02".equals(snInvoice.getInvoiceType()) || "03".equals(snInvoice.getInvoiceType()) || "04".equals(snInvoice.getInvoiceType()) || "08".equals(snInvoice.getInvoiceType()) || "10".equals(snInvoice.getInvoiceType()) || "11".equals(snInvoice.getInvoiceType()) || "14".equals(snInvoice.getInvoiceType()) || "15".equals(snInvoice.getInvoiceType()) || "31".equals(snInvoice.getInvoiceType()) || "32".equals(snInvoice.getInvoiceType())) { //可查验发票, log.info("发票类型为:{}", snInvoice.getInvoiceType()); //0608 区块链发票 并且 checkStatus = 3 的时候 提交sap状态为3(刘振和谈楚才提出需求) if ("10".equals(snInvoice.getInvoiceType()) && "3".equals(snInvoice.getCheckStatus())) { log.info("区块链发票 传输状态checkStatus = 3"); info.setZCYZT("3"); } else { info.setZCYZT("1"); } } else { //不可查验发票 info.setZCYZT("3"); } // info.setZCYZT(snInvoice.getCheckStaus()); // if (snInvoice.getCheckStaus() == null) { // info.setZCYZT("1"); // } info.setZCJR("opentext"); info.setZGXR("opentext"); info.setZQM("opentext"); if (snInvoice.getCheckDate() != null) { String checkDate = snInvoice.getCheckDate().replace("-", ""); info.setZCYRQ(checkDate); } if (snInvoice.getInvoiceCode() != null) { info.setZTID(snInvoice.getInvoiceCode() + snInvoice.getInvoiceNumber() + snFplx); } else { info.setZTID(snInvoice.getInvoiceNumber() + snFplx); } return info; } private String convertToSapType(String invoiceType) { switch (invoiceType) { case "01":// 增值税专用发票 invoiceType = "10"; break; case "04": invoiceType = "11"; break; case "10": invoiceType = "12"; break; case "11": invoiceType = "13"; break; case "03": invoiceType = "14"; break; case "15": invoiceType = "15"; break; case "95": invoiceType = "16"; break; case "97": invoiceType = "17"; break; case "91": invoiceType = "18"; break; case "92": invoiceType = "19"; break; case "89": invoiceType = "20"; break; case "90": invoiceType = "21"; break; case "101": invoiceType = "22"; break; case "14": invoiceType = "24"; break; case "08": invoiceType = "25"; break; case "31": invoiceType = "26"; break; case "32": invoiceType = "27"; break; case "88": invoiceType = "28"; break; default: invoiceType = ""; break; } return invoiceType; } private String getZSM(String taxRate) { String sm = ""; switch (taxRate) { // 发票采集 case "0": sm = "J0"; break; case "0.00": sm = "J0"; break; case "0.000": sm = "J0"; break; case "免税": sm = "J0"; break; case "不征税": sm = "J0"; break; case "0.16": sm = "J1"; break; case "0.13": sm = "J2"; break; case "0.10": sm = "J3"; break; case "0.09": sm = "J4"; break; case "0.06": sm = "J5"; break; case "0.05": sm = "J6"; break; case "0.03": sm = "J7"; break; case "0.02": sm = "J8"; break; case "0.01": sm = "J9"; break; case "0.17": sm = "JC"; break; case "16": sm = "J1"; break; case "13": sm = "J2"; break; case "10": sm = "J3"; break; case "9": sm = "J4"; break; case "6": sm = "J5"; break; case "5": sm = "J6"; break; case "3": sm = "J7"; break; case "2": sm = "J8"; break; case "1.5": sm = "JA"; break; case "0.015": sm = "JA"; break; case "1": sm = "J9"; break; case "17": sm = "JC"; break; default: sm = ""; break; } return sm; } private String getZRZJG(String invoiceCode, String invoiceNo, String FPLX) { String ZCYJG = ""; String uuid = invoiceCode + invoiceNo; //可价税分离票种 //10:增值税专用发票 //12:增值税电子普通发票 //13:增值税普通发票(卷票) //14:机动车销售统一发票 //15:二手车销售统一发票 //24:增值税电子普通发票(通行费) //25:增值税电子专用发票 //26:电子发票(增值税专用发票) //27:电子发票(普通发票) //22:过路费发票 //11:增值税普通发票 //16:定额发票 //17:机打发票 //18:出租车发票 if ("10".equals(FPLX) || "12".equals(FPLX) || "13".equals(FPLX) || "14".equals(FPLX) || "15".equals(FPLX) || "24".equals(FPLX) || "25".equals(FPLX) || "26".equals(FPLX) || "27".equals(FPLX) || "22".equals(FPLX) || "11".equals(FPLX) || "16".equals(FPLX) || "17".equals(FPLX) || "18".equals(FPLX)) { QueryWrapper wrapper = new QueryWrapper(); wrapper.eq("uuid", uuid); TDxInvoiceReimburse one = tDxInvoiceReimburseService.getOne(wrapper); if (one != null) { ZCYJG = one.getRzhYesorno(); } } else { //19:火车票 //20:客运汽车 //21:航空运输电子客票行程单 //28:船票 //如果是可查验类型,直接更新数据库 if ("19".equals(FPLX) || "20".equals(FPLX) || "21".equals(FPLX) || "28".equals(FPLX)) { //其他票种 QueryWrapper wrapper = new QueryWrapper(); wrapper.eq("uuid", uuid); RecordInvoice recordInvoice = snPushCheckRecordService.selectByWrapper(wrapper); if (recordInvoice != null) { ZCYJG = recordInvoice.getRzhYesorno(); } } else { //海关缴款书 QueryWrapper wrapper = new QueryWrapper(); wrapper.eq("uuid", uuid); TdxCustomsRecord tdxCustomsRecord = snPushCheckRecordService.selectCustomRecordByWrapper(wrapper); if (tdxCustomsRecord != null) { ZCYJG = tdxCustomsRecord.getRzhYesorno(); } } } return ZCYJG; } public static void main(String[] args) { DecimalFormat decimalFormat = new DecimalFormat("#.00"); String formattedDecimal = decimalFormat.format(new BigDecimal("15")); BigDecimal bigDecimal = new BigDecimal("0.13"); boolean b = bigDecimal.compareTo(new BigDecimal("0.99")) < 1; System.out.println(); } }