{{-- NEW CUSTOM INVOICE TEMPLATE --}}
    <style>
        body {
            font-family: Arial, sans-serif;
            font-size: 13px;
        }
        h2 {
            text-align: center;
            font-size: 20px;
            margin-bottom: 10px;
            text-decoration: underline;
        }
        .header {
            width: 100%;
            margin-bottom: 15px;
        }
        .header td {
            padding: 3px 6px;
            vertical-align: top;
        }
        table.items {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 15px;
        }
        table.items th, table.items td {
            border: 1px solid #000;
            padding: 5px;
            text-align: center;
            font-size: 12px;
        }
        table.items th {
            background: #f0f0f0;
            font-weight: bold;
        }
        .total-row td {
            font-weight: bold;
        }
        .amount-words {
            margin: 10px 0;
            font-size: 13px;
        }
        .signatures {
            margin-top: 40px;
            width: 100%;
        }
        .signatures td {
            width: 33%;
            text-align: center;
            vertical-align: top;
            font-size: 12px;
        }
        .line {
            display: block;
            margin: 25px auto 5px;
            border-top: 1px solid #000;
            width: 80%;
        }
    </style>

    <h2>INVOICE</h2>

    <table class="header">
        <tr>
            <td>
                <strong>Customer's Name:</strong> {{ strip_tags($receipt_details->customer_info ?? 'Walk-in Customer') }} <br>
                <strong>Sales Person:</strong> {{ $receipt_details->sales_person ?? 'Not Assigned' }} <br>
                <strong>Ref No.:</strong> {{ $receipt_details->ref_no ?? 'N/A' }} <br>
                <strong>Remarks:</strong> {{ $receipt_details->additional_notes ?? 'None' }}
            </td>
            <td align="right">
                <strong>Invoice No.:</strong> {{ $receipt_details->invoice_no }} <br>
                <strong>Date:</strong> {{ $receipt_details->invoice_date }} <br>
                <strong>Delivery Challan:</strong> {{ $receipt_details->vehicle_number ?? $receipt_details->shipping_custom_field_1_value ?? $receipt_details->sell_custom_field_1_value ?? $receipt_details->custom_field_1 ?? 'N/A' }} <br>
                <strong>Delivery Challan Date:</strong> {{ $receipt_details->shipping_custom_field_2_value ?? $receipt_details->delivery_date ?? $receipt_details->invoice_date ?? 'N/A' }} <br>
                <strong>Term of Sales:</strong> 
                @php
                    $payment_status = $receipt_details->payment_status ?? '';
                    $payment_method = $receipt_details->payment_method ?? $receipt_details->payments[0]['method'] ?? '';
                    $due_amount = $receipt_details->due_amount ?? 0;
                    $customer_credit = $receipt_details->customer_credit ?? 0;
                    
                    if ($payment_status == 'paid') {
                        echo $payment_method ?: 'Paid';
                    } elseif ($due_amount > 0) {
                        echo 'Due - ' . ($payment_method ?: 'Credit');
                    } elseif ($customer_credit > 0) {
                        echo 'Credit Available';
                    } else {
                        echo $payment_method ?: 'Cash';
                    }
		@endphp
            </td>
        </tr>
    </table>

    <table class="items">
			<thead>
				<tr>
                <th>S.No</th>
                <th>Item Description</th>
                <th>Batch No.</th>
                <th>U.O.M</th>
                <th>Unit Type</th>
                <th>Unit Weight</th>
                <th>No. Of Units</th>
                <th>Qty</th>
                <th>Rate</th>
                <th>Gross Amount</th>
                <th>Disc. Amount</th>
                <th>Net Amount</th>
				</tr>
			</thead>
			<tbody>
            @foreach($receipt_details->lines as $index => $line)
                <tr>
                    <td>{{ $index+1 }}</td>
                    <td>{{ $line['name'] }} {{ $line['product_variation'] }} {{ $line['variation'] }}</td>
                    <td>
                        @if(!empty($line['lot_number']))
                            {{ $line['lot_number'] }}
                        @else
                            N/A
                            @endif
                        </td>
                    <td>{{ $line['units'] }}</td>
                    <td>{{ $line['product_variation'] ?? '' }}</td>
                    <td>
                        @if(!empty($line['unit_weight']))
                            {{ $line['unit_weight'] }}
                        @else
                            <span style="color: #999; font-style: italic;">Not Set</span>
                            @endif
						</td>
                    <td>{{ $line['quantity'] }}</td>
                    <td>{{ $line['quantity'] }}</td>
                    <td>{{ $line['unit_price_before_discount'] }}</td>
                    <td>{{ $line['line_total_before_discount'] ?? $line['line_total'] }}</td>
                    <td>{{ $line['total_line_discount'] ?? '0.00' }}</td>
                    <td>{{ $line['line_total'] }}</td>
							</tr>
						@endforeach
            <tr class="total-row">
                <td colspan="6" align="right">Total :</td>
                <td>{{ $receipt_details->total_quantity ?? '' }}</td>
                <td>{{ $receipt_details->total_quantity ?? '' }}</td>
    					<td></td>
                <td>{{ $receipt_details->subtotal ?? '' }}</td>
                <td>{{ $receipt_details->total_line_discount ?? '0.00' }}</td>
                <td>{{ $receipt_details->total ?? '' }}</td>
					</tr>
			</tbody>
		</table>

    <div class="amount-words">
        <strong>RUPEES :</strong> 
        @if(!empty($receipt_details->total_in_words))
            {{ $receipt_details->total_in_words }}
        @else
            @php
                // Clean the total amount by removing currency symbols and formatting
                $total_string = $receipt_details->total ?? '0';
                $total_string = str_replace(['₹', '$', '€', '£', ','], '', $total_string);
                $total_string = preg_replace('/[^0-9.]/', '', $total_string);
                $total_amount = floatval($total_string);
                
                // If the formatted total is 0, try the unformatted total
                if ($total_amount == 0 && !empty($receipt_details->total_unformatted)) {
                    $total_amount = floatval($receipt_details->total_unformatted);
                }
                
                // If still 0, try to get from final_total
                if ($total_amount == 0 && !empty($receipt_details->final_total)) {
                    $total_amount = floatval($receipt_details->final_total);
                }
                
                // Debug output removed - parsing is working correctly
                
                function convertNumberToWords($number) {
                    $hyphen      = '-';
                    $conjunction = ' and ';
                    $separator   = ', ';
                    $negative    = 'negative ';
                    $decimal     = ' point ';
                    $dictionary  = [
                        0 => 'zero',
                        1 => 'one',
                        2 => 'two',
                        3 => 'three',
                        4 => 'four',
                        5 => 'five',
                        6 => 'six',
                        7 => 'seven',
                        8 => 'eight',
                        9 => 'nine',
                        10 => 'ten',
                        11 => 'eleven',
                        12 => 'twelve',
                        13 => 'thirteen',
                        14 => 'fourteen',
                        15 => 'fifteen',
                        16 => 'sixteen',
                        17 => 'seventeen',
                        18 => 'eighteen',
                        19 => 'nineteen',
                        20 => 'twenty',
                        30 => 'thirty',
                        40 => 'forty',
                        50 => 'fifty',
                        60 => 'sixty',
                        70 => 'seventy',
                        80 => 'eighty',
                        90 => 'ninety',
                        100 => 'hundred',
                        1000 => 'thousand',
                        100000 => 'lakh',
                        10000000 => 'crore'
                    ];

                    if (!is_numeric($number)) {
                        return false;
                    }

                    if ($number < 0) {
                        return $negative . convertNumberToWords(abs($number));
                    }

                    $string = $fraction = null;

                    if (strpos($number, '.') !== false) {
                        list($number, $fraction) = explode('.', $number);
                    }

                    switch (true) {
                        case $number < 21:
                            $string = $dictionary[$number];
                            break;
                        case $number < 100:
                            $tens   = ((int) ($number / 10)) * 10;
                            $units  = $number % 10;
                            $string = $dictionary[$tens];
                            if ($units) {
                                $string .= $hyphen . $dictionary[$units];
                            }
                            break;
                        case $number < 1000:
                            $hundreds  = (int) ($number / 100);
                            $remainder = $number % 100;
                            $string = $dictionary[$hundreds] . ' ' . $dictionary[100];
                            if ($remainder) {
                                $string .= $conjunction . convertNumberToWords($remainder);
                            }
                            break;
                        default:
                            $baseUnit = pow(1000, floor(log($number, 1000)));
                            $numBaseUnits = (int) ($number / $baseUnit);
                            $remainder = $number % $baseUnit;
                            $string = convertNumberToWords($numBaseUnits) . ' ' . $dictionary[$baseUnit];
                            if ($remainder) {
                                $string .= $remainder < 100 ? $conjunction : $separator;
                                $string .= convertNumberToWords($remainder);
                            }
                            break;
                    }

                    if ($fraction !== null && is_numeric($fraction)) {
                        $string .= $decimal;
                        $words = [];
                        foreach (str_split((string) $fraction) as $digit) {
                            $words[] = $dictionary[$digit];
                        }
                        $string .= implode(' ', $words);
                    }

                    return $string;
                }
                
                $amount_in_words = convertNumberToWords($total_amount);
                echo ucfirst($amount_in_words) . ' Rupees Only';
            @endphp
			@endif
	</div>

    <table class="signatures">
        <tr>
            <td>
                <span class="line"></span>
                <strong>PREPARED BY</strong><br>
                {{ $receipt_details->prepared_by ?? '' }}
							</td>
            <td>
                <span class="line"></span>
                <strong>CHECKED BY</strong><br>
                {{ $receipt_details->checked_by ?? '' }}
							</td>
            <td>
                <span class="line"></span>
                <strong>AUTHORISED SIGNATURE</strong>
						</td>
					</tr>
    </table>

    <br><br>

    <table class="signatures">
							<tr>
								<td>
                <span class="line"></span>
                <strong>Receiver's Name</strong>
								</td>
            <td>
                <span class="line"></span>
                <strong>Signature</strong>
								</td>
            <td></td>
	        			</tr>
	        	</table>