| | |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.date.DatePattern; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.hutool.core.util.ArrayUtil; |
| | | import cn.hutool.core.lang.tree.Tree; |
| | | import cn.hutool.core.lang.tree.TreeNode; |
| | | import cn.hutool.core.lang.tree.TreeUtil; |
| | | import cn.hutool.core.util.IdUtil; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.http.HttpResponse; |
| | | import cn.hutool.http.HttpUtil; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | |
| | | import com.by4cloud.platformx.admin.api.feign.RemoteDeptService; |
| | | import com.by4cloud.platformx.business.api.feign.RemoteFlowProcessService; |
| | | import com.by4cloud.platformx.business.constant.FlowNameEnum; |
| | | import com.by4cloud.platformx.business.dto.ContractInvoiceQueryDTO; |
| | | import com.by4cloud.platformx.business.dto.ContractQueryDTO; |
| | | import com.by4cloud.platformx.business.dto.GenInvoiceInfoDTO; |
| | | import com.by4cloud.platformx.business.dto.*; |
| | | import com.by4cloud.platformx.business.entity.*; |
| | | import com.by4cloud.platformx.business.mapper.*; |
| | | import com.by4cloud.platformx.business.service.ContractInvoiceService; |
| | |
| | | import com.github.yulichang.wrapper.MPJLambdaWrapper; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.data.redis.core.StringRedisTemplate; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Slf4j |
| | |
| | | private final RemoteFlowProcessService remoteFlowProcessService; |
| | | private final PaymentConfirmService paymentConfirmService; |
| | | private final StringRedisTemplate redisTemplate; |
| | | private final BipViewMapper bipViewMapper; |
| | | private final BipDictMapper bipDictMapper; |
| | | |
| | | |
| | | @Value("${BWInvoice.url}") |
| | |
| | | @Value("${bip.url}") |
| | | private String url; |
| | | |
| | | @Value("${bip.appKey}") |
| | | private String appKey; |
| | | @Value("${dept.sgb}") |
| | | private String sgb; |
| | | |
| | | @Value("${bip.appSecret}") |
| | | private String appSecret; |
| | | @Value("${dept.smj}") |
| | | private String smj; |
| | | |
| | | @Value("${dept.smjzt}") |
| | | private String smjzt; |
| | | |
| | | @Value("${dept.smjsc}") |
| | | private String smjsc; |
| | | |
| | | @Value("${dept.tfgs}") |
| | | private String tfgs; |
| | | |
| | | @Value("${dept.jxc}") |
| | | private String jxc; |
| | | |
| | | @Value("${dept.ymj}") |
| | | private String ymj; |
| | | |
| | | @Value("${file.path}") |
| | | private String localPath; |
| | | |
| | | |
| | | @Override |
| | |
| | | invoiceOutBoundVoList.stream().forEach(contractInvoiceOutBoundVo -> { |
| | | |
| | | contractInvoiceOutBoundVo.setInvoiceNum(contractInvoiceOutBoundVo.getOutBoundNum().subtract(contractInvoiceOutBoundVo.getInvoiceNum())); |
| | | ContractSubjectMatter subjectMatter = contractSubjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery().eq(ContractSubjectMatter::getContractId,contractId) |
| | | .eq(ContractSubjectMatter::getMaterialCode,contractInvoiceOutBoundVo.getSubjectMatterCode()) |
| | | .eq(StrUtil.isNotBlank(contractInvoiceOutBoundVo.getLineNo()),ContractSubjectMatter::getLineNo,contractInvoiceOutBoundVo.getLineNo()) |
| | | ContractSubjectMatter subjectMatter = contractSubjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery().eq(ContractSubjectMatter::getContractId, contractId) |
| | | .eq(ContractSubjectMatter::getMaterialCode, contractInvoiceOutBoundVo.getSubjectMatterCode()) |
| | | .eq(StrUtil.isNotBlank(contractInvoiceOutBoundVo.getLineNo()), ContractSubjectMatter::getLineNo, contractInvoiceOutBoundVo.getLineNo()) |
| | | .last("limit 1")); |
| | | if (ObjUtil.isNotNull(subjectMatter)&&subjectMatter.getAdvanceNum().compareTo(new BigDecimal("0"))>0){ |
| | | contractInvoiceOutBoundVo.setInvoiceNum(contractInvoiceOutBoundVo.getOutBoundNum().subtract(subjectMatter.getAdvanceNum())); |
| | | if (ObjUtil.isNotNull(subjectMatter)) { |
| | | contractInvoiceOutBoundVo.setUnitPrice(subjectMatter.getUnitPrice()); |
| | | if (subjectMatter.getAdvanceNum().compareTo(new BigDecimal("0")) > 0) { |
| | | contractInvoiceOutBoundVo.setInvoiceNum(contractInvoiceOutBoundVo.getOutBoundNum().subtract(subjectMatter.getAdvanceNum())); |
| | | } |
| | | } |
| | | contractInvoiceOutBoundVo.setToInvoiceNum(contractInvoiceOutBoundVo.getInvoiceNum()); |
| | | }); |
| | | } |
| | | return R.ok(invoiceOutBoundVoList); |
| | | return R.ok(invoiceOutBoundVoList.stream().filter(item->item.getUnitPrice().compareTo(new BigDecimal("0"))>0).collect(Collectors.toList())); |
| | | } |
| | | |
| | | @Override |
| | |
| | | .selectAs(ContractSubjectMatter::getQuantity, "outBoundNum") |
| | | .eq(ContractSubjectMatter::getContractId, contractId); |
| | | List<ContractInvoiceOutBoundVo> invoiceOutBoundVoList = contractSubjectMatterMapper.selectJoinList(ContractInvoiceOutBoundVo.class, wrapper); |
| | | if (ArrayUtil.isNotEmpty(invoiceOutBoundVoList)) { |
| | | if (CollUtil.isNotEmpty(invoiceOutBoundVoList)) { |
| | | invoiceOutBoundVoList.stream().forEach(contractInvoiceOutBoundVo -> { |
| | | contractInvoiceOutBoundVo.setInvoiceNum(contractInvoiceOutBoundVo.getOutBoundNum().subtract(contractInvoiceOutBoundVo.getAdvanceNum())); |
| | | contractInvoiceOutBoundVo.setToInvoiceNum(contractInvoiceOutBoundVo.getInvoiceNum()); |
| | | }); |
| | | } |
| | | return R.ok(invoiceOutBoundVoList); |
| | | return R.ok(invoiceOutBoundVoList.stream().filter(item->item.getUnitPrice().compareTo(new BigDecimal("0"))>0).collect(Collectors.toList())); |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | @Override |
| | | public R genInvoiceInfo(GenInvoiceInfoDTO genInvoiceInfoDTO) { |
| | | if (ArrayUtil.isEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) { |
| | | if (CollUtil.isEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) { |
| | | return R.failed("请选择开票出库单后再操作"); |
| | | } |
| | | genInvoiceInfoDTO.setOutBoundInvoiceList(genInvoiceInfoDTO.getOutBoundInvoiceList().stream().filter(item-> |
| | | item.getInvoiceNum().compareTo(new BigDecimal("0"))>0).collect(Collectors.toList())); |
| | | if (CollUtil.isEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) { |
| | | return R.failed("请选择开票出库单后或调整开票数量再操作"); |
| | | } |
| | | Long contractId = null; |
| | | //预收款开票 |
| | | if (StrUtil.isNotBlank(genInvoiceInfoDTO.getContractId())) { |
| | | ContractPaymentSchedule schedule = contractPaymentScheduleMapper.selectOne(Wrappers.<ContractPaymentSchedule>lambdaQuery() |
| | | .eq(ContractPaymentSchedule::getStageName, "合同签订").eq(ContractPaymentSchedule::getContractId,genInvoiceInfoDTO.getContractId()) |
| | | .eq(ContractPaymentSchedule::getStageName, "合同签订").eq(ContractPaymentSchedule::getContractId, genInvoiceInfoDTO.getContractId()) |
| | | .last("limit 1")); |
| | | if (ObjUtil.isNull(schedule)) { |
| | | return R.failed("合同预收款阶段异常,请联系技术人员"); |
| | |
| | | return R.failed("获取开票单位异常"); |
| | | } |
| | | SysDept b = r.getData(); |
| | | BusinessCustomer customer = businessCustomerMapper.selectById(contract.getPartyAId()); |
| | | if (ObjUtil.isNull(customer)){ |
| | | return R.failed("获取购方单位异常"); |
| | | } |
| | | BigDecimal advanceAmount = new BigDecimal("0"); |
| | | for (ContractOutBoundInvoiceVo contractOutBoundInvoiceVo : genInvoiceInfoDTO.getOutBoundInvoiceList()) { |
| | | ContractSubjectMatter subjectMatter = contractSubjectMatterMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId()); |
| | |
| | | advanceAmount = advanceAmount.add(contractOutBoundInvoiceVo.getInvoiceNum().multiply(subjectMatter.getUnitPrice())); |
| | | } |
| | | } |
| | | log.info("advanceAmount:{}",advanceAmount); |
| | | log.info("schedule.getPlannedAmount:{}",schedule.getPlannedAmount()); |
| | | log.info("advanceAmount:{}", advanceAmount); |
| | | log.info("schedule.getPlannedAmount:{}", schedule.getPlannedAmount()); |
| | | if (advanceAmount.compareTo(schedule.getPlannedAmount()) > 0) { |
| | | return R.failed("开票金额超出合同签订阶段应收金额:" + schedule.getPlannedAmount() + ",请调整开票数量"); |
| | | } |
| | | GenInvoiceInfoVo vo = genAdvanceInvoiceInfoVo(genInvoiceInfoDTO, contract, b); |
| | | |
| | | vo = transBipVo(vo,b,customer); |
| | | return R.ok(vo); |
| | | } |
| | | |
| | | //正常产品开票 |
| | | if (!StrUtil.equals(genInvoiceInfoDTO.getContractCategory(), "water_house")) { |
| | | ContractOutBound outBound = contractOutBoundMapper.selectById(genInvoiceInfoDTO.getOutBoundInvoiceList().get(0).getContractOutBoundId()); |
| | | contractId = outBound.getContractId(); |
| | |
| | | return R.failed("获取开票单位异常"); |
| | | } |
| | | SysDept b = r.getData(); |
| | | GenInvoiceInfoVo vo = genInvoiceInfoVo(genInvoiceInfoDTO, contract, b); |
| | | BusinessCustomer customer = businessCustomerMapper.selectById(contract.getPartyAId()); |
| | | if (ObjUtil.isNull(customer)){ |
| | | return R.failed("获取购方单位异常"); |
| | | } |
| | | //机动车开票 |
| | | if (StrUtil.equals(genInvoiceInfoDTO.getInvoiceType(), "3") || StrUtil.equals(genInvoiceInfoDTO.getInvoiceType(), "4")) { |
| | | if (StrUtil.isBlank(genInvoiceInfoDTO.getVehicleNo())) { |
| | | return R.failed("机动车开票请输入车架号"); |
| | | } |
| | | BigDecimal invoiceNumSum = genInvoiceInfoDTO.getOutBoundInvoiceList().stream().map(item -> item.getInvoiceNum()).reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | if (invoiceNumSum.compareTo(new BigDecimal("1")) > 0) { |
| | | return R.failed("机动车开票只能1次开1辆"); |
| | | } |
| | | JSONObject request = genQueryVinParam(genInvoiceInfoDTO.getVehicleNo(), b); |
| | | R r1 = requestQueryVehicleNo(request); |
| | | if (!r1.isOk()) { |
| | | return r1; |
| | | } |
| | | JSONObject request1 = genQueryVehDetailParam(genInvoiceInfoDTO.getVehicleNo(), b); |
| | | R<JSONObject> r2 = requestQueryVehicleDetail(request1,genInvoiceInfoDTO); |
| | | if (!r2.isOk()) { |
| | | return r2; |
| | | } |
| | | r2.getData().put("contact",b.getOrgContact()); |
| | | genInvoiceInfoDTO.setVehicleData(r2.getData().toString()); |
| | | } |
| | | |
| | | GenInvoiceInfoVo vo = genInvoiceInfoVo(genInvoiceInfoDTO, contract, b); |
| | | vo = transBipVo(vo,b,customer); |
| | | return R.ok(vo); |
| | | } |
| | | |
| | | private GenInvoiceInfoVo transBipVo(GenInvoiceInfoVo vo,SysDept comp,BusinessCustomer customer) { |
| | | ContractInvoiceBipVo bipVo = new ContractInvoiceBipVo(); |
| | | bipVo.setDjrq(DateUtil.today()); |
| | | bipVo.setKpzz(comp.getBipCode()); |
| | | bipVo.setYwzz(comp.getBipCode()); |
| | | R<SysDept> rDept = remoteDeptService.getById(SecurityUtils.getUser().getDeptId()); |
| | | if (rDept.isOk()) { |
| | | bipVo.setBm(rDept.getData().getOrgCode()); |
| | | bipVo.setHsdy(rDept.getData().getOrgCode()); |
| | | } |
| | | bipVo.setYwy(SecurityUtils.getUser().getNickname()); |
| | | bipVo.setPzzdr(SecurityUtils.getUser().getNickname()); |
| | | bipVo.setJylx("ZBYXGLXT"); |
| | | bipVo.setWldxlx("1"); |
| | | bipVo.setKh(customer.getCreditCode()); |
| | | bipVo.setBz("CNY"); |
| | | bipVo.setFjzs("1"); |
| | | bipVo.setSfzy("1"); |
| | | bipVo.setSfnbwl("1"); |
| | | bipVo.setSfjyjs("0"); |
| | | bipVo.setDjfx("1"); |
| | | bipVo.setItemList(vo.getInvoiceItemList()); |
| | | vo.setBipVo(bipVo); |
| | | return vo; |
| | | } |
| | | |
| | | private R requestQueryVehicleDetail(JSONObject request,GenInvoiceInfoDTO invoiceInfoDTO) { |
| | | String requestId = IdUtil.fastUUID(); |
| | | |
| | | log.info("机动车合格证查询接口入参:{},", request.toJSONString()); |
| | | String result = HttpUtil.post(bwUrl + "method=baiwang.output.invoice.queryVinDetail&version=1.0&request_id=" + requestId, request.toJSONString()); |
| | | log.info("机动车合格证查询接口回参:{},", result); |
| | | saveBwRequestRecord("baiwang.output.invoice.queryVinDetail", requestId, request.toJSONString(), result); |
| | | JSONObject resultObj = JSONObject.parseObject(result); |
| | | if (resultObj.containsKey("requestId") && StrUtil.equals(resultObj.getString("requestId"), requestId) && |
| | | !resultObj.getBoolean("success")) { |
| | | return R.failed("机动车合格证查询失败", result); |
| | | } |
| | | JSONArray models = resultObj.getJSONArray("model"); |
| | | if (CollUtil.isNotEmpty(models)) { |
| | | JSONObject model = (JSONObject) models.get(0); |
| | | if (StrUtil.isNotBlank(invoiceInfoDTO.getOrigin())) { |
| | | model.put("origin",invoiceInfoDTO.getOrigin()); |
| | | } |
| | | if (StrUtil.isNotBlank(invoiceInfoDTO.getTonnage())) { |
| | | model.put("tonnage",invoiceInfoDTO.getTonnage()); |
| | | } |
| | | if (StrUtil.isNotBlank(invoiceInfoDTO.getPassengersLimited())) { |
| | | model.put("passengersLimited",invoiceInfoDTO.getPassengersLimited()); |
| | | } |
| | | model.put("origin",invoiceInfoDTO.getOrigin()); |
| | | model.put("vehicleNo",invoiceInfoDTO.getVehicleNo()); |
| | | return R.ok(model); |
| | | } |
| | | return R.failed("机动车合格证查询异常"); |
| | | } |
| | | |
| | | private JSONObject genQueryVehDetailParam(String vehicleNo, SysDept b) { |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.put("taxNo", b.getOrgCode()); |
| | | JSONObject data = new JSONObject(); |
| | | data.put("vehicleNo", vehicleNo); |
| | | jsonObject.put("data", data); |
| | | return jsonObject; |
| | | } |
| | | |
| | | private R requestQueryVehicleNo(JSONObject request) { |
| | | String requestId = IdUtil.fastUUID(); |
| | | |
| | | log.info("查询税局车架号码入参:{},", request.toJSONString()); |
| | | String result = HttpUtil.post(bwUrl + "method=baiwang.output.einvoice.queryvinlist&version=1.0&request_id=" + requestId, request.toJSONString()); |
| | | log.info("查询税局车架号码回参:{},", result); |
| | | saveBwRequestRecord("baiwang.output.einvoice.queryvinlist", requestId, request.toJSONString(), result); |
| | | JSONObject resultObj = JSONObject.parseObject(result); |
| | | if (resultObj.containsKey("requestId") && StrUtil.equals(resultObj.getString("requestId"), requestId) && |
| | | !resultObj.getBoolean("success")) { |
| | | return R.failed("查询税局车架号码失败", result); |
| | | } |
| | | JSONArray models = resultObj.getJSONArray("model"); |
| | | if (CollUtil.isNotEmpty(models)) { |
| | | JSONObject model = (JSONObject) models.get(0); |
| | | if (!model.getBoolean("isOpenInvoice")) { |
| | | return R.failed(model.getString("isNotOpenInvReason")); |
| | | } |
| | | } |
| | | return R.ok(); |
| | | } |
| | | |
| | | private JSONObject genQueryVinParam(String vehicleNo, SysDept b) { |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.put("taxNo", b.getOrgCode()); |
| | | JSONObject data = new JSONObject(); |
| | | data.put("vinList", new ArrayList<String>(Arrays.asList(vehicleNo))); |
| | | jsonObject.put("data", data); |
| | | return jsonObject; |
| | | } |
| | | |
| | | private GenInvoiceInfoVo genInvoiceInfoVo(GenInvoiceInfoDTO genInvoiceInfoDTO, Contract contract, SysDept b) { |
| | | GenInvoiceInfoVo vo = new GenInvoiceInfoVo(); |
| | | BusinessCustomer a = businessCustomerMapper.selectById(contract.getPartyAId()); |
| | | vo.setContractId(contract.getId()); |
| | | vo.setPartyA(a.getCompanyName()); |
| | | vo.setPartyAOrgCode(a.getCreditCode()); |
| | | vo.setPartyAOpenBank(a.getBankName()); |
| | |
| | | vo.setPartyBOrgCode(b.getOrgCode()); |
| | | vo.setPartyBOpenBank(b.getOrgBank()); |
| | | vo.setPartyBBankAccount(b.getOrgBankAccount()); |
| | | vo.setInvoiceType(a.getInvoiceType()); |
| | | vo.setRemarks(genInvoiceInfoDTO.getRemarks()); |
| | | vo.setOutBoundInvoiceList(genInvoiceInfoDTO.getOutBoundInvoiceList()); |
| | | if (StrUtil.isNotBlank(genInvoiceInfoDTO.getInvoiceType())) { |
| | | vo.setInvoiceVehicleType(genInvoiceInfoDTO.getInvoiceType()); |
| | | } |
| | | if (StrUtil.isNotBlank(genInvoiceInfoDTO.getVehicleNo())) { |
| | | vo.setVehicleNo(genInvoiceInfoDTO.getVehicleNo()); |
| | | } |
| | | if (StrUtil.isNotBlank(genInvoiceInfoDTO.getCertificateNo())) { |
| | | vo.setCertificateNo(genInvoiceInfoDTO.getCertificateNo()); |
| | | } |
| | | if (StrUtil.isNotBlank(genInvoiceInfoDTO.getVehicleData())) { |
| | | vo.setVehicleData(genInvoiceInfoDTO.getVehicleData()); |
| | | } |
| | | List<InvoiceItemVo> invoiceItemVoList = new ArrayList<>(); |
| | | List<String> erpCode = new ArrayList<>(); |
| | | List<ContractOutBoundMergeVo> mergeOutBounds = new ArrayList<>(); |
| | |
| | | if (CollUtil.isNotEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) { |
| | | genInvoiceInfoDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> { |
| | | ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId()); |
| | | |
| | | if (!CollUtil.contains(erpCode, contractOutBound.getSubjectMatterCode())) { |
| | | erpCode.add(contractOutBound.getSubjectMatterCode()); |
| | | ContractSubjectMatter subjectMatter = subjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery() |
| | | .eq(ContractSubjectMatter::getMaterialCode, contractOutBound.getSubjectMatterCode()) |
| | | .eq(StrUtil.isNotBlank(contractOutBound.getLineNo()),ContractSubjectMatter::getLineNo,contractOutBound.getLineNo()) |
| | | .eq(ContractSubjectMatter::getContractId, contract.getId()).last("limit 1")); |
| | | if (!CollUtil.contains(erpCode, contractOutBound.getSubjectMatterCode()+subjectMatter.getUnitPrice())) { |
| | | erpCode.add(contractOutBound.getSubjectMatterCode()+subjectMatter.getUnitPrice()); |
| | | ContractOutBoundMergeVo mergeVo = new ContractOutBoundMergeVo(); |
| | | mergeVo.setMaterialCode(contractOutBound.getSubjectMatterCode()); |
| | | mergeVo.setMaterialCode(contractOutBound.getSubjectMatterCode()+subjectMatter.getUnitPrice()); |
| | | mergeVo.setMaterialInternalName(contractOutBound.getSubjectMatterName()); |
| | | mergeVo.setQuantity(contractOutBoundInvoiceVo.getInvoiceNum()); |
| | | Product product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getErpCode, contractOutBound.getSubjectMatterCode()) |
| | | .eq(Product::getCompId, SecurityUtils.getUser().getCompId()).last("limit 1")); |
| | | Product product = new Product(); |
| | | List<String> smjList = new ArrayList<>(); |
| | | smjList.add(smj); |
| | | smjList.add(smjsc); |
| | | smjList.add(smjzt); |
| | | if ((""+SecurityUtils.getUser().getCompId()).equals(smj)||(""+SecurityUtils.getUser().getCompId()).equals(smjsc)|| |
| | | (""+SecurityUtils.getUser().getCompId()).equals(smjzt)){ |
| | | product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getErpCode, contractOutBound.getSubjectMatterCode()) |
| | | .in(Product::getCompId, smjList).last("limit 1")); |
| | | }else { |
| | | product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getErpCode, contractOutBound.getSubjectMatterCode()) |
| | | .eq(Product::getCompId, SecurityUtils.getUser().getCompId()).last("limit 1")); |
| | | } |
| | | |
| | | if (ObjUtil.isNotNull(product)) { |
| | | mergeVo.setTaxClass(product.getTaxClass()); |
| | | mergeVo.setSpecification(product.getProductType()); |
| | | mergeVo.setTaxRate(new BigDecimal(product.getTaxRate())); |
| | | mergeVo.setTaxCode(product.getTaxCode()); |
| | | Product productClass = productMapper.selectById(product.getParentId()); |
| | | if (ObjUtil.isNotNull(product)) { |
| | | if (StrUtil.equals(product.getErpCode(), "water_bill")) { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*水费"); |
| | | } else if (StrUtil.equals(product.getErpCode(), "electricity_bill")) { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*电费"); |
| | | } else if (StrUtil.equals(product.getErpCode(), "house_rental")) { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*房屋租赁"); |
| | | } else { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*" + productClass.getProductName()); |
| | | } |
| | | |
| | | } |
| | | } |
| | | ContractSubjectMatter subjectMatter = subjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery() |
| | | .eq(ContractSubjectMatter::getMaterialCode, contractOutBound.getSubjectMatterCode()) |
| | | .eq(ContractSubjectMatter::getContractId, contract.getId()).last("limit 1")); |
| | | if (ObjUtil.isNotNull(subjectMatter)) { |
| | | mergeVo.setUnitPrice(subjectMatter.getUnitPrice()); |
| | | mergeVo.setUnit(subjectMatter.getUnit()); |
| | | if (StrUtil.equals(product.getErpCode(), "water_bill")) { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*水费"); |
| | | } else if (StrUtil.equals(product.getErpCode(), "electricity_bill")) { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*电费"); |
| | | } else if (StrUtil.equals(product.getErpCode(), "house_rental")) { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*房屋租赁"); |
| | | } else { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*" + subjectMatter.getMaterialName()); |
| | | } |
| | | } |
| | | mergeOutBounds.add(mergeVo); |
| | | } else { |
| | | List<ContractOutBoundMergeVo> filteredList = mergeOutBounds.stream() |
| | | .filter(item -> contractOutBound.getSubjectMatterCode().equals(item.getMaterialCode())) // 防止空指针,建议常量在前 |
| | | .filter(item -> item.getMaterialCode().equals(subjectMatter.getMaterialCode()+subjectMatter.getUnitPrice())) // 防止空指针,建议常量在前 |
| | | .collect(Collectors.toList()); |
| | | if (ArrayUtil.isNotEmpty(filteredList)) { |
| | | if (CollUtil.isNotEmpty(filteredList)) { |
| | | filteredList.get(0).setQuantity(filteredList.get(0).getQuantity().add(contractOutBound.getOutBoundNum())); |
| | | } |
| | | } |
| | | }); |
| | | if (ArrayUtil.isNotEmpty(mergeOutBounds)) { |
| | | if (CollUtil.isNotEmpty(mergeOutBounds)) { |
| | | invoiceItemVoList = BeanUtil.copyToList(mergeOutBounds, InvoiceItemVo.class); |
| | | invoiceItemVoList.stream().forEach(invoiceItemVo -> { |
| | | invoiceItemVoList.stream().filter(invoiceItem->invoiceItem.getUnitPrice().compareTo(new BigDecimal("0"))>0) |
| | | .forEach(invoiceItemVo -> { |
| | | // for (InvoiceItemVo invoiceItemVo:invoiceItemVoList) { |
| | | BigDecimal itemPrice = invoiceItemVo.getUnitPrice().multiply(invoiceItemVo.getQuantity()); |
| | | BigDecimal itemPriceTax = itemPrice.divide(new BigDecimal("100").add(invoiceItemVo.getTaxRate()), 2, RoundingMode.HALF_UP).multiply(invoiceItemVo.getTaxRate()); |
| | | invoiceItemVo.setPriceTax(itemPriceTax); |
| | | invoiceItemVo.setPriceNoTax(itemPrice.subtract(itemPriceTax)); |
| | | invoiceItemVo.setPriceAndTax(itemPrice); |
| | | }); |
| | | vo.setTotalPrice(invoiceItemVoList.stream().map(item -> item.getPriceTax().add(item.getPriceNoTax())).reduce(BigDecimal.ZERO, BigDecimal::add)); |
| | | vo.setTotalPrice(invoiceItemVoList.stream().map(item -> item.getPriceAndTax()).reduce(BigDecimal.ZERO, BigDecimal::add)); |
| | | vo.setTotalTax(invoiceItemVoList.stream().map(item -> item.getPriceTax()).reduce(BigDecimal.ZERO, BigDecimal::add)); |
| | | vo.setInvoiceItemList(invoiceItemVoList); |
| | | vo.setPriceWords(Convert.digitToChinese(vo.getTotalPrice())); |
| | | } |
| | | } |
| | | } else { |
| | | if (ArrayUtil.isNotEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) { |
| | | if (CollUtil.isNotEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) { |
| | | genInvoiceInfoDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> { |
| | | ContractPaymentSchedule schedule = contractPaymentScheduleMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId()); |
| | | |
| | |
| | | mergeVo.setMaterialCode(schedule.getSubjectMatterCode()); |
| | | mergeVo.setMaterialInternalName(schedule.getSubjectMatterName()); |
| | | mergeVo.setPriceAndTax(schedule.getPlannedAmount()); |
| | | mergeVo.setQuantity(schedule.getInvoiceNum()); |
| | | Product product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getErpCode, schedule.getSubjectMatterCode()) |
| | | .eq(Product::getCompId, SecurityUtils.getUser().getCompId()).last("limit 1")); |
| | | if (ObjUtil.isNotNull(product)) { |
| | |
| | | mergeVo.setSpecification(product.getProductType()); |
| | | mergeVo.setTaxRate(new BigDecimal(product.getTaxRate())); |
| | | mergeVo.setTaxCode(product.getTaxCode()); |
| | | Product productClass = productMapper.selectById(product.getParentId()); |
| | | if (ObjUtil.isNotNull(product)) { |
| | | if (StrUtil.equals(product.getErpCode(), "water_bill")) { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*水费"); |
| | | } else if (StrUtil.equals(product.getErpCode(), "electricity_bill")) { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*电费"); |
| | | } else if (StrUtil.equals(product.getErpCode(), "house_rental")) { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*房屋租赁"); |
| | | } else { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*" + productClass.getProductName()); |
| | | } |
| | | |
| | | } |
| | | } |
| | | ContractSubjectMatter subjectMatter = subjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery() |
| | | .eq(ContractSubjectMatter::getMaterialCode, schedule.getSubjectMatterCode()) |
| | | .eq(ContractSubjectMatter::getContractId, contract.getId()).last("limit 1")); |
| | | if (ObjUtil.isNotNull(subjectMatter)) { |
| | | mergeVo.setUnitPrice(subjectMatter.getUnitPrice()); |
| | | if (StrUtil.equals(product.getErpCode(), "water_bill")) { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*水费"); |
| | | mergeVo.setUnit("吨"); |
| | | } else if (StrUtil.equals(product.getErpCode(), "electricity_bill")) { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*电费"); |
| | | mergeVo.setUnit("度"); |
| | | } else if (StrUtil.equals(product.getErpCode(), "house_rental")) { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*房屋租赁"); |
| | | } else { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*" + subjectMatter.getMaterialName()); |
| | | } |
| | | |
| | | } |
| | | mergeOutBounds.add(mergeVo); |
| | | } else { |
| | | List<ContractOutBoundMergeVo> filterMergeVo = mergeOutBounds.stream().filter(item -> item.getMaterialCode() |
| | | .equals(schedule.getSubjectMatterCode())).collect(Collectors.toList()); |
| | | if (ArrayUtil.isNotEmpty(filterMergeVo)) { |
| | | if (CollUtil.isNotEmpty(filterMergeVo)) { |
| | | filterMergeVo.get(0).setPriceAndTax(schedule.getPlannedAmount().add(filterMergeVo.get(0).getPriceAndTax())); |
| | | } |
| | | } |
| | | }); |
| | | if (ArrayUtil.isNotEmpty(mergeOutBounds)) { |
| | | if (CollUtil.isNotEmpty(mergeOutBounds)) { |
| | | invoiceItemVoList = BeanUtil.copyToList(mergeOutBounds, InvoiceItemVo.class); |
| | | invoiceItemVoList.stream().forEach(invoiceItemVo -> { |
| | | invoiceItemVoList.stream().filter(invoiceItem->invoiceItem.getUnitPrice().compareTo(new BigDecimal("0"))>0) |
| | | .forEach(invoiceItemVo -> { |
| | | // for (InvoiceItemVo invoiceItemVo:invoiceItemVoList) { |
| | | BigDecimal itemPrice = invoiceItemVo.getPriceAndTax(); |
| | | BigDecimal itemPriceTax = itemPrice.divide(new BigDecimal("100").add(invoiceItemVo.getTaxRate()), 2, RoundingMode.HALF_UP).multiply(invoiceItemVo.getTaxRate()); |
| | | invoiceItemVo.setPriceTax(itemPriceTax); |
| | | invoiceItemVo.setPriceNoTax(itemPrice.subtract(itemPriceTax)); |
| | | |
| | | }); |
| | | vo.setTotalPrice(invoiceItemVoList.stream().map(item -> item.getPriceAndTax()).reduce(BigDecimal.ZERO, BigDecimal::add)); |
| | | vo.setTotalTax(invoiceItemVoList.stream().map(item -> item.getPriceTax()).reduce(BigDecimal.ZERO, BigDecimal::add)); |
| | | vo.setInvoiceItemList(invoiceItemVoList); |
| | | vo.setPriceWords(Convert.digitToChinese(vo.getTotalPrice())); |
| | | } |
| | |
| | | private GenInvoiceInfoVo genAdvanceInvoiceInfoVo(GenInvoiceInfoDTO genInvoiceInfoDTO, Contract contract, SysDept b) { |
| | | GenInvoiceInfoVo vo = new GenInvoiceInfoVo(); |
| | | BusinessCustomer a = businessCustomerMapper.selectById(contract.getPartyAId()); |
| | | vo.setContractId(contract.getId()); |
| | | vo.setAdvanceInvoice("111"); |
| | | vo.setPartyA(a.getCompanyName()); |
| | | vo.setPartyAOrgCode(a.getCreditCode()); |
| | |
| | | vo.setPartyBOrgCode(b.getOrgCode()); |
| | | vo.setPartyBOpenBank(b.getOrgBank()); |
| | | vo.setPartyBBankAccount(b.getOrgBankAccount()); |
| | | vo.setRemarks(genInvoiceInfoDTO.getRemarks()); |
| | | vo.setOutBoundInvoiceList(genInvoiceInfoDTO.getOutBoundInvoiceList()); |
| | | List<InvoiceItemVo> invoiceItemVoList = new ArrayList<>(); |
| | | List<String> erpCode = new ArrayList<>(); |
| | | List<ContractOutBoundMergeVo> mergeOutBounds = new ArrayList<>(); |
| | |
| | | genInvoiceInfoDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> { |
| | | ContractSubjectMatter subjectMatter = contractSubjectMatterMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId()); |
| | | if (ObjUtil.isNotNull(subjectMatter)) { |
| | | if (!CollUtil.contains(erpCode, subjectMatter.getMaterialCode())) { |
| | | if (!CollUtil.contains(erpCode, subjectMatter.getMaterialCode()+subjectMatter.getUnitPrice())) { |
| | | erpCode.add(subjectMatter.getMaterialCode()); |
| | | ContractOutBoundMergeVo mergeVo = new ContractOutBoundMergeVo(); |
| | | mergeVo.setMaterialCode(subjectMatter.getMaterialCode()); |
| | |
| | | mergeVo.setSpecification(product.getProductType()); |
| | | mergeVo.setTaxRate(new BigDecimal(product.getTaxRate())); |
| | | mergeVo.setTaxCode(product.getTaxCode()); |
| | | Product productClass = productMapper.selectById(product.getParentId()); |
| | | if (ObjUtil.isNotNull(product)) { |
| | | if (StrUtil.equals(product.getErpCode(), "water_bill")) { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*水费"); |
| | | } else if (StrUtil.equals(product.getErpCode(), "electricity_bill")) { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*电费"); |
| | | } else if (StrUtil.equals(product.getErpCode(), "house_rental")) { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*房屋租赁"); |
| | | } else { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*" + productClass.getProductName()); |
| | | } |
| | | |
| | | } |
| | | if (ObjUtil.isNotNull(product)) { |
| | | if (StrUtil.equals(product.getErpCode(), "water_bill")) { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*水费"); |
| | | mergeVo.setUnit("吨"); |
| | | } else if (StrUtil.equals(product.getErpCode(), "electricity_bill")) { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*电费"); |
| | | mergeVo.setUnit("度"); |
| | | } else if (StrUtil.equals(product.getErpCode(), "house_rental")) { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*房屋租赁"); |
| | | } else { |
| | | mergeVo.setItemName("*" + product.getTaxClass() + "*" + subjectMatter.getMaterialName()); |
| | | } |
| | | |
| | | } |
| | | mergeVo.setUnitPrice(subjectMatter.getUnitPrice()); |
| | | mergeVo.setUnit(subjectMatter.getUnit()); |
| | |
| | | |
| | | } |
| | | }); |
| | | if (ArrayUtil.isNotEmpty(mergeOutBounds)) { |
| | | if (CollUtil.isNotEmpty(mergeOutBounds)) { |
| | | invoiceItemVoList = BeanUtil.copyToList(mergeOutBounds, InvoiceItemVo.class); |
| | | invoiceItemVoList.stream().forEach(invoiceItemVo -> { |
| | | invoiceItemVoList.stream().filter(invoiceItem->invoiceItem.getUnitPrice().compareTo(new BigDecimal("0"))>0).forEach(invoiceItemVo -> { |
| | | // for (InvoiceItemVo invoiceItemVo:invoiceItemVoList) { |
| | | BigDecimal itemPrice = invoiceItemVo.getUnitPrice().multiply(invoiceItemVo.getQuantity()); |
| | | BigDecimal itemPriceTax = itemPrice.divide(new BigDecimal("100").add(invoiceItemVo.getTaxRate()), 2, RoundingMode.HALF_UP).multiply(invoiceItemVo.getTaxRate()); |
| | | invoiceItemVo.setPriceTax(itemPriceTax); |
| | | invoiceItemVo.setPriceNoTax(itemPrice.subtract(itemPriceTax)); |
| | | invoiceItemVo.setPriceAndTax(itemPrice); |
| | | }); |
| | | vo.setTotalPrice(invoiceItemVoList.stream().map(item -> item.getPriceTax().add(item.getPriceNoTax())).reduce(BigDecimal.ZERO, BigDecimal::add)); |
| | | vo.setTotalPrice(invoiceItemVoList.stream().map(item -> item.getPriceAndTax()).reduce(BigDecimal.ZERO, BigDecimal::add)); |
| | | vo.setTotalTax(invoiceItemVoList.stream().map(item -> item.getPriceTax()).reduce(BigDecimal.ZERO, BigDecimal::add)); |
| | | vo.setInvoiceItemList(invoiceItemVoList); |
| | | vo.setPriceWords(Convert.digitToChinese(vo.getTotalPrice())); |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | public R toInvoice(GenInvoiceInfoDTO genInvoiceInfoDTO) { |
| | | if (ArrayUtil.isEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) { |
| | | return R.failed("请选择开票出库单后再操作"); |
| | | public R toInvoice(ToInvoiceDTO toInvoiceDTO) { |
| | | Contract contract = contractMapper.selectById(toInvoiceDTO.getContractId()); |
| | | if (ObjUtil.isNull(contract)) { |
| | | return R.failed("合同信息异常,请联系技术人员"); |
| | | } |
| | | Long contractId = null; |
| | | if (StrUtil.isNotBlank(genInvoiceInfoDTO.getContractId())) { |
| | | if (StrUtil.isNotBlank(toInvoiceDTO.getAdvanceInvoice())) { |
| | | |
| | | ContractPaymentSchedule schedule = contractPaymentScheduleMapper.selectOne(Wrappers.<ContractPaymentSchedule>lambdaQuery() |
| | | .eq(ContractPaymentSchedule::getStageName, "合同签订").eq(ContractPaymentSchedule::getContractId,genInvoiceInfoDTO.getContractId()) |
| | | .eq(ContractPaymentSchedule::getStageName, "合同签订").eq(ContractPaymentSchedule::getContractId, toInvoiceDTO.getContractId()) |
| | | .last("limit 1")); |
| | | if (ObjUtil.isNull(schedule)) { |
| | | return R.failed("合同预收款阶段异常,请联系技术人员"); |
| | | } |
| | | Contract contract = contractMapper.selectById(genInvoiceInfoDTO.getContractId()); |
| | | if (ObjUtil.isNull(contract)) { |
| | | return R.failed("合同信息异常,请联系技术人员"); |
| | | } |
| | | ContractInvoice lastInvoice = baseMapper.selectOne(Wrappers.<ContractInvoice>lambdaQuery().eq(ContractInvoice::getContractId,contract.getId()) |
| | | ContractInvoice lastInvoice = baseMapper.selectOne(Wrappers.<ContractInvoice>lambdaQuery().eq(ContractInvoice::getContractId, contract.getId()) |
| | | .orderByDesc(ContractInvoice::getCreateTime).last("limit 1")); |
| | | if (ObjUtil.isNotNull(lastInvoice)&&StrUtil.equals(lastInvoice.getApprovalStatus(),"0")){ |
| | | if (ObjUtil.isNotNull(lastInvoice) && StrUtil.equals(lastInvoice.getApprovalStatus(), "0")) { |
| | | return R.failed("请等待最近的发票审核完成后再进行预收开票"); |
| | | } |
| | | R<SysDept> r = remoteDeptService.getById(contract.getPartyBId()); |
| | |
| | | } |
| | | SysDept b = r.getData(); |
| | | BigDecimal advanceAmount = new BigDecimal("0"); |
| | | for (ContractOutBoundInvoiceVo contractOutBoundInvoiceVo : genInvoiceInfoDTO.getOutBoundInvoiceList()) { |
| | | for (ContractOutBoundInvoiceVo contractOutBoundInvoiceVo : toInvoiceDTO.getOutBoundInvoiceList()) { |
| | | ContractSubjectMatter subjectMatter = contractSubjectMatterMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId()); |
| | | if (ObjUtil.isNotNull(subjectMatter)) { |
| | | advanceAmount = advanceAmount.add(contractOutBoundInvoiceVo.getInvoiceNum().multiply(subjectMatter.getUnitPrice())); |
| | |
| | | if (advanceAmount.compareTo(schedule.getPlannedAmount()) > 0) { |
| | | return R.failed("开票金额超出合同签订阶段应收金额:" + schedule.getPlannedAmount() + ",请调整开票数量"); |
| | | } |
| | | GenInvoiceInfoVo vo = genAdvanceInvoiceInfoVo(genInvoiceInfoDTO, contract, b); |
| | | GenInvoiceInfoVo vo = BeanUtil.copyProperties(toInvoiceDTO,GenInvoiceInfoVo.class); |
| | | //发票保存 |
| | | ContractInvoice invoice = saveContractInvoice(vo, contract, genInvoiceInfoDTO.getOutBoundInvoiceList(), DateUtil.current() + "","1"); |
| | | ContractInvoice invoice = saveContractInvoice(vo, contract, toInvoiceDTO.getOutBoundInvoiceList(), DateUtil.current() + "", "1"); |
| | | //启动流程 |
| | | ProcessInstanceParamDto dto = new ProcessInstanceParamDto(); |
| | | Map<String, Object> map = BeanUtil.beanToMap(invoice); |
| | |
| | | return R.failed("流程启动失败"); |
| | | } |
| | | //更新合同出库单状态 |
| | | genInvoiceInfoDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> { |
| | | toInvoiceDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> { |
| | | ContractSubjectMatter subjectMatter = contractSubjectMatterMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId()); |
| | | subjectMatter.setAdvanceNum(subjectMatter.getAdvanceNum().add(contractOutBoundInvoiceVo.getInvoiceNum())); |
| | | contractSubjectMatterMapper.updateById(subjectMatter); |
| | | }); |
| | | return R.ok(); |
| | | } |
| | | if (!StrUtil.equals(genInvoiceInfoDTO.getContractCategory(), "water_house")) { |
| | | ContractOutBound outBound = contractOutBoundMapper.selectById(genInvoiceInfoDTO.getOutBoundInvoiceList().get(0).getContractOutBoundId()); |
| | | contractId = outBound.getContractId(); |
| | | } else { |
| | | ContractPaymentSchedule schedule = contractPaymentScheduleMapper.selectById(genInvoiceInfoDTO.getOutBoundInvoiceList().get(0).getContractOutBoundId()); |
| | | contractId = schedule.getContractId(); |
| | | } |
| | | Contract contract = contractMapper.selectById(contractId); |
| | | if (ObjUtil.isNull(contract)) { |
| | | return R.failed("合同信息异常,请联系技术人员"); |
| | | } |
| | | R<SysDept> r = remoteDeptService.getById(contract.getPartyBId()); |
| | | if (!r.isOk()) { |
| | | return R.failed("获取开票单位异常"); |
| | | } |
| | | SysDept b = r.getData(); |
| | | GenInvoiceInfoVo vo = genInvoiceInfoVo(genInvoiceInfoDTO, contract, b); |
| | | GenInvoiceInfoVo vo = BeanUtil.copyProperties(toInvoiceDTO,GenInvoiceInfoVo.class); |
| | | //发票保存 |
| | | ContractInvoice invoice = saveContractInvoice(vo, contract, genInvoiceInfoDTO.getOutBoundInvoiceList(), DateUtil.current() + "","0"); |
| | | ContractInvoice invoice = saveContractInvoice(vo, contract, toInvoiceDTO.getOutBoundInvoiceList(), DateUtil.current() + "", "0"); |
| | | //启动流程 |
| | | ProcessInstanceParamDto dto = new ProcessInstanceParamDto(); |
| | | Map<String, Object> map = BeanUtil.beanToMap(invoice); |
| | | if (StrUtil.equals(genInvoiceInfoDTO.getContractCategory(), "water_house")) { |
| | | if (StrUtil.equals(toInvoiceDTO.getContractCategory(), "water_house")) { |
| | | map.put("contractCategory", "water_house"); |
| | | } |
| | | dto.setParamMap(map); |
| | |
| | | } |
| | | if (!StrUtil.equals(contract.getContractCategory(), "water_house")) { |
| | | //更新合同出库单状态 |
| | | genInvoiceInfoDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> { |
| | | toInvoiceDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> { |
| | | ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId()); |
| | | contractOutBound.setInvoiceStatus("1"); |
| | | if (contractOutBoundInvoiceVo.getInvoiceNum().compareTo(contractOutBound.getOutBoundNum().subtract(contractOutBound.getInvoiceNum())) <= 0) { |
| | | contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().add(contractOutBoundInvoiceVo.getInvoiceNum())); |
| | | } |
| | | contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().add(contractOutBoundInvoiceVo.getInvoiceNum())); |
| | | contractOutBoundMapper.updateById(contractOutBound); |
| | | }); |
| | | } else { |
| | | //更新合同房租 水电 费开票状态 |
| | | genInvoiceInfoDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> { |
| | | toInvoiceDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> { |
| | | ContractPaymentSchedule schedule = contractPaymentScheduleMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId()); |
| | | schedule.setInvoiceFlag("1"); |
| | | contractPaymentScheduleMapper.updateById(schedule); |
| | |
| | | bwRequestRecordMapper.insert(bwRequestRecord); |
| | | } |
| | | |
| | | private JSONObject genBlueInvoice(GenInvoiceInfoVo vo, String serialNo) { |
| | | private JSONObject genBlueInvoice(GenInvoiceInfoVo vo, String serialNo, SysDept dept) { |
| | | JSONObject jsonObject = new JSONObject(); |
| | | //销方 |
| | | jsonObject.put("taxNo", vo.getPartyBOrgCode());//税号 |
| | |
| | | jsonObject.put("formatPushType", false);//版式生成是否推送(true / false) |
| | | //data |
| | | JSONObject data = new JSONObject(); |
| | | data.put("invoiceTypeCode", "01"); |
| | | data.put("invoiceTypeCode", vo.getInvoiceType()); |
| | | data.put("serialNo", serialNo); |
| | | //data.invoiceDetailsList |
| | | JSONArray invoiceDetailsList = new JSONArray(); |
| | |
| | | invoiceDetail.put("goodsUnit", invoiceItemVo.getUnit()); |
| | | invoiceDetail.put("goodsQuantity", invoiceItemVo.getQuantity()); |
| | | // invoiceDetail.put("goodsPrice", invoiceItemVo.getUnitPrice()); |
| | | if (StrUtil.equals(vo.getInvoiceVehicleType(), "3")) { |
| | | invoiceDetail.put("goodsSpecification", vo.getVehicleNo()); |
| | | } else { |
| | | invoiceDetail.put("goodsSpecification", invoiceItemVo.getSpecification()); |
| | | } |
| | | |
| | | if (!StrUtil.equals(vo.getContractCategory(), "water_house")) { |
| | | invoiceDetail.put("goodsTotalPrice", invoiceItemVo.getPriceNoTax()); |
| | | } else { |
| | |
| | | //购方 |
| | | data.put("buyerTaxNo", vo.getPartyAOrgCode()); |
| | | data.put("buyerName", vo.getPartyA()); |
| | | data.put("drawer", SecurityUtils.getUser().getPhone());// TODO 税务总局系统登陆人手机号 |
| | | data.put("drawer", dept.getInvoiceContact());// TODO 税务总局系统登陆人手机号 |
| | | data.put("invoiceTotalPriceTax", vo.getTotalPrice()); |
| | | data.put("buyerBankName", vo.getPartyAOpenBank()); |
| | | data.put("buyerBankNumber", vo.getPartyABankAccount()); |
| | | |
| | | data.put("remarks",vo.getRemarks()); |
| | | jsonObject.put("data", data); |
| | | return jsonObject; |
| | | } |
| | | |
| | | private ContractInvoice saveContractInvoice(GenInvoiceInfoVo genInvoiceInfoVo, Contract contract, |
| | | List<ContractOutBoundInvoiceVo> outBoundInvoiceVoList, String serialNo,String advanceFlag) { |
| | | List<ContractOutBoundInvoiceVo> outBoundInvoiceVoList, String serialNo, String advanceFlag) { |
| | | ContractInvoice invoice = new ContractInvoice(); |
| | | invoice.setContractId(contract.getId()); |
| | | invoice.setInvoiceCategory("1"); |
| | |
| | | |
| | | @Override |
| | | public R toRedInvoice(Long invoiceId) { |
| | | |
| | | ContractInvoice blueInvoice = baseMapper.selectById(invoiceId); |
| | | blueInvoice.setRedReversal("2"); |
| | | baseMapper.updateById(blueInvoice); |
| | |
| | | invoice.setInvoiceTotalTax(blueInvoice.getInvoiceTotalTax()); |
| | | invoice.setInvoiceTotalPrice(blueInvoice.getInvoiceTotalPrice()); |
| | | invoice.setInvoiceTotalPriceTax(blueInvoice.getInvoiceTotalPriceTax()); |
| | | invoice.setInvoicePreview(blueInvoice.getInvoicePreview()); |
| | | invoice.setInvoiceTotalTax(blueInvoice.getInvoiceTotalTax()); |
| | | invoice.setInvoiceTotalPrice(blueInvoice.getInvoiceTotalPrice()); |
| | | invoice.setInvoiceTotalPriceTax(blueInvoice.getInvoiceTotalPriceTax()); |
| | | |
| | | ToInvoiceDTO toInvoiceDTO = BeanUtil.copyProperties(JSONObject.parseObject(blueInvoice.getInvoicePreview()),ToInvoiceDTO.class); |
| | | toInvoiceDTO.setTotalPrice(toInvoiceDTO.getTotalPrice().multiply(new BigDecimal(-1))); |
| | | toInvoiceDTO.setTotalTax(toInvoiceDTO.getTotalTax().multiply(new BigDecimal(-1))); |
| | | if (CollUtil.isNotEmpty(toInvoiceDTO.getInvoiceItemList())){ |
| | | toInvoiceDTO.getInvoiceItemList().stream().forEach(item->{ |
| | | item.setPriceTax(item.getPriceTax().multiply(new BigDecimal("-1"))); |
| | | item.setPriceNoTax(item.getPriceNoTax().multiply(new BigDecimal("-1"))); |
| | | item.setPriceAndTax(item.getPriceAndTax().multiply(new BigDecimal("-1"))); |
| | | }); |
| | | } |
| | | invoice.setInvoicePreview(JSONObject.toJSONString(toInvoiceDTO)); |
| | | invoice.setInvoiceTotalTax(blueInvoice.getInvoiceTotalTax().multiply(new BigDecimal("-1"))); |
| | | invoice.setInvoiceTotalPrice(blueInvoice.getInvoiceTotalPrice().multiply(new BigDecimal("-1"))); |
| | | invoice.setInvoiceTotalPriceTax(blueInvoice.getInvoiceTotalPriceTax().multiply(new BigDecimal("-1"))); |
| | | invoice.setOutBoundId(blueInvoice.getOutBoundId()); |
| | | invoice.setInvoiceTime(new Date()); |
| | | invoice.setInvoiceCategory("2"); |
| | |
| | | return R.ok(); |
| | | } |
| | | |
| | | private JSONObject genRedInvoice(Contract contract, ContractInvoice blueInvoice, String redConfirmSerialNo) { |
| | | private JSONObject genRedInvoice(Contract contract, ContractInvoice blueInvoice,ContractInvoice redInvoice, String redConfirmSerialNo) { |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.put("taxNo", blueInvoice.getTaxNo()); |
| | | jsonObject.put("taxNo", redInvoice.getTaxNo()); |
| | | jsonObject.put("redConfirmSerialNo", redConfirmSerialNo); |
| | | jsonObject.put("entryIdentity", "01"); |
| | | jsonObject.put("sellerTaxNo", blueInvoice.getTaxNo()); |
| | | jsonObject.put("sellerTaxName", blueInvoice.getSellerTaxName()); |
| | | jsonObject.put("buyerTaxName", blueInvoice.getBuyerTaxName()); |
| | | jsonObject.put("buyerTaxNo", blueInvoice.getBuyerTaxNo()); |
| | | jsonObject.put("originInvoiceDate", blueInvoice.getInvoiceTime()); |
| | | jsonObject.put("originInvoiceTotalPrice", blueInvoice.getInvoiceTotalPrice()); |
| | | jsonObject.put("originInvoiceTotalTax", blueInvoice.getInvoiceTotalTax()); |
| | | jsonObject.put("invoiceTotalPrice", blueInvoice.getInvoiceTotalPrice().multiply(new BigDecimal("-1"))); |
| | | jsonObject.put("invoiceTotalTax", blueInvoice.getInvoiceTotalTax().multiply(new BigDecimal("-1"))); |
| | | jsonObject.put("sellerTaxNo", redInvoice.getTaxNo()); |
| | | jsonObject.put("sellerTaxName", redInvoice.getSellerTaxName()); |
| | | jsonObject.put("buyerTaxName", redInvoice.getBuyerTaxName()); |
| | | jsonObject.put("buyerTaxNo", redInvoice.getBuyerTaxNo()); |
| | | jsonObject.put("originInvoiceDate", DateUtil.formatDateTime(blueInvoice.getInvoiceTime())); |
| | | jsonObject.put("originInvoiceTotalPrice", redInvoice.getInvoiceTotalPriceTax()); |
| | | jsonObject.put("originInvoiceTotalTax", redInvoice.getInvoiceTotalTax()); |
| | | jsonObject.put("invoiceTotalPrice", redInvoice.getInvoiceTotalPrice()); |
| | | jsonObject.put("invoiceTotalTax", redInvoice.getInvoiceTotalTax()); |
| | | jsonObject.put("originInvoiceIsPaper", "N"); |
| | | jsonObject.put("originInvoiceType", "01"); |
| | | jsonObject.put("redInvoiceLabel", "01"); |
| | | jsonObject.put("invoiceSource", "2"); |
| | | jsonObject.put("originalInvoiceNo", blueInvoice.getInvoiceNo()); |
| | | GenInvoiceInfoVo vo = BeanUtil.copyProperties(JSONObject.parseObject(blueInvoice.getInvoicePreview()), GenInvoiceInfoVo.class); |
| | | GenInvoiceInfoVo vo = BeanUtil.copyProperties(JSONObject.parseObject(redInvoice.getInvoicePreview()), GenInvoiceInfoVo.class); |
| | | JSONArray invoiceDetailsList = new JSONArray(); |
| | | Integer lineNo = 1; |
| | | for (InvoiceItemVo invoiceItemVo : vo.getInvoiceItemList()) { |
| | |
| | | invoiceDetail.put("projectName", invoiceItemVo.getItemName()); |
| | | invoiceDetail.put("goodsLineNo", lineNo); |
| | | invoiceDetail.put("goodsCode", invoiceItemVo.getTaxCode()); |
| | | invoiceDetail.put("goodsTaxRate", invoiceItemVo.getTaxRate().divide(new BigDecimal("100"))); |
| | | invoiceDetail.put("goodsTaxRate", invoiceItemVo.getTaxRate()); |
| | | invoiceDetail.put("goodsUnit", invoiceItemVo.getUnit()); |
| | | invoiceDetail.put("goodsQuantity", invoiceItemVo.getQuantity()); |
| | | // invoiceDetail.put("goodsPrice", invoiceItemVo.getUnitPrice()); |
| | | invoiceDetail.put("goodsTotalPrice", invoiceItemVo.getPriceNoTax().multiply(new BigDecimal("-1"))); |
| | | invoiceDetail.put("goodsTotalTax", invoiceItemVo.getPriceTax().multiply(new BigDecimal("-1"))); |
| | | invoiceDetail.put("goodsTotalPrice", invoiceItemVo.getPriceNoTax()); |
| | | invoiceDetail.put("goodsTotalTax", invoiceItemVo.getPriceTax()); |
| | | if (vo.getInvoiceVehicleType().equals("3")||vo.getInvoiceVehicleType().equals("4")){ |
| | | invoiceDetail.put("goodsSpecification", invoiceItemVo.getSpecification()); |
| | | } |
| | | invoiceDetailsList.add(invoiceDetail); |
| | | lineNo++; |
| | | } |
| | |
| | | if (bwFlag) { |
| | | List<ContractInvoice> contractInvoices = baseMapper.selectList(Wrappers.<ContractInvoice>lambdaQuery() |
| | | .eq(ContractInvoice::getApprovalStatus, "1") |
| | | .eq(ContractInvoice::getBwRequestResult,"1") |
| | | .eq(ContractInvoice::getBwRequestResult, "1") |
| | | .isNull(ContractInvoice::getInvoicePath)); |
| | | if (ArrayUtil.isNotEmpty(contractInvoices)) { |
| | | if (CollUtil.isNotEmpty(contractInvoices)) { |
| | | contractInvoices.stream().forEach(contractInvoice -> { |
| | | JSONObject request = new JSONObject(); |
| | | request.put("taxNo", contractInvoice.getTaxNo()); |
| | |
| | | data.put("serialNo", contractInvoice.getSerialNo()); |
| | | data.put("returnType", "3"); |
| | | data.put("invoiceIssueMode", "1"); |
| | | request.put("data",data); |
| | | request.put("data", data); |
| | | String requestId = IdUtil.fastUUID(); |
| | | |
| | | log.info("serial_no:{},发票查询入参:{},", contractInvoice.getSerialNo(), request.toJSONString()); |
| | | String result = HttpUtil.post(bwUrl+"method=baiwang.output.format.query&version=1.0&request_id=" + requestId, request.toJSONString()); |
| | | String result = HttpUtil.post(bwUrl + "method=baiwang.output.format.query&version=1.0&request_id=" + requestId, request.toJSONString()); |
| | | log.info("serial_no:{},发票查询回参:{},", contractInvoice.getSerialNo(), result); |
| | | saveBwRequestRecord("baiwang.output.format.query", requestId, request.toJSONString(), result); |
| | | JSONObject resultObj = JSONObject.parseObject(result); |
| | |
| | | contractInvoice.setInvoicePath(model.getString("queryData")); |
| | | contractInvoice.setInvoiceStatus("2"); |
| | | baseMapper.updateById(contractInvoice); |
| | | //传输附件 |
| | | // pushBipInvoicePdf(contractInvoice); |
| | | } |
| | | }); |
| | | } |
| | | } |
| | | return R.ok(); |
| | | } |
| | | |
| | | private void pushBipInvoicePdf(ContractInvoice contractInvoice){ |
| | | String accessToken = ""; |
| | | if (redisTemplate.hasKey("BIP_TOKEN")) { |
| | | accessToken = (String) redisTemplate.opsForValue().get("BIP_TOKEN"); |
| | | } else { |
| | | accessToken = paymentConfirmService.getAccessToken(accessToken); |
| | | } |
| | | if (StrUtil.isEmpty(accessToken)) { |
| | | log.error("bip accessToken 获取异常"); |
| | | return; |
| | | } |
| | | String finalAccessToken = accessToken; |
| | | String requestId = IdUtil.fastUUID(); |
| | | JSONObject request = new JSONObject(); |
| | | request.put("taxNo", contractInvoice.getTaxNo()); |
| | | JSONObject data = new JSONObject(); |
| | | data.put("serialNo", contractInvoice.getSerialNo()); |
| | | data.put("returnType", "2"); |
| | | data.put("invoiceIssueMode", "1"); |
| | | request.put("data", data); |
| | | log.info("serial_no:{},发票查询入参:{},", contractInvoice.getSerialNo(), request.toJSONString()); |
| | | String result = HttpUtil.post(bwUrl + "method=baiwang.output.format.query&version=1.0&request_id=" + requestId, request.toJSONString()); |
| | | log.info("serial_no:{},发票查询回参:{},", contractInvoice.getSerialNo(), result); |
| | | JSONObject resultObj = JSONObject.parseObject(result); |
| | | String filePath = localPath + File.separator + DateUtil.today() + File.separator + IdUtil.fastUUID() + ".pdf"; |
| | | if (resultObj.containsKey("requestId") && StrUtil.equals(resultObj.getString("requestId"), requestId) && resultObj.getBoolean("success")) { |
| | | JSONObject model = resultObj.getJSONObject("model"); |
| | | saveBase64ToFile(model.getString("queryData"), filePath); |
| | | |
| | | } |
| | | File file = new File(filePath); |
| | | if (file.exists()) { |
| | | log.info("应收发票附件上传入参:{}", url + "/yonbip/uspace/iuap-apcom-file/rest/v1/file?access_token=" |
| | | + finalAccessToken + "&businessType=yonbip-fi-earapbill&businessId=" + contractInvoice.getBipId()); |
| | | HttpResponse response = HttpUtil.createPost(url + "/yonbip/uspace/iuap-apcom-file/rest/v1/file?access_token=" |
| | | + finalAccessToken + "&businessType=yonbip-fi-earapbill&businessId=" + contractInvoice.getBipId()) |
| | | .header("Content-Type", "multipart/form-data") |
| | | // 设置表单参数(文件),第一个参数为接口接收的参数名(如"file") |
| | | .form("files", file) |
| | | // 执行请求 |
| | | .execute(); |
| | | log.info("应收发票附件上传回参:{}", response.body()); |
| | | //保存请求记录 |
| | | paymentConfirmService.saveBipRequestRecord(request, "应收发票附件上传接口", response.body()); |
| | | JSONObject resultJson = JSONObject.parseObject(response.body()); |
| | | if (resultJson.containsKey("code") && resultJson.getString("code").equals("200")) { |
| | | JSONObject datas = resultJson.getJSONObject("data"); |
| | | JSONArray arr = datas.getJSONArray("data"); |
| | | if (CollUtil.isNotEmpty(arr)) { |
| | | JSONObject data0 = (JSONObject) arr.get(0); |
| | | contractInvoice.setBipFileId(data0.getString("id")); |
| | | baseMapper.updateById(contractInvoice); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 将 Base64 字符串转换为本地文件 |
| | | * |
| | | * @param base64Str Base64 编码的字符串 |
| | | * @param targetPath 目标文件路径(包含文件名和后缀,如 "D:/test.pdf") |
| | | */ |
| | | public static void saveBase64ToFile(String base64Str, String targetPath) { |
| | | if (base64Str == null || base64Str.isEmpty()) { |
| | | throw new IllegalArgumentException("Base64 字符串不能为空"); |
| | | } |
| | | |
| | | // 1. 处理可能存在的 Data URI 前缀 (如: "data:application/pdf;base64,JVBERi...") |
| | | if (base64Str.contains(",")) { |
| | | base64Str = base64Str.split(",")[1]; |
| | | } |
| | | |
| | | // 2. 解码 Base64 为字节数组 |
| | | byte[] fileBytes = Base64.getDecoder().decode(base64Str); |
| | | |
| | | File file = new File(targetPath); |
| | | |
| | | // 1. 获取父目录对象 |
| | | File parentDir = file.getParentFile(); |
| | | |
| | | // 2. 如果父目录不为空且不存在,则创建所有必需的父目录 |
| | | if (parentDir != null && !parentDir.exists()) { |
| | | // mkdirs() 会递归创建所有不存在的父目录 |
| | | // 必须检查返回值,因为可能因权限不足或路径非法而失败 |
| | | if (!parentDir.mkdirs()) { |
| | | // 如果 mkdirs 返回 false,再次检查是否存在(处理并发情况) |
| | | if (!parentDir.exists()) { |
| | | log.error("无法创建父目录: " + parentDir.getAbsolutePath()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 3. 创建文件本身 |
| | | // createNewFile() 仅在文件不存在时创建,如果已存在则返回 false 但不报错 |
| | | if (!file.exists()) { |
| | | boolean created = false; |
| | | try { |
| | | created = file.createNewFile(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if (!created) { |
| | | // 处理并发创建失败或其他异常 |
| | | log.error("无法创建文件: " + file.getAbsolutePath()); |
| | | } |
| | | } |
| | | |
| | | // 3. 将字节数组写入文件 |
| | | try (FileOutputStream fos = new FileOutputStream(file)) { |
| | | fos.write(fileBytes); |
| | | fos.flush(); |
| | | System.out.println("文件保存成功: " + targetPath); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | throw new RuntimeException("文件保存失败", e); |
| | | } |
| | | } |
| | | |
| | | private void pushBipInvoice(ContractInvoice contractInvoice) { |
| | |
| | | log.error("查询单位信息异常"); |
| | | return; |
| | | } |
| | | SysDept dept = r.getData(); |
| | | SysDept comp = r.getData(); |
| | | R<SysDept> rDept = remoteDeptService.getById(SecurityUtils.getUser().getDeptId()); |
| | | if (!r.isOk()) { |
| | | log.error("查询单位信息异常"); |
| | | return; |
| | | } |
| | | SysDept dept = rDept.getData(); |
| | | //蓝票 |
| | | if (StrUtil.equals(contractInvoice.getInvoiceCategory(), "1")) { |
| | | JSONObject params = genBlueReceiveParams(contractInvoice, contract, customer, dept); |
| | | |
| | | JSONObject params = genBlueReceiveParams(contractInvoice, contract, customer, comp, dept); |
| | | String finalAccessToken = accessToken; |
| | | |
| | | log.info("应收发票集成入参:{}", params.toJSONString()); |
| | | String result = HttpUtil.post(url + "/yonbip/EFI/receivable/save?access_token=" + finalAccessToken, params.toJSONString()); |
| | | String result = HttpUtil.post(url + "/yonbip/EFI/receivable/saveandsubmit?access_token=" + finalAccessToken, params.toJSONString()); |
| | | log.info("应收发票集成回参:{}", result); |
| | | //保存请求记录 |
| | | paymentConfirmService.saveBipRequestRecord(params, "应收发票集成接口", result); |
| | |
| | | JSONObject resultJson = JSONObject.parseObject(result); |
| | | if (resultJson.containsKey("code") && resultJson.getString("code").equals("200")) { |
| | | contractInvoice.setBipPush("1"); |
| | | contractInvoice.setBipBody(resultJson.getString("data")); |
| | | baseMapper.updateById(contractInvoice); |
| | | } |
| | | } |
| | |
| | | return jsonObject; |
| | | } |
| | | |
| | | private JSONObject genBlueReceiveParams(ContractInvoice contractInvoice, Contract contract, BusinessCustomer customer, SysDept dept) { |
| | | private JSONObject genBlueReceiveParams(ContractInvoice contractInvoice, Contract contract, BusinessCustomer customer |
| | | , SysDept comp, SysDept dept) { |
| | | JSONObject jsonObject = new JSONObject(); |
| | | JSONObject data = new JSONObject(); |
| | | if (contractInvoice.getInvoiceCategory().equals("1")) { |
| | |
| | | data.put("billDate", DateUtil.format(contractInvoice.getInvoiceTime(), DatePattern.NORM_DATETIME_FORMAT)); |
| | | data.put("exchangeRate", "1");//汇率 |
| | | data.put("exchangeRateDate", DateUtil.now());//汇率时间 |
| | | data.put("objectType", "1"); |
| | | data.put("objectType", "1");//往来对象类型(枚举值 1:客户; 2:员工;3:资金业务对象) |
| | | data.put("deptCode", dept.getOrgCode());//部门编码 SS2 |
| | | data.put("staffCode", SecurityUtils.getUser().getNickname());//业务员编码 |
| | | data.put("customerCode", customer.getBipCode());//客户编码 |
| | | data.put("financeOrgCode", dept.getBipCode());//开票组织编码 |
| | | data.put("orgCode", dept.getBipCode());//业务组织编码 |
| | | data.put("bustypeCode", "");//交易类型编码 暂估默认ZGYSFP,销售默认MJHYXGLXT |
| | | data.put("financeOrgCode", comp.getBipCode());//开票组织编码 |
| | | data.put("orgCode", comp.getBipCode());//业务组织编码 |
| | | data.put("bustypeCode", "ZBYXGLXT");//交易类型编码 暂估默认ZGYSFP,销售默认ZBYXGLXT |
| | | data.put("contractNo", contract.getContractNo()); |
| | | data.put("oriCurrencyCode", "CNY");//币种 |
| | | |
| | | data.put("_status", "Insert"); |
| | | GenInvoiceInfoVo vo = BeanUtil.copyProperties(JSONObject.parseObject(contractInvoice.getInvoicePreview()), GenInvoiceInfoVo.class); |
| | | JSONArray bodyItem = new JSONArray(); |
| | | for (InvoiceItemVo invoiceItemVo : vo.getInvoiceItemList()) { |
| | | for (InvoiceItemVo invoiceItemVo : vo.getBipVo().getItemList()) { |
| | | JSONObject item = new JSONObject(); |
| | | item.put("expenseItemCode",invoiceItemVo.getJjsx());//经济事项 |
| | | item.put("invoiceNo", contractInvoice.getInvoiceNo()); |
| | | item.put("taxRate", invoiceItemVo.getTaxRate()); |
| | | item.put("oriTaxAmount", invoiceItemVo.getPriceTax()); |
| | | item.put("oriTaxIncludedAmount", invoiceItemVo.getPriceAndTax()); |
| | | item.put("_status", "Insert"); |
| | | |
| | | JSONObject freeChId = new JSONObject(); |
| | | freeChId.put("GX08",invoiceItemVo.getHsdy());//核算单元 |
| | | freeChId.put("YSYF31",invoiceItemVo.getSfjs());//是否计税 |
| | | freeChId.put("JT04",invoiceItemVo.getHsjylx());//核算交易类型 0101 集团外部 01020301 三级分子公司间 01020302 三级分公司与子子公司之间 |
| | | freeChId.put("JT06",invoiceItemVo.getKxmx());//款项明细 |
| | | freeChId.put("JT32",invoiceItemVo.getZyys());//作业要素 |
| | | freeChId.put("zzbkfl",invoiceItemVo.getBkfl());//板块分类 |
| | | freeChId.put("JT12",invoiceItemVo.getCpwlfl());//产品物料分类 |
| | | item.put("freeChId",freeChId); |
| | | bodyItem.add(item); |
| | | } |
| | | data.put("bodyItem", bodyItem); |
| | | JSONObject freeChId = new JSONObject(); |
| | | freeChId.put("YSYF_SFZY",vo.getBipVo().getSfzy());//是否主营 |
| | | freeChId.put("fk_038",vo.getBipVo().getPzzdr());//凭证制单人 |
| | | freeChId.put("YSYF_08",vo.getBipVo().getSfnbwl());//是否内部往来 |
| | | freeChId.put("YSYF24",vo.getBipVo().getSfjyjs());//是否简易计税 |
| | | freeChId.put("fk_002",vo.getBipVo().getFjzs());//附件1 |
| | | freeChId.put("GX08",vo.getBipVo().getHsdy());//核算单元 |
| | | freeChId.put("TzMY042",contract.getExpirationDate());//预计回款日期 |
| | | data.put("freeChId", freeChId); |
| | | data.put("remarks",vo.getBipVo().getZy());//摘要 |
| | | jsonObject.put("data", data); |
| | | return jsonObject; |
| | | } |
| | | |
| | | private String transEconomic(String economicMatters) { |
| | | if (economicMatters.equals("1")){ |
| | | return "09001006"; |
| | | } |
| | | if (economicMatters.equals("2")){ |
| | | return "09004003"; |
| | | } |
| | | if (economicMatters.equals("3")){ |
| | | return "09003005"; |
| | | } |
| | | if (economicMatters.equals("4")){ |
| | | return "09002042"; |
| | | } |
| | | if (economicMatters.equals("5")){ |
| | | return "09002002"; |
| | | } |
| | | if (economicMatters.equals("6")){ |
| | | return "09004001"; |
| | | } |
| | | if (economicMatters.equals("7")){ |
| | | return "09003002001"; |
| | | } |
| | | if (economicMatters.equals("8")){ |
| | | return "09003002002"; |
| | | } |
| | | if (economicMatters.equals("9")){ |
| | | return "09003002003"; |
| | | } |
| | | if (economicMatters.equals("10")){ |
| | | return "09003003"; |
| | | } |
| | | return ""; |
| | | } |
| | | |
| | | @Override |
| | |
| | | if (bwFlag) { |
| | | List<ContractInvoice> contractInvoices = baseMapper.selectList(Wrappers.<ContractInvoice>lambdaQuery() |
| | | .eq(ContractInvoice::getInvoiceCategory, "2").eq(ContractInvoice::getInvoiceStatus, "3")); |
| | | if (ArrayUtil.isNotEmpty(contractInvoices)) { |
| | | if (CollUtil.isNotEmpty(contractInvoices)) { |
| | | contractInvoices.stream().forEach(contractInvoice -> { |
| | | JSONObject request = new JSONObject(); |
| | | request.put("taxNo", contractInvoice.getTaxNo()); |
| | |
| | | return; |
| | | } |
| | | JSONArray models = resultObj.getJSONArray("model"); |
| | | if (ArrayUtil.isNotEmpty(models)) { |
| | | if (CollUtil.isNotEmpty(models)) { |
| | | JSONObject model = (JSONObject) models.get(0); |
| | | |
| | | if (StrUtil.isEmpty(model.getString("confirmBillingMark")) || StrUtil.equals(model.getString("confirmBillingMark"), "N")) { |
| | |
| | | contractInvoice.setInvoiceNo(model.getString("redInvoiceNo")); |
| | | contractInvoice.setRedReversal("1"); |
| | | //推送BIP |
| | | pushBipInvoice(contractInvoice); |
| | | // pushBipInvoice(contractInvoice); |
| | | } |
| | | } |
| | | }); |
| | |
| | | @Override |
| | | public void takeEffect(Long id) { |
| | | ContractInvoice invoice = baseMapper.selectById(id); |
| | | R<SysDept> r = remoteDeptService.getById(SecurityUtils.getUser().getCompId()); |
| | | if (!r.isOk()) { |
| | | log.error("单位信息查询异常"); |
| | | return; |
| | | } |
| | | SysDept dept = r.getData(); |
| | | Contract contract = contractMapper.selectById(invoice.getContractId()); |
| | | GenInvoiceInfoVo vo = BeanUtil.copyProperties(JSONObject.parseObject(invoice.getInvoicePreview()), GenInvoiceInfoVo.class); |
| | | vo.setContractCategory(contract.getContractCategory()); |
| | | invoice.setApprovalStatus("1"); |
| | | baseMapper.updateById(invoice); |
| | | invoice.setBwRequestResult("0"); |
| | | |
| | | //蓝票 |
| | | if (invoice.getInvoiceCategory().equals("1")) { |
| | | // TODO 开蓝票接口 |
| | | JSONObject request = genBlueInvoice(vo, invoice.getSerialNo()); |
| | | if (bwFlag) { |
| | | // TODO 开蓝票接口机动车销售 |
| | | if (StrUtil.equals(vo.getInvoiceVehicleType(), "4")) { |
| | | JSONObject request = genBlueInvoiceVehicle(vo, invoice.getSerialNo(), dept); |
| | | String requestId = IdUtil.fastUUID(); |
| | | invoice.setBwRequestId(requestId); |
| | | invoice.setBwRequestResult("1"); |
| | | invoice.setBwRequestParam(request.toJSONString()); |
| | | log.info("机动车销售统一发票开具请求接口入参:{}", request.toJSONString()); |
| | | String result = HttpUtil.post(bwUrl + "method=baiwang.output.invoice.vehicleissue&version=1.0&requestId=" + requestId, request.toJSONString()); |
| | | log.info("机动车销售统一发票开具请求接口回参:{}", result); |
| | | saveBwRequestRecord("baiwang.output.invoice.vehicleissue", requestId, request.toJSONString(), result); |
| | | JSONObject resultObj = JSONObject.parseObject(result); |
| | | if (resultObj.containsKey("requestId") && StrUtil.equals(resultObj.getString("requestId"), requestId) && |
| | | resultObj.getBoolean("success")) { |
| | | log.info("开票成功!!!"); |
| | | invoice.setBwRequestResult("1"); |
| | | } |
| | | JSONObject modelJson = resultObj.getJSONObject("model"); |
| | | if (ObjUtil.isNotNull(modelJson)) { |
| | | Date invoiceDate = DateUtil.parse(modelJson.getString("invoiceDate"), DatePattern.PURE_DATETIME_PATTERN); |
| | | invoice.setInvoiceTime(invoiceDate); |
| | | invoice.setInvoiceNo(modelJson.getString("invoiceNo")); |
| | | //推送BIP |
| | | // pushBipInvoice(invoice); |
| | | } |
| | | } |
| | | |
| | | // TODO 开蓝票接口机动车专用 |
| | | if (StrUtil.equals(vo.getInvoiceVehicleType(), "3")) { |
| | | JSONObject request = genBlueInvoiceVehiclePre(vo, invoice.getSerialNo(), dept); |
| | | String requestId = IdUtil.fastUUID(); |
| | | invoice.setBwRequestId(requestId); |
| | | invoice.setBwRequestParam(request.toJSONString()); |
| | | log.info("机动车专用发票开具请求接口入参:{}", request.toJSONString()); |
| | | String result = HttpUtil.post(bwUrl + "method=baiwang.output.invoice.issue&version=1.0&requestId=" + requestId, request.toJSONString()); |
| | | log.info("机动车专用发票开具请求接口回参:{}", result); |
| | | saveBwRequestRecord("baiwang.output.invoice.issue", requestId, request.toJSONString(), result); |
| | | JSONObject resultObj = JSONObject.parseObject(result); |
| | | if (resultObj.containsKey("requestId") && StrUtil.equals(resultObj.getString("requestId"), requestId) && |
| | | resultObj.getBoolean("success")) { |
| | | log.info("开票成功!!!"); |
| | | invoice.setBwRequestResult("1"); |
| | | } |
| | | JSONObject modelJson = resultObj.getJSONObject("model"); |
| | | if (ObjUtil.isNotNull(modelJson)) { |
| | | Date invoiceDate = DateUtil.parse(modelJson.getString("invoiceDate"), DatePattern.PURE_DATETIME_PATTERN); |
| | | invoice.setInvoiceTime(invoiceDate); |
| | | invoice.setInvoiceNo(modelJson.getString("invoiceNo")); |
| | | //推送BIP |
| | | // pushBipInvoice(invoice); |
| | | } |
| | | } |
| | | |
| | | // TODO 开蓝票接口 非机动车 |
| | | if (!StrUtil.equals(vo.getInvoiceVehicleType(), "3")&&!StrUtil.equals(vo.getInvoiceVehicleType(), "4")) { |
| | | JSONObject request = genBlueInvoice(vo, invoice.getSerialNo(), dept); |
| | | String requestId = IdUtil.fastUUID(); |
| | | invoice.setBwRequestId(requestId); |
| | | invoice.setBwRequestParam(request.toJSONString()); |
| | | log.info("蓝票请求接口入参:{}", request.toJSONString()); |
| | | String result = HttpUtil.post(bwUrl + "method=baiwang.output.invoice.issue&version=1.0&requestId=" + requestId, request.toJSONString()); |
| | |
| | | invoice.setInvoiceTime(invoiceDate); |
| | | invoice.setInvoiceNo(model.getString("invoiceNo")); |
| | | //推送BIP |
| | | pushBipInvoice(invoice); |
| | | // pushBipInvoice(invoice); |
| | | } |
| | | } |
| | | } |
| | | if (StrUtil.isNotBlank(vo.getAdvanceInvoice())){ |
| | | if (StrUtil.isNotBlank(vo.getAdvanceInvoice())) { |
| | | contract.setBillingAmout(vo.getTotalPrice().add(contract.getBillingAmout())); |
| | | contractMapper.updateById(contract); |
| | | return; |
| | | } |
| | | if (!StrUtil.equals(contract.getContractCategory(), "water_house")) { |
| | | //合同状态 |
| | | if (StrUtil.equals(contract.getBillingStatus(), "2")) { |
| | | contract.setBillingAmout(vo.getTotalPrice().add(contract.getBillingAmout())); |
| | | if (contract.getBillingAmout().compareTo(contract.getAmount()) == 0) { |
| | | contract.setBillingStatus("3"); |
| | | } |
| | | if (contract.getPaidAmount().compareTo(contract.getAmount().subtract(contract.getDropAmount())) >= 0&& |
| | | contract.getBillingAmout().compareTo(contract.getAmount()) == 0){ |
| | | contract.setContractStatus(3); |
| | | } |
| | | if (contract.getBillingAmout().compareTo(contract.getAmount()) < 0) { |
| | | contract.setBillingStatus("1"); |
| | | List<ContractOutBound> outBoundList = contractOutBoundMapper.selectList(Wrappers.<ContractOutBound>lambdaQuery() |
| | | .eq(ContractOutBound::getContractId, contract.getId()) |
| | | .apply("invoice_num < out_bound_num")); |
| | | if (CollUtil.isNotEmpty(outBoundList)) { |
| | | contract.setBillingStatus("2"); |
| | | } |
| | | |
| | | } |
| | | //防止重复审开票审核通过后 开票金额超出合同金额 |
| | | if (contract.getBillingAmout().compareTo(contract.getAmount()) > 0) { |
| | | contract.setBillingAmout(contract.getAmount()); |
| | | } |
| | | contractMapper.updateById(contract); |
| | | |
| | | } else { |
| | | //判断 循环是否完成 |
| | | List<ContractExecDate> execDateList = contractExecDateMapper.selectList(Wrappers.<ContractExecDate>lambdaQuery().eq(ContractExecDate::getContractId, contract.getId()) |
| | | .eq(ContractExecDate::getGenFlag, "0")); |
| | | if (CollUtil.isEmpty(execDateList)) { |
| | | //判断是否有可开票记录 |
| | | List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery() |
| | | .eq(ContractPaymentSchedule::getContractId, contract.getId()).eq(ContractPaymentSchedule::getInvoiceFlag, "0")); |
| | | if (CollUtil.isEmpty(scheduleList)) { |
| | | contract.setBillingStatus("3"); |
| | | } else { |
| | | contract.setBillingStatus("2"); |
| | | } |
| | | contract.setBillingAmout(vo.getTotalPrice().add(contract.getBillingAmout())); |
| | | if (contract.getBillingAmout().compareTo(contract.getAmount()) == 0) { |
| | | contract.setBillingStatus("3"); |
| | | } |
| | | if (contract.getBillingAmout().compareTo(contract.getAmount()) < 0) { |
| | | contract.setBillingStatus("1"); |
| | | List<ContractOutBound> outBoundList = contractOutBoundMapper.selectList(Wrappers.<ContractOutBound>lambdaQuery() |
| | | .eq(ContractOutBound::getContractId,contract.getId()) |
| | | .apply("invoice_num < out_bound_num")); |
| | | if (CollUtil.isNotEmpty(outBoundList)){ |
| | | contract.setBillingStatus("2"); |
| | | } |
| | | |
| | | if (contract.getBillingAmout().compareTo(contract.getAmount()) == 0 && |
| | | contract.getPaidAmount().compareTo(contract.getAmount().subtract(contract.getDropAmount())) >= 0) { |
| | | contract.setBillingStatus("3"); |
| | | contract.setContractStatus(3); |
| | | } |
| | | //防止重复审开票审核通过后 开票金额超出合同金额 |
| | | if (contract.getBillingAmout().compareTo(contract.getAmount()) > 0) { |
| | | contract.setBillingAmout(contract.getAmount()); |
| | | } |
| | | contractMapper.updateById(contract); |
| | | } |
| | | } else { |
| | | //判断 循环是否完成 |
| | | List<ContractExecDate> execDateList = contractExecDateMapper.selectList(Wrappers.<ContractExecDate>lambdaQuery().eq(ContractExecDate::getContractId, contract.getId()) |
| | | .eq(ContractExecDate::getGenFlag, "0")); |
| | | List<MeterReadRecord> recordList = meterReadRecordMapper.selectList(Wrappers.<MeterReadRecord>lambdaQuery() |
| | | .eq(MeterReadRecord::getContractId, contract.getId()) |
| | | .isNull(MeterReadRecord::getMeterReadNum)); |
| | | if (ArrayUtil.isEmpty(execDateList) && ArrayUtil.isEmpty(recordList)) { |
| | | //判断是否有可开票记录 |
| | | List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery() |
| | | .eq(ContractPaymentSchedule::getContractId, contract.getId()).eq(ContractPaymentSchedule::getInvoiceFlag, "0")); |
| | | if (ArrayUtil.isEmpty(scheduleList)) { |
| | | contract.setBillingStatus("3"); |
| | | } else { |
| | | contract.setBillingStatus("2"); |
| | | } |
| | | |
| | | } else { |
| | | //判断是否有可开票记录 |
| | | List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery() |
| | | .eq(ContractPaymentSchedule::getContractId, contract.getId())); |
| | | if (ArrayUtil.isEmpty(scheduleList)) { |
| | | if (CollUtil.isEmpty(scheduleList)) { |
| | | contract.setBillingStatus("1"); |
| | | } else { |
| | | contract.setBillingStatus("2"); |
| | | } |
| | | } |
| | | contract.setBillingAmout(vo.getTotalPrice().add(contract.getBillingAmout())); |
| | | //防止重复审开票审核通过后 开票金额超出合同金额 |
| | | if (contract.getBillingAmout().compareTo(contract.getAmount()) > 0) { |
| | | contract.setBillingAmout(contract.getAmount()); |
| | | } |
| | | |
| | | contractMapper.updateById(contract); |
| | | } |
| | | baseMapper.updateById(invoice); |
| | |
| | | if (invoice.getInvoiceCategory().equals("2")) { |
| | | // TODO 开红票接口 |
| | | ContractInvoice blueInvoice = baseMapper.selectById(invoice.getBlueInvoiceId()); |
| | | JSONObject request = genRedInvoice(contract, blueInvoice, invoice.getSerialNo()); |
| | | JSONObject request = genRedInvoice(contract,blueInvoice, invoice, invoice.getSerialNo()); |
| | | String requestId = IdUtil.fastUUID(); |
| | | invoice.setBwRequestId(requestId); |
| | | invoice.setBwRequestParam(request.toJSONString()); |
| | |
| | | invoice.setInvoiceNo(model.getString("redInvoiceNo")); |
| | | invoice.setRedReversal("1"); |
| | | //推送BIP |
| | | pushBipInvoice(invoice); |
| | | // pushBipInvoice(invoice); |
| | | } |
| | | } |
| | | } |
| | | baseMapper.updateById(invoice); |
| | | contract.setBillingAmout(contract.getBillingAmout().subtract(invoice.getInvoiceTotalPriceTax())); |
| | | contract.setBillingAmout(contract.getBillingAmout().add(invoice.getInvoiceTotalPriceTax())); |
| | | //防止重复审开票审核通过后 开票金额为负 |
| | | if (contract.getBillingAmout().compareTo(new BigDecimal("0")) < 0) { |
| | | contract.setBillingAmout(new BigDecimal("0")); |
| | |
| | | contractMapper.updateById(contract); |
| | | blueInvoice.setRedReversal("1"); |
| | | baseMapper.updateById(blueInvoice); |
| | | if (StrUtil.isNotBlank(vo.getAdvanceInvoice())){ |
| | | if (StrUtil.isNotBlank(vo.getAdvanceInvoice())) { |
| | | //还原预开数量 |
| | | List<ContractOutBoundInvoiceVo> outBoundInvoiceVoList = BeanUtil.copyToList(JSONArray.parseArray(blueInvoice.getInvoicePreviewParam()), ContractOutBoundInvoiceVo.class); |
| | | if (ArrayUtil.isNotEmpty(outBoundInvoiceVoList)) { |
| | | if (CollUtil.isNotEmpty(outBoundInvoiceVoList)) { |
| | | outBoundInvoiceVoList.stream().forEach(contractOutBoundInvoiceVo -> { |
| | | ContractSubjectMatter subjectMatter = contractSubjectMatterMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId()); |
| | | if (ObjUtil.isNotNull(subjectMatter)){ |
| | | if (ObjUtil.isNotNull(subjectMatter)) { |
| | | subjectMatter.setAdvanceNum(subjectMatter.getAdvanceNum().subtract(contractOutBoundInvoiceVo.getInvoiceNum())); |
| | | if (subjectMatter.getAdvanceNum().compareTo(new BigDecimal("0"))<=0){ |
| | | if (subjectMatter.getAdvanceNum().compareTo(new BigDecimal("0")) <= 0) { |
| | | subjectMatter.setAdvanceNum(new BigDecimal("0")); |
| | | } |
| | | contractSubjectMatterMapper.updateById(subjectMatter); |
| | |
| | | if (!StrUtil.equals(contract.getContractCategory(), "water_house")) { |
| | | //更新合同出库单状态 |
| | | List<ContractOutBoundInvoiceVo> outBoundInvoiceVoList = BeanUtil.copyToList(JSONArray.parseArray(blueInvoice.getInvoicePreviewParam()), ContractOutBoundInvoiceVo.class); |
| | | if (ArrayUtil.isNotEmpty(outBoundInvoiceVoList)) { |
| | | if (CollUtil.isNotEmpty(outBoundInvoiceVoList)) { |
| | | outBoundInvoiceVoList.stream().forEach(contractOutBoundInvoiceVo -> { |
| | | ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId()); |
| | | if (contractOutBoundInvoiceVo.getInvoiceNum().compareTo(contractOutBound.getOutBoundNum().subtract(contractOutBound.getInvoiceNum())) <= 0) { |
| | | contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().subtract(contractOutBoundInvoiceVo.getInvoiceNum())); |
| | | contractOutBoundMapper.updateById(contractOutBound); |
| | | } |
| | | contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().subtract(contractOutBoundInvoiceVo.getInvoiceNum())); |
| | | contractOutBoundMapper.updateById(contractOutBound); |
| | | }); |
| | | } |
| | | } else { |
| | | //更新合同房租 水电 费开票状态 |
| | | List<ContractOutBoundInvoiceVo> outBoundInvoiceVoList = BeanUtil.copyToList(JSONArray.parseArray(blueInvoice.getInvoicePreviewParam()), ContractOutBoundInvoiceVo.class); |
| | | if (ArrayUtil.isNotEmpty(outBoundInvoiceVoList)) { |
| | | if (CollUtil.isNotEmpty(outBoundInvoiceVoList)) { |
| | | outBoundInvoiceVoList.stream().forEach(contractOutBoundInvoiceVo -> { |
| | | ContractPaymentSchedule schedule = contractPaymentScheduleMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId()); |
| | | schedule.setInvoiceFlag("0"); |
| | |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | private JSONObject genBlueInvoiceVehiclePre(GenInvoiceInfoVo vo, String serialNo, SysDept dept) { |
| | | JSONObject vechicleData = JSONObject.parseObject(vo.getVehicleData()); |
| | | JSONObject jsonObject = new JSONObject(); |
| | | //销方 |
| | | jsonObject.put("taxNo", vo.getPartyBOrgCode());//税号 |
| | | jsonObject.put("taxUserName", SecurityUtils.getUser().getPhone());//数电账号 |
| | | |
| | | jsonObject.put("formatGenerate", false);//是否需要生成版式返回版式链接(true / false) |
| | | jsonObject.put("formatPushType", false);//版式生成是否推送(true / false) |
| | | jsonObject.put("taxUserName", dept.getInvoiceContact()); |
| | | //data |
| | | JSONObject data = new JSONObject(); |
| | | data.put("invoiceTypeCode", "01"); |
| | | data.put("invoiceType", "0"); |
| | | data.put("serialNo", serialNo); |
| | | data.put("invoiceSpecialMark","14");//特定业务标识 14-机动车 |
| | | // data.put("taxationMethod","2"); |
| | | // data.put("taxationLabel","02");//发票类型代码为“01-数电发票(增值税专用发票)、02-数电发票(普通发票)”时且征税方式为“2-差额征税”时必填 |
| | | List<JSONObject> invoiceDetailsList = new ArrayList<>(); |
| | | Integer lineNo = 1; |
| | | //data.invoiceDetailsList |
| | | for (InvoiceItemVo invoiceItemVo : vo.getInvoiceItemList()) { |
| | | JSONObject invoiceDetail = new JSONObject(); |
| | | |
| | | invoiceDetail.put("goodsLineNo", lineNo); |
| | | invoiceDetail.put("goodsName", invoiceItemVo.getItemName()); |
| | | invoiceDetail.put("projectName", invoiceItemVo.getItemName()); |
| | | invoiceDetail.put("goodsCode", invoiceItemVo.getTaxCode()); |
| | | invoiceDetail.put("goodsTaxRate", invoiceItemVo.getTaxRate().divide(new BigDecimal("100"))); |
| | | invoiceDetail.put("goodsUnit", "辆"); |
| | | invoiceDetail.put("goodsQuantity", invoiceItemVo.getQuantity()); |
| | | // invoiceDetail.put("goodsPrice", invoiceItemVo.getUnitPrice()); |
| | | invoiceDetail.put("goodsSpecification", vo.getVehicleNo()); |
| | | invoiceDetail.put("goodsTotalPrice", invoiceItemVo.getPriceNoTax()); |
| | | invoiceDetail.put("goodsTotalTax", invoiceItemVo.getPriceTax()); |
| | | |
| | | invoiceDetailsList.add(invoiceDetail); |
| | | lineNo++; |
| | | } |
| | | data.put("invoiceDetailsList", invoiceDetailsList); |
| | | //购方 |
| | | data.put("buyerTaxNo", vo.getPartyAOrgCode()); |
| | | data.put("buyerName", vo.getPartyA()); |
| | | data.put("sellerTaxNo", vo.getPartyBOrgCode());// TODO 税务总局系统登陆人手机号 |
| | | data.put("invoiceTotalPrice", vo.getTotalPrice().subtract(vo.getTotalTax())); |
| | | data.put("invoiceTotalPriceTax", vo.getTotalPrice()); |
| | | data.put("invoiceTotalTax", vo.getTotalTax()); |
| | | data.put("buyerBankName", vo.getPartyAOpenBank()); |
| | | data.put("buyerBankNumber", vo.getPartyABankAccount()); |
| | | |
| | | jsonObject.put("data", data); |
| | | return jsonObject; |
| | | } |
| | | |
| | | private JSONObject genBlueInvoiceVehicle(GenInvoiceInfoVo vo, String serialNo, SysDept dept) { |
| | | JSONObject vechicleData = JSONObject.parseObject(vo.getVehicleData()); |
| | | JSONObject jsonObject = new JSONObject(); |
| | | //销方 |
| | | jsonObject.put("taxNo", vo.getPartyBOrgCode());//税号 |
| | | jsonObject.put("invoiceTerminalCode", "JDCwxj");//超过发票单张限额是否需要拆分开具,默认不拆分(拆分只支持发票类型代码为 专票 004 普票 007 电子票 026的 |
| | | jsonObject.put("formatGenerate", false);//是否需要生成版式返回版式链接(true / false) |
| | | jsonObject.put("formatPushType", false);//版式生成是否推送(true / false) |
| | | jsonObject.put("taxUserName", dept.getInvoiceContact()); |
| | | //data |
| | | JSONObject data = new JSONObject(); |
| | | data.put("invoiceTypeCode", "03"); |
| | | data.put("invoiceType", "0"); |
| | | data.put("serialNo", serialNo); |
| | | //data.invoiceDetailsList |
| | | Integer lineNo = 1; |
| | | JSONObject invoiceDetail = new JSONObject(); |
| | | |
| | | invoiceDetail.put("vehicleNo", vo.getVehicleNo()); |
| | | invoiceDetail.put("manufacturingEnterprise", "03"); |
| | | invoiceDetail.put("originPlace", vechicleData.getString("originPlace")); |
| | | invoiceDetail.put("certificateNo", vo.getCertificateNo()); |
| | | invoiceDetail.put("engineNo", vechicleData.getString("engineNo")); |
| | | invoiceDetail.put("tonnage", vechicleData.getString("tonnage")); |
| | | invoiceDetail.put("passengersLimited", vechicleData.getString("passengersLimited")); |
| | | invoiceDetail.put("brandModel", vechicleData.getString("brandModel")); |
| | | |
| | | invoiceDetail.put("goodsCode", vo.getInvoiceItemList().get(0).getTaxCode()); |
| | | invoiceDetail.put("goodsTaxRate", vo.getInvoiceItemList().get(0).getTaxRate().divide(new BigDecimal("100"))); |
| | | if (!StrUtil.equals(vo.getContractCategory(), "water_house")) { |
| | | invoiceDetail.put("goodsTotalPrice", vo.getInvoiceItemList().get(0).getPriceNoTax()); |
| | | } else { |
| | | invoiceDetail.put("goodsTotalPrice", vo.getInvoiceItemList().get(0).getPriceNoTax()); |
| | | } |
| | | invoiceDetail.put("goodsTotalTax", vo.getInvoiceItemList().get(0).getPriceTax()); |
| | | data.put("invoiceVehicleInfo", invoiceDetail); |
| | | //购方 |
| | | data.put("buyerTaxNo", vo.getPartyAOrgCode()); |
| | | data.put("buyerName", vo.getPartyA()); |
| | | data.put("sellerTaxNo", vo.getPartyBOrgCode());// TODO 税务总局系统登陆人手机号 |
| | | data.put("invoiceTotalPrice", vo.getTotalPrice().subtract(vo.getTotalTax())); |
| | | data.put("invoiceTotalPriceTax", vo.getTotalPrice()); |
| | | data.put("invoiceTotalTax", vo.getTotalTax()); |
| | | data.put("buyerBankName", vo.getPartyAOpenBank()); |
| | | data.put("buyerBankNumber", vo.getPartyABankAccount()); |
| | | |
| | | jsonObject.put("data", data); |
| | | return jsonObject; |
| | | } |
| | | |
| | | @Override |
| | |
| | | if (!StrUtil.equals(contract.getContractCategory(), "water_house")) { |
| | | //更新合同出库单状态 |
| | | List<ContractOutBoundInvoiceVo> outBoundInvoiceVoList = BeanUtil.copyToList(JSONArray.parseArray(invoice.getInvoicePreviewParam()), ContractOutBoundInvoiceVo.class); |
| | | if (ArrayUtil.isNotEmpty(outBoundInvoiceVoList)) { |
| | | if (CollUtil.isNotEmpty(outBoundInvoiceVoList)) { |
| | | outBoundInvoiceVoList.stream().forEach(contractOutBoundInvoiceVo -> { |
| | | ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId()); |
| | | if (contractOutBoundInvoiceVo.getInvoiceNum().compareTo(contractOutBound.getOutBoundNum().subtract(contractOutBound.getInvoiceNum())) <= 0) { |
| | | contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().subtract(contractOutBoundInvoiceVo.getInvoiceNum())); |
| | | contractOutBoundMapper.updateById(contractOutBound); |
| | | } |
| | | contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().subtract(contractOutBoundInvoiceVo.getInvoiceNum())); |
| | | contractOutBoundMapper.updateById(contractOutBound); |
| | | }); |
| | | } |
| | | } else { |
| | | //更新合同房租 水电 费开票状态 |
| | | List<ContractOutBoundInvoiceVo> outBoundInvoiceVoList = BeanUtil.copyToList(JSONArray.parseArray(invoice.getInvoicePreviewParam()), ContractOutBoundInvoiceVo.class); |
| | | if (ArrayUtil.isNotEmpty(outBoundInvoiceVoList)) { |
| | | if (CollUtil.isNotEmpty(outBoundInvoiceVoList)) { |
| | | outBoundInvoiceVoList.stream().forEach(contractOutBoundInvoiceVo -> { |
| | | ContractPaymentSchedule schedule = contractPaymentScheduleMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId()); |
| | | schedule.setInvoiceFlag("0"); |
| | |
| | | invoice.setInvoiceTime(invoiceDate); |
| | | invoice.setInvoiceNo(model.getString("invoiceNo")); |
| | | //推送BIP |
| | | pushBipInvoice(invoice); |
| | | // pushBipInvoice(invoice); |
| | | } |
| | | } |
| | | } |
| | |
| | | JSONObject resultObj = JSONObject.parseObject(result); |
| | | if (resultObj.containsKey("requestId") && StrUtil.equals(resultObj.getString("requestId"), requestId) && |
| | | !resultObj.getBoolean("success")) { |
| | | log.error("红字确认单成功!!!"); |
| | | log.error("红字确认单失败!!!"); |
| | | |
| | | return R.failed(resultObj.getString("message")); |
| | | } |
| | | invoice.setBwRequestResult("1"); |
| | | JSONArray models = resultObj.getJSONArray("model"); |
| | | if (ArrayUtil.isNotEmpty(models)) { |
| | | if (CollUtil.isNotEmpty(models)) { |
| | | JSONObject model = (JSONObject) models.get(0); |
| | | if (StrUtil.isEmpty(model.getString("confirmBillingMark")) || StrUtil.equals(model.getString("confirmBillingMark"), "N")) { |
| | | //等待确认 |
| | |
| | | invoice.setInvoiceNo(model.getString("redInvoiceNo")); |
| | | invoice.setRedReversal("1"); |
| | | //推送BIP |
| | | pushBipInvoice(invoice); |
| | | // pushBipInvoice(invoice); |
| | | } |
| | | } |
| | | } |
| | |
| | | baseMapper.updateById(invoice); |
| | | return R.ok(); |
| | | } |
| | | |
| | | @Override |
| | | public R bipPush(Long invoiceId) { |
| | | ContractInvoice contractInvoice = baseMapper.selectById(invoiceId); |
| | | String accessToken = ""; |
| | | if (redisTemplate.hasKey("BIP_TOKEN")) { |
| | | accessToken = (String) redisTemplate.opsForValue().get("BIP_TOKEN"); |
| | | } else { |
| | | accessToken = paymentConfirmService.getAccessToken(accessToken); |
| | | } |
| | | if (StrUtil.isEmpty(accessToken)) { |
| | | log.error("bip accessToken 获取异常"); |
| | | return R.failed("bip accessToken 获取异常"); |
| | | } |
| | | Contract contract = contractMapper.selectById(contractInvoice.getContractId()); |
| | | if (ObjUtil.isNull(contract)) { |
| | | log.error("查询合同信息异常"); |
| | | return R.failed("查询合同信息异常"); |
| | | } |
| | | BusinessCustomer customer = businessCustomerMapper.selectById(contract.getPartyAId()); |
| | | if (ObjUtil.isNull(customer) || StrUtil.isEmpty(customer.getBipCode())) { |
| | | log.error("查询合同信息异常"); |
| | | return R.failed("查询合同信息异常"); |
| | | } |
| | | R<SysDept> r = remoteDeptService.getById(contract.getPartyBId()); |
| | | if (!r.isOk()) { |
| | | log.error("查询单位信息异常"); |
| | | return R.failed("查询单位信息异常"); |
| | | } |
| | | SysDept comp = r.getData(); |
| | | |
| | | R<SysDept> rDept = remoteDeptService.getById(SecurityUtils.getUser().getDeptId()); |
| | | if (!r.isOk()) { |
| | | log.error("查询部门信息异常"); |
| | | return R.failed("查询部门信息异常"); |
| | | } |
| | | SysDept dept = rDept.getData(); |
| | | |
| | | //蓝票 |
| | | if (StrUtil.equals(contractInvoice.getInvoiceCategory(), "1")) { |
| | | JSONObject params = genBlueReceiveParams(contractInvoice, contract, customer, comp, dept); |
| | | String finalAccessToken = accessToken; |
| | | |
| | | log.info("应收发票集成入参:{}", params.toJSONString()); |
| | | String result = HttpUtil.post(url + "/yonbip/EFI/receivable/saveandsubmit?access_token=" + finalAccessToken, params.toJSONString()); |
| | | log.info("应收发票集成回参:{}", result); |
| | | //保存请求记录 |
| | | paymentConfirmService.saveBipRequestRecord(params, "应收发票集成接口", result); |
| | | JSONObject resultJson = JSONObject.parseObject(result); |
| | | if (resultJson.containsKey("code") && resultJson.getString("code").equals("200")) { |
| | | JSONObject data = resultJson.getJSONObject("data"); |
| | | contractInvoice.setBipPush("1"); |
| | | contractInvoice.setBipId(data.getLong("id")); |
| | | contractInvoice.setBipBody(resultJson.getString("data")); |
| | | baseMapper.updateById(contractInvoice); |
| | | return R.ok(); |
| | | } |
| | | return R.failed(resultJson, "应收发票集成失败"); |
| | | } |
| | | //红票 |
| | | if (StrUtil.equals(contractInvoice.getInvoiceCategory(), "2")) { |
| | | JSONObject params = genRedReceiveParams(contractInvoice); |
| | | String finalAccessToken = accessToken; |
| | | |
| | | log.info("应收发票删除入参:{}", params.toJSONString()); |
| | | String result = HttpUtil.post(url + "/yonbip/EFI/receivable/delete?access_token=" + finalAccessToken, params.toJSONString()); |
| | | log.info("应收发票删除回参:{}", result); |
| | | //保存请求记录 |
| | | paymentConfirmService.saveBipRequestRecord(params, "应收发票删除接口", result); |
| | | JSONObject resultJson = JSONObject.parseObject(result); |
| | | if (resultJson.containsKey("code") && resultJson.getString("code").equals("200")) { |
| | | contractInvoice.setBipPush("1"); |
| | | contractInvoice.setBipBody(resultJson.getString("data")); |
| | | baseMapper.updateById(contractInvoice); |
| | | return R.ok(); |
| | | } |
| | | return R.failed(resultJson, "应收发票删除失败"); |
| | | } |
| | | return R.ok(); |
| | | } |
| | | |
| | | @Override |
| | | public R queryInvoice(Page page, InvoiceQueryDTO queryDTO) { |
| | | R<SysDept> r = remoteDeptService.getById(SecurityUtils.getUser().getCompId()); |
| | | if (!r.isOk()) { |
| | | log.error("查询单位信息异常"); |
| | | return R.failed("查询单位信息异常"); |
| | | } |
| | | SysDept comp = r.getData(); |
| | | |
| | | JSONObject request = genQueryInvoiceParam(page, queryDTO, comp); |
| | | String requestId = IdUtil.fastUUID(); |
| | | |
| | | log.info("已开发票查询接口入参:{},", request.toJSONString()); |
| | | String result = HttpUtil.post(bwUrl + "method=baiwang.output.invoice.query&version=1.0&request_id=" + requestId, request.toJSONString()); |
| | | log.info("已开发票查询接口回参:{},", result); |
| | | saveBwRequestRecord("baiwang.output.invoice.query", requestId, request.toJSONString(), result); |
| | | JSONObject resultObj = JSONObject.parseObject(result); |
| | | if (resultObj.containsKey("requestId") && StrUtil.equals(resultObj.getString("requestId"), requestId) && |
| | | !resultObj.getBoolean("success")) { |
| | | return R.failed("已开发票查询", result); |
| | | } |
| | | JSONArray models = resultObj.getJSONArray("model"); |
| | | if (CollUtil.isNotEmpty(models)) { |
| | | page.setRecords(models); |
| | | page.setTotal(resultObj.getInteger("total")); |
| | | return R.ok(page); |
| | | } |
| | | page.setTotal(0); |
| | | page.setRecords(null); |
| | | return R.ok(page); |
| | | } |
| | | |
| | | private JSONObject genQueryInvoiceParam(Page page, InvoiceQueryDTO queryDTO, SysDept comp) { |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.put("taxNo", comp.getOrgCode()); |
| | | JSONObject data = new JSONObject(); |
| | | data.put("serialNo", StrUtil.isNotBlank(queryDTO.getSerialNo()) ? queryDTO.getSerialNo() : ""); |
| | | data.put("invoiceCode", StrUtil.isNotBlank(queryDTO.getInvoiceCode()) ? queryDTO.getInvoiceCode() : ""); |
| | | data.put("invoiceNo", StrUtil.isNotBlank(queryDTO.getInvoiceNo()) ? queryDTO.getInvoiceNo() : ""); |
| | | data.put("invoiceTypeCode", StrUtil.isNotBlank(queryDTO.getInvoiceTypeCode()) ? queryDTO.getInvoiceTypeCode() : ""); |
| | | data.put("invoiceTerminalCode", StrUtil.isNotBlank(queryDTO.getInvoiceTerminalCode()) ? queryDTO.getInvoiceTerminalCode() : ""); |
| | | data.put("sourceMark", StrUtil.isNotBlank(queryDTO.getSourceMark()) ? queryDTO.getSourceMark() : ""); |
| | | data.put("invoiceSpecialMark", StrUtil.isNotBlank(queryDTO.getInvoiceSpecialMark()) ? queryDTO.getInvoiceSpecialMark() : ""); |
| | | data.put("buyerTaxNo", StrUtil.isNotBlank(queryDTO.getBuyerTaxNo()) ? queryDTO.getBuyerTaxNo() : ""); |
| | | data.put("buyerName", StrUtil.isNotBlank(queryDTO.getBuyerName()) ? queryDTO.getBuyerName() : ""); |
| | | data.put("invoiceStatus", StrUtil.isNotBlank(queryDTO.getInvoiceStatus()) ? queryDTO.getInvoiceStatus() : ""); |
| | | data.put("invoiceType", StrUtil.isNotBlank(queryDTO.getInvoiceType()) ? queryDTO.getInvoiceType() : ""); |
| | | data.put("invoiceStartDate", StrUtil.isNotBlank(queryDTO.getInvoiceStartDate()) ? queryDTO.getInvoiceStartDate() : ""); |
| | | data.put("invoiceEndDate", StrUtil.isNotBlank(queryDTO.getInvoiceEndDate()) ? queryDTO.getInvoiceEndDate() : ""); |
| | | data.put("machineNo", StrUtil.isNotBlank(queryDTO.getMachineNo()) ? queryDTO.getMachineNo() : ""); |
| | | data.put("systemName", StrUtil.isNotBlank(queryDTO.getSystemName()) ? queryDTO.getSystemName() : ""); |
| | | data.put("systemId", StrUtil.isNotBlank(queryDTO.getSystemId()) ? queryDTO.getSystemId() : ""); |
| | | data.put("pageSize", page.getSize()); |
| | | data.put("pageNo", page.getCurrent()); |
| | | jsonObject.put("data", data); |
| | | return jsonObject; |
| | | } |
| | | |
| | | @Override |
| | | public R add(ContractInvoice contractInvoice) { |
| | | Contract contract = contractMapper.selectById(contractInvoice.getContractId()); |
| | | if (ObjUtil.isNull(contract)) { |
| | | return R.failed("合同信息异常"); |
| | | } |
| | | List<ContractInvoice> invoiceList = baseMapper.selectList(Wrappers.<ContractInvoice>lambdaQuery() |
| | | .eq(ContractInvoice::getInvoiceNo, contractInvoice.getInvoiceNo())); |
| | | if (CollUtil.isNotEmpty(invoiceList)) { |
| | | return R.failed("发票已存在,请勿重复操作"); |
| | | } |
| | | contractInvoice.setInvoiceStatus("2"); |
| | | contractInvoice.setInvoiceSource("1"); |
| | | contractInvoice.setRedReversal("0"); |
| | | contractInvoice.setApprovalStatus("1"); |
| | | baseMapper.insert(contractInvoice); |
| | | contract.setBillingAmout(contract.getBillingAmout().add(contractInvoice.getInvoiceTotalPriceTax())); |
| | | if (contract.getBillingAmout().compareTo(contract.getAmount()) >= 0) { |
| | | contract.setBillingStatus("3"); |
| | | } |
| | | contractMapper.updateById(contract); |
| | | return R.ok(); |
| | | } |
| | | |
| | | @Override |
| | | public ContractInvoice getById(Long id) { |
| | | return baseMapper.selectById(id); |
| | | } |
| | | |
| | | @Override |
| | | public R bipPushPdf(Long invoiceId) { |
| | | ContractInvoice invoice = baseMapper.selectById(invoiceId); |
| | | pushBipInvoicePdf(invoice); |
| | | return R.ok(); |
| | | } |
| | | |
| | | @Override |
| | | public R selectBipByKey(String dictKey) { |
| | | List<BipDict> bipDictList = bipDictMapper.selectList(Wrappers.<BipDict>lambdaQuery().eq(BipDict::getDictKey,dictKey)); |
| | | if (CollUtil.isNotEmpty(bipDictList)){ |
| | | // 权限内部门 |
| | | List<TreeNode<Long>> collect = bipDictList.stream() |
| | | .map(dict -> { |
| | | TreeNode<Long> treeNode = new TreeNode<>(); |
| | | treeNode.setId(dict.getId()); |
| | | treeNode.setParentId(dict.getParentId()); |
| | | treeNode.setName(dict.getDictName()); |
| | | treeNode.setWeight(dict.getDictCode()); |
| | | return treeNode; |
| | | }) |
| | | .collect(Collectors.toList()); |
| | | |
| | | return R.ok(TreeUtil.build(collect,0L)); |
| | | } |
| | | return R.failed("字典获取失败"); |
| | | } |
| | | |
| | | @Override |
| | | public R saveAndPush(ContractInvoiceUpateDTO upateDTO) { |
| | | ContractInvoice invoice = baseMapper.selectById(upateDTO.getId()); |
| | | invoice.setInvoicePreview(upateDTO.getInvoicePreview()); |
| | | baseMapper.updateById(invoice); |
| | | R r = bipPush(invoice.getId()); |
| | | if (!r.isOk()){ |
| | | return r; |
| | | } |
| | | return bipPushPdf(invoice.getId()); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | File file = new File("d://tmp//" + IdUtil.fastUUID() + ".pdf"); |
| | | HttpUtil.downloadFile("http://fp.baiwang.com/format/d?d=BB99645F8ED2AE7D2055E7D29330D366B7CAE92C278C6BB84D222610CEC2118D", file); |
| | | } |
| | | } |