# NextCloud Removal & ERP FileManager Migration Plan
**Date**: 2025-12-01  
**Status**: 🔄 IN PROGRESS

---

## 📋 **EXECUTIVE SUMMARY**

This document outlines the comprehensive removal of NextCloud integration from the M1 ERP system and migration to the built-in ERP FileManager system.

### **Why Remove NextCloud?**
- ✅ **Built-in FileManager**: ERP has a robust file management system
- ✅ **Simplification**: Reduce external dependencies
- ✅ **Performance**: Local file storage is faster
- ✅ **Maintenance**: One less system to maintain
- ✅ **Security**: Centralized permission system

---

## 🔍 **NEXTCLOUD INTEGRATION ANALYSIS**

### **Files Found**:

**Controllers** (0 active):
- ❌ `controllers/NextcloudController.php` - NOT FOUND (only in backups)
- ❌ `controllers/NextcloudAdminController.php` - NOT FOUND (only in backups)

**Models** (0 active):
- ❌ `models/NextcloudClient.php` - NOT FOUND (only in backups)

**Database Migrations** (4 files):
1. ✅ `database/migrations/073_nextcloud_integration.sql`
2. ✅ `database/migrations/074_grant_nextcloud_permissions.sql`
3. ✅ `database/migrations/076_nextcloud_menu_items.sql`
4. ✅ `database/migrations/077_nextcloud_app_grid_buttons.sql`

**Database Tables** (3 tables):
1. ✅ `nextcloud_config` - Configuration settings
2. ✅ `nextcloud_file_links` - File links to entities
3. ✅ `nextcloud_access_tokens` - Access tokens for sharing

**Views with NextCloud References** (12 files):
1. ✅ `views/accounting/assets/show.php`
2. ✅ `views/suppliers/edit.php`
3. ✅ `views/invoices/show.php`
4. ✅ `views/quotes/battery/show.php`
5. ✅ `views/quotes/battery/edit.php`
6. ✅ `views/projects/show.php`
7. ✅ `views/crm/opportunities_show.php`
8. ✅ `views/salesorders/show.php`
9. ✅ `views/documents/view.php`
10. ✅ `views/workorders/show.php`
11. ✅ `views/purchaseorders/show.php`
12. ✅ `views/employees/show.php`

**Documentation** (8 files):
1. ✅ `docs/NextCloud/NEXTCLOUD_INTEGRATION.md`
2. ✅ `docs/NextCloud/NEXTCLOUD_INTEGRATION_README.md`
3. ✅ `docs/NextCloud/NEXTCLOUD_QUICKSTART.md`
4. ✅ `docs/NextCloud/NEXTCLOUD_MENU_PATHS.md`
5. ✅ `docs/NextCloud/NEXTCLOUD_ADMIN_GUIDE.md`
6. ✅ `docs/NextCloud/NEXTCLOUD_APP_GRID_BUTTONS.md`
7. ✅ `docs/NextCloud/NEXTCLOUD_DOCUMENT_EDITING.md`
8. ✅ `docs/NextCloud/NEXTCLOUD_EDITING_QUICK_START.md`

**Scripts** (1 file):
1. ✅ `scripts/sync_nextcloud_users.php`

**Routes** (in `public/index.php`):
- NextCloud routes (to be identified and removed)

**Menu Items** (in database):
- Document Management menu (ID 283)
- 5 submenu items (Dashboard, Activity Log, Storage Report, User Activity, Settings)
- 1 Tools menu item (Cloud Storage)

**Permissions** (in database):
- `nextcloud.view` - View Nextcloud Documents
- `nextcloud.upload` - Upload to Nextcloud
- `nextcloud.delete` - Delete from Nextcloud
- `nextcloud.manage` - Manage Nextcloud Settings

---

## 🎯 **ERP FILEMANAGER SYSTEM**

### **Current Capabilities**:

