View source | Discuss this page | Page history | Printable version   

Projects/Create Invoice Lines From refactor



The purpose of this document is to explain the functional and technical design followed in the refactoring of the "Create Lines From" process on invoice windows, to improve the performance and the user experience. This refactor has been included from the 3.0PR18Q3 product version in advance.

The "Create Lines From" process had some performance problems in environments with high volumes of information, as well as some functional limitations such as not being able to select order/shipment/receipts lines of different orders/shipments/receipts at a time and having to first selecting the document and then the lines.

To cover these deficiencies and provide a more intuitive and functional interaction, this process was migrated to a new processes called “Create Invoice Lines From Orders” and “Create Invoice Lines From Shipment/Receipt”. In this document, we will explain the main functional changes that were needed to fulfill these objectives and some technical topics to a better understanding of it.

This refactor cover some of these limitations explained on the design defect and includes other functionalities that we going to explain with more detail in this document.

Design changes.

The User Interface was affected by some changes. We can say the most important are:

Migration to a Pick and Edit window

The old process "Create Lines From" was defined as a manual process and does not make use of the benefits of a Process Definition and Pick and Edit windows. The first thing done was to migrate the current process logic to two different new Process Definition, using the Pick and Edit (PE) pattern on Sales and Purchase Invoice windows, and improves interaction with users.

Allow to select more than one line at a time

With the old process only was possible to select one order/shipment/receipt at a time and the select it lines, which increases the amount of user interactions to add lines of several documents. This time the new processes allows to select lines of different documents at once.

Separated the logic of creation from Orders and creation from Shipment/Receipts

With the old process, no matter the document type of lines the user wants to create from, the Create Lines From window load all the order and the Shipment/Receipts lines that can be invoiced. It implies an extra load if the user just want to create lines from a particular document type.

The Create Lines From button has been replaced by two different buttons in each window: Sales Invoice: “Create Lines From Order”, “Create Lines From Shipment” Purchase Invoice: “Create Lines From Order”, “Create Lines From Receipt”

It is splitted in two buttons in order to get a better user experience without mixing together orders and shipments/receipts and get a better performance experience as it is only launched a query (order or shipment/receipt) at a time.

The “Create Lines From Order” button will just show Sales/Purchase order lines (depending on the window from which it was launched) that are ready to be invoiced. It means there is no need to have a separate combo (as we have right now) to select first the order. Different lines from different orders can be selected at the same time [new behavior]. Only orders from the invoice’s business partner will be shown (as in the old process)

The “Create From Shipment”/“Create From Receipt” buttons will just show Goods Shipment/Receipt lines (depending on the window from which it was launched) that are ready to be invoiced. It means there is no need to have a separate combo (as in the old process) to select first the shipment/receipt. Different lines from different shipments/receipt can be selected at the same time [new behavior]. Only shipment/receipts from the invoice’s business partner will be shown (as right now)

Added more information of the copied lines and filtering support.

In the new P&Es were added other fields that give more information to the user and the possibility of filtering the selection. Example of new fields are:

Create Invoice Lines From Order P&E Filtering in Create Invoice Lines From Order P&E

Technical Design

The current refactor has as most important changes:

  1. The grids for P&E are based on a HQL tables with HQL insertion points.
  2. The existing SQL queries to get first the order/inout header and then the lines have been merged and optimized in just one HQL query (one for order lines, one for inout lines). Ensuring are showed the same records before/after the refactor and times to get the information hace been improved.
  3. The java code in charge of the business logic has been refactorized following the clean code conventions.
  4. The business logic includes hooks to be extended by external modules.
  5. The records in the P&E are ordered by default by document no descendental and line no ascendingly.

Rules followed when processing

The process copies information from the copied lines and generates its own information according the document header and environment settings. In this section, we going to do a summary of the most remarkable topics to take into account.

The lines are copied in the same order they were selected in the P&Es. The process adds as many lines as lines have been selected. In the case it is copying an order line partial or completely delivered, the process will create as many lines as shipment/receipts the order line has related to. The new invoice lines will have a reference to the order and/or the shipment/receipt lines they are related to and from which they were created.

Limit the records count by a date range

An important change introduced in this refactor is the possibility to filter the records when the P&Es are opened by a date range that can be configurable by a new preference. This preference is used by the process to limit transactions starting from the current date to the defined days ago. If the preference is not defined then queries retrieve all records created since one year ago (365 days), else it will be filtered by the count of days defined as preference value.


