Search in sources :

Example 1 with OrderStatus

use of com.salesmanager.core.model.order.orderstatus.OrderStatus in project shopizer by shopizer-ecommerce.

the class PersistableOrderPopulator method populate.

@Override
public Order populate(PersistableOrder source, Order target, MerchantStore store, Language language) throws ConversionException {
    Validate.notNull(productService, "productService must be set");
    Validate.notNull(digitalProductService, "digitalProductService must be set");
    Validate.notNull(productAttributeService, "productAttributeService must be set");
    Validate.notNull(customerService, "customerService must be set");
    Validate.notNull(countryService, "countryService must be set");
    Validate.notNull(zoneService, "zoneService must be set");
    Validate.notNull(currencyService, "currencyService must be set");
    try {
        Map<String, Country> countriesMap = countryService.getCountriesMap(language);
        Map<String, Zone> zonesMap = zoneService.getZones(language);
        /**
         * customer *
         */
        PersistableCustomer customer = source.getCustomer();
        if (customer != null) {
            if (customer.getId() != null && customer.getId() > 0) {
                Customer modelCustomer = customerService.getById(customer.getId());
                if (modelCustomer == null) {
                    throw new ConversionException("Customer id " + customer.getId() + " does not exists");
                }
                if (modelCustomer.getMerchantStore().getId().intValue() != store.getId().intValue()) {
                    throw new ConversionException("Customer id " + customer.getId() + " does not exists for store " + store.getCode());
                }
                target.setCustomerId(modelCustomer.getId());
                target.setBilling(modelCustomer.getBilling());
                target.setDelivery(modelCustomer.getDelivery());
                target.setCustomerEmailAddress(source.getCustomer().getEmailAddress());
            }
        }
        target.setLocale(LocaleUtils.getLocale(store));
        CreditCard creditCard = source.getCreditCard();
        if (creditCard != null) {
            String maskedNumber = CreditCardUtils.maskCardNumber(creditCard.getCcNumber());
            creditCard.setCcNumber(maskedNumber);
            target.setCreditCard(creditCard);
        }
        Currency currency = null;
        try {
            currency = currencyService.getByCode(source.getCurrency());
        } catch (Exception e) {
            throw new ConversionException("Currency not found for code " + source.getCurrency());
        }
        if (currency == null) {
            throw new ConversionException("Currency not found for code " + source.getCurrency());
        }
        target.setCurrency(currency);
        target.setDatePurchased(source.getDatePurchased());
        // target.setCurrency(store.getCurrency());
        target.setCurrencyValue(new BigDecimal(0));
        target.setMerchant(store);
        target.setStatus(source.getOrderStatus());
        target.setPaymentModuleCode(source.getPaymentModule());
        target.setPaymentType(source.getPaymentType());
        target.setShippingModuleCode(source.getShippingModule());
        target.setCustomerAgreement(source.isCustomerAgreed());
        target.setConfirmedAddress(source.isConfirmedAddress());
        if (source.getPreviousOrderStatus() != null) {
            List<OrderStatus> orderStatusList = source.getPreviousOrderStatus();
            for (OrderStatus status : orderStatusList) {
                OrderStatusHistory statusHistory = new OrderStatusHistory();
                statusHistory.setStatus(status);
                statusHistory.setOrder(target);
                target.getOrderHistory().add(statusHistory);
            }
        }
        if (!StringUtils.isBlank(source.getComments())) {
            OrderStatusHistory statusHistory = new OrderStatusHistory();
            statusHistory.setStatus(null);
            statusHistory.setOrder(target);
            statusHistory.setComments(source.getComments());
            target.getOrderHistory().add(statusHistory);
        }
        List<PersistableOrderProduct> products = source.getOrderProductItems();
        if (CollectionUtils.isEmpty(products)) {
            throw new ConversionException("Requires at least 1 PersistableOrderProduct");
        }
        com.salesmanager.shop.populator.order.PersistableOrderProductPopulator orderProductPopulator = new PersistableOrderProductPopulator();
        orderProductPopulator.setProductAttributeService(productAttributeService);
        orderProductPopulator.setProductService(productService);
        orderProductPopulator.setDigitalProductService(digitalProductService);
        for (PersistableOrderProduct orderProduct : products) {
            OrderProduct modelOrderProduct = new OrderProduct();
            orderProductPopulator.populate(orderProduct, modelOrderProduct, store, language);
            target.getOrderProducts().add(modelOrderProduct);
        }
        List<OrderTotal> orderTotals = source.getTotals();
        if (CollectionUtils.isNotEmpty(orderTotals)) {
            for (OrderTotal total : orderTotals) {
                com.salesmanager.core.model.order.OrderTotal totalModel = new com.salesmanager.core.model.order.OrderTotal();
                totalModel.setModule(total.getModule());
                totalModel.setOrder(target);
                totalModel.setOrderTotalCode(total.getCode());
                totalModel.setTitle(total.getTitle());
                totalModel.setValue(total.getValue());
                target.getOrderTotal().add(totalModel);
            }
        }
    } catch (Exception e) {
        throw new ConversionException(e);
    }
    return target;
}
Also used : OrderProduct(com.salesmanager.core.model.order.orderproduct.OrderProduct) PersistableOrderProduct(com.salesmanager.shop.model.order.PersistableOrderProduct) Customer(com.salesmanager.core.model.customer.Customer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) PersistableOrderProduct(com.salesmanager.shop.model.order.PersistableOrderProduct) OrderStatus(com.salesmanager.core.model.order.orderstatus.OrderStatus) Currency(com.salesmanager.core.model.reference.currency.Currency) ConversionException(com.salesmanager.core.business.exception.ConversionException) Zone(com.salesmanager.core.model.reference.zone.Zone) CreditCard(com.salesmanager.core.model.order.payment.CreditCard) ConversionException(com.salesmanager.core.business.exception.ConversionException) BigDecimal(java.math.BigDecimal) Country(com.salesmanager.core.model.reference.country.Country) OrderStatusHistory(com.salesmanager.core.model.order.orderstatus.OrderStatusHistory) OrderTotal(com.salesmanager.shop.model.order.total.OrderTotal)

