# A Reorder Report by Script

The **Needs Reordering** widget shows the parts to buy on a dashboard. Purchasing may want that list in an email or a file instead. `system.kanoa.inventory.getPartInventoryStock` returns the same information to a script.

It returns a dataset with one row per part and storeroom. Each row joins the stock balance with the stocking policy. Pass filters in a dictionary, or an empty dictionary for every row. The filters you need are:

| Filter | Value | Meaning |
| --- | --- | --- |
| `isStocked` | `True` | Only parts with an active stocking policy |
| `isBelowReorderThreshold` | `True` | Only parts below their reorder point |
| `inventoryAssetName` or `inventoryAssetPath` | `'Parts Room'` or the full path | Only one storeroom |

Each row has columns such as `partNumber`, `inventoryAssetName`, `quantityOnHand`, `quantityOnOrder`, `reorderPointQty`, `reorderQty`, `uomCode` and `isBelowSafetyStock`.

::embed-section{src="../../../systemFunctions/inventory/getPartInventoryStock.md" collapsed="true" summary="Reference Materials: getPartInventoryStock"}

:::action[Print the reorder list]

1. In **Inventory → Parts**, check that `CF-FLT-COOL10` has 2 on hand, below its reorder point. You did this in [Adjust Stock and Find Reorders](../02-stock-transactions/3-adjust-and-reorder.md).
2. In the Ignition Designer, open the **Script Console** and run:

   ```python
   rows = system.kanoa.utilities.convertDatasetToJSON(
       system.kanoa.inventory.getPartInventoryStock({
           'inventoryAssetName': 'Parts Room',
           'isStocked': True,
           'isBelowReorderThreshold': True,
       }))

   for row in rows:
       print row.get('partNumber'), row.get('quantityOnHand'), row.get('reorderPointQty'), row.get('reorderQty'), row.get('uomCode')
   ```

3. Check the output. `CF-FLT-COOL10` is listed with `2.0` on hand, and its reorder point and reorder quantity.
4. Receive some filters into the Parts Room so it rises above its reorder point, and run the script again. The part drops off the list.

:::

## Turn it into a report

Put the query in a project script library function that returns text, so any caller can use it:

```python
def reorderText(storeroomName):
    """
    One line per part to reorder. Returns None when nothing needs ordering.
    """
    rows = system.kanoa.utilities.convertDatasetToJSON(
        system.kanoa.inventory.getPartInventoryStock({
            'inventoryAssetName': storeroomName,
            'isStocked': True,
            'isBelowReorderThreshold': True,
        }))
    if not rows:
        return None

    lines = ['Part, On hand, On order, Reorder qty, UOM']
    for row in rows:
        lines.append('%s, %s, %s, %s, %s' % (row.get('partNumber'), row.get('quantityOnHand'),
            row.get('quantityOnOrder'), row.get('reorderQty'), row.get('uomCode')))
    return '\n'.join(lines)
```

Save the library as `inventoryReports` in your project, then call `inventoryReports.reorderText('Parts Room')` from the Script Console to check it.

## Run it on a schedule

A gateway timer script can call `inventoryReports.reorderText('Parts Room')` once a day. If it returns text, the script sends it to purchasing with Ignition's email function, or writes it to a file. If it returns `None`, it does nothing.

:::note
Scheduling, email and file writing are standard Ignition features, not Kanoa functions. See the Ignition documentation for gateway timer scripts and `system.net.sendEmail`.
:::

{/*
TODO: confirm on a gateway the Ignition setup for the schedule: add a gateway timer script in the Designer (Gateway Events) that calls inventoryReports.reorderText, with a delay or fixed rate set to once a day. Then confirm an SMTP email profile is configured on the gateway and that system.net.sendEmail works from that scope. Add the working call to this lesson.
*/}

:::tip[Only report what changed]
A daily email that repeats the same parts becomes noise. Filter with `hasOnOrder` set to `False` to skip parts that already have an order open.
:::

{/*
TODO: confirm on a gateway that hasOnOrder False excludes parts with quantityOnOrder above zero, as the filter name suggests. The reference gives no description.
*/}

You built a script that finds every stocked part below its reorder point, turned it into a report function, and learned how to run it on a schedule.
