You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

335 lines
13 KiB

from django.contrib import messages
from django.contrib.auth import authenticate, login as auth_login, logout
from django.shortcuts import render, redirect, get_object_or_404
from .models import skyonnAdmin, ClientDetails, SubcompanyDetails, Contact
from django.contrib.auth.hashers import make_password
from django.http import HttpResponse
import random
import string
from django.http import JsonResponse
import json
from django.core.serializers.json import DjangoJSONEncoder
from django.views.decorators.csrf import csrf_exempt
def home(request):
return render(request, 'skyonnadmin/home.html')
def create_admin(request):
if request.method == 'POST':
# Retrieve data from the form
first_name = request.POST['first_name']
last_name = request.POST['last_name']
phone_number = request.POST['phone']
phone_number_2 = request.POST.get('phone_number_2')
company_email = request.POST['company_email']
personal_email = request.POST.get('personal_email')
address = request.POST['address']
work_location = request.POST['work_location']
role = 'admin'
password = request.POST['password'] # Assuming there's a password field in the form
hashed_password = make_password(password)
# Generate admin ID based on the last ID in the database
last_admin = skyonnAdmin.objects.order_by('-id').first()
if last_admin:
last_id = int(last_admin.admin_id[4:]) # Extract the serial number part and convert to int
new_id = last_id + 1
else:
new_id = 1
admin_id = f'SKYA{new_id:03d}' # Construct the new admin ID
# Create the admin user
admin = skyonnAdmin.objects.create(
first_name=first_name,
last_name=last_name,
phone=phone_number,
phone_number_2=phone_number_2,
company_email=company_email,
personal_email=personal_email,
address=address,
work_location=work_location,
role=role,
admin_id=admin_id,
password=hashed_password
)
admin.backend = 'user.backend.PhoneAuthenticationBackend'
auth_login(request, admin)
return redirect('dashboard', admin_id=admin.admin_id)
else:
return render(request, 'skyonnadmin/home.html')
# # Redirect to the admin signup page
# return redirect('home')
#
# return render(request, 'skyonnadmin/home.html')
def login(request):
if request.method == 'POST':
phone = request.POST.get('phone')
password = request.POST.get('password')
print("Phone:", phone)
# Retrieve the admin object based on the provided phone number
admin = skyonnAdmin.objects.filter(phone=phone).first()
print(admin)
if admin is not None:
# Check if the provided password matches the stored password
if admin.check_password(password):
# Password is correct, login successful
# You can implement your login logic here
print("Admin ID:", admin.admin_id)
return redirect('dashboard', admin_id=admin.admin_id)
else:
# Password is incorrect
messages.error(request, 'Invalid password.')
else:
# Admin with the provided phone number does not exist
messages.error(request, 'No admin found for the provided phone number.')
return render(request, 'skyonnadmin/home.html')
def admin_dashboard(request, admin_id):
admin = get_object_or_404(skyonnAdmin, admin_id=admin_id)
# Get the manager's first name
first_name = admin.first_name.capitalize()
full_name = f"{first_name}"
print(full_name)
return render(request, 'skyonnadmin/admin_dashboard.html', {'first_name': full_name, 'admin_id': admin_id})
# Admin dashboard logic goes here
def logout_view(request):
logout(request)
return redirect('admin_home')
def client_details(request):
return render(request, 'skyonnadmin/client_details.html')
def add_subclient(request):
admin_id = request.GET.get('admin_id', None)
clients = ClientDetails.objects.all()
return render(request, 'skyonnadmin/add_subclient.html', {'clients': clients,'admin_id': admin_id})
def add_client(request):
admin_id = request.GET.get('admin_id', None)
print(admin_id)
clients = ClientDetails.objects.all()
return render(request, 'skyonnadmin/add_client.html', {'clients': clients,'admin_id': admin_id})
def existing_client(request):
return render(request, 'skyonnadmin/existing_client.html')
def save_details(request):
if request.method == 'POST':
# Extract data from the first form
parent_company = request.POST.get('ParentCompany', '')
location = request.POST.get('Location', '')
department = request.POST.get('Department', '')
gst_no = request.POST.get('GSTNo', '')
address = request.POST.get('Address', '')
# Extract data from the second form
first_name = request.POST.get('Admin_FirstName', '')
last_name = request.POST.get('Admin_LastName', '')
phone_no1 = request.POST.get('Admin_PhoneNo1', '')
phone_no2 = request.POST.get('Admin_PhoneNo2', '')
company_email = request.POST.get('Admin_CompanyEmail', '')
designation = request.POST.get('Admin_Designation', '')
admin_department = request.POST.get('Admin_Department', '')
admin_location = request.POST.get('Admin_Location', '')
print(admin_location)
client_id = generate_client_id(parent_company)
# Save data into the database
job_details = ClientDetails(
parent_company=parent_company,
location=location,
department=department,
gst_no=gst_no,
address=address,
client_id=client_id,
admin_first_name=first_name,
admin_last_name=last_name,
admin_phone_no1=phone_no1,
admin_phone_no2=phone_no2,
admin_company_email=company_email,
admin_designation=designation,
admin_department=admin_department,
admin_location=admin_location
)
job_details.save()
admin_id = request.GET.get('admin_id', None)
print(admin_id)
return JsonResponse({'status': 'success', 'message': 'Client saved successfully.'})
else:
return HttpResponse('Invalid request method')
@csrf_exempt
def save_details_subcompany(request):
print("00came")
if request.method == 'POST':
# Extract data from the first form
parent_company = request.POST.get('ParentCompany', '')
location = request.POST.get('Location', '')
department = request.POST.get('Department', '')
gst_no = request.POST.get('GSTNo', '')
address = request.POST.get('Address', '')
sub_company = request.POST.get('SubCompany', '')
# Extract data from the second form
first_name = request.POST.get('Admin_FirstName', '')
last_name = request.POST.get('Admin_LastName', '')
phone_no1 = request.POST.get('Admin_PhoneNo1', '')
phone_no2 = request.POST.get('Admin_PhoneNo2', '')
company_email = request.POST.get('Admin_CompanyEmail', '')
designation = request.POST.get('Admin_Designation', '')
admin_department = request.POST.get('Admin_Department', '')
admin_location = request.POST.get('Admin_Location', '')
print(admin_location)
client_id = generate_client_id(parent_company)
# Save data into the database
job_details = SubcompanyDetails(
parent_company=parent_company,
sub_company=sub_company,
location=location,
department=department,
gst_no=gst_no,
address=address,
client_id=client_id,
admin_first_name=first_name,
admin_last_name=last_name,
admin_phone_no1=phone_no1,
admin_phone_no2=phone_no2,
admin_company_email=company_email,
admin_designation=designation,
admin_department=admin_department,
admin_location=admin_location
)
job_details.save()
admin_id = request.GET.get('admin_id', None)
return JsonResponse({'status': 'success', 'message': 'subclient saved successfully.'})
else:
return JsonResponse({'status': 'error', 'message': 'Invalid request method'}, status=400)
def get_combined_details(request):
parent_company = request.GET.get('parent_company')
location = request.GET.get('location')
# Fetch data from ClientDetails and SubcompanyDetails tables based on filters
client_details = ClientDetails.objects.filter(parent_company=parent_company, location=location)
subcompany_details = SubcompanyDetails.objects.filter(parent_company=parent_company, location=location)
# Combine data from both tables
combined_data = []
# Add client details to combined_data
for detail in client_details:
combined_data.append({
'parent_company': detail.parent_company,
'location': detail.location,
'department': detail.department,
'gst_no': detail.gst_no,
'address': detail.address,
'client_id': detail.client_id,
'admin_first_name': detail.admin_first_name,
'admin_last_name': detail.admin_last_name,
'admin_phone_no1': detail.admin_phone_no1,
'admin_phone_no2': detail.admin_phone_no2,
'admin_company_email': detail.admin_company_email,
'admin_designation': detail.admin_designation,
'admin_department': detail.admin_department,
'admin_location': detail.admin_location,
# Add contacts data if needed
})
# Add subcompany details to combined_data
for detail in subcompany_details:
combined_data.append({
'parent_company': detail.parent_company,
'sub_company': detail.sub_company,
'location': detail.location,
'department': detail.department,
'gst_no': detail.gst_no,
'address': detail.address,
'client_id': detail.client_id,
'admin_first_name': detail.admin_first_name,
'admin_last_name': detail.admin_last_name,
'admin_phone_no1': detail.admin_phone_no1,
'admin_phone_no2': detail.admin_phone_no2,
'admin_company_email': detail.admin_company_email,
'admin_designation': detail.admin_designation,
'admin_department': detail.admin_department,
'admin_location': detail.admin_location,
# Add contacts data if needed
})
print(combined_data)
return JsonResponse(combined_data, safe=False)
def get_locations(request):
print(0)
if request.method == 'GET' and 'parent_company' in request.GET:
print(1)
parent_company = request.GET.get('parent_company')
locations = SubcompanyDetails.objects.filter(parent_company=parent_company).values_list('location', flat=True)
serialized_data = json.dumps(list(locations), cls=DjangoJSONEncoder)
return JsonResponse(serialized_data, safe=False)
else:
return JsonResponse({'error': 'Invalid request'})
def generate_client_id(parent_company):
# Extract first 4 letters from parent_company and convert to uppercase
prefix = parent_company[:4].upper()
# Generate 4 random integers
suffix = ''.join(random.choices(string.digits, k=4))
# Concatenate prefix and suffix to form the client_id
client_id = prefix + suffix
return client_id
def get_contacts(request):
client_id = request.GET.get('client_id')
department = request.GET.get('department')
client_details = ClientDetails.objects.filter(client_id=client_id).first()
if department == 'hiring_managers':
contacts = client_details.hiring_manager_contacts.all()
elif department == 'accounts':
contacts = client_details.accounts_contacts.all()
elif department == 'human_resources':
contacts = client_details.hr_contacts.all()
elif department == 'administration':
contacts = client_details.admin_contacts.all()
else:
contacts = []
contacts_data = [{
"name": f"{contact.first_name} {contact.last_name}",
"designation": contact.designation,
"department": contact.department,
"phone": contact.phone_no1,
"email": contact.company_email,
"location": contact.client_id.location, # Adjust based on your model relationships
} for contact in contacts]
return JsonResponse({"contacts": contacts_data})