Merge pull request #50 from LQYBill/fix/InitBalanceError

fix : handle not initialized balance error and uninvoiced purchase fe…
pull/6221/head
Qiuyi LI 2024-01-11 15:08:43 +01:00 committed by GitHub
commit d2cad68020
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 114 additions and 32 deletions

View File

@ -6,8 +6,10 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.business.entity.Client;
import org.jeecg.modules.business.entity.Credit;
import org.jeecg.modules.business.service.IBalanceService;
import org.jeecg.modules.business.service.IClientService;
import org.jeecg.modules.business.service.ICreditService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@ -40,6 +42,8 @@ public class CreditController extends JeecgController<Credit, ICreditService> {
private ICreditService creditService;
@Autowired
private IBalanceService balanceService;
@Autowired
private IClientService clientService;
/**
*
@ -74,7 +78,13 @@ public class CreditController extends JeecgController<Credit, ICreditService> {
@PostMapping(value = "/add")
public Result<String> add(@RequestBody Credit credit) {
creditService.save(credit);
balanceService.updateBalance(credit.getClientId(), credit.getId(), credit.getAmount(), credit.getCurrencyId());
try {
balanceService.updateBalance(credit.getClientId(), credit.getId(), credit.getAmount(), credit.getCurrencyId());
}
catch (RuntimeException e) {
Client client = clientService.getById(credit.getClientId());
return Result.error("Error while updating " + client.fullName() + "'s balance : " + e.getMessage());
}
return Result.OK("sys.api.entryAddSuccess");
}

View File

@ -92,44 +92,67 @@ public class TransactionController {
public Result<?> debit(@RequestParam("clientId") String clientId, @RequestParam("currency") String currency) {
List<String> errorMessages = new ArrayList<>();
List<String> shopIds = shopService.listIdByClient(clientId);
List<PlatformOrder> orders = platformOrderService.findUninvoicedShippingOrdersByShopForClient(shopIds, Arrays.asList(1,2,3));
if(orders.isEmpty())
return Result.OK("No order to invoice.");
Date startDate = orders.stream().map(PlatformOrder::getOrderTime).min(Date::compareTo).get();
Date endDate = orders.stream().map(PlatformOrder::getOrderTime).max(Date::compareTo).get();
List<String> orderIds = orders.stream().map(PlatformOrder::getId).collect(Collectors.toList());
log.info("{} Orders to be estimated", orderIds.size());
ShippingInvoiceFactory factory = new ShippingInvoiceFactory(
platformOrderService, clientMapper, shopMapper, logisticChannelMapper, logisticChannelPriceMapper,
platformOrderContentService, skuDeclaredValueService, countryService, exchangeRatesMapper,
purchaseOrderService, purchaseOrderContentMapper, skuPromotionHistoryMapper, savRefundService, savRefundWithDetailService, emailService, env);
List<ShippingFeesEstimation> shippingFeesEstimations = factory.getEstimations(clientId, orderIds, errorMessages);
if(shippingFeesEstimations.isEmpty())
return Result.OK("No estimation found.");
// purchase estimation
List<String> estimationOrderIds = new ArrayList<>();
List<PlatformOrder> shippingOrders = platformOrderService.findUninvoicedShippingOrdersByShopForClient(shopIds, Arrays.asList(1,2,3));
Date startDate = null;
Date endDate = null;
List<ShippingFeesEstimation> shippingFeesEstimations;
BigDecimal shippingFeesEstimation = BigDecimal.ZERO;
for(ShippingFeesEstimation estimation: shippingFeesEstimations) {
estimationOrderIds.addAll(estimation.getOrderIds());
shippingFeesEstimation = shippingFeesEstimation.add(estimation.getDueForProcessedOrders());
if(!shippingOrders.isEmpty()) {
startDate = shippingOrders.stream().map(PlatformOrder::getOrderTime).min(Date::compareTo).get();
endDate = shippingOrders.stream().map(PlatformOrder::getOrderTime).max(Date::compareTo).get();
List<String> shippingOrderIds = shippingOrders.stream().map(PlatformOrder::getId).collect(Collectors.toList());
log.info("Estimating shipping fees for {}", shippingOrderIds.size());
ShippingInvoiceFactory factory = new ShippingInvoiceFactory(
platformOrderService, clientMapper, shopMapper, logisticChannelMapper, logisticChannelPriceMapper,
platformOrderContentService, skuDeclaredValueService, countryService, exchangeRatesMapper,
purchaseOrderService, purchaseOrderContentMapper, skuPromotionHistoryMapper, savRefundService, savRefundWithDetailService, emailService, env);
shippingFeesEstimations = factory.getEstimations(clientId, shippingOrderIds, errorMessages);
for (ShippingFeesEstimation estimation : shippingFeesEstimations) {
shippingFeesEstimation = shippingFeesEstimation.add(estimation.getDueForProcessedOrders());
}
}
List<PlatformOrderContent> orderContents = platformOrderContentMapper.fetchOrderContent(estimationOrderIds);
List<String> skuIds = orderContents.stream().map(PlatformOrderContent::getSkuId).collect(Collectors.toList());
List<SkuPrice> skuPrices = platformOrderContentMapper.searchSkuPrice(skuIds);
BigDecimal exchangeRateEurToRmb = exchangeRatesMapper.getLatestExchangeRate("EUR", "RMB");
// purchase estimation
List<PlatformOrder> purchaseOrders = platformOrderService.fetchUninvoicedPurchaseOrdersByShopForClient(shopIds, Arrays.asList(1,2,3));
BigDecimal purchaseEstimation = BigDecimal.ZERO;
boolean isCompleteInvoiceReady = true;
if(skuPrices.size() != skuIds.size()) {
isCompleteInvoiceReady = false;
errorMessages.add("Some sku prices are missing.");
if(shippingOrders.isEmpty() && purchaseOrders.isEmpty()) {
return Result.OK("No order to invoice.");
}
for(PlatformOrderContent content : orderContents){
for (SkuPrice skuPrice : skuPrices) {
if(content.getSkuId().equals(skuPrice.getSkuId())) {
purchaseEstimation = purchaseEstimation.add(skuPrice.getPrice(content.getQuantity(), exchangeRateEurToRmb));
boolean isCompleteInvoiceReady = true;
if(!purchaseOrders.isEmpty()) {
Date purchaseStartDate = purchaseOrders.stream().map(PlatformOrder::getOrderTime).min(Date::compareTo).get();
Date purchaseEndDate = purchaseOrders.stream().map(PlatformOrder::getOrderTime).max(Date::compareTo).get();
if(startDate == null)
startDate = purchaseStartDate;
else
startDate = startDate.before(purchaseStartDate) ? startDate : purchaseStartDate;
if(endDate == null)
endDate = purchaseEndDate;
else
endDate = endDate.after(purchaseEndDate) ? endDate : purchaseEndDate;
List<String> purchaseOrderIds = purchaseOrders.stream().map(PlatformOrder::getId).collect(Collectors.toList());
List<PlatformOrderContent> orderContents = platformOrderContentMapper.fetchOrderContent(purchaseOrderIds);
List<String> skuIds = orderContents.stream().map(PlatformOrderContent::getSkuId).collect(Collectors.toList());
List<SkuPrice> skuPrices = platformOrderContentMapper.searchSkuPrice(skuIds);
BigDecimal exchangeRateEurToRmb = exchangeRatesMapper.getLatestExchangeRate("EUR", "RMB");
if (skuPrices.size() != skuIds.size()) {
isCompleteInvoiceReady = false;
errorMessages.add("Some sku prices are missing.");
}
for (PlatformOrderContent content : orderContents) {
for (SkuPrice skuPrice : skuPrices) {
if (content.getSkuId().equals(skuPrice.getSkuId())) {
purchaseEstimation = purchaseEstimation.add(skuPrice.getPrice(content.getQuantity(), exchangeRateEurToRmb));
}
}
}
}
if(shippingFeesEstimation.compareTo(BigDecimal.ZERO) == 0 && purchaseEstimation.compareTo(BigDecimal.ZERO) == 0) {
return Result.OK("No estimation found.");
}
if(!currency.equals("EUR")) {
BigDecimal exchangeRate = exchangeRatesMapper.getLatestExchangeRate("EUR", currency);

View File

@ -186,6 +186,7 @@ public interface PlatformOrderMapper extends BaseMapper<PlatformOrder> {
void cancelBatchInvoice(@Param("invoiceNumbers") List<String> invoiceNumbers);
List<PlatformOrder> findUninvoicedShippingOrdersByShopForClient(@Param("shopIds") List<String> shopIds, @Param("erpStatuses") List<Integer> erpStatuses);
List<PlatformOrder> fetchUninvoicedPurchaseOrdersByShopForClient(@Param("shopIds") List<String> shopIds, @Param("erpStatuses") List<Integer> erpStatuses);
List<PlatformOrder> findUninvoicedOrdersByShopForClient(@Param("shopIds") List<String> shopIds, @Param("erpStatuses") List<Integer> erpStatuses);
List<String> findUninvoicedOrderIdsByShopForClient(@Param("shopIds") List<String> shopIds, @Param("erpStatuses") List<Integer> erpStatuses);

View File

@ -575,6 +575,37 @@
#{shopId}
</foreach>;
</select>
<select id="fetchUninvoicedPurchaseOrdersByShopForClient" resultType="org.jeecg.modules.business.entity.PlatformOrder">
SELECT *
FROM platform_order
WHERE IF(shipping_invoice_number IS NOT NULL, shipping_invoice_number LIKE '%%%%-%%-2%%%',
shipping_invoice_number IS NULL)
AND purchase_invoice_number IS NULL
AND product_available = 0
AND virtual_product_available = 0
AND erp_status IN
<foreach
collection="erpStatuses"
separator=","
open="("
close=")"
index="index"
item="erpStatus"
>
#{erpStatus}
</foreach>
AND shop_id IN
<foreach
collection="shopIds"
separator=","
open="("
close=")"
index="index"
item="shopId"
>
#{shopId}
</foreach>;
</select>
<select id="findUninvoicedOrdersByShopForClient" resultType="org.jeecg.modules.business.entity.PlatformOrder">
SELECT *
FROM platform_order

View File

@ -187,6 +187,14 @@ public interface IPlatformOrderService extends IService<PlatformOrder> {
*/
List<PlatformOrder> findUninvoicedShippingOrdersByShopForClient(List<String> shopIds, List<Integer> erpStatuses);
/**
* Find all orders that can be invoiced (purchase only).
* @param shopIds
* @param erpStatuses
* @return
*/
List<PlatformOrder> fetchUninvoicedPurchaseOrdersByShopForClient(List<String> shopIds, List<Integer> erpStatuses);
/**
* Find all order that can be invoiced (shipping and purchase).
* @param shopIds

View File

@ -67,6 +67,10 @@ public class BalanceServiceImpl extends ServiceImpl<BalanceMapper, Balance> impl
public void updateBalance(String clientId, String CreditId, BigDecimal amount, String currencyId) {
String currency = currencyService.getCodeById(currencyId);
BigDecimal previousBalance = getBalanceByClientIdAndCurrency(clientId, currency);
if(previousBalance == null) {
throw new RuntimeException("Please initialize balance first !");
}
BigDecimal currentBalance = previousBalance.add(amount);
SysUser sysUser = new SysUser();
Balance balance = Balance.of(sysUser.getUsername(), clientId, currencyId, Balance.OperationType.Credit.name(), CreditId, currentBalance);

View File

@ -404,6 +404,11 @@ public class PlatformOrderServiceImpl extends ServiceImpl<PlatformOrderMapper, P
return platformOrderMap.findUninvoicedShippingOrdersByShopForClient(shopIds, erpStatuses);
}
@Override
public List<PlatformOrder> fetchUninvoicedPurchaseOrdersByShopForClient(List<String> shopIds, List<Integer> erpStatuses) {
return platformOrderMap.fetchUninvoicedPurchaseOrdersByShopForClient(shopIds, erpStatuses);
}
@Override
public List<PlatformOrder> findUninvoicedOrdersByShopForClient(List<String> shopIds, List<Integer> erpStatuses) {
return platformOrderMap.findUninvoicedOrdersByShopForClient(shopIds, erpStatuses);