Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added account related deduction balance code #203

Merged
merged 8 commits into from
Nov 19, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public Response<String> addOrUpdateAccount(AddOrUpdateAccountDTO addOrUpdateAcco
account.setStatus(CommonConstants.STATUS_NORMAL);
account.setCreateBy(userId);
account.setCreateTime(LocalDateTime.now());
// 如果当前余额为空,则设置为初始化余额的值
if(account.getCurrentAmount() == null) {
account.setCurrentAmount(account.getInitialAmount());
}
var saveResult = accountMapper.insert(account);
if(saveResult == 0) {
return Response.responseMsg(FinancialCodeEnum.ADD_ACCOUNT_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ public Response<String> addOrUpdatePurchaseStorage(PurchaseStorageDTO purchaseSt
.set(purchaseStorageDTO.getPaymentAmount() != null, ReceiptPurchaseMain::getDiscountAmount, purchaseStorageDTO.getPaymentAmount())
.set(purchaseStorageDTO.getPaymentLastAmount() != null, ReceiptPurchaseMain::getDiscountLastAmount, purchaseStorageDTO.getPaymentLastAmount())
.set(purchaseStorageDTO.getOtherAmount() != null, ReceiptPurchaseMain::getOtherAmount, purchaseStorageDTO.getOtherAmount())
.set(purchaseStorageDTO.getThisPaymentAmount() != null, ReceiptPurchaseMain::getChangeAmount, purchaseStorageDTO.getThisPaymentAmount())
.set(purchaseStorageDTO.getThisPaymentAmount() != null, ReceiptPurchaseMain::getChangeAmount, purchaseStorageDTO.getThisPaymentAmount().negate())
.set(purchaseStorageDTO.getThisArrearsAmount() != null, ReceiptPurchaseMain::getArrearsAmount, purchaseStorageDTO.getThisArrearsAmount())
.set(purchaseStorageDTO.getStatus() != null, ReceiptPurchaseMain::getStatus, purchaseStorageDTO.getStatus())
.set(StringUtils.hasText(purchaseStorageDTO.getOtherReceipt()), ReceiptPurchaseMain::getOtherReceipt, purchaseStorageDTO.getOtherReceipt())
Expand Down Expand Up @@ -711,6 +711,21 @@ public Response<String> addOrUpdatePurchaseStorage(PurchaseStorageDTO purchaseSt
updateProductStock(receiptPurchaseStorageList, 1);
});

var account = accountService.getById(purchaseStorageDTO.getAccountId());
if (account != null) {
var accountBalance = account.getCurrentAmount();
var thisPaymentAmount = purchaseStorageDTO.getThisPaymentAmount();
var beforeChangeAmount = beforeReceipt.stream()
.map(ReceiptPurchaseSub::getTotalAmount)
.reduce(BigDecimal.ZERO, BigDecimal::add);
accountBalance = accountBalance.add(beforeChangeAmount);
if (thisPaymentAmount != null) {
accountBalance = accountBalance.subtract(thisPaymentAmount);
}
account.setCurrentAmount(accountBalance);
accountService.updateById(account);
}

if (updateMainResult && updateSubResult) {
return Response.responseMsg(PurchaseCodeEnum.UPDATE_PURCHASE_RECEIPT_SUCCESS);
} else {
Expand All @@ -733,7 +748,7 @@ public Response<String> addOrUpdatePurchaseStorage(PurchaseStorageDTO purchaseSt
.discountLastAmount(purchaseStorageDTO.getPaymentLastAmount())
.otherAmount(purchaseStorageDTO.getOtherAmount())
.otherReceipt(purchaseStorageDTO.getOtherReceipt())
.changeAmount(purchaseStorageDTO.getThisPaymentAmount())
.changeAmount(purchaseStorageDTO.getThisPaymentAmount().negate())
.arrearsAmount(purchaseStorageDTO.getThisArrearsAmount())
.multipleAccount(multipleAccountIds)
.multipleAccountAmount(multipleAccountAmounts)
Expand Down Expand Up @@ -766,6 +781,17 @@ public Response<String> addOrUpdatePurchaseStorage(PurchaseStorageDTO purchaseSt

var saveSubResult = receiptPurchaseSubService.saveBatch(receiptList);
updateProductStock(receiptList, 1);
var account = accountService.getById(purchaseStorageDTO.getAccountId());
if (account != null) {
var accountBalance = account.getCurrentAmount();
var changeAmount = purchaseStorageDTO.getThisPaymentAmount();
if (changeAmount != null) {
accountBalance = accountBalance.subtract(changeAmount);
account.setId(purchaseStorageDTO.getAccountId());
account.setCurrentAmount(accountBalance);
accountService.updateById(account);
}
}

if (saveMainResult && saveSubResult) {
return Response.responseMsg(PurchaseCodeEnum.ADD_PURCHASE_RECEIPT_SUCCESS);
Expand Down Expand Up @@ -987,6 +1013,20 @@ public Response<String> addOrUpdatePurchaseRefund(PurchaseRefundDTO purchaseRefu
purchaseRefundDTO.getTableData().forEach(item -> {
updateProductStock(receiptPurchaseRefundList, 2);
});
var account = accountService.getById(purchaseRefundDTO.getAccountId());
if (account != null) {
var accountBalance = account.getCurrentAmount();
var thisRefundAmount = purchaseRefundDTO.getThisRefundAmount();
var beforeChangeAmount = beforeReceipt.stream()
.map(ReceiptPurchaseSub::getTotalAmount)
.reduce(BigDecimal.ZERO, BigDecimal::add);
accountBalance = accountBalance.subtract(thisRefundAmount);
if (thisRefundAmount != null) {
accountBalance = accountBalance.add(beforeChangeAmount);
}
account.setCurrentAmount(accountBalance);
accountService.updateById(account);
}


if (updateMainResult && updateSubResult) {
Expand Down Expand Up @@ -1044,6 +1084,18 @@ public Response<String> addOrUpdatePurchaseRefund(PurchaseRefundDTO purchaseRefu

var saveSubResult = receiptPurchaseSubService.saveBatch(receiptList);
updateProductStock(receiptList, 2);
var account = accountService.getById(purchaseRefundDTO.getAccountId());
if (account != null) {
var accountBalance = account.getCurrentAmount();
var thisRefundAmount = purchaseRefundDTO.getThisRefundAmount();
if (thisRefundAmount != null) {
accountBalance = accountBalance.add(thisRefundAmount);
account.setId(purchaseRefundDTO.getAccountId());
account.setCurrentAmount(accountBalance);
accountService.updateById(account);
}
}

if (saveMainResult && saveSubResult) {
return Response.responseMsg(PurchaseCodeEnum.ADD_PURCHASE_REFUND_SUCCESS);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,25 @@ public Response<String> addOrUpdateRetailShipments(RetailShipmentsDTO shipmentsD
.set(ReceiptRetailMain::getUpdateTime, LocalDateTime.now())
.update();

// 更新余额 如果之前已经修改过那么就需要减去之前的金额 再加上现在的金额 如果之前没有修改过那么就直接加上现在的金额

var account = accountService.getById(shipmentsDTO.getAccountId());
if (account != null) {
var accountBalance = account.getCurrentAmount();
var changeAmount = shipmentsDTO.getCollectAmount();
var beforeChangeAmount = beforeReceipt.stream()
.map(item -> item.getTotalAmount())
.reduce(BigDecimal.ZERO, BigDecimal::add);
if (beforeChangeAmount != null) {
accountBalance = accountBalance.subtract(beforeChangeAmount);
}
if (changeAmount != null) {
accountBalance = accountBalance.add(changeAmount);
}
account.setCurrentAmount(accountBalance);
accountService.updateById(account);
}

if (updateMainResult && updateSubResult) {
return Response.responseMsg(RetailCodeEnum.UPDATE_RETAIL_SHIPMENTS_SUCCESS);
} else {
Expand Down Expand Up @@ -449,9 +468,21 @@ public Response<String> addOrUpdateRetailShipments(RetailShipmentsDTO shipmentsD
.collect(Collectors.toList());

var saveSubResult = receiptRetailSubService.saveBatch(receiptList);

updateProductStock(receiptList, 2);

var account = accountService.getById(shipmentsDTO.getAccountId());
if (account != null) {
// 更新余额
var accountBalance = account.getCurrentAmount();
var changeAmount = shipmentsDTO.getReceiptAmount();
if (changeAmount != null) {
accountBalance = accountBalance.add(changeAmount);
account.setId(shipmentsDTO.getAccountId());
account.setCurrentAmount(accountBalance);
accountService.updateById(account);
}
}

if (saveMainResult && saveSubResult) {
return Response.responseMsg(RetailCodeEnum.ADD_RETAIL_SHIPMENTS_SUCCESS);
} else {
Expand Down Expand Up @@ -628,6 +659,24 @@ public Response<String> addOrUpdateRetailRefund(RetailRefundDTO refundDTO) {
updateProductStock(receiptList, 1);
});

// 更新余额 如果之前已经修改过那么就需要加上之前的金额 然后再减去现在的金额 如果之前没有修改过那么就直接加上现在的金额 因为这个是退货 所以是负数
var account = accountService.getById(refundDTO.getAccountId());
if (account != null) {
var accountBalance = account.getCurrentAmount();
var changeAmount = refundDTO.getReceiptAmount();
var beforeChangeAmount = beforeReceipt.stream()
.map(item -> item.getTotalAmount())
.reduce(BigDecimal.ZERO, BigDecimal::add);
if (beforeChangeAmount != null) {
accountBalance = accountBalance.add(beforeChangeAmount);
}
if (changeAmount != null) {
accountBalance = accountBalance.subtract(changeAmount);
}
account.setCurrentAmount(accountBalance);
accountService.updateById(account);
}

if (updateMainResult && updateSubResult) {
return Response.responseMsg(RetailCodeEnum.UPDATE_RETAIL_REFUND_SUCCESS);
} else {
Expand Down Expand Up @@ -678,6 +727,19 @@ public Response<String> addOrUpdateRetailRefund(RetailRefundDTO refundDTO) {
var saveSubResult = receiptRetailSubService.saveBatch(receiptList);
updateProductStock(receiptList, 1);

var account = accountService.getById(refundDTO.getAccountId());
if (account != null) {
// 更新余额
var accountBalance = account.getCurrentAmount();
var changeAmount = refundDTO.getReceiptAmount();
if (changeAmount != null) {
accountBalance = accountBalance.subtract(changeAmount);
account.setId(refundDTO.getAccountId());
account.setCurrentAmount(accountBalance);
accountService.updateById(account);
}
}

if (saveMainResult && saveSubResult) {
return Response.responseMsg(RetailCodeEnum.ADD_RETAIL_REFUND_SUCCESS);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,23 @@ public Response<String> addOrUpdateSaleShipments(SaleShipmentsDTO shipmentsDTO)
updateProductStock(receiptSaleList, 2);
});

var account = accountService.getById(shipmentsDTO.getAccountId());
if (account != null) {
var accountBalance = account.getCurrentAmount();
var thisCollectAmount = shipmentsDTO.getThisCollectAmount();
var beforeChangeAmount = beforeReceipt.stream()
.map(item -> item.getTotalAmount())
.reduce(BigDecimal.ZERO, BigDecimal::add);
if (beforeChangeAmount != null) {
accountBalance = accountBalance.subtract(beforeChangeAmount);
}
if (thisCollectAmount != null) {
accountBalance = accountBalance.add(thisCollectAmount);
}
account.setCurrentAmount(accountBalance);
accountService.updateById(account);
}

if (updateMainResult && updateSubResult) {
return Response.responseMsg(SaleCodeEnum.UPDATE_SALE_SHIPMENTS_SUCCESS);
} else {
Expand Down Expand Up @@ -769,6 +786,18 @@ public Response<String> addOrUpdateSaleShipments(SaleShipmentsDTO shipmentsDTO)
var saveSubResult = receiptSaleSubService.saveBatch(receiptList);
updateProductStock(receiptList, 2);

var account = accountService.getById(shipmentsDTO.getAccountId());
if (account != null) {
var accountBalance = account.getCurrentAmount();
var changeAmount = shipmentsDTO.getThisCollectAmount();
if (changeAmount != null) {
accountBalance = accountBalance.add(changeAmount);
account.setId(shipmentsDTO.getAccountId());
account.setCurrentAmount(accountBalance);
accountService.updateById(account);
}
}

if (saveMainResult && saveSubResult) {
return Response.responseMsg(SaleCodeEnum.ADD_SALE_SHIPMENTS_SUCCESS);
} else {
Expand Down Expand Up @@ -948,7 +977,7 @@ public Response<String> addOrUpdateSaleRefund(SaleRefundDTO refundDTO) {
.set(refundDTO.getRefundOfferAmount() != null, ReceiptSaleMain::getDiscountAmount, refundDTO.getRefundOfferAmount())
.set(refundDTO.getRefundLastAmount() != null, ReceiptSaleMain::getDiscountLastAmount, refundDTO.getRefundLastAmount())
.set(refundDTO.getOtherAmount() != null, ReceiptSaleMain::getOtherAmount, refundDTO.getOtherAmount())
.set(refundDTO.getThisRefundAmount() != null, ReceiptSaleMain::getChangeAmount, refundDTO.getThisRefundAmount())
.set(refundDTO.getThisRefundAmount() != null, ReceiptSaleMain::getChangeAmount, refundDTO.getThisRefundAmount().negate())
.set(refundDTO.getThisArrearsAmount() != null, ReceiptSaleMain::getArrearsAmount, refundDTO.getThisArrearsAmount())
.set(refundDTO.getStatus() != null, ReceiptSaleMain::getStatus, refundDTO.getStatus())
.set(StringUtils.hasLength(refundDTO.getOtherReceipt()), ReceiptSaleMain::getOtherReceipt, refundDTO.getOtherReceipt())
Expand Down Expand Up @@ -990,6 +1019,23 @@ public Response<String> addOrUpdateSaleRefund(SaleRefundDTO refundDTO) {
updateProductStock(receiptSaleList, 1);
});

var account = accountService.getById(refundDTO.getAccountId());
if (account != null) {
var accountBalance = account.getCurrentAmount();
var thisRefundAmount = refundDTO.getThisRefundAmount();
var beforeChangeAmount = beforeReceipt.stream()
.map(item -> item.getTotalAmount())
.reduce(BigDecimal.ZERO, BigDecimal::add);
if (beforeChangeAmount != null) {
accountBalance = accountBalance.add(beforeChangeAmount);
}
if (thisRefundAmount != null) {
accountBalance = accountBalance.subtract(thisRefundAmount);
}
account.setCurrentAmount(accountBalance);
accountService.updateById(account);
}

if (updateMainResult && updateSubResult) {
return Response.responseMsg(SaleCodeEnum.UPDATE_SALE_REFUND_SUCCESS);
} else {
Expand All @@ -1013,7 +1059,7 @@ public Response<String> addOrUpdateSaleRefund(SaleRefundDTO refundDTO) {
.discountLastAmount(refundDTO.getRefundLastAmount())
.otherAmount(refundDTO.getOtherAmount())
.otherReceipt(refundDTO.getOtherReceipt())
.changeAmount(refundDTO.getThisRefundAmount())
.changeAmount(refundDTO.getThisRefundAmount().negate())
.arrearsAmount(refundDTO.getThisArrearsAmount())
.multipleAccount(multipleAccountIds)
.multipleAccountAmount(multipleAccountAmounts)
Expand Down Expand Up @@ -1045,6 +1091,17 @@ public Response<String> addOrUpdateSaleRefund(SaleRefundDTO refundDTO) {

var saveSubResult = receiptSaleSubService.saveBatch(receiptList);
updateProductStock(receiptList,1);
var account = accountService.getById(refundDTO.getAccountId());
if (account != null) {
var accountBalance = account.getCurrentAmount();
var thisRefundAmount = refundDTO.getThisRefundAmount();
if (thisRefundAmount != null) {
accountBalance = accountBalance.subtract(thisRefundAmount);
account.setId(refundDTO.getAccountId());
account.setCurrentAmount(accountBalance);
accountService.updateById(account);
}
}

if (saveMainResult && saveSubResult) {
return Response.responseMsg(SaleCodeEnum.ADD_SALE_REFUND_SUCCESS);
Expand Down
Loading