<?php
require('classes/fpdf.php');
require('classes/db.class.php');

$db = new db();
$where_data = str_replace('|', "'", $_POST['where_data']);
$batches_res = $db->exec_query('batches', ['*'], '', '', '', '', $where_data, 'ORDER BY record_id DESC');

$total = 0;

while ($batches = $batches_res->fetch_assoc()) {

    $weight = $batches['weight'] + $weight;

    $total++;
}

$pdf = new FPDF();
$pdf->AliasNbPages();



$pdf->AddPage('L');

$pdf->Image('images/logo_1.png', 250, 5, 40);
// Arial bold 15
$pdf->SetFont('Arial', 'B', 8);
// Move to the right
$pdf->Cell(20);
$pdf->Cell(60, -4, '', 0, 1, 'C');
$pdf->Cell(-3);

$table_top = [50, 28, 16, 65, 30, 55, 30, 20, 25, 20, 20, 20];
// Title
$pdf->SetFont('Arial', 'B', 22);
$pdf->Cell($table_top[0], 30, 'BATCH RECEIVED REPORT', 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 8);

$pdf->Cell(-2);

$pdf->Cell($table_top[2], 7, 'TOTAL', "LBR", 0, 'L');
$pdf->Cell($table_top[8], 7, 'TOTAL WEIGHT', "LBR", 1, 'L');

$pdf->Cell(-2);
$pdf->Cell($table_top[2], 5, $total, "LBR", 0, 'L');
$pdf->Cell($table_top[8], 5, $weight, "LBR", 1, 'L');

$pdf->Cell(-2);
$pdf->Cell($table_top[1], 15, "", 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 22);
$pdf->Cell($table_top[0], 15, 'BATCHES', 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 8);

$pdf->Cell(-2);
$pdf->Cell($table_top[2], 5, "BATCH ID", "LBR", 0, 'L');
$pdf->Cell($table_top[2], 5, "PV", "LBR", 0, 'l');
$pdf->Cell($table_top[2], 5, "FFA", "LBR", 0, 'l');
$pdf->Cell($table_top[2], 5, "VARIETY", "LBR", 0, 'l');
$pdf->Cell($table_top[2], 5, "WEIGHT", "LBR", 0, 'l');
$pdf->Cell($table_top[2], 5, "SIZE", "LBR", 0, 'l');
$pdf->Cell($table_top[4], 5, "SOUND NOT SOUND", "LBR", 0, 'l');
$pdf->Cell($table_top[2], 5, "MOLD", "LBR", 0, 'l');
$pdf->Cell($table_top[7], 5, "MOISTURE", "LBR", 0, 'l');
$pdf->Cell($table_top[6], 5, "CLIENTS", "LB", 0, 'l');
$pdf->Cell($table_top[2], 5, "SHELL", "LBR", 0, 'l');
$pdf->Cell($table_top[4], 5, "DATE", "LBR", 1, 'l');

$batches_res = $db->exec_query('batches', ['*'], '', '', '', '', $where_data, 'ORDER BY record_id DESC');

while ($batch = $batches_res->fetch_assoc()) {

    $variety_res = $db->exec_query('variety', ['*'], '', '', '', '', "record_id = {$batch['variety_id']}");
    $variety = $variety_res->fetch_assoc();

    $clients_res = $db->exec_query('clients', ['*'], '', '', '', '', "record_id = {$batch['client_id']}");
    $clients = $clients_res->fetch_assoc();

    $pdf->Cell(-2);
    $pdf->Cell($table_top[2], 6, "BAR {$batch['record_id']}", "LBR", 0, 'L');
    $pdf->Cell($table_top[2], 6, $batch['pv'], "LBR", 0, 'l');
    $pdf->Cell($table_top[2], 6, $batch['ffa'], "LBR", 0, 'l');
    $pdf->Cell($table_top[2], 6, $variety['name'], "LBR", 0, 'l');
    $pdf->Cell($table_top[2], 6, $batch['weight'], "LBR", 0, 'l');
    $pdf->Cell($table_top[2], 6, $batch['size'], "LBR", 0, 'l');
    $pdf->Cell($table_top[4], 6, $batch['sound_not_sound'], "LBR", 0, 'l');
    $pdf->Cell($table_top[2], 6, $batch['mold'], "LBR", 0, 'l');
    $pdf->Cell($table_top[7], 6, "{$batch['moisture']} %", "LBR", 0, 'l');
    $pdf->Cell($table_top[6], 6, $clients['client_name'], "LBR", 0, 'l');
    $pdf->Cell($table_top[2], 6, $batch['shell'], "LBR", 0, 'l');
    $pdf->Cell($table_top[4], 6, $batch['date'], "LBR", 1, 'l');
}

$pdf->Output("I");