<?php
require('../../fpdf.php');
include "../../root.class.php";


$db = new db_safeguard();

$jobcard_id = $_GET['record_id'];

$invoice_res = $db->query("invoice", "SELECT * FROM invoice WHERE jobcard_id = '$jobcard_id'");

$jobcard_res = $db->query("jobcards", "SELECT * FROM jobcards WHERE jc_number = '$jobcard_id'");
$jobcard = $jobcard_res->fetch_assoc();

$pdf = new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage('p');

$pdf->SetFont('Arial', 'B', 20);
$pdf->Cell(200, 15, "INVOICE FOR JOBCARD : " . $jobcard['jc_number'], 0, 1, 'C');
$pdf->Cell(10, 5, '', 0, 1, 'L');

$pdf->SetFont('Arial', 'B', 15);
$pdf->Cell(10, 5, '', 0, 1, 'L');
$pdf->SetFont('Arial', '', 10);

$pdf->Cell(20, 8, "QUANTITY", 1, 0, 'L');
$pdf->Cell(30, 8, "METERS", 1, 0, 'L');
$pdf->Cell(70, 8, "DESCRIPTION", 1, 0, 'L');
$pdf->Cell(30, 8, "AMOUNT", 1, 1, 'L');

while ($invoice = $invoice_res->fetch_assoc()) {

    $total += $invoice['amount']; 

    $amount_due = $total - $jobcard['deposite_amount'];

    $pdf->Cell(20, 8, $invoice['quantity'], 1, 0, 'L');
    $pdf->Cell(30, 8, $invoice['meters'], 1, 0, 'L');
    $pdf->Cell(70, 8, $invoice['description'], 1, 0, 'L');
    $pdf->Cell(30, 8, $invoice['amount'], 1, 1, 'L');
}

$pdf->Cell(10, 5, '', 0, 1, 'L');
$pdf->Cell(50, 8, "Total : ", "", 0, 'L');
$pdf->Cell(50, 8, $total, "", 1, 'L');

$pdf->Cell(50, 8, "Deposite Amount : ", "", 0, 'L');
$pdf->Cell(50, 8, $jobcard['deposite_amount'], "", 1, 'L');

$pdf->Cell(50, 8, "Amount Due : ", "", 0, 'L');
$pdf->Cell(50, 8, $amount_due, "", 1, 'L');

// $pdf->Cell(50, 8, "Break Found On : ", "", 0, 'L');
// $pdf->Cell(50, 8, $jobcard['break_found_on'], "", 1, 'L');

// $pdf->Cell(50, 8, "Estimated Liters : ", "", 0, 'L');
// $pdf->Cell(50, 8, $jobcard['estimated_liters'], "", 1, 'L');

$pdf->Output("I");