# KYC Documents - Odoo Integration

## Overview
KYC (Know Your Customer) documents uploaded through the customer portal are now automatically sent to Odoo and attached to the customer's partner record.

## Implementation Details

### Upload Flow
1. Customer uploads KYC document via `/api/kyc/upload` endpoint
2. Document is stored locally in `storage/app/private/kyc_documents/{customer_id}/`
3. Document record is created in `customer_documents` table
4. **If customer has `odoo_partner_id`:**
   - Document is uploaded to Odoo using `OdooService->uploadAttachment()`
   - Attached to customer's partner record (`res.partner` model)
   - `odoo_attachment_id` is stored in the document record

### Odoo Integration

**Model:** `res.partner` (Customer/Partner record)

**Attachment Details:**
- **Name:** Original filename uploaded by customer
- **Content:** Base64 encoded file content
- **Description:** "KYC Document: {Document Type}"
- **Type:** binary

**Document Types:**
- National ID
- Passport
- Driver's License
- Proof of Residence
- Business Registration

### Database Schema
The `customer_documents` table includes:
- `odoo_attachment_id` (nullable) - Stores the Odoo attachment ID
- `uploaded_at` - Timestamp of upload

### Error Handling
- If Odoo upload fails, the document is still saved locally
- Error is logged but doesn't fail the entire operation
- This ensures customers can continue even if Odoo is temporarily unavailable

### Deletion Behavior
- When a KYC document is deleted from the portal, it's removed from local storage
- Odoo attachments are kept for audit trail purposes
- Deletion is logged for tracking

## Where to Find KYC Documents in Odoo

1. Navigate to the customer's partner record in Odoo
2. Go to the **Attachments** section
3. Look for attachments with description starting with "KYC Document:"

## Code Location

**Controller:** `afinet-portal-backend/app/Http/Controllers/API/KYCController.php`
**Service:** `afinet-portal-backend/app/Services/OdooService.php` (uploadAttachment method)
**Migration:** `afinet-portal-backend/database/migrations/2026_02_26_123606_update_customer_documents_for_wholesale_partners.php`

## Logging

All KYC document uploads to Odoo are logged with:
- Customer ID
- Document ID
- Document Type
- Odoo Attachment ID

Check logs at: `storage/logs/laravel.log`

## Testing

To test the integration:
1. Upload a KYC document through the portal
2. Check the logs for "KYC document uploaded to Odoo"
3. Verify in Odoo that the attachment appears on the customer's partner record
4. Confirm `odoo_attachment_id` is populated in the database

## Notes

- Only customers with `odoo_partner_id` will have documents uploaded to Odoo
- Documents are auto-verified (status: 'verified') upon upload
- Maximum file size: 5MB
- Allowed formats: PDF, JPG, JPEG, PNG
