Businessman uchun statistikalar qo'shildi!
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
from django.shortcuts import render
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from core.apps.management.models import Device, Income, Expense, Warehouse, ToyMovement, Report
|
||||
from core.apps.management.models import Device, Income, Expense, Warehouse, ToyMovement
|
||||
from core.apps.accounts.models import User
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from core.apps.management.decorators import role_required
|
||||
from decimal import Decimal
|
||||
from django.db.models import Sum
|
||||
from django.shortcuts import render
|
||||
|
||||
|
||||
@login_required
|
||||
@role_required(["manager", "businessman"])
|
||||
@@ -150,36 +153,114 @@ def device_payment_list(request):
|
||||
}
|
||||
)
|
||||
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.shortcuts import render
|
||||
from django.db.models import Q
|
||||
|
||||
from core.apps.management.models import Report
|
||||
from core.apps.management.decorators import role_required
|
||||
|
||||
|
||||
@login_required
|
||||
@role_required(['manager', 'businessman'])
|
||||
def report_list(request):
|
||||
reports = (
|
||||
Report.objects
|
||||
.select_related("device", "device__district", "created_by")
|
||||
.order_by("-created_at")
|
||||
)
|
||||
@role_required(["businessman", "manager"])
|
||||
def device_statistics(request):
|
||||
"""
|
||||
Har bir ToyMovement uchun kirim / xarajat / foyda hisoblaydi.
|
||||
|
||||
if request.user.role == "manager":
|
||||
reports = reports.filter(
|
||||
device__district__region=request.user.region
|
||||
)
|
||||
Formula:
|
||||
narx1 = oylik maosh / 30
|
||||
narx2 = narx1 / aparat soni (maosh ulushi, qurilma/kun)
|
||||
narx3 = kunlik umumiy xarajat / aparat soni (boshqa umumiy / kun)
|
||||
narx4 = arenda / 30 (shu qurilma arenda / kun)
|
||||
xarajat = narx2 + narx3 + narx4
|
||||
kirim = harakat miqdori * o'yinchoq narxi
|
||||
foyda = kirim - xarajat
|
||||
"""
|
||||
|
||||
return render(
|
||||
request,
|
||||
"common/lists/report_list.html",
|
||||
{
|
||||
"reports": reports,
|
||||
"title": "Hisobotlar"
|
||||
}
|
||||
# ── FILTERS ──────────────────────────────────────────────────
|
||||
start_date = request.GET.get("start_date")
|
||||
end_date = request.GET.get("end_date")
|
||||
device_id = request.GET.get("device")
|
||||
|
||||
qs = ToyMovement.objects.select_related("device", "created_by").filter(
|
||||
device__isnull=False
|
||||
)
|
||||
if start_date:
|
||||
qs = qs.filter(created_at__date__gte=start_date)
|
||||
if end_date:
|
||||
qs = qs.filter(created_at__date__lte=end_date)
|
||||
if device_id:
|
||||
qs = qs.filter(device_id=device_id)
|
||||
|
||||
qs = qs.order_by("-created_at")
|
||||
|
||||
# ── SHARED CONSTANTS ─────────────────────────────────────────
|
||||
total_devices = Device.objects.count() or 1
|
||||
|
||||
# Latest toy price
|
||||
latest_income = Income.objects.order_by("-created_at").first()
|
||||
price_per_toy = latest_income.price_per_toy if latest_income else Decimal("0")
|
||||
|
||||
# Total confirmed salary → narx1 → narx2
|
||||
total_salary = Expense.objects.filter(
|
||||
expense_type=Expense.ExpenseType.SALARY,
|
||||
is_confirmed=True,
|
||||
).aggregate(s=Sum("amount"))["s"] or Decimal("0")
|
||||
|
||||
narx1 = total_salary / 30
|
||||
narx2 = narx1 / total_devices
|
||||
|
||||
# General (non-device-specific, non-salary) confirmed expenses → narx3
|
||||
total_general = Expense.objects.filter(
|
||||
is_confirmed=True,
|
||||
device__isnull=True,
|
||||
).exclude(
|
||||
expense_type=Expense.ExpenseType.SALARY
|
||||
).aggregate(s=Sum("amount"))["s"] or Decimal("0")
|
||||
|
||||
narx3 = (total_general / 30) / total_devices
|
||||
|
||||
# ── BUILD ROWS ────────────────────────────────────────────────
|
||||
rows = []
|
||||
total_kirim = Decimal("0")
|
||||
total_xarajat = Decimal("0")
|
||||
total_foyda = Decimal("0")
|
||||
|
||||
for mv in qs:
|
||||
device = mv.device
|
||||
|
||||
# KIRIM
|
||||
kirim = Decimal(mv.quantity) * price_per_toy
|
||||
|
||||
# narx4 — rent for this device per day
|
||||
narx4 = Decimal(device.amount) / 30 if device.amount else Decimal("0")
|
||||
|
||||
# Direct device expenses (maintenance etc.) / 30
|
||||
direct = Expense.objects.filter(
|
||||
device=device, is_confirmed=True
|
||||
).aggregate(s=Sum("amount"))["s"] or Decimal("0")
|
||||
narx_direct = direct / 30
|
||||
|
||||
xarajat = narx2 + narx3 + narx4 + narx_direct
|
||||
foyda = kirim - xarajat
|
||||
|
||||
total_kirim += kirim
|
||||
total_xarajat += xarajat
|
||||
total_foyda += foyda
|
||||
|
||||
rows.append({
|
||||
"device_name": device.address,
|
||||
"quantity": mv.quantity,
|
||||
"kirim": kirim,
|
||||
"xarajat": xarajat,
|
||||
"foyda": foyda,
|
||||
"date": mv.created_at.strftime("%d.%m.%Y %H:%M"),
|
||||
})
|
||||
|
||||
return render(request, "common/lists/report_list.html", {
|
||||
"rows": rows,
|
||||
"devices": Device.objects.order_by("address"),
|
||||
"total_kirim": total_kirim,
|
||||
"total_xarajat": total_xarajat,
|
||||
"total_foyda": total_foyda,
|
||||
"price_per_toy": price_per_toy,
|
||||
})
|
||||
|
||||
|
||||
|
||||
@login_required
|
||||
@role_required(["employee"])
|
||||
|
||||
Reference in New Issue
Block a user