from django.shortcuts import render, get_object_or_404 from .decorators import * from billing.models import * from billing.add.views import * from .models import * from django.db.models import Q from django.http import Http404 from django.db.models import OuterRef, Subquery # Create your views here. @customer_login_required def redirect_osimenu(request): user_email = request.user.email key = 'pbkdf2_sha256600000' url = f"https://osimenu.com/login/{user_email}/{key}/" return redirect(url) @customer_login_required def redirect_osicard(request): user_email = request.user.email key = 'pbkdf2_sha256600000' url = f"https://mybusinesscardpage.com/login/{user_email}/{key}/" return redirect(url) # ADD @customer_login_required def customer_add_ticket(request, *args, **kwargs): customer_products = OrderItem.objects.filter(order__status='Completed', active__in=[True, None], item__type = 'Product', order__customer = request.user.customerprofile) customer_projects = Project.objects.filter(customer=request.user.customerprofile) if request.method == 'POST': project = None product = None department = None regarding = 'General/Account/Billing' if request.POST.get('project') and request.POST.get('regarding') == 'Project': project = get_object_or_404(Project, id=request.POST.get('project')) department = project.project_type.department regarding = 'Project/Product' elif request.POST.get('product') and request.POST.get('regarding') == 'Product': product = get_object_or_404(Item, id=request.POST.get('product')) department = product.item_type.department regarding = 'Project/Product' ticket = Ticket( status = 'Open', customer = request.user.customerprofile, title = request.POST.get('title'), description = request.POST.get('description'), regarding = regarding, project = project, product = product, department = department, opened_by = request.user, opened_date = datetime.now() ) ticket.save() ticket_status_update = TicketStatusUpdate( ticket = ticket, status = 'Open', added_by = request.user, date_added = datetime.now() ) ticket_status_update.save() for file in request.FILES.getlist('files'): ticket_attachment = TicketAttachment( ticket=ticket, file=file ) ticket_attachment.save() return redirect('customerticketdetails', ticket_number=ticket.ticket_number) context = { 'customer_products': customer_products, 'customer_projects': customer_projects, } return render(request, 'add_templates/customer-add-ticket.html', context) @customer_login_required def customer_add_ticket_update(request, ticket_id): ticket = get_object_or_404(Ticket, id=ticket_id) if request.method == 'POST': ticket_update = TicketUpdate( ticket = ticket, description = request.POST.get('description'), added_by = request.user, date_added = datetime.now(), ) ticket_update.save() for file in request.FILES.getlist('files'): ticket_attachment = TicketAttachment( ticket_update=ticket_update, file=file ) ticket_attachment.save() return redirect('customerticketdetails', ticket_number=ticket.ticket_number) context = { 'ticket': ticket, } return render(request, 'add_templates/customer-add-ticket.html', context) @customer_login_required def customer_add_ticket_update_reaction(request, reaction_type, ticketupdate_id): ticket_update = get_object_or_404(TicketUpdate, id=ticketupdate_id) existing_reaction = TicketUpdateReaction.objects.filter(ticket_update=ticket_update, customer=request.user).first() if existing_reaction: # If the existing reaction type is equal to the new reaction, delete it if existing_reaction.reaction == reaction_type: existing_reaction.delete() else: # If not, delete all previous reactions and add a new one TicketUpdateReaction.objects.filter(ticket_update=ticket_update, customer=request.user).delete() reaction = TicketUpdateReaction.objects.create( ticket_update=ticket_update, reaction=reaction_type, customer=request.user ) else: # If there's no existing reaction, simply add the new one reaction = TicketUpdateReaction.objects.create( ticket_update=ticket_update, reaction=reaction_type, customer=request.user ) return redirect('customerticketdetails', ticket_number=ticket_update.ticket.ticket_number) # LISTING @customer_login_required def customer_invoices(request, *args, **kwargs): invoices = Invoice.objects.filter(order__customer = request.user.customerprofile) context = { 'invoices' : invoices, } return render(request, 'listing_pages/customer-invoices.html', context) @customer_login_required def all_products(request, *args, **kwargs): context = { } return render(request, 'listing_pages/products.html', context) @customer_login_required def customer_orders(request, *args, **kwargs): customer = request.user.customerprofile orders = Order.objects.filter(customer=customer, orderstatus__isnull=False).order_by('-order_id') context = { 'orders': orders, } return render(request, 'listing_pages/customer-orders.html', context) def customer_order_details(request, order_id): order = get_object_or_404(Order, id=order_id) order_items = OrderItem.objects.filter(order=order).order_by('-id') order_item_ids = order_items.values_list('item_id', flat=True) services = Item.objects.filter(Q(type='Service') & (Q(customer=order.customer) | Q(customer__isnull=True))).exclude(id__in=order_item_ids).order_by('-id') products = Item.objects.filter(Q(type='Product') & (Q(customer=order.customer) | Q(customer__isnull=True))).exclude(id__in=order_item_ids).order_by('-id') payments = OrderPayment.objects.filter(order = order).order_by('-id') context = { 'order' : order, 'order_items' : order_items, 'services': services, 'payments' : payments, } return render(request, 'details_templates/inner-customer-order.html', context) @customer_login_required def customer_tickets(request, *args, **kwargs): open_tickets = Ticket.objects.filter(Q(status__in=['Open', 'Working On']) & Q(customer=request.user.customerprofile)) closed_tickets = Ticket.objects.filter(Q(status__in=['Closed']) & Q(customer=request.user.customerprofile)).order_by('-id') for ticket in open_tickets: unread_updates_count = 0 last_update = ticket.ticketupdate_set.exclude(added_by=request.user).order_by('-date_added').first() if last_update: last_update_added_by = last_update.added_by last_update_date_added = last_update.date_added else: last_update_added_by = None last_update_date_added = None for ticket_update in ticket.ticketupdate_set.exclude(added_by=request.user): if not TicketRead.objects.filter(ticket_update=ticket_update, user=request.user, read=True).exists(): unread_updates_count += 1 ticket.unread_updates_count = unread_updates_count ticket.last_update_added_by = last_update_added_by ticket.last_update_date_added = last_update_date_added for ticket in closed_tickets: last_update = ticket.ticketupdate_set.order_by('-date_added').first() if last_update: last_update_added_by = last_update.added_by last_update_date_added = last_update.date_added else: last_update_added_by = None last_update_date_added = None ticket.last_update_added_by = last_update_added_by ticket.last_update_date_added = last_update_date_added context = { 'open_tickets': open_tickets, 'closed_tickets': closed_tickets, } return render(request, 'listing_pages/customer-tickets.html', context) # DETAILS @customer_login_required def customer_ticket_details(request, ticket_number): ticket = get_object_or_404(Ticket, ticket_number=ticket_number) # Check if the logged-in user is the customer associated with the ticket if request.user.is_authenticated: if ticket.customer != request.user.customerprofile: raise Http404("Page not found.") # Subquery to get the last reaction added by the logged-in customer for each ticket update last_reaction_subquery = TicketUpdateReaction.objects.filter( ticket_update=OuterRef('pk'), customer=request.user ).order_by('-id').values('reaction')[:1] ticket_updates = TicketUpdate.objects.filter(ticket=ticket).annotate( last_customer_reaction=Subquery(last_reaction_subquery) ).order_by('id') # Mark updates as read for the current user for update in TicketUpdate.objects.filter(ticket=ticket).exclude(added_by=request.user).order_by('id'): if not TicketRead.objects.filter(ticket_update=update, user=request.user).exists(): TicketRead.objects.create(ticket_update=update, user=request.user, read=True) last_ticket_status = TicketStatusUpdate.objects.all().last() else: ticket_updates = None last_ticket_status = None context = { 'ticket': ticket, 'ticket_updates': ticket_updates, 'last_ticket_status': last_ticket_status, } return render(request, 'details_templates/inner-customer-ticket.html', context) @customer_login_required def customer_add_ticket_update_reaction(request, reaction_type, ticketupdate_id): ticket_update = get_object_or_404(TicketUpdate, id=ticketupdate_id) existing_reaction = TicketUpdateReaction.objects.filter(ticket_update=ticket_update, customer=request.user).first() if existing_reaction: # If the existing reaction type is equal to the new reaction, delete it if existing_reaction.reaction == reaction_type: existing_reaction.delete() else: # If not, delete all previous reactions and add a new one TicketUpdateReaction.objects.filter(ticket_update=ticket_update, customer=request.user).delete() reaction = TicketUpdateReaction.objects.create( ticket_update=ticket_update, reaction=reaction_type, customer=request.user ) else: # If there's no existing reaction, simply add the new one reaction = TicketUpdateReaction.objects.create( ticket_update=ticket_update, reaction=reaction_type, customer=request.user ) return redirect('customerticketdetails', ticket_number=ticket_update.ticket.ticket_number) # PRODUCTS @customer_login_required def osimenu_plans(request, *args, **kwargs): osimenu_basic = Item.objects.filter(title='OSIMENU BASIC').first() osimenu_standard = Item.objects.filter(title='OSIMENU STANDARD').first() osimenu_premium = Item.objects.filter(title='OSIMENU PREMIUM').first() active_order_item_basic = OrderItem.objects.filter(order__customer=request.user.customerprofile, item=osimenu_basic, active=True) active_order_item_standard = OrderItem.objects.filter(order__customer=request.user.customerprofile, item=osimenu_standard, active=True) active_order_item_premium = OrderItem.objects.filter(order__customer=request.user.customerprofile, item=osimenu_premium, active=True) context = { 'osimenu_basic': osimenu_basic, 'osimenu_standard': osimenu_standard, 'osimenu_premium': osimenu_premium, 'active_order_item_basic': active_order_item_basic, 'active_order_item_standard': active_order_item_standard, 'active_order_item_premium': active_order_item_premium, } return render(request, 'products/osimenu-plans.html', context) @customer_login_required def osicard_plans(request, *args, **kwargs): osicard_basic = Item.objects.filter(title='OSICARD BASIC').first() active_order_item_basic = OrderItem.objects.filter(order__customer=request.user.customerprofile, item=osicard_basic, active=True) context = { 'active_order_item_basic': active_order_item_basic, } return render(request, 'products/osicard-plans.html', context) @customer_login_required def shared_hosting_plans(request, *args, **kwargs): context = { } return render(request, 'products/shared-hosting-plans.html', context) @customer_login_required def cloud_vps_hosting_plans(request, *args, **kwargs): context = { } return render(request, 'products/cloud-vps-hosting-plans.html', context) @customer_login_required def dedicated_servers_plans(request, *args, **kwargs): context = { } return render(request, 'products/dedicated-servers-plans.html', context) @customer_login_required def customer_projects(request, *args, **kwargs): projects = Project.objects.filter(customer=request.user.customerprofile) context = { 'projects': projects, } return render(request, 'listing_pages/customer-projects.html', context) @customer_login_required def customer_project_details(request, *args, **kwargs): context = { } return render(request, 'details_templates/inner-customer-project.html', context)