**Database Tables**:
- ✅ `files` - File metadata and storage
- ✅ `file_folders` - Folder hierarchy
- ✅ `file_shares` - File sharing (if exists)
- ✅ `file_versions` - Version control (if exists)

**Features**:
- ✅ **File Upload/Download**: Full CRUD operations
- ✅ **Folder Management**: Hierarchical folder structure
- ✅ **Entity Linking**: Link files to customers, invoices, projects, etc.
- ✅ **Permissions**: Context-based access control
- ✅ **Search**: File search by name, tags, description
- ✅ **Metadata**: File size, MIME type, checksum, tags
- ✅ **Storage**: Local filesystem (`uploads/files/`)
- ✅ **Compression**: ZIP archive creation
- ✅ **Bulk Operations**: Move, copy, delete multiple files
- ✅ **File Viewing**: Inline viewing for supported types

**Controller**: `FileManagerController.php`
- `index()` - Main file manager interface
- `browse()` - AJAX file browsing
- `upload()` - File upload
- `download($fileId)` - File download
- `viewFile($fileId)` - Inline file viewing
- `createFolder()` - Create folder
- `deleteFile($fileId)` - Delete file
- `deleteFolder($folderId)` - Delete folder
- `moveFiles()` - Move files/folders
- `copyFiles()` - Copy files/folders
- `renameFile($fileId)` - Rename file
- `renameFolder($folderId)` - Rename folder
- `compressFiles()` - Create ZIP archive
- `search()` - Search files

**Model**: `FileManager.php`
- `getFolderTree()` - Get folder hierarchy
- `getFiles()` - Get files in folder
- `uploadFile()` - Upload file
- `createFolder()` - Create folder
- `getFile()` - Get file metadata
- `deleteFile()` - Delete file
- `canAccessContext()` - Permission checking

---

## 📝 **REMOVAL TASKS**

### **Task 1: Database Tables** ✅
**Action**: Drop NextCloud tables
```sql
DROP TABLE IF EXISTS `nextcloud_access_tokens`;
DROP TABLE IF EXISTS `nextcloud_file_links`;
DROP TABLE IF EXISTS `nextcloud_config`;
```