Example 2 with OrderStatus

use of com.salesmanager.core.model.order.orderstatus.OrderStatus in project shopizer by shopizer-ecommerce.

the class OrderFacadeImpl method initializeOrder.

@Override
public ShopOrder initializeOrder(MerchantStore store, Customer customer, ShoppingCart shoppingCart, Language language) throws Exception {
    // assert not null shopping cart items
    ShopOrder order = new ShopOrder();
    OrderStatus orderStatus = OrderStatus.ORDERED;
    order.setOrderStatus(orderStatus);
    if (customer == null) {
        customer = this.initEmptyCustomer(store);
    }
    PersistableCustomer persistableCustomer = persistableCustomer(customer, store, language);
    order.setCustomer(persistableCustomer);
    // keep list of shopping cart items for core price calculation
    List<ShoppingCartItem> items = new ArrayList<ShoppingCartItem>(shoppingCart.getLineItems());
    order.setShoppingCartItems(items);
    return order;
}
Also used : OrderStatus(com.salesmanager.core.model.order.orderstatus.OrderStatus) ShopOrder(com.salesmanager.shop.model.order.ShopOrder) PersistableCustomer(com.salesmanager.shop.model.customer.PersistableCustomer) ArrayList(java.util.ArrayList) ShoppingCartItem(com.salesmanager.core.model.shoppingcart.ShoppingCartItem)

Example 3 with OrderStatus

use of com.salesmanager.core.model.order.orderstatus.OrderStatus in project shopizer by shopizer-ecommerce.

the class OrderApi method updateOrderStatus.

