from django.shortcuts import render, redirect from django.http import HttpResponse from django.db.models import Count, Q from .models import Plant, InvoiceValidation, Users, Lines, BreakdownLogs from .utils import get_shift_date_range import bcrypt from django.contrib import messages from django.http import JsonResponse from django.views.decorators.csrf import csrf_exempt import json from django.db import connection from django.utils import timezone from datetime import datetime, time, timedelta def login(request): if request.method == "POST": username = request.POST.get("username") password = request.POST.get("password") user = Users.objects.filter(name=username).first() if user: if bcrypt.checkpw( password.encode(), user.password.encode() ): return redirect("dashboard") else: messages.error(request, "Invalid Password") return redirect("login") else: messages.error(request, "Username not found") return redirect("login") return render(request, "login.html") def get_lines(request, plant_id): lines = Lines.objects.filter( plant_id=plant_id, deleted_at__isnull=True ).values("id", "name") return JsonResponse({ "lines": list(lines) }) @csrf_exempt def save_breakdown(request): try: data = json.loads(request.body) with connection.cursor() as cursor: cursor.execute(""" INSERT INTO breakdown_logs ( plant_id, line_id, machine_name, reason, breakdown_at, breakdown_time, down_time ) VALUES (%s,%s,%s,%s,%s,%s,%s) """, [ data["plant_id"], data["line_id"], data["machine_name"], data["reason"], data["breakdown_date"], data["breakdown_time"], data["down_time"] ]) return JsonResponse({"status": "success"}) except Exception as e: print(e) return JsonResponse({ "status": "error", "message": str(e) }, status=500) def dashboard(request): start_date, end_date = get_shift_date_range("daily") plants = Plant.objects.all() total_plants = plants.count() invoice_report = [] for plant in plants: # Serial Invoice total_serial = ( InvoiceValidation.objects .filter( plant_id=plant.id, quantity__isnull=True, created_at__gte=start_date, created_at__lt=end_date ) .values('invoice_number') .distinct() .count() ) scanned_serial = ( InvoiceValidation.objects .filter( plant_id=plant.id, quantity__isnull=True, scanned_status="Scanned", created_at__gte=start_date, created_at__lt=end_date ) .values('invoice_number') .distinct() .count() ) # Material Invoice total_material = ( InvoiceValidation.objects .filter( plant_id=plant.id, quantity=1, created_at__gte=start_date, created_at__lt=end_date ) .values('invoice_number') .distinct() .count() ) # Bundle Invoice total_bundle = ( InvoiceValidation.objects .filter( plant_id=plant.id, quantity__gt=1, created_at__gte=start_date, created_at__lt=end_date ) .values('invoice_number') .distinct() .count() ) invoice_report.append({ "plant": plant.name, "serial_total": total_serial, "serial_scanned": scanned_serial, "material_total": total_material, "bundle_total": total_bundle }) plants = Plant.objects.filter(deleted_at__isnull=True).order_by("name") now = timezone.localtime() now1 = timezone.now() start_time = timezone.make_aware( datetime.combine(now.date(), time(8, 0, 0)) ) # If current time is before 8 AM, shift starts yesterday 8 AM if now < start_time: start_time = start_time - timedelta(days=1) # Next day 08:00 AM end_time = start_time + timedelta(days=1) breakdowns = BreakdownLogs.objects.filter( deleted_at__isnull=True, created_at__gte=start_time, created_at__lt=end_time ) return render( request, "dashboard.html", { "total_plants": total_plants, "plants": plants, "breakdowns": breakdowns, "total_serial": total_serial, "scanned_serial": scanned_serial, "invoice_report": invoice_report, "start_date":start_date, "end_date":end_date } )