Default Ordeline Item composition relationship

Hi

sorry I am new to CUBA and trying out these two scenarios but not sure how to proceed ?

scenario 1:

I have a composition relationship on order and orderline tables.
now when creating an order I want  orderline to have a default orderline item 
populated all times. how do I go about that ?

scenario 2

I also have composition relationship on order and orderline tables
based on customer  selected on order, I want order to have certain default values and also orderline
to have default line items.

I can successfully set defaults on the order table when selecting different customer by
subscribing to FieldValueChange event, but not sure how to go to orderline and set
default orderline item?

sorry I am new to CUBA and don’t have sensible example code to share so any guidance and example code based on above will be appreciated very much

Thanks
Bothwell

Hi @bothwellw and welcome to the Community,

I have created an example based on the ordermanagement example application, that you can find here: GitHub - mariodavid/rtcab-ordermanagement: Ordermanagement system done with CUBA platform

In particular this PR will activate your described behavior:

Here’s the explanation of it:

Customer aware Default Products

Based on the Customer or CustomerType, certain Line Items should be created automatically.

For this, there is a dedicated entity, called DefaultProduct which has a CRUD UI:
Bildschirmfoto 2019-10-05 um 12 02 44

It is possible to create a default product for a particular customer, for a customer type, or a global default product (leaving customer and customer type blank).

Creating a new Order with default products

The Order Screen will pick up the default products and create line items out of it.
Additionally, based on the Customer Type, the Order Priority will be determined.

Example 1: special Customer

Bildschirmfoto 2019-10-05 um 12 02 53
Bildschirmfoto 2019-10-05 um 12 03 10
Bildschirmfoto 2019-10-05 um 12 03 26

Example 2: no special Customer

Bildschirmfoto 2019-10-05 um 12 03 38
Bildschirmfoto 2019-10-05 um 12 03 45

Source code

The Order with Defaults controller, generates line items out of the default products that match the current context (taking customer & customer type into consideration).

OrderWithDefaultsEdit.java:

@Subscribe
    protected void onAfterShow(AfterShowEvent event) {
        defaultProductsDc.getItems()
                .stream()
                .filter(this::isValidDefaultProductInThisContext)
                .forEach(defaultProduct -> lineItemsDc
                        .getMutableItems()
                        .add(
                                createLineItemFromDefaultProduct(defaultProduct)
                        )
                );
    }

    private boolean isValidDefaultProductInThisContext(DefaultProduct defaultProduct) {
        if (defaultProduct.globalDefaultProduct()) {
            return true;
        }
        else if (isDefaultProductForCurrentCustomer(defaultProduct)) {
            return true;
        }
        else if (isDefaultProductForCustomerType(defaultProduct)) {
            return true;
        }
        else {
            return false;
        }
    }

    private boolean isDefaultProductForCustomerType(DefaultProduct defaultProduct) {
        return getEditedEntity().getCustomer().getType() != null && defaultProduct.getCustomerType().equals(getEditedEntity().getCustomer().getType());
    }

    private boolean isDefaultProductForCurrentCustomer(DefaultProduct defaultProduct) {
        return defaultProduct.getCustomer() != null && defaultProduct.getCustomer().equals(getEditedEntity().getCustomer());
    }

    private LineItem createLineItemFromDefaultProduct(DefaultProduct defaultProduct) {
        LineItem lineItem = dataContext.create(LineItem.class);
        lineItem.setOrder(getEditedEntity());
        lineItem.setProduct(defaultProduct.getProduct());
        lineItem.setQuantity(1);
        lineItem.setPrice(new BigDecimal("100.0"));
        return lineItem;
    }

The complete example can be found on Github or directly downloaded here: rtcab-ordermanagement.zip (283.0 KB)

Bye
Mario

2 Likes

Many thanks @mario for your time spent to provide a complete answer.

your github rtcab-ordermanagement repo and the accompanying vedio really help a lot to show how to work with CUBA.

Regards
Bothwell