| | |
| | | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.account.pojo.AccountExpense; |
| | | import com.ruoyi.account.pojo.AccountIncome; |
| | | import com.ruoyi.account.service.AccountExpenseService; |
| | | import com.ruoyi.account.service.AccountIncomeService; |
| | | import com.ruoyi.basic.mapper.ProductMapper; |
| | | import com.ruoyi.basic.mapper.ProductModelMapper; |
| | | import com.ruoyi.basic.mapper.SupplierManageMapper; |
| | |
| | | import com.ruoyi.project.system.domain.SysUser; |
| | | import com.ruoyi.project.system.mapper.SysUserMapper; |
| | | import com.ruoyi.purchase.dto.PurchaseLedgerDto; |
| | | import com.ruoyi.purchase.dto.TicketRegistrationDto; |
| | | import com.ruoyi.purchase.mapper.ProductRecordMapper; |
| | | import com.ruoyi.purchase.mapper.PurchaseLedgerMapper; |
| | | import com.ruoyi.purchase.mapper.TicketRegistrationMapper; |
| | |
| | | import com.ruoyi.purchase.pojo.PurchaseLedger; |
| | | import com.ruoyi.purchase.pojo.TicketRegistration; |
| | | import com.ruoyi.purchase.service.IPurchaseLedgerService; |
| | | import com.ruoyi.purchase.service.ITicketRegistrationService; |
| | | import com.ruoyi.sales.mapper.*; |
| | | import com.ruoyi.sales.pojo.CommonFile; |
| | | import com.ruoyi.sales.pojo.InvoiceRegistrationProduct; |
| | |
| | | @RequiredArgsConstructor |
| | | @Slf4j |
| | | public class PurchaseLedgerServiceImpl extends ServiceImpl<PurchaseLedgerMapper, PurchaseLedger> implements IPurchaseLedgerService { |
| | | |
| | | private final AccountExpenseService accountExpenseService; |
| | | private final PurchaseLedgerMapper purchaseLedgerMapper; |
| | | |
| | | private final SalesLedgerMapper salesLedgerMapper; |
| | |
| | | purchaseLedger.setRecorderId(purchaseLedgerDto.getRecorderId()); |
| | | purchaseLedger.setRecorderName(sysUser.getNickName()); |
| | | purchaseLedger.setPhoneNumber(sysUser.getPhonenumber()); |
| | | // 2. 处理账户收入 |
| | | AccountExpense accountExpense = new AccountExpense(); |
| | | accountExpense.setExpenseDate(purchaseLedger.getEntryDate()); |
| | | accountExpense.setExpenseType("0"); |
| | | accountExpense.setSupplierName(purchaseLedger.getSupplierName()); |
| | | accountExpense.setExpenseMoney(purchaseLedger.getContractAmount()); |
| | | accountExpense.setExpenseDescribed("采购合同:" + purchaseLedger.getPurchaseContractNumber()); |
| | | accountExpense.setExpenseMethod("0"); |
| | | accountExpense.setInvoiceNumber(purchaseLedger.getPurchaseContractNumber()); |
| | | accountExpense.setInputTime(new Date()); |
| | | accountExpense.setInputUser(loginUser.getNickName()); |
| | | |
| | | |
| | | // 3. 新增或更新主表 |
| | | if (purchaseLedger.getId() == null) { |
| | | purchaseLedgerMapper.insert(purchaseLedger); |
| | | // accountIncomeService.save(accountIncome); |
| | | accountExpenseService.save(accountExpense); |
| | | } else { |
| | | purchaseLedgerMapper.updateById(purchaseLedger); |
| | | PurchaseLedger purchaseLedgerDB = purchaseLedgerMapper.selectById(purchaseLedger.getId()); |
| | | List<AccountExpense> accountExpenseDBs = accountExpenseService.getByInvoiceNumberList(purchaseLedger.getPurchaseContractNumber()); |
| | | if (!CollectionUtils.isEmpty(accountExpenseDBs)) { |
| | | accountExpenseDBs.forEach(accountExpenseDB ->{ |
| | | accountExpenseDB.setExpenseDate(purchaseLedgerDB.getEntryDate()); |
| | | accountExpenseDB.setExpenseType("0"); |
| | | accountExpenseDB.setSupplierName(purchaseLedgerDB.getSupplierName()); |
| | | accountExpenseDB.setExpenseMoney(purchaseLedgerDB.getContractAmount()); |
| | | accountExpenseDB.setExpenseDescribed("采购合同:" + purchaseLedgerDB.getPurchaseContractNumber()); |
| | | accountExpenseDB.setExpenseMethod("0"); |
| | | accountExpenseDB.setInvoiceNumber(purchaseLedgerDB.getPurchaseContractNumber()); |
| | | accountExpenseService.updateById(accountExpenseDB); |
| | | }); |
| | | } |
| | | |
| | | } |
| | | |
| | | // 4. 处理子表数据 |
| | | List<SalesLedgerProduct> productList = purchaseLedgerDto.getProductData(); |
| | | if (productList != null && !productList.isEmpty()) { |
| | | handleSalesLedgerProducts(purchaseLedger.getId(), productList, purchaseLedgerDto.getType()); |
| | | handleSalesLedgerProducts(purchaseLedgerDto,purchaseLedger.getId(), productList, purchaseLedgerDto.getType()); |
| | | } |
| | | |
| | | // 5. 迁移临时文件到正式目录 |
| | |
| | | return 1; |
| | | } |
| | | |
| | | private void handleSalesLedgerProducts(Long salesLedgerId, List<SalesLedgerProduct> products, Integer type) { |
| | | private final ITicketRegistrationService ticketRegistrationService; |
| | | |
| | | private void handleSalesLedgerProducts(PurchaseLedgerDto purchaseLedgerDto,Long salesLedgerId, List<SalesLedgerProduct> products, Integer type) throws IOException { |
| | | if (products == null || products.isEmpty()) { |
| | | throw new BaseException("产品信息不存在"); |
| | | } |
| | |
| | | salesLedgerProduct.setRegisterDate(localDateTime); |
| | | salesLedgerProductMapper.insert(salesLedgerProduct); |
| | | } |
| | | if (purchaseLedgerDto.getIsInvoice().equals(2)) { |
| | | // 无需开票,提示系统生成发票 |
| | | |
| | | for (SalesLedgerProduct productDatum : insertList) { |
| | | productDatum.setTicketsAmount(productDatum.getTaxInclusiveTotalPrice()); |
| | | productDatum.setTicketsNum(productDatum.getQuantity()); |
| | | } |
| | | BigDecimal invoiceAmount = insertList.stream() |
| | | .map(SalesLedgerProduct::getTicketsAmount) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | TicketRegistrationDto ticketRegistrationDto = new TicketRegistrationDto(); |
| | | BeanUtils.copyProperties(purchaseLedgerDto, ticketRegistrationDto); |
| | | ticketRegistrationDto.setInvoiceAmount(invoiceAmount); |
| | | ticketRegistrationDto.setProductData(insertList); |
| | | ticketRegistrationDto.setPurchaseLedgerId(salesLedgerId); |
| | | ticketRegistrationDto.setIssUer("系统自动生成"); |
| | | ticketRegistrationDto.setIssueDate(LocalDate.now()); |
| | | ticketRegistrationDto.setInvoiceNumber("此台账无需手动开票,系统将所有产品自动生成来票台账"); |
| | | ticketRegistrationService.addOrUpdateRegistration(ticketRegistrationDto); |
| | | } |
| | | } |
| | | |
| | | // 计算总含税金额 |
| | |
| | | |
| | | try { |
| | | // 执行文件迁移(使用原子操作确保安全性) |
| | | Files.move( |
| | | Paths.get(tempFile.getTempPath()), |
| | | formalFilePath, |
| | | StandardCopyOption.REPLACE_EXISTING, |
| | | StandardCopyOption.ATOMIC_MOVE |
| | | ); |
| | | // Files.move( |
| | | // Paths.get(tempFile.getTempPath()), |
| | | // formalFilePath, |
| | | // StandardCopyOption.REPLACE_EXISTING, |
| | | // StandardCopyOption.ATOMIC_MOVE |
| | | // ); |
| | | // 原子移动失败,使用复制+删除 |
| | | Files.copy(Paths.get(tempFile.getTempPath()), formalFilePath, StandardCopyOption.REPLACE_EXISTING); |
| | | Files.deleteIfExists(Paths.get(tempFile.getTempPath())); |
| | | log.info("文件迁移成功: {} -> {}", tempFile.getTempPath(), formalFilePath); |
| | | |
| | | // 更新文件记录(关联到业务ID) |