<?php
ob_start();
require("../../classes/autoload.php");

$record_id = isset($_GET['record_id']) ? $_GET['record_id'] : '';

$credit_note_res = $db->query('credit_notes', "SELECT * FROM credit_notes WHERE invoice_id = '{$record_id}'");
$credit_note = $credit_note_res->fetch_assoc();

if (!$credit_note) {
    ob_end_clean();
    die("Credit note not found.");
}

$company_res = $db->query("company_info", "SELECT * FROM company_info WHERE record_id = 1");
$company = $company_res->fetch_assoc();

$invoice_res = $db->query("invoices", "SELECT * FROM invoices WHERE record_id = '{$credit_note['invoice_id']}'");
$invoice = $invoice_res->fetch_assoc();

if (!$invoice) {
    ob_end_clean();
    die("Linked invoice not found.");
}

$client_res = $db->query("clients", "SELECT * FROM clients WHERE record_id = '{$invoice['client_id']}'");
$client = $client_res->fetch_assoc();

if (!$client) {
    ob_end_clean();
    die("Client not found.");
}

$credit_note_list_res = $db->query("credit_notes_list", "SELECT * FROM credit_notes_list WHERE credit_note_id = '{$credit_note['record_id']}'");

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 12);

// Logos
$pdf->Image('../../assets/logo.png', 10, 10, 30);
$pdf->Image('../../assets/logo_2.png', 170, 10, 30);

// Title
$pdf->SetFont('Arial', 'B', 16);
$pdf->Cell(0, 20, 'CREDIT NOTE NO. ' . $credit_note['record_id'], 0, 1, 'C');
$pdf->Ln(10);

// Client & Company Details
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(100, 8, $client['name'], 0, 0);
$pdf->Cell(80, 8, $company['name'], 0, 1);
$pdf->SetFont('Arial', '', 9);

$pdf->Cell(100, 8, $client['email'], 0, 0);
$pdf->Cell(80, 8, "Reg: " . $company['reg'] . " | Vat NR: " . $company['vat'], 0, 1);

$pdf->Cell(100, 8, $client['contact_person'], 0, 0);
$pdf->Cell(80, 8, "TEL: " . $company['tel'], 0, 1);

$pdf->Cell(100, 8, $client['contact_number'], 0, 0);
$pdf->Cell(80, 8, "EMAIL: " . $company['email'], 0, 1);

$pdf->MultiCell(100, 8, $client['address']);
$pdf->Ln(6);
$pdf->SetXY(110, 72);
$pdf->MultiCell(80, 8, $company['address_line_1'] . " " . $company['address_line_2'] . " " . $company['address_line_3']);

// Credit Note Info
$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(40, 6, "DATE:", 0, 0);
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(50, 6, date("Y-m-d", strtotime($credit_note['date_time'])), 0, 1);

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(40, 6, "LINKED INVOICE:", 0, 0);
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(50, 6, $invoice['invoice_number'], 0, 1);

$pdf->SetFont('Arial', 'B', 10);
$pdf->Cell(40, 6, "ORDER TYPE:", 0, 0);
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(50, 6, $invoice['order_type'], 0, 1);

$pdf->Ln(3);

// Table Header
$pdf->SetFont('Arial', 'B', 9);
$pdf->SetFillColor(200, 200, 200);
$pdf->Cell(145, 7, 'Description', 1, 0, 'C', true);
$pdf->Cell(40, 7, 'Amount', 1, 1, 'C', true);

// Table Body
$pdf->SetFont('Arial', '', 9);
$subtotal = 0;

while ($row = $credit_note_list_res->fetch_assoc()) {
    $amount = (float) preg_replace('/[^0-9\.\-]/', '', $row['amount']);
    $subtotal += $amount;

    $pdf->Cell(145, 7, $row['name'], 1, 0, 'L');
    $pdf->Cell(40, 7, 'R ' . number_format($amount, 2), 1, 1, 'L');
}

// Totals
// $pdf->Ln(2);
$pdf->SetFont('Arial', '', 9);
$pdf->Cell(145, 7, 'Subtotal', 0, 0, 'R');
$pdf->Cell(40, 7, 'R ' . number_format($subtotal, 2), 1, 1, 'L');

$vat = $subtotal * 0.15;
$net_total = $subtotal + $vat;

$pdf->Cell(145, 7, 'VAT (15%)', 0, 0, 'R');
$pdf->Cell(40, 7, 'R ' . number_format($vat, 2), 1, 1, 'L');

$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(145, 7, 'Net Total Credit', 0, 0, 'R');
$pdf->Cell(40, 7, 'R ' . number_format($net_total, 2), 1, 1, 'L');

// Client Banking Details / Notes
$pdf->Ln(4);
$pdf->SetFont('Arial', 'B', 9);
$pdf->Cell(0, 6, 'CLIENT BANKING DETAILS:', 0, 1);
$pdf->SetFont('Arial', '', 9);
$pdf->MultiCell(185, 5, $credit_note['client_banking_details'], 1);

$pdf->Ln(3);

// Company Bank Details
$pdf->SetFont('Arial', 'B', 8);
$pdf->Cell(0, 6, 'OUR BANK DETAILS:', 0, 1);
$pdf->Cell(20, 6, 'NAME:', "LT", 0);
$pdf->Cell(65, 6, $company['name'], "TR", 1);
$pdf->Cell(20, 6, 'BANK:', "L", 0);
$pdf->Cell(65, 6, $company['bank'], "R", 1);
$pdf->Cell(20, 6, 'BRANCH:', "L", 0);
$pdf->Cell(65, 6, $company['branch'], "R", 1);
$pdf->Cell(20, 6, 'TYPE:', "L", 0);
$pdf->Cell(65, 6, $company['account_type'], "R", 1);
$pdf->Cell(20, 6, 'ACC:', "LB", 0);
$pdf->Cell(65, 6, $company['account_no'], "BR", 1);

// Clear any buffered output (warnings etc) then send PDF
ob_end_clean();
$pdf->Output('I', 'credit_note.pdf');
