diff --git a/docs/docs/plugins/mixins/report.md b/docs/docs/plugins/mixins/report.md index 09e6e9d89a..d424ce7155 100644 --- a/docs/docs/plugins/mixins/report.md +++ b/docs/docs/plugins/mixins/report.md @@ -4,15 +4,57 @@ title: Report Mixin ## ReportMixin -The `ReportMixin` class provides a plugin with the ability to extend the functionality of custom [report templates](../../report/report.md). A plugin which implements the ReportMixin mixin class can add custom context data to a report template for rendering. +The `ReportMixin` class provides a plugin with the ability to extend the functionality of custom [report templates](../../report/report.md). A plugin which implements the ReportMixin mixin class can add custom context data to a report template for rendering, and can also receive a callback when a report is generated. ### Add Report Context A plugin which implements the ReportMixin mixin can define the `add_report_context` method, allowing custom context data to be added to a report template at time of printing. +This method is called each time a report is generated, and is passed the following arguments: + +| Argument | Description | +| --- | --- | +| `report_instance` | The report template instance which is being rendered | +| `model_instance` | The model instance against which the report is being generated | +| `user` | The user who initiated the report generation | +| `context` | The context dictionary, which can be modified in-place | + +Any data added to the provided `context` dictionary is made available to the report template, and can be rendered using standard django template syntax: + +```python +def add_report_context(self, report_instance, model_instance, user, context): + """Add extra context data to the report template.""" + context['my_custom_data'] = self.calculate_custom_data(model_instance) +``` + ### Add Label Context -Additionally the `add_label_context` method, allowing custom context data to be added to a label template at time of printing. +Similarly, the `add_label_context` method allows custom context data to be added to a label template at time of printing: + +| Argument | Description | +| --- | --- | +| `label_instance` | The label template instance which is being rendered | +| `model_instance` | The model instance against which the label is being generated | +| `user` | The user who initiated the label generation | +| `context` | The context dictionary, which can be modified in-place | + +### Report Callback + +The `report_callback` method is called after a report has been generated, and allows the plugin to perform custom actions with the generated report - for example, forwarding the report to an external system, or performing custom post-processing. + +| Argument | Description | +| --- | --- | +| `template` | The report template instance which was used to generate the report | +| `instance` | The model instance against which the report was generated | +| `report` | The generated report (PDF file data) | +| `user` | The user who initiated the report generation | + +```python +def report_callback(self, template, instance, report, user, **kwargs): + """Custom callback function - called after a report is generated.""" + # For example, forward the generated report to an external service + self.upload_to_external_service(report) +``` ### Sample Plugin diff --git a/docs/docs/report/assets.md b/docs/docs/report/assets.md new file mode 100644 index 0000000000..176209634a --- /dev/null +++ b/docs/docs/report/assets.md @@ -0,0 +1,45 @@ +--- +title: Report Assets +--- + +## Report Assets + +Users can upload asset files (e.g. images) which can be used when generating reports. For example, you may wish to generate a report with your company logo in the header. + +Asset files are managed from the [Admin Center](../settings/admin.md#admin-center), via the *Report Assets* panel. Staff users can upload new asset files, and remove assets which are no longer required. + +Asset files can be rendered directly into the template as follows + +```html +{% raw %} + +{% load report %} + + + + + + + + + + + + + + + +{% endraw %} +``` + +!!! warning "Asset Naming" + If the requested asset name does not match the name of an uploaded asset, the template will continue without loading the image. + +!!! info "Assets location" + Upload new assets via the *Report Assets* panel in the [Admin Center](../settings/admin.md#admin-center) to ensure they are uploaded to the correct location on the server. + +There are various [helper functions](./helpers.md#report-assets) available to assist with embedding assets into templates. diff --git a/docs/docs/report/context_variables.md b/docs/docs/report/context_variables.md index e37891b711..7fc365fda1 100644 --- a/docs/docs/report/context_variables.md +++ b/docs/docs/report/context_variables.md @@ -69,12 +69,14 @@ Templates (whether for generating [reports](./report.md) or [labels](./labels.md | Model Type | Description | | --- | --- | -| company | A Company instance | +| [company](#company) | A Company instance | | [build](#build-order) | A [Build Order](../manufacturing/build.md) instance | | [buildline](#build-line) | A [Build Order Line Item](../manufacturing/build.md) instance | | [salesorder](#sales-order) | A [Sales Order](../sales/sales_order.md) instance | +| [salesordershipment](#sales-order-shipment) | A [Sales Order Shipment](../sales/sales_order.md#sales-order-shipments) instance | | [returnorder](#return-order) | A [Return Order](../sales/return_order.md) instance | | [purchaseorder](#purchase-order) | A [Purchase Order](../purchasing/purchase_order.md) instance | +| [transferorder](#transfer-order) | A [Transfer Order](../stock/transfer_order.md) instance | | [stockitem](#stock-item) | A [StockItem](../stock/index.md#stock-item) instance | | [stocklocation](#stock-location) | A [StockLocation](../stock/index.md#stock-location) instance | | [part](#part) | A [Part](../part/index.md) instance | @@ -141,6 +143,16 @@ When printing a report or label against a [PurchaseOrder](../purchasing/purchase {{ report_context("models", "purchaseorder") }} +### Transfer Order + +When printing a report or label against a [TransferOrder](../stock/transfer_order.md) object, the following context variables are available: + +{{ report_context("models", "transferorder") }} + +::: order.models.TransferOrder.report_context + options: + show_source: True + ### Stock Item When printing a report or label against a [StockItem](../stock/index.md#stock-item) object, the following context variables are available: @@ -173,7 +185,7 @@ When printing a report or label against a [Part](../part/index.md) object, the f ## Model Variables -Additional to the context variables provided directly to each template, each model type has a number of attributes and methods which can be accessedd via the template. +Additional to the context variables provided directly to each template, each model type has a number of attributes and methods which can be accessed via the template. For each model type, a subset of the most commonly used attributes are listed below. For a full list of attributes and methods, refer to the source code for the particular model type. @@ -187,7 +199,6 @@ Each part object has access to a lot of context variables about the part. The fo |----------|-------------| | name | Brief name for this part | | full_name | Full name for this part (including IPN, if not null and including variant, if not null) | -| variant | Optional variant number for this part - Must be unique for the part name | category | The [PartCategory](#part-category) object to which this part belongs | description | Longer form description of the part | keywords | Optional keywords for improving part search results @@ -244,7 +255,6 @@ Each part object has access to a lot of context variables about the part. The fo | Variable | Description | |----------|-------------| | parent | Link to another [StockItem](#stock-item) from which this StockItem was created | -| uid | Field containing a unique-id which is mapped to a third-party identifier (e.g. a barcode) | | part | Link to the master abstract [Part](#part) that this [StockItem](#stock-item) is an instance of | | supplier_part | Link to a specific [SupplierPart](#supplierpart) (optional) | | location | The [StockLocation](#stock-location) Where this [StockItem](#stock-item) is located | @@ -263,7 +273,6 @@ Each part object has access to a lot of context variables about the part. The fo | build | Link to a Build (if this stock item was created from a build) | | is_building | Boolean field indicating if this stock item is currently being built (or is "in production") | | purchase_order | Link to a [PurchaseOrder](#purchase-order) (if this stock item was created from a PurchaseOrder) | -| infinite | If True this [StockItem](#stock-item) can never be exhausted | | sales_order | Link to a [SalesOrder](#sales-order) object (if the StockItem has been assigned to a SalesOrder) | | purchase_price | The unit purchase price for this [StockItem](#stock-item) - this is the unit price at time of purchase (if this item was purchased from an external supplier) | | packaging | Description of how the StockItem is packaged (e.g. "reel", "loose", "tape" etc) | @@ -353,7 +362,7 @@ Each part object has access to a lot of context variables about the part. The fo | Variable | Description | |----------|-------------| | username | the username of the user | -| fist_name | The first name of the user | +| first_name | The first name of the user | | last_name | The last name of the user | | email | The email address of the user | | pk | The primary key of the user | diff --git a/docs/docs/report/helpers.md b/docs/docs/report/helpers.md index 1cde507797..dbbe7306ad 100644 --- a/docs/docs/report/helpers.md +++ b/docs/docs/report/helpers.md @@ -907,7 +907,7 @@ If you have a custom logo, but explicitly wish to load the InvenTree logo itself ## Report Assets -[Report Assets](./index.md#report-assets) are files specifically uploaded by the user for inclusion in generated reports and labels. +[Report Assets](./assets.md) are files specifically uploaded by the user for inclusion in generated reports and labels. You can add asset images to the reports and labels by using the `{% raw %}{% asset ... %}{% endraw %}` template tag: diff --git a/docs/docs/report/index.md b/docs/docs/report/index.md index 37124e13d6..39fafdacc0 100644 --- a/docs/docs/report/index.md +++ b/docs/docs/report/index.md @@ -53,29 +53,43 @@ To read more about the capabilities of the report templating engine, and how to ## Creating Templates -Report and label templates can be created (and edited) via the [admin interface](../settings/admin.md), under the *Report* section. +Report and label templates are managed from the [Admin Center](../settings/admin.md#admin-center), which provides dedicated panels (under the *Reporting* group) for each template type: -Select the type of template you are wanting to create (a *Report Template* or *Label Template*) and press the *Add* button in the top right corner: +- **Label Templates** - Create and edit [label templates](./labels.md) +- **Report Templates** - Create and edit [report templates](./report.md) +- **Report Snippets** - Manage reusable [snippet](#report-snippets) files +- **Report Assets** - Manage uploaded [asset](#report-assets) files -{{ image("report/report_template_admin.png", "Report template admin") }} +Label and report templates are created and edited using the built-in [template editor](./template_editor.md), which allows templates to be written directly within the browser, with a live preview of the rendered output. !!! tip "Staff Access Only" - Only users with staff access can upload or edit report template files. + Only users with staff access can create, upload or edit templates, snippets and assets. -!!! info "Editing Reports" - Existing reports can be edited from the admin interface, in the same location as described above. To change the contents of the template, re-upload a template file, to override the existing template data. - -!!! tip "Template Editor" - InvenTree also provides a powerful [template editor](./template_editor.md) which allows for the creation and editing of report templates directly within the browser. +!!! info "Backend Admin Interface" + Templates can also be managed at a lower level via the [backend admin interface](../settings/admin.md#backend-admin-interface), under the *Report* section. This is recommended for advanced users only. ### Name and Description Each report template requires a name and description, which identify and describe the report template. +### Revision + +Each template has a revision number, which is automatically incremented each time the template is updated. This provides a simple mechanism for tracking changes to a template over time. The revision number is read-only, and cannot be edited directly. + +!!! info "Template Revision Context" + The revision number of the template is made available when rendering, via the `template_revision` [context variable](./context_variables.md#global-context). + ### Enabled Status Boolean field which determines if the specific report template is enabled, and available for use. Reports can be disabled to remove them from the list of available templates, but without deleting them from the database. +### Attach to Model + +If the *Attach to Model on Print* option is enabled, a copy of the generated report is automatically saved as a file attachment against the item (model instance) for which it was generated, each time the template is printed. + +!!! warning "Attachment Support" + The report output is only attached if the target model type supports file attachments. + ### Filename Pattern The filename pattern used to generate the output `.pdf` file. Defaults to "report.pdf". @@ -147,89 +161,15 @@ Setting the *Debug Mode* option renders the template as raw HTML instead of PDF, ## Report Assets -User can upload asset files (e.g. images) which can be used when generating reports. For example, you may wish to generate a report with your company logo in the header. Asset files are uploaded via the admin interface. - -Asset files can be rendered directly into the template as follows - -```html -{% raw %} - -{% load report %} - - - - - - - - - - - - - - - -{% endraw %} -``` - -!!! warning "Asset Naming" - If the requested asset name does not match the name of an uploaded asset, the template will continue without loading the image. - -!!! info "Assets location" - Upload new assets via the [admin interface](../settings/admin.md) to ensure they are uploaded to the correct location on the server. +User can upload asset files (e.g. images) which can be used when generating reports. For example, you may wish to generate a report with your company logo in the header. +Refer to the [report assets](./assets.md) documentation for further information. ## Report Snippets -A powerful feature provided by the django / WeasyPrint templating framework is the ability to include external template files. This allows commonly used template features to be broken out into separate files and reused across multiple templates. +InvenTree provides report "snippets" - reusable template files which cannot be rendered by themselves, but can be included in other templates. -To support this, InvenTree provides report "snippets" - short (or not so short) template files which cannot be rendered by themselves, but can be called from other templates. - -Similar to assets files, snippet template files are uploaded via the admin interface. - -Snippets are included in a template as follows: - -``` -{% raw %}{% include 'snippets/' %}{% endraw %} -``` - -For example, consider a custom stocktake report for a particular stock location, where we wish to render a table with a row for each item in that location. - -```html -{% raw %} - - - - - - - {% for item in location.stock_items %} - {% include 'snippets/stock_row.html' with item=item %} - {% endfor %} - - -{% endraw %} -``` - -!!! info "Snippet Arguments" - Note above that named argument variables can be passed through to the snippet! - -And the snippet file `stock_row.html` may be written as follows: - -```html -{% raw %} - - - - - -{% endraw %} -``` +Refer to the [report snippets](./snippets.md) documentation for further information. ## Security @@ -243,7 +183,7 @@ When WeasyPrint renders a template to PDF it can make outbound requests to load |---|---| | `data:` URIs | Always permitted — self-contained, no network access | | `file://` | Always blocked — assets and images must be inlined as `data:` URIs before reaching WeasyPrint | -| `http` / `https` | Disabled by default, but can be blocked - see *Remote URL Fetching* below | +| `http` / `https` | Disabled by default, but can be enabled - see *Remote URL Fetching* below | | Any other scheme | Always blocked | HTTP redirects are also disabled: a URL that passes validation cannot redirect to an internal address. @@ -259,6 +199,6 @@ When enabled, URLs are still validated against private, loopback, link-local, an ### Asset Files -Asset files uploaded through the admin interface are embedded directly into the rendered PDF as base64 `data:` URIs — they are read via the Django storage API and never loaded through WeasyPrint's URL fetcher. This means assets work correctly regardless of whether remote URL fetching is enabled, and also work with remote storage backends such as S3. +[Asset files](./assets.md) uploaded through the admin interface are embedded directly into the rendered PDF as base64 `data:` URIs — they are read via the Django storage API and never loaded through WeasyPrint's URL fetcher. This means assets work correctly regardless of whether remote URL fetching is enabled, and also work with remote storage backends such as S3. There are various [helper functions](./helpers.md#report-assets) available to assist with embedding assets into templates. diff --git a/docs/docs/report/labels.md b/docs/docs/report/labels.md index 7b4ac1b768..cdaf7136d2 100644 --- a/docs/docs/report/labels.md +++ b/docs/docs/report/labels.md @@ -128,6 +128,25 @@ As an example, consider a label template for a StockItem. A user may wish to def To restrict the label accordingly, we could set the *filters* value to `part__IPN=IPN123`. +## Printing Labels + +Labels are printed directly from the web interface, from the pages where the target items are displayed. To print labels against one or more items: + +1. Select the items to print - either from a table (using the row checkboxes), or by viewing the detail page of a single item +2. Select the *Print* action, and choose the *Print Label* option +3. Select the desired label template - only *enabled* templates which match the selected model type (and pass any template filters) are available for selection +4. Select the *printer* (plugin) to use for printing the labels + +### Label Printing Plugins + +The actual printing of labels is handled by a [label printing plugin](../plugins/mixins/label.md). InvenTree provides a number of built-in printing plugins: + +- The default [InvenTree Label Printer](../plugins/builtin/inventree_label.md) plugin generates a PDF file, which is then made available for download. +- The [Label Sheet](../plugins/builtin/inventree_label_sheet.md) plugin arranges multiple labels onto a single sheet for printing. +- The [Label Machine](../plugins/builtin/inventree_label_machine.md) plugin sends the label to an external [label printer machine](../plugins/machines/label_printer.md). + +Custom label printing plugins (e.g. for driving a specific hardware printer) can be installed to extend this list - refer to the [label mixin documentation](../plugins/mixins/label.md) for further information. + ## Built-In Templates The InvenTree installation provides a number of simple *default* templates which can be used as a starting point for creating custom labels. These built-in templates can be disabled if they are not required. diff --git a/docs/docs/report/report.md b/docs/docs/report/report.md index 935e977af9..47556e08ef 100644 --- a/docs/docs/report/report.md +++ b/docs/docs/report/report.md @@ -1,53 +1,51 @@ --- -title: Report and Label Generation +title: Report Templates --- -## Custom Reports +## Report Templates -InvenTree supports a customizable reporting ecosystem, allowing the user to develop document templates that meet their particular needs. +Report templates are used to generate formal PDF documents - such as order reports, packing lists, or test reports - rendered against a particular database item (or list of items). -PDF files are generated from custom HTML template files which are written by the user. +Like all InvenTree templates, report templates are written using a mixture of HTML / CSS and the django template language, and are rendered to a PDF file using the WeasyPrint engine: -Templates can be used to generate *reports* or *labels* which can be used in a variety of situations to format data in a friendly format for printing, distribution, conformance and testing. +- Refer to the [template overview](./index.md) for information on creating and managing templates. +- Refer to the [template rendering documentation](./weasyprint.md) for information on the WeasyPrint engine and the django template language. +- Refer to the [context variables documentation](./context_variables.md) for the variables available when rendering a template. -In addition to providing the ability for end-users to provide their own reporting templates, some report types offer "built-in" report templates ready for use. +## Report Options -### WeasyPrint Templates +In addition to the [options common to all templates](./index.md#creating-templates), each report template provides the following options: -InvenTree report templates utilize the powerful [WeasyPrint](https://weasyprint.org/) PDF generation engine. +### Page Size -!!! info "WeasyPrint" - WeasyPrint is an extremely powerful and flexible reporting library. Refer to the [WeasyPrint docs](https://doc.courtbouillon.org/weasyprint/stable/) for further information. +The page size (e.g. `A4` or `Letter`) used when rendering the report to a PDF file. When a new report template is created, this value defaults to the [default page size](./index.md#default-page-size) specified in the global settings. -### Stylesheets +### Landscape -Templates are rendered using standard HTML / CSS - if you are familiar with web page layout, you're ready to go! +If enabled, the report is rendered in landscape orientation, rather than the default portrait orientation. -### Template Language +### Merge -Uploaded report template files are passed through the [django template rendering framework]({% include "django.html" %}/topics/templates/), and as such accept the same variable template strings as any other django template file. Different variables are passed to the report template (based on the context of the report) and can be used to customize the contents of the generated PDF. +If enabled, a single report document is generated for all selected items, rather than a separate document for each item. Refer to the [merging reports](#merging-reports) section below for further information. -### Variables +!!! info "Context Variables" + The `page_size`, `landscape` and `merge` values are also made available to the template as [context variables](./context_variables.md#report-context). -Each report template is provided a set of *context variables* which can be used when rendering the template. +## Generating Reports -For example, rendering the name of a part (which is available in the particular template context as `part`) is as follows: +Reports are generated directly from the web interface, from the pages where the target items are displayed. To generate a report against one or more items: -```html -{% raw %} +1. Select the items to print - either from a table (using the row checkboxes), or by viewing the detail page of a single item +2. Select the *Print* action, and choose the *Print Report* option +3. Select the desired report template - only *enabled* templates which match the selected model type (and pass any [template filters](./index.md#template-filters)) are available for selection +4. The report is generated by the server, and the resulting PDF file is made available for download - -

