|
|
|
@ -7,6 +7,8 @@ import cc.hiver.core.entity.User; |
|
|
|
import cc.hiver.mall.bill.vo.ArrearsVo; |
|
|
|
import cc.hiver.mall.bill.vo.SupplierBillQueryVo; |
|
|
|
import cc.hiver.mall.dao.mapper.PurchaseMapper; |
|
|
|
import cc.hiver.mall.debt.entity.Debt; |
|
|
|
import cc.hiver.mall.debt.service.DebtService; |
|
|
|
import cc.hiver.mall.entity.*; |
|
|
|
import cc.hiver.mall.pojo.dto.DebtSupplier; |
|
|
|
import cc.hiver.mall.pojo.query.PurchasePageQuery; |
|
|
|
@ -23,6 +25,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.util.*; |
|
|
|
@ -40,6 +43,9 @@ public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> i |
|
|
|
@Autowired |
|
|
|
private PurchaseDetailService purchaseDetailService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private StockService stockService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private StockLogService stockLogService; |
|
|
|
|
|
|
|
@ -61,6 +67,15 @@ public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> i |
|
|
|
@Autowired |
|
|
|
private ProductService productService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private SaleService saleService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private SaleDetailService saleDetailService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private DebtService debtService; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public Page<DebtSupplier> getDebtByShopId(PurchasePageQuery purchasePageQuery) { |
|
|
|
@ -499,10 +514,11 @@ public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> i |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取从某个供应商的拿货入库记录(按入库单) |
|
|
|
* @author 王富康 |
|
|
|
* @date 2024/7/24 |
|
|
|
* |
|
|
|
* @param purchasePageQuery |
|
|
|
* @return Page<PurchaseVo> |
|
|
|
* @author 王富康 |
|
|
|
* @date 2024/7/24 |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public Page<PurchaseVo> getPurchaseListOfSupplier(PurchasePageQuery purchasePageQuery) { |
|
|
|
@ -608,4 +624,168 @@ public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> i |
|
|
|
} |
|
|
|
return purchaseVos; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 撤销采购单 |
|
|
|
* |
|
|
|
* @param id |
|
|
|
* @author 王富康 |
|
|
|
* @date 2024/7/27 |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
public void cancelPurchase(String id) { |
|
|
|
|
|
|
|
// 获取采购单信息
|
|
|
|
Purchase purchase = purchaseService.getById(id); |
|
|
|
List<String> purchaseIdList = new ArrayList<>(); |
|
|
|
purchaseIdList.add(id); |
|
|
|
// 3. 商品及库存回退
|
|
|
|
// 查询商品是有有销售记录并且是不是有且只有这一次入库记录,是的话,直接删除商品及其库存信息,其他的只回退库存即可
|
|
|
|
final List<PurchaseDetail> byPurchaseIdList = purchaseDetailService.getByPurchaseIdList(purchaseIdList, ""); |
|
|
|
// 拿到商品id集合
|
|
|
|
final List<String> productIdList = new ArrayList<>(); |
|
|
|
for (PurchaseDetail purchaseDetail : byPurchaseIdList) { |
|
|
|
productIdList.add(purchaseDetail.getProductId()); |
|
|
|
} |
|
|
|
// 查询该商品除当前入库单的其他的入库记录
|
|
|
|
final List<PurchaseDetail> purductPurchaseList = purchaseDetailService.getPurchaseDetailListByProductIds(productIdList, id); |
|
|
|
// 封装为商品id为key,List<PurchaseDetail>为value 的map
|
|
|
|
final Map<String, List<PurchaseDetail>> purductPurchaseMap = new HashMap<>(); |
|
|
|
for (PurchaseDetail purchaseDetail : purductPurchaseList) { |
|
|
|
final String productId = purchaseDetail.getProductId(); |
|
|
|
if (purductPurchaseMap.containsKey(productId)) { |
|
|
|
purductPurchaseMap.get(productId).add(purchaseDetail); |
|
|
|
} else { |
|
|
|
final List<PurchaseDetail> purchaseDetails = new ArrayList<>(); |
|
|
|
purchaseDetails.add(purchaseDetail); |
|
|
|
purductPurchaseMap.put(productId, purchaseDetails); |
|
|
|
} |
|
|
|
} |
|
|
|
// 查询该商品的销售记录
|
|
|
|
final List<SaleDetail> purductSaleList = saleDetailService.getSaleDetailListByProductIds(productIdList); |
|
|
|
// 封装为商品id为key,List<SaleDetail>为value 的map
|
|
|
|
final Map<String, List<SaleDetail>> purductSaleMap = new HashMap<>(); |
|
|
|
for (SaleDetail saleDetail : purductSaleList) { |
|
|
|
final String productId = saleDetail.getProductId(); |
|
|
|
if (purductSaleMap.containsKey(productId)) { |
|
|
|
purductSaleMap.get(productId).add(saleDetail); |
|
|
|
} else { |
|
|
|
final List<SaleDetail> saleDetails = new ArrayList<>(); |
|
|
|
saleDetails.add(saleDetail); |
|
|
|
purductSaleMap.put(productId, saleDetails); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//
|
|
|
|
final StringJoiner needDeleteProductId = new StringJoiner(","); |
|
|
|
final StringJoiner needDeleteStockProductId = new StringJoiner(","); |
|
|
|
final StringJoiner needReduceStockProductId = new StringJoiner(","); |
|
|
|
|
|
|
|
for (PurchaseDetail purchaseDetail : byPurchaseIdList) { |
|
|
|
final String productId = purchaseDetail.getProductId(); |
|
|
|
if (!purductPurchaseMap.containsKey(productId) && !purductSaleMap.containsKey(productId)) { |
|
|
|
// 可以删除商品的相关信息
|
|
|
|
needDeleteProductId.add(productId); |
|
|
|
// 库存删除
|
|
|
|
needDeleteStockProductId.add(productId); |
|
|
|
}else{ |
|
|
|
// 商品保留,库存扣减
|
|
|
|
needReduceStockProductId.add(productId); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
// 执行删除商品
|
|
|
|
if(StringUtils.isNotEmpty(needDeleteProductId.toString())){ |
|
|
|
productService.batchDeleteProduct(needDeleteProductId.toString()); |
|
|
|
} |
|
|
|
// 执行删除库存
|
|
|
|
if(StringUtils.isNotEmpty(needReduceStockProductId.toString())){ |
|
|
|
stockService.batchDeleteStockByProductIds(needDeleteStockProductId.toString()); |
|
|
|
} |
|
|
|
// 执行扣减库存
|
|
|
|
if(StringUtils.isNotEmpty(needReduceStockProductId.toString())){ |
|
|
|
// 获取当前入库单各商品的规格及数量信息
|
|
|
|
final List<StockLog> stockLogList = stockLogService.getByProductIds(id, needReduceStockProductId.toString()); |
|
|
|
// 根据入库记录减掉库存数
|
|
|
|
stockService.batchReduceStockByLog(stockLogList); |
|
|
|
} |
|
|
|
// 4. 根据采购单id删除库存记录
|
|
|
|
stockLogService.deleteByPurchaseId(id); |
|
|
|
// 5. 计算平均采购价
|
|
|
|
// 获取该商品的历次入库记录,排除掉本次入库记录 入库的价格*入库数量/入库总数量
|
|
|
|
Map<String , BigDecimal> productPurchasePriceMap = new HashMap<>(); |
|
|
|
|
|
|
|
List<Product> productList = new ArrayList<>(); |
|
|
|
for (Map.Entry<String, List<PurchaseDetail>> stringListEntry : purductPurchaseMap.entrySet()) { |
|
|
|
final String productId = stringListEntry.getKey(); |
|
|
|
List<PurchaseDetail> purchaseDetail = stringListEntry.getValue(); |
|
|
|
// 计算该商品的平均采购价 = (当前入库价格*当前入库数量 + 历史入库价格*历史入库数量)/(当前入库数量+历史入库数量)
|
|
|
|
// 总入库数量
|
|
|
|
int totalInStorageCount = 0; |
|
|
|
// 总入库金额
|
|
|
|
BigDecimal totalInStoragePrice = BigDecimal.ZERO; |
|
|
|
for (PurchaseDetail detail : purchaseDetail) { |
|
|
|
totalInStorageCount += detail.getProductCount(); |
|
|
|
BigDecimal bigDecimalOfProductCount = new BigDecimal(detail.getProductCount()); |
|
|
|
BigDecimal purchasePrice = detail.getPurchasePrice(); |
|
|
|
totalInStoragePrice = totalInStoragePrice.add(purchasePrice.multiply(bigDecimalOfProductCount)); |
|
|
|
} |
|
|
|
// 平均采购价为 总入库金额 / 总入库数量
|
|
|
|
BigDecimal newPurchasePrice = totalInStoragePrice.divide(BigDecimal.valueOf(totalInStorageCount)); |
|
|
|
Product product = new Product(); |
|
|
|
product.setId(productId); |
|
|
|
product.setPurchasePrice(newPurchasePrice); |
|
|
|
productList.add(product); |
|
|
|
} |
|
|
|
|
|
|
|
// 执行更新平均采购价
|
|
|
|
if(!productList.isEmpty()){ |
|
|
|
productService.batchUpdatePurchasePrice(productList); |
|
|
|
} |
|
|
|
// 6. 欠款回退
|
|
|
|
// 4. 订单款项记录作废
|
|
|
|
// 根据订单获取交易记录
|
|
|
|
final String supplierId = purchase.getSupplierId(); |
|
|
|
final Debt oldDebt = debtService.selectByUserId(purchase.getShopId(), supplierId); |
|
|
|
if (oldDebt != null) { |
|
|
|
final BigDecimal newDebtAmount; |
|
|
|
final BigDecimal lastDebtAmount = oldDebt.getAmountOwed(); |
|
|
|
// 余额抵扣,需要从欠款中减掉
|
|
|
|
final BigDecimal balanceDeductionAmount = purchase.getBalanceDeductionAmount() == null ? BigDecimal.ZERO : purchase.getBalanceDeductionAmount(); |
|
|
|
// 本单欠款
|
|
|
|
final BigDecimal noPay = purchase.getNoPay() == null ? BigDecimal.ZERO : purchase.getNoPay(); |
|
|
|
// 计算欠款
|
|
|
|
newDebtAmount = oldDebt.getAmountOwed().subtract(noPay).subtract(balanceDeductionAmount); |
|
|
|
oldDebt.setAmountOwed(newDebtAmount); |
|
|
|
debtService.updateById(oldDebt); |
|
|
|
|
|
|
|
// 2. 新增欠款记录
|
|
|
|
// 创建人
|
|
|
|
final User user = securityUtil.getCurrUser(); |
|
|
|
final DealingsRecord dealingsRecord = new DealingsRecord(); |
|
|
|
dealingsRecord.setCreateBy(user.getId()); |
|
|
|
dealingsRecord.setCreateByName(user.getNickname()); |
|
|
|
dealingsRecord.setCreateTime(new Date()); |
|
|
|
dealingsRecord.setDealingsUserId(purchase.getSupplierId()); |
|
|
|
dealingsRecord.setDealingsUserName(purchase.getSupplierName()); |
|
|
|
dealingsRecord.setDealingsWay("订单撤销"); |
|
|
|
dealingsRecord.setShopId(purchase.getShopId()); |
|
|
|
// 上次欠款
|
|
|
|
dealingsRecord.setLastDebtAmount(lastDebtAmount); |
|
|
|
// 最新欠款
|
|
|
|
dealingsRecord.setBalanceDue(newDebtAmount); |
|
|
|
// 金额
|
|
|
|
dealingsRecord.setAmount(noPay); |
|
|
|
dealingsRecord.setDealingsType(DealingsRecordConstant.DEALINGS_TYPE[6]); |
|
|
|
dealingsRecord.setUserType(DealingsRecordConstant.TYPE[0]); |
|
|
|
dealingsRecordService.save(dealingsRecord); |
|
|
|
} |
|
|
|
// 作废当前订单的欠款记录
|
|
|
|
dealingsRecordService.cancelRecord(id); |
|
|
|
// 1. 将订单状态设置为删除状态
|
|
|
|
purchaseService.deleteById(id); |
|
|
|
// 2. 将订单详情设置为删除状态
|
|
|
|
purchaseDetailService.deleteByPurchaseId(id); |
|
|
|
} |
|
|
|
} |
|
|
|
|