Example #1
0
  @Override
  public void returns(Order order, Returns returns, Admin operator) {
    Assert.notNull(order);
    Assert.notNull(returns);
    Assert.notEmpty(returns.getReturnsItems());

    orderDao.lock(order, LockModeType.PESSIMISTIC_WRITE);

    returns.setOrder(order);
    returnsDao.persist(returns);
    for (ReturnsItem returnsItem : returns.getReturnsItems()) {
      OrderItem orderItem = order.getOrderItem(returnsItem.getSn());
      if (orderItem != null) {
        orderItemDao.lock(orderItem, LockModeType.PESSIMISTIC_WRITE);
        orderItem.setReturnQuantity(orderItem.getReturnQuantity() + returnsItem.getQuantity());
      }
    }
    if (order.getReturnQuantity() >= order.getShippedQuantity()) {
      order.setShippingStatus(ShippingStatus.returned);
    } else if (order.getReturnQuantity() > 0) {
      order.setShippingStatus(ShippingStatus.partialReturns);
    }
    order.setExpire(null);
    orderDao.merge(order);

    OrderLog orderLog = new OrderLog();
    orderLog.setType(Type.returns);
    orderLog.setOperator(operator != null ? operator.getUsername() : null);
    orderLog.setOrder(order);
    orderLogDao.persist(orderLog);
  }
Example #2
0
  @Override
  public void refunds(Order order, Refunds refunds, Admin operator) {
    Assert.notNull(order);
    Assert.notNull(refunds);

    orderDao.lock(order, LockModeType.PESSIMISTIC_WRITE);

    refunds.setOrder(order);
    refundsDao.persist(refunds);
    if (refunds.getMethod() == Refunds.Method.deposit) {
      Member member = order.getMember();
      memberDao.lock(member, LockModeType.PESSIMISTIC_WRITE);
      member.setBalance(member.getBalance().add(refunds.getAmount()));
      memberDao.merge(member);

      Deposit deposit = new Deposit();
      deposit.setType(Deposit.Type.adminRefunds);
      deposit.setCredit(refunds.getAmount());
      deposit.setDebit(new BigDecimal(0));
      deposit.setBalance(member.getBalance());
      deposit.setOperator(operator != null ? operator.getUsername() : null);
      deposit.setMember(member);
      deposit.setOrder(order);
      depositDao.persist(deposit);
    }

    order.setAmountPaid(order.getAmountPaid().subtract(refunds.getAmount()));
    order.setExpire(null);
    if (order.getAmountPaid().compareTo(new BigDecimal(0)) == 0) {
      order.setPaymentStatus(PaymentStatus.refunded);
    } else if (order.getAmountPaid().compareTo(new BigDecimal(0)) > 0) {
      order.setPaymentStatus(PaymentStatus.partialRefunds);
    }
    orderDao.merge(order);

    OrderLog orderLog = new OrderLog();
    orderLog.setType(Type.refunds);
    orderLog.setOperator(operator != null ? operator.getUsername() : null);
    orderLog.setOrder(order);
    orderLogDao.persist(orderLog);
  }
