Kaynağa Gözat

往来账款 发票识别验真

dudm 4 yıl önce
ebeveyn
işleme
279be25237

+ 32 - 2
sc-service/src/main/java/com/huyi/service/base/entity/ZcInvoiceInf.java

@@ -15,7 +15,7 @@ import lombok.experimental.Accessors;
  * </p>
  *
  * @author xuefy
- * @since 2021-08-17
+ * @since 2021-08-18
  */
 @Data
 @EqualsAndHashCode(callSuper = false)
@@ -32,12 +32,24 @@ public class ZcInvoiceInf implements Serializable {
     private String ziiId;
 
     /**
-     * 发票编号
+     * 发票代码
      */
     @TableField("zii_no")
     private String ziiNo;
 
     /**
+     * 发票号码
+     */
+    @TableField("zii_number")
+    private String ziiNumber;
+
+    /**
+     * 校验码
+     */
+    @TableField("zii_check_code")
+    private String ziiCheckCode;
+
+    /**
      * 开票日期
      */
     @TableField("zii_date")
@@ -50,6 +62,12 @@ public class ZcInvoiceInf implements Serializable {
     private String ziiAmount;
 
     /**
+     * 不含税金额
+     */
+    @TableField("zii_excluded_amount")
+    private String ziiExcludedAmount;
+
+    /**
      * 开票类型
      */
     @TableField("zii_type")
@@ -86,6 +104,18 @@ public class ZcInvoiceInf implements Serializable {
     private String ziiTotalAmount;
 
     /**
+     * 校验结果(0未通过, 1已通过)
+     */
+    @TableField("zii_check_stt")
+    private String ziiCheckStt;
+
+    /**
+     * 状态(0:正常, 99:删除)
+     */
+    @TableField("zii_status")
+    private String ziiStatus;
+
+    /**
      * 附件(pub_file_inf.pfi_file_id)
      */
     @TableField("zii_file")

+ 1 - 1
sc-service/src/main/java/com/huyi/service/base/mapper/ZcInvoiceInfMapper.java

@@ -9,7 +9,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  * </p>
  *
  * @author xuefy
- * @since 2021-08-16
+ * @since 2021-08-18
  */
 public interface ZcInvoiceInfMapper extends BaseMapper<ZcInvoiceInf> {
 

+ 1 - 1
sc-service/src/main/java/com/huyi/service/base/service/IZcInvoiceInfService.java

@@ -17,7 +17,7 @@ import java.util.List;
  * Company: keao
  *
  * @author xuefy
- * @version v1.0.0    2021-08-16  xuefy    由Generator自动创建
+ * @version v1.0.0    2021-08-18  xuefy    由Generator自动创建
  */
 public interface IZcInvoiceInfService extends IService<ZcInvoiceInf> {
     /**

+ 1 - 1
sc-service/src/main/java/com/huyi/service/base/service/impl/ZcInvoiceInfServiceImpl.java

@@ -24,7 +24,7 @@ import java.util.List;
  * Company: keao
  *
  * @author xuefy
- * @version v1.0.0    2021-08-16  xuefy    由Generator自动创建
+ * @version v1.0.0    2021-08-18  xuefy    由Generator自动创建
  */
 @Service
 @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)

+ 207 - 0
sc-service/src/main/java/com/huyi/service/bill/controller/OwnBillController.java

@@ -0,0 +1,207 @@
+package com.huyi.service.bill.controller;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.huyi.service.base.entity.ZcBillInf;
+import com.huyi.service.base.entity.ZcInvoiceInf;
+import com.huyi.service.base.service.IZcBillInfService;
+import com.huyi.service.base.service.IZcInvoiceInfService;
+import com.huyi.service.bill.service.IOwnBillService;
+import com.keao.tianhu.starter.mybatis.plus.entity.QueryRequest;
+import com.tianhu.common.core.constant.SalaryConstants;
+import com.tianhu.common.core.domain.R;
+import com.tianhu.common.core.utils.CommonUtil;
+import com.tianhu.common.core.utils.DateUtils;
+import com.tianhu.common.core.utils.SecurityUtils;
+import com.tianhu.common.core.utils.ServletUtils;
+import com.tianhu.common.core.web.controller.BaseController;
+import com.tianhu.common.security.service.TokenService;
+import com.tianhu.system.api.RemoteFileService;
+import com.tianhu.system.api.domain.PubFileInf;
+import com.tianhu.system.api.domain.SysUser;
+import com.tianhu.system.api.model.LoginUser;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 往来账款管理
+ *
+ * @author dudm@minpay.cc
+ * @date 2021-08-17
+ */
+@RestController
+@RequestMapping("ownBill")
+public class OwnBillController extends BaseController {
+
+    /**
+     * 验证状态 0未验证1成功2失败
+     */
+    private static String CHECK_SUCCESS = "1";
+
+    @Autowired
+    private IOwnBillService iOwnBillService;
+
+    @Autowired
+    private IZcBillInfService iZcBillInfService;
+
+    @Autowired
+    private TokenService tokenService;
+
+    @Autowired
+    private  IZcInvoiceInfService iZcInvoiceInfService;
+
+    @Autowired
+    private RemoteFileService remoteFileService;
+
+    /**
+     * 往来账款列表
+     *
+     * @param zbiName           账款名称
+     * @param zbiContractNo     合同编号
+     * @param payerName         应付方
+     * @param payeeName         应收方
+     * @return
+     */
+    @GetMapping("/billList")
+    public R list(
+            @RequestParam(required=false) String zbiName,
+            @RequestParam(required=false) String zbiContractNo,
+            @RequestParam(required=false) String payerName,
+            @RequestParam(required=false) String payeeName,
+            QueryRequest request) {
+        //查询当前操作员
+        LoginUser userInfo = tokenService.getLoginUser();
+        SysUser user = userInfo.getSysUser();
+        //获取企业
+        String companyId = user.getCompanyId();
+        Map map = new HashMap();
+        if (!SalaryConstants.OPEX.equals(companyId)) {
+            map.put("companyId", companyId);
+        }
+        //账款名称
+        map.put("zbiName", zbiName);
+        //合同编号
+        map.put("zbiContractNo", zbiContractNo);
+        //应付方
+        map.put("payerName", payerName);
+        //应收方
+        map.put("payeeName", payeeName);
+        IPage<Map> list = iOwnBillService.selectBillInfList(map,request);
+        return R.ok(list);
+    }
+
+
+    /**
+     * 新增账款信息
+     *
+     * @param map    账款信息
+     * @return
+     */
+    @PostMapping("/add")
+    public R add(Map map) throws ParseException {
+        //获取登录用户
+        LoginUser user = tokenService.getLoginUser();
+        //账款名称
+        String zbiName = CommonUtil.objToString(map.get("zbiName"));
+        //应付方
+        String zbiPayerId = CommonUtil.objToString(map.get("zbiPayerId"));
+        //应收方
+        String zbiPayeeId = CommonUtil.objToString(map.get("zbiPayeeId"));
+        //时间格式化
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        //贸易日期
+        String zbiDate = CommonUtil.objToString(map.get("zbiDate"));
+        Date date = sdf.parse(zbiDate);
+        //预计收付款日期
+        String zbiPayDate = CommonUtil.objToString(map.get("zbiPayDate"));
+        Date payDate = sdf.parse(zbiPayDate);
+        //贸易金额
+        String zbiAmount = CommonUtil.objToString(map.get("zbiAmount"));
+        //合同编号
+        String zbiContractNo = CommonUtil.objToString(map.get("zbiContractNo"));
+        //账款服务
+        String zbiService = CommonUtil.objToString(map.get("zbiService"));
+        //配送订单
+        String zbiOrderNo = CommonUtil.objToString(map.get("zbiOrderNo"));
+        //配送企业
+        String zbiDistributor = CommonUtil.objToString(map.get("zbiDistributor"));
+        //备注
+        String zbiRemark = CommonUtil.objToString(map.get("zbiRemark"));
+        ZcBillInf zcBillInf = new ZcBillInf();
+        iZcBillInfService.createZcBillInf(zcBillInf);
+        //TODO
+        return R.ok();
+    }
+
+    /**
+     * 发票识别
+     *
+     * @param file    文件
+     * @return
+     */
+    @PostMapping("/getInvoiceText")
+    public ZcInvoiceInf getInvoiceText(MultipartFile file) {
+        //获取登录用户
+        LoginUser user = tokenService.getLoginUser();
+        String companyId = user.getSysUser().getCompanyId();
+        //获取发票信息
+        ZcInvoiceInf zcInvoiceInf = iOwnBillService.getInvoice(file);
+        LambdaQueryWrapper<ZcInvoiceInf> queryWrapper = new LambdaQueryWrapper<>();
+        //发票代码
+        queryWrapper.eq(ZcInvoiceInf::getZiiNo,zcInvoiceInf.getZiiNo());
+        //发票号码
+        queryWrapper.eq(ZcInvoiceInf::getZiiNumber,zcInvoiceInf.getZiiNumber());
+        List<ZcInvoiceInf> list = iZcInvoiceInfService.findZcInvoiceInfs(queryWrapper);
+        //不重复则储存发票并上传
+        if(list.size() == 0){
+            iZcInvoiceInfService.createZcInvoiceInf(zcInvoiceInf);
+            //TODO 只验真部分发票
+            //验真发票
+            zcInvoiceInf = iOwnBillService.invoiceVerification(zcInvoiceInf);
+            zcInvoiceInf.setCreateBy(user.getUserid().toString());
+            zcInvoiceInf.setCreateTime(DateUtils.getNowDate());
+            //验真通过发票上传到文件服务器
+            if(CHECK_SUCCESS.equals(zcInvoiceInf.getZiiCheckStt())){
+                //获取上传文件token
+                String token = SecurityUtils.getToken(ServletUtils.getRequest());
+                //文件传输
+                R<PubFileInf> fileResult = remoteFileService.uploadFile(file,"01","04","00", companyId, token);
+                zcInvoiceInf.setZiiFile(fileResult.getData().getPfiFileId());
+            }
+        }else{
+            zcInvoiceInf.setZiiId(list.get(0).getZiiId());
+        }
+        //返回发票信息
+        return zcInvoiceInf;
+    }
+
+    /**
+     * 重新验真
+     *
+     * @param zcInvoiceInf    发票信息
+     * @return
+     */
+    @PostMapping("/invoiceVerification")
+    public ZcInvoiceInf invoiceVerification(ZcInvoiceInf zcInvoiceInf) {
+        //获取登录用户
+        LoginUser user = tokenService.getLoginUser();
+        if(CommonUtil.isEmpty(zcInvoiceInf.getZiiId())){
+            return zcInvoiceInf;
+        }
+        //验真发票
+        zcInvoiceInf = iOwnBillService.invoiceVerification(zcInvoiceInf);
+        zcInvoiceInf.setUpdateBy(user.getUserid().toString());
+        zcInvoiceInf.setUpdateTime(DateUtils.getNowDate());
+        //修改发票信息
+        iZcInvoiceInfService.updateZcInvoiceInf(zcInvoiceInf);
+        return zcInvoiceInf;
+    }
+}

+ 22 - 0
sc-service/src/main/java/com/huyi/service/bill/mapper/OwnBillMapper.java

@@ -0,0 +1,22 @@
+package com.huyi.service.bill.mapper;
+
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.Map;
+
+@Mapper
+public interface OwnBillMapper {
+
+
+    /**
+     * 往来账款查询
+     * @param page
+     * @param map
+     * @return
+     */
+    public IPage<Map> selectBillInfList(Page<Map> page, @Param("paramMap") Map map);
+
+}

+ 34 - 0
sc-service/src/main/java/com/huyi/service/bill/service/IOwnBillService.java

@@ -0,0 +1,34 @@
+package com.huyi.service.bill.service;
+
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.huyi.service.base.entity.ZcInvoiceInf;
+import com.keao.tianhu.starter.mybatis.plus.entity.QueryRequest;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.util.Map;
+
+public interface IOwnBillService {
+
+
+    /**
+     * 往来账款查询
+     * @param map
+     * @param request
+     * @return
+     */
+    public IPage<Map> selectBillInfList(Map map, QueryRequest request);
+
+    /**
+     * 获取发票信息
+     * @param file
+     * @return
+     */
+    public ZcInvoiceInf getInvoice(MultipartFile file);
+
+    /**
+     * 发票验真
+     * @param zcInvoiceInf
+     * @return
+     */
+    public ZcInvoiceInf invoiceVerification(ZcInvoiceInf zcInvoiceInf);
+}

+ 203 - 0
sc-service/src/main/java/com/huyi/service/bill/service/impl/OwnBillServiceImpl.java

@@ -0,0 +1,203 @@
+package com.huyi.service.bill.service.impl;
+
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.huyi.service.base.entity.ZcInvoiceInf;
+import com.huyi.service.bill.mapper.OwnBillMapper;
+import com.huyi.service.bill.service.IOwnBillService;
+import com.keao.tianhu.starter.mybatis.plus.entity.QueryRequest;
+import com.tianhu.common.core.ocr.OcrUtil;
+import com.tianhu.common.core.utils.CommonUtil;
+import com.tianhu.common.redis.common.RedisUtils;
+import com.tianhu.common.redis.service.RedisService;
+import lombok.SneakyThrows;
+import org.json.JSONObject;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.text.SimpleDateFormat;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+@Service
+public class OwnBillServiceImpl implements IOwnBillService {
+
+    /**
+     * OCR appKey
+     */
+    private static String APP_KEY = "1nmSLQ4nRzQZzEznE5lbk2ja";
+
+    /**
+     * OCR secretKey
+     */
+    private static String SECRET_KEY = "d2OwQSGI1NqFXiuWuulTuXFN1fyKkkDH";
+
+    /**
+     * OCR 返回结果key
+     */
+    private static String WORDS_RESULT = "words_result";
+
+    /**
+     * OCR 验真状态key
+     */
+    private static String VERIFY_RESULT = "VerifyResult";
+
+    /**
+     * OCR 验真成功状态
+     */
+    private static String VERIFY_SUCCESS = "0001";
+
+    /**
+     * OCR文字识别token缓存时间
+     */
+    private static Long REDIS_TIMES = 2505600L;
+
+    /**
+     * redis缓存token的key
+     */
+    private static String OCR_TOKEN_KEY = "ocr_access_token";
+
+    /**
+     * 验证状态 0未验证1成功2失败
+     */
+    private static String CHECK_SUCCESS = "1";
+
+    /**
+     * 验证状态 0未验证1成功2失败
+     */
+    private static String CHECK_ERROR = "2";
+
+
+    @Autowired
+    private RedisService redisService;
+
+    @Autowired
+    private OwnBillMapper ownBillMapper;
+
+    /**
+     * 往来账款查询
+     *
+     * @return
+     */
+    @Override
+    public IPage<Map> selectBillInfList(Map map, QueryRequest request) {
+        Page page = new Page<>(request.getPageNum(), request.getPageSize());
+        return ownBillMapper.selectBillInfList(page, map);
+    }
+
+    /**
+     * 获取发票信息
+     *
+     * @return
+     */
+    @SneakyThrows
+    @Override
+    public ZcInvoiceInf getInvoice(MultipartFile file) {
+        //获取发票信息
+        JSONObject result = OcrUtil.vatInvoice(getToken(), file);
+        System.out.print("发票文字提取结果==========>" + result);
+        ZcInvoiceInf zcInvoiceInf = new ZcInvoiceInf();
+        //校验返回结果是否成功
+        if (result != null && result.getJSONObject(WORDS_RESULT) != null) {
+            JSONObject wordsResult = result.getJSONObject(WORDS_RESULT);
+            //时间格式化
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
+            //开票日期
+            String invoiceDate = wordsResult.getString("InvoiceDate");
+            zcInvoiceInf.setZiiDate(sdf.parse(invoiceDate));
+            //发票类型
+            String invoiceType = wordsResult.getString("InvoiceType");
+            //发票类型编码
+            String type = RedisUtils.getDictValue("invoice_type", invoiceType);
+            zcInvoiceInf.setZiiType(type);
+            //校验码
+            String checkCode = wordsResult.getString("CheckCode");
+            zcInvoiceInf.setZiiCheckCode(checkCode);
+            //发票代码
+            String invoiceCode = wordsResult.getString("InvoiceCode");
+            zcInvoiceInf.setZiiNo(invoiceCode);
+            //发票号码
+            String invoiceNum = wordsResult.getString("InvoiceNum");
+            zcInvoiceInf.setZiiNumber(invoiceNum);
+            //购方名称
+            String purchaserName = wordsResult.getString("PurchaserName");
+            zcInvoiceInf.setZiiPurchaserName(purchaserName);
+            //购方纳税人识别号
+            String purchaserRegisterNum = wordsResult.getString("PurchaserRegisterNum");
+            zcInvoiceInf.setZiiPurchaserNo(purchaserRegisterNum);
+            //销方名称
+            String sellerName = wordsResult.getString("SellerName");
+            zcInvoiceInf.setZiiSellerName(sellerName);
+            //销方纳税人识别号
+            String sellerRegisterNum = wordsResult.getString("SellerRegisterNum");
+            zcInvoiceInf.setZiiSellerNo(sellerRegisterNum);
+            //价税合计
+            String amountInFiguers = wordsResult.getString("AmountInFiguers");
+            zcInvoiceInf.setZiiAmount(amountInFiguers);
+            //合计不含税金额
+            String totalAmount = wordsResult.getString("TotalAmount");
+            zcInvoiceInf.setZiiExcludedAmount(totalAmount);
+            zcInvoiceInf.setZiiTotalAmount(totalAmount);
+        }
+        //返回获取文字后的发票信息
+        return zcInvoiceInf;
+    }
+
+    /**
+     * 发票验真
+     *
+     * @return
+     */
+    @SneakyThrows
+    @Override
+    public ZcInvoiceInf invoiceVerification(ZcInvoiceInf zcInvoiceInf) {
+        //时间格式化
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
+        //发票代码
+        String no = zcInvoiceInf.getZiiNo();
+        //发票号码
+        String number = zcInvoiceInf.getZiiNumber();
+        //开票日期
+        String date = sdf.format(zcInvoiceInf.getZiiDate());
+        //校验码
+        String checkCode = zcInvoiceInf.getZiiCheckCode();
+        //发票类型
+        String type = zcInvoiceInf.getZiiType();
+        //不含税金额
+        String amount = zcInvoiceInf.getZiiExcludedAmount();
+        //获取发票信息
+        JSONObject result = OcrUtil.invoiceVerification(getToken(), no, number, date, checkCode, type, amount);
+        System.out.print("发票验真结果==========>" + result);
+        //返回结果校验是否成功
+        if (result != null
+                && result.getJSONObject(WORDS_RESULT) != null
+                && VERIFY_SUCCESS.equals(result.getJSONObject(WORDS_RESULT).getString(VERIFY_RESULT))
+        ) {
+            //校验成功
+            zcInvoiceInf.setZiiCheckStt(CHECK_SUCCESS);
+        } else {
+            //校验失败
+            zcInvoiceInf.setZiiCheckStt(CHECK_ERROR);
+        }
+        return zcInvoiceInf;
+    }
+
+    /**
+     * 获取授权token
+     *
+     * @return
+     */
+    private String getToken() {
+        //授权token
+        String accessToken = redisService.getCacheObject(OCR_TOKEN_KEY);
+        if (!CommonUtil.isEmpty(accessToken)) {
+            return accessToken;
+        }
+        //授权token
+        accessToken = OcrUtil.getAuth(APP_KEY, SECRET_KEY);
+        //缓存29天
+        redisService.setCacheObject(OCR_TOKEN_KEY, accessToken, REDIS_TIMES, TimeUnit.SECONDS);
+        return accessToken;
+    }
+}

+ 5 - 0
sc-service/src/main/resources/mapper/base/ZcInvoiceInfMapper.xml

@@ -6,14 +6,19 @@
     <resultMap id="BaseResultMap" type="com.huyi.service.base.entity.ZcInvoiceInf">
         <id column="zii_id" property="ziiId" />
         <result column="zii_no" property="ziiNo" />
+        <result column="zii_number" property="ziiNumber" />
+        <result column="zii_check_code" property="ziiCheckCode" />
         <result column="zii_date" property="ziiDate" />
         <result column="zii_amount" property="ziiAmount" />
+        <result column="zii_excluded_amount" property="ziiExcludedAmount" />
         <result column="zii_type" property="ziiType" />
         <result column="zii_purchaser_name" property="ziiPurchaserName" />
         <result column="zii_purchaser_no" property="ziiPurchaserNo" />
         <result column="zii_seller_name" property="ziiSellerName" />
         <result column="zii_seller_no" property="ziiSellerNo" />
         <result column="zii_total_amount" property="ziiTotalAmount" />
+        <result column="zii_check_stt" property="ziiCheckStt" />
+        <result column="zii_status" property="ziiStatus" />
         <result column="zii_file" property="ziiFile" />
         <result column="zii_company_id" property="ziiCompanyId" />
         <result column="create_by" property="createBy" />

+ 47 - 0
sc-service/src/main/resources/mapper/bill/OwnBillMapper.xml

@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper
+PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
+"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.huyi.service.bill.mapper.OwnBillMapper">
+
+    <select id="selectBillInfList" parameterType="map" resultType="map">
+        SELECT
+            b.zbi_id		        'zbiId',
+            b.zbi_name		        'zbiName',
+            b.zbi_contract_no	    'zbiContractNo',
+            b.zbi_amount		    'zbiAmount',
+            b.zbi_date		        'zbiDate',
+            b.zbi_pay_date	        'zbiPayDate',
+            r.scy_name		        'payerName',
+            e.scy_name		        'payeeName'
+        FROM
+            zc_bill_inf b
+        LEFT JOIN sys_company r
+            ON r.scy_id = b.zbi_payer_id
+        LEFT JOIN sys_company e
+            ON e.scy_id = b.zbi_payee_id
+        WHERE 1 = 1
+            and (b.zbi_payer_id = #{paramMap.contractNo} or b.zbi_payee_id = #{paramMap.contractNo})
+        <if test="paramMap.contractNo != null  and paramMap.contractNo != ''">
+            and b.zbi_contract_no = #{paramMap.contractNo}
+        </if>
+
+        <if test="paramMap.name != null  and paramMap.name != ''">
+            and b.zbi_name like concat('%', #{paramMap.name}, '%')
+        </if>
+
+        <if test="paramMap.payerName != null  and paramMap.payerName != ''">
+            and r.scy_name like concat('%', #{paramMap.payerName}, '%')
+        </if>
+
+        <if test="paramMap.payeeName != null  and paramMap.payeeName != ''">
+            and e.scy_name like concat('%', #{paramMap.payeeName}, '%')
+        </if>
+        order by b.create_time desc
+
+    </select>
+
+
+
+
+</mapper>