Program: 58929
Project: 58929
Buggy source:
@Override public void process() throws AxelorException {
  super.process();
  if (invoice.getOperationTypeSelect() == InvoiceRepository.OPERATION_TYPE_CLIENT_SALE) {
    if (invoice.getSaleOrder() == null) {
      SaleOrder currentSaleOrder=null;
      List<SaleOrder> saleOrderList=new ArrayList<SaleOrder>();
      for (      InvoiceLine invoiceLine : invoice.getInvoiceLineList()) {
        if (currentSaleOrder == null || !currentSaleOrder.equals(invoiceLine.getSaleOrderLine().getSaleOrder())) {
          saleOrderList.add(invoiceLine.getSaleOrderLine().getSaleOrder());
          currentSaleOrder=invoiceLine.getSaleOrderLine().getSaleOrder();
        }
        BigDecimal invoicedAmountToAdd=invoiceLine.getExTaxTotal();
        if (!invoice.getCurrency().equals(invoiceLine.getSaleOrderLine().getSaleOrder().getCurrency())) {
          BigDecimal currentCompanyInvoicedAmount=invoiceLine.getCompanyExTaxTotal();
          BigDecimal rate=currentCompanyInvoicedAmount.divide(invoiceLine.getSaleOrderLine().getCompanyExTaxTotal(),4,RoundingMode.HALF_UP);
          invoicedAmountToAdd=rate.multiply(invoiceLine.getSaleOrderLine().getExTaxTotal());
        }
        invoiceLine.getSaleOrderLine().setAmountInvoiced(invoiceLine.getSaleOrderLine().getAmountInvoiced().add(invoicedAmountToAdd));
        JPA.save(invoiceLine.getSaleOrderLine());
      }
      for (      SaleOrder saleOrder : saleOrderList) {
        saleOrder.setAmountInvoiced(saleOrderInvoiceService.getAmountInvoiced(saleOrder,invoice.getId(),true));
        JPA.save(saleOrder);
      }
    }
 else {
      invoice.getSaleOrder().setAmountInvoiced(saleOrderInvoiceService.getAmountInvoiced(invoice.getSaleOrder(),invoice.getId(),true));
    }
  }
 else   if (invoice.getOperationTypeSelect() == InvoiceRepository.OPERATION_TYPE_SUPPLIER_PURCHASE) {
    if (invoice.getPurchaseOrder() == null) {
      PurchaseOrder currentPurchaseOrder=null;
      List<PurchaseOrder> purchaseOrderList=new ArrayList<PurchaseOrder>();
      for (      InvoiceLine invoiceLine : invoice.getInvoiceLineList()) {
        if (currentPurchaseOrder == null || !currentPurchaseOrder.equals(invoiceLine.getPurchaseOrderLine().getPurchaseOrder())) {
          purchaseOrderList.add(invoiceLine.getPurchaseOrderLine().getPurchaseOrder());
          currentPurchaseOrder=invoiceLine.getPurchaseOrderLine().getPurchaseOrder();
        }
        BigDecimal invoicedAmountToAdd=invoiceLine.getExTaxTotal();
        if (!invoice.getCurrency().equals(invoiceLine.getPurchaseOrderLine().getPurchaseOrder().getCurrency())) {
          BigDecimal currentCompanyInvoicedAmount=invoiceLine.getCompanyExTaxTotal();
          BigDecimal rate=currentCompanyInvoicedAmount.divide(invoiceLine.getPurchaseOrderLine().getCompanyExTaxTotal(),4,RoundingMode.HALF_UP);
          invoicedAmountToAdd=rate.multiply(invoiceLine.getPurchaseOrderLine().getExTaxTotal());
        }
        invoiceLine.getPurchaseOrderLine().setAmountInvoiced(invoiceLine.getPurchaseOrderLine().getAmountInvoiced().add(invoicedAmountToAdd));
        JPA.save(invoiceLine.getPurchaseOrderLine());
      }
      for (      PurchaseOrder purchaseOrder : purchaseOrderList) {
        purchaseOrder.setAmountInvoiced(purchaseOrderInvoiceService.getAmountInvoiced(purchaseOrder,invoice.getId(),true));
        JPA.save(purchaseOrder);
      }
    }
 else {
      invoice.getPurchaseOrder().setAmountInvoiced(purchaseOrderInvoiceService.getAmountInvoiced(invoice.getPurchaseOrder(),invoice.getId(),true));
    }
  }
}

