<?php

namespace App\Http\Controllers\Processing;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class SupplierProcessingController extends Controller
{
    public function index()
    {
        $suppliers = DB::table('supplier_accounts as sa')
            ->leftJoin('supplier_categories as sc', 'sc.id', '=', 'sa.category_id')
            ->select([
                'sa.id',
                'sa.supplier_code',
                'sa.supplier_account',
                DB::raw('COALESCE(sc.name, "Uncategorised") as category_name'),
                DB::raw('COALESCE((SELECT SUM(sdh.inclusive_total) FROM supplier_document_headers sdh WHERE sdh.supplier_account_id = sa.id AND sdh.status <> "paid"), 0) as outstanding_balance'),
            ])
            ->orderBy('sa.supplier_account')
            ->paginate(25);

        return view('processing.suppliers.index', compact('suppliers'));
    }
}

