Multipage invoice reports

Hey guys, I hope everybody is doing great.

Is there a way in Tryton ERP to create a subtotal line if the report will have more than one page?

Let’s say I have an invoice with 20 lines in it. So instead of just printing everything in several pages, I need a way to insert a subtotal line when the page changes, as it appears in images below.

Thank you so much for all your help.

There are a few possible solutions which I didn’t test:

  1. Use jasper reports with a specific Tryton module (https://github.com/trytonspain/trytond-jasper_reports)
  2. Use Latex markup with some extension like LongTable
  3. Use WeasyPrint

As I said, I haven’t test any of those, but personally I would looking into the Latex option because it’s much simpler then adding a new reportengine. WeasyPrint is also a really nice solution as well because you can define everything in HTML and CSS. But there some issues there and at the moment they are rewriting almost the whole engine.

If you want complete control and make much more complex reports then Jasper is the way to go.

Such feature would break the flexibility of the document size. But also would require to put business logic (like summing correctly money) in the template.
I would advice to not try to do such thing, it is for sure going to have tones of problems for no real benefit.

To calculate and render page totals as a last entry on a page, the word processor must be able to give the user some information about the page ending. This isn’t trivial to solve, because word-processors like LibreOffice Writer and Latex are working on floating texts and not page-centric like DTP programs. Maybe Calc could give a little bit more page control abilities. But when wanting a page-centric-layout I would not vote for a spread sheet program…
In LaTex I didn’t found any solution. But in LibreOffice Writer there was one, which always looked like a fragile hack. But it worked many years ago and I don’t know if it works today, as we don’t use this feature.

The Tryton invoice line needs a field, which calculates the running total.
In the odt template you’ll need to add an extra column, where you map a LibreOffice variable [sic!] to the running total of each line. To show the page total, you need to print the LibreOffice variable with the running total in the footer.

It works, because in each table row the LibreOffice variable is re-defined with the actual running total of the invoice line. Showing this variable in the footer, shows the value of the last defined running total on the page. The same way you can set a “carry over” in the header of the next page.

1 Like

I did a bit of research, digged in and learned some Latex :slight_smile: and made a proof-of-concept.
What I did was creating a table with the longtable package. Using this package you will be able to add a main header and footer and a page header and footer. The first ones are only added at the start and the end of the table, the seconds ones are added on each page if the table spans more then one page.

Using the output macro of the table, you will get the ‘running row’. If that row is a header or footer you can then move in your subtotals. The counting is done in a special counter which is triggered by each row. It will never be set to zero, so you have a ‘running total’.
In this case the main header and footer is not used or needed because this is only about the subtotals.

ONLY the amount is counted to display the subtotals. All the other data and totals should come from Tryton. Below a screenshot how this looks like.

The tex-template is a bit of a mess at the moment. If anybody is interested, please let me know so I can clean it up, comment it and make it work with Tryton as a report. Basically ALL the data is coming from Tryton, only the subtotals are in the template counted so no business logic is needed for this. Maybe there can be a rounding error but not tested yet.

Interesting use of LaTeX :smiley: :+1: (I wonder if it’s not doable with LibreOffice).

But as said @ced, you will run into problem if there is more than one currency used.

Not really actually. You just have to add another counter so one counter for Euro and one for Dollar. Then on each row you have to decide (just if-then-else) which amount goes to which counter. In the code for the header and footer you add the values of both counters (find the dollars :wink: ).

The counters are nothing more then a counter function in Latex and nothing special. Latex uses lots of counters to count pages, headers, sections, images, references etc.

Each row of data sends it’s amount to the counter(s). In my case I also let the counter add the value to the row so if you want you can display both currencies alongside each other. But each value can be styled differently.

Screenshot from 2021-03-10 20-23-45

To be honest, this is just a file (screenshots taken from the rendered PDF), the actual data with the amounts are not added dynamically from Tryton yet.

I’m starting to like Latex more and more and I am thinking to move all my reports to Latex. Hopefully I can get rid of LibreOffice to be installed on my headless server. The Latex templates are way more clean then the fodt templates.
I also did some research if such thing is possible in LibreOffice, but couldn’t find anything. For my Latex file I used longtable - How to print subtotals in each page of a long table? - TeX - LaTeX Stack Exchange as a reference and made some small changes to get it the shape it is now.

I had not been using latex for some time but I remember using it at my University times and it provides great layouts. Just for curiosity, did you need to tweak something of the tryton server to run latex templates or it works out of the box? If you needed to tweak something, it is something simple that we can add on the trytond package by default?

This is just a plain tex file which I compile with xelatex to a PDF because I use a non standard font and pdflatex won’t compile that. So no Tryton at the moment, but I surely will.

But won’t relatorio pick this up? Or is it better to use the same flow as with HTML and WeasyPrint?

8 posts were split to a new topic: Using LaTeX with Tryton