# Item History Tracking Module

This module tracks changes to item selling prices and names in the Salamanda system.

## Overview

The Item History Tracking module automatically records changes to an item's:
- Unit price
- Name

Each change is stored with:
- The item ID
- The field that was changed
- The old value
- The new value
- The user who made the change
- Timestamp of when the change occurred

## Implementation

### Database Structure

The module uses a new table called `item_history` with the following structure:

| Column     | Type                | Description                       |
|------------|---------------------|-----------------------------------|
| id         | bigint(20) unsigned | Primary key                       |
| item_id    | bigint(20) unsigned | Foreign key to items table        |
| field_name | string              | Name of the field that changed    |
| old_value  | string              | Previous value                    |
| new_value  | string              | New value                         |
| user_id    | bigint(20) unsigned | User who made the change          |
| created_at | timestamp           | When the record was created       |
| updated_at | timestamp           | When the record was last updated  |

### Models

#### ItemHistory Model

The `ItemHistory` model (`app/ItemHistory.php`) represents records in the `item_history` table and defines relationships with the `Item` and `User` models.

#### Item Model Updates

The `Item` model has been updated to:
1. Define a relationship with the `ItemHistory` model
2. Automatically track changes to the `unit_price` and `name` fields using the `booted` method

### API Endpoints

The module provides the following API endpoints:

#### Get all history records
```
GET /api/item-history
```
Returns all history records for all items.

#### Get history records filtered by field name
```
GET /api/item-history/by-field?field_name=unit_price
```
Returns history records filtered by the specified field name.

#### Get history records for a specific item
```
GET /api/items/{item_id}/history
```
Returns all history records for the specified item.

#### Create a new history record manually
```
POST /api/item-history/create
```
Creates a new history record. Requires the following parameters:
- `item_id`: ID of the item
- `field_name`: Name of the field that changed
- `old_value`: Previous value
- `new_value`: New value

Note: History records are automatically created when items are updated through the model, so this endpoint is primarily for manual tracking or testing.

## Usage

### Tracking Changes

Changes to item prices and names are automatically tracked when items are updated through the model. No additional code is required to track these changes.

### Accessing History

To access an item's history:

```php
// Get all history records for an item
$history = $item->history;

// Get only price change history
$priceHistory = $item->history()->where('field_name', 'unit_price')->get();

// Get only name change history
$nameHistory = $item->history()->where('field_name', 'name')->get();
```

### Example

```php
// Update an item's price
$item = Item::find(1);
$item->unit_price = 150.00;
$item->save();

// A history record is automatically created

// Retrieve the history
$history = $item->history()->latest()->first();
echo "Changed from {$history->old_value} to {$history->new_value}";
```

## Testing

The module includes tests in `tests/Feature/ItemHistoryTest.php` that verify:
1. Price changes are correctly tracked
2. Name changes are correctly tracked

Run the tests with:
```
php artisan test --filter=ItemHistoryTest
```
