protected void addTotals(final AbstractOrderModel source, final AbstractOrderData prototype) {
   prototype.setTotalPrice(createPrice(source, source.getTotalPrice()));
   prototype.setTotalTax(createPrice(source, source.getTotalTax()));
   prototype.setSubTotal(createPrice(source, source.getSubtotal()));
   prototype.setDeliveryCost(
       source.getDeliveryMode() != null ? createPrice(source, source.getDeliveryCost()) : null);
   prototype.setTotalPriceWithTax((createPrice(source, calcTotalWithTax(source))));
 }
 @Override
 protected boolean shouldHandleEvent(final SendNotPickedUpConsignmentCanceledMessageEvent event) {
   final AbstractOrderModel order = event.getProcess().getConsignment().getOrder();
   ServicesUtil.validateParameterNotNullStandardMessage("event.order", order);
   final BaseSiteModel site = order.getSite();
   ServicesUtil.validateParameterNotNullStandardMessage("event.order.site", site);
   return SiteChannel.B2C.equals(site.getChannel());
 }
  /**
   * Initially just to test if the delivery mode and address are set, than calculate the external
   * taxes. TODO: Hash of products in cart, delivery mode, delivery address and payment information
   * to determine whether or not to calculate taxes.
   */
  @Override
  public boolean shouldCalculateExternalTaxes(final AbstractOrderModel abstractOrder) {
    if (abstractOrder == null) {
      throw new IllegalStateException("Order is null. Cannot apply external tax to it.");
    }

    return (Boolean.TRUE.equals(abstractOrder.getNet())
        && abstractOrder.getDeliveryMode() != null
        && abstractOrder.getDeliveryAddress() != null);
  }
  protected void addPaymentInformation(
      final AbstractOrderModel source, final AbstractOrderData prototype) {
    final PaymentInfoModel paymentInfo = source.getPaymentInfo();
    if (paymentInfo instanceof CreditCardPaymentInfoModel) {
      final CCPaymentInfoData paymentInfoData =
          getCreditCardPaymentInfoConverter().convert((CreditCardPaymentInfoModel) paymentInfo);

      final boolean isDefaultPayment = isDefaultPaymentInfo(source.getUser(), paymentInfo);
      paymentInfoData.setDefaultPaymentInfo(isDefaultPayment);
      prototype.setPaymentInfo(paymentInfoData);
    }
  }
  protected Double calcTotalWithTax(final AbstractOrderModel source) {
    if (source == null) {
      throw new IllegalArgumentException("source order must not be null");
    }
    double totalPrice =
        source.getTotalPrice() == null ? 0.0d : source.getTotalPrice().doubleValue();

    // Add the taxes to the total price if the cart is net; if the total was null taxes should be
    // null as well
    if (Boolean.TRUE.equals(source.getNet()) && totalPrice != 0.0d) {
      totalPrice += source.getTotalTax() == null ? 0.0d : source.getTotalTax().doubleValue();
    }

    return Double.valueOf(totalPrice);
  }
 protected Integer calcTotalUnitCount(final AbstractOrderModel source) {
   int totalUnitCount = 0;
   for (final AbstractOrderEntryModel orderEntryModel : source.getEntries()) {
     totalUnitCount += orderEntryModel.getQuantity().intValue();
   }
   return Integer.valueOf(totalUnitCount);
 }
 protected void addCommon(final AbstractOrderModel source, final AbstractOrderData prototype) {
   prototype.setCode(source.getCode());
   if (source.getSite() != null) {
     prototype.setSite(source.getSite().getUid());
   }
   if (source.getStore() != null) {
     prototype.setStore(source.getStore().getUid());
   }
   prototype.setTotalItems(calcTotalItems(source));
   prototype.setNet(Boolean.TRUE.equals(source.getNet()));
   prototype.setGuid(source.getGuid());
   prototype.setCalculated(Boolean.TRUE.equals(source.getCalculated()));
 }
  protected double getOrderDiscountsAmount(final AbstractOrderModel source) {
    double discounts = 0.0d;
    final List<DiscountValue> discountList =
        source.getGlobalDiscountValues(); // discounts on the cart itself
    if (discountList != null && !discountList.isEmpty()) {
      for (final DiscountValue discount : discountList) {
        final double value = discount.getAppliedValue();
        if (value > 0.0d) {
          discounts += value;
        }
      }
    }

    return discounts;
  }
  protected PriceData createPrice(final AbstractOrderModel source, final Double val) {
    if (source == null) {
      throw new IllegalArgumentException("source order must not be null");
    }

    final CurrencyModel currency = source.getCurrency();
    if (currency == null) {
      throw new IllegalArgumentException("source order currency must not be null");
    }

    // Get double value, handle null as zero
    final double priceValue = val != null ? val.doubleValue() : 0d;

    return getPriceDataFactory()
        .create(PriceDataType.BUY, BigDecimal.valueOf(priceValue), currency);
  }
  protected double getProductsDiscountsAmount(final AbstractOrderModel source) {
    double discounts = 0.0d;

    final List<AbstractOrderEntryModel> entries = source.getEntries();
    if (entries != null) {
      for (final AbstractOrderEntryModel entry : entries) {
        final List<DiscountValue> discountValues = entry.getDiscountValues();
        if (discountValues != null) {
          for (final DiscountValue dValue : discountValues) {
            discounts += dValue.getAppliedValue();
          }
        }
      }
    }
    return discounts;
  }
  protected void addDeliveryMethod(
      final AbstractOrderModel source, final AbstractOrderData prototype) {
    final DeliveryModeModel deliveryMode = source.getDeliveryMode();
    if (deliveryMode != null) {
      // unnecessary if?
      if (deliveryMode instanceof ZoneDeliveryModeModel) {
        final ZoneDeliveryModeData zoneDeliveryModeData =
            getZoneDeliveryModeConverter().convert((ZoneDeliveryModeModel) deliveryMode);

        final PriceValue deliveryCost =
            getDeliveryService()
                .getDeliveryCostForDeliveryModeAndAbstractOrder(deliveryMode, source);
        if (deliveryCost != null) {
          zoneDeliveryModeData.setDeliveryCost(
              getPriceDataFactory()
                  .create(
                      PriceDataType.BUY,
                      BigDecimal.valueOf(deliveryCost.getValue()),
                      deliveryCost.getCurrencyIso()));
        }
        prototype.setDeliveryMode(zoneDeliveryModeData);
      } else {
        final DeliveryModeData deliveryModeData = getDeliveryModeConverter().convert(deliveryMode);

        final PriceValue deliveryCost =
            getDeliveryService()
                .getDeliveryCostForDeliveryModeAndAbstractOrder(deliveryMode, source);
        if (deliveryCost != null) {
          deliveryModeData.setDeliveryCost(
              getPriceDataFactory()
                  .create(
                      PriceDataType.BUY,
                      BigDecimal.valueOf(deliveryCost.getValue()),
                      deliveryCost.getCurrencyIso()));
        }
        prototype.setDeliveryMode(deliveryModeData);
      }
    }
  }
 protected void addPrincipalInformation(
     final AbstractOrderModel source, final AbstractOrderData target) {
   target.setUser(getPrincipalConverter().convert(source.getUser()));
 }
 protected void addDeliveryAddress(
     final AbstractOrderModel source, final AbstractOrderData prototype) {
   if (source.getDeliveryAddress() != null) {
     prototype.setDeliveryAddress(getAddressConverter().convert(source.getDeliveryAddress()));
   }
 }
 protected void addEntries(final AbstractOrderModel source, final AbstractOrderData prototype) {
   prototype.setEntries(Converters.convertAll(source.getEntries(), getOrderEntryConverter()));
 }
 protected Integer calcTotalItems(final AbstractOrderModel source) {
   return Integer.valueOf(source.getEntries().size());
 }