Fixed version (ground truth):
@Override public void process() throws AxelorException {
  super.process();
  if (invoice.getOperationTypeSelect() == InvoiceRepository.OPERATION_TYPE_CLIENT_SALE) {
    if (invoice.getSaleOrder() == null) {
      SaleOrder currentSaleOrder=null;
      List<SaleOrder> saleOrderList=new ArrayList<SaleOrder>();
      for (      InvoiceLine invoiceLine : invoice.getInvoiceLineList()) {
        if (invoiceLine.getSaleOrderLine() != null) {
          if (currentSaleOrder == null || !currentSaleOrder.equals(invoiceLine.getSaleOrderLine().getSaleOrder())) {
            saleOrderList.add(invoiceLine.getSaleOrderLine().getSaleOrder());
            currentSaleOrder=invoiceLine.getSaleOrderLine().getSaleOrder();
          }
          BigDecimal invoicedAmountToAdd=invoiceLine.getExTaxTotal();
          if (!invoice.getCurrency().equals(invoiceLine.getSaleOrderLine().getSaleOrder().getCurrency())) {
            BigDecimal currentCompanyInvoicedAmount=invoiceLine.getCompanyExTaxTotal();
            BigDecimal rate=currentCompanyInvoicedAmount.divide(invoiceLine.getSaleOrderLine().getCompanyExTaxTotal(),4,RoundingMode.HALF_UP);
            invoicedAmountToAdd=rate.multiply(invoiceLine.getSaleOrderLine().getExTaxTotal());
          }
          invoiceLine.getSaleOrderLine().setAmountInvoiced(invoiceLine.getSaleOrderLine().getAmountInvoiced().add(invoicedAmountToAdd));
          JPA.save(invoiceLine.getSaleOrderLine());
        }
      }
      for (      SaleOrder saleOrder : saleOrderList) {
        saleOrder.setAmountInvoiced(saleOrderInvoiceService.getAmountInvoiced(saleOrder,invoice.getId(),true));
        JPA.save(saleOrder);
      }
    }
 else {
      invoice.getSaleOrder().setAmountInvoiced(saleOrderInvoiceService.getAmountInvoiced(invoice.getSaleOrder(),invoice.getId(),true));
    }
  }
 else   if (invoice.getOperationTypeSelect() == InvoiceRepository.OPERATION_TYPE_SUPPLIER_PURCHASE) {
    if (invoice.getPurchaseOrder() == null) {
      PurchaseOrder currentPurchaseOrder=null;
      List<PurchaseOrder> purchaseOrderList=new ArrayList<PurchaseOrder>();
      for (      InvoiceLine invoiceLine : invoice.getInvoiceLineList()) {
        if (currentPurchaseOrder == null || !currentPurchaseOrder.equals(invoiceLine.getPurchaseOrderLine().getPurchaseOrder())) {
          purchaseOrderList.add(invoiceLine.getPurchaseOrderLine().getPurchaseOrder());
          currentPurchaseOrder=invoiceLine.getPurchaseOrderLine().getPurchaseOrder();
        }
        BigDecimal invoicedAmountToAdd=invoiceLine.getExTaxTotal();
        if (!invoice.getCurrency().equals(invoiceLine.getPurchaseOrderLine().getPurchaseOrder().getCurrency())) {
          BigDecimal currentCompanyInvoicedAmount=invoiceLine.getCompanyExTaxTotal();
          BigDecimal rate=currentCompanyInvoicedAmount.divide(invoiceLine.getPurchaseOrderLine().getCompanyExTaxTotal(),4,RoundingMode.HALF_UP);
          invoicedAmountToAdd=rate.multiply(invoiceLine.getPurchaseOrderLine().getExTaxTotal());
        }
        invoiceLine.getPurchaseOrderLine().setAmountInvoiced(invoiceLine.getPurchaseOrderLine().getAmountInvoiced().add(invoicedAmountToAdd));
        JPA.save(invoiceLine.getPurchaseOrderLine());
      }
      for (      PurchaseOrder purchaseOrder : purchaseOrderList) {
        purchaseOrder.setAmountInvoiced(purchaseOrderInvoiceService.getAmountInvoiced(purchaseOrder,invoice.getId(),true));
        JPA.save(purchaseOrder);
      }
    }
 else {
      invoice.getPurchaseOrder().setAmountInvoiced(purchaseOrderInvoiceService.getAmountInvoiced(invoice.getPurchaseOrder(),invoice.getId(),true));
    }
  }
}