Part: {{ part.name }}

-

- Description:
- {{ part.description }} -

-{% endraw %} -``` +!!! info "Enable Reports" + Report generation must be [enabled in the global settings](./index.md#enable-reports) before reports can be generated. ## Merging Reports -When rendering reports for multiple items, the default behaviour is that each item is rendered as a separate report. The chosen templeate is rendered multiple times, once for each item selected, and expects a single item in the context variable. +When rendering reports for multiple items, the default behaviour is that each item is rendered as a separate report. The chosen template is rendered multiple times, once for each item selected, and expects a single item in the context variable. However, it is possible to merge multiple items into a single report document. This is achieved by enabling the `merge` attribute of the report template: diff --git a/docs/docs/report/samples.md b/docs/docs/report/samples.md index 0489e3b92c..7fed4424ac 100644 --- a/docs/docs/report/samples.md +++ b/docs/docs/report/samples.md @@ -22,6 +22,7 @@ The following report templates are provided "out of the box" and can be used as | [Sales Order Shipment](#sales-order-shipment) | [SalesOrderShipment](../sales/sales_order.md) | Sales Order Shipment report | | [Stock Location](#stock-location) | [StockLocation](../stock/index.md#stock-location) | Stock Location report | | [Test Report](#test-report) | [StockItem](../stock/index.md#stock-item) | Test Report | +| [Transfer Order](#transfer-order) | [TransferOrder](../stock/transfer_order.md) | Transfer Order report | | [Selected Stock Items Report](#selected-stock-items-report) | [StockItem](../stock/index.md#stock-item) | Selected Stock Items report | @@ -57,6 +58,10 @@ The following report templates are provided "out of the box" and can be used as {{ templatefile("report/inventree_test_report.html") }} +### Transfer Order + +{{ templatefile("report/inventree_transfer_order_report.html") }} + ### Selected Stock Items Report {{ templatefile("report/inventree_stock_report_merge.html") }} @@ -68,9 +73,11 @@ The following label templates are provided "out of the box" and can be used as a | Template | Model Type | Description | | --- | --- | --- | | [Build Line](#build-line-label) | [Build line item](../manufacturing/build.md) | Build Line label | -| [Part](#part-label) | [Part](../part/index.md) | Part label | +| [Part](#part-label) | [Part](../part/index.md) | Part label (QR code and part name) | +| [Part (Code128)](#part-label-code128) | [Part](../part/index.md) | Part label (Code128 barcode) | | [Stock Item](#stock-item-label) | [StockItem](../stock/index.md#stock-item) | Stock Item label | -| [Stock Location](#stock-location-label) | [StockLocation](../stock/index.md#stock-location) | Stock Location label | +| [Stock Location](#stock-location-label) | [StockLocation](../stock/index.md#stock-location) | Stock Location label (QR code) | +| [Stock Location (with text)](#stock-location-label-with-text) | [StockLocation](../stock/index.md#stock-location) | Stock Location label (QR code and location details) | ### Build Line Label @@ -78,6 +85,10 @@ The following label templates are provided "out of the box" and can be used as a ### Part Label +{{ templatefile("label/part_label.html") }} + +### Part Label (Code128) + {{ templatefile("label/part_label_code128.html") }} ### Stock Item Label @@ -86,4 +97,8 @@ The following label templates are provided "out of the box" and can be used as a ### Stock Location Label +{{ templatefile("label/stocklocation_qr.html") }} + +### Stock Location Label (with text) + {{ templatefile("label/stocklocation_qr_and_text.html") }} diff --git a/docs/docs/report/snippets.md b/docs/docs/report/snippets.md new file mode 100644 index 0000000000..6acf3f5eaa --- /dev/null +++ b/docs/docs/report/snippets.md @@ -0,0 +1,52 @@ +--- +title: Report Snippets +--- + +## Report Snippets + +A powerful feature provided by the django / WeasyPrint templating framework is the ability to include external template files. This allows commonly used template features to be broken out into separate files and reused across multiple templates. + +To support this, InvenTree provides report "snippets" - short (or not so short) template files which cannot be rendered by themselves, but can be called from other templates. + +Snippet files are managed from the [Admin Center](../settings/admin.md#admin-center), via the *Report Snippets* panel. Staff users can upload new snippet files, and edit or remove existing snippets. + +Additionally, the content of an existing snippet can be modified directly within the browser - simply select a snippet from the table to open it in the built-in [template editor](./template_editor.md#editing-snippets). + +Snippets are included in a template as follows: + +``` +{% raw %}{% include 'snippets/' %}{% endraw %} +``` + +For example, consider a custom stocktake report for a particular stock location, where we wish to render a table with a row for each item in that location. + +```html +{% raw %} + +
{{ item.part.full_name }}{{ item.quantity }}
+ + + + + {% for item in location.stock_items %} + {% include 'snippets/stock_row.html' with item=item %} + {% endfor %} + + +{% endraw %} +``` + +!!! info "Snippet Arguments" + Note above that named argument variables can be passed through to the snippet! + +And the snippet file `stock_row.html` may be written as follows: + +```html +{% raw %} + + + + + +{% endraw %} +``` diff --git a/docs/docs/report/template_editor.md b/docs/docs/report/template_editor.md index b4fbddbf04..3008434944 100644 --- a/docs/docs/report/template_editor.md +++ b/docs/docs/report/template_editor.md @@ -4,11 +4,11 @@ title: Template editor ## Template editor -The Template Editor is integrated into the [Admin Center](../settings//admin.md#admin-center) of the Web UI. It allows users to create and edit label and report templates directly with a side by side preview for a more productive workflow. +The Template Editor is integrated into the [Admin Center](../settings/admin.md#admin-center) of the Web UI. It allows users to create and edit label and report templates directly with a side by side preview for a more productive workflow. ![Template Table](../assets/images/report/template-table.png) -On the left side (1) are all possible possible template types for labels and reports listed. With the "+" button (2), above the template table (3), new templates for the selected type can be created. To switch to the template editor click on a template. +On the left side (1) are all possible template types for labels and reports listed. With the "+" button (2), above the template table (3), new templates for the selected type can be created. To switch to the template editor click on a template. ### Editing Templates @@ -31,3 +31,10 @@ If you don't want to override the template, but just render a preview for a temp #### Edit template metadata Editing metadata such as name, description, filters and even width/height for labels and orientation/page size for reports can be done from the edit modal accessible when clicking on the three dots (4) and select "Edit" in the dropdown menu. + +### Editing Snippets + +[Report snippets](./index.md#report-snippets) can also be edited directly within the browser, from the *Report Snippets* panel in the Admin Center. Selecting a snippet from the table opens it in the same code editor as used for report and label templates. + +!!! info "No Preview" + As snippets cannot be rendered by themselves (they must be included in a report or label template), no preview area is available when editing a snippet. diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index ca20e704c7..f24d8db82c 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -174,6 +174,8 @@ nav: - Template Editor: report/template_editor.md - Reports: report/report.md - Labels: report/labels.md + - Report Assets: report/assets.md + - Report Snippets: report/snippets.md - Context Variables: report/context_variables.md - Helper Functions: report/helpers.md - Barcodes: report/barcodes.md diff --git a/src/frontend/src/components/editors/TemplateEditor/PdfPreview/PdfPreview.tsx b/src/frontend/src/components/editors/TemplateEditor/PdfPreview/PdfPreview.tsx index 5a0fea7cc2..3536f92c2e 100644 --- a/src/frontend/src/components/editors/TemplateEditor/PdfPreview/PdfPreview.tsx +++ b/src/frontend/src/components/editors/TemplateEditor/PdfPreview/PdfPreview.tsx @@ -33,7 +33,7 @@ export const PdfPreviewComponent: PreviewAreaComponent = forwardRef( } let preview = await api.post( - printingUrl, + printingUrl!, { items: [previewItem], template: template.pk diff --git a/src/frontend/src/components/editors/TemplateEditor/TemplateEditor.tsx b/src/frontend/src/components/editors/TemplateEditor/TemplateEditor.tsx index 6aafc01f54..ef5a0e4587 100644 --- a/src/frontend/src/components/editors/TemplateEditor/TemplateEditor.tsx +++ b/src/frontend/src/components/editors/TemplateEditor/TemplateEditor.tsx @@ -1,6 +1,11 @@ +import { Boundary } from '@lib/components/Boundary'; +import { ModelInformationDict } from '@lib/enums/ModelInformation'; +import { ModelType } from '@lib/enums/ModelType'; +import { apiUrl } from '@lib/functions/Api'; import { t } from '@lingui/core/macro'; import { Alert, + Button, CloseButton, Group, List, @@ -24,11 +29,6 @@ import { import Split from '@uiw/react-split'; import type React from 'react'; import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; - -import { Boundary } from '@lib/components/Boundary'; -import { ModelInformationDict } from '@lib/enums/ModelInformation'; -import { ModelType } from '@lib/enums/ModelType'; -import { apiUrl } from '@lib/functions/Api'; import { api } from '../../../App'; import type { TemplateI } from '../../../tables/settings/TemplateTable'; import { SplitButton } from '../../buttons/SplitButton'; @@ -78,26 +78,38 @@ export type PreviewArea = { export type TemplateEditorProps = { templateUrl: string; - printingUrl: string; + printingUrl?: string; editors: Editor[]; previewAreas: PreviewArea[]; template: TemplateI; + // Name of the file field on the template model (e.g. 'template' or 'snippet') + fileField?: string; }; export function TemplateEditor(props: Readonly) { - const { templateUrl, editors, previewAreas, template } = props; + const { + templateUrl, + editors, + previewAreas, + template, + fileField = 'template' + } = props; const editorRef = useRef(null); const previewRef = useRef(null); + // If no preview areas are provided, the template is saved directly + const hasPreview = previewAreas.length > 0; + const [hasSaveConfirmed, setHasSaveConfirmed] = useState(false); const [previewItem, setPreviewItem] = useState(''); const [renderingErrors, setRenderingErrors] = useState(null); const [isPreviewLoading, setIsPreviewLoading] = useState(false); + const [isSaving, setIsSaving] = useState(false); const [editorValue, setEditorValue] = useState(editors[0].key); const [previewValue, setPreviewValue] = useState( - previewAreas[0].key + previewAreas[0]?.key ?? null ); const codeRef = useRef(undefined); @@ -131,12 +143,12 @@ export function TemplateEditor(props: Readonly) { if (!templateUrl) return; api.get(templateUrl).then((response: any) => { - if (response.data?.template) { + if (response.data?.[fileField]) { // Fetch the raw template file from the server // Request that it is provided without any caching, // to ensure that we always get the latest version api - .get(response.data.template, { + .get(response.data[fileField], { headers: { 'Cache-Control': 'no-cache', Pragma: 'no-cache', @@ -149,7 +161,7 @@ export function TemplateEditor(props: Readonly) { }) .catch(() => { console.error( - `ERR: Could not load template from ${response.data.template}` + `ERR: Could not load template from ${response.data[fileField]}` ); codeRef.current = undefined; hideNotification('template-load-error'); @@ -244,6 +256,45 @@ export function TemplateEditor(props: Readonly) { [previewItem] ); + // Save the template file directly to the server, without any preview + const saveTemplate = useCallback(async () => { + const code = await getCodeFromEditor(); + + if (code === undefined) return; + + const filename = + (template as any)[fileField]?.split('/').pop() ?? 'template.html'; + + const formData = new FormData(); + formData.append(fileField, new File([code], filename)); + + setIsSaving(true); + + api + .patch(templateUrl, formData) + .then(() => { + hideNotification('template-save'); + showNotification({ + id: 'template-save', + title: t`Saved`, + message: t`Template file has been updated`, + color: 'green' + }); + }) + .catch(() => { + hideNotification('template-save'); + showNotification({ + id: 'template-save', + title: t`Error`, + message: t`Could not save the template to the server.`, + color: 'red' + }); + }) + .finally(() => { + setIsSaving(false); + }); + }, [getCodeFromEditor, template, templateUrl, fileField]); + const previewApiUrl = useMemo( () => ModelInformationDict[template.model_type ?? ModelType.stockitem] @@ -261,18 +312,20 @@ export function TemplateEditor(props: Readonly) { }, [template]); useEffect(() => { + if (!hasPreview) return; + api .get(apiUrl(previewApiUrl), { params: { limit: 1, ...templateFilters } }) .then((res) => { if (res.data.results.length === 0) return; setPreviewItem(res.data.results[0].pk); }); - }, [previewApiUrl, templateFilters]); + }, [hasPreview, previewApiUrl, templateFilters]); return ( - + { @@ -282,7 +335,7 @@ export function TemplateEditor(props: Readonly) { keepMounted={false} style={{ minWidth: '300px', - width: '50%', + width: hasPreview ? '50%' : '100%', display: 'flex', flexDirection: 'column' }} @@ -301,29 +354,39 @@ export function TemplateEditor(props: Readonly) { })} - updatePreview(true, false), - disabled: !previewItem || isPreviewLoading - }, - { - key: 'preview_save', - name: t`Save & Reload Preview`, - tooltip: t`Save the current template and reload the preview`, - icon: IconDeviceFloppy, - onClick: () => updatePreview(hasSaveConfirmed), - disabled: !previewItem || isPreviewLoading - } - ]} - /> + {hasPreview ? ( + updatePreview(true, false), + disabled: !previewItem || isPreviewLoading + }, + { + key: 'preview_save', + name: t`Save & Reload Preview`, + tooltip: t`Save the current template and reload the preview`, + icon: IconDeviceFloppy, + onClick: () => updatePreview(hasSaveConfirmed), + disabled: !previewItem || isPreviewLoading + } + ]} + /> + ) : ( + + )} @@ -342,100 +405,102 @@ export function TemplateEditor(props: Readonly) { ))} - - - {previewAreas.map((PreviewArea) => ( - - {PreviewArea.name} - - ))} - - -
- setPreviewItem(value) - }} - /> -
+ + {previewAreas.map((PreviewArea) => ( + + {PreviewArea.name} + + ))} + - {previewAreas.map((PreviewArea) => ( - -
setPreviewItem(value) + }} + /> +
+ + {previewAreas.map((PreviewArea) => ( + - {/* @ts-ignore-next-line */} - +
+ {/* @ts-ignore-next-line */} + - {renderingErrors && ( - - setRenderingErrors(null)} - style={{ - position: 'absolute', - top: '10px', - right: '10px', - color: '#fff' - }} - variant='filled' - /> - } - title={t`Error rendering template`} - mx='10px' - > - - {renderingErrors.map((error, index) => ( - {error} - ))} - - - - )} -
-
- ))} -
+ {renderingErrors && ( + + setRenderingErrors(null)} + style={{ + position: 'absolute', + top: '10px', + right: '10px', + color: '#fff' + }} + variant='filled' + /> + } + title={t`Error rendering template`} + mx='10px' + > + + {renderingErrors.map((error, index) => ( + {error} + ))} + + + + )} + + + ))} + + )}
diff --git a/src/frontend/src/pages/Index/Settings/AdminCenter/Index.tsx b/src/frontend/src/pages/Index/Settings/AdminCenter/Index.tsx index ecbdddfae4..454225c5b5 100644 --- a/src/frontend/src/pages/Index/Settings/AdminCenter/Index.tsx +++ b/src/frontend/src/pages/Index/Settings/AdminCenter/Index.tsx @@ -1,3 +1,6 @@ +import { PluginPanelKey } from '@lib/enums/ModelType'; +import { UserRoles } from '@lib/enums/Roles'; +import type { PanelGroupType, PanelType } from '@lib/types/Panel'; import { t } from '@lingui/core/macro'; import { Stack } from '@mantine/core'; import { @@ -5,6 +8,7 @@ import { IconCpu, IconDevicesPc, IconExclamationCircle, + IconFileCode, IconFileDownload, IconFileUpload, IconHome, @@ -12,6 +16,7 @@ import { IconListDetails, IconMail, IconPackages, + IconPhoto, IconPlugConnected, IconQrcode, IconReport, @@ -21,10 +26,6 @@ import { IconUsersGroup } from '@tabler/icons-react'; import { lazy, useMemo } from 'react'; - -import { PluginPanelKey } from '@lib/enums/ModelType'; -import { UserRoles } from '@lib/enums/Roles'; -import type { PanelGroupType, PanelType } from '@lib/types/Panel'; import PermissionDenied from '../../../../components/errors/PermissionDenied'; import PageTitle from '../../../../components/nav/PageTitle'; import { SettingsHeader } from '../../../../components/nav/SettingsHeader'; @@ -103,6 +104,14 @@ const LocationTypesTable = Loadable( lazy(() => import('../../../../tables/stock/LocationTypesTable')) ); +const SnippetTable = Loadable( + lazy(() => import('../../../../tables/settings/SnippetTable')) +); + +const AssetTable = Loadable( + lazy(() => import('../../../../tables/settings/AssetTable')) +); + export default function AdminCenter() { const user = useUserState(); @@ -221,6 +230,18 @@ export default function AdminCenter() { icon: , content: }, + { + name: 'snippets', + label: t`Report Snippets`, + icon: , + content: + }, + { + name: 'assets', + label: t`Report Assets`, + icon: , + content: + }, { name: 'location-types', label: t`Location Types`, @@ -273,7 +294,7 @@ export default function AdminCenter() { { id: 'reporting', label: t`Reporting`, - panelIDs: ['labels', 'reports'] + panelIDs: ['labels', 'reports', 'snippets', 'assets'] }, { id: 'plm', diff --git a/src/frontend/src/tables/settings/AssetTable.tsx b/src/frontend/src/tables/settings/AssetTable.tsx new file mode 100644 index 0000000000..a04ac10af0 --- /dev/null +++ b/src/frontend/src/tables/settings/AssetTable.tsx @@ -0,0 +1,124 @@ +import { AddItemButton } from '@lib/components/AddItemButton'; +import { type RowAction, RowDeleteAction } from '@lib/components/RowActions'; +import { ApiEndpoints } from '@lib/enums/ApiEndpoints'; +import { apiUrl } from '@lib/functions/Api'; +import useTable from '@lib/hooks/UseTable'; +import type { ApiFormFieldSet } from '@lib/types/Forms'; +import type { TableColumn } from '@lib/types/Tables'; +import { t } from '@lingui/core/macro'; +import { Alert, Text } from '@mantine/core'; +import { IconInfoCircle } from '@tabler/icons-react'; +import { type ReactNode, useCallback, useMemo, useState } from 'react'; +import { AttachmentLink } from '../../components/items/AttachmentLink'; +import { + useCreateApiFormModal, + useDeleteApiFormModal +} from '../../hooks/UseForm'; +import { useUserState } from '../../states/UserState'; +import { DescriptionColumn } from '../ColumnRenderers'; +import { InvenTreeTable } from '../InvenTreeTable'; + +export type AssetI = { + pk: number; + asset: string; + description: string; +}; + +export default function AssetTable() { + const table = useTable('report-asset'); + const user = useUserState(); + + const columns: TableColumn[] = useMemo(() => { + return [ + { + accessor: 'asset', + title: t`Asset`, + sortable: false, + switchable: false, + render: (record: AssetI) => { + if (!record.asset) { + return '-'; + } + + return ; + }, + noContext: true + }, + DescriptionColumn({ + accessor: 'description', + sortable: false, + switchable: false + }) + ]; + }, []); + + const [selectedAsset, setSelectedAsset] = useState(-1); + + const rowActions = useCallback( + (record: AssetI): RowAction[] => { + return [ + RowDeleteAction({ + hidden: !user.isStaff(), + onClick: () => { + setSelectedAsset(record.pk); + deleteAsset.open(); + } + }) + ]; + }, + [user] + ); + + const newAssetFields: ApiFormFieldSet = useMemo(() => { + return { + asset: {}, + description: {} + }; + }, []); + + const deleteAsset = useDeleteApiFormModal({ + url: ApiEndpoints.report_asset, + pk: selectedAsset, + title: t`Delete Asset`, + table: table + }); + + const newAsset = useCreateApiFormModal({ + url: ApiEndpoints.report_asset, + title: t`Add Asset`, + fields: newAssetFields, + table: table + }); + + const tableActions: ReactNode[] = useMemo(() => { + return [ + newAsset.open()} + tooltip={t`Add asset`} + hidden={!user.isStaff()} + /> + ]; + }, [user]); + + return ( + <> + {newAsset.modal} + {deleteAsset.modal} + } title={t`Assets`}> + {t`Assets are files (such as images) which can be used when rendering reports and labels.`} + + + + ); +} diff --git a/src/frontend/src/tables/settings/SnippetTable.tsx b/src/frontend/src/tables/settings/SnippetTable.tsx new file mode 100644 index 0000000000..1abb3e2b13 --- /dev/null +++ b/src/frontend/src/tables/settings/SnippetTable.tsx @@ -0,0 +1,241 @@ +import { AddItemButton } from '@lib/components/AddItemButton'; +import { + type RowAction, + RowDeleteAction, + RowEditAction +} from '@lib/components/RowActions'; +import { DetailDrawer } from '@lib/components/nav/DetailDrawer'; +import { ApiEndpoints } from '@lib/enums/ApiEndpoints'; +import { apiUrl } from '@lib/functions/Api'; +import useTable from '@lib/hooks/UseTable'; +import type { ApiFormFieldSet } from '@lib/types/Forms'; +import type { TableColumn } from '@lib/types/Tables'; +import { t } from '@lingui/core/macro'; +import { Trans } from '@lingui/react/macro'; +import { + Alert, + Group, + LoadingOverlay, + Stack, + Text, + Title +} from '@mantine/core'; +import { IconFileCode, IconInfoCircle } from '@tabler/icons-react'; +import { type ReactNode, useCallback, useMemo, useState } from 'react'; +import { useNavigate } from 'react-router-dom'; +import { + CodeEditor, + TemplateEditor +} from '../../components/editors/TemplateEditor'; +import { AttachmentLink } from '../../components/items/AttachmentLink'; +import { + useCreateApiFormModal, + useDeleteApiFormModal, + useEditApiFormModal +} from '../../hooks/UseForm'; +import { useInstance } from '../../hooks/UseInstance'; +import { useUserState } from '../../states/UserState'; +import { DescriptionColumn } from '../ColumnRenderers'; +import { InvenTreeTable } from '../InvenTreeTable'; +import type { TemplateI } from './TemplateTable'; + +export type SnippetI = { + pk: number; + snippet: string; + description: string; +}; + +export function SnippetDrawer({ + id +}: Readonly<{ + id: string | number; +}>) { + const { + instance: snippet, + instanceQuery: { isFetching, error } + } = useInstance({ + endpoint: ApiEndpoints.report_snippet, + hasPrimaryKey: true, + pk: id + }); + + const filename = useMemo( + () => snippet?.snippet?.split('/').pop() ?? '', + [snippet] + ); + + if (isFetching) { + return ; + } + + if (error || !snippet) { + return ( + + {(error as any)?.response?.status === 404 ? ( + Snippet not found + ) : ( + An error occurred while fetching snippet details + )} + + ); + } + + return ( + + + {filename} + + + + + ); +} + +export default function SnippetTable() { + const table = useTable('report-snippet'); + const navigate = useNavigate(); + const user = useUserState(); + + const openDetailDrawer = useCallback((pk: number) => navigate(`${pk}/`), []); + + const columns: TableColumn[] = useMemo(() => { + return [ + { + accessor: 'snippet', + title: t`Snippet`, + sortable: false, + switchable: false, + render: (record: SnippetI) => { + if (!record.snippet) { + return '-'; + } + + return ; + }, + noContext: true + }, + DescriptionColumn({ + accessor: 'description', + sortable: false, + switchable: false + }) + ]; + }, []); + + const [selectedSnippet, setSelectedSnippet] = useState(-1); + + const rowActions = useCallback( + (record: SnippetI): RowAction[] => { + return [ + { + title: t`Modify`, + tooltip: t`Modify snippet file`, + icon: , + onClick: () => openDetailDrawer(record.pk), + hidden: !user.isStaff() + }, + RowEditAction({ + hidden: !user.isStaff(), + onClick: () => { + setSelectedSnippet(record.pk); + editSnippet.open(); + } + }), + RowDeleteAction({ + hidden: !user.isStaff(), + onClick: () => { + setSelectedSnippet(record.pk); + deleteSnippet.open(); + } + }) + ]; + }, + [user] + ); + + const editSnippetFields: ApiFormFieldSet = useMemo(() => { + return { + description: {} + }; + }, []); + + const newSnippetFields: ApiFormFieldSet = useMemo(() => { + return { + snippet: {}, + description: {} + }; + }, []); + + const editSnippet = useEditApiFormModal({ + url: ApiEndpoints.report_snippet, + pk: selectedSnippet, + title: t`Edit Snippet`, + fields: editSnippetFields, + onFormSuccess: (record: any) => table.updateRecord(record) + }); + + const deleteSnippet = useDeleteApiFormModal({ + url: ApiEndpoints.report_snippet, + pk: selectedSnippet, + title: t`Delete Snippet`, + table: table + }); + + const newSnippet = useCreateApiFormModal({ + url: ApiEndpoints.report_snippet, + title: t`Add Snippet`, + fields: newSnippetFields, + onFormSuccess: (data) => { + table.refreshTable(); + openDetailDrawer(data.pk); + } + }); + + const tableActions: ReactNode[] = useMemo(() => { + return [ + newSnippet.open()} + tooltip={t`Add snippet`} + hidden={!user.isStaff()} + /> + ]; + }, [user]); + + return ( + <> + {newSnippet.modal} + {editSnippet.modal} + {deleteSnippet.modal} + { + return ; + }} + /> + } title={t`Snippets`}> + {t`Snippets are reusable pieces of HTML content that can be inserted into reports and labels.`} + + openDetailDrawer(record.pk) + }} + /> + + ); +} diff --git a/src/frontend/src/tables/stock/InstalledItemsTable.tsx b/src/frontend/src/tables/stock/InstalledItemsTable.tsx index 5ccbc10eef..ed3cfe1c16 100644 --- a/src/frontend/src/tables/stock/InstalledItemsTable.tsx +++ b/src/frontend/src/tables/stock/InstalledItemsTable.tsx @@ -16,7 +16,12 @@ import { } from '../../forms/StockForms'; import { useCreateApiFormModal } from '../../hooks/UseForm'; import { useUserState } from '../../states/UserState'; -import { PartColumn, StatusColumn, StockColumn } from '../ColumnRenderers'; +import { + IPNColumn, + PartColumn, + StatusColumn, + StockColumn +} from '../ColumnRenderers'; import { InvenTreeTable } from '../InvenTreeTable'; export default function InstalledItemsTable({ @@ -61,6 +66,7 @@ export default function InstalledItemsTable({ PartColumn({ part: 'part_detail' }), + IPNColumn({}), StockColumn({ accessor: '', title: t`Stock Item`, diff --git a/src/frontend/tests/pui_settings.spec.ts b/src/frontend/tests/pui_settings.spec.ts index 71042ba5fa..dbd478c60d 100644 --- a/src/frontend/tests/pui_settings.spec.ts +++ b/src/frontend/tests/pui_settings.spec.ts @@ -254,6 +254,37 @@ test('Settings - Admin', async ({ browser }) => { await loadTab(page, 'Category Parameters'); await loadTab(page, 'Label Templates'); await loadTab(page, 'Report Templates'); + + // Check the "report snippets" panel + await loadTab(page, 'Report Snippets'); + await page + .getByText( + 'Snippets are reusable pieces of HTML content that can be inserted into reports and labels.' + ) + .waitFor(); + + // Launch the dialog to upload a new snippet + await page.getByLabel('action-button-add-snippet').click(); + await page.getByText('Add Snippet', { exact: true }).waitFor(); + await page.locator('input[type="file"]').waitFor({ state: 'attached' }); + await page.getByLabel('text-field-description').waitFor(); + await page.getByRole('button', { name: 'Cancel' }).click(); + + // Check the "report assets" panel + await loadTab(page, 'Report Assets'); + await page + .getByText( + 'Assets are files (such as images) which can be used when rendering reports and labels.' + ) + .waitFor(); + + // Launch the dialog to upload a new asset + await page.getByLabel('action-button-add-asset').click(); + await page.getByText('Add Asset', { exact: true }).waitFor(); + await page.locator('input[type="file"]').waitFor({ state: 'attached' }); + await page.getByLabel('text-field-description').waitFor(); + await page.getByRole('button', { name: 'Cancel' }).click(); + await loadTab(page, 'Plugins'); // Adjust some "location type" items
{{ item.part.full_name }}{{ item.quantity }}