# Company Settings Improvements

## Overview
Enhanced the Company Settings page to use structured address fields and integrate with the locations system for multi-location management.

## Implementation Date
November 27, 2025

## Changes Made

### 1. Structured Address Fields
**Before:** Single textarea for address
**After:** Separate fields for proper address management

New fields:
- **Address Line 1** - Street address, P.O. box
- **Address Line 2** - Apartment, suite, unit, building, floor, etc.
- **City** - City name
- **State/Province** - State or province
- **ZIP/Postal Code** - Postal code
- **Country** - Country (defaults to "United States")

### 2. Multiple Locations Support
Added a "Business Locations" section to the company settings page that:
- Shows preview of existing locations (up to 5)
- Links to full locations management at `/settings/locations`
- Displays location name, code, city/state, and status
- Shows helpful message if no locations configured yet

### 3. Database Fields
Added to `system_settings` table:
- `company_address_line1`
- `company_address_line2`
- `company_city`
- `company_state`
- `company_zip`
- `company_country`

**Note:** Legacy `company_address` field retained for backwards compatibility.

### 4. Locations Table Structure
The existing `locations` table already has proper fields:
- `name` - Location name
- `code` - Unique location code
- `address` - Street address
- `city` - City
- `state` - State
- `zip` - ZIP code
- `country` - Country
- `square_footage` - Facility size
- `phone` - Location phone
- `manager_id` - Location manager
- `status` - active/inactive
- Plus operating parameters (work_days_per_week, shifts_per_day, etc.)

## Files Modified

1. `/views/settings/company.php`
   - Lines 48-92: Added structured address fields
   - Lines 202-264: Added locations management section

2. `/models/SystemSettings.php`
   - Lines 214-220: Added new address fields to getCompanyInfo()

3. `/public/index.php`
   - Line 1231: Added route for `/settings/locations`

## Benefits

### For Users
✅ **Better Data Entry** - Separate fields ensure proper address formatting
✅ **Address Validation** - Easier to validate individual components
✅ **Multi-Location** - Easy access to manage multiple business locations
✅ **Consistency** - Address structure matches locations table format

### For System
✅ **Database Normalization** - Structured data instead of free text
✅ **Integration Ready** - Address fields match locations table
✅ **Reporting** - Easier to group/filter by city, state, country
✅ **API Compatibility** - Structured data for integrations

## Usage

### Updating Company Address
1. Navigate to **Settings > Company Information**
2. Fill in structured address fields:
   - Address Line 1 (required for full address)
   - Address Line 2 (optional)
   - City
   - State/Province  
   - ZIP/Postal Code
   - Country
3. Click "Save Company Info"

### Managing Multiple Locations
1. From **Settings > Company Information**
2. Scroll to "Business Locations" section
3. Click "Manage Locations" button
4. Add/edit locations with full address details
5. Each location can have operating parameters and capacity settings

### Access Locations Directly
- URL: `/locations` or `/settings/locations`
- Both routes lead to the same locations management page

## Migration Notes

### Existing Addresses
- If `company_address` exists as single textarea value, manually split into new fields
- New structured fields take precedence in forms
- Legacy field preserved in database for backwards compatibility

### Data Migration (Optional)
If you want to split existing address data:
```sql
-- Example: Update from legacy single-line address
-- Manually parse and update based on your address format
UPDATE system_settings 
SET setting_value = 'Value' 
WHERE setting_key IN (
    'company_address_line1',
    'company_city',
    'company_state',
    'company_zip'
);
```

## Locations vs Company Address

**Company Address** (Settings > Company)
- Primary business address
- Used on invoices, quotes, official documents
- Stored in `system_settings` table
- Single "headquarters" location

**Business Locations** (Settings > Locations)
- Multiple physical locations
- Warehouses, offices, stores, facilities
- Stored in `locations` table
- Each with own address and operating parameters
- Used for inventory, capacity planning, data scoping

## Future Enhancements

Potential improvements:
- [ ] Auto-complete for city/state based on ZIP
- [ ] Address validation using Google Maps API
- [ ] Default location selection (mark one as headquarters)
- [ ] Import locations from CSV
- [ ] Location hierarchy (regions > locations)
- [ ] Custom fields per location type

---
**Status:** ✅ Implemented and Active
