|
@@ -1,384 +0,0 @@
|
|
-package com.iamberry.rst.util;
|
|
|
|
-
|
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
|
-import com.alibaba.fastjson.JSONArray;
|
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
|
-import com.iamberry.file.DownloadFile;
|
|
|
|
-import com.iamberry.nuonuo.bean.PrivateData;
|
|
|
|
-import com.iamberry.nuonuo.bean.PublicData;
|
|
|
|
-import com.iamberry.nuonuo.bean.RequestMode;
|
|
|
|
-import com.iamberry.nuonuo.exception.OpensnsException;
|
|
|
|
-import com.iamberry.nuonuo.oauth.Oauth2AccessToken;
|
|
|
|
-import com.iamberry.nuonuo.service.OpenApiV1;
|
|
|
|
-import com.iamberry.nuonuo.util.StateData;
|
|
|
|
-import com.iamberry.rst.core.ei.Billing;
|
|
|
|
-import com.iamberry.rst.core.ei.BillingItem;
|
|
|
|
-import com.iamberry.rst.core.ei.InvoPlatform;
|
|
|
|
-import com.iamberry.rst.core.ei.PlatformInfo;
|
|
|
|
-import com.iamberry.rst.faces.ei.BillingService;
|
|
|
|
-import com.iamberry.rst.faces.ei.InvoPlatformService;
|
|
|
|
-import com.iamberry.rst.faces.ei.PlatformInfoService;
|
|
|
|
-import com.iamberry.wechat.tools.NameUtils;
|
|
|
|
-import org.slf4j.Logger;
|
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
|
-
|
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
|
-import java.util.*;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- * 诺诺开票系统
|
|
|
|
- */
|
|
|
|
-@Service
|
|
|
|
-public class NuoNuoServiceUtil {
|
|
|
|
- private Logger logger = LoggerFactory.getLogger(NuoNuoServiceUtil.class);
|
|
|
|
- private static String NUONUO_URL = NameUtils.getConfig("NUONUO_URL");
|
|
|
|
- private static Integer NUONUO_ID = Integer.valueOf(NameUtils.getConfig("NUONUO_ID"));
|
|
|
|
-
|
|
|
|
- private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
-
|
|
|
|
- @Autowired
|
|
|
|
- private InvoPlatformService invoPlatformService;
|
|
|
|
- @Autowired
|
|
|
|
- private PlatformInfoService platformInfoService;
|
|
|
|
- @Autowired
|
|
|
|
- private BillingService billingService;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- * 开票
|
|
|
|
- * @param billing
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public String nuoNInvo(Billing billing){
|
|
|
|
- InvoPlatform invoPlatform = invoPlatformService.getInvoPlatformById(billing.getInvoPlatId());
|
|
|
|
- PlatformInfo platformInfo = platformInfoService.getPlatformInfoById(billing.getPlatId());
|
|
|
|
-
|
|
|
|
- if(StateData.app_id != billing.getInvoPlatId()){
|
|
|
|
- this.setHeader(invoPlatform,platformInfo);
|
|
|
|
- }
|
|
|
|
- String msg = this.setToken(invoPlatform);
|
|
|
|
- if(!"success".equals(msg)){
|
|
|
|
- return msg;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- Map<String,String> headers = getHeaders();
|
|
|
|
- PublicData pdData = getPublicData("nuonuo.electronInvoice.requestBilling");
|
|
|
|
- PrivateData<Object> pvData = new PrivateData<Object>();
|
|
|
|
- List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
|
|
|
|
- List<Map<String,Object>> list_invoiceDetail = new ArrayList<Map<String,Object>>();
|
|
|
|
- Map<String,Object> map_s = new HashMap<String,Object>();
|
|
|
|
- Map<String,Object> map_order = new HashMap<String,Object>();
|
|
|
|
-
|
|
|
|
- map_order.put("buyerName", billing.getBillCompanyName());
|
|
|
|
- map_order.put("buyerTaxNum", billing.getBillTaxNumber());
|
|
|
|
- if(billing.getBillTel() == null || "".equals(billing.getBillTel())){
|
|
|
|
- map_order.put("buyerPhone", "13265476840");
|
|
|
|
- }else{
|
|
|
|
- map_order.put("buyerPhone", billing.getBillTel());
|
|
|
|
- }
|
|
|
|
- map_order.put("buyerAddress", billing.getBillAddress());
|
|
|
|
-
|
|
|
|
- String billBankInfo = getStr(billing.getBillBankName()) + getStr(billing.getBillBankNumber());
|
|
|
|
- map_order.put("buyerAccount", billBankInfo);
|
|
|
|
- map_order.put("orderNo", billing.getBillOrderNo());
|
|
|
|
-
|
|
|
|
- if(billing.getBillOrderDate() == null){
|
|
|
|
- map_order.put("invoiceDate", sdf.format(new Date()));
|
|
|
|
- }else{
|
|
|
|
- map_order.put("invoiceDate", sdf.format(billing.getBillOrderDate()));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- map_order.put("clerk", platformInfo.getPlatBilling());
|
|
|
|
- String platBankInfo = getStr(platformInfo.getPlatBankName()) + getStr(platformInfo.getPlatBankNumber());
|
|
|
|
- map_order.put("salerAccount", platBankInfo);
|
|
|
|
- map_order.put("salerTel", platformInfo.getPlatTel());
|
|
|
|
- map_order.put("salerAddress", platformInfo.getPlatAddress());
|
|
|
|
-
|
|
|
|
- if(1 == NUONUO_ID){
|
|
|
|
- map_order.put("salerTaxNum", "339901999999142");
|
|
|
|
- }else{
|
|
|
|
- map_order.put("salerTaxNum", platformInfo.getPlatTaxNumber());
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- map_order.put("invoiceType", "1");
|
|
|
|
- map_order.put("remark", "");
|
|
|
|
- map_order.put("payee", platformInfo.getPlatPayee());
|
|
|
|
- map_order.put("checker",platformInfo.getPlatReviewer());
|
|
|
|
- map_order.put("invoiceCode", "");
|
|
|
|
- map_order.put("invoiceNum", "");
|
|
|
|
- map_order.put("pushMode", "1");
|
|
|
|
- map_order.put("email", "");
|
|
|
|
- map_order.put("listFlag", "1");
|
|
|
|
- map_order.put("listName", "详见销货清单");
|
|
|
|
- map_order.put("buyerTel", getStr(billing.getBillTel()));
|
|
|
|
- map_order.put("proxyInvoiceFlag", "0");
|
|
|
|
- map_order.put("departmentId", "");
|
|
|
|
- map_order.put("clerkId", "");
|
|
|
|
-
|
|
|
|
- for (BillingItem billingItem : billing.getBillingItemList()) {
|
|
|
|
- Map<String,Object> map_invoiceDetails = new HashMap<String,Object>();
|
|
|
|
- map_invoiceDetails.put("goodsName", billingItem.getBillItemProductName()+"("+ billingItem.getBillItemColorName() +")");
|
|
|
|
- map_invoiceDetails.put("num", billingItem.getBillItemNum());
|
|
|
|
- map_invoiceDetails.put("withTaxFlag", "1");
|
|
|
|
- map_invoiceDetails.put("price", Double.valueOf(billingItem.getBillItemUnitPrice())/100);
|
|
|
|
- map_invoiceDetails.put("taxRate", "0.13");
|
|
|
|
- map_invoiceDetails.put("specType", "");
|
|
|
|
- map_invoiceDetails.put("unit", "");
|
|
|
|
- map_invoiceDetails.put("goodsCode", billingItem.getBillItemColorBar());
|
|
|
|
- map_invoiceDetails.put("selfCode", billingItem.getJdProductId());
|
|
|
|
- map_invoiceDetails.put("invoiceLineProperty", "0");
|
|
|
|
- map_invoiceDetails.put("favouredPolicyFlag", "0");
|
|
|
|
- map_invoiceDetails.put("favouredPolicyName", null);
|
|
|
|
- map_invoiceDetails.put("zeroRateFlag", null);
|
|
|
|
- map_invoiceDetails.put("deduction", null);
|
|
|
|
-
|
|
|
|
- map_invoiceDetails.put("taxIncludedAmount", null);
|
|
|
|
- map_invoiceDetails.put("taxExcludedAmount", null);
|
|
|
|
- list_invoiceDetail.add(map_invoiceDetails);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- map_order.put("invoiceDetail", list_invoiceDetail);
|
|
|
|
- map_order.put("invoiceLine", "p");
|
|
|
|
- map_order.put("productOilFlag", "0");
|
|
|
|
- map_s.put("order", map_order);
|
|
|
|
- list.add(map_s);
|
|
|
|
- pvData.setServicedata(list);
|
|
|
|
- RequestMode requestMode = getRequestMode(pdData,pvData);
|
|
|
|
- OpenApiV1 sdk = new OpenApiV1();
|
|
|
|
- String result = "";
|
|
|
|
- try {
|
|
|
|
- result = sdk.handle(NUONUO_URL,headers, requestMode);
|
|
|
|
- String invoiceSerialNum = "";
|
|
|
|
- Integer status = 0;
|
|
|
|
- JSONObject jsonObject = JSON.parseObject(result);
|
|
|
|
- String code = jsonObject.getString("code");
|
|
|
|
- if("E0000".equals(code)){
|
|
|
|
- status = 1;
|
|
|
|
- JSONObject jo = jsonObject.getJSONObject("result");
|
|
|
|
- invoiceSerialNum = jo.getString("invoiceSerialNum");
|
|
|
|
- msg = "success";
|
|
|
|
- }else if("E9106".equals(code)){
|
|
|
|
- status = 1;
|
|
|
|
- msg = "success";
|
|
|
|
- }else{
|
|
|
|
- msg = "发票生成错误:"+result;
|
|
|
|
- }
|
|
|
|
- Billing bl = new Billing();
|
|
|
|
- bl.setBillId(billing.getBillId());
|
|
|
|
- bl.setBillInvoiceSerialNum(invoiceSerialNum);
|
|
|
|
- bl.setBillResultMsg(msg);
|
|
|
|
- bl.setBillStatus(status);
|
|
|
|
- Integer flag = billingService.update(bl);
|
|
|
|
- } catch (OpensnsException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- msg = "发票生成错误:"+result;
|
|
|
|
- }
|
|
|
|
- return msg;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- * 获取电子发票
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public boolean invoGetByNuoNuo(String path){
|
|
|
|
- boolean flag = true;
|
|
|
|
-
|
|
|
|
- Billing bl = new Billing();
|
|
|
|
- bl.setBillStatus(1);
|
|
|
|
- List<Billing> billingList = billingService.getBillingList(bl);
|
|
|
|
- if(billingList != null && billingList.size() > 0){
|
|
|
|
- List<String> list_s = new ArrayList<>();
|
|
|
|
- Integer platId = 0;
|
|
|
|
- for (Billing bi:billingList) {
|
|
|
|
- platId = bi.getPlatId();
|
|
|
|
- list_s.add(bi.getBillInvoiceSerialNum());
|
|
|
|
- }
|
|
|
|
- invoGetByNuoNuoList(NUONUO_ID,platId,list_s,path);
|
|
|
|
- }
|
|
|
|
- return flag;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- * 获取电子发票
|
|
|
|
- * @param InvoPlatId 诺诺平台在数据库的id
|
|
|
|
- * @param platId 所属平台
|
|
|
|
- * @param list_s
|
|
|
|
- * @param path E:\java-work\iamberry-work\watero-rst-web\target\watero-rst-web\common
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- public void invoGetByNuoNuoList(Integer InvoPlatId,Integer platId, List<String> list_s,String path){
|
|
|
|
- InvoPlatform invoPlatform = invoPlatformService.getInvoPlatformById(platId);
|
|
|
|
- PlatformInfo platformInfo = platformInfoService.getPlatformInfoById(InvoPlatId);
|
|
|
|
- if(StateData.app_id != InvoPlatId){
|
|
|
|
- this.setHeader(invoPlatform,platformInfo);
|
|
|
|
- }
|
|
|
|
- this.setToken(invoPlatform);
|
|
|
|
-
|
|
|
|
- Map<String,String> headers = getHeaders();
|
|
|
|
- PublicData pdData = getPublicData("nuonuo.electronInvoice.CheckEInvoice");
|
|
|
|
- PrivateData<Object> pvData = new PrivateData<Object>();
|
|
|
|
- List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
|
|
|
|
- Map<String,Object> map_s = new HashMap<String,Object>();
|
|
|
|
- map_s.put("invoiceSerialNum", list_s);
|
|
|
|
- list.add(map_s);
|
|
|
|
- pvData.setServicedata(list);
|
|
|
|
- RequestMode requestMode = getRequestMode(pdData,pvData);
|
|
|
|
- OpenApiV1 sdk = new OpenApiV1();
|
|
|
|
- String result = null;
|
|
|
|
- try {
|
|
|
|
- result = sdk.handle(NUONUO_URL,headers, requestMode);
|
|
|
|
- JSONObject jsonObject = JSON.parseObject(result);
|
|
|
|
- String res = jsonObject.getString("result");
|
|
|
|
- JSONArray jsonArray = JSON.parseArray(res);
|
|
|
|
- for (int i=0;i<jsonArray.size();i++){
|
|
|
|
- JSONObject jo = jsonArray.getJSONObject(i);
|
|
|
|
- Integer status = jo.getInteger("status");
|
|
|
|
- String orderNo = jo.getString("orderNo");
|
|
|
|
- String statusMsg = jo.getString("statusMsg");
|
|
|
|
- Billing billing = new Billing();
|
|
|
|
- billing.setBillOrderNo(orderNo);
|
|
|
|
- billing.setBillResultMsg(statusMsg);
|
|
|
|
- if(2 == status){
|
|
|
|
- String invoiceFileUrl = jo.getString("invoiceFileUrl");
|
|
|
|
- String invoiceDate = jo.getString("invoiceDate");
|
|
|
|
- String invoiceLine = jo.getString("invoiceLine");
|
|
|
|
- String invoiceCode = jo.getString("invoiceCode");
|
|
|
|
- String invoiceNum = jo.getString("invoiceNum");
|
|
|
|
- Double taxExcludedAmount = jo.getDouble("taxExcludedAmount")*100;
|
|
|
|
- Double taxIncludedAmount = jo.getDouble("taxIncludedAmount")*100;
|
|
|
|
-
|
|
|
|
- String pdfUnitPath = "common/ei/pdf";
|
|
|
|
-
|
|
|
|
- long start = System.currentTimeMillis();
|
|
|
|
- DownloadFile.downLoadByUrl(invoiceFileUrl,orderNo+".pdf",path+pdfUnitPath);
|
|
|
|
- long end = System.currentTimeMillis();
|
|
|
|
- System.out.println("下载pdf时间为:"+ (end-start));
|
|
|
|
-
|
|
|
|
- String pdfFilePath = pdfUnitPath+ "/" + orderNo+".pdf";
|
|
|
|
- billing.setBillStatus(status);
|
|
|
|
- billing.setBillInvoiceFileUrl(pdfFilePath);
|
|
|
|
- billing.setBillInvoiceImageUrl(invoiceFileUrl);
|
|
|
|
- billing.setBillInvoiceDate(sdf.parse(invoiceDate));
|
|
|
|
- billing.setBillInvoiceLine(invoiceLine);
|
|
|
|
- billing.setBillTaxExcludedAmount(Integer.valueOf(taxExcludedAmount.intValue()));
|
|
|
|
- billing.setBillTaxIncludedAmount(Integer.valueOf(taxIncludedAmount.intValue()));
|
|
|
|
- billing.setBillInvoiceCode(invoiceCode);
|
|
|
|
- billing.setBillInvoiceNum(invoiceNum);
|
|
|
|
- Integer flag = billingService.updateByOrderNo(billing);
|
|
|
|
- if(flag < 1){
|
|
|
|
- logger.info("发票修改失败:"+result);
|
|
|
|
- billing.setBillStatus(3);
|
|
|
|
- }
|
|
|
|
- }else{
|
|
|
|
- logger.info("订单编号:"+orderNo+",发票还在生成中!");
|
|
|
|
- billing.setBillStatus(status);
|
|
|
|
- }
|
|
|
|
- Integer flag = billingService.updateByOrderNo(billing);
|
|
|
|
- }
|
|
|
|
- } catch (OpensnsException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- logger.info("获取发票信息失败:"+list_s.toString());
|
|
|
|
- }catch (Exception e){
|
|
|
|
- e.printStackTrace();
|
|
|
|
- logger.info("获取发票信息失败:"+result);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- * 如果token过期,获取新的token
|
|
|
|
- * @param invoPlatform
|
|
|
|
- * @return
|
|
|
|
- */
|
|
|
|
- private String setToken(InvoPlatform invoPlatform){
|
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
|
- calendar.add(Calendar.DAY_OF_MONTH, 1);
|
|
|
|
- if(invoPlatform.getInvoPlatEndDate() == null || invoPlatform.getInvoPlatEndDate().before(new Date())){
|
|
|
|
- StateData.grant_type_token = "authorization_code";
|
|
|
|
- String msg = Oauth2AccessToken.getAccessToken();
|
|
|
|
- String token = "";
|
|
|
|
-
|
|
|
|
- try{
|
|
|
|
- JSONObject jsonObject = JSON.parseObject(msg);
|
|
|
|
- token = jsonObject.getString("access_token");
|
|
|
|
- if(token == null || "".equals(token)){
|
|
|
|
- throw new Exception();
|
|
|
|
- }
|
|
|
|
- }catch (Exception e){
|
|
|
|
- logger.info("获取新token失败,获取token结果:"+msg);
|
|
|
|
- return "获取新token失败,获取token结果:"+msg;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- InvoPlatform ip = new InvoPlatform();
|
|
|
|
- ip.setInvoPlatId(invoPlatform.getInvoPlatId());
|
|
|
|
- ip.setInvoPlatAccessToken(token);
|
|
|
|
- ip.setInvoPlatEndDate(calendar.getTime());
|
|
|
|
- Integer flag = invoPlatformService.update(ip);
|
|
|
|
- if(flag < 0){
|
|
|
|
- logger.info("刷新诺诺token失败");
|
|
|
|
- return "刷新诺诺token失败";
|
|
|
|
- }
|
|
|
|
- StateData.app_accessToken = token;
|
|
|
|
- }
|
|
|
|
- return "success";
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- * 设置key
|
|
|
|
- * @param invoPlatform
|
|
|
|
- * @param platformInfo
|
|
|
|
- */
|
|
|
|
- private void setHeader(InvoPlatform invoPlatform,PlatformInfo platformInfo){
|
|
|
|
- StateData.app_id = invoPlatform.getInvoPlatId();
|
|
|
|
- StateData.app_secret = invoPlatform.getInvoPlatAppSecret();
|
|
|
|
- StateData.app_key = invoPlatform.getInvoPlatAppKey();
|
|
|
|
- StateData.app_accessToken = invoPlatform.getInvoPlatAccessToken();
|
|
|
|
- StateData.app_signType = "AES";
|
|
|
|
-
|
|
|
|
- if(1 == NUONUO_ID){
|
|
|
|
- StateData.user_Tax = "339901999999142";
|
|
|
|
- }else{
|
|
|
|
- StateData.user_Tax = platformInfo.getPlatTaxNumber();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- Oauth2AccessToken.client_id = invoPlatform.getInvoPlatAppKey();
|
|
|
|
- Oauth2AccessToken.client_secret = invoPlatform.getInvoPlatAppSecret();
|
|
|
|
- Oauth2AccessToken.token_grant_type = "client_credentials";
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- private Map<String, String> getHeaders() {
|
|
|
|
- Map<String, String> headers = new HashMap<String, String>();
|
|
|
|
- headers.put("appKey", StateData.app_key);
|
|
|
|
- headers.put("accessToken", StateData.app_accessToken);
|
|
|
|
- headers.put("compress", StateData.app_compressType);
|
|
|
|
- headers.put("signMethod", StateData.app_signType);
|
|
|
|
- headers.put("dataType", StateData.app_dataType);
|
|
|
|
- headers.put("appRate", StateData.app_rate);
|
|
|
|
- headers.put("userTax", StateData.user_Tax);
|
|
|
|
- headers.put("Content-Type", StateData.contentType);
|
|
|
|
- return headers;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- private PublicData getPublicData(String api) {
|
|
|
|
- PublicData pdData = new PublicData();
|
|
|
|
- pdData.setVersion(StateData.app_apiVersion);
|
|
|
|
- pdData.setTimestamp(String.valueOf(System.currentTimeMillis()));
|
|
|
|
- pdData.setMethod(api);
|
|
|
|
- return pdData;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private RequestMode getRequestMode(PublicData pdata, PrivateData<Object> pvData) {
|
|
|
|
- RequestMode requestMode = new RequestMode();
|
|
|
|
- requestMode.setPrivate(pvData);
|
|
|
|
- requestMode.setPublic(pdata);
|
|
|
|
- return requestMode;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private static String getStr(String str){
|
|
|
|
- if(str == null)
|
|
|
|
- return "";
|
|
|
|
- return str;
|
|
|
|
- }
|
|
|
|
-}
|
|
|