OLIVIA                      JURNAL UMUM

    Voucher No. {{$generals->general_journal_number}}
    Tanggal : {{ \Carbon\Carbon::parse($generals->general_journal_date)->format('d M Y') }}

    --------------------------------------------------------------------------------------
    |Account No.|    Account Name    | Nominal Credit | Nominal Debet  |      Memo       |
    --------------------------------------------------------------------------------------
@php
    if (!function_exists('splitAtWordBoundary')) {
        function splitAtWordBoundary($text, $maxLength) {
            if (strlen($text) <= $maxLength) return [$text];
            $pos = strrpos(substr($text, 0, $maxLength + 1), ' ');
            if ($pos === false) $pos = $maxLength;
            return [substr($text, 0, $pos), substr($text, $pos + 1)];
        }
    }
@endphp
@foreach($generals->details as $index => $detail)
@php
    $cols = [
        'account_number' => $detail->account_number . ' ',
        'account_name' => $detail->coa->account_name,
        'nominal_debet' => number_format($detail->nominal_debet, 0),
        'nominal_credit' => number_format($detail->nominal_credit, 0),
        'memo' => $detail->note
    ];
    $maxLengths = [
        'account_number' => 11,
        'account_name' => 20,
        'nominal_debet' => 16,
        'nominal_credit' => 16,
        'memo' => 17
    ];
    $lines = [[]];
    $remaining = array_map(fn($col) => $col, $cols);

    // Split columns into lines
    while (array_filter($remaining)) {
        $currentLine = [];
        foreach ($cols as $key => $value) {
            $max = $maxLengths[$key];
            $text = $remaining[$key] ?? '';
            if (strlen($text) > 0) {
                $split = splitAtWordBoundary($text, $max);
                $currentLine[$key] = $split[0];
                $remaining[$key] = $split[1] ?? '';
            } else {
                $currentLine[$key] = '';
            }
        }
        $lines[] = $currentLine;
    }
    array_shift($lines); // Remove empty first line
@endphp
@foreach($lines as $line)
    |{{ str_pad($line['account_number'], 11) }}|{{ str_pad($line['account_name'], 20) }}|{{ str_pad($line['nominal_debet'], 16, ' ', STR_PAD_LEFT) }}|{{ str_pad($line['nominal_credit'], 16, ' ', STR_PAD_LEFT) }}|{{ str_pad($line['memo'], 17, ' ', STR_PAD_LEFT) }}|
@endforeach
@endforeach
    --------------------------------------------------------------------------------------
@php
    // Helper function to split at word boundary <= maxLength

    $totalPayment = 'Total Payment: '.number_format($generals->nominal_debet, 0);
    $maxLineLength = 84; // Total line length
    $maxHurufLength = $maxLineLength - strlen($totalPayment); // Max for $totalHuruf per line
    $lines = [];
    $remaining = $totalHuruf;

    // First line: Fit as much of $totalHuruf as possible before Total Payment
    $split = splitAtWordBoundary($remaining, $maxHurufLength);
    $lines[] = $split[0] . str_repeat(' ', $maxHurufLength - strlen($split[0])).' '.  $totalPayment;
    $remaining = $split[1] ?? '';

    // Additional lines: Use same maxHurufLength for consistency
    while (!empty($remaining) && count($lines) < 3) {
        $split = splitAtWordBoundary($remaining, $maxHurufLength);
        $lines[] = $split[0];
        $remaining = $split[1] ?? '';
    }
@endphp
@foreach($lines as $line)
    {{ $line }}
@endforeach

    Memo
    --------------------------------------------------------------------------------------
@php
        $memoLines = [];
        $remainingMemo = $generals->note ?? '';
        $maxMemoLength = 86;

        while (!empty($remainingMemo) && count($memoLines) < 3) {
            $split = splitAtWordBoundary($remainingMemo, $maxMemoLength);
            $memoLines[] = $split[0];
            $remainingMemo = $split[1] ?? '';
        }
@endphp
@foreach($memoLines as $memoLine)
    {{ $memoLine }}
@endforeach

    Disiapkan                   Dibayar oleh                Diterima oleh


    --------                    ------------                -------------
    Tgl.                        Tgl.                        Tgl.
Kembali