from django.shortcuts import render # views.py from django.http import JsonResponse from django.shortcuts import render, get_object_or_404, redirect from django.views.generic import DetailView, UpdateView, ListView, CreateView from django.urls import reverse_lazy from cmc.models import Enquiry, Customer from cmc.forms import EnquiryForm from django.core.paginator import Paginator import logging logger = logging.getLogger(__name__) class EnquiryDetailView(DetailView): model = Enquiry template_name = 'enquiry_detail.html' context_object_name = 'enquiry' class EnquiryUpdateView(UpdateView): model = Enquiry form_class = EnquiryForm template_name = 'enquiry_form.html' success_url = reverse_lazy('enquiry_list') # Redirect to a list view or another appropriate view after saving def form_valid(self, form): return super().form_valid(form) def enquiry_list(request): enquiries = Enquiry.objects.all().order_by("-id") per_page = 500 paginator = Paginator(enquiries, per_page) page_number = request.GET.get('page') page_obj = paginator.get_page(page_number) return render(request, 'enquiry_list.html', {'page_obj': page_obj}) class EnquiryCreateView(CreateView): model = Enquiry form_class = EnquiryForm template_name = 'enquiry_form.html' success_url = reverse_lazy('enquiry_list') # Redirect to a list view or another appropriate view after saving def form_valid(self, form): return super().form_valid(form) def customer_autocomplete(request): if 'term' in request.POST: qs = Customer.objects.filter(name__icontains=request.POST.get('term')) logger.debug(qs) names = list(qs.values_list('name', flat=True)) return render(request, 'customer_autocomplete.html', {'names': names}) return render(request, 'customer_autocomplete.html', {'names': []}) def customer_form(request): return render(request, 'customer_form.html') ## AI generated add enquiry from django.shortcuts import render, get_object_or_404 from django.http import JsonResponse from .models import Enquiry, Customer, Contact from .forms import EnquiryForm def add_enquiry(request): if request.method == 'POST': form = EnquiryForm(request.POST) if form.is_valid(): form.save() return JsonResponse({'success': True}) else: form = EnquiryForm() return render(request, 'add_enquiry.html', {'form': form}) def customer_autocomplete(request): if 'customer' in request.GET and request.GET['customer'] != '': qs = Customer.objects.filter(name__icontains(request.GET.get('customer'))) customers = list(qs.values('id', 'name')) return render(request, 'partials/customer_list.html', {'customers': qs}) return render(request, 'partials/customer_list.html', {'customers': []}) def load_contacts(request): customer_id = request.GET.get('customer_id') contacts = Contact.objects.filter(customer_id=customer_id).values('id', 'first_name', 'last_name') return render(request, 'partials/contact_dropdown.html', {'contacts': contacts}) ## end AI generated add enquiry from django.core.paginator import Paginator from .models import Customer, CustomerCategory def customer_list(request): customer_categories = CustomerCategory.objects.all().order_by('name') selected_category_id = request.GET.get('showonly') customer_queryset = Customer.objects.select_related('customer_category', 'country').order_by('name') if selected_category_id: try: selected_category_id = int(selected_category_id) customer_queryset = customer_queryset.filter(customer_category_id=selected_category_id) except ValueError: # Handle invalid category ID if necessary, or ignore pass paginator = Paginator(customer_queryset, 25) # Show 25 customers per page page_number = request.GET.get('page') page_obj = paginator.get_page(page_number) context = { 'page_obj': page_obj, 'customer_categories': customer_categories, 'selected_category_id': selected_category_id, } return render(request, 'cmc/customer_list.html', context) # You'll also need views for 'customer_detail', 'customer_edit', 'customer_add' # For example: # def customer_detail(request, pk): # customer = get_object_or_404(Customer, pk=pk) # return render(request, 'cmc/customer_detail.html', {'customer': customer}) def enquiry_detail(request, pk): enquiry = get_object_or_404( Enquiry.objects.select_related( 'customer', 'contact', 'status', 'user', 'state', 'country', 'principle', 'billing_address', 'shipping_address', 'billing_address__state', 'billing_address__country', # Pre-fetch for addresses 'shipping_address__state', 'shipping_address__country' ), pk=pk ) # Fetch related objects # Ensure related_names are set up correctly in your models quotes = enquiry.quotes.select_related('document').order_by('-document__revision', '-created') # Assuming 'document' and 'created' exist invoices = enquiry.invoices.select_related('job', 'customer', 'document').order_by('-document__revision', '-issue_date') jobs = enquiry.jobs.all().order_by('-id') # Or appropriate order order_acknowledgements = enquiry.order_acknowledgements.select_related('job', 'document').order_by('-id') # Emails (assuming a ManyToManyField from Enquiry to Email or vice-versa) emails = enquiry.emails.all().order_by('-udate') # Assuming 'udate' for date # Principle Contacts (this depends heavily on your PrincipleContact model structure) # Example: if Principle has a related_name 'contacts' from PrincipleContact principle_contacts = [] if enquiry.principle: # This is a placeholder. Adjust based on your actual model relationship. # For example, if PrincipleContact has a ForeignKey to Principle: # principle_contacts = PrincipleContact.objects.filter(principle=enquiry.principle) # Or if Principle has a ManyToMany to Contact through PrincipleContact: # principle_contacts = enquiry.principle.contacts.all() # if 'contacts' is the M2M field pass # Replace with actual logic # Packing Lists (complex in CakePHP, assuming Job has a related_name 'packing_lists') packing_lists = [] for job in jobs: # Assuming PackingList has a ForeignKey to Job and a 'document' relation packing_lists.extend(job.packing_lists.select_related('document').all()) context = { 'enquiry': enquiry, 'quotes': quotes, 'invoices': invoices, 'jobs': jobs, 'order_acknowledgements': order_acknowledgements, 'emails': emails, 'principle_contacts': principle_contacts, # You'll need to fetch this based on your models 'packing_lists': packing_lists, 'page_title': f"Enquiry: {enquiry.title}", } return render(request, 'cmc/enquiry_detail.html', context)