<?php
require('classes/fpdf.php');
require('classes/db.class.php');

$db = new db();

$pdf = new FPDF();

$pdf->AliasNbPages();

$batches_res = $db->exec_query('batches', ['*'], '', '', '', '', '', "record_id = {$_GET['record_id']}");
$batches = $batches_res->fetch_assoc();

$variety_res = $db->exec_query('variety', ['*'], '', '', '', '', "record_id = {$batches['variety_id']}");
$variety = $variety_res->fetch_assoc();

$clients_res = $db->exec_query('clients', ['*'], '', '', '', '', "record_id = {$batches['client_id']}");
$clients = $clients_res->fetch_assoc();

$users_res = $db->exec_query('users', ['*'], '', '', '', '', "record_id = {$batches['user_id']}");
$users = $users_res->fetch_assoc();

$pdf->AddPage('P');

$table_top = [50, 28, 16, 65, 35, 55, 30, 40, 25, 20, 20, 20];

$pdf->SetFont('Arial', 'B', 25);
$pdf->Cell(200, 15, "BATCH REPORT ", 0, 1, 'C');
$pdf->Cell(10, 5, '', 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 20);
$pdf->Cell($table_top[0], 10, 'BATCH NO: BA ' . $_GET['record_id'], 0, 1, 'L');

$pdf->Cell(10, 5, '', 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 20);
$pdf->Cell($table_top[0], 10, 'STATUS: ' . $batches['status'], 0, 1, 'L');

$pdf->SetFont('Arial', 'B', 15);
$pdf->Cell(60, 15, '', 0, 1, 'C');

$pdf->Cell($table_top[0], 10, 'Batch pre-checked by ' . $users['username'] . ' on ' . $batches['date'], 0, 1, 'L');
$pdf->Cell(60, 5, '', 0, 1, 'C');

$pdf->Cell(100, 15, 'PV:', "R", 0, 'L');
$pdf->Cell(100, 15, $batches['pv'], "", 1, 'L');

$pdf->Cell(100, 15, 'FFA:', "R", 0, 'L');
$pdf->Cell(100, 15, $batches['ffa'], "", 1, 'L');

$pdf->Cell(100, 15, 'VARIETY:', "R", 0, 'L');
$pdf->Cell(100, 15, $variety['name'], "", 1, 'L');

$pdf->Cell(100, 15, 'WEIGHT:', "R", 0, 'L');
$pdf->Cell(100, 15, $batches['weight'], "", 1, 'L');

$pdf->Cell(100, 15, 'SIZE:', "R", 0, 'L');
$pdf->Cell(100, 15, $batches['size'], "", 1, 'L');

$pdf->Cell(100, 15, 'SOUND NOT SOUND:', "R", 0, 'L');
$pdf->Cell(100, 15, $batches['sound_not_sound'], "", 1, 'L');

$pdf->Cell(100, 15, 'MOLD:', "R", 0, 'L');
$pdf->Cell(100, 15, $batches['mold'], "", 1, 'L');

$pdf->Cell(100, 15, 'MOISTURE:', "R", 0, 'L');
$pdf->Cell(100, 15, $batches['moisture'] . ' %', "", 1, 'L');

$pdf->Cell(100, 15, 'CLIENT NAME:', "R", 0, 'L');
$pdf->Cell(100, 15, $clients['client_name'], "", 1, 'L');

$pdf->Cell(100, 15, 'SHELL:', "R", 0, 'L');
$pdf->Cell(100, 15, $batches['shell'], "", 1, 'L');

