Use parameters with expressions

Printing parameters in ReportBro is simple but what if you need to manipulate parameters or depend printing on the result of an evaluation of parameters? That's where you use expressions. ReportBro supports expressions on different levels of layout template design: Evaluating parameters, setting print conditions and applying conditional styling.

In this tutorial you will learn to use expressions to:
Evaluate parameters
Perform calculations
Apply conditional styling
Conditional printing

1 Evaluate parameters

Let's follow up on the example of the tutorial on creating tables. We extend the shopping list parameter to a product order.

Expressions in ReportBro are Python statements and can contain parameters. We use the parameter shippable to append a note to the product for its availability:

${product} + ' (available)' if ${shippable} else ${product}

However, if we preview the printed report we notice that the expression is printed as text while only the parameter values are replaced. What's missing here is the activated checkbox Evaluate to make sure the text is correctly interpreted.

Finally, we set another expression to print the delivery date; 3 days from today if the product is in stock (shippable) and 14 days if it's not:

${today} + datetime.timedelta(days=3) if ${shippable}
    else ${today} + datetime.timedelta(days=14)

2 Perform calculations

Parameters used for expressions consider their respective type (e.g. a parameter text represents a string in Python, a parameter number represents a decimal.Decimal in Python).

Let's take a look at our parameters. We'd like to calculate the gross amount based on the parameter total_amount, which is a sum of ${product_order.price}

To achieve that we can set an evaluation on the the parameter total_amount_w_tax. Once the checkbox is activated we can add the expression to add 20% to our net amount:

${total_amount} * 1.2

This will, however, not work and produce an error when we try to print it. Because parameters of type Number are represented in Python with decimal.Decimal we cannot use them in operations together with floats. Instead we rather use:

${total_amount} * decimal('1.2')

3 Apply conditional styling

Expressions are useful for styling decisions. In our example we take the parameter shippable to visualize the product's availability. Let's assume that the standard case is an available product and we want to set a different style otherwise.

Click the respective element (in our case it's the table cell) and set a green background color to define the standard case. Now open the conditional style section in the detail panel. Here we can set both the condition and the styling to be applied:
not ${shippable} as the condition and a red background color

4 Conditional printing

One of the most important and powerful features in ReportBro is the ability to define Print if conditions. This allows optimizing a report template.

We can also use the Print if condition to hide a whole table column. Condition-based table column printing is a complete tutorial for that use case.

In our example we use len(${product_order}) > 10 to print a note for free delivery because of the amount of ordered items.

We can also use the print if capabilities to make more complex evaluations, e.g. to offer free delivery only in case of a minimum total amount in combination with either enough items or total quantity:

${total_amount} > 50 and
    (len(${product_order}) >= 15 or ${total_quantity} > 10)

Bro tips for expressions

Use brackets if you evaluate more than 2 terms to avoid unexpected evaluation precedence
a and (b or c) instead of a and b or c
Always pass a string parameter to decimal function to avoid possible rounding issues with floats
${Price} * decimal('2.5')
instead of
${Price} * decimal(2.5)

Try it yourself: Download the report of this tutorial
Read here how to import the report into ReportBro Designer.

Want to know more about expressions?
Read the user guide for expression syntax
For more detailed information about writing Python statements, please refer to https://docs.python.org/3/reference/simple_stmts.html.

By the way, ReportBro is also available for everyone as open-source on github (see Download page). Star us on github if you like what you see!