# Lot Attributes

::embed-section{src="../../kanoa-trace/lot-management.md#lot-attributes" collapsed="true" summary="Reference Materials: Lot Attributes"}

:::overview
A lot is only an ID, a name, an item and a unit, and its lot events give it a quantity, location and state. Real material carries more than that: the supplier's own lot number, a steel heat number, an expiry date, a moisture reading, a certificate of analysis. **Lot attributes** hold these facts.
:::

A lot attribute is a **name** and a **value** attached to one lot. A lot can have as many attributes as you need.

| Information | Where it belongs | Example |
| --- | --- | --- |
| How much, where, and in what condition | Lot events, from lot operations | 900 lbs at Receiving, `OK` |
| True for every lot of an item | Item attributes | The standard shelf life of `STD-COAT` |
| True for this lot only | Lot attributes | Supplier lot `ALC-24-0917` |

Keep these in mind when you plan lot attributes:

- **Names are free text.** There's no list to configure: the first time a name is used, it exists. `Supplier Lot` and `supplier lot #` are two different attributes, so agree the names before go-live.
- **Values are text.** Write dates as `2027-04-30` and put units in the name, such as `Hardness (HRC)`, so values sort and compare the same way everywhere.
- **Attributes stay on their lot.** A lot operation doesn't copy them to its output lots, but you can see the attributes of every lot in a genealogy together.
- **Editing overwrites.** An attribute keeps who created it and when, not its earlier values.
- **Deleting a lot deletes its attributes.**

***

## Lot Attributes in Lot Manager

Lot attributes are on the **Lot Attributes** tab below the lot table in **Ops → Lots → Lot Manager**. Adding, editing and deleting them needs the **Configure Lots** permission.

You'll record the supplier's information on the raw materials you received in [Execute the Trace](03-execute-the-trace.md), and an inspection result on the coated parts.

:::action[Add lot attributes]

1. In Lot Manager, select the aluminum lot (`RAL000000000x`) and open the **Lot Attributes** tab.
2. Click **Add Lot Attribute** (**+**). In **Lot Attribute**, type `Supplier Lot`, enter the value `ALC-24-0917`, and click **Save**.
3. Add a second attribute to `RAL000000000x`: `Heat Number`, `H58213`.
4. Select the `STD-COAT` lot (`RBI000000000x`) and add `Expiry Date`, `2027-04-30`. The **Lot Attribute** list now offers `Supplier Lot` and `Heat Number`: it lists every name already used on any lot. Choose one to keep names consistent, or type a new one.
5. Clear **Active** and click **Get lots** to reload the table, widening the date range if your lots are older. Select the coated parts lot (`WCP000000000x`) and add `Hardness (HRC)`, `58`.

:::

:::action[Edit and view a lot attribute]

1. Select `RAL000000000x` again. Select the `Heat Number` row and click **Edit Lot Attribute**.
2. Change the value to `H58214` and click **Save**. The table shows who created the attribute and when.
3. Double-click a row to see its whole value in a popup. That's useful for long values, such as notes or JSON from another system.

:::

:::action[See attributes across the genealogy]

1. Select the shipped package (`FGP000000000x`). **Show Full Lot Attributes** is selected to start with, so its **Lot Attributes** tab lists the attributes of every lot linked to the package, with a **lot #** column: the aluminum's supplier lot and heat number, the coating's expiry date and the parts' hardness.
2. Clear **Show Full Lot Attributes**. The tab is now empty: you never added an attribute to the package itself. Select it again before you go on.

:::

This is how attributes answer a customer question such as "which heat of aluminum is in this shipment?" without copying the heat number onto every lot along the way.

:::tip
Add attributes where the information arrives: the supplier lot at receiving, the hardness at inspection. Don't copy them forward to the output lots; **Show Full Lot Attributes** and the trace already link them.
:::

***

## Lot Attributes in Scripts

Most lot attributes come from somewhere other than a person typing them: a receiving screen that scans the supplier's label, a gauge, an ERP or a lab system. Four functions cover them:

| Function | What it does |
| --- | --- |
| `system.kanoa.lot.getLotAttributes` | Returns attributes filtered by lot, lot name, attribute name or value. Name and value filters are `LIKE` patterns, so `%` matches anything |
| `system.kanoa.lot.addLotAttribute` | Adds an attribute to a lot |
| `system.kanoa.lot.updateLotAttribute` | Changes an attribute, by its `lotAttributeId` |
| `system.kanoa.lot.deleteLotAttribute` | Deletes an attribute, by its `lotAttributeId` |

`addLotAttribute` always adds a new row, even when the lot already has an attribute with that name. To keep one value per name, look for it first and update it if it's there:

```python
def setLotAttribute(lotId, name, value, userId):
	'''Adds the attribute, or updates it when the lot already has one with this name'''
	info = {'lotId': lotId, 'lotAttributeName': name, 'lotAttributeValue': str(value)}
	existing = system.kanoa.lot.getLotAttributes({'lotId': lotId, 'lotAttributeName': name})
	if existing.rowCount:
		info['lotAttributeId'] = existing.getValueAt(0, 'lotAttributeId')
		return system.kanoa.lot.updateLotAttribute(info, userId)
	return system.kanoa.lot.addLotAttribute(info, userId)
```

Each function returns a value and a message, and the message is `None` when it worked. Show the message to the user when it isn't.

:::action[Write lot attributes from a script]

1. In the Ignition Designer Script Console (**Tools → Script Console**), get a user ID and the `PRM-COAT` lot's ID. Replace the `x` with your lot's number:

   ```python
   userId = system.kanoa.security.getIDPUserId({'userName': 'SYSTEM'})
   lotId = system.kanoa.utilities.getFieldValue('lotId', system.kanoa.lot.getLots({'lotName': 'RBI000000000x'}))
   ```

2. Paste `setLotAttribute` and record the supplier lot: `print setLotAttribute(lotId, 'Supplier Lot', 'PC-7731', userId)`.
3. Run the same line again with `PC-7732`. Check the **Lot Attributes** tab in Lot Manager: the lot has one `Supplier Lot`, with the new value.

:::

:::action[Find lots by attribute]

A supplier says its lot `ALC-24-0917` was out of spec. Find your lot that came from it, then every lot linked to it:

```python
data = system.kanoa.lot.getLotAttributes({'lotAttributeName': 'Supplier Lot', 'lotAttributeValue': 'ALC-24-0917'})
for row in system.dataset.toPyDataSet(data):
	print row['lotName']
	trace = system.kanoa.lot.traceLot(row['lotId'], 20)
	print sorted(set(r['lotName'] for r in system.dataset.toPyDataSet(trace)))
```

1. Run it in the Script Console. It finds your aluminum lot, then lists the lots linked to it.
2. Compare the list with the aluminum lot's trace graph, at depth `D`, from [Execute the Trace](03-execute-the-trace.md#trace-your-lots).

:::

:::tip[In a receiving screen]
After `executeLotOperation` creates the lot, look up its `lotId` with `getLots` and write the scanned supplier lot, expiry date and certificate number in the same button press, so a lot never exists without them.
:::

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

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

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