| | |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.basic.dto.SupplierManageDto; |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | | import com.ruoyi.common.vo.FileVo; |
| | | import com.ruoyi.sales.dto.InvoiceLedgerDto; |
| | | import com.ruoyi.sales.dto.InvoiceRegistrationProductDto; |
| | | import com.ruoyi.sales.excel.InvoiceLedgerExcelDto; |
| | | import com.ruoyi.sales.mapper.InvoiceLedgerFileMapper; |
| | | import com.ruoyi.sales.mapper.InvoiceLedgerMapper; |
| | | import com.ruoyi.sales.pojo.InvoiceLedger; |
| | | import com.ruoyi.sales.pojo.InvoiceLedgerFile; |
| | | import com.ruoyi.sales.mapper.*; |
| | | import com.ruoyi.sales.pojo.*; |
| | | import com.ruoyi.sales.service.InvoiceLedgerService; |
| | | import org.apache.commons.collections4.CollectionUtils; |
| | | import org.springframework.beans.BeanUtils; |
| | |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDate; |
| | | import java.time.YearMonth; |
| | | import java.util.Comparator; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.UUID; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | @Autowired |
| | | private InvoiceLedgerFileMapper invoiceLedgerFileMapper; |
| | | |
| | | @Autowired |
| | | private InvoiceRegistrationProductMapper invoiceRegistrationProductMapper; |
| | | |
| | | @Autowired |
| | | private ReceiptPaymentMapper receiptPaymentMapper; |
| | | |
| | | @Autowired |
| | | private SalesLedgerProductMapper salesLedgerProductMapper; |
| | | |
| | | /** |
| | | * 开票台账新增 |
| | | * @param invoiceLedgerDto |
| | | * @param productDto |
| | | * @return |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int invoiceLedgerSaveOrUpdate(InvoiceLedgerDto invoiceLedgerDto) { |
| | | InvoiceLedger invoiceLedger = new InvoiceLedger(); |
| | | BeanUtils.copyProperties(invoiceLedgerDto, invoiceLedger); |
| | | public int invoiceLedgerSaveOrUpdate(InvoiceRegistrationProductDto productDto) { |
| | | // 判断是否已经新增开票台账 |
| | | QueryWrapper<InvoiceLedger> ledgerQueryWrapper = new QueryWrapper<>(); |
| | | ledgerQueryWrapper.eq("invoice_registration_product_id", productDto.getId()); |
| | | InvoiceLedger invoiceLedger = invoiceLedgerMapper.selectOne(ledgerQueryWrapper); |
| | | int result; |
| | | if(invoiceLedgerDto.getId() == null){ |
| | | if(ObjectUtils.isEmpty(invoiceLedger)){ |
| | | invoiceLedger = new InvoiceLedger(); |
| | | invoiceLedger.setInvoiceRegistrationProductId(productDto.getId()); |
| | | invoiceLedger.setInvoiceNo(productDto.getInvoiceNo()); |
| | | invoiceLedger.setInvoiceTotal(productDto.getInvoiceTotal()); |
| | | invoiceLedger.setInvoiceDate(productDto.getInvoiceDate()); |
| | | invoiceLedger.setInvoicePerson(productDto.getInvoicePerson()); |
| | | result = invoiceLedgerMapper.insert(invoiceLedger); |
| | | }else { |
| | | // 修改sale_leger_product |
| | | InvoiceRegistrationProduct invoiceRegistrationProduct = invoiceRegistrationProductMapper.selectById(productDto.getId()); |
| | | SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(invoiceRegistrationProduct.getSalesLedgerProductId()); |
| | | |
| | | BigDecimal invoiceNum = productDto.getInvoiceTotal().divide(invoiceRegistrationProduct.getTaxInclusiveTotalPrice(), 2, BigDecimal.ROUND_HALF_UP); |
| | | //计算 未开票数/未开票金额 |
| | | BigDecimal noInvoiceAmount = salesLedgerProduct.getNoInvoiceAmount().add(invoiceRegistrationProduct.getInvoiceAmount()).subtract(productDto.getInvoiceTotal()); |
| | | BigDecimal noInvoiceNum = salesLedgerProduct.getNoInvoiceNum().add(invoiceRegistrationProduct.getInvoiceNum()).subtract(invoiceNum); |
| | | salesLedgerProduct.setNoInvoiceAmount(noInvoiceAmount); |
| | | salesLedgerProduct.setNoInvoiceNum(noInvoiceNum); |
| | | salesLedgerProductMapper.updateById(salesLedgerProduct); |
| | | |
| | | // 修改当前产品开票登记记录数据 |
| | | BigDecimal currentNoInvoiceNum = invoiceRegistrationProduct.getNoInvoiceNum().add(invoiceRegistrationProduct.getInvoiceNum()).subtract(invoiceNum); |
| | | BigDecimal currentNoInvoiceAmount = invoiceRegistrationProduct.getNoInvoiceAmount().add(invoiceRegistrationProduct.getInvoiceAmount()).subtract(productDto.getInvoiceTotal()); |
| | | invoiceRegistrationProduct.setInvoiceNum(invoiceNum); |
| | | invoiceRegistrationProduct.setInvoiceAmount(productDto.getInvoiceAmount()); |
| | | invoiceRegistrationProduct.setNoInvoiceNum(currentNoInvoiceNum); |
| | | invoiceRegistrationProduct.setNoInvoiceAmount(currentNoInvoiceAmount); |
| | | invoiceRegistrationProductMapper.updateById(invoiceRegistrationProduct); |
| | | |
| | | invoiceLedger.setInvoiceNo(productDto.getInvoiceNo()); |
| | | invoiceLedger.setInvoiceTotal(productDto.getInvoiceTotal()); |
| | | invoiceLedger.setInvoiceDate(productDto.getInvoiceDate()); |
| | | invoiceLedger.setInvoicePerson(productDto.getInvoicePerson()); |
| | | result = invoiceLedgerMapper.updateById(invoiceLedger); |
| | | //删除所有附件关联 |
| | | LambdaQueryWrapper<InvoiceLedgerFile> delWrapper = new LambdaQueryWrapper<>(); |
| | | delWrapper.eq(InvoiceLedgerFile::getInvoiceLedgerId, invoiceLedgerDto.getId()); |
| | | delWrapper.eq(InvoiceLedgerFile::getInvoiceLedgerId, invoiceLedger.getId()); |
| | | invoiceLedgerFileMapper.delete(delWrapper); |
| | | } |
| | | List<FileVo> fileList = invoiceLedgerDto.getFileList(); |
| | | List<FileVo> fileList = productDto.getFileList(); |
| | | if(CollectionUtils.isNotEmpty(fileList)){ |
| | | fileList.forEach(fileVo -> { |
| | | for (FileVo fileVo : fileList) { |
| | | InvoiceLedgerFile invoiceLedgerFile = new InvoiceLedgerFile(); |
| | | BeanUtils.copyProperties(fileVo, invoiceLedgerFile); |
| | | invoiceLedgerFile.setInvoiceLedgerId(invoiceLedger.getId()); |
| | | invoiceLedgerFileMapper.insert(invoiceLedgerFile); |
| | | }); |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | |
| | | /** |
| | | * 附件下载 |
| | | * @param response |
| | | * @param invoiceLedgerDto |
| | | * @param invoiceRegistrationProductDto |
| | | * @return |
| | | */ |
| | | @Override |
| | | public void invoiceLedgerExport(HttpServletResponse response, InvoiceLedgerDto invoiceLedgerDto) { |
| | | List<InvoiceLedgerDto> invoiceLedgerDtoList = invoiceLedgerMapper.invoiceLedgerList(invoiceLedgerDto); |
| | | List<InvoiceLedgerExcelDto> invoiceLedgerExcelDtoList = invoiceLedgerDtoList.stream().map(item -> { |
| | | public void invoiceLedgerExport(HttpServletResponse response, InvoiceRegistrationProductDto invoiceRegistrationProductDto) { |
| | | List<InvoiceRegistrationProductDto> invoiceRegistrationProductList = invoiceRegistrationProductMapper.invoiceRegistrationProductList(invoiceRegistrationProductDto); |
| | | List<InvoiceLedgerExcelDto> invoiceLedgerExcelDtoList = invoiceRegistrationProductList.stream().map(item -> { |
| | | InvoiceLedgerExcelDto invoiceLedgerExcelDto = new InvoiceLedgerExcelDto(); |
| | | BeanUtils.copyProperties(item, invoiceLedgerExcelDto); |
| | | return invoiceLedgerExcelDto; |
| | |
| | | if(CollectionUtils.isEmpty(invoiceLedgerDto.getFileList())){ |
| | | throw new RuntimeException("缺少文件信息"); |
| | | } |
| | | QueryWrapper<InvoiceLedger> ledgerQueryWrapper = new QueryWrapper<>(); |
| | | ledgerQueryWrapper.eq("invoice_registration_product_id", invoiceLedgerDto.getId()); |
| | | InvoiceLedger invoiceLedger = invoiceLedgerMapper.selectOne(ledgerQueryWrapper); |
| | | if(ObjectUtils.isEmpty(invoiceLedger)){ |
| | | throw new RuntimeException("开票台账未登记"); |
| | | } |
| | | List<FileVo> fileList = invoiceLedgerDto.getFileList(); |
| | | fileList.forEach(fileVo -> { |
| | | InvoiceLedgerFile invoiceLedgerFile = new InvoiceLedgerFile(); |
| | | BeanUtils.copyProperties(fileVo, invoiceLedgerFile); |
| | | invoiceLedgerFile.setInvoiceLedgerId(invoiceLedgerDto.getId()); |
| | | invoiceLedgerFile.setInvoiceLedgerId(invoiceLedger.getId()); |
| | | invoiceLedgerFileMapper.insert(invoiceLedgerFile); |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 开票台账查询 |
| | | * @param invoiceLedgerDto |
| | | * @return |
| | | */ |
| | | @Override |
| | | public List<InvoiceLedgerDto> invoiceLedgerList(InvoiceLedgerDto invoiceLedgerDto) { |
| | | return invoiceLedgerMapper.invoiceLedgerList(invoiceLedgerDto); |
| | | } |
| | | |
| | | /** |
| | | * 客户销售记录 |
| | | * @param page |
| | | * @param invoiceLedgerDto |
| | | * @return |
| | | */ |
| | | @Override |
| | | public IPage<InvoiceLedgerDto> invoiceLedgerSalesAccount(Page page, InvoiceLedgerDto invoiceLedgerDto) { |
| | | IPage<InvoiceLedgerDto> invoiceLedgerDtoIPage = invoiceLedgerMapper.invoiceLedgerSalesAccount(page, invoiceLedgerDto); |
| | | return invoiceLedgerDtoIPage; |
| | | } |
| | | |
| | | @Override |
| | | public BigDecimal getInvoiceAmount() { |
| | | LocalDate now = LocalDate.now(); |
| | | YearMonth currentMonth = YearMonth.from(now); |
| | | |
| | | // 创建LambdaQueryWrapper |
| | | LambdaQueryWrapper<InvoiceLedger> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.ge(InvoiceLedger::getInvoiceDate, currentMonth.atDay(1).atStartOfDay()) // 大于等于本月第一天 |
| | | .lt(InvoiceLedger::getInvoiceDate, currentMonth.plusMonths(1).atDay(1).atStartOfDay()); // 小于下月第一天 |
| | | |
| | | // 执行查询并计算总和 |
| | | List<InvoiceLedger> invoiceLedgers = invoiceLedgerMapper.selectList(queryWrapper); |
| | | |
| | | BigDecimal totalContractAmount = invoiceLedgers.stream() |
| | | .map(InvoiceLedger::getInvoiceTotal) |
| | | .filter(Objects::nonNull) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | |
| | | return totalContractAmount; |
| | | } |
| | | |
| | | /** |
| | | * 开票登记产品分页查询 |
| | | * @param page |
| | | * @param registrationProductDto |
| | | * @return |
| | | */ |
| | | @Override |
| | | public IPage<InvoiceRegistrationProductDto> registrationProductPage(Page page, InvoiceRegistrationProductDto registrationProductDto) { |
| | | return invoiceRegistrationProductMapper.invoiceRegistrationProductPage(page,registrationProductDto); |
| | | } |
| | | |
| | | /** |
| | | * 产品开票台账详情 |
| | | * @param id |
| | | * @return |
| | | */ |
| | | @Override |
| | | public InvoiceRegistrationProductDto invoiceLedgerProductDetail(Integer id) { |
| | | InvoiceRegistrationProductDto invoiceRegistrationProductDto = invoiceLedgerMapper.invoiceLedgerProductInfo(id); |
| | | if(ObjectUtils.isEmpty(invoiceRegistrationProductDto)){ |
| | | throw new RuntimeException("产品开票台账查找失败"); |
| | | } |
| | | // 查询附件 |
| | | QueryWrapper<InvoiceLedgerFile> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("invoice_ledger_id", invoiceRegistrationProductDto.getInvoiceLedgerId()); |
| | | List<InvoiceLedgerFile> invoiceLedgerFileList = invoiceLedgerFileMapper.selectList(queryWrapper); |
| | | List<FileVo> fileList = invoiceLedgerFileList.stream().map(item -> { |
| | | FileVo fileVo = new FileVo(); |
| | | BeanUtils.copyProperties(item, fileVo); |
| | | return fileVo; |
| | | }).collect(Collectors.toList()); |
| | | invoiceRegistrationProductDto.setFileList(fileList); |
| | | return invoiceRegistrationProductDto; |
| | | } |
| | | |
| | | /** |
| | | * 开票台账删除 |
| | | * @param invoiceRegistrationProductId |
| | | * @return |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void delInvoiceLedger(Integer invoiceRegistrationProductId) { |
| | | // 判断是否有回款记录 |
| | | List<ReceiptPayment> receiptPaymentList = receiptPaymentMapper.receiptPaymentListByProdRegId(invoiceRegistrationProductId); |
| | | if(!CollectionUtils.isEmpty(receiptPaymentList)){ |
| | | throw new RuntimeException("请先删除回款记录"); |
| | | } |
| | | // 判断是否有开票台账 |
| | | InvoiceRegistrationProduct invoiceRegistrationProduct = invoiceRegistrationProductMapper.selectById(invoiceRegistrationProductId); |
| | | if(ObjectUtils.isEmpty(invoiceRegistrationProduct)){ |
| | | throw new RuntimeException("开票台账查找失败"); |
| | | } |
| | | // 判断该开票台账是否未当月,如果为当月直接删除否则新增一条相反负数数据 |
| | | |
| | | // 删除开票台账 |
| | | QueryWrapper<InvoiceLedger> delMapper = new QueryWrapper<>(); |
| | | delMapper.eq("invoice_registration_product_id", invoiceRegistrationProductId); |
| | | invoiceLedgerMapper.delete(delMapper); |
| | | // 删除开票登记并回滚 |
| | | QueryWrapper<InvoiceRegistrationProduct> queryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("sales_ledger_id", invoiceRegistrationProduct.getSalesLedgerId()); |
| | | queryWrapper.eq("sales_ledger_product_id", invoiceRegistrationProduct.getSalesLedgerProductId()); |
| | | queryWrapper.orderByAsc("create_time"); |
| | | List<InvoiceRegistrationProduct> invoiceRegistrationProductList = invoiceRegistrationProductMapper.selectList(queryWrapper); |
| | | int index = -1; |
| | | for (int i = 0; i < invoiceRegistrationProductList.size(); i++) { |
| | | InvoiceRegistrationProduct currentInvoiceRegProduct = invoiceRegistrationProductList.get(i); |
| | | if(invoiceRegistrationProduct.getId() == currentInvoiceRegProduct.getId()){ |
| | | index = i; |
| | | } |
| | | } |
| | | if(index == -1){ |
| | | return; |
| | | } |
| | | for (int i = index + 1; i < invoiceRegistrationProductList.size(); i++) { |
| | | InvoiceRegistrationProduct currentInvoiceRegProduct = invoiceRegistrationProductList.get(i); |
| | | // 回滚未开票数/未开票金额 |
| | | BigDecimal noInvoiceAmount = currentInvoiceRegProduct.getNoInvoiceAmount().add(invoiceRegistrationProduct.getInvoiceAmount()); |
| | | BigDecimal noInvoiceNum = currentInvoiceRegProduct.getNoInvoiceNum().add(invoiceRegistrationProduct.getInvoiceNum()); |
| | | currentInvoiceRegProduct.setNoInvoiceAmount(noInvoiceAmount); |
| | | currentInvoiceRegProduct.setNoInvoiceNum(noInvoiceNum); |
| | | invoiceRegistrationProductMapper.updateById(currentInvoiceRegProduct); |
| | | } |
| | | invoiceRegistrationProductMapper.deleteById(invoiceRegistrationProductId); |
| | | // 修改sale_product数据 |
| | | SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(invoiceRegistrationProduct.getSalesLedgerProductId()); |
| | | QueryWrapper<InvoiceRegistrationProduct> newQueryWrapper = new QueryWrapper<>(); |
| | | queryWrapper.eq("sales_ledger_id", invoiceRegistrationProduct.getSalesLedgerId()); |
| | | queryWrapper.eq("sales_ledger_product_id", invoiceRegistrationProduct.getSalesLedgerProductId()); |
| | | queryWrapper.orderByAsc("create_time"); |
| | | List<InvoiceRegistrationProduct> newInvoiceRegistrationProductList = invoiceRegistrationProductMapper.selectList(newQueryWrapper); |
| | | if(CollectionUtils.isEmpty(newInvoiceRegistrationProductList)){ |
| | | salesLedgerProduct.setNoInvoiceNum(salesLedgerProduct.getQuantity()); |
| | | salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getTaxInclusiveTotalPrice()); |
| | | }else { |
| | | salesLedgerProduct.setNoInvoiceNum(newInvoiceRegistrationProductList.get(newInvoiceRegistrationProductList.size()-1).getNoInvoiceNum()); |
| | | salesLedgerProduct.setNoInvoiceAmount(newInvoiceRegistrationProductList.get(newInvoiceRegistrationProductList.size()-1).getNoInvoiceAmount()); |
| | | } |
| | | salesLedgerProductMapper.updateById(salesLedgerProduct); |
| | | } |
| | | } |