Initial commit
This commit is contained in:
0
accounts/__init__.py
Normal file
0
accounts/__init__.py
Normal file
BIN
accounts/__pycache__/__init__.cpython-310.pyc
Normal file
BIN
accounts/__pycache__/__init__.cpython-310.pyc
Normal file
Binary file not shown.
BIN
accounts/__pycache__/admin.cpython-310.pyc
Normal file
BIN
accounts/__pycache__/admin.cpython-310.pyc
Normal file
Binary file not shown.
BIN
accounts/__pycache__/apps.cpython-310.pyc
Normal file
BIN
accounts/__pycache__/apps.cpython-310.pyc
Normal file
Binary file not shown.
BIN
accounts/__pycache__/models.cpython-310.pyc
Normal file
BIN
accounts/__pycache__/models.cpython-310.pyc
Normal file
Binary file not shown.
BIN
accounts/__pycache__/urls.cpython-310.pyc
Normal file
BIN
accounts/__pycache__/urls.cpython-310.pyc
Normal file
Binary file not shown.
BIN
accounts/__pycache__/utils.cpython-310.pyc
Normal file
BIN
accounts/__pycache__/utils.cpython-310.pyc
Normal file
Binary file not shown.
BIN
accounts/__pycache__/views.cpython-310.pyc
Normal file
BIN
accounts/__pycache__/views.cpython-310.pyc
Normal file
Binary file not shown.
3
accounts/admin.py
Normal file
3
accounts/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
6
accounts/apps.py
Normal file
6
accounts/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AccountsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'accounts'
|
||||
0
accounts/migrations/__init__.py
Normal file
0
accounts/migrations/__init__.py
Normal file
BIN
accounts/migrations/__pycache__/__init__.cpython-310.pyc
Normal file
BIN
accounts/migrations/__pycache__/__init__.cpython-310.pyc
Normal file
Binary file not shown.
110
accounts/models.py
Normal file
110
accounts/models.py
Normal file
@@ -0,0 +1,110 @@
|
||||
from django.db import models
|
||||
|
||||
|
||||
class Plant(models.Model):
|
||||
|
||||
id = models.BigAutoField(primary_key=True)
|
||||
|
||||
company_id = models.IntegerField()
|
||||
|
||||
code = models.IntegerField(unique=True)
|
||||
|
||||
name = models.TextField(unique=True)
|
||||
|
||||
address = models.TextField()
|
||||
|
||||
created_at = models.DateTimeField()
|
||||
|
||||
updated_at = models.DateTimeField()
|
||||
|
||||
deleted_at = models.DateTimeField(
|
||||
blank=True,
|
||||
null=True
|
||||
)
|
||||
|
||||
warehouse_number = models.TextField(
|
||||
blank=True,
|
||||
null=True
|
||||
)
|
||||
|
||||
class Meta:
|
||||
managed = False
|
||||
db_table = 'plants'
|
||||
|
||||
class InvoiceValidation(models.Model):
|
||||
|
||||
plant = models.ForeignKey(
|
||||
Plant,
|
||||
on_delete=models.DO_NOTHING,
|
||||
db_column='plant_id'
|
||||
)
|
||||
|
||||
invoice_number = models.CharField(max_length=100)
|
||||
|
||||
quantity = models.IntegerField(null=True)
|
||||
|
||||
scanned_status = models.CharField(max_length=50)
|
||||
|
||||
serial_number = models.CharField(
|
||||
max_length=100,
|
||||
null=True
|
||||
)
|
||||
|
||||
created_at = models.DateTimeField()
|
||||
|
||||
|
||||
class Meta:
|
||||
managed = False
|
||||
db_table = "invoice_validations"
|
||||
|
||||
class Users(models.Model):
|
||||
|
||||
id = models.BigAutoField(primary_key=True)
|
||||
|
||||
name = models.CharField(max_length=255)
|
||||
|
||||
email = models.CharField(max_length=255)
|
||||
|
||||
password = models.CharField(max_length=255)
|
||||
|
||||
class Meta:
|
||||
managed = False
|
||||
db_table = "users"
|
||||
|
||||
class Lines(models.Model):
|
||||
id = models.BigAutoField(primary_key=True)
|
||||
plant = models.ForeignKey(
|
||||
'Plant',
|
||||
models.DO_NOTHING,
|
||||
db_column='plant_id'
|
||||
)
|
||||
name = models.TextField()
|
||||
type = models.TextField()
|
||||
created_at = models.DateTimeField()
|
||||
updated_at = models.DateTimeField()
|
||||
deleted_at = models.DateTimeField(blank=True, null=True)
|
||||
line_capacity = models.TextField(blank=True, null=True)
|
||||
|
||||
class Meta:
|
||||
managed = False
|
||||
db_table = 'lines'
|
||||
|
||||
class BreakdownLogs(models.Model):
|
||||
id = models.BigAutoField(primary_key=True)
|
||||
|
||||
plant = models.ForeignKey('Plant',models.DO_NOTHING,db_column='plant_id')
|
||||
line = models.ForeignKey('Lines',models.DO_NOTHING,db_column='line_id')
|
||||
machine_name = models.CharField(max_length=255)
|
||||
reason = models.TextField(blank=True,null=True)
|
||||
breakdown_at = models.DateField(blank=True,null=True)
|
||||
breakdown_time = models.TimeField(blank=True,null=True)
|
||||
down_time = models.TimeField(blank=True,null=True)
|
||||
created_at = models.DateTimeField(blank=True,null=True)
|
||||
created_by = models.TextField(blank=True,null=True)
|
||||
updated_at = models.DateTimeField(blank=True,null=True)
|
||||
updated_by = models.TextField(blank=True,null=True)
|
||||
deleted_at = models.DateTimeField(blank=True,null=True)
|
||||
|
||||
class Meta:
|
||||
managed = False
|
||||
db_table = "breakdown_logs"
|
||||
3
accounts/tests.py
Normal file
3
accounts/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
9
accounts/urls.py
Normal file
9
accounts/urls.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.login, name='login'),
|
||||
path('dashboard/', views.dashboard, name='dashboard'),
|
||||
path("get-lines/<int:plant_id>/",views.get_lines,name="get_lines"),
|
||||
path("save-breakdown/", views.save_breakdown, name="save_breakdown"),
|
||||
]
|
||||
27
accounts/utils.py
Normal file
27
accounts/utils.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from datetime import timedelta
|
||||
from django.utils import timezone
|
||||
|
||||
|
||||
def get_shift_date_range(schedule="daily"):
|
||||
|
||||
now = timezone.now()
|
||||
|
||||
today_8am = now.replace(
|
||||
hour=8,
|
||||
minute=0,
|
||||
second=0,
|
||||
microsecond=0
|
||||
)
|
||||
|
||||
if schedule.lower() == "daily":
|
||||
|
||||
start_date = today_8am - timedelta(days=1)
|
||||
end_date = today_8am
|
||||
|
||||
else:
|
||||
|
||||
start_date = today_8am
|
||||
end_date = today_8am + timedelta(days=1)
|
||||
|
||||
|
||||
return start_date, end_date
|
||||
224
accounts/views.py
Normal file
224
accounts/views.py
Normal file
@@ -0,0 +1,224 @@
|
||||
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
|
||||
}
|
||||
|
||||
)
|
||||
Reference in New Issue
Block a user