**Impact**: None (tables don't exist in current database)

---

### **Task 2: Database Menu Items** ✅
**Action**: Remove NextCloud menu items
```sql
-- Remove submenu items
DELETE FROM menu_items WHERE parent_id = 283;

-- Remove main menu item
DELETE FROM menu_items WHERE id = 283;

-- Remove Tools > Cloud Storage
DELETE FROM menu_items WHERE url = 'nextcloud/admin/dashboard' AND parent_id = 207;
```

**Impact**: Menu items removed from navigation

---

### **Task 3: Database Permissions** ✅
**Action**: Remove NextCloud permissions
```sql
-- Remove role permissions
DELETE FROM role_permissions WHERE permission_id IN (
    SELECT id FROM permissions WHERE module = 'nextcloud'
);

-- Remove permissions
DELETE FROM permissions WHERE module = 'nextcloud';
```

**Impact**: NextCloud permissions removed

---

### **Task 4: Migration Files** ✅
**Action**: Remove migration files
```bash
rm database/migrations/073_nextcloud_integration.sql
rm database/migrations/074_grant_nextcloud_permissions.sql
rm database/migrations/076_nextcloud_menu_items.sql
rm database/migrations/077_nextcloud_app_grid_buttons.sql
```

**Impact**: Migration files removed (won't affect existing database)

---

### **Task 5: Documentation** ✅
**Action**: Remove NextCloud documentation
```bash
rm -rf docs/NextCloud/
```

**Impact**: Documentation removed

---

### **Task 6: Scripts** ✅
**Action**: Remove NextCloud scripts
```bash
rm scripts/sync_nextcloud_users.php
```

**Impact**: Sync script removed

---

### **Task 7: View Files** ✅
**Action**: Remove NextCloud widgets from 12 view files

**Files to Update**:
1. `views/accounting/assets/show.php`
2. `views/suppliers/edit.php`
3. `views/invoices/show.php`
4. `views/quotes/battery/show.php`
5. `views/quotes/battery/edit.php`
6. `views/projects/show.php`
7. `views/crm/opportunities_show.php`
8. `views/salesorders/show.php`
9. `views/documents/view.php`
10. `views/workorders/show.php`
11. `views/purchaseorders/show.php`
12. `views/employees/show.php`

**Pattern to Remove**:
```php
<!-- Nextcloud Documents Widget -->
<?php
require_once BASE_PATH . '/controllers/NextcloudController.php';
$nc = new NextcloudController();
$nc->documentsWidget('entity_type', $entity_id);
?>
```

**Replace With**: ERP FileManager widget (to be created)

---

### **Task 8: Routes** ✅
**Action**: Remove NextCloud routes from `public/index.php`

**Routes to Remove**:
- `/nextcloud/*` routes

---

### **Task 9: Create FileManager Widget** ✅
**Action**: Create reusable FileManager widget for entity pages

**File**: `views/file_manager/entity_widget.php`

**Usage**:
```php
<!-- ERP FileManager Widget -->
<?php
require_once BASE_PATH . '/controllers/FileManagerController.php';
$fm = new FileManagerController();
$fm->entityWidget('customer', $customer['id']);
?>
```

---

### **Task 10: Migration Script** ✅
**Action**: Create script to migrate NextCloud file links to ERP FileManager

**File**: `scripts/migrate_nextcloud_to_filemanager.php`

**Purpose**: If any NextCloud file links exist, migrate them to ERP FileManager

---

## 🔄 **MIGRATION STRATEGY**

### **Phase 1: Analysis** ✅
- [x] Identify all NextCloud references
- [x] Document ERP FileManager capabilities
- [x] Create removal plan

### **Phase 2: Database Cleanup**
- [ ] Drop NextCloud tables
- [ ] Remove menu items
- [ ] Remove permissions

### **Phase 3: File Cleanup**
- [ ] Remove migration files
- [ ] Remove documentation
- [ ] Remove scripts

### **Phase 4: Code Updates**
- [ ] Remove NextCloud widgets from views
- [ ] Remove NextCloud routes
- [ ] Create FileManager entity widget

### **Phase 5: Testing**
- [ ] Test all affected modules
- [ ] Verify file upload/download works
- [ ] Verify entity file linking works

### **Phase 6: Documentation**
- [ ] Create FileManager usage guide
- [ ] Document removal process
- [ ] Update deployment guide

---

## ✅ **BENEFITS OF REMOVAL**

1. **Simplified Architecture**: One less external dependency
2. **Better Performance**: Local file storage is faster than WebDAV
3. **Unified Permissions**: Use ERP's permission system
4. **Easier Maintenance**: No need to maintain NextCloud integration
5. **Better Integration**: FileManager is built specifically for ERP
6. **Cost Savings**: No need to run NextCloud server

---

## 📊 **COMPARISON**

| Feature | NextCloud | ERP FileManager |
|---------|-----------|-----------------|
| File Upload | ✅ | ✅ |
| File Download | ✅ | ✅ |
| Folder Management | ✅ | ✅ |
| Entity Linking | ✅ | ✅ |
| Permissions | External | Built-in ✅ |
| Search | ✅ | ✅ |
| Versioning | ✅ | ⚠️ (can be added) |
| Collaboration | ✅ | ❌ |
| Online Editing | ✅ | ❌ |
| Storage | External | Local ✅ |
| Performance | Medium | Fast ✅ |
| Maintenance | High | Low ✅ |

---

## 🎯 **NEXT STEPS**

1. ✅ **Review this plan** with stakeholders
2. ⏳ **Execute removal** tasks in order
3. ⏳ **Test thoroughly** after each phase
4. ⏳ **Document changes** for users
5. ⏳ **Train users** on FileManager

---

**Status**: Ready to execute removal  
**Estimated Time**: 2-3 hours  
**Risk Level**: Low (NextCloud not actively used)

