
@login_required
def customer_statement(request):
    customers = Customer.objects.all().order_by('name')
    selected_customer = None
    sales = []
    
    customer_id = request.GET.get('customer')
    start_date = request.GET.get('start_date')
    end_date = request.GET.get('end_date')
    
    if customer_id:
        selected_customer = get_object_or_404(Customer, id=customer_id)
        sales = Sale.objects.filter(customer=selected_customer).order_by('-created_at')
        
        if start_date:
            sales = sales.filter(created_at__date__gte=start_date)
        if end_date:
            sales = sales.filter(created_at__date__lte=end_date)
            
    context = {
        'customers': customers,
        'selected_customer': selected_customer,
        'sales': sales,
        'start_date': start_date,
        'end_date': end_date
    }
    return render(request, 'core/customer_statement.html', context)

@login_required
def supplier_statement(request):
    suppliers = Supplier.objects.all().order_by('name')
    selected_supplier = None
    purchases = []
    
    supplier_id = request.GET.get('supplier')
    start_date = request.GET.get('start_date')
    end_date = request.GET.get('end_date')
    
    if supplier_id:
        selected_supplier = get_object_or_404(Supplier, id=supplier_id)
        purchases = Purchase.objects.filter(supplier=selected_supplier).order_by('-created_at')
        
        if start_date:
            purchases = purchases.filter(created_at__date__gte=start_date)
        if end_date:
            purchases = purchases.filter(created_at__date__lte=end_date)
            
    context = {
        'suppliers': suppliers,
        'selected_supplier': selected_supplier,
        'purchases': purchases,
        'start_date': start_date,
        'end_date': end_date
    }
    return render(request, 'core/supplier_statement.html', context)

@login_required
def cashflow_report(request):
    # Simplified Cashflow
    start_date = request.GET.get('start_date')
    end_date = request.GET.get('end_date')
    
    sales = Sale.objects.all()
    expenses = Expense.objects.all()
    purchases = Purchase.objects.all()
    
    if start_date:
        sales = sales.filter(created_at__date__gte=start_date)
        expenses = expenses.filter(date__gte=start_date)
        purchases = purchases.filter(created_at__date__gte=start_date)
        
    if end_date:
        sales = sales.filter(created_at__date__lte=end_date)
        expenses = expenses.filter(date__lte=end_date)
        purchases = purchases.filter(created_at__date__lte=end_date)
        
    total_sales = sales.aggregate(total=Sum('total_amount'))['total'] or 0
    total_expenses = expenses.aggregate(total=Sum('amount'))['total'] or 0
    total_purchases = purchases.aggregate(total=Sum('total_amount'))['total'] or 0
    
    net_profit = total_sales - total_expenses - total_purchases
    
    context = {
        'total_sales': total_sales,
        'total_expenses': total_expenses,
        'total_purchases': total_purchases,
        'net_profit': net_profit,
        'start_date': start_date,
        'end_date': end_date
    }
    return render(request, 'core/cashflow_report.html', context)