Important to remark is that:

You can define this preference in the same way than any other Client/Organization preference. Taking into account the transactions volume, periods and how frequently there are order/shipment/receipt pending to be invoiced is possible to create more than one preference with different settings. For example, if the organization have not to many invoiceable orders in six months, then administrator can consider limit the count of days to 180 instead of 365 (one year) for the Create Invoice Lines From Order window:


This setting included in the project improves the performance because avoid iterate for a high volume of information that probably will be unneded.

Technical Changes

This refactor generates several changes into the database and core java classes. In this section, we are going to do a brief explanation of the most important changes.

Java classes changes

Several changes were done in the java classes to implements the design requirements. In this section, we will explain the created artifacts and how to extend the core’s logic by hooks.

Classes were created to separate the action handler and the processing logic, to make more easiest the understanding of the code and make the processing independent of the flows where it is used, and making possible to be used in programmatic codes like automated tests.

CreateInvoiceLinesFromHandler class.

It was created as an abstract action handler class, that it is implemented by the new two processes (Create Lines From Order and Create Lines From Shipment/Receipt), but also brings support to any future development that would like to create invoice lines from other document types.

 * Abstract class to be implemented by any process that creates invoice lines from any Openbravo
 * BaseOBObject
 * @param <T>
 *          Invoice lines will be created from an object whose class extends from the BaseOBObject
