InvenTree icon indicating copy to clipboard operation
InvenTree copied to clipboard

[Report] Generate single report against selected items

Open SchrodingersGat opened this issue 9 months ago • 9 comments

Discussed in https://github.com/inventree/InvenTree/discussions/9408

Originally posted by tristanle22 March 30, 2025 I want to select a few stock items and generate a report. In the report, I want to have a table, with each row for each stock item. I can't find a context variable that represents a list of selected stock items.

I'm aware of the built-in "Inventree Test Report" generates a new page for each selected item.

Feature Description

Instead of generating a separate report for each individually selected item (e.g. stock item / order / part), add an option to pass all selected items through to the report context, and then generate a single report from that.

Implementation Ideas

  • Add a "merge" (or similar named) field to the "ReportTemplate" model
  • This allows the report template creator to decide whether the report is generated against a single output, or against all selected outputs

The ReportTemplate model has an existing print method which could be overloaded:

https://github.com/inventree/InvenTree/blob/66d5180d8fa1542891de848655fc77e7ce28ad82/src/backend/InvenTree/report/models.py#L351

Simply replace this iterator:

https://github.com/inventree/InvenTree/blob/66d5180d8fa1542891de848655fc77e7ce28ad82/src/backend/InvenTree/report/models.py#L403

SchrodingersGat avatar Mar 31 '25 08:03 SchrodingersGat

@tristanle22 FYI

SchrodingersGat avatar Mar 31 '25 08:03 SchrodingersGat

I'm working on this feature. Just have a dump question as I'm not very familiar with the matter :). Currently I'm adding the "merge" field as a Boolean. If the user want to set this field (or any other field) to True in the report template, how would they do it?

tristanle22 avatar Apr 03 '25 01:04 tristanle22

@tristanle22 great question. Here's a rough sequence of required steps:

  1. Add new field to ReportTemplate model

https://github.com/inventree/InvenTree/blob/67bdf3162afed8280df3190a6eab9918c1d4a97b/src/backend/InvenTree/report/models.py#L330

  1. Run database migrations (to actually add the new field)

invoke migrate

  1. Expose new field to the API

Add the new field name to the Meta.fields attribute of the ReportTemplateSerializer class

https://github.com/inventree/InvenTree/blob/67bdf3162afed8280df3190a6eab9918c1d4a97b/src/backend/InvenTree/report/serializers.py#L61

This will ensure that the new field is exposed to the API

  1. Add the new field to the frontend form

Add your new field name to the additionalFormFields attribute of the ReportTemplateTable component:

https://github.com/inventree/InvenTree/blob/67bdf3162afed8280df3190a6eab9918c1d4a97b/src/frontend/src/pages/Index/Settings/AdminCenter/ReportTemplatePanel.tsx#L7

That should be sufficient to allow the user to edit this new field. LMK how you go!

SchrodingersGat avatar Apr 03 '25 01:04 SchrodingersGat

Thanks @SchrodingersGat for the instructions! I have followed your steps above. After making the changes, I ran debug with vscode, however, the new field doesn't show up. Is anything else I need to do before running?

From a quick search, this autogenerated field is missing the new field: src/backend/InvenTree/report/migrations/0022_reporttemplate.py. Not sure if it has any signficance.

tristanle22 avatar Apr 03 '25 02:04 tristanle22

I ran debug with vscode

In vscode you'll need two processes running:

Backend Server

invoke dev.server

Frontend Dev Server (with auto-reload)

invoke dev.frontend-server

And then access at http://localhost:5173

SchrodingersGat avatar Apr 03 '25 02:04 SchrodingersGat

Ah I see. Thanks for your help! I can see the changes in the front end now.

tristanle22 avatar Apr 06 '25 16:04 tristanle22

After running dev.setup-test to load the sample dataset, I was able to login to http://localhost:8000 with user 'admin' and pw 'inventree'. However, the same credentials didn't work on http://localhost:5173. Do you have any idea why? @SchrodingersGat

tristanle22 avatar Apr 08 '25 01:04 tristanle22

Probably due to CORS issues - have a look at the network response in your browser when you try to login.

By default the devcontainer setup should work seamlessly with the localhost:5173 - have you done anything "non-standard" in your setup?

SchrodingersGat avatar Apr 08 '25 02:04 SchrodingersGat

No I didn't do anything "non-standard", beside having to manually install psycopg since it was missing from src/backend/requirements.txt. After restarting the devcontainer docker image, I was able to login to localhost:5173.

tristanle22 avatar Apr 09 '25 00:04 tristanle22