> ## Documentation Index
> Fetch the complete documentation index at: https://docs.serial.okos.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Work Orders

> An in-depth look at the structure and purpose of the `work_orders` table in the Serial database

## Introduction to the Work\_Orders Table

The work\_orders table is designed to store and manage work orders within the Serial application. This table plays a crucial role in tracking manufacturing processes, approvals, and associated components and part numbers.

## Table Structure

The work\_orders table is structured to capture comprehensive information about each work order. Here's a detailed breakdown of its columns:

| Column Name           | Data Type                | Constraints               | Description                                          |
| --------------------- | ------------------------ | ------------------------- | ---------------------------------------------------- |
| id                    | uuid                     | primary key, not null     | Unique identifier for the work order                 |
| company\_id           | uuid                     | not null, foreign key     | Reference to the associated company                  |
| wo\_number            | bigint                   | not null                  | Unique work order number                             |
| name                  | text                     | not null                  | Name of the work order                               |
| description           | text                     |                           | Optional description of the work order               |
| component\_id         | uuid                     | not null, foreign key     | Reference to the associated component                |
| part\_number\_id      | uuid                     | foreign key               | Optional reference to the associated part number     |
| created\_at           | timestamp with time zone | not null, default now()   | Timestamp of work order creation                     |
| created\_by\_user\_id | uuid                     | not null                  | ID of the user who created the work order            |
| status                | text                     | not null, default 'DRAFT' | Current status of the work order                     |
| phase                 | text                     |                           | Optional phase information                           |
| quantity              | bigint                   | not null                  | Quantity associated with the work order              |
| approvals             | jsonb                    | not null, default '{}'    | JSON object storing approval information             |
| assignee\_user\_id    | uuid                     | foreign key               | Optional reference to the assigned user              |
| deadline              | timestamp with time zone |                           | Optional deadline for the work order                 |
| activity\_log         | jsonb\[]                 | not null                  | Array of JSON objects logging work order activities  |
| last\_edited\_at      | timestamp with time zone |                           | Timestamp of the last edit to the work order         |
| attachments           | jsonb\[]                 | not null                  | Array of JSON objects storing attachment information |

## Usage and Functionality

The work\_orders table is designed to be a comprehensive record of manufacturing tasks and their progress. Here are some key points about its usage:

1. **Work Order Management**: The table allows for the creation, tracking, and updating of work orders throughout their lifecycle, from draft to production.

2. **Approval Tracking**: The approvals column stores JSON data to manage the approval process, allowing for multiple approvers and their status.

3. **Activity Logging**: The activity\_log column maintains a detailed history of actions and changes related to each work order, enhancing traceability.

4. **File Attachments**: The attachments column supports storing multiple files associated with a work order, such as documentation or images.

5. **Relational Integrity**: Foreign key relationships ensure that work orders are properly associated with companies, components, part numbers, and users.

## Notes

* The wo\_number is automatically incremented, ensuring a unique identifier for each work order within the system.

* The status column allows for tracking the progress of work orders, with 'DRAFT' as the default initial status.

* The activity\_log and attachments columns use JSONB arrays, providing flexibility in storing varying amounts of data for each work order.

```typescript
// Example of creating a new work order
const newWorkOrder = {
  company_id: "company-uuid",
  name: "New Assembly Process",
  component_id: "component-uuid",
  created_by_user_id: "user-uuid",
  quantity: 100,
  // Other fields...
};

const { data, error } = await supabase.from("work_orders").insert(newWorkOrder);
```

By leveraging the work\_orders table, the Serial application can efficiently manage and track manufacturing processes, ensuring proper documentation, approval workflows, and traceability throughout the production lifecycle.