@RequestMapping(value = { "/private/orders/{id}/status" }, method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
@ApiImplicitParams({ @ApiImplicitParam(name = "store", dataType = "string", defaultValue = "DEFAULT"), @ApiImplicitParam(name = "lang", dataType = "string", defaultValue = "en") })
public void updateOrderStatus(@PathVariable final Long id, @Valid @RequestBody String status, @ApiIgnore MerchantStore merchantStore, @ApiIgnore Language language) {
    String user = authorizationUtils.authenticatedUser();
    authorizationUtils.authorizeUser(user, Stream.of(Constants.GROUP_SUPERADMIN, Constants.GROUP_ADMIN, Constants.GROUP_ADMIN_ORDER, Constants.GROUP_ADMIN_RETAIL).collect(Collectors.toList()), merchantStore);
    Order order = orderService.getOrder(id, merchantStore);
    if (order == null) {
        throw new GenericRuntimeException("412", "Order not found [" + id + "]");
    }
    OrderStatus statusEnum = OrderStatus.valueOf(status);
    orderFacade.updateOrderStatus(order, statusEnum, merchantStore);
    return;
}
Also used : PersistableAnonymousOrder(com.salesmanager.shop.model.order.v1.PersistableAnonymousOrder) PersistableOrder(com.salesmanager.shop.model.order.v1.PersistableOrder) Order(com.salesmanager.core.model.order.Order) ReadableOrder(com.salesmanager.shop.model.order.v0.ReadableOrder) OrderStatus(com.salesmanager.core.model.order.orderstatus.OrderStatus) GenericRuntimeException(com.salesmanager.shop.store.api.exception.GenericRuntimeException) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 4 with OrderStatus

use of com.salesmanager.core.model.order.orderstatus.OrderStatus in project shopizer by shopizer-ecommerce.

the class OrderFacadeImpl method updateOrderStatus.

@Override
public void updateOrderStatus(Order order, OrderStatus newStatus, MerchantStore store) {
    // make sure we are changing to different that current status
    if (order.getStatus().equals(newStatus)) {
        // we have the same status, lets just return
        return;
    }
    OrderStatus oldStatus = order.getStatus();
    order.setStatus(newStatus);
    OrderStatusHistory history = new OrderStatusHistory();
    history.setComments(messages.getMessage("email.order.status.changed", new String[] { oldStatus.name(), newStatus.name() }, LocaleUtils.getLocale(store)));
    history.setCustomerNotified(0);
    history.setStatus(newStatus);
    history.setDateAdded(new Date());
    try {
        orderService.addOrderStatusHistory(order, history);
    } catch (ServiceException e) {
        e.printStackTrace();
    }
}
Also used : OrderStatus(com.salesmanager.core.model.order.orderstatus.OrderStatus) ServiceException(com.salesmanager.core.business.exception.ServiceException) ReadableOrderStatusHistory(com.salesmanager.shop.model.order.history.ReadableOrderStatusHistory) PersistableOrderStatusHistory(com.salesmanager.shop.model.order.history.PersistableOrderStatusHistory) OrderStatusHistory(com.salesmanager.core.model.order.orderstatus.OrderStatusHistory) Date(java.util.Date) LocalDate(java.time.LocalDate)

Example 5 with OrderStatus

use of com.salesmanager.core.model.order.orderstatus.OrderStatus in project shopizer by shopizer-ecommerce.

the class OrderServiceImpl method process.

private Order process(Order order, Customer customer, List<ShoppingCartItem> items, OrderTotalSummary summary, Payment payment, Transaction transaction, MerchantStore store) throws ServiceException {
    Validate.notNull(order, "Order cannot be null");
    Validate.notNull(customer, "Customer cannot be null (even if anonymous order)");
    Validate.notEmpty(items, "ShoppingCart items cannot be null");
    Validate.notNull(payment, "Payment cannot be null");
    Validate.notNull(store, "MerchantStore cannot be null");
    Validate.notNull(summary, "Order total Summary cannot be null");
    UserContext context = UserContext.getCurrentInstance();
    if (context != null) {
        String ipAddress = context.getIpAddress();
        if (!StringUtils.isBlank(ipAddress)) {
            order.setIpAddress(ipAddress);
        }
    }
    // first process payment
    Transaction processTransaction = paymentService.processPayment(customer, store, payment, items, order);
    if (order.getOrderHistory() == null || order.getOrderHistory().size() == 0 || order.getStatus() == null) {
        OrderStatus status = order.getStatus();
        if (status == null) {
            status = OrderStatus.ORDERED;
            order.setStatus(status);
        }
        Set<OrderStatusHistory> statusHistorySet = new HashSet<OrderStatusHistory>();
        OrderStatusHistory statusHistory = new OrderStatusHistory();
        statusHistory.setStatus(status);
        statusHistory.setDateAdded(new Date());
        statusHistory.setOrder(order);
        statusHistorySet.add(statusHistory);
        order.setOrderHistory(statusHistorySet);
    }
    if (customer.getId() == null || customer.getId() == 0) {
        customerService.create(customer);
    }
    order.setCustomerId(customer.getId());
    this.create(order);
    if (transaction != null) {
        transaction.setOrder(order);
        if (transaction.getId() == null || transaction.getId() == 0) {
            transactionService.create(transaction);
        } else {
            transactionService.update(transaction);
        }
    }
    if (processTransaction != null) {
        processTransaction.setOrder(order);
        if (processTransaction.getId() == null || processTransaction.getId() == 0) {
            transactionService.create(processTransaction);
        } else {
            transactionService.update(processTransaction);
        }
    }
    /**
     * decrement inventory
     */
    LOGGER.debug("Update inventory");
    Set<OrderProduct> products = order.getOrderProducts();
    for (OrderProduct orderProduct : products) {
        orderProduct.getProductQuantity();
        Product p = productService.getById(orderProduct.getId());
        if (p == null)
            throw new ServiceException(ServiceException.EXCEPTION_INVENTORY_MISMATCH);
        for (ProductAvailability availability : p.getAvailabilities()) {
            int qty = availability.getProductQuantity();
            if (qty < orderProduct.getProductQuantity()) {
                // throw new ServiceException(ServiceException.EXCEPTION_INVENTORY_MISMATCH);
                LOGGER.error("APP-BACKEND [" + ServiceException.EXCEPTION_INVENTORY_MISMATCH + "]");
            }
            qty = qty - orderProduct.getProductQuantity();
            availability.setProductQuantity(qty);
        }
        productService.update(p);
    }
    return order;
}
Also used : OrderProduct(com.salesmanager.core.model.order.orderproduct.OrderProduct) UserContext(com.salesmanager.core.model.common.UserContext) OrderProduct(com.salesmanager.core.model.order.orderproduct.OrderProduct) Product(com.salesmanager.core.model.catalog.product.Product) Date(java.util.Date) LocalDate(java.time.LocalDate) OrderStatus(com.salesmanager.core.model.order.orderstatus.OrderStatus) Transaction(com.salesmanager.core.model.payments.Transaction) ServiceException(com.salesmanager.core.business.exception.ServiceException) ProductAvailability(com.salesmanager.core.model.catalog.product.availability.ProductAvailability) OrderStatusHistory(com.salesmanager.core.model.order.orderstatus.OrderStatusHistory) HashSet(java.util.HashSet)

Aggregations

OrderStatus (com.salesmanager.core.model.order.orderstatus.OrderStatus)5 OrderStatusHistory (com.salesmanager.core.model.order.orderstatus.OrderStatusHistory)3 ServiceException (com.salesmanager.core.business.exception.ServiceException)2 OrderProduct (com.salesmanager.core.model.order.orderproduct.OrderProduct)2 PersistableCustomer (com.salesmanager.shop.model.customer.PersistableCustomer)2 LocalDate (java.time.LocalDate)2 Date (java.util.Date)2 ConversionException (com.salesmanager.core.business.exception.ConversionException)1 Product (com.salesmanager.core.model.catalog.product.Product)1 ProductAvailability (com.salesmanager.core.model.catalog.product.availability.ProductAvailability)1 UserContext (com.salesmanager.core.model.common.UserContext)1 Customer (com.salesmanager.core.model.customer.Customer)1 Order (com.salesmanager.core.model.order.Order)1 CreditCard (com.salesmanager.core.model.order.payment.CreditCard)1 Transaction (com.salesmanager.core.model.payments.Transaction)1 Country (com.salesmanager.core.model.reference.country.Country)1 Currency (com.salesmanager.core.model.reference.currency.Currency)1 Zone (com.salesmanager.core.model.reference.zone.Zone)1 ShoppingCartItem (com.salesmanager.core.model.shoppingcart.ShoppingCartItem)1 PersistableOrderProduct (com.salesmanager.shop.model.order.PersistableOrderProduct)1