abstract class CreateInvoiceLinesFromHandler<T extends BaseOBObject> extends BaseProcessActionHandler {
  private static final Logger log = LoggerFactory.getLogger(CreateInvoiceLinesFromHandler.class);
  protected abstract Class<T> getFromClass();
  protected JSONObject doExecute(Map<String, Object> parameters, String content) {
    try {
      JSONObject jsonRequest = new JSONObject(content);
      JSONArray selectedLines = CreateLinesFromUtil.getSelectedLines(jsonRequest);
      Invoice currentInvoice = CreateLinesFromUtil.getCurrentInvoice(jsonRequest);
      // CreateLinesFromProcess is instantiated using Weld so it can use Dependency Injection
      CreateInvoiceLinesFromProcess createLinesFromProcess = WeldUtils
      createLinesFromProcess.createInvoiceLinesFromDocumentLines(selectedLines, currentInvoice,
      jsonRequest.put(CreateLinesFromUtil.MESSAGE, CreateLinesFromUtil.getSuccessMessage());
      return jsonRequest;
    } catch (Exception e) {
      log.error("Error in CreateInvoiceLinesFrom Action Handler", e);
      return showExceptionInViewAndRetry(e);

It is important to remarks that this solution is using Weld and can be instantiated the CreateInvoiceLinesFromProcess class using dependency injection. In the next topics, we going to explain it and the java classes structure with more details.

CreateInvoiceLinesFromInOutLines and CreateInvoiceLinesFromOrderLines classes

We have two classes managing the creation from the Order Lines and from the Shipment/Receipts lines. They are CreateInvoiceLinesFromOrder and CreateInviceLinesFromInOut classes. As the logic executed by both process is generic and share common logic, all the logic s in the abstract class CreateInvoiceLinesFromHandler, and only is needed to specify the class of the objects that process will copy.

 * Implementation for Create Invoice Lines From InOut Lines
public class CreateInvoiceLinesFromInOutLines extends CreateInvoiceLinesFromHandler<ShipmentInOutLine> {
  protected Class<ShipmentInOutLine> getFromClass() {
    return ShipmentInOutLine.class;
 * Implementation for Create Invoice Lines From Order Lines
public class CreateInvoiceLinesFromOrderLines extends CreateInvoiceLinesFromHandler<OrderLine> {
  protected Class<OrderLine> getFromClass() {
    return OrderLine.class;

CreateInvoiceLinesFromProcess class

This class execute the processing of the selected lines. It provide support to extend the functionality by hooks, in fact almost all the logic is implemented as part of a hook.

As reference of how to implements a specific functionality in a hook, can be found as part of the classes included on Core as part of this project. For example, the process follows the following steps:

  1. Update Invoice and Invoice Line related information (implemented on UpdateInvoiceLineInformation.class): This hook Updates the Information of the new Invoice Line that is related with the Invoice Header and the copied order line. It is in charge of:
  1. Copy product and attributes (implemented on UpdateProductAndAttributes.class): This hook copies the product of the selected line in the P&E and it attributes to the new invoice line.
  2. Calculate amounts and UOM's. (implemented on UpdateQuantitiesAndUOMs.class): This hook manage the amounts and gives support to alternatives unit of measures (AUM).
  3. Calculate Prices based on price list (implemented on UpdatePricesAndAmounts.class): Updates prices and amounts. If the copied line is related with an order line then copy exactly the same prices from the original line. If the copied line is unrelated with an order line then if the product has a product price in the invoice price list then all prices and amounts will be recalculated using currency precisions and taking into account if the price list includes taxes or not.
  4. Calculate Acc and Def Plan from Product (implemented on UpdateAccAndDefPlanFromProduct.class): This hook updates the accounts and definition plans from product. It includes the plan type, period number, starting period and if the invoice line will be marked as deferred or not.
  5. Recalculate Taxes (implemented on UpdateTax.class): This hook is in cahrge of search the correct line tax, taxable amount and tax amount of the new invoice line.

How to extend Core’s functionality with hooks.

If you want to extend the core implementation included on this refactor with your own logic, you must take into account the following.

Add the following annotations to the class:


The class must implement the CreateLinesFromProcessHook abstract class:

class NewHook implements CreateLinesFromProcessHook{

Taking as example the UpdateTax hook implementation:

class UpdateTax extends CreateLinesFromProcessHook {
  private static final Logger log = LoggerFactory.getLogger(UpdateTax.class);
  public int getOrder() {
    return -10;
   * Update order line tax, taxable amount and tax amount. Throws an exception if no taxes are
   * found.
  public void exec() {

When a new hook is implemented you need to override the following two methods:

Example of a hook creation.

According to the explained above, if we want to implement a hook, for instance that updates the new invoice line description with the “It is a test” string, and it is needed to be executed after the core actions and between other customized hooks (some with defined execution order less and greater than 10), a possible implementation of it could be:

public class TestHook implements CreateLinesFromProcessHook{
    public int getOrder() {
      return 10;
    public void exec() {
      getInvoiceLine().setDescription("It is a test");

In this code it is enough to fulfill the requirements of our example, but it can be as complex as the logic you want to execute.

Database changes

Important changes were done in the database to implement the design requirements. In this section, we are going to explain the created artifacts and how to easily extend the information in the P&Es with customized fields.

New HQL tables used in P&Es

Were created two new HQL tables in the AD to get the P&E records:

As these tables are used in Sales windows as well as Purchase windows, they are defined in a generic way, and they are transformed according the window they are executed (Sales Invoice or Purchase Invoice). Taking the C_OrderLine_PE_HQL HQL Query as example, we are going to explain how it works and how user can extend this to add more fields to the P&E. We going to start viewing the HQL Query definition:

  @orderedQuantity@ AS orderedQuantity,
  (SELECT FROM Product p WHERE = AS product,
  (o.documentNo || ' - ' || to_char(trunc(o.orderDate)) || ' - ' || o.grandTotalAmount) AS salesOrder,  
  @operativeQuantity@ AS operativeQuantity,
  @operativeUOM@ AS operativeUOM,
  @orderQuantity@ AS orderQuantity,,
  e.orderDate AS orderDate,
  o.scheduledDeliveryDate AS scheduledDeliveryDate, 
  (SELECT FROM Warehouse wh WHERE = AS warehouse,  
  o.documentNo AS documentNo,
  o.grandTotalAmount AS grandTotalAmount

As you can see, there are some elements in the query that are strings surrounded by @@. These strings represent part of our query that we can replace with more specific clauses depending on determinated conditions.

Of course this query will fail if we execute it with these undefined elements, but in this case we can use a HqlQueryTransformer, to transform the query according the scenario it is executed. To transform the C_OrderLine_PE_HQL we have the, and for M_InOutLine_PE_HQL we have

Overriding the transformHqlQuery method we make possible to change any part of the query with more convenient code. This is the case of the transformer for the Create Lines From Order:

  public String transformHqlQuery(String _hqlQuery, Map<String, String> requestParameters,
      Map<String, Object> queryNamedParameters) {
    isSalesTransaction = StringUtils.equals(requestParameters.get("@Invoice.salesTransaction@"),
    final String strInvoicePriceListId = requestParameters.get("@Invoice.priceList@");
    final PriceList priceList = OBDal.getInstance().get(PriceList.class, strInvoicePriceListId);
    final String strBusinessPartnerId = requestParameters.get("@Invoice.businessPartner@");
    final String strCurrencyId = requestParameters.get("@Invoice.currency@");
    final String strInvoiceId = requestParameters.get("");
    queryNamedParameters.put("issotrx", isSalesTransaction);
    queryNamedParameters.put("bp", strBusinessPartnerId);
    queryNamedParameters.put("plIncTax", priceList.isPriceIncludesTax());
    queryNamedParameters.put("cur", strCurrencyId);
    if (!isSalesTransaction) {
      queryNamedParameters.put("invId", strInvoiceId);
    String transformedHql = _hqlQuery.replace("@selectClause@", getSelectClauseHQL());
    transformedHql = transformedHql.replace("@fromClause@", getFromClauseHQL());
    transformedHql = transformedHql.replace("@whereClause@", getWhereClauseHQL());
    transformedHql = transformedHql.replace("@groupByClause@", getGroupByHQL());
    transformedHql = transformedHql.replace("@orderedQuantity@", getOrderedQuantityHQL());
    transformedHql = transformedHql.replace("@operativeQuantity@", getOperativeQuantityHQL());
    transformedHql = transformedHql.replace("@orderQuantity@", getOrderQuantityHQL());
    transformedHql = transformedHql.replace("@operativeUOM@", getOperativeUOM());
    transformedHql = transformedHql.replace("@filterByDocumentsProcessedSinceNDaysAgo@",
    transformedHql = changeAdditionalFiltersIfIsSalesTransaction(transformedHql);
    return transformedHql;

For example, the logic of how the ordered quantity is calculated taking into account if it is executed from a Sales or a Purchase Invoice is defined by this way:

protected String getOrderedQuantityHQL() {
    StringBuilder orderedQuantityHql = new StringBuilder();
    if (isSalesTransaction) {
      orderedQuantityHql.append(" e.orderedQuantity-COALESCE(e.invoicedQuantity,0)");
    } else {
          .append(" e.orderedQuantity-SUM(COALESCE(m.quantity, 0))-SUM(COALESCE(il.invoicedQuantity, 0))");
    return orderedQuantityHql.toString();

It can be as complex as you need, for example in the refactor if we are creating from a purchase invoice, the ordered quantity will not include the already invoiced quantities in the processing invoice.

How to add customized fields to the P&Es.

1. To add more customized fields is important to start createing a new transformer extending the existing ones: for shipment/receipt lines and when you want to extend the Create Lines From Order P&E.

With a transformer defined as follow it is possible to add the UOM symbol to the fields retrieved by our query:

import java.util.Map;
import org.openbravo.client.kernel.ComponentProvider;
 * Example of how to extend the current InOutLinePEHQLTransformer
public class NewCustomizedTransformer extends InOutLinePEHQLTransformer {
  private static final String UOM_SYMBOL = "uom.symbol";
   * You need to define a priority greater than 100 to be executed after the
   * InOutLinePEHQLTransformer or less to be executed before. In our case we going to execute the
   * logic previously.
  public int getPriority(Map<String, String> parameters) {
    return 90;
   * Build a new Select clause using the base one and adding the uom.symbol field
  protected String getSelectClauseHQL() {
    String baseSelectClause = super.getSelectClauseHQL();
    StringBuffer newSelectClause = new StringBuffer();
    return newSelectClause.toString();
   * Build a new Group By clause using the base one and adding the uom.symbol field
  protected String getGroupByHQL() {
    String baseGroupBy = super.getGroupByHQL();
    StringBuffer newGroupBy = new StringBuffer();
    return newGroupBy.toString();

Inside the transformer we can add relations with other tables, conditions and grouping to adapt the new query to our new requirements. In our example is just needed to add the uom.symbol to the select clause and the group by.

2. Define a new Column to the table: You should add a new column to the table to link this new field.

3. Add a new field in the corresponding P&E related with the created column.

And that's all. This way you can customize your P&E to show/hide the information you want.

Retrieved from ""

This page has been accessed 2,214 times. This page was last modified on 25 June 2018, at 21:37. Content is available under Creative Commons Attribution-ShareAlike 2.5 Spain License.