# Dolibarr Donations Module Database Fix

## Problem
The Dolibarr system was showing a database error when trying to access the accounting/billing index page:

```
DB_ERROR_NOSUCHTABLE
Information for latest database access request error: Table 'dolibarr2.llx_don' doesn't exist
```

This error occurred when the system tried to access `/compta/index.php` and attempted to query the donations table to display recent donation activity in the accounting dashboard.

## Root Cause
The donations module was enabled in Dolibarr but the required database tables were missing from the `dolibarr2` database. The donations module requires two main tables to function properly:
1. `llx_don` - Main donations table
2. `llx_don_extrafields` - Custom fields for donations

## Solution Applied
Created all missing donations module tables in the `dolibarr2` database:

### 1. Main Donations Table
**`llx_don`** - Core table for donation records
- **Primary fields**: rowid (auto-increment), ref, entity, tms, fk_statut
- **Donation details**: datedon, amount, fk_payment, paid
- **Donor information**: firstname, lastname, societe, address, zip, town, country, fk_country
- **Contact details**: email, phone, phone_mobile
- **Settings**: public (visibility), fk_projet (project association)
- **System fields**: datec, fk_user_author, fk_user_modif, date_valid, fk_user_valid
- **Additional**: note_private, note_public, model_pdf, import_key, extraparams, ip

### 2. Custom Fields Support
**`llx_don_extrafields`** - Extended custom fields for donations
- **Fields**: rowid, tms, fk_object, import_key
- **Purpose**: Allows adding custom fields to donation records

## Database Schema Details

### Table Structure
```sql
CREATE TABLE llx_don (
    rowid int(11) AUTO_INCREMENT PRIMARY KEY,
    ref varchar(30),
    entity int(11) NOT NULL DEFAULT 1,
    tms timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    fk_statut smallint(6) NOT NULL DEFAULT 0,
    datedon datetime,
    amount double(24,8) DEFAULT 0,
    fk_payment int(11),
    paid smallint(6) NOT NULL DEFAULT 0,
    fk_soc int(11),
    firstname varchar(50),
    lastname varchar(50),
    societe varchar(50),
    -- ... additional fields for address, contact, etc.
);
```

### Indexes and Constraints
- Primary key on `rowid`
- Unique index on `(ref, entity)` for reference uniqueness
- Indexes on `fk_soc`, `fk_projet`, `fk_user_author`, `fk_user_valid`, `entity`, `fk_statut` for performance
- Foreign key constraints to `llx_user` and `llx_societe` tables

## Commands Executed
```bash
# Create all donation tables
docker exec -i cbeezai-dolibarr_db2-1 mysql -u root -proot dolibarr2 < create_donation_tables.sql
```

## Verification Results
- ✅ All donation tables created successfully
- ✅ Table structures match the working [dolibarr](file:///root/cbeezai/php-code/dev/setup/nginx/dolibarr) database
- ✅ All required fields present including rowid, lastname, firstname, societe, datedon, tms, amount, fk_statut
- ✅ Proper field types and constraints applied
- ✅ Indexes and foreign keys configured correctly
- ✅ Database schema now consistent across both instances

## Final Database State
Both databases now contain:
1. **llx_don** - Main donations table with complete structure
2. **llx_don_extrafields** - Custom fields support
3. **llx_payment_donation** - Payment tracking (already existed)

## Expected Functionality
The donations module should now be fully functional with:
- ✅ Creating and managing donation records
- ✅ Tracking donor information (individuals and companies)
- ✅ Managing donation payments and status
- ✅ Project association for donations
- ✅ Public/private donation visibility settings
- ✅ Custom fields support
- ✅ Integration with accounting/billing dashboard
- ✅ PDF generation and document management

## Files Created
- `/root/cbeezai/create_donation_tables.sql` - Complete fix script
- `/root/cbeezai/DONATIONS_FIX_SUMMARY.md` - This summary document

The donations module database issues have been completely resolved. The system should now work without any DB_ERROR_NOSUCHTABLE errors when accessing the accounting dashboard at `/compta/index.php` or any other donations functionality.