mirror of https://github.com/jeecgboot/jeecg-boot
Merge pull request #50 from LQYBill/fix/InitBalanceError
fix : handle not initialized balance error and uninvoiced purchase fe…pull/6221/head
commit
d2cad68020
|
@ -6,8 +6,10 @@ import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import org.jeecg.common.api.vo.Result;
|
import org.jeecg.common.api.vo.Result;
|
||||||
import org.jeecg.common.system.query.QueryGenerator;
|
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.entity.Credit;
|
||||||
import org.jeecg.modules.business.service.IBalanceService;
|
import org.jeecg.modules.business.service.IBalanceService;
|
||||||
|
import org.jeecg.modules.business.service.IClientService;
|
||||||
import org.jeecg.modules.business.service.ICreditService;
|
import org.jeecg.modules.business.service.ICreditService;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
@ -40,6 +42,8 @@ public class CreditController extends JeecgController<Credit, ICreditService> {
|
||||||
private ICreditService creditService;
|
private ICreditService creditService;
|
||||||
@Autowired
|
@Autowired
|
||||||
private IBalanceService balanceService;
|
private IBalanceService balanceService;
|
||||||
|
@Autowired
|
||||||
|
private IClientService clientService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 分页列表查询
|
* 分页列表查询
|
||||||
|
@ -74,7 +78,13 @@ public class CreditController extends JeecgController<Credit, ICreditService> {
|
||||||
@PostMapping(value = "/add")
|
@PostMapping(value = "/add")
|
||||||
public Result<String> add(@RequestBody Credit credit) {
|
public Result<String> add(@RequestBody Credit credit) {
|
||||||
creditService.save(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");
|
return Result.OK("sys.api.entryAddSuccess");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -92,44 +92,67 @@ public class TransactionController {
|
||||||
public Result<?> debit(@RequestParam("clientId") String clientId, @RequestParam("currency") String currency) {
|
public Result<?> debit(@RequestParam("clientId") String clientId, @RequestParam("currency") String currency) {
|
||||||
List<String> errorMessages = new ArrayList<>();
|
List<String> errorMessages = new ArrayList<>();
|
||||||
List<String> shopIds = shopService.listIdByClient(clientId);
|
List<String> shopIds = shopService.listIdByClient(clientId);
|
||||||
List<PlatformOrder> orders = platformOrderService.findUninvoicedShippingOrdersByShopForClient(shopIds, Arrays.asList(1,2,3));
|
List<PlatformOrder> shippingOrders = platformOrderService.findUninvoicedShippingOrdersByShopForClient(shopIds, Arrays.asList(1,2,3));
|
||||||
if(orders.isEmpty())
|
Date startDate = null;
|
||||||
return Result.OK("No order to invoice.");
|
Date endDate = null;
|
||||||
Date startDate = orders.stream().map(PlatformOrder::getOrderTime).min(Date::compareTo).get();
|
List<ShippingFeesEstimation> shippingFeesEstimations;
|
||||||
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<>();
|
|
||||||
BigDecimal shippingFeesEstimation = BigDecimal.ZERO;
|
BigDecimal shippingFeesEstimation = BigDecimal.ZERO;
|
||||||
for(ShippingFeesEstimation estimation: shippingFeesEstimations) {
|
if(!shippingOrders.isEmpty()) {
|
||||||
estimationOrderIds.addAll(estimation.getOrderIds());
|
startDate = shippingOrders.stream().map(PlatformOrder::getOrderTime).min(Date::compareTo).get();
|
||||||
shippingFeesEstimation = shippingFeesEstimation.add(estimation.getDueForProcessedOrders());
|
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());
|
// purchase estimation
|
||||||
List<SkuPrice> skuPrices = platformOrderContentMapper.searchSkuPrice(skuIds);
|
List<PlatformOrder> purchaseOrders = platformOrderService.fetchUninvoicedPurchaseOrdersByShopForClient(shopIds, Arrays.asList(1,2,3));
|
||||||
BigDecimal exchangeRateEurToRmb = exchangeRatesMapper.getLatestExchangeRate("EUR", "RMB");
|
|
||||||
BigDecimal purchaseEstimation = BigDecimal.ZERO;
|
BigDecimal purchaseEstimation = BigDecimal.ZERO;
|
||||||
boolean isCompleteInvoiceReady = true;
|
|
||||||
if(skuPrices.size() != skuIds.size()) {
|
if(shippingOrders.isEmpty() && purchaseOrders.isEmpty()) {
|
||||||
isCompleteInvoiceReady = false;
|
return Result.OK("No order to invoice.");
|
||||||
errorMessages.add("Some sku prices are missing.");
|
|
||||||
}
|
}
|
||||||
for(PlatformOrderContent content : orderContents){
|
|
||||||
for (SkuPrice skuPrice : skuPrices) {
|
boolean isCompleteInvoiceReady = true;
|
||||||
if(content.getSkuId().equals(skuPrice.getSkuId())) {
|
if(!purchaseOrders.isEmpty()) {
|
||||||
purchaseEstimation = purchaseEstimation.add(skuPrice.getPrice(content.getQuantity(), exchangeRateEurToRmb));
|
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")) {
|
if(!currency.equals("EUR")) {
|
||||||
BigDecimal exchangeRate = exchangeRatesMapper.getLatestExchangeRate("EUR", currency);
|
BigDecimal exchangeRate = exchangeRatesMapper.getLatestExchangeRate("EUR", currency);
|
||||||
|
|
||||||
|
|
|
@ -186,6 +186,7 @@ public interface PlatformOrderMapper extends BaseMapper<PlatformOrder> {
|
||||||
void cancelBatchInvoice(@Param("invoiceNumbers") List<String> invoiceNumbers);
|
void cancelBatchInvoice(@Param("invoiceNumbers") List<String> invoiceNumbers);
|
||||||
|
|
||||||
List<PlatformOrder> findUninvoicedShippingOrdersByShopForClient(@Param("shopIds") List<String> shopIds, @Param("erpStatuses") List<Integer> erpStatuses);
|
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<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);
|
List<String> findUninvoicedOrderIdsByShopForClient(@Param("shopIds") List<String> shopIds, @Param("erpStatuses") List<Integer> erpStatuses);
|
||||||
|
|
||||||
|
|
|
@ -575,6 +575,37 @@
|
||||||
#{shopId}
|
#{shopId}
|
||||||
</foreach>;
|
</foreach>;
|
||||||
</select>
|
</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 id="findUninvoicedOrdersByShopForClient" resultType="org.jeecg.modules.business.entity.PlatformOrder">
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM platform_order
|
FROM platform_order
|
||||||
|
|
|
@ -187,6 +187,14 @@ public interface IPlatformOrderService extends IService<PlatformOrder> {
|
||||||
*/
|
*/
|
||||||
List<PlatformOrder> findUninvoicedShippingOrdersByShopForClient(List<String> shopIds, List<Integer> erpStatuses);
|
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).
|
* Find all order that can be invoiced (shipping and purchase).
|
||||||
* @param shopIds
|
* @param shopIds
|
||||||
|
|
|
@ -67,6 +67,10 @@ public class BalanceServiceImpl extends ServiceImpl<BalanceMapper, Balance> impl
|
||||||
public void updateBalance(String clientId, String CreditId, BigDecimal amount, String currencyId) {
|
public void updateBalance(String clientId, String CreditId, BigDecimal amount, String currencyId) {
|
||||||
String currency = currencyService.getCodeById(currencyId);
|
String currency = currencyService.getCodeById(currencyId);
|
||||||
BigDecimal previousBalance = getBalanceByClientIdAndCurrency(clientId, currency);
|
BigDecimal previousBalance = getBalanceByClientIdAndCurrency(clientId, currency);
|
||||||
|
|
||||||
|
if(previousBalance == null) {
|
||||||
|
throw new RuntimeException("Please initialize balance first !");
|
||||||
|
}
|
||||||
BigDecimal currentBalance = previousBalance.add(amount);
|
BigDecimal currentBalance = previousBalance.add(amount);
|
||||||
SysUser sysUser = new SysUser();
|
SysUser sysUser = new SysUser();
|
||||||
Balance balance = Balance.of(sysUser.getUsername(), clientId, currencyId, Balance.OperationType.Credit.name(), CreditId, currentBalance);
|
Balance balance = Balance.of(sysUser.getUsername(), clientId, currencyId, Balance.OperationType.Credit.name(), CreditId, currentBalance);
|
||||||
|
|
|
@ -404,6 +404,11 @@ public class PlatformOrderServiceImpl extends ServiceImpl<PlatformOrderMapper, P
|
||||||
return platformOrderMap.findUninvoicedShippingOrdersByShopForClient(shopIds, erpStatuses);
|
return platformOrderMap.findUninvoicedShippingOrdersByShopForClient(shopIds, erpStatuses);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<PlatformOrder> fetchUninvoicedPurchaseOrdersByShopForClient(List<String> shopIds, List<Integer> erpStatuses) {
|
||||||
|
return platformOrderMap.fetchUninvoicedPurchaseOrdersByShopForClient(shopIds, erpStatuses);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<PlatformOrder> findUninvoicedOrdersByShopForClient(List<String> shopIds, List<Integer> erpStatuses) {
|
public List<PlatformOrder> findUninvoicedOrdersByShopForClient(List<String> shopIds, List<Integer> erpStatuses) {
|
||||||
return platformOrderMap.findUninvoicedOrdersByShopForClient(shopIds, erpStatuses);
|
return platformOrderMap.findUninvoicedOrdersByShopForClient(shopIds, erpStatuses);
|
||||||
|
|
Loading…
Reference in New Issue