|
@@ -1,21 +1,17 @@
|
|
|
package com.bz.smart_city.service.impl.pay;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.bz.smart_city.commom.model.Pagination;
|
|
|
-import com.bz.smart_city.commom.util.DESDZFP;
|
|
|
-import com.bz.smart_city.commom.util.HttpClientUtils;
|
|
|
-import com.bz.smart_city.commom.util.RandomUtil;
|
|
|
-import com.bz.smart_city.commom.util.UserUtil;
|
|
|
+import com.bz.smart_city.commom.util.*;
|
|
|
import com.bz.smart_city.dto.LoginUser;
|
|
|
import com.bz.smart_city.dto.pay.*;
|
|
|
-import com.bz.smart_city.entity.pay.PayBaseAccount;
|
|
|
import com.bz.smart_city.entity.pay.PayInvoiceParam;
|
|
|
+import com.bz.smart_city.entity.pay.PayInvoiceprinted;
|
|
|
import com.bz.smart_city.entity.pay.archives.PayBaseCustomerandmeterrela;
|
|
|
import com.bz.smart_city.service.pay.*;
|
|
|
import com.bz.smart_city.service.pay.archives.PayBaseCustomerandmeterrelaService;
|
|
|
-import com.google.gson.JsonObject;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
@@ -42,12 +38,17 @@ public class IssueElectronicInvoiceServiceImpl implements IssueElectronicInvoice
|
|
|
PayPayReceivedService payPayReceivedService;
|
|
|
@Autowired
|
|
|
PayBaseCustomerandmeterrelaService payBaseCustomerandmeterrelaService;
|
|
|
+ @Autowired
|
|
|
+ PrintInvoicePrintService printInvoicePrintService;
|
|
|
+ @Autowired
|
|
|
+ PrintInvoiceQueryResultServiceImpl printInvoiceQueryResultService;
|
|
|
+ @Autowired
|
|
|
+ private SnowflakeIdWorker idWorker;
|
|
|
|
|
|
- private Map<String, String> getPrintParam() {
|
|
|
+ private Map<String, String> getPrintParam(Integer siteId, Integer customerId) {
|
|
|
Map<String, String> map = new HashMap<>();
|
|
|
//查询销方参数
|
|
|
- Pagination<PayBaseConfigDto> payBaseConfigDtoPagination = payBaseConfigService.getAll("PRINT_", 1, 15);
|
|
|
- List<PayBaseConfigDto> payBaseConfigDtos = payBaseConfigDtoPagination.getList();
|
|
|
+ List<PayBaseConfigDto> payBaseConfigDtos = payBaseConfigService.getPrintInfo("PRINT_", siteId, customerId);
|
|
|
for (PayBaseConfigDto item : payBaseConfigDtos) {
|
|
|
switch (item.getName().trim()) {
|
|
|
case "PRINT_INVOICE_ADDRESS":
|
|
@@ -76,9 +77,9 @@ public class IssueElectronicInvoiceServiceImpl implements IssueElectronicInvoice
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
- private Map<Integer, PayInvoiceParam> getInvoiceParam() {
|
|
|
+ private Map<Integer, PayInvoiceParam> getInvoiceParam(Integer siteId, Integer customerId) {
|
|
|
Map<Integer, PayInvoiceParam> map = new HashMap<>();
|
|
|
- List<PayInvoiceParam> payInvoiceParams = payInvoiceParamService.findList(null);
|
|
|
+ List<PayInvoiceParam> payInvoiceParams = payInvoiceParamService.findList(null, siteId, customerId);
|
|
|
for (PayInvoiceParam item : payInvoiceParams) {
|
|
|
map.put(item.getFeetype(), item);
|
|
|
}
|
|
@@ -90,82 +91,114 @@ public class IssueElectronicInvoiceServiceImpl implements IssueElectronicInvoice
|
|
|
return a;
|
|
|
}
|
|
|
|
|
|
- public BigDecimal saveEightDecimal(BigDecimal d) {
|
|
|
- BigDecimal a = d.setScale(8, BigDecimal.ROUND_HALF_UP);
|
|
|
- return a;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
- * @MethodName:
|
|
|
- * @Description: TODO
|
|
|
+ * @return
|
|
|
+ * @MethodName:请求开票
|
|
|
+ * @Description: 请求开票
|
|
|
* @Param: kpType 开票类型 1 正票 2 红票 userCode 用户号 payseriesno 缴费流水号
|
|
|
* @Return:
|
|
|
* @Author:
|
|
|
* @Date: 2020/7/28
|
|
|
- **/
|
|
|
- public void requestPrint(String kpType, String userCode, String payseriesno) {
|
|
|
+ */
|
|
|
+ public ReturnPrintResultDto requestPrint(String kpType, String userCode, String payseriesno, String invoicePrintId){
|
|
|
LoginUser loginUser = UserUtil.getCurrentUser();
|
|
|
String name = loginUser.getName();//操作员姓名
|
|
|
- BigInteger siteId = BigInteger.valueOf(loginUser.getSiteId());
|
|
|
- BigInteger customerId = BigInteger.valueOf(loginUser.getCustomerId());
|
|
|
-
|
|
|
- Map<String, String> invoiceParam = getPrintParam();//发票参数
|
|
|
- PayEleInvoiceDto payEleInvoiceDto = new PayEleInvoiceDto();
|
|
|
- payEleInvoiceDto.setIdentity(invoiceParam.get("saleIdentity"));
|
|
|
-
|
|
|
- Map<Integer, PayInvoiceParam> invoiceMap = getInvoiceParam();//税率相关参数
|
|
|
|
|
|
+ ReturnPrintResultDto returnPrintResultDto = new ReturnPrintResultDto();//开票返回结果信息
|
|
|
PayInvoiceOrderDto payInvoiceOrderDto = new PayInvoiceOrderDto();//开票订单信息
|
|
|
+ PayEleInvoiceDto payEleInvoiceDto = new PayEleInvoiceDto();//电子发票请求主体信息
|
|
|
+ PayInvoiceprinted oldInvoiceprinted = new PayInvoiceprinted();//原始蓝票信息(红票时处理)
|
|
|
+ List<PayInvoiceOrderDetailDto> orderDetailDtos = new ArrayList<>();//蓝票详细信息
|
|
|
+ Map<String, String> invoiceParam = getPrintParam(loginUser.getSiteId(), loginUser.getCustomerId());//销方开票信息
|
|
|
+ Map<Integer, PayInvoiceParam> invoiceMap = getInvoiceParam(loginUser.getSiteId(), loginUser.getCustomerId());//税率相关参数
|
|
|
+ payEleInvoiceDto.setIdentity(invoiceParam.get("saleIdentity"));//认证信息
|
|
|
+
|
|
|
payInvoiceOrderDto.setSaleaccount(invoiceParam.get("saleAccount"));//销方银行账号
|
|
|
payInvoiceOrderDto.setSalephone(invoiceParam.get("salePhone"));//销方电话
|
|
|
payInvoiceOrderDto.setSaleaddress(invoiceParam.get("saleAddress"));//销方地址
|
|
|
payInvoiceOrderDto.setSaletaxnum(invoiceParam.get("saleCode"));//销方税号
|
|
|
payInvoiceOrderDto.setChecker(invoiceParam.get("saleChecker"));//复核人
|
|
|
- if(name.length()>4) {
|
|
|
+ payInvoiceOrderDto.setKptype(kpType);//开票类型
|
|
|
+ payInvoiceOrderDto.setTsfs("2");//推送方式
|
|
|
+ payInvoiceOrderDto.setInvoicedate(LocalDateTime.now());//开票时间
|
|
|
+ payInvoiceOrderDto.setOrderno(RandomUtil.getRandomString());//订单号
|
|
|
+
|
|
|
+ if (name.length() > 4) {
|
|
|
name = "*" + name.substring(name.length() - 3, name.length());
|
|
|
payInvoiceOrderDto.setClerk(name);//开票人
|
|
|
payInvoiceOrderDto.setPayee(name);//收款人
|
|
|
}
|
|
|
|
|
|
- payInvoiceOrderDto.setKptype(kpType);//开票类型
|
|
|
- payInvoiceOrderDto.setTsfs("2");//推送方式
|
|
|
- payInvoiceOrderDto.setInvoicedate(LocalDateTime.now());
|
|
|
- payInvoiceOrderDto.setOrderno(RandomUtil.getRandomString());
|
|
|
+ if (kpType.equals("2")) {//红票按蓝票票ID查询
|
|
|
+ oldInvoiceprinted = printInvoicePrintService.findInvoiceByPayseriesno(invoicePrintId, BigInteger.valueOf(loginUser.getSiteId()), BigInteger.valueOf(loginUser.getCustomerId()));
|
|
|
+ if (oldInvoiceprinted != null && oldInvoiceprinted.getInvoiceCode() != null) {
|
|
|
+ payInvoiceOrderDto.setFpdm(oldInvoiceprinted.getInvoiceCode());
|
|
|
+ payInvoiceOrderDto.setFphm(oldInvoiceprinted.getInvoiceNo());
|
|
|
+ payInvoiceOrderDto.setMessage("对应正数发票代码:" + oldInvoiceprinted.getInvoiceCode() + "号码:" + oldInvoiceprinted.getInvoiceNo());
|
|
|
+ payseriesno = oldInvoiceprinted.getPayseriesno();
|
|
|
+ } else {
|
|
|
+ returnPrintResultDto.setResult("1");//开票失败
|
|
|
+ returnPrintResultDto.setMsg("开票失败,失败原因:冲红蓝票信息缺失");
|
|
|
+ return returnPrintResultDto;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
//查询购方相关信息
|
|
|
- PayBaseCustomerandmeterrela payBaseAccount = payBaseCustomerandmeterrelaService.findAccountByAccountNumber(userCode);
|
|
|
+ PayBaseCustomerandmeterrela payBaseAccount = payBaseCustomerandmeterrelaService.findAccountByAccountNumber(userCode, loginUser.getSiteId(), loginUser.getCustomerId());
|
|
|
+ if (payBaseAccount == null) {
|
|
|
+ returnPrintResultDto.setResult("1");//开票失败
|
|
|
+ returnPrintResultDto.setMsg("开票失败,失败原因:购方信息缺失");
|
|
|
+ return returnPrintResultDto;
|
|
|
+ }
|
|
|
payInvoiceOrderDto.setBuyername(payBaseAccount.getAccountname());//购方名称
|
|
|
payInvoiceOrderDto.setPhone(payBaseAccount.getTelephone());//电话
|
|
|
payInvoiceOrderDto.setAddress(payBaseAccount.getAddress());//地址
|
|
|
payInvoiceOrderDto.setAccount(payBaseAccount.getBankaccount());//银行账号
|
|
|
payInvoiceOrderDto.setTelephone(payBaseAccount.getTelephone());//电话
|
|
|
+ payInvoiceOrderDto.setTaxnum(payBaseAccount.getVatno());//购方税号
|
|
|
|
|
|
- List<PayInvoiceOrderDetailDto> orderDetailDtos = new ArrayList<>();
|
|
|
//循环加入明细
|
|
|
List<PayReceivedInvoiceDto> payReceivedInvoiceDtos = payPayReceivedService.findInvoiceReceivedByPayseriesno(payseriesno, loginUser.getSiteId(), loginUser.getCustomerId());
|
|
|
- //将水费分开,其他类型水费合并todo
|
|
|
for (PayReceivedInvoiceDto item : payReceivedInvoiceDtos) {
|
|
|
PayInvoiceOrderDetailDto orderDetailDto = new PayInvoiceOrderDetailDto();
|
|
|
PayInvoiceParam payInvoiceParam = invoiceMap.get(item.getFeetype());
|
|
|
orderDetailDto.setGoodsname(item.getFeetypename());
|
|
|
- orderDetailDto.setTaxamt(String.valueOf(saveTwoDecimal(item.getReceivedamount())));//含税金额
|
|
|
+ if (kpType.equals("1")) {//蓝票
|
|
|
+ orderDetailDto.setTaxamt(String.valueOf(saveTwoDecimal(item.getReceivedamount())));//含税金额
|
|
|
+ } else {
|
|
|
+ orderDetailDto.setTaxamt(String.valueOf(saveTwoDecimal(item.getReceivedamount().multiply(BigDecimal.valueOf(-1)))));//含税金额
|
|
|
+ }
|
|
|
//含税标志为1 含税,税额=round((数量*含税单价)*税率/(1+税率)),2),不含税金额=含税金额-税额
|
|
|
//含税标志为0 不含税,税额=round((数量*不含税单价)*税率),2),含税金额=不含税金额+税额
|
|
|
BigDecimal taxAmount = new BigDecimal(0);//税额
|
|
|
BigDecimal receAmount = item.getReceivedamount();//实收金额
|
|
|
BigDecimal rate = new BigDecimal(payInvoiceParam.getTaxrate());//税率
|
|
|
if (payInvoiceParam.getPricetax().equals("1")) {
|
|
|
- taxAmount = saveTwoDecimal(receAmount.multiply(rate).divide(rate.add(BigDecimal.valueOf(1)),2, BigDecimal.ROUND_HALF_UP));
|
|
|
- orderDetailDto.setTax(String.valueOf(saveTwoDecimal(taxAmount)));//税额
|
|
|
- orderDetailDto.setTaxfreeamt(String.valueOf(saveTwoDecimal(receAmount.subtract(taxAmount))));//不含税金额
|
|
|
+ taxAmount = saveTwoDecimal(receAmount.multiply(rate).divide(rate.add(BigDecimal.valueOf(1)), 2, BigDecimal.ROUND_HALF_UP));
|
|
|
+ if (kpType.equals("2")) {
|
|
|
+ orderDetailDto.setTax(String.valueOf(saveTwoDecimal(taxAmount.multiply(BigDecimal.valueOf(-1)))));//税额
|
|
|
+ orderDetailDto.setTaxfreeamt(String.valueOf(saveTwoDecimal(receAmount.subtract(taxAmount).multiply(BigDecimal.valueOf(-1)))));//不含税金额
|
|
|
+ } else {
|
|
|
+ orderDetailDto.setTax(String.valueOf(saveTwoDecimal(taxAmount)));//税额
|
|
|
+ orderDetailDto.setTaxfreeamt(String.valueOf(saveTwoDecimal(receAmount.subtract(taxAmount))));//不含税金额
|
|
|
+ }
|
|
|
} else {
|
|
|
taxAmount = saveTwoDecimal(receAmount.multiply(rate));
|
|
|
- orderDetailDto.setTax(String.valueOf(saveTwoDecimal(taxAmount)));//税额
|
|
|
- orderDetailDto.setTaxfreeamt(String.valueOf(saveTwoDecimal(receAmount.add(taxAmount))));//不含税金额
|
|
|
+ if (kpType.equals("2")) {
|
|
|
+ orderDetailDto.setTax(String.valueOf(saveTwoDecimal(taxAmount)));//税额
|
|
|
+ orderDetailDto.setTaxfreeamt(String.valueOf(saveTwoDecimal(receAmount.add(taxAmount).multiply(BigDecimal.valueOf(-1)))));//不含税金额
|
|
|
+ } else {
|
|
|
+ orderDetailDto.setTax(String.valueOf(saveTwoDecimal(taxAmount)));//税额
|
|
|
+ orderDetailDto.setTaxfreeamt(String.valueOf(saveTwoDecimal(receAmount.add(taxAmount).multiply(BigDecimal.valueOf(-1)))));//不含税金额
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
if (item.getPrice() != null) {
|
|
|
orderDetailDto.setPrice(String.valueOf(saveTwoDecimal(item.getPrice())));
|
|
|
- orderDetailDto.setNum(String.valueOf(receAmount.divide(saveTwoDecimal(item.getPrice()), 8, BigDecimal.ROUND_HALF_UP)));
|
|
|
+ if (kpType.equals("1")) {
|
|
|
+ orderDetailDto.setNum(String.valueOf(receAmount.divide(saveTwoDecimal(item.getPrice()), 8, BigDecimal.ROUND_HALF_UP)));
|
|
|
+ } else {
|
|
|
+ orderDetailDto.setNum(String.valueOf(receAmount.divide(saveTwoDecimal(item.getPrice()), 8, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(-1))));
|
|
|
+ }
|
|
|
}
|
|
|
if (!payInvoiceParam.getZerotax().equals("0")) {
|
|
|
orderDetailDto.setLslbs(payInvoiceParam.getZerotax());//零税率标志
|
|
@@ -183,8 +216,13 @@ public class IssueElectronicInvoiceServiceImpl implements IssueElectronicInvoice
|
|
|
|
|
|
orderDetailDtos.add(orderDetailDto);
|
|
|
}
|
|
|
+ if (orderDetailDtos.size() == 0) {
|
|
|
+ returnPrintResultDto.setResult("1");//开票失败
|
|
|
+ returnPrintResultDto.setMsg("开票失败,失败原因:发票详细信息缺失");
|
|
|
+ return returnPrintResultDto;
|
|
|
+ }
|
|
|
payInvoiceOrderDto.setDetail(orderDetailDtos);
|
|
|
- payInvoiceOrderDto.setEmail("wangyangyang@hxiswater.com");
|
|
|
+ payInvoiceOrderDto.setEmail(invoiceParam.get("saleEmail"));
|
|
|
payEleInvoiceDto.setOrder(payInvoiceOrderDto);
|
|
|
//发送开票请求
|
|
|
String jsonsString = DESDZFP.encrypt(JSON.toJSONString(payEleInvoiceDto));
|
|
@@ -192,9 +230,55 @@ public class IssueElectronicInvoiceServiceImpl implements IssueElectronicInvoice
|
|
|
map.put("order", jsonsString);
|
|
|
try {
|
|
|
String returnString = HttpClientUtils.doPost("http://nnfpbox.nuonuocs.cn/shop/buyer/allow/cxfKp/cxfServerKpOrderSync.action", map);
|
|
|
- System.out.println("returnString:" + returnString);
|
|
|
+ JSONObject jsonObject = (JSONObject) JSON.parse(returnString);
|
|
|
+ PayInvoiceprinted payInvoiceprinted = new PayInvoiceprinted();//发票信息
|
|
|
+ payInvoiceprinted.setTaxnum(payInvoiceOrderDto.getTaxnum());
|
|
|
+ payInvoiceprinted.setSendtype(2);
|
|
|
+ payInvoiceprinted.setInvoiceType(1);
|
|
|
+ payInvoiceprinted.setPrintNo(payInvoiceOrderDto.getOrderno());
|
|
|
+ payInvoiceprinted.setOrderNo(payInvoiceOrderDto.getOrderno());
|
|
|
+ payInvoiceprinted.setPayseriesno(payseriesno);
|
|
|
+ payInvoiceprinted.setAccountId(payBaseAccount.getAccountId());
|
|
|
+ payInvoiceprinted.setCreateBy(BigInteger.valueOf(loginUser.getId()));
|
|
|
+ payInvoiceprinted.setCreateDate(LocalDateTime.now());
|
|
|
+ payInvoiceprinted.setUpdateBy(BigInteger.valueOf(loginUser.getId()));
|
|
|
+ payInvoiceprinted.setUpdateDate(LocalDateTime.now());
|
|
|
+ payInvoiceprinted.setSiteId(BigInteger.valueOf(loginUser.getSiteId()));
|
|
|
+ payInvoiceprinted.setCustomerId(BigInteger.valueOf(loginUser.getCustomerId()));
|
|
|
+ payInvoiceprinted.setDelFlag("0");
|
|
|
+ payInvoiceprinted.setRemarks(jsonObject.getString("message"));
|
|
|
+ payInvoiceprinted.setType(1); //发票
|
|
|
+ payInvoiceprinted.setInvoiceType(1); //蓝票
|
|
|
+ payInvoiceprinted.setInvoiceLine("p");
|
|
|
+ payInvoiceprinted.setUserCode(userCode);
|
|
|
+ payInvoiceprinted.setSaleIdentity(invoiceParam.get("saleIdentity"));
|
|
|
+ if (kpType.equals("2")) {
|
|
|
+ payInvoiceprinted.setInvoiceType(2);//红票
|
|
|
+ payInvoiceprinted.setCancelInvoiceId(BigInteger.valueOf(Long.parseLong(invoicePrintId)));
|
|
|
+ payInvoiceprinted.setOldInvoiceCode(oldInvoiceprinted.getInvoiceCode());
|
|
|
+ payInvoiceprinted.setOldInvoiceNo(oldInvoiceprinted.getInvoiceNo());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (jsonObject.getString("status").equals("0000"))//开票成功
|
|
|
+ {
|
|
|
+ payInvoiceprinted.setId(BigInteger.valueOf(idWorker.nextId()));
|
|
|
+ payInvoiceprinted.setState(0);//待生成
|
|
|
+ payInvoiceprinted.setFpqqlsh(jsonObject.getString("fpqqlsh"));
|
|
|
+ returnPrintResultDto.setResult("0");//开票失败
|
|
|
+ returnPrintResultDto.setMsg("开票成功,请等待系统票据查询结果");
|
|
|
+ //开票成功的情况,新保存开票的相关信息,查询时再进行更新
|
|
|
+ int num = printInvoicePrintService.insert(payInvoiceprinted);
|
|
|
+ //异步查询结果,根据查询结果进行更新票据相关信息
|
|
|
+ printInvoiceQueryResultService.queryPrintInvoiceInfo(payInvoiceprinted);
|
|
|
+ } else {//开票失败
|
|
|
+ payInvoiceprinted.setState(4);//开票失败
|
|
|
+ returnPrintResultDto.setResult("1");//开票失败
|
|
|
+ returnPrintResultDto.setMsg("开票失败,失败原因:" + jsonObject.getString("message"));
|
|
|
+ }
|
|
|
} catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ returnPrintResultDto.setResult("1");//开票失败
|
|
|
+ returnPrintResultDto.setMsg("开票失败,失败原因:" + e.getMessage());
|
|
|
}
|
|
|
+ return returnPrintResultDto;
|
|
|
}
|
|
|
}
|