/** * 按照百分比分摊 order级别的discount * 当商品行价格<需要扣减的价格,取商品行价格作为抵扣 * 如果到最后个产品商品行依然<需要扣减的价格,抛出异常 * @param orderLines * @param orderPayment * @param wholePromotions * @return * @throws OrderValidateException */ private BigDecimal divideWholeDiscount(List<OrderLineCommand> orderLines,BigDecimal orderPayment,BigDecimal origionOrderPayment ,List<OrderPromotionCommand> wholePromotions) throws OrderValidateException{ /*if(orderPayment==null||orderPayment.compareTo(BigDecimal.ZERO)<1){ throw new OrderValidateException("payment less than zero",ValidateException.ORDER_DISCOUNT_VALIDATE_EXCEPTION); }*/ BigDecimal sumDiscount = BigDecimal.ZERO; if(wholePromotions!=null){ BigDecimal wholeDiscount = BigDecimal.ZERO; for(OrderPromotionCommand wholePromotion : wholePromotions){ wholeDiscount = wholeDiscount.add(wholePromotion.getDiscountAmount()); } sumDiscount = sumDiscount.add(wholeDiscount); Collections.sort(orderLines,new Comparator<OrderLineCommand>() { public int compare(OrderLineCommand o1, OrderLineCommand o2) { if(Validator.isNullOrEmpty(o1)||Validator.isNullOrEmpty(o2)) return 0; return o1.getSubTotal().compareTo(o2.getSubTotal()); } }); int index = 0; BigDecimal appliedDiscount = BigDecimal.ZERO; for(OrderLineCommand orderLine : orderLines){ index++; BigDecimal disPriceSum = orderLine.getSubTotal(); logger.debug("whole discount adjust ...:disPriceSum="+disPriceSum+",index="+index+",skuId="+orderLine.getSkuId()+",subtotal="+orderLine.getSubTotal()); BigDecimal adjustment = BigDecimal.ZERO; if(index==orderLines.size()){ //这里表示是最后一个商品了,剩余的discount全叠加在上面 BigDecimal lastDiscount = wholeDiscount.subtract(appliedDiscount); if(disPriceSum.compareTo(lastDiscount)<0){ throw new OrderValidateException("discount greater than order payment",ValidateException.ORDER_DISCOUNT_VALIDATE_EXCEPTION); }else{ adjustment = adjustment.add(lastDiscount); } }else{ BigDecimal percent = BigDecimal.ZERO; //获取当前商品行在整单里的一个百分比,取整 if(orderPayment.compareTo(BigDecimal.ZERO) ==1){ percent = disPriceSum.divide(orderPayment,BigDecimal.ROUND_HALF_UP,4); } else if(origionOrderPayment.compareTo(BigDecimal.ZERO) == 1){ percent = disPriceSum.divide(origionOrderPayment,BigDecimal.ROUND_HALF_UP,4); } adjustment = wholeDiscount.multiply(percent); adjustment = adjustment.setScale(2, BigDecimal.ROUND_HALF_UP); if(disPriceSum.compareTo(adjustment)<1){ adjustment = disPriceSum; } } appliedDiscount = appliedDiscount.add(adjustment); orderLine.setDiscount(orderLine.getDiscount().add(adjustment)); orderLine.setDiscountPrice(getDiscountPrice(orderLine.getSalePrice(), orderLine.getQuantity(), orderLine.getDiscount())); orderLine.setSubTotal(orderLine.getSubTotal().subtract(adjustment)); logger.debug("whole discount adjust ...:adjustment="+adjustment); } } return sumDiscount; } private BigDecimal getDiscountPrice(BigDecimal salesPrice,Integer qty,BigDecimal discountAmount){ BigDecimal qty_b = BigDecimal.valueOf(qty); BigDecimal discountPrice = salesPrice.multiply(qty_b).subtract(discountAmount).divide(qty_b,2,RoundingMode.HALF_UP); Money m_dp = new Money(discountPrice); return m_dp.getAmount(); }
促销计算价格
整单优惠的价格按照比例均摊到每一订单行价格
猜你喜欢
转载自572327713.iteye.com/blog/2387787
今日推荐
周排行