李白
2026-06-05 d1f4be00fc8ab2faf990aa0446156b305fc25586
platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/invoice/service/impl/BIPYsServiceImpl.java
@@ -6,6 +6,7 @@
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.by4cloud.platformx.business.entity.BillingInfo;
import com.by4cloud.platformx.business.entity.SaleCredit;
import com.by4cloud.platformx.business.entity.invoice.BipInvoice;
import com.by4cloud.platformx.business.entity.invoice.InvoiceResult;
@@ -689,6 +690,306 @@
      return bipResVo;
   }
   @Override
   public BipResVo sendBIPYss(BillingInfo railwayEntrust) {
      BIPBody bipBody = new BIPBody();
      BIPYsMxHeader mx = new BIPYsMxHeader();
      List<BIPYsItem> bipYsItems = new ArrayList<>();
//      mx.(railwayEntrust.getFyCompName());//发运矿
      //mx.setMX01_code(fromStation2);//发站
      /*if (accountLedger.getId().equals(80) || accountLedger.getId().equals(81) && !accountLedger.getId().equals(82)) {
         contractNum = "";
      }*/
      if (StrUtil.isNotEmpty(railwayEntrust.getContractNo())) {
         mx.setHTBMBT(1);
      } else {
         mx.setHTBMBT(0);
      }
      mx.setContractNo(railwayEntrust.getContractNo());
      //mx.setMX021_code(customerCode);//收货客户
      //mx.setMX031_code(customerCode);//结算客户
      //mx.setMX04(number);//结算单号
      mx.setMX0401(railwayEntrust.getEntrustCode());//托收单号
      //mx.setMX051_code(businessType);//业务种类
      //mx.setMX06(createName);//创建人
      //mx.setMX07(railwayEntrust.getUserName());//审核人
      //mx.setGX08_code(accountUnitNumber);//核算单元
      //String remark = getRemark(railwayEntrust, "销", customerCode, id2);
      mx.setRemarks(railwayEntrust.getInvoiceRemark());
      //String fileUrl = fileUploadService.fileUpload(6, railwayEntrust.getId());
      mx.setFk_002(3);
      /*if (StrUtil.isNotEmpty(fileUrl) && "01".equals(jyType)) {
         mx.setFk_002(3 + fileUrl.split(",").length);
      }*/
      /*if (railwayEntrust.getFatherEntrustId() != null) {
         fileUrl = fileUploadService.fileUpload(6, contractNum);
         mx.setFk_002(3);
         if (StrUtil.isNotEmpty(fileUrl) && "01".equals(jyType)) {
            mx.setFk_002(3 + fileUrl.split(",").length);
         }
      }*/
      bipBody.setBustypeCode("MXysfp");
      //bipBody.setStaffCode(userCode);
//      bipBody.setFk_038_code(userCode);
      bipBody.setObjectType("1");
      //bipBody.setCustomerCode(customerCode);
      bipBody.setExtVouchCode(railwayEntrust.getEntrustCode());//煤销系统单号
      //bipBody.setFinanceOrgCode(id2);//开票组织ID
      bipBody.setDirection("1");//单据方向 1蓝 -1红
      bipBody.setBillDate(DateUtil.format(new Date(), "yyyy-MM-dd"));//开票日期
//      bipBody.setBillDate(DateUtil.format(railwayEntrust.getBalanceDate(), "yyyy-MM-dd"));//开票日期
//      if ("0101".equals(jyType)){
//
//      }
      //bipBody.setContractNo(contractNum);
      //bipBody.setOrgCode(id2);//业务组织id
      //bipBody.setDeptCode(deptCode);
      bipBody.setExchangeRateDate(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
//      bipBody.setExchangeRateDate(DateUtil.format(railwayEntrust.getBalanceDate(), "yyyy-MM-dd HH:mm:ss"));
//      bipBody.setCustomer();//客户编码
      //bipBody.setRemarks(remark);
      //表体
      //煤种类目
      BIPYsItem bipYsItem = new BIPYsItem();
//      bipYsItem.setExchangeRateDate(DateUtil.format(railwayEntrust.getBalanceDate(),"yyyy-MM-dd HH:mm:ss"));
      //bipYsItem.setTaxRate(NumUtils.multiplyDouble(railwayEntrust.getTaxRate(),100d));
      //bipYsItem.setUnitName("吨");
      /*bipYsItem.setMaterialCode("7704000001");
      bipYsItem.setQuantity(BigDecimal.valueOf(railwayEntrust.getTonnage()));
      bipYsItem.setCustomerCode(customerCode);//客户编码
      Double noTaxPrice = railwayEntrust.getPrice();
      bipYsItem.setOriTaxAmount(BigDecimal.valueOf(railwayEntrust.getTaxFee()));//税额
      String[] codes = expenseItemCode.get().split(",");
      bipYsItem.setExpenseItemCode(codes[0]);
      Double allPrice = railwayEntrust.getTotalPrice();
      bipYsItem.setOriTaxExcludedAmount(BigDecimal.valueOf(railwayEntrust.getCoalFree()));//原币无税金额
      bipYsItem.setOriTaxIncludedAmount(BigDecimal.valueOf(railwayEntrust.getTotalPrice()));//原币含税金额
      bipYsItem.setLocalTaxExcludedAmount(BigDecimal.valueOf(railwayEntrust.getCoalFree()));//本币无税金额
      bipYsItem.setLocalTaxIncludedAmount(BigDecimal.valueOf(railwayEntrust.getTotalPrice()));//本币含税金额
      bipYsItem.setOriTaxExcludedPrice(NumUtils.divideDouble(railwayEntrust.getCoalFree(), railwayEntrust.getTonnage(), 8));//无税单价
      bipYsItem.setLocalTaxExcludedPrice(NumUtils.divideDouble(railwayEntrust.getCoalFree(), railwayEntrust.getTonnage(), 8));//无税单价
      Double realPrice = NumUtils.divideDouble(railwayEntrust.getTotalPrice(), railwayEntrust.getTonnage(), 8);
      bipYsItem.setOriTaxIncludedPrice(realPrice);//本币含税单价
      bipYsItem.setLocalTaxIncludedPrice(realPrice);//本币含税单价
      //分割单金额
      if (CompUtils.isTsCustomer(railwayEntrust.getCustomerId())) {
         if (railwayEntrust.getFatherEntrustId() == null) {
            Double noTaxMoney = railwayEntrust.getCoalMoney();
            Double taxMoney = railwayEntrust.getCoalTaxMoney();
            Double allMoney = NumUtils.addDouble(noTaxMoney, taxMoney, 2);
            allPrice = allMoney;
            bipYsItem.setOriTaxAmount(BigDecimal.valueOf(taxMoney));//税额
            bipYsItem.setOriTaxExcludedAmount(BigDecimal.valueOf(noTaxMoney));//原币无税金额
            bipYsItem.setOriTaxIncludedAmount(BigDecimal.valueOf(allMoney));//原币含税金额
            bipYsItem.setLocalTaxExcludedAmount(BigDecimal.valueOf(noTaxMoney));//本币无税金额
            bipYsItem.setLocalTaxIncludedAmount(BigDecimal.valueOf(allMoney));//本币含税金额
            bipYsItem.setOriTaxExcludedPrice(railwayEntrust.getPrice());//无税单价
            bipYsItem.setLocalTaxExcludedPrice(railwayEntrust.getPrice());//无税单价
            bipYsItem.setOriTaxIncludedPrice(NumUtils.divideDouble(allMoney, railwayEntrust.getTonnage(), 8));//本币含税单价
            bipYsItem.setLocalTaxIncludedPrice(NumUtils.divideDouble(allMoney, railwayEntrust.getTonnage(), 8));//本币含税单价
         }
      }
      bipYsItem.setRemarks(remark);*/
      //煤种表体
      BIPYsMx bipYsMx = new BIPYsMx();
      //bipYsMx.setJT12_code(ncCoalNumber);//物料编码
//      bipYsMx.setJT25_code(codes[1]);
      //bipYsMx.setGX06_code(coalBipCode);//物料名称
      //bipYsMx.setMX021_code(customerCode);//收货客户
      //bipYsMx.setCustomerCode(customerAddressCode);//客户编码
      bipYsMx.setJT06_code("01001001");//款项明细
      bipYsMx.setYSYF14_code("2");
      /*if (id2!=null){
       *//*if (id2 != null && id2.equals("10439") && railwayEntrust.getCoalId() != null && railwayEntrust.getCoalId().equals(45)) {
            bipYsMx.setJT12_code("0101020401");//物料编码
            bipYsMx.setGX06_code("01010201043902438");//物料名称
         }*//*
         if (id2.equals("126") || id2.equals("10439") || id2.equals("1045116") || id2.equals("104810210") || id2.equals("1027902")) {
            bipBody.setYSYF_08("0");
            bipYsMx.setYSYF_08("0");
         }
         if (id2.startsWith("104")) {
            if (focus) {
               bipYsMx.setYSYF52("0");
            } else {
               bipYsMx.setYSYF52("1");
            }
         }
      }
      bipYsMx.setGX08_code(accountUnitNumber);*/
      /*bipYsMx.setMX09(railwayEntrust.getCoalName());//运销煤种
      bipYsMx.setMX10(BigDecimal.valueOf(railwayEntrust.getRealTonnage()));//水煤量
      bipYsMx.setMX11(railwayEntrust.getAshContentMetering());//灰分
      bipYsMx.setMX13(railwayEntrust.getMoistureMetering());
      bipYsMx.setMX14(railwayEntrust.getSulFurMetering());
      bipYsMx.setMX15(railwayEntrust.getCoalName());
      if (railwayEntrust.getInvoiceResultId() == null && !CompUtils.isKp(accountLedger.getId()) && !accountLedger.getId().equals(165)) {
         throw new RuntimeException("结算单未开票");
      }*/
      InvoiceResult invoiceResult = fpInvoiceResultService.getById(railwayEntrust.getBlueInvoiceResultId());
      if (invoiceResult != null) {
         bipYsMx.setYSYF33(invoiceResult.getInvoiceNo());
         bipYsItem.setInvoiceNo(invoiceResult.getInvoiceNo());
//         bipYsMx.setInvoiceNo(invoiceResult.getInvoiceNo());
      }
      bipYsMx.setZzbkfl_code("01");
      /*if (StrUtil.isNotEmpty(accountLedger.getZzbkfl())) {
         bipYsMx.setZzbkfl_code(accountLedger.getZzbkfl());
      }
      String jyType1 = jyType;//特殊交易单
      if (jyType.equals("0101") && railwayEntrust.getCustomerId() != null && railwayEntrust.getCustomerId().equals(203)) {
         jyType1 = "01020202";
      }
      if (jyType.equals("0101") && railwayEntrust.getCustomerId() != null && (railwayEntrust.getCustomerId().equals(172) || railwayEntrust.getCustomerId().equals(200) || railwayEntrust.getCustomerId().equals(2002381))) {
         jyType1 = "010201";
      }*/
      //bipYsMx.setJT04_code(jyType1);
      //bipYsMx.setMX16(railwayEntrust.getTonnage());//折干量
      bipYsItem.setFreeChId(bipYsMx);
//      bipYsMx.setMX18(railwayEntrust.getRealTonnage());//标煤量
//      bipYsMx.setMX18(railwayEntrust.getRealPrice());//标煤量单价
      //收款计划
      /*if (focus) {
         List<BIPPayPlan> bipPayPlans = this.getBipPayPlan(railwayEntrust, allPrice);
         bipYsItem.setGrandsonItem(bipPayPlans);
      }*/
      bipYsItems.add(bipYsItem);
      String invoiceNo = invoiceResult == null ? "" : invoiceResult.getInvoiceNo();
      /*if (railwayEntrust.getCustomerId() != null && (railwayEntrust.getCustomerId().equals(618) || railwayEntrust.getCustomerId().equals(608) || railwayEntrust.getCustomerId().equals(1367) || railwayEntrust.getCustomerId().equals(1002134))) {
         this.getTsBt(railwayEntrust, customerCode, jyType1, focus, bipYsItems, isNd, accountUnitNumber, id2, fromStation, invoiceNo, contractNum);
      } else {
         this.getPtBt(railwayEntrust, customerCode, jyType1, focus, bipYsItems, isNd, accountUnitNumber, id2, fromStation, invoiceNo, contractNum);
      }*/
      bipBody.setBodyItem(bipYsItems);
      /*if (railwayEntrust.getFatherEntrustId() == null && !railwayEntrust.getFyCompId().equals(227) && !order.getFiledId().equals(48)) {
         List<Contract> contracts = fileUploadService.getContractsByEntrustId(railwayEntrust.getId());
         System.out.println("重复合同:" + contracts.size() + "上级ID:" + railwayEntrust.getFatherEntrustId() + "结算单号:" + railwayEntrust.getEntrustCode());
         if (contracts != null && contracts.size() > 0) {
            contracts.forEach(e -> {
               BIPYsMxHeader newMx = new BIPYsMxHeader();
               BeanUtils.copyProperties(mx, newMx);
               if (accountLedger.getId().equals(80) || accountLedger.getId().equals(81) || accountLedger.getId().equals(82)) {
                  newMx.setContractNo("");
                  newMx.setHTBMBT(0);
               } else {
                  newMx.setContractNo(e.getNum());
                  newMx.setHTBMBT(1);
               }
               bipBody.setFreeChId(newMx);
            });
         }
      } else {
         bipBody.setFreeChId(mx);
      }*/
      String result = bipHttpUtil.post(BipApiEnum.应收发票保存接口, new JSONObject().set("data", bipBody));
      BipResVo resVo = JSONUtil.toBean(result, BipResVo.class);
      //放到单据记录表
      BipInvoice bipInvoice = new BipInvoice();
      bipInvoice.setDay(DateUtil.date());
      bipInvoice.setInvoice(JSONUtil.toJsonStr(bipBody));
      bipInvoice.setType(0);
      bipInvoice.setInvoiceType(0);
      bipInvoice.setContractNum(railwayEntrust.getContractNo());
      bipInvoice.setRailwayEntrustId(railwayEntrust.getId());
      //bipInvoice.setAllId(Long.parseLong(allId+""));
      bipInvoice.setBipCompNumber("id2");
      bipInvoice.setDirection(1);
      bipInvoice.setCompId(railwayEntrust.getCompId());
      bipInvoice.setResStatus(resVo.getCode());
//         bipInvoice.setMoney(railwayEntrust.getAllPrice());
//         bipInvoice.setTonnage(railwayEntrust.getTonnage());
//         bipInvoice.setPrice(railwayEntrust.getPrice());
      //bipInvoice.setTrainType(railwayEntrust.getTrainType());
      BipResVo bipResVo = new BipResVo<>();
      bipResVo.setCode(resVo.getCode());
      BIPBody resBipBody = null;
      if (resVo.getData() == null) {
         resBipBody = new BIPBody();
      } else {
         resBipBody = JSONUtil.toBean(resVo.getData().toString(), BIPBody.class);
      }
      if (resVo.getCode().equals("200") || (StrUtil.isNotEmpty(resVo.getMessage()) && resVo.getMessage().contains("活动[财务审核]上找不到用户")) || StrUtil.isNotEmpty(resBipBody.getCode())) {
         bipInvoice.setResInvoice(resVo.getData().toString());
         bipInvoice.setBipNumber(resBipBody.getId());
         //            bipInvoice.setCode("200");
         bipInvoice.setCode(resBipBody.getCode());
         bipInvoice.setMessage(resVo.getMessage());
         bipInvoiceService.save(bipInvoice);
         bipResVo.setCode("200");
      } else {
         if (resVo.getData() != null && StrUtil.isNotEmpty(resVo.getData().toString())) {
            bipInvoice.setBipNumber(resBipBody.getId());
            bipInvoice.setCode(resBipBody.getCode());
         }
         bipInvoice.setMessage(resVo.getMessage());
         bipResVo.setMessage(resVo.getMessage());
         bipInvoiceService.save(bipInvoice);
      }
      if (resVo.getData() != null && StrUtil.isNotEmpty(resVo.getData().toString())) {
         if (resBipBody.getId() != null) {
            try {
               /*if (railwayEntrust.getTrainType().equals(1)) {
                  if (order != null && order.getCompId() != null && order.getCompId().equals(227) && order.getFiledId() != null && order.getFiledId().equals(48)) {
                     fileUploadService.uploadToBip(railwayEntrust.getId(), resBipBody.getId(), fileUploadService.fileUpload(8, allId));
                  } else {
                     fileUploadService.uploadToBip(railwayEntrust.getId(), resBipBody.getId(), fileUploadService.fileUpload(0, allId));
                  }
                  fileUploadService.uploadToBip(railwayEntrust.getId(), resBipBody.getId(), fileUploadService.fileUpload(3, railwayEntrust.getId(), railwayEntrust.getQualityName(), railwayEntrust.getUserCompId()));
               } else if (railwayEntrust.getTrainType().equals(2)) {
                  fileUploadService.uploadToBip(railwayEntrust.getId(), resBipBody.getId(), fileUploadService.fileUpload(1, allId));
                  fileUploadService.uploadToBip(railwayEntrust.getId(), resBipBody.getId(), fileUploadService.fileUpload(4, railwayEntrust.getId(), railwayEntrust.getQualityName(), railwayEntrust.getUserCompId()));
               }
               if (StrUtil.isNotEmpty(fileUrl)) {
                  for (String s : fileUrl.split(",")) {
                     fileUploadService.uploadUrlToBip(railwayEntrust.getId(), resBipBody.getId(), s);
                  }
               }*/
               //发票
               railwayEntrust.setBipStatus(200);
               fpInvoiceResultService.update(new UpdateWrapper<InvoiceResult>().lambda()
                     .eq(InvoiceResult::getEntrustCode, railwayEntrust.getEntrustCode())
                     .set(InvoiceResult::getPushBip, null));
               //fpInvoiceResultService.pushBlue(railwayEntrust, 0);
               //fileUploadService.uploadToBip(railwayEntrust.getId(), resBipBody.getId(), fileUploadService.fileUpload(2, allId));
            } catch (Exception e) {
               //railwayEntrust.setBipFileStatus(-1);
               log.error("结算单推送附件错误---", e);
               e.printStackTrace();
            }
         }
      }
      return bipResVo;
   }
   public BipResVo sendBIPYf(SaleCredit railwayEntrust,String id1,String id2,String customerCode,String businessType,String fromStation,Integer allId,String contractNum,String number,String jyType,String accountUnitNumber,String deptCode,String userCode,Integer isNd) {
//      BipResVo bipResVo1 = new BipResVo<>();