Example #3
0
  @Override
  public void shipping(Order order, Shipping shipping, Admin operator) {
    Assert.notNull(order);
    Assert.notNull(shipping);
    Assert.notEmpty(shipping.getShippingItems());

    orderDao.lock(order, LockModeType.PESSIMISTIC_WRITE);

    Setting setting = SettingUtils.get();
    if (!order.getIsAllocatedStock()
        && setting.getStockAllocationTime() == StockAllocationTime.ship) {
      for (OrderItem orderItem : order.getOrderItems()) {
        if (orderItem != null) {
          Product product = orderItem.getProduct();
          productDao.lock(product, LockModeType.PESSIMISTIC_WRITE);
          if (product != null && product.getStock() != null) {
            product.setAllocatedStock(
                product.getAllocatedStock()
                    + (orderItem.getQuantity() - orderItem.getShippedQuantity()));
            productDao.merge(product);
            orderDao.flush();
            staticService.build(product);
          }
        }
      }
      order.setIsAllocatedStock(true);
    }

    shipping.setOrder(order);
    shippingDao.persist(shipping);
    for (ShippingItem shippingItem : shipping.getShippingItems()) {
      OrderItem orderItem = order.getOrderItem(shippingItem.getSn());
      if (orderItem != null) {
        Product product = orderItem.getProduct();
        productDao.lock(product, LockModeType.PESSIMISTIC_WRITE);
        if (product != null) {
          if (product.getStock() != null) {
            product.setStock(product.getStock() - shippingItem.getQuantity());
            if (order.getIsAllocatedStock()) {
              product.setAllocatedStock(product.getAllocatedStock() - shippingItem.getQuantity());
            }
          }
          productDao.merge(product);
          orderDao.flush();
          staticService.build(product);
        }
        orderItemDao.lock(orderItem, LockModeType.PESSIMISTIC_WRITE);
        orderItem.setShippedQuantity(orderItem.getShippedQuantity() + shippingItem.getQuantity());
      }
    }
    if (order.getShippedQuantity() >= order.getQuantity()) {
      order.setShippingStatus(ShippingStatus.shipped);
      order.setIsAllocatedStock(false);
    } else if (order.getShippedQuantity() > 0) {
      order.setShippingStatus(ShippingStatus.partialShipment);
    }
    order.setExpire(null);
    orderDao.merge(order);

    OrderLog orderLog = new OrderLog();
    orderLog.setType(Type.shipping);
    orderLog.setOperator(operator != null ? operator.getUsername() : null);
    orderLog.setOrder(order);
    orderLogDao.persist(orderLog);
  }
Example #4
0
  @Override
  public void payment(Order order, Payment payment, Admin operator) {
    Assert.notNull(order);
    Assert.notNull(payment);

    orderDao.lock(order, LockModeType.PESSIMISTIC_WRITE);

    payment.setOrder(order);
    paymentDao.merge(payment);
    if (payment.getMethod() == Payment.Method.deposit) {
      Member member = order.getMember();
      memberDao.lock(member, LockModeType.PESSIMISTIC_WRITE);
      member.setBalance(member.getBalance().subtract(payment.getAmount()));
      memberDao.merge(member);

      Deposit deposit = new Deposit();
      deposit.setType(operator != null ? Deposit.Type.adminPayment : Deposit.Type.memberPayment);
      deposit.setCredit(new BigDecimal(0));
      deposit.setDebit(payment.getAmount());
      deposit.setBalance(member.getBalance());
      deposit.setOperator(operator != null ? operator.getUsername() : null);
      deposit.setMember(member);
      deposit.setOrder(order);
      depositDao.persist(deposit);
    }

    Setting setting = SettingUtils.get();
    if (!order.getIsAllocatedStock()
        && setting.getStockAllocationTime() == StockAllocationTime.payment) {
      for (OrderItem orderItem : order.getOrderItems()) {
        if (orderItem != null) {
          Product product = orderItem.getProduct();
          productDao.lock(product, LockModeType.PESSIMISTIC_WRITE);
          if (product != null && product.getStock() != null) {
            product.setAllocatedStock(
                product.getAllocatedStock()
                    + (orderItem.getQuantity() - orderItem.getShippedQuantity()));
            productDao.merge(product);
            orderDao.flush();
            staticService.build(product);
          }
        }
      }
      order.setIsAllocatedStock(true);
    }

    order.setAmountPaid(order.getAmountPaid().add(payment.getAmount()));
    order.setFee(payment.getFee());
    order.setExpire(null);
    if (order.getAmountPaid().compareTo(order.getAmount()) >= 0) {
      order.setOrderStatus(OrderStatus.confirmed);
      order.setPaymentStatus(PaymentStatus.paid);
    } else if (order.getAmountPaid().compareTo(new BigDecimal(0)) > 0) {
      order.setOrderStatus(OrderStatus.confirmed);
      order.setPaymentStatus(PaymentStatus.partialPayment);
    }
    orderDao.merge(order);

    OrderLog orderLog = new OrderLog();
    orderLog.setType(Type.payment);
    orderLog.setOperator(operator != null ? operator.getUsername() : null);
    orderLog.setOrder(order);
    orderLogDao.persist(orderLog);
  }