#!/usr/bin/env python3
"""
Execute the comprehensive employee import
"""
import csv
import sys
import os
import time
from datetime import datetime
# Add the src directory to Python path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
from odoo_mcp.odoo_client import get_odoo_client
from csv_to_odoo_field_mapping import map_csv_row_to_odoo_employee
def get_existing_employee_names(client):
"""Get list of existing employee names in Odoo"""
print("Fetching existing employees...")
employees = client.search_read('hr.employee', [], ['name'], limit=1000)
existing_names = set(emp['name'] for emp in employees)
print(f"Found {len(existing_names)} existing employees")
return existing_names
def get_department_mapping(client):
"""Get mapping of department names to IDs"""
departments = client.search_read('hr.department', [], ['name'])
dept_mapping = {}
for dept in departments:
dept_mapping[dept['name'].lower()] = dept['id']
return dept_mapping
def get_country_mapping(client):
"""Get mapping of country names to IDs"""
countries = client.search_read('res.country', [], ['name'])
country_mapping = {}
for country in countries:
country_mapping[country['name'].lower()] = country['id']
return country_mapping
def create_department_if_needed(client, project_name, dept_mapping):
"""Create department if it doesn't exist"""
if not project_name:
return None
project_key = project_name.lower().strip()
if project_key in dept_mapping:
return dept_mapping[project_key]
try:
dept_data = {'name': project_name.strip()}
result = client.create_record('hr.department', dept_data)
if isinstance(result, list) and len(result) > 0:
dept_id = result[0]
dept_mapping[project_key] = dept_id
print(f" Created department: {project_name} (ID: {dept_id})")
return dept_id
except Exception as e:
print(f" Error creating department '{project_name}': {e}")
return None
def find_country_id(nationality, country_mapping):
"""Find country ID from nationality"""
if not nationality:
return None
nationality_mappings = {
'bangladesh': 'bangladesh',
'bangladeshi': 'bangladesh',
'india': 'india',
'indian': 'india',
'pakistan': 'pakistan',
'pakistani': 'pakistan',
'nepal': 'nepal',
'nepali': 'nepal',
'egypt': 'egypt',
'egyptian': 'egypt',
'sudan': 'sudan',
'sudanese': 'sudan',
'saudi arabia': 'saudi arabia',
'saudi': 'saudi arabia',
}
nat_key = nationality.lower().strip()
country_name = nationality_mappings.get(nat_key, nat_key)
return country_mapping.get(country_name)
def create_employee_comprehensive(client, row, dept_mapping, country_mapping):
"""Create employee with comprehensive field mapping"""
try:
# Get mapped employee data
employee_data = map_csv_row_to_odoo_employee(row)
# Handle department mapping
if row.get('Project'):
dept_id = create_department_if_needed(client, row['Project'], dept_mapping)
if dept_id:
employee_data['department_id'] = dept_id
# Handle nationality/country mapping
if row.get('Nationality'):
country_id = find_country_id(row['Nationality'], country_mapping)
if country_id:
employee_data['country_of_birth'] = country_id
# Generate work email if not provided
if not employee_data.get('work_email') and employee_data.get('name'):
name_parts = employee_data['name'].lower().split()
if len(name_parts) > 1:
email = f"{name_parts[0]}.{name_parts[-1]}@barqapp.com"
else:
email = f"{name_parts[0]}@barqapp.com"
employee_data['work_email'] = email
# Remove fields that might cause issues with references
safe_data = {}
for field, value in employee_data.items():
if field in ['parent_id']: # Skip supervisor field for now
continue
safe_data[field] = value
print(f" Creating with {len(safe_data)} fields...")
result = client.create_record('hr.employee', safe_data)
if isinstance(result, list) and len(result) > 0:
return result[0]
else:
print(f" Failed: {result}")
return None
except Exception as e:
print(f" Error: {str(e)}")
return None
def execute_comprehensive_import(batch_size=25):
"""Execute the comprehensive employee import"""
csv_file = '/Users/ramizmohamed/Downloads/Asset Managment - COURIER MASTER SHEET (1).csv'
try:
print("=== STARTING COMPREHENSIVE EMPLOYEE IMPORT ===")
print()
# Initialize client
print("Connecting to Odoo...")
client = get_odoo_client()
print("✓ Connected successfully!")
# Get existing data
existing_names = get_existing_employee_names(client)
dept_mapping = get_department_mapping(client)
country_mapping = get_country_mapping(client)
print(f"✓ Loaded {len(dept_mapping)} departments")
print(f"✓ Loaded {len(country_mapping)} countries")
print()
# Process CSV
created_count = 0
failed_count = 0
skipped_count = 0
with open(csv_file, 'r', encoding='utf-8') as file:
reader = csv.DictReader(file)
for row_num, row in enumerate(reader, start=2):
if row['Status'].strip().lower() == 'active':
employee_name = row['Name'].strip()
# Skip if already exists
if employee_name in existing_names:
print(f"Row {row_num}: Skipping {employee_name} (exists)")
skipped_count += 1
continue
print(f"Row {row_num}: Processing {employee_name}")
employee_id = create_employee_comprehensive(client, row, dept_mapping, country_mapping)
if employee_id:
created_count += 1
existing_names.add(employee_name)
print(f" ✓ Created ID: {employee_id}")
else:
failed_count += 1
print(f" ✗ Failed")
# Progress update
total_processed = created_count + failed_count + skipped_count
if total_processed % batch_size == 0:
print(f"\n--- Batch Complete ---")
print(f"Created: {created_count}, Failed: {failed_count}, Skipped: {skipped_count}")
print("Pausing 2 seconds...\n")
time.sleep(2)
print(f"\n=== IMPORT COMPLETE ===")
print(f"✓ Successfully created: {created_count} employees")
print(f"✗ Failed to create: {failed_count} employees")
print(f"⚠ Skipped (existed): {skipped_count} employees")
print(f"📊 Total processed: {created_count + failed_count + skipped_count} active employees")
except Exception as e:
print(f"Error during import: {str(e)}")
import traceback
traceback.print_exc()
if __name__ == '__main__':
execute_comprehensive_import()