$batch_res = $db->exec_query('batch', ['*'], '', '', '', '', "batch_id = {$_GET['record_id']}", '', false);
if ($batch_res->num_rows > 0) {
    $batch = $batch_res->fetch_assoc();

    $batchs_res = $db->exec_query('batches', ['*'], '', '', '', '', "record_id = {$_GET['record_id']}", '', false);
    $batchs = $batchs_res->fetch_assoc();

    $users_res = $db->exec_query('users', ['*'], '', '', '', '', "record_id = {$batchs['user_id']}", '', false);
    $users = $users_res->fetch_assoc();

    $variety_res = $db->exec_query('variety', ['*'], '', '', '', '', "record_id = {$batch['variety_id']}");
    $varieties = $variety_res->fetch_assoc();

    $pdf->AddPage('P');

    $pdf->SetFont('Arial', 'B', 15);
    $pdf->Cell(60, 15, '', 0, 1, 'C');

    $pdf->Cell($table_top[0], 10, 'Batch checked by ' . $users['username'] . ' on ' . $batch['date'], 0, 1, 'L');
    $pdf->Cell(60, 5, '', 0, 1, 'C');

    $pdf->Cell(100, 15, 'PV:', "R", 0, 'L');
    $pdf->Cell(100, 15, $batch['pv'], "", 1, 'L');

    $pdf->Cell(100, 15, 'FFA:', "R", 0, 'L');
    $pdf->Cell(100, 15, $batch['ffa'], "", 1, 'L');

    $pdf->Cell(100, 15, 'VARIETY:', "R", 0, 'L');
    $pdf->Cell(100, 15, $varieties['name'], "", 1, 'L');

    $pdf->Cell(100, 15, 'WEIGHT:', "R", 0, 'L');
    $pdf->Cell(100, 15, $batch['weight'], "", 1, 'L');

    $pdf->Cell(100, 15, 'MOISTURE:', "R", 0, 'L');
    $pdf->Cell(100, 15, $batch['moisture'] . ' %', "", 1, 'L');

    $pdf->Cell(100, 15, 'DATE:', "R", 0, 'L');
    $pdf->Cell(100, 15, $batch['date'], "", 1, 'L');

    $dehusking_res = $db->exec_query('dehusking', ['*'], '', '', '', '', "batch_id = {$_GET['record_id']}", '', false);

    if ($dehusking_res->num_rows > 0) {

        $dehusking_data = $db->exec_query('dehusking', ['*'], '', '', '', '', "batch_id = {$_GET['record_id']}", '', false);
        $dehusking = $dehusking_data->fetch_assoc();

        $users_res = $db->exec_query('users', ['*'], '', '', '', '', "record_id = {$dehusking['user_id']}", '', false);
        $users = $users_res->fetch_assoc();

        $variety_res = $db->exec_query('variety', ['*'], '', '', '', '', "record_id = {$dehusking['variety_id']}");
        $variet = $variety_res->fetch_assoc();

        $drying_bins_res = $db->exec_query('drying_bins', ['*'], '', '', '', '', "record_id = {$dehusking['drying_bin_id']}");
        $drying_bins = $drying_bins_res->fetch_assoc();

        $pdf->SetFont('Arial', 'B', 15);
        $pdf->Cell(60, 15, '', 0, 1, 'C');

        $pdf->Cell($table_top[0], 10, 'Batch dehusked by ' . $users['username'] . ' on ' . $dehusking['date_received'], 0, 1, 'L');
        $pdf->Cell(60, 5, '', 0, 1, 'C');

        $pdf->Cell(100, 15, 'DATE FINISHED:', "R", 0, 'L');
        $pdf->Cell(100, 15, $dehusking['date_finished'], "", 1, 'L');

        $pdf->Cell(100, 15, 'VARIETY:', "R", 0, 'L');
        $pdf->Cell(100, 15, $variet['name'], "", 1, 'L');

        $pdf->Cell(100, 15, 'WEIGHT:', "R", 0, 'L');
        $pdf->Cell(100, 15, $dehusking['weight'], "", 1, 'L');

        $pdf->Cell(100, 15, 'DRYING BIN:', "R", 0, 'L');
        $pdf->Cell(100, 15, $drying_bins['name'], "", 1, 'L');

        $drying_bins_data = $db->exec_query('bins', ['*'], '', '', '', '', "batch_id = {$_GET['record_id']}", '', false);

        if ($drying_bins_data->num_rows > 0) {

            $bin_res = $db->exec_query('bins_trans', ['*'], '', '', '', '', "batch_id = {$_GET['record_id']}", '', false);
            $bin = $bin_res->fetch_assoc();

            $variety_res = $db->exec_query('variety', ['*'], '', '', '', '', "record_id = {$bin['variety_id']}");
            $varietys = $variety_res->fetch_assoc();

            $drying_bin_res = $db->exec_query('drying_bins', ['*'], '', '', '', '', "record_id = {$dehusking['drying_bin_id']}");
            $drying_bin = $drying_bin_res->fetch_assoc();

            $pdf->AddPage('P');

            $pdf->SetFont('Arial', 'B', 15);
            $pdf->Cell(60, 15, '', 0, 1, 'C');

            $pdf->Cell(100, 15, 'DATE:', "R", 0, 'L');
            $pdf->Cell(100, 15, $bin['date'], "", 1, 'L');

            $pdf->Cell(100, 15, 'VARIETY:', "R", 0, 'L');
            $pdf->Cell(100, 15, $varietys['name'], "", 1, 'L');

            $pdf->Cell(100, 15, 'AMOUNT:', "R", 0, 'L');
            $pdf->Cell(100, 15, $bin['amount'], "", 1, 'L');

            $pdf->Cell(100, 15, 'DRYING BIN:', "R", 0, 'L');
            $pdf->Cell(100, 15, $drying_bin['name'], "", 1, 'L');

        }

    }
}

$pdf->Output("I");